aboutsummaryrefslogtreecommitdiff
path: root/modules/textadept
diff options
context:
space:
mode:
authormitchell <70453897+667e-11@users.noreply.github.com>2014-06-12 21:18:13 -0400
committermitchell <70453897+667e-11@users.noreply.github.com>2014-06-12 21:18:13 -0400
commita9f6d85ead8c83ce210e5ffb05f84361e029f419 (patch)
treed7fcd5c957e3170f396eb515b94927174ad955d1 /modules/textadept
parentd3e1bd0272a6ac75d927e0c7d5fcfde91d90bcc4 (diff)
downloadtextadept-a9f6d85ead8c83ce210e5ffb05f84361e029f419.tar.gz
textadept-a9f6d85ead8c83ce210e5ffb05f84361e029f419.zip
Lua code cleanup.
Diffstat (limited to 'modules/textadept')
-rw-r--r--modules/textadept/command_entry.lua47
-rw-r--r--modules/textadept/editing.lua4
-rw-r--r--modules/textadept/file_types.lua45
-rw-r--r--modules/textadept/find.lua5
-rw-r--r--modules/textadept/init.lua2
-rw-r--r--modules/textadept/session.lua9
-rw-r--r--modules/textadept/snippets.lua24
7 files changed, 58 insertions, 78 deletions
diff --git a/modules/textadept/command_entry.lua b/modules/textadept/command_entry.lua
index c2fa414c..6d495199 100644
--- a/modules/textadept/command_entry.lua
+++ b/modules/textadept/command_entry.lua
@@ -98,11 +98,9 @@ local env = setmetatable({}, {
-- Prints the results of '=' expressions like in the Lua prompt.
-- @param code The Lua code to execute.
local function execute_lua(code)
- if code:sub(1, 1) == '=' then code = 'return '..code:sub(2) end
- local f, err = load(code, nil, 'bt', env)
- assert(f, err)
- local result = f()
- if result ~= nil then ui.print(result) end
+ if code:find('^=') then code = 'return '..code:sub(2) end
+ local result = assert(load(code, nil, 'bt', env))()
+ if result ~= nil or code:find('^return ') then ui.print(result) end
events.emit(events.UPDATE_UI)
end
args.register('-e', '--execute', 1, execute_lua, 'Execute Lua code')
@@ -113,36 +111,32 @@ args.register('-e', '--execute', 1, execute_lua, 'Execute Lua code')
-- @param code The Lua code to complete. The default value is the value of
-- `entry_text`.
local function complete_lua(code)
- local substring = (code or M.entry_text):match('[%w_.:]+$') or ''
- local path, op, prefix = substring:match('^([%w_.:]-)([.:]?)([%w_]*)$')
- local f, err = load('return ('..path..')', nil, 'bt', env)
- local ok, result = pcall(f)
+ if not code then code = M.entry_text end
+ local symbol, op, part = code:match('([%w_.]-)([%.:]?)([%w_]*)$')
+ local ok, result = pcall((load('return ('..symbol..')', nil, 'bt', env)))
local cmpls = {}
- prefix = '^'..prefix
+ part = '^'..part
+ if (not ok or type(result) ~= 'table') and symbol ~= '' then return end
if not ok then -- shorthand notation
- for _, t in ipairs{buffer, view, ui, _G} do
- for k in pairs(t) do
- if type(k) == 'string' and k:find(prefix) then cmpls[#cmpls + 1] = k end
+ local pool = {
+ buffer, view, ui, _G, _SCINTILLA.functions, _SCINTILLA.properties
+ }
+ for i = 1, #pool do
+ for k in pairs(pool[i]) do
+ if type(k) == 'string' and k:find(part) then cmpls[#cmpls + 1] = k end
end
end
- for f in pairs(_SCINTILLA.functions) do
- if f:find(prefix) then cmpls[#cmpls + 1] = f end
- end
- for p in pairs(_SCINTILLA.properties) do
- if p:find(prefix) then cmpls[#cmpls + 1] = p end
- end
else
- if type(result) ~= 'table' then return end
for k in pairs(result) do
- if type(k) == 'string' and k:find(prefix) then cmpls[#cmpls + 1] = k end
+ if type(k) == 'string' and k:find(part) then cmpls[#cmpls + 1] = k end
end
- if path == 'buffer' and op == ':' then
+ if symbol == 'buffer' and op == ':' then
for f in pairs(_SCINTILLA.functions) do
- if f:find(prefix) then cmpls[#cmpls + 1] = f end
+ if f:find(part) then cmpls[#cmpls + 1] = f end
end
- elseif path == 'buffer' and op == '.' then
+ elseif symbol == 'buffer' and op == '.' then
for p in pairs(_SCINTILLA.properties) do
- if p:find(prefix) then cmpls[#cmpls + 1] = p end
+ if p:find(part) then cmpls[#cmpls + 1] = p end
end
end
end
@@ -152,8 +146,7 @@ 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}
}
-- Pass command entry keys to the default keypress handler.
diff --git a/modules/textadept/editing.lua b/modules/textadept/editing.lua
index b6235195..4533d43c 100644
--- a/modules/textadept/editing.lua
+++ b/modules/textadept/editing.lua
@@ -506,10 +506,10 @@ function M.filter_through(command)
local p = io.popen(cmd)
if s ~= e then
buffer.target_start, buffer.target_end = s, e
- buffer:replace_target(p:read('*all'))
+ buffer:replace_target(p:read('*a'))
buffer:set_sel(buffer.target_start, buffer.target_end)
else
- buffer:set_text(p:read('*all'))
+ buffer:set_text(p:read('*a'))
buffer:goto_pos(s)
end
p:close()
diff --git a/modules/textadept/file_types.lua b/modules/textadept/file_types.lua
index 9d7e4600..4433dc04 100644
--- a/modules/textadept/file_types.lua
+++ b/modules/textadept/file_types.lua
@@ -54,33 +54,32 @@ local function get_lexer(buffer, current)
return current and lexer:match('[^/]+$') or lexer:match('^[^/]+')
end
+-- Attempts to detect the language based on a buffer's first line of text or
+-- that buffer's filename.
+-- @param buffer The buffer to detect the language of.
+-- @return lexer language
+local function detect_language(buffer)
+ local line = buffer:get_line(0)
+ -- Detect from shebang line.
+ if line:find('^#!') then
+ for word in line:gsub('[/\\]', ' '):gmatch('%S+') do
+ if M.shebangs[word] then return M.shebangs[word] end
+ end
+ end
+ -- Detect from first line.
+ for patt, lexer in pairs(M.patterns) do
+ if line:find(patt) then return lexer end
+ end
+ -- Detect from file extension.
+ return buffer.filename and M.extensions[buffer.filename:match('[^/\\.]+$')] or
+ 'text'
+end
+
local SETDIRECTPOINTER = _SCINTILLA.properties.doc_pointer[2]
local SETLEXERLANGUAGE = _SCINTILLA.properties.lexer_language[2]
-- LuaDoc is in core/.buffer.luadoc.
local function set_lexer(buffer, lang)
- -- If no language was given, attempt to detect it.
- if not lang then
- local line = buffer:get_line(0)
- -- Detect from shebang line.
- if line:find('^#!') then
- for word in line:gsub('[/\\]', ' '):gmatch('%S+') do
- if M.shebangs[word] then lang = M.shebangs[word] break end
- end
- end
- -- Detect from first line.
- if not lang then
- for patt, lexer in pairs(M.patterns) do
- if line:find(patt) then lang = lexer break end
- end
- end
- -- Detect from file extension.
- if not lang and buffer.filename then
- lang = M.extensions[buffer.filename:match('[^/\\.]+$')]
- end
- if not lang then lang = 'text' end
- end
-
- -- Set the lexer and load its language module.
+ if not lang then lang = detect_language(buffer) end
buffer:private_lexer_call(SETDIRECTPOINTER, buffer.direct_pointer)
buffer:private_lexer_call(SETLEXERLANGUAGE, lang)
buffer._lexer = lang
diff --git a/modules/textadept/find.lua b/modules/textadept/find.lua
index bd9c8825..e293a7c4 100644
--- a/modules/textadept/find.lua
+++ b/modules/textadept/find.lua
@@ -231,7 +231,7 @@ function M.find_in_files(dir)
local text = M.find_entry_text
if not M.lua then text = text:gsub('([().*+?^$%%[%]-])', '%%%1') end
if not M.match_case then text = text:lower() end
- if M.whole_word then text = '%f[%w_]'..text..'%f[^%w_]' end
+ if M.whole_word then text = '%f[%w_]'..text..'%f[^%w_]' end -- TODO: wordchars
local matches = {_L['Find:']..' '..text}
lfs.dir_foreach(dir, function(file)
local match_case = M.match_case
@@ -271,8 +271,7 @@ local function replace(rtext)
end
local ok, rtext = pcall(rtext.gsub, rtext, '%%(%b())', function(code)
code = code:gsub('[\a\b\f\n\r\t\v\\]', escapes)
- local ok, result = pcall(load('return '..code))
- assert(ok, result)
+ local result = assert(load('return '..code))()
return result:gsub('\\[abfnrtv\\]', escapes)
end)
if ok then
diff --git a/modules/textadept/init.lua b/modules/textadept/init.lua
index 795768b6..4d1618c3 100644
--- a/modules/textadept/init.lua
+++ b/modules/textadept/init.lua
@@ -12,8 +12,8 @@ module('textadept')]]
M.bookmarks = require('textadept.bookmarks')
require('textadept.command_entry')
M.editing = require('textadept.editing')
-require('textadept.find')
M.file_types = require('textadept.file_types')
+require('textadept.find')
M.run = require('textadept.run')
M.session = require('textadept.session')
M.snippets = require('textadept.snippets')
diff --git a/modules/textadept/session.lua b/modules/textadept/session.lua
index d2e1a921..fd5a595d 100644
--- a/modules/textadept/session.lua
+++ b/modules/textadept/session.lua
@@ -52,8 +52,8 @@ function M.load(filename)
local lfs_attributes = lfs.attributes
for line in f:lines() do
if line:find('^buffer:') then
- local anchor, current_pos, first_visible_line, filename =
- line:match('^buffer: (%d+) (%d+) (%d+) (.+)$')
+ local patt = '^buffer: (%d+) (%d+) (%d+) (.+)$'
+ local anchor, current_pos, first_visible_line, filename = line:match(patt)
if not filename:find('^%[.+%]$') then
if lfs_attributes(filename) then
io.open_file(filename)
@@ -65,9 +65,8 @@ function M.load(filename)
events.emit(events.FILE_OPENED, filename)
end
-- Restore saved buffer selection and view.
- local anchor = tonumber(anchor) or 0
- local current_pos = tonumber(current_pos) or 0
- local first_visible_line = tonumber(first_visible_line) or 0
+ anchor, current_pos = tonumber(anchor) or 0, tonumber(current_pos) or 0
+ first_visible_line = tonumber(first_visible_line) or 0
buffer._anchor, buffer._current_pos = anchor, current_pos
buffer._first_visible_line = first_visible_line
buffer:line_scroll(0, buffer:visible_from_doc_line(first_visible_line))
diff --git a/modules/textadept/snippets.lua b/modules/textadept/snippets.lua
index 54127fb5..e373e6aa 100644
--- a/modules/textadept/snippets.lua
+++ b/modules/textadept/snippets.lua
@@ -86,9 +86,7 @@ local newlines = {[0] = '\r\n', '\r', '\n'}
-- @param trigger The trigger text used to expand the snippet, if any.
local function new_snippet(text, trigger)
local snippet = setmetatable({
- trigger = trigger,
- original_sel_text = buffer:get_sel_text(),
- snapshots = {}
+ trigger = trigger, original_sel_text = buffer:get_sel_text(), snapshots = {}
}, {__index = M._snippet_mt})
snippet_stack[#snippet_stack + 1] = snippet
@@ -139,13 +137,10 @@ function M._insert(text)
local lexer = buffer:get_lexer(true)
trigger = buffer:text_range(buffer:word_start_position(buffer.current_pos),
buffer.current_pos)
- local snip = snippets
- text = snip[trigger]
- if type(snip) == 'table' and snip[lexer] then snip = snip[lexer] end
- text = snip[trigger] or text
+ text = type(M[lexer]) == 'table' and M[lexer][trigger] or M[trigger]
end
- local snippet = snippet_stack[#snippet_stack]
- if type(text) == 'string' then snippet = new_snippet(text, trigger) end
+ local snippet = text and new_snippet(text, trigger) or
+ snippet_stack[#snippet_stack]
if not snippet then return false end
snippet:next()
end
@@ -174,7 +169,6 @@ end
-- @name _select
function M._select()
local list, t = {}, {}
- local type = type
for trigger, text in pairs(snippets) do
if type(text) == 'string' then list[#list + 1] = trigger..'\0 \0'..text end
end
@@ -214,17 +208,13 @@ M._snippet_mt = {
-- Gets a snippet's end position in the buffer.
-- @param snippet The snippet returned by `new_snippet()`.
get_end_position = function(snippet)
- local e = buffer:indicator_end(INDIC_SNIPPET, snippet.start_position + 1)
- if e == 0 then e = snippet.start_position end
- return e
+ return buffer:indicator_end(INDIC_SNIPPET, snippet.start_position + 1)
end,
-- Gets the text for a snippet.
-- @param snippet The snippet returned by `new_snippet()`.
get_text = function(snippet)
- local s, e = snippet.start_position, snippet:get_end_position()
- local ok, text = pcall(buffer.text_range, buffer, s, e)
- return ok and text or ''
+ return buffer:text_range(snippet.start_position, snippet:get_end_position())
end,
-- Sets the text for a snippet.
@@ -272,7 +262,7 @@ M._snippet_mt = {
-- Shell code.
escaped_text = escaped_text:gsub('%%'..index..'%[([^%]]*)%]', function(code)
local p = io.popen(snippet.unescape_text(code, true))
- local result = p:read('*all'):sub(1, -2) -- chop '\n'
+ local result = p:read('*a'):sub(1, -2) -- chop '\n'
p:close()
return result
end)