aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/events.lua56
-rw-r--r--core/ext/command_entry.lua6
-rw-r--r--core/ext/find.lua15
-rw-r--r--core/ext/key_commands.lua13
-rw-r--r--core/ext/menu.lua11
-rw-r--r--core/file_io.lua17
-rw-r--r--core/init.lua2
7 files changed, 63 insertions, 57 deletions
diff --git a/core/events.lua b/core/events.lua
index 0ef56b58..900ccbb3 100644
--- a/core/events.lua
+++ b/core/events.lua
@@ -5,7 +5,7 @@ local locale = _G.locale
---
-- Textadept's core event structure and handlers.
-module('textadept.events', package.seeall)
+module('events', package.seeall)
-- Markdown:
-- ## Overview
@@ -143,10 +143,10 @@ module('textadept.events', package.seeall)
-- textadept.print(message)
-- end
--
--- textadept.events.add_handler('my_event', my_event_handler)
--- textadept.events.handle('my_event', 'my message')
+-- events.connect('my_event', my_event_handler)
+-- events.emit('my_event', 'my message')
-local events = textadept.events
+local events = events
---
-- Adds a handler function to an event.
@@ -154,7 +154,7 @@ local events = textadept.events
-- anywhere.
-- @param f The Lua function to add.
-- @param index Optional index to insert the handler into.
-function add_handler(event, f, index)
+function connect(event, f, index)
local plural = event..'s'
if not events[plural] then events[plural] = {} end
local handlers = events[plural]
@@ -173,7 +173,7 @@ end
-- @param event The string event name.
-- @param ... Arguments passed to the handler.
-- @return true or false if any handler explicitly returned such; nil otherwise.
-function handle(event, ...)
+function emit(event, ...)
local plural = event..'s'
local handlers = events[plural]
if not handlers then return end
@@ -183,6 +183,9 @@ function handle(event, ...)
end
end
+local connect = connect
+local emit = emit
+
--- Map of Scintilla notifications to their handlers.
local c = textadept.constants
local scnnotifications = {
@@ -207,13 +210,13 @@ function notification(n)
if f then
local args = { unpack(f, 2) }
for k, v in ipairs(args) do args[k] = n[v] end
- return handle(f[1], unpack(args))
+ return emit(f[1], unpack(args))
end
end
-- Default handlers to follow.
-add_handler('view_new',
+connect('view_new',
function() -- sets default properties for a Scintilla window
local buffer = buffer
local c = textadept.constants
@@ -239,7 +242,7 @@ add_handler('view_new',
end
end)
-add_handler('buffer_new',
+connect('buffer_new',
function() -- sets default properties for a Scintilla document
local function run()
local buffer = buffer
@@ -284,19 +287,19 @@ local function set_title(buffer)
filename)
end
-add_handler('save_point_reached',
+connect('save_point_reached',
function() -- changes Textadept title to show 'clean' buffer
buffer.dirty = false
set_title(buffer)
end)
-add_handler('save_point_left',
+connect('save_point_left',
function() -- changes Textadept title to show 'dirty' buffer
buffer.dirty = true
set_title(buffer)
end)
-add_handler('uri_dropped',
+connect('uri_dropped',
function(utf8_uris)
local lfs = require 'lfs'
for utf8_uri in utf8_uris:gmatch('[^\r\n\f]+') do
@@ -318,7 +321,7 @@ local EOLs = {
locale.STATUS_CR,
locale.STATUS_LF
}
-add_handler('update_ui',
+connect('update_ui',
function() -- sets docstatusbar text
local buffer = buffer
local pos = buffer.current_pos
@@ -333,15 +336,15 @@ add_handler('update_ui',
locale.DOCSTATUSBAR_TEXT:format(line, max, col, lexer, eol, tabs, enc)
end)
-add_handler('margin_click',
+connect('margin_click',
function(margin, modifiers, position) -- toggles folding
local line = buffer:line_from_position(position)
buffer:toggle_fold(line)
end)
-add_handler('buffer_new', function() set_title(buffer) end)
+connect('buffer_new', function() set_title(buffer) end)
-add_handler('buffer_before_switch',
+connect('buffer_before_switch',
function() -- save buffer properties
local buffer = buffer
-- Save view state.
@@ -361,7 +364,7 @@ add_handler('buffer_before_switch',
end
end)
-add_handler('buffer_after_switch',
+connect('buffer_after_switch',
function() -- restore buffer properties
local buffer = buffer
if not buffer._folds then return end
@@ -374,19 +377,19 @@ add_handler('buffer_after_switch',
buffer.first_visible_line)
end)
-add_handler('buffer_after_switch',
+connect('buffer_after_switch',
function() -- updates titlebar and statusbar
set_title(buffer)
- handle('update_ui')
+ emit('update_ui')
end)
-add_handler('view_after_switch',
+connect('view_after_switch',
function() -- updates titlebar and statusbar
set_title(buffer)
- handle('update_ui')
+ emit('update_ui')
end)
-add_handler('quit',
+connect('quit',
function() -- prompts for confirmation if any buffers are dirty
local any = false
local list = {}
@@ -411,10 +414,10 @@ add_handler('quit',
end)
if MAC then
- add_handler('appleevent_odoc',
- function(uri) return handle('uri_dropped', 'file://'..uri) end)
+ connect('appleevent_odoc',
+ function(uri) return emit('uri_dropped', 'file://'..uri) end)
- textadept.events.add_handler('buffer_new',
+ connect('buffer_new',
function()
buffer.paste = function()
local clipboard_text = textadept.clipboard_text
@@ -423,5 +426,4 @@ if MAC then
end)
end
-add_handler('error',
- function(...) textadept._print(locale.ERROR_BUFFER, ...) end)
+connect('error', function(...) textadept._print(locale.ERROR_BUFFER, ...) end)
diff --git a/core/ext/command_entry.lua b/core/ext/command_entry.lua
index 34dfbe13..c29a2180 100644
--- a/core/ext/command_entry.lua
+++ b/core/ext/command_entry.lua
@@ -3,7 +3,7 @@
local textadept = _G.textadept
local locale = _G.locale
-textadept.events.add_handler('command_entry_command',
+events.connect('command_entry_command',
function(command) -- execute a Lua command
local f, err = loadstring(command)
if err then error(err) end
@@ -11,10 +11,10 @@ textadept.events.add_handler('command_entry_command',
f()
end)
-textadept.events.add_handler('command_entry_keypress',
+events.connect('command_entry_keypress',
function(code)
local ce = textadept.command_entry
- local KEYSYMS = textadept.keys.KEYSYMS
+ local KEYSYMS = keys.KEYSYMS
if KEYSYMS[code] == 'esc' then
ce.focus() -- toggle focus to hide
return true
diff --git a/core/ext/find.lua b/core/ext/find.lua
index f8cf27c8..9aebaaee 100644
--- a/core/ext/find.lua
+++ b/core/ext/find.lua
@@ -2,6 +2,7 @@
local textadept = _G.textadept
local locale = _G.locale
+local events = _G.events
local find = textadept.find
local lfs = require 'lfs'
@@ -150,7 +151,7 @@ local function find_(text, next, flags, nowrap, wrapped)
return result
end
-textadept.events.add_handler('find', find_)
+events.connect('find', find_)
-- Finds and selects text incrementally in the current buffer from a start
-- point.
@@ -172,7 +173,7 @@ function find.find_incremental()
textadept.command_entry.focus()
end
-textadept.events.add_handler('command_entry_keypress',
+events.connect('command_entry_keypress',
function(code)
if find.incremental then
if code == 0xff1b then -- escape
@@ -188,7 +189,7 @@ textadept.events.add_handler('command_entry_keypress',
end
end, 1) -- place before command_entry.lua's handler (if necessary)
-textadept.events.add_handler('command_entry_command',
+events.connect('command_entry_command',
function(text) -- 'find next' for incremental search
if find.incremental then
find.incremental_start = buffer.current_pos + 1
@@ -240,7 +241,7 @@ local function replace(rtext)
buffer:goto_pos(buffer.current_pos)
end
end
-textadept.events.add_handler('replace', replace)
+events.connect('replace', replace)
-- Replaces all found text.
-- If any text is selected, all found text in that selection is replaced.
@@ -288,7 +289,7 @@ local function replace_all(ftext, rtext, flags)
string.format(locale.FIND_REPLACEMENTS_MADE, tostring(count))
buffer:end_undo_action()
end
-textadept.events.add_handler('replace_all', replace_all)
+events.connect('replace_all', replace_all)
-- When the user double-clicks a found file, go to the line in the file the text
-- was found at.
@@ -326,7 +327,7 @@ local function goto_file(pos, line_num)
end
end
end
-textadept.events.add_handler('double_click', goto_file)
+events.connect('double_click', goto_file)
-- LuaDoc is in core/.find.lua.
function find.goto_file_in_list(next)
@@ -359,5 +360,5 @@ function find.goto_file_in_list(next)
end
if buffer then buffer:marker_set_back(MARK_FIND, MARK_FIND_COLOR) end
-textadept.events.add_handler('view_new',
+events.connect('view_new',
function() buffer:marker_set_back(MARK_FIND, MARK_FIND_COLOR) end)
diff --git a/core/ext/key_commands.lua b/core/ext/key_commands.lua
index 297cb3a8..93f02713 100644
--- a/core/ext/key_commands.lua
+++ b/core/ext/key_commands.lua
@@ -2,6 +2,7 @@
local textadept = _G.textadept
local locale = _G.locale
+local events = _G.events
---
-- Manages and defines key commands in Textadept.
@@ -246,7 +247,7 @@ if not MAC then
elseif type(state) == 'number' then
buffer[setting] = buffer[setting] == 0 and 1 or 0
end
- t.events.handle('update_ui') -- for updating statusbar
+ events.emit('update_ui') -- for updating statusbar
end
keys.ct.v = {
e = { toggle_setting, 'view_eol' },
@@ -274,7 +275,7 @@ if not MAC then
-- Miscellaneous not in standard menu.
-- Recent files.
local RECENT_FILES = 1
- t.events.add_handler('user_list_selection',
+ events.connect('user_list_selection',
function(type, text)
if type == RECENT_FILES then io.open_file(text) end
end)
@@ -408,7 +409,7 @@ else
elseif type(state) == 'number' then
buffer[setting] = buffer[setting] == 0 and 1 or 0
end
- t.events.handle('update_ui') -- for updating statusbar
+ events.emit('update_ui') -- for updating statusbar
end
keys.at.v = {
e = { toggle_setting, 'view_eol' },
@@ -436,7 +437,7 @@ else
-- Miscellaneous not in standard menu.
-- Recent files.
local RECENT_FILES = 1
- t.events.add_handler('user_list_selection',
+ events.connect('user_list_selection',
function(type, text)
if type == RECENT_FILES then io.open_file(text) end
end)
@@ -589,7 +590,7 @@ local function keypress(code, shift, control, alt)
if ch:find('[%p%d]') and #keychain == 0 then
if buffer.anchor ~= buffer.current_pos then buffer:delete_back() end
buffer:add_text(ch)
- textadept.events.handle('char_added', code)
+ events.emit('char_added', code)
return true
end
end
@@ -649,4 +650,4 @@ local function keypress(code, shift, control, alt)
end
end
end
-textadept.events.add_handler('keypress', keypress, 1)
+events.connect('keypress', keypress, 1)
diff --git a/core/ext/menu.lua b/core/ext/menu.lua
index 7fede4da..ee60bc60 100644
--- a/core/ext/menu.lua
+++ b/core/ext/menu.lua
@@ -2,6 +2,7 @@
local textadept = _G.textadept
local locale = _G.locale
+local events = _G.events
---
-- Provides dynamic menus for Textadept.
@@ -291,7 +292,7 @@ local m_run = _m.textadept.run
local function set_encoding(encoding)
buffer:set_encoding(encoding)
- t.events.handle('update_ui') -- for updating statusbar
+ events.emit('update_ui') -- for updating statusbar
end
local function toggle_setting(setting)
local state = buffer[setting]
@@ -300,17 +301,17 @@ local function toggle_setting(setting)
elseif type(state) == 'number' then
buffer[setting] = buffer[setting] == 0 and 1 or 0
end
- t.events.handle('update_ui') -- for updating statusbar
+ events.emit('update_ui') -- for updating statusbar
end
local function set_eol_mode(mode)
buffer.eol_mode = mode
buffer:convert_eo_ls(mode)
- t.events.handle('update_ui') -- for updating statusbar
+ events.emit('update_ui') -- for updating statusbar
end
local function set_lexer(lexer)
buffer:set_lexer(lexer)
buffer:colourise(0, -1)
- t.events.handle('update_ui') -- for updating statusbar
+ events.emit('update_ui') -- for updating statusbar
end
local function open_webpage(url)
local cmd
@@ -475,7 +476,7 @@ local actions = {
}
-- Most of this handling code comes from keys.lua.
-t.events.add_handler('menu_clicked',
+events.connect('menu_clicked',
function(menu_id)
local active_table = actions[menu_id]
if menu_id >= ID.LEXER_START and menu_id < ID.LEXER_START + 99 then
diff --git a/core/file_io.lua b/core/file_io.lua
index d08907f5..a994b34a 100644
--- a/core/file_io.lua
+++ b/core/file_io.lua
@@ -2,6 +2,7 @@
local textadept = _G.textadept
local locale = _G.locale
+local events = _G.events
---
-- Extends Lua's io package to provide file input/output routines for Textadept.
@@ -30,7 +31,7 @@ module('io', package.seeall)
--
-- Example:
--
--- textadept.events.add_handler('file_opened',
+-- events.connect('file_opened',
-- function(utf8_filename)
-- local filename = textadept.iconv(utf8_filename, _CHARSET, 'UTF-8')
-- local f = io.open(filename, 'rb')
@@ -170,7 +171,7 @@ local function open_helper(utf8_filename)
end
buffer.filename = utf8_filename
buffer:set_save_point()
- textadept.events.handle('file_opened', utf8_filename)
+ events.emit('file_opened', utf8_filename)
for index, file in ipairs(recent_files) do
if file == utf8_filename then
@@ -242,7 +243,7 @@ end
local function save(buffer)
textadept.check_focused_buffer(buffer)
if not buffer.filename then return buffer:save_as() end
- textadept.events.handle('file_before_save', buffer.filename)
+ events.emit('file_before_save', buffer.filename)
local text = buffer:get_text(buffer.length)
if buffer.encoding then
local bom = buffer.encoding_bom or ''
@@ -277,7 +278,7 @@ local function save_as(buffer, utf8_filename)
if #utf8_filename > 0 then
buffer.filename = utf8_filename
buffer:save()
- textadept.events.handle('file_saved_as', utf8_filename)
+ events.emit('file_saved_as', utf8_filename)
end
end
@@ -351,10 +352,10 @@ local function update_modified_file()
end
end
end
-textadept.events.add_handler('buffer_after_switch', update_modified_file)
-textadept.events.add_handler('view_after_switch', update_modified_file)
+events.connect('buffer_after_switch', update_modified_file)
+events.connect('view_after_switch', update_modified_file)
-textadept.events.add_handler('buffer_new',
+events.connect('buffer_new',
function() -- set additional buffer functions
local buffer = buffer
buffer.reload = reload
@@ -365,7 +366,7 @@ textadept.events.add_handler('buffer_new',
buffer.encoding = 'UTF-8'
end)
-textadept.events.add_handler('file_opened',
+events.connect('file_opened',
function(utf8_filename) -- close initial 'Untitled' buffer
local b = textadept.buffers[1]
if #textadept.buffers == 2 and not (b.filename or b._type or b.dirty) then
diff --git a/core/init.lua b/core/init.lua
index f1468202..fcf8e354 100644
--- a/core/init.lua
+++ b/core/init.lua
@@ -64,7 +64,7 @@ function textadept._print(buffer_type, ...)
if not message_buffer then
message_buffer = textadept.new_buffer()
message_buffer._type = buffer_type
- textadept.events.handle('file_opened')
+ events.emit('file_opened')
else
message_view:goto_buffer(message_buffer_index, true)
end