aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authormitchell <70453897+667e-11@users.noreply.github.com>2012-07-21 09:34:51 -0400
committermitchell <70453897+667e-11@users.noreply.github.com>2012-07-21 09:34:51 -0400
commit5a50a7e55b870ae66a6bb69fcd3a7bd1841f3d9b (patch)
tree7b7d99c5a42cf7597d00815f941b8070439f32d7 /modules
parent53de5fafa2344ae17a0457a34c7a6a866f1020d1 (diff)
downloadtextadept-5a50a7e55b870ae66a6bb69fcd3a7bd1841f3d9b.tar.gz
textadept-5a50a7e55b870ae66a6bb69fcd3a7bd1841f3d9b.zip
Lua code cleanup and API changes.
Diffstat (limited to 'modules')
-rw-r--r--modules/lua/init.lua21
-rw-r--r--modules/textadept/editing.lua60
-rw-r--r--modules/textadept/keys.lua4
-rw-r--r--modules/textadept/run.lua7
-rw-r--r--modules/textadept/session.lua80
5 files changed, 68 insertions, 104 deletions
diff --git a/modules/lua/init.lua b/modules/lua/init.lua
index f7a0aa17..54027c23 100644
--- a/modules/lua/init.lua
+++ b/modules/lua/init.lua
@@ -13,8 +13,6 @@ local M = {}
--
-- + `Ctrl+L, M` (`⌘L, M` on Mac OSX)
-- Open this module for editing.
--- + `Ctrl+L, G` (`⌘L, G`)
--- Goto file being 'require'd on the current line.
-- + `Shift+Return` (`⇧↩`)
-- Try to autocomplete an `if`, `for`, etc. statement with `end`.
-- + `.`
@@ -131,24 +129,6 @@ function M.try_to_autocomplete_end()
return false
end
----
--- Determines the Lua file being 'require'd, searches through package.path for
--- that file, and opens it in Textadept.
--- @name goto_required
-function M.goto_required()
- local line = buffer:get_cur_line()
- local patterns = { 'require%s*(%b())', 'require%s*(([\'"])[^%2]+%2)' }
- local file
- for _, patt in ipairs(patterns) do
- file = line:match(patt)
- if file then break end
- end
- if not file then return end
- file = package.searchpath(file:sub(2, -2):iconv('UTF-8', _CHARSET),
- package.path)
- if file then io.open_file(file) end
-end
-
-- Show syntax errors as annotations.
events.connect(events.FILE_AFTER_SAVE, function()
if buffer:get_lexer() ~= 'lua' then return end
@@ -173,7 +153,6 @@ keys.lua = {
[keys.LANGUAGE_MODULE_PREFIX] = {
m = { io.open_file,
(_HOME..'/modules/lua/init.lua'):iconv('UTF-8', _CHARSET) },
- g = M.goto_required,
},
['s\n'] = M.try_to_autocomplete_end,
}
diff --git a/modules/textadept/editing.lua b/modules/textadept/editing.lua
index ea2c1d9f..c87bb9c7 100644
--- a/modules/textadept/editing.lua
+++ b/modules/textadept/editing.lua
@@ -156,6 +156,33 @@ events_connect(events.AUTO_C_SELECTION, function(text, position)
buffer:auto_c_cancel() -- tell Scintilla not to handle autocompletion normally
end)
+-- Prepares the buffer for saving to a file.
+events_connect(events.FILE_BEFORE_SAVE, function()
+ if not M.STRIP_WHITESPACE_ON_SAVE then return end
+ local buffer = buffer
+ buffer:begin_undo_action()
+ -- Strip trailing whitespace.
+ 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, e = buffer:position_from_line(line), line_end_position[line]
+ local i, c = e - 1, char_at[e - 1]
+ while i >= s and c == 9 or c == 32 do i, c = i - 1, char_at[i - 1] end
+ if i < e - 1 then
+ buffer.target_start, buffer.target_end = i + 1, e
+ buffer:replace_target('')
+ end
+ end
+ -- Ensure ending newline.
+ local e = buffer:position_from_line(lines)
+ if lines == 1 or lines > 1 and e > buffer:position_from_line(lines - 1) then
+ buffer:insert_text(e, '\n')
+ end
+ -- Convert non-consistent EOLs
+ buffer:convert_eo_ls(buffer.eol_mode)
+ buffer:end_undo_action()
+end)
+
---
-- Goes to a matching brace position, selecting the text inside if specified to.
-- @param select If `true`, selects the text between matching braces.
@@ -283,39 +310,6 @@ function M.goto_line(line)
end
---
--- Prepares the buffer for saving to a file.
--- Strips trailing whitespace off of every line if `STRIP_WHITESPACE_ON_SAVE` is
--- `true`, ensures an ending newline, and converts non-consistent EOLs.
--- @see STRIP_WHITESPACE_ON_SAVE
--- @name prepare_for_save
-function M.prepare_for_save()
- if not M.STRIP_WHITESPACE_ON_SAVE then return end
- local buffer = buffer
- buffer:begin_undo_action()
- -- Strip trailing whitespace.
- 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, e = buffer:position_from_line(line), line_end_position[line]
- local i, c = e - 1, char_at[e - 1]
- while i >= s and c == 9 or c == 32 do i, c = i - 1, char_at[i - 1] end
- if i < e - 1 then
- buffer.target_start, buffer.target_end = i + 1, e
- buffer:replace_target('')
- end
- end
- -- Ensure ending newline.
- local e = buffer:position_from_line(lines)
- if lines == 1 or lines > 1 and e > buffer:position_from_line(lines - 1) then
- buffer:insert_text(e, '\n')
- end
- -- Convert non-consistent EOLs
- buffer:convert_eo_ls(buffer.eol_mode)
- buffer:end_undo_action()
-end
-events_connect(events.FILE_BEFORE_SAVE, M.prepare_for_save)
-
----
-- Transposes characters intelligently.
-- If the caret is at the end of a line, the two characters before the caret are
-- transposed. Otherwise, the characters to the left and right are.
diff --git a/modules/textadept/keys.lua b/modules/textadept/keys.lua
index e3d7eaed..f38de109 100644
--- a/modules/textadept/keys.lua
+++ b/modules/textadept/keys.lua
@@ -177,8 +177,8 @@ keys[not OSX and 'cs' or 'ms'] = buffer.save
keys[not OSX and (not NCURSES and 'cS' or 'cms') or 'mS'] = buffer.save_as
keys[not OSX and 'cw' or 'mw'] = buffer.close
keys[not OSX and (not NCURSES and 'cW' or 'cmw') or 'mW'] = io.close_all
--- TODO: m_textadept.sessions.prompt_load
--- TODO: m_textadept.sessions.prompt_save
+-- TODO: m_textadept.sessions.load
+-- TODO: m_textadept.sessions.save
keys[not OSX and 'cq' or 'mq'] = quit
-- Edit.
diff --git a/modules/textadept/run.lua b/modules/textadept/run.lua
index e69f5441..450580c3 100644
--- a/modules/textadept/run.lua
+++ b/modules/textadept/run.lua
@@ -174,10 +174,11 @@ events_connect(RUN_OUTPUT, print_output)
M.error_detail = {}
---
--- When the user double-clicks an error message, go to the line in the file
--- the error occured at and display a calltip with the error message.
+-- Goes to the line in the file an error occured at and displays a calltip with
+-- the error message.
+-- This is typically called when the user double-clicks an error message,
-- @param pos The position of the caret.
--- @param line_num The line double-clicked.
+-- @param line_num The line the error occurs on.
-- @see error_detail
function goto_error(pos, line_num)
if buffer._type ~= _L['[Message Buffer]'] and
diff --git a/modules/textadept/session.lua b/modules/textadept/session.lua
index 2ccee81d..cee879c4 100644
--- a/modules/textadept/session.lua
+++ b/modules/textadept/session.lua
@@ -24,16 +24,26 @@ M.MAX_RECENT_FILES = 10
---
-- Loads a Textadept session file.
-- Textadept restores split views, opened buffers, cursor information, and
--- project manager details.
--- @param filename The absolute path to the session file to load. The default
--- value is `DEFAULT_SESSION`.
+-- recent files.
+-- @param filename The absolute path to the session file to load. If `nil`, the
+-- user is prompted for one.
-- @return `true` if the session file was opened and read; `false` otherwise.
-- @usage _M.textadept.session.load(filename)
-- @see DEFAULT_SESSION
-- @name load
function M.load(filename)
+ filename = filename or gui.dialog('fileselect',
+ '--title', _L['Load Session'],
+ '--button1', _L['_Open'],
+ '--button2', _L['_Cancel'],
+ '--with-directory',
+ M.DEFAULT_SESSION:match('.+[/\\]') or '',
+ '--with-file',
+ M.DEFAULT_SESSION:match('[^/\\]+$') or '',
+ '--no-newline'):iconv(_CHARSET, 'UTF-8')
+ if filename == '' then return end
local not_found = {}
- local f = io.open(filename or M.DEFAULT_SESSION, 'rb')
+ local f = io.open(filename, 'rb')
if not f then io.close_all() return false end
local current_view, splits = 1, { [0] = {} }
local lfs_attributes = lfs.attributes
@@ -101,18 +111,29 @@ function M.load(filename)
return true
end
-- Load session when no args are present.
-events.connect('arg_none', function() if M.SAVE_ON_QUIT then M.load() end end)
+events.connect('arg_none', function()
+ if M.SAVE_ON_QUIT then M.load(M.DEFAULT_SESSION) end
+end)
---
-- Saves a Textadept session to a file.
--- Saves split views, opened buffers, cursor information, and project manager
--- details.
--- @param filename The absolute path to the session file to save. The default
--- value is either the current session file or `DEFAULT_SESSION`.
+-- Saves split views, opened buffers, cursor information, and recent files.
+-- @param filename The absolute path to the session file to save. If `nil`, the
+-- user is prompted for one.
-- @usage _M.textadept.session.save(filename)
-- @see DEFAULT_SESSION
-- @name save
function M.save(filename)
+ filename = filename or gui.dialog('filesave',
+ '--title', _L['Save Session'],
+ '--button1', _L['_Save'],
+ '--button2', _L['_Cancel'],
+ '--with-directory',
+ M.DEFAULT_SESSION:match('.+[/\\]') or '',
+ '--with-file',
+ M.DEFAULT_SESSION:match('[^/\\]+$') or '',
+ '--no-newline'):iconv(_CHARSET, 'UTF-8')
+ if filename == '' then return end
local session = {}
local buffer_line = "buffer: %d %d %d %s" -- anchor, cursor, line, filename
local split_line = "%ssplit%d: %s %d" -- level, number, type, size
@@ -165,47 +186,16 @@ function M.save(filename)
session[#session + 1] = ("recent: %s"):format(filename)
end
-- Write the session.
- local f = io.open(filename or M.DEFAULT_SESSION, 'wb')
+ local f = io.open(filename, 'wb')
if f then
f:write(table.concat(session, '\n'))
f:close()
end
end
-
----
--- Prompts the user for a Textadept session to load.
--- @name prompt_load
-function M.prompt_load()
- local utf8_filename = gui.dialog('fileselect',
- '--title', _L['Load Session'],
- '--button1', _L['_Open'],
- '--button2', _L['_Cancel'],
- '--with-directory',
- M.DEFAULT_SESSION:match('.+[/\\]') or '',
- '--with-file',
- M.DEFAULT_SESSION:match('[^/\\]+$') or '',
- '--no-newline')
- if #utf8_filename > 0 then M.load(utf8_filename:iconv(_CHARSET, 'UTF-8')) end
-end
-
----
--- Prompts the user to save the current Textadept session to a file.
--- @name prompt_save
-function M.prompt_save()
- local utf8_filename = gui.dialog('filesave',
- '--title', _L['Save Session'],
- '--button1', _L['_Save'],
- '--button2', _L['_Cancel'],
- '--with-directory',
- M.DEFAULT_SESSION:match('.+[/\\]') or '',
- '--with-file',
- M.DEFAULT_SESSION:match('[^/\\]+$') or '',
- '--no-newline')
- if #utf8_filename > 0 then M.save(utf8_filename:iconv(_CHARSET, 'UTF-8')) end
-end
-
-events.connect(events.QUIT,
- function() if M.SAVE_ON_QUIT then M.save() end end, 1)
+-- Save session on quit.
+events.connect(events.QUIT, function()
+ if M.SAVE_ON_QUIT then M.save(M.DEFAULT_SESSION) end
+end, 1)
local function no_session() M.SAVE_ON_QUIT = false end
args.register('-n', '--nosession', 0, no_session, 'No session functionality')