From c90910baf4f9c81711b31697aedf40d359491228 Mon Sep 17 00:00:00 2001 From: mitchell <70453897+667e-11@users.noreply.github.com> Date: Wed, 15 Jun 2016 08:51:48 -0400 Subject: Do not auto-load `post_init.lua` files in language modules anymore. Additional language module functionality should be manually loaded by the user. --- doc/manual.md | 32 +++++++++++++++++++++++++------- modules/textadept/file_types.lua | 6 +----- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/doc/manual.md b/doc/manual.md index 4f8a0c73..fd12b37a 100644 --- a/doc/manual.md +++ b/doc/manual.md @@ -1027,20 +1027,26 @@ For example, copying the default Lua language module from *modules/lua/* to *~/.textadept/modules/* results in Textadept loading that module for editing Lua code in place of its own. However, if you make custom changes to that module and upgrade Textadept later, the module may no longer be compatible. Rather than -potentially wasting time merging changes, run custom code independent of a -module in the module's *post_init.lua* file. In this case, instead of copying -the `lua` module and creating an `events.LEXER_LOADED` event handler to use -tabs, simply put the event handler in *~/.textadept/modules/lua/post_init.lua*: +potentially wasting time merging changes, create a new "sub-module" for the +language (e.g. *~/.textadept/modules/lua/mine.lua*) and in +*~/.textadept/init.lua*, `require()` your sub-module from an +`events.LEXER_LOADED` event handler: events.connect(events.LEXER_LOADED, function(lang) - if lang == 'lua' then buffer.use_tabs = true end + if lang == 'lua' then require('lua.mine') end end) -Similarly, use *post_init.lua* to change the module's [compile and run][] -commands, load more Autocompletion tags, and add additional +(Lua's `require()` function will not run code in *mine.lua* more than once.) + +Similarly, you can use your sub-module to change the main language module's +[compile and run][] commands, load more Autocompletion tags, and add additional [key bindings](#Key.Bindings) and [snippets](#Snippet.Preferences) (instead of in *~/.textadept/init.lua*). For example: + -- In ~/.textadept/modules/lua/mine.lua. + events.connect(events.LEXER_LOADED, function(lang) + if lang == 'lua' then buffer.use_tabs = true end + end) textadept.run.run_commands.lua = 'lua5.2' _M.lua.tags[#_M.lua.tags + 1] = '/path/to/my/projects/tags' keys.lua['c\n'] = function() @@ -1884,6 +1890,18 @@ braces |Replaced|[brace\_matches][] [autocomplete\_all\_words]: api.html#textadept.editing.autocomplete_all_words [brace\_matches]: api.html#textadept.editing.brace_matches +#### Language Module Handling Changes + +Textadept 9 no longer auto-loads a *post_init.lua* in language modules. Instead, +it must be loaded manually from an `events.LEXER_LOADED` event. For example: + + events.connect(events.LEXER_LOADED, function() + if lang == 'lua' then require('lua.mine') end + end) + +will load a *~/.textadept/modules/lua/mine.lua* "sub-module" for Lua files. Keep +in mind that Lua's `require()` function will only execute module code once. + ### Textadept 7 to 8 Textadept 8 upgraded its internal copy of Lua from [5.2 to 5.3][]. Nearly all diff --git a/modules/textadept/file_types.lua b/modules/textadept/file_types.lua index fd61e2e2..9d1f4042 100644 --- a/modules/textadept/file_types.lua +++ b/modules/textadept/file_types.lua @@ -68,11 +68,7 @@ local function set_lexer(buffer, lang) buffer:private_lexer_call(SETDIRECTPOINTER, buffer.direct_pointer) buffer:private_lexer_call(SETLEXERLANGUAGE, lang) buffer._lexer = lang - if package.searchpath(lang, package.path) then - _M[lang] = require(lang) - local post_init = lang..'.post_init' - if package.searchpath(post_init, package.path) then require(post_init) end - end + if package.searchpath(lang, package.path) then _M[lang] = require(lang) end if buffer ~= ui.command_entry then events.emit(events.LEXER_LOADED, lang) end local last_line = buffer.first_visible_line + buffer.lines_on_screen buffer:colourise(0, buffer:position_from_line(last_line + 1)) -- cgit v1.2.3