From 85b8810660ffa37eb2d3edf85a4dbf12ba5f1ca3 Mon Sep 17 00:00:00 2001
From: mitchell <70453897+orbitalquark@users.noreply.github.com>
Date: Sun, 1 Nov 2020 23:30:43 -0500
Subject: Added optional mode parameter to `ui.command_entry.append_history()`.
This allows for arbitrary appending to history, not just for the current or
most recent mode.
---
docs/api.md | 9 ++++++---
modules/lua/ta_api | 2 +-
modules/lua/ta_tags | 2 +-
modules/textadept/command_entry.lua | 25 ++++++++++++++++---------
test/test.lua | 11 ++++++++++-
5 files changed, 34 insertions(+), 15 deletions(-)
diff --git a/docs/api.md b/docs/api.md
index 9152aca2..8405ff84 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -8022,16 +8022,19 @@ The height in pixels of the command entry.
### Functions defined by `ui.command_entry`
-#### `ui.command_entry.append_history`(*text*)
+#### `ui.command_entry.append_history`(*f, text*)
-Appends the given text to the history for the current or most recent command
-entry mode.
+Appends string *text* to the history for command entry mode *f* or the
+current or most recent mode.
This should only be called if `ui.command_entry.run()` is called with a keys
table that has a custom binding for the Enter key ('\n').
Otherwise, history is automatically appended as needed.
Parameters:
+* *`f`*: Optional command entry mode to append history to. This is a function
+ passed to `ui.command_entry_run()`. If omitted, uses the current or most
+ recent mode.
* *`text`*: String text to append to history.
diff --git a/modules/lua/ta_api b/modules/lua/ta_api
index 31327fc8..5832ece4 100644
--- a/modules/lua/ta_api
+++ b/modules/lua/ta_api
@@ -333,7 +333,7 @@ ansi_c _G.snippets.ansi_c (table)\nTable of C-specific snippets.
ansi_c _M.ansi_c (module)\nThe ansi_c module.\nIt provides utilities for editing C code.
any lexer.any (pattern)\nA pattern that matches any single character.
api_files textadept.editing.api_files (table)\nMap of lexer names to API documentation file tables.\nFile tables contain API file paths or functions that return such paths.\nEach line in an API file consists of a symbol name (not a fully qualified\nsymbol name), a space character, and that symbol's documentation. "\\n"\nrepresents a newline character.\n@see show_documentation
-append_history ui.command_entry.append_history(text)\nAppends the given text to the history for the current or most recent command\nentry mode.\nThis should only be called if `ui.command_entry.run()` is called with a keys\ntable that has a custom binding for the Enter key ('\\n').\nOtherwise, history is automatically appended as needed.\n@param text String text to append to history.
+append_history ui.command_entry.append_history(f, text)\nAppends string *text* to the history for command entry mode *f* or the\ncurrent or most recent mode.\nThis should only be called if `ui.command_entry.run()` is called with a keys\ntable that has a custom binding for the Enter key ('\\n').\nOtherwise, history is automatically appended as needed.\n@param f Optional command entry mode to append history to. This is a function\n passed to `ui.command_entry_run()`. If omitted, uses the current or most\n recent mode.\n@param text String text to append to history.
append_text buffer.append_text(buffer, text)\nAppends string *text* to the end of the buffer without modifying any existing\nselections or scrolling the text into view.\n@param buffer A buffer.\n@param text The text to append.
arg _G.arg (table)\nTable of command line parameters passed to Textadept.\n@see args
args _G.args (module)\nProcesses command line arguments for Textadept.
diff --git a/modules/lua/ta_tags b/modules/lua/ta_tags
index e186e8e3..dc56db4f 100644
--- a/modules/lua/ta_tags
+++ b/modules/lua/ta_tags
@@ -335,7 +335,7 @@ ansi_c _HOME/modules/ansi_c/init.lua /^module('_M.ansi_c')]]$/;" m class:_M
ansi_c _HOME/modules/ansi_c/init.lua /^snippets.ansi_c = {$/;" t class:snippets
any _HOME/lexers/lexer.lua /^module('lexer')]=]$/;" F class:lexer
api_files _HOME/modules/textadept/editing.lua /^M.api_files = setmetatable({}, {__index = function(t, k)$/;" t class:textadept.editing
-append_history _HOME/modules/textadept/command_entry.lua /^function M.append_history(text)$/;" f class:ui.command_entry
+append_history _HOME/modules/textadept/command_entry.lua /^function M.append_history(f, text)$/;" f class:ui.command_entry
append_text _HOME/core/.buffer.luadoc /^function append_text(buffer, text) end$/;" f class:buffer
arg _HOME/core/init.lua /^local arg$/;" t
args _HOME/core/args.lua /^module('args')]]$/;" m
diff --git a/modules/textadept/command_entry.lua b/modules/textadept/command_entry.lua
index 3e344f81..d7475a8c 100644
--- a/modules/textadept/command_entry.lua
+++ b/modules/textadept/command_entry.lua
@@ -18,7 +18,10 @@ module('ui.command_entry')]]
-- The current mode is in the `mode` field.
-- @class table
-- @name history
-local history = {}
+local history = setmetatable({}, {__index = function(t, k)
+ if type(k) == 'function' then t[k] = {pos = 0} else return nil end
+ return t[k]
+end})
-- Cycles through command history for the current mode.
-- @param prev Flag that indicates whether to cycle to the previous command or
@@ -35,18 +38,23 @@ local function cycle_history(prev)
end
---
--- Appends the given text to the history for the current or most recent command
--- entry mode.
+-- Appends string *text* to the history for command entry mode *f* or the
+-- current or most recent mode.
-- This should only be called if `ui.command_entry.run()` is called with a keys
-- table that has a custom binding for the Enter key ('\n').
-- Otherwise, history is automatically appended as needed.
+-- @param f Optional command entry mode to append history to. This is a function
+-- passed to `ui.command_entry_run()`. If omitted, uses the current or most
+-- recent mode.
-- @param text String text to append to history.
-- @name append_history
-function M.append_history(text)
- if not history.mode then return end
- local mode_history = history[history.mode]
- mode_history[#mode_history + 1] = assert_type(text, 'string', 1)
- mode_history.pos = #mode_history
+function M.append_history(f, text)
+ if not assert_type(text, 'string/nil', 2) then
+ f, text = history.mode, assert_type(f, 'string', 1)
+ if not f then return end
+ end
+ local mode_history = history[assert_type(f, 'function', 1)]
+ mode_history[#mode_history + 1], mode_history.pos = text, #mode_history + 1
end
---
@@ -235,7 +243,6 @@ function M.run(f, keys, lang, height)
end
end
if not getmetatable(keys) then setmetatable(keys, M.editing_keys) end
- if f and not history[f] then history[f] = {pos = 0} end
history.mode = f
local mode_history = history[history.mode]
M:set_text(mode_history and mode_history[mode_history.pos] or '')
diff --git a/test/test.lua b/test/test.lua
index cc545257..53a7cb24 100644
--- a/test/test.lua
+++ b/test/test.lua
@@ -1413,7 +1413,16 @@ function test_command_entry_history_append()
assert_equal(ui.command_entry:get_text(), 'bar') -- no further history
events.emit(events.KEYPRESS, not CURSES and 0xFF0D or 343) -- \n
- assert_raises(function() ui.command_entry:append_history('text') end, 'string expected, got table')
+ -- Verify no previous mode or history is needed for adding history.
+ local f2 = function() end
+ ui.command_entry.append_history(f2, 'baz')
+ ui.command_entry.run(f2, keys)
+ assert_equal(ui.command_entry:get_text(), 'baz')
+ events.emit(events.KEYPRESS, not CURSES and 0xFF0D or 343) -- \n
+
+ assert_raises(function() ui.command_entry.append_history(1) end, 'string expected, got number')
+ assert_raises(function() ui.command_entry:append_history('text') end, 'function expected, got table')
+ assert_raises(function() ui.command_entry.append_history(function() end, true) end, 'string/nil expected, got boolean')
end
function test_command_entry_mode_restore()
--
cgit v1.2.3