From ac698c5ea71d26e56289a18e664c6f8be1aa56c5 Mon Sep 17 00:00:00 2001 From: mitchell <70453897+667e-11@users.noreply.github.com> Date: Sat, 10 Jan 2009 17:31:21 -0500 Subject: Various improvements to speed and readability of Lua code. Added 'local textadept = _G.textadept' to all Lua modules, themes, etc. Added more locals to core/ext/keys.lua for speed improvement. Reformatted some Lua modules to the earlier standard committed. --- core/events.lua | 15 +++++++++------ core/ext/command_entry.lua | 2 +- core/ext/find.lua | 4 ++-- core/ext/key_commands.lua | 9 +++------ core/ext/key_commands_mac.lua | 9 +++------ core/ext/key_commands_std.lua | 9 +++------ core/ext/keys.lua | 35 ++++++++++++++++++++++++++--------- core/ext/menu.lua | 6 +++++- core/ext/mime_types.lua | 11 ++++++----- core/ext/pm/buffer_browser.lua | 13 +++++++------ core/ext/pm/ctags_browser.lua | 11 ++++++----- core/ext/pm/file_browser.lua | 2 ++ core/ext/pm/find_browser.lua | 2 ++ core/ext/pm/macro_browser.lua | 9 ++++----- core/ext/pm/modules_browser.lua | 3 ++- core/ext/pm/project_browser.lua | 2 ++ core/file_io.lua | 11 +++++------ core/init.lua | 2 ++ 18 files changed, 90 insertions(+), 65 deletions(-) (limited to 'core') diff --git a/core/events.lua b/core/events.lua index afb119e0..fd9a958c 100644 --- a/core/events.lua +++ b/core/events.lua @@ -1,5 +1,7 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + --- -- Module that handles Scintilla and Textadept notifications/events. -- Most of Textadept's functionality comes through handlers. Scintilla @@ -204,7 +206,8 @@ end add_handler('view_new', function() -- sets default properties for a Scintilla window - local c, buffer = textadept.constants, buffer + local buffer = buffer + local c = textadept.constants -- properties buffer.property['textadept.home'] = _HOME @@ -293,7 +296,7 @@ add_handler('view_new', add_handler('buffer_new', function() -- sets default properties for a Scintilla document local function run() - local textadept, buffer = textadept, buffer + local buffer = buffer -- lexer buffer.style_bits = 8 @@ -370,7 +373,7 @@ local title_text = '%s %s Textadept (%s)' -- filename. -- @param buffer The currently focused buffer. local function set_title(buffer) - local buffer, textadept = buffer, textadept + local buffer = buffer local filename = buffer.filename or textadept.locale.UNTITLED local d = buffer.dirty and '*' or '-' textadept.title = @@ -481,7 +484,8 @@ local EOLs = { } add_handler('update_ui', function() -- sets docstatusbar text - local buffer, locale = buffer, textadept.locale + local buffer = buffer + local locale = textadept.locale local pos = buffer.current_pos local line, max = buffer:line_from_position(pos) + 1, buffer.line_count local col = buffer.column[pos] + 1 @@ -503,7 +507,7 @@ add_handler('margin_click', add_handler('buffer_new', function() -- set additional buffer functions - local buffer, textadept = buffer, textadept + local buffer = buffer buffer.reload = textadept.io.reload buffer.save = textadept.io.save buffer.save_as = textadept.io.save_as @@ -558,7 +562,6 @@ end -- @param ... Error strings. function error(...) local function handle_error(...) - local textadept = textadept local error_message = table.concat({...} , '\n') local error_buffer for index, buffer in ipairs(textadept.buffers) do diff --git a/core/ext/command_entry.lua b/core/ext/command_entry.lua index f1bd1820..29e38bb6 100644 --- a/core/ext/command_entry.lua +++ b/core/ext/command_entry.lua @@ -1,5 +1,6 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept local ce = textadept.command_entry --- @@ -8,7 +9,6 @@ local ce = textadept.command_entry -- @param command The command to complete. -- @return sorted table of completions function ce.get_completions_for(command) - local textadept = textadept local substring = command:match('[%w_.:]+$') or '' local path, o, prefix = substring:match('^([%w_.:]-)([.:]?)([%w_]*)$') local ret, tbl = pcall(loadstring('return ('..path..')')) diff --git a/core/ext/find.lua b/core/ext/find.lua index 5def64e8..e036934f 100644 --- a/core/ext/find.lua +++ b/core/ext/find.lua @@ -1,5 +1,6 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept local find = textadept.find --- @@ -25,7 +26,7 @@ local escapes = { -- for displaying useful statusbar information. This flag is used and set -- internally, and should not be set otherwise. function find.find(text, next, flags, nowrap, wrapped) - local buffer, textadept = buffer, textadept + local buffer = buffer local increment, result text = text:gsub('\\[abfnrtv\\]', escapes) find.captures = nil @@ -135,7 +136,6 @@ end -- @param flags The number mask identical to the one in 'find'. -- @see find.find function find.replace_all(ftext, rtext, flags) - local textadept = textadept buffer:goto_pos(0) local count = 0 while(find.find(ftext, true, flags, true)) do diff --git a/core/ext/key_commands.lua b/core/ext/key_commands.lua index 6e3f0145..25c7c239 100644 --- a/core/ext/key_commands.lua +++ b/core/ext/key_commands.lua @@ -1,5 +1,7 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + --- -- Defines the key commands used by the Textadept key command manager. -- For non-ascii keys, see textadept.keys for string aliases. @@ -14,12 +16,7 @@ module('textadept.key_commands', package.seeall) CSA: C D G H J K L O Q R S T U W X Z ]]-- ---- --- Global container that holds all key commands. --- @class table --- @name keys -_G.keys = {} -local keys = keys +local keys = _G.keys keys.clear_sequence = 'esc' diff --git a/core/ext/key_commands_mac.lua b/core/ext/key_commands_mac.lua index 11378804..2e8e82b2 100644 --- a/core/ext/key_commands_mac.lua +++ b/core/ext/key_commands_mac.lua @@ -1,5 +1,7 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + --- -- Defines the key commands used by the Textadept key command manager. -- For non-ascii keys, see textadept.keys for string aliases. @@ -15,12 +17,7 @@ module('textadept.key_commands_mac', package.seeall) CSA: A C D E G H J K L M N O P Q R S T U V W X Y Z ]]-- ---- --- Global container that holds all key commands. --- @class table --- @name keys -_G.keys = {} -local keys = keys +local keys = _G.keys keys.clear_sequence = 'aesc' diff --git a/core/ext/key_commands_std.lua b/core/ext/key_commands_std.lua index 499305f6..2d7688d8 100644 --- a/core/ext/key_commands_std.lua +++ b/core/ext/key_commands_std.lua @@ -1,5 +1,7 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + --- -- Defines the key commands used by the Textadept key command manager. -- For non-ascii keys, see textadept.keys for string aliases. @@ -15,12 +17,7 @@ module('textadept.key_commands_std', package.seeall) CSA: A B C D E F G H J K L M N O P Q R S T U V W X Y Z ]]-- ---- --- Global container that holds all key commands. --- @class table --- @name keys -_G.keys = {} -local keys = keys +local keys = _G.keys keys.clear_sequence = 'esc' diff --git a/core/ext/keys.lua b/core/ext/keys.lua index 818479a9..466a74c2 100644 --- a/core/ext/keys.lua +++ b/core/ext/keys.lua @@ -1,5 +1,7 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + --- -- Manages key commands in Textadept. -- Default key commands should be defined in a separate file and loaded after @@ -75,6 +77,23 @@ local SHIFT = 's'..ADD local ALT = 'a'..ADD -- end options +--- +-- Global container that holds all key commands. +-- @class table +-- @name keys +_G.keys = {} + +-- optimize for speed +local keys = _G.keys +local string = _G.string +local string_char = string.char +local string_format = string.format +local pcall = _G.pcall +local ipairs = _G.ipairs +local next = _G.next +local type = _G.type +local unpack = _G.unpack + --- -- [Local table] Lookup table for key values higher than 255. -- If a key value given to 'keypress' is higher than 255, this table is used to @@ -124,15 +143,13 @@ end -- @return whatever the executed command returns, true by default. A true -- return value will tell Textadept not to handle the key afterwords. local function keypress(code, shift, control, alt) - local buffer, textadept = buffer, textadept - local string, pcall = string, pcall - local keys = _G.keys + local buffer = buffer local key --print(code, string.char(code)) if code < 256 then - key = string.char(code):lower() + key = string_char(code):lower() if MAC and not shift and not control and not alt then - local ch = string.char(code) + local ch = string_char(code) -- work around native GTK-OSX's handling of Alt key if ch:match('[^A-Za-z ]') and #keychain == 0 then buffer:add_text(ch) @@ -147,7 +164,7 @@ local function keypress(code, shift, control, alt) control = control and CTRL or '' shift = shift and SHIFT or '' alt = alt and ALT or '' - local key_seq = string.format('%s%s%s%s', control, shift, alt, key) + local key_seq = string_format('%s%s%s%s', control, shift, alt, key) if #keychain > 0 and key_seq == keys.clear_sequence then clear_key_sequence() @@ -224,10 +241,10 @@ end -- of -1. This way, pcall will return false and -1, where the -1 can easily and -- efficiently be checked rather than using a string error message. try_get_cmd = function(active_table) - local locale = textadept.locale for _, key_seq in ipairs(keychain) do active_table = active_table[key_seq] end if #active_table == 0 and next(active_table) then - textadept.statusbar_text = locale.KEYCHAIN..table.concat(keychain, ' ') + textadept.statusbar_text = + textadept.locale.KEYCHAIN..table.concat(keychain, ' ') error(-1, 0) else local func = active_table[1] @@ -241,7 +258,7 @@ try_get_cmd = function(active_table) return view[func], { view, unpack(active_table, 3) } end else - error(locale.KEYS_UNKNOWN_COMMAND..tostring(func)) + error(textadept.locale.KEYS_UNKNOWN_COMMAND..tostring(func)) end end end diff --git a/core/ext/menu.lua b/core/ext/menu.lua index e7f57d5a..3398f47e 100644 --- a/core/ext/menu.lua +++ b/core/ext/menu.lua @@ -1,11 +1,15 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + --- -- Provides dynamic menus for Textadept. -- This module, like ext/key_commands, should be 'require'ed last. module('textadept.menu', package.seeall) -local t, gtkmenu, l = textadept, textadept.gtkmenu, textadept.locale +local t = textadept +local l = textadept.locale +local gtkmenu = textadept.gtkmenu local SEPARATOR = 'separator' local ID = { diff --git a/core/ext/mime_types.lua b/core/ext/mime_types.lua index 6d4ec37a..8ee41c7c 100644 --- a/core/ext/mime_types.lua +++ b/core/ext/mime_types.lua @@ -1,5 +1,7 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + --- Handles file-specific settings (based on file extension). module('textadept.mime_types', package.seeall) @@ -334,8 +336,7 @@ local function handle_switch() end end -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) +textadept.events.add_handler('file_opened', handle_new) +textadept.events.add_handler('file_saved_as', handle_new) +textadept.events.add_handler('buffer_switch', handle_switch) +textadept.events.add_handler('view_new', handle_switch) diff --git a/core/ext/pm/buffer_browser.lua b/core/ext/pm/buffer_browser.lua index 73b09d55..83bb26f9 100644 --- a/core/ext/pm/buffer_browser.lua +++ b/core/ext/pm/buffer_browser.lua @@ -1,5 +1,7 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + --- -- Buffer browser for the Textadept project manager. -- It is enabled with the prefix 'buffers' in the project manager entry field. @@ -62,12 +64,11 @@ function perform_menu_action(menu_item, menu_id, selected_item) textadept.pm.activate() end -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('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) +textadept.events.add_handler('file_opened', update_view) +textadept.events.add_handler('buffer_new', update_view) +textadept.events.add_handler('buffer_deleted', update_view) +textadept.events.add_handler('save_point_reached', update_view) +textadept.events.add_handler('save_point_left', update_view) diff --git a/core/ext/pm/ctags_browser.lua b/core/ext/pm/ctags_browser.lua index e1b4586d..8f112470 100644 --- a/core/ext/pm/ctags_browser.lua +++ b/core/ext/pm/ctags_browser.lua @@ -1,5 +1,7 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + --- -- CTags Browser for the Textadept project manager. -- It is enabled with the prefix 'ctags' in the project manager entry field @@ -234,7 +236,6 @@ function perform_menu_action(menu_item, menu_id, selected_item) end -local add_handler = textadept.events.add_handler local function update_view() if matches(textadept.pm.entry_text) then if buffer.filename then @@ -244,7 +245,7 @@ local function update_view() end end end -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) +textadept.events.add_handler('file_opened', update_view) +textadept.events.add_handler('buffer_deleted', update_view) +textadept.events.add_handler('buffer_switch', update_view) +textadept.events.add_handler('save_point_reached', update_view) diff --git a/core/ext/pm/file_browser.lua b/core/ext/pm/file_browser.lua index c658b6c0..87d4b8e6 100644 --- a/core/ext/pm/file_browser.lua +++ b/core/ext/pm/file_browser.lua @@ -1,5 +1,7 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + --- -- File browser for the Textadept project manager. -- It is enabled by providing the absolute path to a directory in the project diff --git a/core/ext/pm/find_browser.lua b/core/ext/pm/find_browser.lua index a8722e78..c79ab167 100644 --- a/core/ext/pm/find_browser.lua +++ b/core/ext/pm/find_browser.lua @@ -1,5 +1,7 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + --- -- Find in files browser for the Textadept project manager. -- It is enabled with the prefix 'find' in the project manager entry field. diff --git a/core/ext/pm/macro_browser.lua b/core/ext/pm/macro_browser.lua index c2892fa7..051487cf 100644 --- a/core/ext/pm/macro_browser.lua +++ b/core/ext/pm/macro_browser.lua @@ -1,12 +1,12 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + --- -- Macro browser for the Textadept project manager. -- It is enabled with the prefix 'macros' in the project manager entry field. module('textadept.pm.browsers.macro', package.seeall) -local textadept = textadept - function matches(entry_text) return entry_text:sub(1, 7) == 'macros' end @@ -38,9 +38,8 @@ function perform_menu_action(menu_item, menu_id, selected_item) textadept.pm.activate() end -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('macro_saved', update_view) -add_handler('macro_deleted', update_view) +textadept.events.add_handler('macro_saved', update_view) +textadept.events.add_handler('macro_deleted', update_view) diff --git a/core/ext/pm/modules_browser.lua b/core/ext/pm/modules_browser.lua index 4fc97206..757e7d93 100644 --- a/core/ext/pm/modules_browser.lua +++ b/core/ext/pm/modules_browser.lua @@ -1,5 +1,7 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + --- -- Modules browser for the Textadept project manager. -- It is enabled with the prefix 'modules' in the project manager entry field. @@ -182,7 +184,6 @@ function perform_menu_action(menu_item, menu_id, selected_item) elseif menu_id == ID.CONF_MIME_TYPES then textadept.io.open(_HOME..'/core/ext/mime_types.lua') elseif menu_id == ID.CONF_KEY_COMMANDS then - local textadept = textadept if textadept.key_commands then textadept.io.open(_HOME..'/core/ext/key_commands.lua') elseif textadept.key_commands_std then diff --git a/core/ext/pm/project_browser.lua b/core/ext/pm/project_browser.lua index 30c6a847..0219839e 100644 --- a/core/ext/pm/project_browser.lua +++ b/core/ext/pm/project_browser.lua @@ -1,5 +1,7 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + --- -- Browser template for the Textadept project manager. -- It is enabled with the prefix 'project' in the project manager entry field. diff --git a/core/file_io.lua b/core/file_io.lua index 943bc6f0..5e76e886 100644 --- a/core/file_io.lua +++ b/core/file_io.lua @@ -1,5 +1,7 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + --- -- Provides file input/output routines for Textadept. -- Opens and saves files and sessions and reads API files. @@ -9,8 +11,6 @@ -- file_saved_as(filename) module('textadept.io', package.seeall) -local events = textadept.events - --- -- List of recently opened files. -- @class table @@ -34,7 +34,7 @@ local function open_helper(filename) end buffer.filename = filename buffer:set_save_point() - events.handle('file_opened', filename) + textadept.events.handle('file_opened', filename) recent_files[#recent_files + 1] = filename end @@ -92,7 +92,7 @@ function save(buffer) f:close() buffer:set_save_point() else - events.error(err) + textadept.events.error(err) end end @@ -115,7 +115,7 @@ function save_as(buffer, filename) if #filename > 0 then buffer.filename = filename buffer:save() - events.handle('file_saved_as', filename) + textadept.events.handle('file_saved_as', filename) end end @@ -179,7 +179,6 @@ end -- @return true if the session file was opened and read; false otherwise. -- @usage textadept.io.load_session(filename) function load_session(filename, only_pm) - local textadept = textadept local user_dir = os.getenv(not WIN32 and 'HOME' or 'USERPROFILE') if not user_dir then return end local ta_session = user_dir..'/.ta_session' diff --git a/core/init.lua b/core/init.lua index 8a7b96de..38ed7734 100644 --- a/core/init.lua +++ b/core/init.lua @@ -1,5 +1,7 @@ -- Copyright 2007-2009 Mitchell mitchellcaladbolg.net. See LICENSE. +local textadept = _G.textadept + package.path = _HOME..'/core/?.lua;'..package.path if not WIN32 then package.cpath = _HOME..'/core/?.so;'..package.cpath -- cgit v1.2.3