diff options
-rw-r--r-- | core/file_io.lua | 169 | ||||
-rw-r--r-- | core/gui.lua | 103 | ||||
-rw-r--r-- | core/keys.lua | 9 | ||||
-rw-r--r-- | modules/lua/init.lua | 35 | ||||
-rw-r--r-- | modules/textadept/adeptsense.lua | 5 | ||||
-rw-r--r-- | modules/textadept/bookmarks.lua | 5 | ||||
-rw-r--r-- | modules/textadept/command_entry.lua | 15 | ||||
-rw-r--r-- | modules/textadept/editing.lua | 19 | ||||
-rw-r--r-- | modules/textadept/find.lua | 15 | ||||
-rw-r--r-- | modules/textadept/keys.lua | 2 | ||||
-rw-r--r-- | modules/textadept/mime_types.lua | 52 | ||||
-rw-r--r-- | modules/textadept/run.lua | 5 | ||||
-rw-r--r-- | modules/textadept/session.lua | 16 | ||||
-rw-r--r-- | modules/textadept/snippets.lua | 2 |
14 files changed, 182 insertions, 270 deletions
diff --git a/core/file_io.lua b/core/file_io.lua index a5c42a20..de0e793e 100644 --- a/core/file_io.lua +++ b/core/file_io.lua @@ -18,13 +18,12 @@ module('io', package.seeall) -- -- Example: -- --- events.connect(events.FILE_OPENED, --- function(utf8_filename) --- local filename = utf8_filename:iconv(_CHARSET, 'UTF-8') --- local f = io.open(filename, 'rb') --- -- process file --- f:close() --- end) +-- events.connect(events.FILE_OPENED, function(utf8_filename) +-- local filename = utf8_filename:iconv(_CHARSET, 'UTF-8') +-- local f = io.open(filename, 'rb') +-- -- process file +-- f:close() +-- end) -- -- [string_iconv]: ../modules/string.html#iconv -- @@ -95,80 +94,7 @@ end -- List of encodings to try to decode files as after UTF-8. -- @class table -- @name try_encodings -try_encodings = { - 'UTF-8', 'ASCII', 'ISO-8859-1', 'MacRoman' -} - --- Opens a file or goes to its already open buffer. --- @param utf8_filename The absolute path to the file to open. Must be UTF-8 --- encoded. -local function _open(utf8_filename) - if not utf8_filename then return end - utf8_filename = utf8_filename:gsub('^file://', '') - if WIN32 then utf8_filename = utf8_filename:gsub('/', '\\') end - for i, buffer in ipairs(_BUFFERS) do - if utf8_filename == buffer.filename then - view:goto_buffer(i) - return - end - end - - local text - local filename = utf8_filename:iconv(_CHARSET, 'UTF-8') - local f, err = io.open(filename, 'rb') - if not f then error(err) end - text = f:read('*all') - f:close() - if not text then return end -- filename exists, but cannot read it - local buffer = new_buffer() - -- Tries to detect character encoding and convert text from it to UTF-8. - local encoding, encoding_bom = detect_encoding(text) - if encoding ~= 'binary' then - if encoding then - if encoding_bom then text = text:sub(#encoding_bom + 1, -1) end - text = text:iconv('UTF-8', encoding) - else - -- Try list of encodings. - for _, try_encoding in ipairs(try_encodings) do - local ret, conv = pcall(string.iconv, text, 'UTF-8', try_encoding) - if ret then - encoding = try_encoding - text = conv - break - end - end - if not encoding then error(L('Encoding conversion failed.')) end - end - else - encoding = nil - end - local c = _SCINTILLA.constants - buffer.encoding, buffer.encoding_bom = encoding, encoding_bom - buffer.code_page = encoding and c.SC_CP_UTF8 or 0 - -- Tries to set the buffer's EOL mode appropriately based on the file. - local s, e = text:find('\r\n?') - if s and e then - buffer.eol_mode = (s == e and c.SC_EOL_CR or c.SC_EOL_CRLF) - else - buffer.eol_mode = c.SC_EOL_LF - end - buffer:add_text(text, #text) - buffer:goto_pos(0) - buffer:empty_undo_buffer() - buffer.modification_time = lfs.attributes(filename).modification - buffer.filename = utf8_filename - buffer:set_save_point() - events.emit(events.FILE_OPENED, utf8_filename) - - -- Add file to recent files list, eliminating duplicates. - for i, file in ipairs(recent_files) do - if file == utf8_filename then - table.remove(recent_files, i) - break - end - end - table.insert(recent_files, 1, utf8_filename) -end +try_encodings = { 'UTF-8', 'ASCII', 'ISO-8859-1', 'MacRoman' } --- -- Opens a list of files. @@ -182,7 +108,61 @@ function open_file(utf8_filenames) '--select-multiple', '--with-directory', (buffer.filename or ''):match('.+[/\\]') or '') - for filename in utf8_filenames:gmatch('[^\n]+') do _open(filename) end + for utf8_filename in utf8_filenames:gmatch('[^\n]+') do + utf8_filename = utf8_filename:gsub('^file://', '') + if WIN32 then utf8_filename = utf8_filename:gsub('/', '\\') end + for i, buffer in ipairs(_BUFFERS) do + if utf8_filename == buffer.filename then view:goto_buffer(i) return end + end + + local filename = utf8_filename:iconv(_CHARSET, 'UTF-8') + local f, err = io.open(filename, 'rb') + if not f then error(err) end + local text = f:read('*all') + f:close() + if not text then return end -- filename exists, but cannot read it + local buffer = new_buffer() + -- Tries to detect character encoding and convert text from it to UTF-8. + local encoding, encoding_bom = detect_encoding(text) + if encoding ~= 'binary' then + if encoding then + if encoding_bom then text = text:sub(#encoding_bom + 1, -1) end + text = text:iconv('UTF-8', encoding) + else + -- Try list of encodings. + for _, try_encoding in ipairs(try_encodings) do + local ok, conv = pcall(string.iconv, text, 'UTF-8', try_encoding) + if ok then encoding, text = try_encoding, conv break end + end + if not encoding then error(L('Encoding conversion failed.')) end + end + else + encoding = nil + end + local c = _SCINTILLA.constants + buffer.encoding, buffer.encoding_bom = encoding, encoding_bom + buffer.code_page = encoding and c.SC_CP_UTF8 or 0 + -- Tries to set the buffer's EOL mode appropriately based on the file. + local s, e = text:find('\r\n?') + if s and e then + buffer.eol_mode = (s == e and c.SC_EOL_CR or c.SC_EOL_CRLF) + else + buffer.eol_mode = c.SC_EOL_LF + end + buffer:add_text(text, #text) + buffer:goto_pos(0) + buffer:empty_undo_buffer() + buffer.modification_time = lfs.attributes(filename).modification + buffer.filename = utf8_filename + buffer:set_save_point() + events.emit(events.FILE_OPENED, utf8_filename) + + -- Add file to recent files list, eliminating duplicates. + for i, file in ipairs(recent_files) do + if file == utf8_filename then table.remove(recent_files, i) break end + end + table.insert(recent_files, 1, utf8_filename) + end end -- LuaDoc is in core/.buffer.luadoc. @@ -231,8 +211,7 @@ local function save(buffer) events.emit(events.FILE_BEFORE_SAVE, buffer.filename) local text = buffer:get_text(buffer.length) if buffer.encoding then - local bom = buffer.encoding_bom or '' - text = bom..text:iconv(buffer.encoding, 'UTF-8') + text = (buffer.encoding_bom or '')..text:iconv(buffer.encoding, 'UTF-8') end local filename = buffer.filename:iconv(_CHARSET, 'UTF-8') local f, err = io.open(filename, 'wb') @@ -282,16 +261,14 @@ end local function close(buffer) if not buffer then buffer = _G.buffer end buffer:check_global() - if buffer.dirty and - gui.dialog('msgbox', - '--title', L('Close without saving?'), - '--text', L('There are unsaved changes in'), - '--informative-text', - string.format('%s', (buffer.filename or - buffer._type or L('Untitled'))), - '--button1', 'gtk-cancel', - '--button2', L('Close _without saving'), - '--no-newline') ~= '2' then + local filename = buffer.filename or buffer._type or L('Untitled') + if buffer.dirty and gui.dialog('msgbox', + '--title', L('Close without saving?'), + '--text', L('There are unsaved changes in'), + '--informative-text', filename, + '--button1', 'gtk-cancel', + '--button2', L('Close _without saving'), + '--no-newline') ~= '2' then return false end buffer:delete() @@ -327,8 +304,8 @@ local function update_modified_file() '--title', L('Reload?'), '--text', L('Reload modified file?'), '--informative-text', - string.format('"%s"\n%s', utf8_filename, - L('has been modified. Reload it?')), + ('"%s"\n%s'):format(utf8_filename, + L('has been modified. Reload it?')), '--no-cancel', '--no-newline') == '1' then buffer:reload() diff --git a/core/gui.lua b/core/gui.lua index 40e6ce4a..d838b4ac 100644 --- a/core/gui.lua +++ b/core/gui.lua @@ -3,36 +3,21 @@ local L = locale.localize local gui = gui --- TODO: --- @param buffer_type --- @param ... +-- LuaDoc is in core/.gui.luadoc. local function _print(buffer_type, ...) if buffer._type ~= buffer_type then - -- Try to find a message buffer to print to. Otherwise create one. - local message_buffer, message_view - for _, buffer in ipairs(_BUFFERS) do - if buffer._type == buffer_type then - message_buffer = buffer - for _, view in ipairs(_VIEWS) do - if view.buffer == message_buffer then - message_view = view - break - end - end - break - end + for i, view in ipairs(_VIEWS) do + if view.buffer._type == buffer_type then gui.goto_view(i) break end end - if not message_view then - local _, message_view = view:split() - if not message_buffer then - message_buffer = new_buffer() - message_buffer._type = buffer_type + if view.buffer._type ~= buffer_type then + view:split() + for i, buffer in ipairs(_BUFFERS) do + if buffer._type == buffer_type then view:goto_buffer(i) break end + end + if buffer._type ~= buffer_type then + new_buffer()._type = buffer_type events.emit(events.FILE_OPENED) - else - message_view:goto_buffer(_BUFFERS[message_buffer]) end - else - gui.goto_view(_VIEWS[message_view]) end end local args, n = {...}, select('#', ...) @@ -107,39 +92,37 @@ connect(events.VIEW_NEW, function() events.emit(events.UPDATE_UI) end) local SETDIRECTFUNCTION = _SCINTILLA.properties.direct_function[1] local SETDIRECTPOINTER = _SCINTILLA.properties.doc_pointer[2] local SETLEXERLANGUAGE = _SCINTILLA.functions.set_lexer_language[1] +local function set_properties() + local buffer = buffer + -- Lexer. + buffer:set_lexer_language('lpeg') + buffer:private_lexer_call(SETDIRECTFUNCTION, buffer.direct_function) + buffer:private_lexer_call(SETDIRECTPOINTER, buffer.direct_pointer) + buffer:private_lexer_call(SETLEXERLANGUAGE, 'container') + buffer.style_bits = 8 + -- Properties. + buffer.property['textadept.home'] = _HOME + buffer.property['lexer.lpeg.home'] = _LEXERPATH + buffer.property['lexer.lpeg.script'] = _HOME..'/lexers/lexer.lua' + if _THEME and #_THEME > 0 then + buffer.property['lexer.lpeg.color.theme'] = _THEME..'/lexer.lua' + end + -- Buffer. + buffer.code_page = _SCINTILLA.constants.SC_CP_UTF8 + -- Load theme. + if _THEME and #_THEME > 0 then + local ok, err = pcall(dofile, _THEME..'/buffer.lua') + if not ok then io.stderr:write(err) end + end +end + -- Sets default properties for a Scintilla document. connect(events.BUFFER_NEW, function() - local function run() - local buffer = buffer - - -- Lexer. - buffer:set_lexer_language('lpeg') - buffer:private_lexer_call(SETDIRECTFUNCTION, buffer.direct_function) - buffer:private_lexer_call(SETDIRECTPOINTER, buffer.direct_pointer) - buffer:private_lexer_call(SETLEXERLANGUAGE, 'container') - buffer.style_bits = 8 - - -- Properties. - buffer.property['textadept.home'] = _HOME - buffer.property['lexer.lpeg.home'] = _LEXERPATH - buffer.property['lexer.lpeg.script'] = _HOME..'/lexers/lexer.lua' - if _THEME and #_THEME > 0 then - buffer.property['lexer.lpeg.color.theme'] = _THEME..'/lexer.lua' - end - - -- Buffer. - buffer.code_page = _SCINTILLA.constants.SC_CP_UTF8 - - if _THEME and #_THEME > 0 then - local ok, err = pcall(dofile, _THEME..'/buffer.lua') - if not ok then io.stderr:write(err) end - end - end -- Normally when an error occurs, a new buffer is created with the error -- message, but if an error occurs here, this event would be called again and -- again, erroring each time resulting in an infinite loop; print error to -- stderr instead. - local ok, err = pcall(run) + local ok, err = pcall(set_properties) if not ok then io.stderr:write(err) end end) connect(events.BUFFER_NEW, function() events.emit(events.UPDATE_UI) end) @@ -259,15 +242,13 @@ connect(events.QUIT, function() list[#list + 1] = buffer.filename or buffer._type or L('Untitled') end end - if #list > 0 and - gui.dialog('msgbox', - '--title', L('Quit without saving?'), - '--text', L('The following buffers are unsaved:'), - '--informative-text', - string.format('%s', table.concat(list, '\n')), - '--button1', 'gtk-cancel', - '--button2', L('Quit _without saving'), - '--no-newline') ~= '2' then + if #list > 0 and gui.dialog('msgbox', + '--title', L('Quit without saving?'), + '--text', L('The following buffers are unsaved:'), + '--informative-text', table.concat(list, '\n'), + '--button1', 'gtk-cancel', + '--button2', L('Quit _without saving'), + '--no-newline') ~= '2' then return false end return true diff --git a/core/keys.lua b/core/keys.lua index 400d06dd..4d438426 100644 --- a/core/keys.lua +++ b/core/keys.lua @@ -167,8 +167,8 @@ local function run_command(command, command_type) end end end - local _, ret = xpcall(function() return f(unpack(args, 2)) end, error) - return ret + local _, result = xpcall(function() return f(unpack(args, 2)) end, error) + return result end _M.run_command = run_command -- export for menu.lua without creating LuaDoc @@ -276,10 +276,7 @@ local function get_gdk_key(key_seq) local byte = string_byte(key) if #key > 1 or byte < 32 then for i, s in pairs(KEYSYMS) do - if s == key and i ~= 0xFE20 then - byte = i - break - end + if s == key and i ~= 0xFE20 then byte = i break end end end return byte, modifiers diff --git a/modules/lua/init.lua b/modules/lua/init.lua index 843d55ac..8a7fb860 100644 --- a/modules/lua/init.lua +++ b/modules/lua/init.lua @@ -150,25 +150,22 @@ function goto_required() end end -events.connect(events.FILE_AFTER_SAVE, - function() -- show syntax errors as annotations - if buffer:get_lexer() == 'lua' then - local buffer = buffer - buffer:annotation_clear_all() - local text = buffer:get_text() - text = text:gsub('^#![^\n]+', '') -- ignore shebang line - local _, err = loadstring(text) - if err then - local line, msg = err:match('^.-:(%d+):%s*(.+)$') - if line then - buffer.annotation_visible = 2 - buffer:annotation_set_text(line - 1, msg) - buffer.annotation_style[line - 1] = 8 -- error style number - buffer:goto_line(line - 1) - end - end - end - end) +-- Show syntax errors as annotations. +events.connect(events.FILE_AFTER_SAVE, function() + if buffer:get_lexer() ~= 'lua' then return end + local buffer = buffer + buffer:annotation_clear_all() + local text = buffer:get_text():gsub('^#![^\n]+', '') -- ignore shebang line + local f, err = loadstring(text) + if f then return end + local line, msg = err:match('^.-:(%d+):%s*(.+)$') + if line then + buffer.annotation_visible = 2 + buffer:annotation_set_text(line - 1, msg) + buffer.annotation_style[line - 1] = 8 -- error style number + buffer:goto_line(line - 1) + end +end) --- -- Container for Lua-specific key commands. diff --git a/modules/textadept/adeptsense.lua b/modules/textadept/adeptsense.lua index f54c3064..20dae39d 100644 --- a/modules/textadept/adeptsense.lua +++ b/modules/textadept/adeptsense.lua @@ -575,10 +575,7 @@ function get_apidoc(sense, symbol) if entity == '' then class = sense:get_class(entity) end if type(class) ~= 'string' then class = entity end -- fall back to entity for i, apidoc in ipairs(apidocs) do - if apidoc:sub(1, #class) == class then - apidocs.pos = i - break - end + if apidoc:sub(1, #class) == class then apidocs.pos = i break end end return apidocs end diff --git a/modules/textadept/bookmarks.lua b/modules/textadept/bookmarks.lua index 57e3da64..a032d19c 100644 --- a/modules/textadept/bookmarks.lua +++ b/modules/textadept/bookmarks.lua @@ -86,5 +86,6 @@ function goto() end if buffer then buffer:marker_set_back(MARK_BOOKMARK, MARK_BOOKMARK_COLOR) end -events.connect(events.VIEW_NEW, - function() buffer:marker_set_back(MARK_BOOKMARK, MARK_BOOKMARK_COLOR) end) +events.connect(events.VIEW_NEW, function() + buffer:marker_set_back(MARK_BOOKMARK, MARK_BOOKMARK_COLOR) +end) diff --git a/modules/textadept/command_entry.lua b/modules/textadept/command_entry.lua index b2eab2c1..b574f9dd 100644 --- a/modules/textadept/command_entry.lua +++ b/modules/textadept/command_entry.lua @@ -19,10 +19,7 @@ local env = setmetatable({}, { end, __newindex = function(t, k, v) for _, t2 in ipairs{ buffer, view, gui } do - if t2[k] ~= nil then - t2[k] = v - return - end + if t2[k] ~= nil then t2[k] = v return end end rawset(t, k, v) end, @@ -33,18 +30,16 @@ events.connect(events.COMMAND_ENTRY_COMMAND, function(command) local f, err = loadstring(command) if err then error(err) end gui.command_entry.focus() -- toggle focus to hide - setfenv(f, env) - f() + setfenv(f, env)() events.emit(events.UPDATE_UI) end) events.connect(events.COMMAND_ENTRY_KEYPRESS, function(code) - local ce = gui.command_entry if keys.KEYSYMS[code] == 'esc' then - ce.focus() -- toggle focus to hide + gui.command_entry.focus() -- toggle focus to hide return true elseif keys.KEYSYMS[code] == '\t' then - local substring = ce.entry_text:match('[%w_.:]+$') or '' + local substring = gui.command_entry.entry_text:match('[%w_.:]+$') or '' local path, o, prefix = substring:match('^([%w_.:]-)([.:]?)([%w_]*)$') local f, err = loadstring('return ('..path..')') if type(f) == "function" then setfenv(f, env) end @@ -83,7 +78,7 @@ events.connect(events.COMMAND_ENTRY_KEYPRESS, function(code) end end table.sort(cmpls) - ce.show_completions(cmpls) + gui.command_entry.show_completions(cmpls) return true end end) diff --git a/modules/textadept/editing.lua b/modules/textadept/editing.lua index bb859b7e..cb340cfc 100644 --- a/modules/textadept/editing.lua +++ b/modules/textadept/editing.lua @@ -199,8 +199,7 @@ function autocomplete_word(word_chars) local s, e = buffer_text:find(patt, match_pos + 1) local match = buffer_text:sub(s, e) if not completions[match] and #match > #root then - c_list[#c_list + 1] = match - completions[match] = true + c_list[#c_list + 1], completions[match] = match, true end buffer.target_start, buffer.target_end = match_pos + 1, buffer.length match_pos = buffer:search_in_target(root) @@ -277,12 +276,10 @@ function prepare_for_save() local buffer = buffer buffer:begin_undo_action() -- Strip trailing whitespace. - local line_end_position = buffer.line_end_position - local char_at = buffer.char_at + local line_end_position, char_at = buffer.line_end_position, buffer.char_at local lines = buffer.line_count for line = 0, lines - 1 do - local s = buffer:position_from_line(line) - local e = line_end_position[line] + local s, e = buffer:position_from_line(line), line_end_position[line] local i = e - 1 local c = char_at[i] while i >= s and c == 9 or c == 32 do @@ -312,9 +309,8 @@ events.connect(events.FILE_BEFORE_SAVE, prepare_for_save) -- is deleted. function current_word(action) local buffer = buffer - local s = buffer:word_start_position(buffer.current_pos) - local e = buffer:word_end_position(buffer.current_pos) - buffer:set_sel(s, e) + buffer:set_sel(buffer:word_start_position(buffer.current_pos), + buffer:word_end_position(buffer.current_pos)) if action == 'delete' then buffer:delete_back() end end @@ -478,8 +474,9 @@ local function clear_highlighted_words() buffer.indicator_current = INDIC_HIGHLIGHT buffer:indicator_clear_range(0, buffer.length) end -events.connect(events.KEYPRESS, - function(code) if K[code] == 'esc' then clear_highlighted_words() end end) +events.connect(events.KEYPRESS, function(code) + if K[code] == 'esc' then clear_highlighted_words() end +end) --- -- Highlights all occurances of the word under the caret and adds markers to the diff --git a/modules/textadept/find.lua b/modules/textadept/find.lua index 409ffe4c..8debc7ec 100644 --- a/modules/textadept/find.lua +++ b/modules/textadept/find.lua @@ -238,8 +238,7 @@ local function replace(rtext) local captures = find.captures if captures then for i = 1, #captures do - local v = captures[i]:gsub('%%', '%%%%') -- escape '%' for gsub - rtext = rtext:gsub('%%'..i, v) + rtext = rtext:gsub('%%'..i, captures[i]:gsub('%%', '%%%%')) end end local ok, rtext = pcall(rtext.gsub, rtext, '%%(%b())', run) @@ -301,6 +300,7 @@ local function replace_all(ftext, rtext, flags) end events.connect(events.REPLACE_ALL, replace_all) +-- TODO: refactor? -- When the user double-clicks a found file, go to the line in the file the text -- was found at. -- @param pos The position of the caret. @@ -354,10 +354,8 @@ function find.goto_file_in_list(next) line = line + (next and 1 or -1) if line > buffer.line_count - 1 then line = 0 end if line < 0 then line = buffer.line_count - 1 end - if line == orig_line then -- prevent infinite loops - gui.goto_view(orig_view) - return - end + -- Prevent infinite loops. + if line == orig_line then gui.goto_view(orig_view) return end if buffer:get_line(line):match('^(.+):(%d+):.+$') then buffer:goto_line(line) goto_file(buffer.current_pos, line) @@ -371,5 +369,6 @@ function find.goto_file_in_list(next) end if buffer then buffer:marker_set_back(MARK_FIND, MARK_FIND_COLOR) end -events.connect(events.VIEW_NEW, - function() buffer:marker_set_back(MARK_FIND, MARK_FIND_COLOR) end) +events.connect(events.VIEW_NEW, function() + buffer:marker_set_back(MARK_FIND, MARK_FIND_COLOR) +end) diff --git a/modules/textadept/keys.lua b/modules/textadept/keys.lua index 5672933a..d3bdb1ec 100644 --- a/modules/textadept/keys.lua +++ b/modules/textadept/keys.lua @@ -94,7 +94,7 @@ local function constantize_menu_buffer_functions() for _, f in ipairs(menu_buffer_functions) do buffer[f] = buffer[f] end end events.connect(events.BUFFER_NEW, constantize_menu_buffer_functions) --- Scintilla's first buffer doesn't have this. +-- Scintilla's first buffer does not have this. if not RESETTING then constantize_menu_buffer_functions() end --[[ diff --git a/modules/textadept/mime_types.lua b/modules/textadept/mime_types.lua index 3d461db4..119d5b55 100644 --- a/modules/textadept/mime_types.lua +++ b/modules/textadept/mime_types.lua @@ -100,11 +100,7 @@ end for lexer in pairs(lexers_found) do lexers[#lexers + 1] = lexer end table.sort(lexers) --- --- Returns the name of the style associated with a style number. --- @param buffer The buffer to get the style name of. --- @param style_num A style number in the range 0 <= style_num < 256. --- @see buffer.style_at +-- LuaDoc is in core/.buffer.luadoc. local function get_style_name(buffer, style_num) buffer:check_global() if style_num < 0 or style_num > 255 then error('0 <= style_num < 256') end @@ -117,31 +113,22 @@ end -- @class table -- @name ws_styles local ws_styles = {} - local SETDIRECTPOINTER = _SCINTILLA.properties.doc_pointer[2] local SETLEXERLANGUAGE = _SCINTILLA.functions.set_lexer_language[1] --- --- Replacement for buffer:set_lexer_language(). --- Sets a buffer._lexer field so it can be restored without querying the --- mime-types tables. Also if the user manually sets the lexer, it should be --- restored. --- Loads the language-specific module if it exists. --- @param buffer The buffer to set the lexer language of. --- @param lang The string language to set. --- @usage buffer:set_lexer('language_name') +-- LuaDoc is in core/.buffer.luadoc. local function set_lexer(buffer, lang) buffer:check_global() buffer._lexer = lang buffer:private_lexer_call(SETDIRECTPOINTER, buffer.direct_pointer) buffer:private_lexer_call(SETLEXERLANGUAGE, lang) - local ret, err = pcall(require, lang) - if ret then - ret, err = pcall(require, lang..'.post_init') + local ok, err = pcall(require, lang) + if ok then + ok, err = pcall(require, lang..'.post_init') _m[lang].set_buffer_properties() events.emit(events.LANGUAGE_MODULE_LOADED, lang) end local module_not_found = "^module '"..lang.."[^\']*' not found:" - if not ret and not err:find(module_not_found) then error(err) end + if not ok and not err:find(module_not_found) then error(err) end buffer:colourise(0, -1) if ws_styles[lang] then return end @@ -154,12 +141,7 @@ local function set_lexer(buffer, lang) end local GETLEXERLANGUAGE = _SCINTILLA.functions.get_lexer_language[1] --- --- Replacement for buffer:get_lexer_language(). --- @param buffer The buffer to get the lexer language of. --- @param current If true, returns the lexer at the current caret position. This --- lexer can be different from the lexer passed to buffer:set_lexer(). --- Defaults to false. +-- LuaDoc is in core/.buffer.luadoc. local function get_lexer(buffer, current) buffer:check_global() local lexer = buffer:private_lexer_call(GETLEXERLANGUAGE) @@ -173,7 +155,7 @@ events.connect(events.BUFFER_NEW, function() buffer.set_lexer, buffer.get_lexer = set_lexer, get_lexer buffer.get_style_name = get_style_name end, 1) --- Scintilla's first buffer doesn't have these. +-- Scintilla's first buffer does not have these. if not RESETTING then buffer.set_lexer, buffer.get_lexer = set_lexer, get_lexer buffer.get_style_name = get_style_name @@ -192,10 +174,7 @@ local function handle_new() end if not lexer then for patt, lex in pairs(patterns) do - if line:find(patt) then - lexer = lex - break - end + if line:find(patt) then lexer = lex break end end end if not lexer and buffer.filename then @@ -203,6 +182,8 @@ local function handle_new() end buffer:set_lexer(lexer or 'container') end +events.connect(events.FILE_OPENED, handle_new) +events.connect(events.FILE_SAVED_AS, handle_new) -- Sets the buffer's lexer based on filename, shebang words, or -- first line pattern. @@ -210,14 +191,11 @@ local function restore_lexer() buffer:private_lexer_call(SETDIRECTPOINTER, buffer.direct_pointer) buffer:private_lexer_call(SETLEXERLANGUAGE, buffer._lexer or 'container') end +events.connect(events.BUFFER_AFTER_SWITCH, restore_lexer) +events.connect(events.VIEW_NEW, restore_lexer, 1) -local connect = events.connect -connect(events.FILE_OPENED, handle_new) -connect(events.FILE_SAVED_AS, handle_new) -connect(events.BUFFER_AFTER_SWITCH, restore_lexer) -connect(events.VIEW_NEW, restore_lexer, 1) -connect(events.RESET_AFTER, - function() buffer:set_lexer(buffer._lexer or 'container') end) +events.connect(events.RESET_AFTER, + function() buffer:set_lexer(buffer._lexer or 'container') end) --- -- Prompts the user to select a lexer from a filtered list for the current diff --git a/modules/textadept/run.lua b/modules/textadept/run.lua index 34171ce2..567178fb 100644 --- a/modules/textadept/run.lua +++ b/modules/textadept/run.lua @@ -68,9 +68,8 @@ local function command(cmd_table) if not buffer.filename then return end buffer:save() local action = cmd_table[buffer.filename:match('[^.]+$')] - if action then - return execute(type(action) == 'function' and action() or action) - end + if not action then return end + return execute(type(action) == 'function' and action() or action) end --- diff --git a/modules/textadept/session.lua b/modules/textadept/session.lua index 1178380f..9890b7f2 100644 --- a/modules/textadept/session.lua +++ b/modules/textadept/session.lua @@ -33,10 +33,7 @@ MAX_RECENT_FILES = 10 function load(filename) local not_found = {} local f = io.open(filename or DEFAULT_SESSION, 'rb') - if not f then - io.close_all() - return false - end + if not f then io.close_all() return false end local current_view, splits = 1, { [0] = {} } local lfs_attributes = lfs.attributes for line in f:lines() do @@ -50,8 +47,7 @@ function load(filename) not_found[#not_found + 1] = filename end else - new_buffer() - buffer._type = filename + new_buffer()._type = filename events.emit(events.FILE_OPENED, filename) end -- Restore saved buffer selection and view. @@ -62,7 +58,7 @@ function load(filename) 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)) + buffer:visible_from_doc_line(first_visible_line)) buffer:set_sel(anchor, current_pos) elseif line:find('^%s*split%d:') then local level, num, type, size = @@ -77,8 +73,7 @@ function load(filename) if buf_idx > #_BUFFERS then buf_idx = #_BUFFERS end view:goto_buffer(buf_idx) elseif line:find('^current_view:') then - local view_idx = line:match('^current_view: (%d+)') - current_view = tonumber(view_idx) or 1 + current_view = tonumber(line:match('^current_view: (%d+)')) or 1 elseif line:find('^size:') then local width, height = line:match('^size: (%d+) (%d+)$') if width and height then gui.size = { width, height } end @@ -97,8 +92,7 @@ function load(filename) gui.dialog('msgbox', '--title', L('Session Files Not Found'), '--text', L('The following session files were not found'), - '--informative-text', - string.format('%s', table.concat(not_found, '\n'))) + '--informative-text', table.concat(not_found, '\n')) end return true end diff --git a/modules/textadept/snippets.lua b/modules/textadept/snippets.lua index a89931d1..337e056e 100644 --- a/modules/textadept/snippets.lua +++ b/modules/textadept/snippets.lua @@ -420,7 +420,7 @@ _snippet_mt = { local INDIC_HIDDEN = _SCINTILLA.constants.INDIC_HIDDEN if buffer then buffer.indic_style[INDIC_SNIPPET] = INDIC_HIDDEN end events.connect(events.VIEW_NEW, - function() buffer.indic_style[INDIC_SNIPPET] = INDIC_HIDDEN end) + function() buffer.indic_style[INDIC_SNIPPET] = INDIC_HIDDEN end) --- -- Provides access to snippets from _G. |