diff options
author | 2009-02-15 23:58:21 -0500 | |
---|---|---|
committer | 2009-02-15 23:58:21 -0500 | |
commit | 5955540da3e0a8da20c6e627a5322b719103362e (patch) | |
tree | 909827a8d72d4b703b5e369f6f17ed22dac9b60c | |
parent | 825f6a76c4c938f0978a9454b20e9ac502a246fc (diff) | |
download | textadept-5955540da3e0a8da20c6e627a5322b719103362e.tar.gz textadept-5955540da3e0a8da20c6e627a5322b719103362e.zip |
Cleaned up some Lua code.
-rw-r--r-- | core/events.lua | 70 | ||||
-rw-r--r-- | core/ext/find.lua | 5 | ||||
-rw-r--r-- | core/file_io.lua | 156 | ||||
-rw-r--r-- | modules/textadept/editing.lua | 14 |
4 files changed, 115 insertions, 130 deletions
diff --git a/core/events.lua b/core/events.lua index f0d629cc..b28b36ee 100644 --- a/core/events.lua +++ b/core/events.lua @@ -105,47 +105,6 @@ function handle(event, ...) end end ---- --- Reloads event handlers. --- Clears each event's table of handlers and reloads this module to reset to the --- default handlers. -function reload() - package.loaded['events'] = nil - for handler in pairs(events) do - if events[handler..'s'] then events[handler..'s'] = nil end - end - require 'events' -end - --- Textadept events. -function buffer_new() - return handle('buffer_new') -end -function buffer_deleted() - return handle('buffer_deleted') -end -function buffer_switch() - return handle('buffer_switch') -end -function view_new() - return handle('view_new') -end -function view_switch() - return handle('view_switch') -end -function quit() - return handle('quit') -end -function keypress(code, shift, control, alt) - return handle('keypress', code, shift, control, alt) -end -function menu_clicked(menu_id_str) - return handle('menu_clicked', tonumber(menu_id_str)) -end -function pm_view_filled() - return handle('pm_view_filled') -end - -- Scintilla notifications. function char_added(n) if n.ch < 256 then return handle('char_added', string.char(n.ch)) end @@ -205,6 +164,35 @@ function notification(n) if f then f(n) end end +-- Textadept events. +function buffer_new() + return handle('buffer_new') +end +function buffer_deleted() + return handle('buffer_deleted') +end +function buffer_switch() + return handle('buffer_switch') +end +function view_new() + return handle('view_new') +end +function view_switch() + return handle('view_switch') +end +function quit() + return handle('quit') +end +function keypress(code, shift, control, alt) + return handle('keypress', code, shift, control, alt) +end +function menu_clicked(menu_id_str) + return handle('menu_clicked', tonumber(menu_id_str)) +end +function pm_view_filled() + return handle('pm_view_filled') +end + -- Default handlers to follow. add_handler('view_new', diff --git a/core/ext/find.lua b/core/ext/find.lua index d8fea666..bf72b833 100644 --- a/core/ext/find.lua +++ b/core/ext/find.lua @@ -4,6 +4,8 @@ local textadept = _G.textadept local locale = _G.locale local find = textadept.find +local lfs = require 'lfs' + local MARK_FIND = 0 local MARK_FIND_COLOR = 0x4D9999 local previous_view @@ -91,7 +93,6 @@ function find.find(text, next, flags, nowrap, wrapped) if not find.lua then text = text:gsub('([().*+?^$%%[%]-])', '%%%1') end if not find.match_case then text = text:lower() end if find.whole_word then text = '[^%W_]'..text..'[^%W_]' end - local lfs = require 'lfs' local match_case = find.match_case local whole_word = find.whole_word local format = string.format @@ -214,8 +215,8 @@ end -- @see find.find function find.replace_all(ftext, rtext, flags) if #ftext == 0 then return end - local buffer = buffer if find.in_files then find.in_files = false end + local buffer = buffer buffer:begin_undo_action() local count = 0 if #buffer:get_sel_text() == 0 then diff --git a/core/file_io.lua b/core/file_io.lua index 59017b64..66b7be81 100644 --- a/core/file_io.lua +++ b/core/file_io.lua @@ -77,15 +77,14 @@ function reload(buffer) textadept.check_focused_buffer(buffer) if not buffer.filename then return end local f, err = io.open(buffer.filename, 'rb') - if f then - local pos = buffer.current_pos - local first_visible_line = buffer.first_visible_line - buffer:set_text(f:read('*all')) - buffer:line_scroll(0, first_visible_line) - buffer:goto_pos(pos) - buffer:set_save_point() - f:close() - end + if not f then return end + local pos = buffer.current_pos + local first_visible_line = buffer.first_visible_line + buffer:set_text(f:read('*all')) + buffer:line_scroll(0, first_visible_line) + buffer:goto_pos(pos) + buffer:set_save_point() + f:close() end --- @@ -198,55 +197,53 @@ function load_session(filename, only_pm) local ta_session = user_dir..'/.ta_session' local f = io.open(filename or ta_session, 'rb') local current_view, splits = 1, { [0] = {} } - if f then - for line in f:lines() do - if not only_pm then - if line:find('^buffer:') then - local anchor, current_pos, first_visible_line, filename = - line:match('^buffer: (%d+) (%d+) (%d+) (.+)$') - textadept.io.open(filename or '', 'rb') - -- 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 - local buffer = buffer - 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:set_sel(anchor, current_pos) - elseif line:find('^%s*split%d:') then - local level, num, type, size = - line:match('^(%s*)split(%d): (%S+) (%d+)') - local view = splits[#level] and splits[#level][tonumber(num)] or view - splits[#level + 1] = { view:split(type == 'true') } - splits[#level + 1][1].size = tonumber(size) -- could be 1 or 2 - elseif line:find('^%s*view%d:') then - local level, num, buf_idx = line:match('^(%s*)view(%d): (%d+)$') - local view = splits[#level][tonumber(num)] or view - buf_idx = tonumber(buf_idx) - if buf_idx > #textadept.buffers then buf_idx = #textadept.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 - end - end - if line:find('^size:') then - local width, height = line:match('^size: (%d+) (%d+)$') - if width and height then textadept.size = { width, height } end - elseif line:find('^pm:') then - local width, text = line:match('^pm: (%d+) (.+)$') - textadept.pm.width = width or 0 - textadept.pm.entry_text = text or '' - textadept.pm.activate() + if not f then return false end + for line in f:lines() do + if not only_pm then + if line:find('^buffer:') then + local anchor, current_pos, first_visible_line, filename = + line:match('^buffer: (%d+) (%d+) (%d+) (.+)$') + textadept.io.open(filename or '', 'rb') + -- 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 + local buffer = buffer + 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:set_sel(anchor, current_pos) + elseif line:find('^%s*split%d:') then + local level, num, type, size = + line:match('^(%s*)split(%d): (%S+) (%d+)') + local view = splits[#level] and splits[#level][tonumber(num)] or view + splits[#level + 1] = { view:split(type == 'true') } + splits[#level + 1][1].size = tonumber(size) -- could be 1 or 2 + elseif line:find('^%s*view%d:') then + local level, num, buf_idx = line:match('^(%s*)view(%d): (%d+)$') + local view = splits[#level][tonumber(num)] or view + buf_idx = tonumber(buf_idx) + if buf_idx > #textadept.buffers then buf_idx = #textadept.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 end end - f:close() - textadept.views[current_view]:focus() - return true + if line:find('^size:') then + local width, height = line:match('^size: (%d+) (%d+)$') + if width and height then textadept.size = { width, height } end + elseif line:find('^pm:') then + local width, text = line:match('^pm: (%d+) (.+)$') + textadept.pm.width = width or 0 + textadept.pm.entry_text = text or '' + textadept.pm.activate() + end end - return false + f:close() + textadept.views[current_view]:focus() + return true end --- @@ -270,8 +267,7 @@ function save_session(filename) local first_visible_line = current and 'first_visible_line' or '_first_visible_line' session[#session + 1] = - buffer_line:format(buffer[anchor] or 0, - buffer[current_pos] or 0, + buffer_line:format(buffer[anchor] or 0, buffer[current_pos] or 0, buffer[first_visible_line] or 0, buffer.filename) end end @@ -337,17 +333,16 @@ end function read_api_file(filename, word_chars) local api = {} local f = io.open(filename, 'rb') - if f then - for line in f:lines() do - local func, params, desc = - line:match('(['..word_chars..']+)%s*(%b())(.*)$') - if func and params and desc then - if not api[func] then api[func] = {} end - api[func][#api[func] + 1] = { params, desc } - end + if not f then return api end + for line in f:lines() do + local func, params, desc = + line:match('(['..word_chars..']+)%s*(%b())(.*)$') + if func and params and desc then + if not api[func] then api[func] = {} end + api[func][#api[func] + 1] = { params, desc } end - f:close() end + f:close() return api end @@ -355,22 +350,21 @@ end -- [Local function] Prompts the user to reload the current file if it has been -- modified outside of Textadept. local function update_modified_file() + if not buffer.filename then return end local filename = buffer.filename - if filename then - local attributes = lfs.attributes(filename) - if not attributes then return end - if buffer.modification_time < attributes.modification then - if cocoa_dialog('yesno-msgbox', { - title = locale.IO_RELOAD_TITLE, - text = locale.IO_RELOAD_TEXT, - ['informative-text'] = string.format(locale.IO_RELOAD_MSG, filename), - ['no-cancel'] = true, - ['no-newline'] = true - }) == '1' then - buffer:reload() - else - buffer.modification_time = attributes.modification - end + local attributes = lfs.attributes(filename) + if not attributes then return end + if buffer.modification_time < attributes.modification then + if cocoa_dialog('yesno-msgbox', { + title = locale.IO_RELOAD_TITLE, + text = locale.IO_RELOAD_TEXT, + ['informative-text'] = string.format(locale.IO_RELOAD_MSG, filename), + ['no-cancel'] = true, + ['no-newline'] = true + }) == '1' then + buffer:reload() + else + buffer.modification_time = attributes.modification end end end diff --git a/modules/textadept/editing.lua b/modules/textadept/editing.lua index 545f2e52..aa939282 100644 --- a/modules/textadept/editing.lua +++ b/modules/textadept/editing.lua @@ -347,13 +347,16 @@ function smart_paste(action, reindent) -- If text was copied to the clipboard from other apps, insert it into the -- kill-ring so it can be pasted (thanks to Nathan Robinson). - local clip_txt, found = textadept.clipboard_text, false - if clip_txt ~= '' then + if #textadept.clipboard_text > 0 then + local clipboard_text, found = textadept.clipboard_text, false for _, ring_txt in ipairs(kill_ring) do - if clip_txt == ring_txt then found = true break end + if clipboard_text == ring_txt then + found = true + break + end end + if not found then insert_into_kill_ring(clipboard_text) end end - if not found then insert_into_kill_ring(clip_txt) end txt = kill_ring[kill_ring.pos] if txt then @@ -595,8 +598,7 @@ end -- @see smart_cutcopy insert_into_kill_ring = function(txt) table.insert(kill_ring, 1, txt) - local maxn = kill_ring.maxn - if #kill_ring > maxn then kill_ring[maxn + 1] = nil end + if #kill_ring > kill_ring.maxn then kill_ring[kill_ring.maxn + 1] = nil end end --- |