aboutsummaryrefslogtreecommitdiff
path: root/modules/textadept
diff options
context:
space:
mode:
authormitchell <70453897+orbitalquark@users.noreply.github.com>2020-11-01 23:30:43 -0500
committermitchell <70453897+orbitalquark@users.noreply.github.com>2020-11-01 23:30:43 -0500
commit85b8810660ffa37eb2d3edf85a4dbf12ba5f1ca3 (patch)
treec364b17b99c60326e7c85278c29ffc2c6584f598 /modules/textadept
parent2ec90788dc08346d74a79877a35f91c74d661688 (diff)
downloadtextadept-85b8810660ffa37eb2d3edf85a4dbf12ba5f1ca3.tar.gz
textadept-85b8810660ffa37eb2d3edf85a4dbf12ba5f1ca3.zip
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.
Diffstat (limited to 'modules/textadept')
-rw-r--r--modules/textadept/command_entry.lua25
1 files changed, 16 insertions, 9 deletions
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 '')