aboutsummaryrefslogtreecommitdiff
path: root/core/ext
diff options
context:
space:
mode:
Diffstat (limited to 'core/ext')
-rw-r--r--core/ext/command_entry.lua69
-rw-r--r--core/ext/find.lua58
-rw-r--r--core/ext/pm.lua53
-rw-r--r--core/ext/pm/modules_browser.lua6
4 files changed, 119 insertions, 67 deletions
diff --git a/core/ext/command_entry.lua b/core/ext/command_entry.lua
index d2a64243..0170f21c 100644
--- a/core/ext/command_entry.lua
+++ b/core/ext/command_entry.lua
@@ -2,32 +2,51 @@
local textadept = _G.textadept
local locale = _G.locale
-local ce = textadept.command_entry
--- LuaDoc is in core/.command_entry.lua
-function ce.get_completions_for(command)
- local substring = command:match('[%w_.:]+$') or ''
- local path, o, prefix = substring:match('^([%w_.:]-)([.:]?)([%w_]*)$')
- local ret, tbl = pcall(loadstring('return ('..path..')'))
- if not ret then tbl = getfenv(0) end
- if type(tbl) ~= 'table' then return end
- local cmpls = {}
- for k in pairs(tbl) do
- if type(k) == 'string' and k:find('^'..prefix) then
- cmpls[#cmpls + 1] = k
- end
- end
- if path == 'buffer' then
- if o == ':' then
- for f in pairs(textadept.buffer_functions) do
- if f:find('^'..prefix) then cmpls[#cmpls + 1] = f end
+textadept.events.add_handler('command_entry_completions_request',
+ function(command) -- get a Lua completion list for the command being entered
+ local substring = command:match('[%w_.:]+$') or ''
+ local path, o, prefix = substring:match('^([%w_.:]-)([.:]?)([%w_]*)$')
+ local ret, tbl = pcall(loadstring('return ('..path..')'))
+ if not ret then tbl = getfenv(0) end
+ if type(tbl) ~= 'table' then return end
+ local cmpls = {}
+ for k in pairs(tbl) do
+ if type(k) == 'string' and k:find('^'..prefix) then
+ cmpls[#cmpls + 1] = k
end
- else
- for p in pairs(textadept.buffer_properties) do
- if p:find('^'..prefix) then cmpls[#cmpls + 1] = p end
+ end
+ if path == 'buffer' then
+ if o == ':' then
+ for f in pairs(textadept.buffer_functions) do
+ if f:find('^'..prefix) then cmpls[#cmpls + 1] = f end
+ end
+ else
+ for p in pairs(textadept.buffer_properties) do
+ if p:find('^'..prefix) then cmpls[#cmpls + 1] = p end
+ end
end
end
- end
- table.sort(cmpls)
- return cmpls
-end
+ table.sort(cmpls)
+ textadept.command_entry.show_completions(cmpls)
+ end)
+
+textadept.events.add_handler('command_entry_command',
+ function(command) -- execute a Lua command
+ local f, err = loadstring(command)
+ if err then error(err) end
+ f()
+ end)
+
+textadept.events.add_handler('command_entry_keypress',
+ function(code)
+ local ce = textadept.command_entry
+ if code == 65307 then -- escape
+ ce.focus() -- toggle focus to hide
+ return true
+ elseif code == 65289 then -- tab
+ textadept.events.handle('command_entry_completions_request',
+ ce.entry_text)
+ return true
+ end
+ end)
diff --git a/core/ext/find.lua b/core/ext/find.lua
index b4f4efee..c97cefd3 100644
--- a/core/ext/find.lua
+++ b/core/ext/find.lua
@@ -10,14 +10,28 @@ local MARK_FIND = 0
local MARK_FIND_COLOR = 0x4D9999
local previous_view
--- LuaDoc is in core/.find.lua.
+---
+-- [Local table] Text escape sequences with their associated characters.
+-- @class table
+-- @name escapes
local escapes = {
['\\a'] = '\a', ['\\b'] = '\b', ['\\f'] = '\f', ['\\n'] = '\n',
['\\r'] = '\r', ['\\t'] = '\t', ['\\v'] = '\v', ['\\\\'] = '\\'
}
--- LuaDoc is in core/.find.lua.
-function find.find(text, next, flags, nowrap, wrapped)
+---
+-- [Local function] Finds and selects text in the current buffer.
+-- @param text The text to find.
+-- @param next Flag indicating whether or not the search direction is forward.
+-- @param flags Search flags. This is a number mask of 4 flags: match case (2),
+-- whole word (4), Lua pattern (8), and in files (16) joined with binary OR.
+-- If nil, this is determined based on the checkboxes in the find box.
+-- @param nowrap Flag indicating whether or not the search won't wrap.
+-- @param wrapped Utility flag indicating whether or not the search has wrapped
+-- for displaying useful statusbar information. This flag is used and set
+-- internally, and should not be set otherwise.
+-- @return position of the found text or -1
+local function find_(text, next, flags, nowrap, wrapped)
if #text == 0 then return end
local buffer = buffer
local first_visible_line = buffer.first_visible_line -- for 'no results found'
@@ -128,7 +142,7 @@ function find.find(text, next, flags, nowrap, wrapped)
buffer:goto_pos(buffer.length)
end
textadept.statusbar_text = locale.FIND_SEARCH_WRAPPED
- result = find.find(text, next, flags, true, true)
+ result = find_(text, next, flags, true, true)
if result == -1 then
textadept.statusbar_text = locale.FIND_NO_RESULTS
buffer:line_scroll(0, first_visible_line)
@@ -141,9 +155,18 @@ function find.find(text, next, flags, nowrap, wrapped)
return result
end
+textadept.events.add_handler('find', find_)
--- LuaDoc is in core/.find.lua.
-function find.replace(rtext)
+---
+-- [Local function] Replaces found text.
+-- 'find_' is called first, to select any found text. The selected text is then
+-- replaced by the specified replacement text.
+-- This function ignores 'Find in Files'.
+-- @param rtext The text to replace found text with. It can contain both Lua
+-- capture items (%n where 1 <= n <= 9) for Lua pattern searches and %()
+-- sequences for embedding Lua code for any search.
+-- @see find
+local function replace(rtext)
if #buffer:get_sel_text() == 0 then return end
if find.in_files then find.in_files = false end
local buffer = buffer
@@ -179,9 +202,17 @@ function find.replace(rtext)
buffer:goto_pos(buffer.current_pos)
end
end
+textadept.events.add_handler('replace', replace)
--- LuaDoc is in core/.find.lua.
-function find.replace_all(ftext, rtext, flags)
+---
+-- [Local function] Replaces all found text.
+-- If any text is selected, all found text in that selection is replaced.
+-- This function ignores 'Find in Files'.
+-- @param ftext The text to find.
+-- @param rtext The text to replace found text with.
+-- @param flags The number mask identical to the one in 'find'.
+-- @see find
+local function replace_all(ftext, rtext, flags)
if #ftext == 0 then return end
if find.in_files then find.in_files = false end
local buffer = buffer
@@ -189,8 +220,8 @@ function find.replace_all(ftext, rtext, flags)
local count = 0
if #buffer:get_sel_text() == 0 then
buffer:goto_pos(0)
- while(find.find(ftext, true, flags, true) ~= -1) do
- find.replace(rtext)
+ while(find_(ftext, true, flags, true) ~= -1) do
+ replace(rtext)
count = count + 1
end
else
@@ -201,13 +232,13 @@ function find.replace_all(ftext, rtext, flags)
local end_marker =
buffer:marker_add(buffer:line_from_position(e + 1), MARK_FIND)
buffer:goto_pos(s)
- local pos = find.find(ftext, true, flags, true)
+ local pos = find_(ftext, true, flags, true)
while pos ~= -1 and
pos < buffer:position_from_line(
buffer:marker_line_from_handle(end_marker)) do
- find.replace(rtext)
+ replace(rtext)
count = count + 1
- pos = find.find(ftext, true, flags, true)
+ pos = find_(ftext, true, flags, true)
end
e = buffer:position_from_line(buffer:marker_line_from_handle(end_marker))
buffer:goto_pos(e)
@@ -220,6 +251,7 @@ function find.replace_all(ftext, rtext, flags)
string.format(locale.FIND_REPLACEMENTS_MADE, tostring(count))
buffer:end_undo_action()
end
+textadept.events.add_handler('replace_all', replace_all)
---
-- [Local function] When the user double-clicks a found file, go to the line in
diff --git a/core/ext/pm.lua b/core/ext/pm.lua
index 67cbbbfb..8838582c 100644
--- a/core/ext/pm.lua
+++ b/core/ext/pm.lua
@@ -8,37 +8,36 @@ local current_browser = nil
local last_browser_text = nil
local browser_cursors = {}
--- LuaDoc is in core/.browser.lua.
-function pm.get_contents_for(full_path, expanding)
- for _, browser in pairs(pm.browsers) do
- if browser.matches(full_path[1]) then
- current_browser = browser
- if last_browser_text and last_browser_text ~= pm.entry_text then
- -- Switching browsers, save the current one's cursor.
- -- Don't reset last_browser_text here though, we still need to detect
- -- the switch when the 'pm_view_filled' event is called so as to restore
- -- the cursor to the new browser.
- browser_cursors[last_browser_text] = pm.cursor
+textadept.events.add_handler('pm_contents_request',
+ function(full_path, expanding)
+ for _, browser in pairs(pm.browsers) do
+ if browser.matches(full_path[1]) then
+ current_browser = browser
+ if last_browser_text and last_browser_text ~= pm.entry_text then
+ -- Switching browsers, save the current one's cursor.
+ -- Don't reset last_browser_text here though, we still need to detect
+ -- the switch when the 'pm_view_filled' event is called so as to restore
+ -- the cursor to the new browser.
+ browser_cursors[last_browser_text] = pm.cursor
+ end
+ pm.fill(browser.get_contents_for(full_path, expanding), expanding)
+ textadept.events.handle('pm_view_filled')
end
- return browser.get_contents_for(full_path, expanding)
end
- end
-end
+ end)
--- LuaDoc is in core/.browser.lua.
-function pm.perform_action(selected_item)
- current_browser.perform_action(selected_item)
-end
+textadept.events.add_handler('pm_item_selected',
+ function(selected_item) current_browser.perform_action(selected_item) end)
--- LuaDoc is in core/.browser.lua.
-function pm.get_context_menu(selected_item)
- return current_browser.get_context_menu(selected_item)
-end
+textadept.events.add_handler('pm_context_menu_request',
+ function(selected_item, event)
+ pm.show_context_menu(current_browser.get_context_menu(selected_item), event)
+ end)
--- LuaDoc is in core/.browser.lua.
-function pm.perform_menu_action(menu_id, selected_item)
- current_browser.perform_menu_action(menu_id, selected_item)
-end
+textadept.events.add_handler('pm_menu_clicked',
+ function(menu_id, selected_item)
+ current_browser.perform_menu_action(menu_id, selected_item)
+ end)
-- LuaDoc is in core/.browser.lua.
function pm.toggle_visible()
@@ -51,7 +50,7 @@ function pm.toggle_visible()
end
textadept.events.add_handler('pm_view_filled',
- function() -- tries to restore the cursor for a previous browser
+ function() -- try to restore previous browser cursor
if last_browser_text ~= pm.entry_text then
last_browser_text = pm.entry_text
local previous_cursor = browser_cursors[pm.entry_text]
diff --git a/core/ext/pm/modules_browser.lua b/core/ext/pm/modules_browser.lua
index 4cbe715c..8472ee0d 100644
--- a/core/ext/pm/modules_browser.lua
+++ b/core/ext/pm/modules_browser.lua
@@ -84,8 +84,10 @@ function matches(entry_text)
end
local function modify_path(path)
- path[1] = textadept.iconv(_HOME..'/modules', 'UTF-8', _CHARSET)
- return path
+ local new_path = {}
+ for _, v in ipairs(path) do new_path[#new_path + 1] = v end
+ new_path[1] = textadept.iconv(_HOME..'/modules', 'UTF-8', _CHARSET)
+ return new_path
end
function get_contents_for(full_path)