From 1be7d50f3cee5aa4a06c083d195290e8fe8cacaf Mon Sep 17 00:00:00 2001 From: mitchell <70453897+667e-11@users.noreply.github.com> Date: Sun, 5 Oct 2014 00:28:18 -0400 Subject: Replaced command entry text field with a Scintilla buffer. `ui.command_entry` now has the same functions and fields as `buffer`s as well as an additional `height` property. Note that when the command entry is active, `_G.buffer` is unchanged, so many existing key commands cannot be bound to command entry key modes. See `keys.lua_command` in *modules/textadept/command_entry.lua* for the proper way to bind them. Removed `ui.command_entry.entry_text` and `ui.command_entry.show_completions()`. --- modules/textadept/command_entry.lua | 52 +++++++++++++++++-------------------- modules/textadept/file_types.lua | 5 ++-- modules/textadept/find.lua | 2 +- modules/textadept/keys.lua | 10 +++---- 4 files changed, 32 insertions(+), 37 deletions(-) (limited to 'modules') diff --git a/modules/textadept/command_entry.lua b/modules/textadept/command_entry.lua index b75405ca..8dca8c6a 100644 --- a/modules/textadept/command_entry.lua +++ b/modules/textadept/command_entry.lua @@ -30,8 +30,8 @@ local M = ui.command_entry -- `Tab` shows a list of Lua completions for the entry text and `Enter` exits -- "lua_command" key mode and executes the entered code. The command entry -- handles all other keys normally. --- @field entry_text (string) --- The text in the command entry. +-- @field height (number) +-- The height in pixels of the command entry. module('ui.command_entry')]] --- @@ -47,9 +47,7 @@ module('ui.command_entry')]] function M.enter_mode(mode) keys.MODE = mode if mode and not keys[mode]['esc'] then keys[mode]['esc'] = M.enter_mode end - -- In curses, M.focus() does not return immediately, so the key sequence that - -- called M.focus() is still on the keychain. Clear it. - if CURSES then keys.clear_key_sequence() end + M:select_all() M.focus() end @@ -63,9 +61,9 @@ end -- @usage keys['\n'] = {ui.command_entry.finish_mode, ui.print} -- @name finish_mode function M.finish_mode(f) + if M:auto_c_active() then return false end -- allow Enter to autocomplete M.enter_mode(nil) - if f then f(M.entry_text) end - if CURSES then return false end -- propagate to exit CDK entry on Enter + if f then f(M:get_text()) end end -- Environment for abbreviated commands. @@ -103,13 +101,12 @@ local function execute_lua(code) end args.register('-e', '--execute', 1, execute_lua, 'Execute Lua code') --- Shows a set of Lua code completions for string *code* or `entry_text`. +-- Shows a set of Lua code completions for string *code* or the entry's text. -- Completions are subject to an "abbreviated" environment where the `buffer`, -- `view`, and `ui` tables are also considered as globals. --- @param code The Lua code to complete. The default value is the value of --- `entry_text`. +-- @param code The Lua code to complete. The default value is the entry's text. local function complete_lua(code) - if not code then code = M.entry_text end + if not code then code = M:get_text() end local symbol, op, part = code:match('([%w_.]-)([%.:]?)([%w_]*)$') local ok, result = pcall((load('return ('..symbol..')', nil, 'bt', env))) local cmpls = {} @@ -139,19 +136,28 @@ local function complete_lua(code) end end table.sort(cmpls) - M.show_completions(cmpls) + M:auto_c_show(#part - 1, table.concat(cmpls, ' ')) end -- Define key mode for entering Lua commands. keys.lua_command = { - ['\t'] = complete_lua, ['\n'] = {M.finish_mode, execute_lua} + ['\t'] = complete_lua, ['\n'] = {M.finish_mode, execute_lua}, + [not OSX and 'cx' or 'mx'] = {buffer.cut, M}, + [not OSX and 'cc' or 'mc'] = {buffer.copy, M}, + [not OSX and 'cv' or 'mv'] = {buffer.paste, M}, + [not OSX and not CURSES and 'ca' or 'ma'] = {buffer.select_all, M}, + [not OSX and 'cz' or 'mz'] = {buffer.undo, M}, + [not OSX and 'cy' or 'mZ'] = {buffer.redo, M}, + [not OSX and 'cZ' or 'mZ'] = {buffer.redo, M}, } --- Pass command entry keys to the default keypress handler. --- Since the command entry is designed to be modal, command entry key bindings --- should stay separate from editor key bindings. -events.connect(events.COMMAND_ENTRY_KEYPRESS, function(...) - if keys.MODE then return events.emit(events.KEYPRESS, ...) end +-- Configure the command entry's default properties. +events.connect(events.INITIALIZED, function() + if not arg then return end -- no need to reconfigure on reset + M.h_scroll_bar, M.v_scroll_bar = false, false + M.margin_width_n[0], M.margin_width_n[1], M.margin_width_n[2] = 0, 0, 0 + if not CURSES then M.height = M:text_height(1) end + M:set_lexer('lua') end) --[[ The function below is a Lua C function. @@ -161,14 +167,4 @@ end) -- @class function -- @name focus local focus - ---- --- Shows completion list *completions* for the current word prefix. --- Word prefix characters are alphanumerics and underscores. On selection, the --- word prefix is replaced with the completion. --- @param completions The table of completions to show. Non-string values are --- ignored. --- @class function --- @name show_completions -local show_completions ]] diff --git a/modules/textadept/file_types.lua b/modules/textadept/file_types.lua index cb5c34da..48c3414e 100644 --- a/modules/textadept/file_types.lua +++ b/modules/textadept/file_types.lua @@ -94,7 +94,7 @@ local function set_lexer(buffer, lang) end -- Gives new buffers lexer-specific functions. -local function set_lexer_functions() +events.connect(events.BUFFER_NEW, function() buffer.get_lexer, buffer.set_lexer = get_lexer, set_lexer buffer.style_name = setmetatable({}, { __index = function(t, style_num) -- LuaDoc is in core/.buffer.luadoc @@ -103,8 +103,7 @@ local function set_lexer_functions() end, __newindex = function() error('read-only property') end }) -end -events.connect(events.BUFFER_NEW, set_lexer_functions, 1) +end, 1) -- Auto-detect lexer on file open or save as. events.connect(events.FILE_OPENED, function() buffer:set_lexer() end) diff --git a/modules/textadept/find.lua b/modules/textadept/find.lua index 8f5dde69..bde8dfa5 100644 --- a/modules/textadept/find.lua +++ b/modules/textadept/find.lua @@ -207,7 +207,7 @@ end function M.find_incremental(text, next, anchor) if text then find_incremental(text, next, anchor) return end M.incremental_start = buffer.current_pos - ui.command_entry.entry_text = '' + ui.command_entry:set_text('') ui.command_entry.enter_mode('find_incremental') end diff --git a/modules/textadept/keys.lua b/modules/textadept/keys.lua index 8904fc15..36161672 100644 --- a/modules/textadept/keys.lua +++ b/modules/textadept/keys.lua @@ -612,21 +612,21 @@ keys.filter_through = { } keys.find_incremental = { ['\n'] = function() - ui.find.find_entry_text = ui.command_entry.entry_text -- save - ui.find.find_incremental(ui.command_entry.entry_text, true, true) + ui.find.find_entry_text = ui.command_entry:get_text() -- save + ui.find.find_incremental(ui.command_entry:get_text(), true, true) end, ['cr'] = function() - ui.find.find_incremental(ui.command_entry.entry_text, false, true) + ui.find.find_incremental(ui.command_entry:get_text(), false, true) end, ['\b'] = function() - ui.find.find_incremental(ui.command_entry.entry_text:sub(1, -2), true) + ui.find.find_incremental(ui.command_entry:get_text():sub(1, -2), true) return false -- propagate end } -- Add the character for any key pressed without modifiers to incremental find. setmetatable(keys.find_incremental, {__index = function(t, k) if #k > 1 and k:find('^[cams]*.+$') then return end - ui.find.find_incremental(ui.command_entry.entry_text..k, true) + ui.find.find_incremental(ui.command_entry:get_text()..k, true) end}) return M -- cgit v1.2.3