diff options
-rw-r--r-- | core/events.lua | 8 | ||||
-rw-r--r-- | doc/14_Appendix.md | 14 | ||||
-rw-r--r-- | init.lua | 7 | ||||
-rw-r--r-- | modules/textadept/bookmarks.lua | 1 | ||||
-rw-r--r-- | modules/textadept/editing.lua | 1 | ||||
-rw-r--r-- | modules/textadept/file_types.lua | 1 | ||||
-rw-r--r-- | modules/textadept/init.lua | 8 | ||||
-rw-r--r-- | modules/textadept/keys.lua | 16 | ||||
-rw-r--r-- | modules/textadept/run.lua | 1 | ||||
-rw-r--r-- | modules/textadept/snippets.lua | 7 | ||||
-rw-r--r-- | src/textadept.c | 17 |
11 files changed, 43 insertions, 38 deletions
diff --git a/core/events.lua b/core/events.lua index 493fae6a..658d011b 100644 --- a/core/events.lua +++ b/core/events.lua @@ -164,6 +164,8 @@ local M = {} -- Arguments: -- -- * _`position`_: The position in the buffer unclicked. +-- @field INITIALIZED (string) +-- Emitted after Textadept finishes initializing. -- @field KEYPRESS (string) -- Emitted when pressing a key. -- If any handler returns `true`, the key is not inserted into the buffer. @@ -367,9 +369,9 @@ for _, n in pairs(scnotifications) do M[n[1]:upper()] = n[1] end local ta_events = { 'appleevent_odoc', 'buffer_after_switch', 'buffer_before_switch', 'buffer_deleted', 'buffer_new', 'command_entry_command', - 'command_entry_keypress', 'error', 'find', 'keypress', 'menu_clicked', 'quit', - 'replace', 'replace_all', 'reset_after', 'reset_before', 'view_after_switch', - 'view_before_switch', 'view_new' + 'command_entry_keypress', 'error', 'find', 'initialized', 'keypress', + 'menu_clicked', 'quit', 'replace', 'replace_all', 'reset_after', + 'reset_before', 'view_after_switch', 'view_before_switch', 'view_new' } for _, e in pairs(ta_events) do M[e:upper()] = e end diff --git a/doc/14_Appendix.md b/doc/14_Appendix.md index a586dd27..663be78d 100644 --- a/doc/14_Appendix.md +++ b/doc/14_Appendix.md @@ -158,7 +158,7 @@ buffer\_new() |Renamed |\_G.[buffer.new()][] **_M.textadept** |Renamed |[textadept][] filter\_through |Removed |N/A filter\_through.filter\_through() |Renamed |editing.[filter\_through()][] -mime\_types |Renamed |[file\_types][]<sup>†</sup> +mime\_types |Renamed |[file\_types][]<sup>+</sup> **_M.textadept.bookmark** | | N/A |New |[goto\_mark()][] MARK\_BOOKMARK\_COLOR |Renamed |[BOOKMARK\_COLOR][] @@ -175,8 +175,9 @@ contextmenu |Removed |N/A **_M.textadept.run** | | MARK\_ERROR\_BACK |Renamed |[ERROR\_COLOR][] **_M.textadept.snapopen** |Removed |N/A -open |Changed |\_G.[io.snapopen()][]<sup>‡</sup> +open |Changed |\_G.[io.snapopen()][]<sup>†</sup> **events** | | +N/A |New |[INITIALIZED][]<sup>‡</sup> handlers |Removed |N/A **gui** |Renamed |[ui][] N/A |New |[maximized][] @@ -187,9 +188,13 @@ try\_encodings |Renamed |[encodings][] <sup>\*</sup>`arg` is `nil` when resetting. -<sup>†</sup>Removed *mime_types.conf* files. Interact with Lua tables directly. +<sup>+</sup>Removed *mime_types.conf* files. Interact with Lua tables directly. -<sup>‡</sup>Changed arguments too. +<sup>†</sup>Changed arguments too. + +<sup>‡</sup>Custom menus and key bindings should take advantage of this since +not all buffer functions are available at the `require()` stage. See +*modules/textadept/init.lua* for an example. [buffer.new()]: api/buffer.html#new [textadept]: api/textadept.html @@ -202,6 +207,7 @@ try\_encodings |Renamed |[encodings][] [select\_enclosed()]: api/textadept.editing.html#select_enclosed [ERROR\_COLOR]: api/textadept.run.html#ERROR_COLOR [io.snapopen()]: api/io.html#snapopen +[INITIALIZED]: api/events.html#INITIALIZED [ui]: api/ui.html [maximized]: api/ui.html#maximized [goto\_file\_found()]: api/ui.find.html#goto_file_found @@ -13,4 +13,9 @@ textadept = require('textadept') local ok, err = pcall(dofile, _USERHOME..'/init.lua') if not ok and lfs.attributes(_USERHOME..'/init.lua') then ui.print(err) end -if arg then args.process(arg) end +if arg then + events.emit(events.BUFFER_NEW) -- for the first buffer + events.emit(events.VIEW_NEW) -- for the first view + args.process(arg) +end +events.emit(events.INITIALIZED) diff --git a/modules/textadept/bookmarks.lua b/modules/textadept/bookmarks.lua index 852c0df9..4e2d7b03 100644 --- a/modules/textadept/bookmarks.lua +++ b/modules/textadept/bookmarks.lua @@ -73,7 +73,6 @@ local function set_bookmark_properties() if CURSES then buffer:marker_define(MARK_BOOKMARK, CURSES_MARK) end buffer.marker_back[MARK_BOOKMARK] = buffer.property_int[M.BOOKMARK_COLOR] end -if buffer then set_bookmark_properties() end events.connect(events.VIEW_NEW, set_bookmark_properties) return M diff --git a/modules/textadept/editing.lua b/modules/textadept/editing.lua index 670eecec..36dee287 100644 --- a/modules/textadept/editing.lua +++ b/modules/textadept/editing.lua @@ -509,7 +509,6 @@ local function set_highlight_properties() buffer.indic_alpha[INDIC_HIGHLIGHT] = 255 if not CURSES then buffer.indic_under[INDIC_HIGHLIGHT] = true end end -if buffer then set_highlight_properties() end events.connect(events.VIEW_NEW, set_highlight_properties) --- diff --git a/modules/textadept/file_types.lua b/modules/textadept/file_types.lua index f28e9d29..d4f6df57 100644 --- a/modules/textadept/file_types.lua +++ b/modules/textadept/file_types.lua @@ -111,7 +111,6 @@ local function set_lexer_functions() buffer.get_style_name = get_style_name end events.connect(events.BUFFER_NEW, set_lexer_functions, 1) -set_lexer_functions() -- for the first buffer -- Auto-detect lexer on file open or save as. events.connect(events.FILE_OPENED, function() buffer:set_lexer() end) diff --git a/modules/textadept/init.lua b/modules/textadept/init.lua index 9dd072b7..b3049a49 100644 --- a/modules/textadept/init.lua +++ b/modules/textadept/init.lua @@ -19,8 +19,10 @@ M.run = require('textadept.run') M.session = require('textadept.session') M.snippets = require('textadept.snippets') --- These need to be loaded last. -M.keys = require('textadept.keys') -M.menu = require('textadept.menu') +events.connect(events.INITIALIZED, function() + -- These need to be loaded last. + M.keys = require('textadept.keys') + M.menu = require('textadept.menu') +end) return M diff --git a/modules/textadept/keys.lua b/modules/textadept/keys.lua index 95f182ab..27f44c86 100644 --- a/modules/textadept/keys.lua +++ b/modules/textadept/keys.lua @@ -289,6 +289,11 @@ M.utils = { buffer:cut() end } + +local keys, buffer, view = keys, buffer, view +local editing, utils = textadept.editing, M.utils +local OSX, CURSES = OSX, CURSES + -- The following buffer functions need to be constantized in order for menu -- items to identify the key associated with the functions. local menu_buffer_functions = { @@ -296,16 +301,7 @@ local menu_buffer_functions = { 'select_all', 'upper_case', 'lower_case', 'move_selected_lines_up', 'move_selected_lines_down', 'zoom_in', 'zoom_out', 'colourise' } -local function constantize_menu_buffer_functions() - local buffer = buffer - for _, f in ipairs(menu_buffer_functions) do buffer[f] = buffer[f] end -end -events.connect(events.BUFFER_NEW, constantize_menu_buffer_functions) -constantize_menu_buffer_functions() -- for the first buffer - -local keys, buffer, view = keys, buffer, view -local editing, utils = textadept.editing, M.utils -local OSX, CURSES = OSX, CURSES +for _, f in ipairs(menu_buffer_functions) do buffer[f] = buffer[f] end -- Windows and Linux key bindings. -- diff --git a/modules/textadept/run.lua b/modules/textadept/run.lua index 6a66e8d3..e253a6c9 100644 --- a/modules/textadept/run.lua +++ b/modules/textadept/run.lua @@ -250,7 +250,6 @@ local function set_error_properties() if CURSES then buffer:marker_define(MARK_ERROR, CURSES_MARK) end buffer.marker_back[MARK_ERROR] = buffer.property_int[M.ERROR_COLOR] end -if buffer then set_error_properties() end events.connect(events.VIEW_NEW, set_error_properties) return M diff --git a/modules/textadept/snippets.lua b/modules/textadept/snippets.lua index 76c36223..4827c8c8 100644 --- a/modules/textadept/snippets.lua +++ b/modules/textadept/snippets.lua @@ -368,10 +368,9 @@ M._snippet_mt = { end, } -local INDIC_HIDDEN = buffer.INDIC_HIDDEN -if buffer then buffer.indic_style[INDIC_SNIPPET] = INDIC_HIDDEN end -events.connect(events.VIEW_NEW, - function() buffer.indic_style[INDIC_SNIPPET] = INDIC_HIDDEN end) +events.connect(events.VIEW_NEW, function() + buffer.indic_style[INDIC_SNIPPET] = buffer.INDIC_HIDDEN +end) --- -- Map of snippet triggers with their snippet text, with language-specific diff --git a/src/textadept.c b/src/textadept.c index a7136f44..355ad03d 100644 --- a/src/textadept.c +++ b/src/textadept.c @@ -127,7 +127,7 @@ static lua_State *lua; #if CURSES static int quit; #endif -static int closing; +static int initing, closing; static int tVOID = 0, tINT = 1, tLENGTH = 2, /*tPOSITION = 3, tCOLOUR = 4,*/ tBOOL = 5, tKEYMOD = 6, tSTRING = 7, tSTRINGRESULT = 8; static int lL_init(lua_State *, int, char **, int); @@ -784,10 +784,10 @@ static void l_pushdoc(lua_State *L, sptr_t doc) { * @param view The Scintilla view to focus. */ static void goto_view(Scintilla *view) { - if (!closing) lL_event(lua, "view_before_switch", -1); + if (!initing && !closing) lL_event(lua, "view_before_switch", -1); l_setglobalview(lua, focused_view = view); l_setglobaldoc(lua, SS(view, SCI_GETDOCPOINTER, 0, 0)); - if (!closing) lL_event(lua, "view_after_switch", -1); + if (!initing && !closing) lL_event(lua, "view_after_switch", -1); } /** `ui.goto_view()` Lua function. */ @@ -1346,7 +1346,7 @@ static void new_buffer(sptr_t doc) { SS(focused_view, SCI_ADDREFDOCUMENT, 0, doc); } l_setglobaldoc(lua, doc); - lL_event(lua, "buffer_new", -1); + if (!initing) lL_event(lua, "buffer_new", -1); } /** `_G.quit()` Lua function. */ @@ -1865,9 +1865,9 @@ static int lview_goto_buffer(lua_State *L) { // to handlers will not throw 'indexed buffer is not the focused one' error. int switch_focus = (view != focused_view); if (switch_focus) SS(view, SCI_SETFOCUS, TRUE, 0); - lL_event(L, "buffer_before_switch", -1); + if (!initing) lL_event(L, "buffer_before_switch", -1); lL_gotodoc(L, view, n, relative); - lL_event(L, "buffer_after_switch", -1); + if (!initing) lL_event(L, "buffer_after_switch", -1); if (switch_focus) SS(view, SCI_SETFOCUS, FALSE, 0), focus_view(prev_view); return 0; } @@ -2009,7 +2009,7 @@ static Scintilla *new_view(sptr_t doc) { SS(view, SCI_SETDOCPOINTER, 0, doc); l_setglobaldoc(lua, doc); } else new_buffer(SS(view, SCI_GETDOCPOINTER, 0, 0)); - lL_event(lua, "view_new", -1); + if (!initing) lL_event(lua, "view_new", -1); return view; } @@ -2320,8 +2320,7 @@ int main(int argc, char **argv) { setlocale(LC_COLLATE, "C"), setlocale(LC_NUMERIC, "C"); if (lua = luaL_newstate(), !lL_init(lua, argc, argv, FALSE)) return 1; - new_window(); - lL_dofile(lua, "init.lua"); + initing = TRUE, new_window(), lL_dofile(lua, "init.lua"), initing = FALSE; #if (__APPLE__ && !CURSES) gtkosx_application_ready(osxapp); #endif |