diff options
-rw-r--r-- | core/events.lua (renamed from core/handlers.lua) | 133 | ||||
-rw-r--r-- | core/ext/mime_types.lua | 12 | ||||
-rw-r--r-- | core/ext/pm/buffer_browser.lua | 12 | ||||
-rw-r--r-- | core/ext/pm/ctags_browser.lua | 10 | ||||
-rw-r--r-- | core/ext/pm/macro_browser.lua | 6 | ||||
-rw-r--r-- | core/file_io.lua | 8 | ||||
-rw-r--r-- | core/init.lua | 2 | ||||
-rw-r--r-- | modules/cpp/commands.lua | 4 | ||||
-rw-r--r-- | modules/lua/commands.lua | 1 | ||||
-rw-r--r-- | modules/textadept/editing.lua | 4 | ||||
-rw-r--r-- | modules/textadept/key_commands.lua | 8 | ||||
-rw-r--r-- | modules/textadept/keys.lua | 4 | ||||
-rw-r--r-- | modules/textadept/macros.lua | 6 | ||||
-rw-r--r-- | src/lua_interface.c | 10 | ||||
-rw-r--r-- | src/textadept.c | 2 |
15 files changed, 136 insertions, 86 deletions
diff --git a/core/handlers.lua b/core/events.lua index 2a343dd3..9c8cbdb4 100644 --- a/core/handlers.lua +++ b/core/events.lua @@ -1,56 +1,111 @@ -- Copyright 2007 Mitchell mitchell<att>caladbolg.net. See LICENSE. ---- Handler module that handles Scintilla and Textadept notifications/events. -module('textadept.handlers', package.seeall) +--- +-- Module that handles Scintilla and Textadept notifications/events. +-- Most of Textadept's functionality comes through handlers. Scintilla +-- notifications, Textadept's own events, and user-defined events can all be +-- handled. +-- +-- @usage +-- Each event can have multiple handlers, which are simply Lua functions that +-- are called in the sequence they are added as handler functions. Sometimes it +-- is useful to have a handler run under a specific condition(s). If this is the +-- case, place a conditional in the function that returns if it isn't met. +-- +-- For users creating their own events, one does not have to be explicitly +-- defined. A handler can simply be added for an event name, and 'handle'd when +-- necessary. +-- +-- Scintilla notifications: +-- char_added +-- ch: the (integer) character added. +-- save_point_reached +-- save_point_left +-- double_click +-- position: the position of the beginning of the line clicked. +-- line: the line clicked. +-- update_ui +-- macro_record +-- message: the SCI_* message. +-- wParam: wParam in SCI_*. +-- lParam: lParam in SCI_*. +-- margin_click +-- margin: the margin number. +-- modifiers: mouse modifiers. +-- position: the position of the beginning of the line at the point clicked. +-- user_list_selection +-- wParam: the user list ID. +-- text: the text of the selected item. +-- uri_dropped: +-- text: the URI dropped. +-- call_tip_click +-- position: 1 or 2 if the up or down arrow was clicked; 0 otherwise. +-- auto_c_selection +-- lParam: the start position of the word being completed. +-- text: the text of the selected item. +-- +-- Textadept events: +-- buffer_new +-- buffer_deleted +-- buffer_switch +-- view_new +-- view_switch +-- quit +-- keypress +-- code: the key code. +-- shift: flag indicating whether or not shift is pressed. +-- control: flag indicating whether or not control is pressed. +-- alt: flag indicating whether or not alt is pressed. +module('textadept.events', package.seeall) -local handlers = textadept.handlers +local events = textadept.events --- --- Adds a function to a handler. --- Every handler has a table of functions associated with it that are run when --- the handler is called by Textadept. --- @param handler The string handler name. +-- Adds a handler function to an event. +-- @param event The string event name. -- @param f The Lua function to add. -- @param index Optional index to insert the handler into. -function add_handler_function(handler, f, index) - local plural = handler..'s' - if not handlers[plural] then handlers[plural] = {} end - local funcs = handlers[plural] +function add_handler(event, f, index) + local plural = event..'s' + if not events[plural] then events[plural] = {} end + local handlers = events[plural] if index then - table.insert(funcs, index, f) + table.insert(handlers, index, f) else - funcs[#funcs+ 1] = f + handlers[#handlers + 1] = f end end --- --- Calls every function added to a handler in sequence. --- If true or false is returned by any function, the iteration ceases. --- @param handler The string handler name. +-- Calls every handler function added to an event in sequence. +-- If true or false is returned by any handler, the iteration ceases. Normally +-- this function is called by the system when necessary, but it can be called +-- in scripts to handle user-defined events. +-- @param event The string event name. -- @param ... Arguments to the handler. -function handle(handler, ...) - local plural = handler..'s' - if not handlers[plural] then return end - local funcs = handlers[plural] - for _, f in ipairs(funcs) do +function handle(event, ...) + local plural = event..'s' + local handlers = events[plural] + if not handlers then return end + for _, f in ipairs(handlers) do local result = f( unpack{...} ) if result == true or result == false then return result end end end --- --- Reloads handlers. --- Clears each table of handlers for each handler function and reloads this --- module to reset to the default handlers. +-- Reloads event handlers. +-- Clears each event's table of handlers and reloads this module to reset to the +-- default handlers. function reload() - package.loaded['handlers'] = nil - for handler in pairs(handlers) do - if handlers[handler..'s'] then handlers[handler..'s'] = nil end + package.loaded['events'] = nil + for handler in pairs(events) do + if events[handler..'s'] then events[handler..'s'] = nil end end - require 'handlers' + require 'events' end --- Signals. +-- Textadept events. function buffer_new() return handle('buffer_new') end @@ -134,7 +189,7 @@ end -- Default handlers to follow. -add_handler_function('char_added', +add_handler('char_added', function(char) -- auto-indent on return if char ~= '\n' then return end local buffer = buffer @@ -172,13 +227,13 @@ local function set_title(buffer) textadept.title = filename:match('[^/]+$')..d..'Textadept ('..filename..')' end -add_handler_function('save_point_reached', +add_handler('save_point_reached', function() -- changes Textadept title to show 'clean' buffer buffer.dirty = false set_title(buffer) end) -add_handler_function('save_point_left', +add_handler('save_point_left', function() -- changes Textadept title to show 'dirty' buffer buffer.dirty = true set_title(buffer) @@ -211,7 +266,7 @@ local function match_brace(current_pos) return false end -add_handler_function('update_ui', +add_handler('update_ui', function() -- highlights matching braces local buffer = buffer if not match_brace(buffer.current_pos) then buffer:brace_bad_light(-1) end @@ -219,7 +274,7 @@ add_handler_function('update_ui', local docstatusbar_text = "Line: %d/%d Col: %d Lexer: %s %s %s %s" -add_handler_function('update_ui', +add_handler('update_ui', function() -- sets docstatusbar text local buffer = buffer local pos = buffer.current_pos @@ -233,14 +288,14 @@ add_handler_function('update_ui', docstatusbar_text:format(line, max, col, lexer, mode, eol, tabs) end) -add_handler_function('margin_click', +add_handler('margin_click', function(margin, modifiers, position) -- toggles folding local buffer = buffer local line = buffer:line_from_position(position) buffer:toggle_fold(line) end) -add_handler_function('buffer_new', +add_handler('buffer_new', function() -- set additional buffer functions local buffer, textadept = buffer, textadept buffer.save = textadept.io.save @@ -249,19 +304,19 @@ add_handler_function('buffer_new', set_title(buffer) end) -add_handler_function('buffer_switch', +add_handler('buffer_switch', function() -- updates titlebar and statusbar set_title(buffer) update_ui() end) -add_handler_function('view_switch', +add_handler('view_switch', function() -- updates titlebar and statusbar set_title(buffer) update_ui() end) -add_handler_function('quit', +add_handler('quit', function() -- prompts for confirmation if any buffers are dirty; saves session local any = false local list = 'The following buffers are unsaved:\n\n' diff --git a/core/ext/mime_types.lua b/core/ext/mime_types.lua index ec4ef808..912faf00 100644 --- a/core/ext/mime_types.lua +++ b/core/ext/mime_types.lua @@ -94,7 +94,7 @@ local function load_language_module_from_filename(filename) if ret then _m[lang].set_buffer_properties() elseif not ret and not err:match("^module '"..lang.."' not found:") then - textadept.handlers.error(err) + textadept.events.error(err) end end end @@ -119,8 +119,8 @@ local function handle_switch() end end -local handlers = textadept.handlers -handlers.add_handler_function('file_opened', handle_new) -handlers.add_handler_function('file_saved_as', handle_new) -handlers.add_handler_function('buffer_switch', handle_switch) -handlers.add_handler_function('view_new', handle_switch) +local events = textadept.events +events.add_handler('file_opened', handle_new) +events.add_handler('file_saved_as', handle_new) +events.add_handler('buffer_switch', handle_switch) +events.add_handler('view_new', handle_switch) diff --git a/core/ext/pm/buffer_browser.lua b/core/ext/pm/buffer_browser.lua index 1c2fb33c..47cc6de6 100644 --- a/core/ext/pm/buffer_browser.lua +++ b/core/ext/pm/buffer_browser.lua @@ -46,12 +46,12 @@ function perform_menu_action(menu_item, selected_item) textadept.pm.activate() end -local add_handler_function = textadept.handlers.add_handler_function +local add_handler = textadept.events.add_handler local function update_view() if matches(textadept.pm.entry_text) then textadept.pm.activate() end end -add_handler_function('file_opened', update_view) -add_handler_function('buffer_new', update_view) -add_handler_function('buffer_deleted', update_view) -add_handler_function('save_point_reached', update_view) -add_handler_function('save_point_left', update_view) +add_handler('file_opened', update_view) +add_handler('buffer_new', update_view) +add_handler('buffer_deleted', update_view) +add_handler('save_point_reached', update_view) +add_handler('save_point_left', update_view) diff --git a/core/ext/pm/ctags_browser.lua b/core/ext/pm/ctags_browser.lua index 842dce9e..5166b423 100644 --- a/core/ext/pm/ctags_browser.lua +++ b/core/ext/pm/ctags_browser.lua @@ -230,7 +230,7 @@ function perform_menu_action(menu_item, selected_item) end -local add_handler_function = textadept.handlers.add_handler_function +local add_handler = textadept.events.add_handler local function update_view() if matches(textadept.pm.entry_text) then if buffer.filename then @@ -240,7 +240,7 @@ local function update_view() end end end -add_handler_function('file_opened', update_view) -add_handler_function('buffer_deleted', update_view) -add_handler_function('buffer_switch', update_view) -add_handler_function('save_point_reached', update_view) +add_handler('file_opened', update_view) +add_handler('buffer_deleted', update_view) +add_handler('buffer_switch', update_view) +add_handler('save_point_reached', update_view) diff --git a/core/ext/pm/macro_browser.lua b/core/ext/pm/macro_browser.lua index 3fc38aca..de8af373 100644 --- a/core/ext/pm/macro_browser.lua +++ b/core/ext/pm/macro_browser.lua @@ -35,9 +35,9 @@ function perform_menu_action(menu_item, selected_item) textadept.pm.activate() end -local add_handler_function = textadept.handlers.add_handler_function +local add_handler = textadept.events.add_handler local function update_view() if matches(textadept.pm.entry_text) then textadept.pm.activate() end end -add_handler_function('macro_saved', update_view) -add_handler_function('macro_deleted', update_view) +add_handler('macro_saved', update_view) +add_handler('macro_deleted', update_view) diff --git a/core/file_io.lua b/core/file_io.lua index 958a03cc..cdc09af0 100644 --- a/core/file_io.lua +++ b/core/file_io.lua @@ -5,7 +5,7 @@ -- Opens and saves files and sessions and reads API files. module('textadept.io', package.seeall) -local handlers = textadept.handlers +local events = textadept.events --- -- [Local function] Opens a file or goes to its already open buffer. @@ -24,7 +24,7 @@ local function open_helper(filename) end buffer.filename = filename buffer:set_save_point() - handlers.handle('file_opened', filename) + events.handle('file_opened', filename) end --- @@ -58,7 +58,7 @@ function save(buffer) f:close() buffer:set_save_point() else - handlers.error(err) + events.error(err) end end @@ -77,7 +77,7 @@ function save_as(buffer, filename) if #filename > 0 then buffer.filename = filename:sub(1, -2) -- chomp buffer:save() - handlers.handle('file_saved_as', filename) + events.handle('file_saved_as', filename) end end diff --git a/core/init.lua b/core/init.lua index 297495e6..f699b8e0 100644 --- a/core/init.lua +++ b/core/init.lua @@ -17,5 +17,5 @@ end package.path = package.path..';'.._HOME..'/core/?.lua' require 'iface' -require 'handlers' +require 'events' require 'file_io' diff --git a/modules/cpp/commands.lua b/modules/cpp/commands.lua index 472ca130..62c06be2 100644 --- a/modules/cpp/commands.lua +++ b/modules/cpp/commands.lua @@ -8,7 +8,6 @@ module('_m.cpp.commands', package.seeall) local keys = _G.keys if type(keys) == 'table' then local m_editing = _m.textadept.editing - local m_handlers = textadept.handlers keys.cpp = { al = { textadept.io.open, _HOME..'/modules/cpp/init.lua' }, ['s\n'] = { function() @@ -18,10 +17,7 @@ if type(keys) == 'table' then end }, cq = { m_editing.block_comment, '//~' }, ['('] = { function() ---~ buffer.word_chars = ---~ '_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' m_editing.show_call_tip(_m.cpp.api, true) ---~ buffer:set_chars_default() return false end }, } diff --git a/modules/lua/commands.lua b/modules/lua/commands.lua index f3dbe333..2b59f171 100644 --- a/modules/lua/commands.lua +++ b/modules/lua/commands.lua @@ -71,7 +71,6 @@ end local keys = _G.keys if type(keys) == 'table' then local m_editing = _m.textadept.editing - local m_handlers = textadept.handlers keys.lua = { al = { textadept.io.open, _HOME..'/modules/lua/init.lua' }, ac = { diff --git a/modules/textadept/editing.lua b/modules/textadept/editing.lua index 282d5ad3..d15ebb9b 100644 --- a/modules/textadept/editing.lua +++ b/modules/textadept/editing.lua @@ -55,7 +55,7 @@ local enclosure = { single_tag = { left = '<', right = ' />' } } -textadept.handlers.add_handler_function('char_added', +textadept.events.add_handler('char_added', function(c) -- matches characters specified in char_matches if char_matches[c] then buffer:insert_text( -1, char_matches[c] ) @@ -154,7 +154,7 @@ function show_call_tip(api, start) buffer:call_tip_show(current_call_tip.start_pos, call_tip) end -textadept.handlers.add_handler_function('call_tip_click', +textadept.events.add_handler('call_tip_click', function(position) -- display the next or previous call tip if not buffer:call_tip_active() then return end if position == 1 and current_call_tip.num > 1 then diff --git a/modules/textadept/key_commands.lua b/modules/textadept/key_commands.lua index 7493a6ec..2072795f 100644 --- a/modules/textadept/key_commands.lua +++ b/modules/textadept/key_commands.lua @@ -143,9 +143,9 @@ keys.ap = { 'goto_buffer', v, -1, false } keys.can = { textadept.goto_view, 1, false } keys.cap = { textadept.goto_view, -1, false } -local m_handlers = textadept.handlers -keys.cab = { m_handlers.handle, 'call_tip_click', 1 } -keys.caf = { m_handlers.handle, 'call_tip_click', 2 } +local m_events = textadept.events +keys.cab = { m_events.handle, 'call_tip_click', 1 } +keys.caf = { m_events.handle, 'call_tip_click', 2 } keys.cs = { textadept.find.focus } keys['c\t'] = { textadept.pm.focus } @@ -180,7 +180,7 @@ local function toggle_setting(setting) elseif type(state) == 'number' then buffer[setting] = buffer[setting] == 0 and 1 or 0 end - textadept.handlers.update_ui() -- for updating statusbar + textadept.events.update_ui() -- for updating statusbar end keys.ct.v = {} -- view chain diff --git a/modules/textadept/keys.lua b/modules/textadept/keys.lua index 93d089a9..996cdabf 100644 --- a/modules/textadept/keys.lua +++ b/modules/textadept/keys.lua @@ -159,7 +159,7 @@ local function keypress(code, shift, control, alt) local ret, retval = pcall( func, unpack(args) ) if ret then if type(retval) == 'boolean' then return retval end - else textadept.handlers.error(retval) end -- error + else textadept.events.error(retval) end -- error end return true else @@ -175,7 +175,7 @@ local function keypress(code, shift, control, alt) else return true end end end -textadept.handlers.add_handler_function('keypress', keypress, 1) +textadept.events.add_handler('keypress', keypress, 1) --- -- [Local function] Tries to get a key command based on the lexer and current diff --git a/modules/textadept/macros.lua b/modules/textadept/macros.lua index 0d81f105..c97a372b 100644 --- a/modules/textadept/macros.lua +++ b/modules/textadept/macros.lua @@ -38,7 +38,7 @@ local function macro_notification(msg, wParam, lParam) textadept.statusbar_text = 'Macro recording' end end -textadept.handlers.add_handler_function('macro_record', macro_notification) +textadept.events.add_handler('macro_record', macro_notification) --- -- Starts recording a macro. @@ -81,7 +81,7 @@ function stop_recording() list[macro_name] = current save() textadept.statusbar_text = 'Macro saved' - textadept.handlers.handle('macro_saved') + textadept.events.handle('macro_saved') else textadept.statusbar_text = 'Macro not saved' end @@ -124,7 +124,7 @@ function delete(macro_name) if list[macro_name] then list[macro_name] = nil save() - textadept.handlers.handle('macro_deleted') + textadept.events.handle('macro_deleted') end end diff --git a/src/lua_interface.c b/src/lua_interface.c index c3f8d99d..0be9362e 100644 --- a/src/lua_interface.c +++ b/src/lua_interface.c @@ -328,7 +328,7 @@ bool l_call_function(int nargs, int retn=0, bool keep_return=false) { // error message is at stack top void l_handle_error(LS *lua, const char *errmsg) { - if (focused_editor && l_is_ta_table_function("handlers", "error")) { + if (focused_editor && l_is_ta_table_function("events", "error")) { l_insert(lua, -1); // shift error message down if (errmsg) lua_pushstring(lua, errmsg); l_call_function(errmsg ? 2 : 1); @@ -340,11 +340,11 @@ void l_handle_error(LS *lua, const char *errmsg) { } bool l_handle_signal(const char *s) { - return l_is_ta_table_function("handlers", s) ? l_call_function(0, 1) : true; + return l_is_ta_table_function("events", s) ? l_call_function(0, 1) : true; } bool l_handle_keypress(int keyval, GdkEventKey *event) { - if (!l_is_ta_table_function("handlers", "keypress")) return false; + if (!l_is_ta_table_function("events", "keypress")) return false; lua_pushinteger(lua, keyval); lua_pushboolean(lua, (event->state & GDK_SHIFT_MASK) > 0 ? 1 : 0); lua_pushboolean(lua, (event->state & GDK_CONTROL_MASK) > 0 ? 1 : 0); @@ -353,7 +353,7 @@ bool l_handle_keypress(int keyval, GdkEventKey *event) { } void l_handle_completion(const char *command) { - if (!l_is_ta_table_function("handlers", + if (!l_is_ta_table_function("events", command ? "show_completions" : "hide_completions")) return; if (command) lua_pushstring(lua, command); l_call_function(command ? 1 : 0); @@ -363,7 +363,7 @@ void l_handle_completion(const char *command) { #define l_scn_str(s, n) { lua_pushstring(lua, s); lua_setfield(lua, -2, n); } void l_handle_scnnotification(SCNotification *n) { - if (!l_is_ta_table_function("handlers", "notification")) return; + if (!l_is_ta_table_function("events", "notification")) return; lua_newtable(lua); l_scn_int(n->nmhdr.code, "code"); l_scn_int(n->position, "position"); diff --git a/src/textadept.c b/src/textadept.c index 416b83fb..98096123 100644 --- a/src/textadept.c +++ b/src/textadept.c @@ -244,7 +244,7 @@ static bool t_keypress(GtkWidget*, GdkEventKey *event, gpointer) { return l_handle_keypress(event->keyval, event); } -static bool w_keypress(GtkWidget* , GdkEventKey *event, gpointer) { +static bool w_keypress(GtkWidget*, GdkEventKey *event, gpointer) { if (event->keyval == 0xff1b && GTK_WIDGET_VISIBLE(findbox)) { gtk_widget_hide(findbox); gtk_widget_grab_focus(focused_editor); |