diff options
-rw-r--r-- | core/.gui.luadoc | 12 | ||||
-rw-r--r-- | core/gui.lua | 15 | ||||
-rw-r--r-- | modules/textadept/find.lua | 32 | ||||
-rw-r--r-- | modules/textadept/run.lua | 36 |
4 files changed, 51 insertions, 44 deletions
diff --git a/core/.gui.luadoc b/core/.gui.luadoc index 2ff13019..deb39d58 100644 --- a/core/.gui.luadoc +++ b/core/.gui.luadoc @@ -26,6 +26,18 @@ module('gui') function goto_view(n, relative) end --- +-- Goes to the buffer with the given filename. +-- If the desired buffer is open in a view, goes to that view. Otherwise, opens +-- the buffer in either the `preferred_view` if given, the first view that is +-- not the current one, a split view if `split` is `true`, or the current view. +-- @param filename The filename of the buffer to go to. +-- @param split If there is only one view, split it and open the buffer in the +-- other view. +-- @param preferred_view When multiple views exist and the desired buffer is not +-- open in any of them, open it in this one. +function goto_file(filename, split, preferred_view) end + +--- -- Gets the current split view structure. -- @return table of split views. Each split view entry is a table with 4 -- fields: `1`, `2`, `vertical`, and `size`. `1` and `2` have values of either diff --git a/core/gui.lua b/core/gui.lua index 6c6e60ef..0beb1a11 100644 --- a/core/gui.lua +++ b/core/gui.lua @@ -63,6 +63,21 @@ function gui.switch_buffer() if i then view:goto_buffer(i + 1) end end +-- LuaDoc is in core/.gui.luadoc. +function gui.goto_file(filename, split, preferred_view) + if #_VIEWS == 1 and view.buffer.filename ~= filename and split then + view:split() + else + local other_view = _VIEWS[preferred_view] + for i, v in ipairs(_VIEWS) do + if v.buffer.filename == filename then gui.goto_view(i) return end + if not other_view and v ~= view then other_view = i end + end + if other_view then gui.goto_view(other_view) end + end + io.open_file(filename) +end + local connect = events.connect -- Sets default properties for a Scintilla window. diff --git a/modules/textadept/find.lua b/modules/textadept/find.lua index 8187954e..989c84c8 100644 --- a/modules/textadept/find.lua +++ b/modules/textadept/find.lua @@ -18,7 +18,7 @@ find.in_files_label_text = L('In files') local MARK_FIND = _SCINTILLA.next_marker_number() local MARK_FIND_COLOR = 0x4D9999 -local prev_view +local preferred_view -- Text escape sequences with their associated characters. -- @class table @@ -62,7 +62,7 @@ function find.find_in_files(utf8_dir) function search_dir(directory) for file in lfs_dir(directory) do if not file:find('^%.%.?$') then -- ignore . and .. - local path = directory..'/'..file + local path = directory..(not WIN32 and '/' or '\\')..file local type = lfs_attributes(path, 'mode') if type == 'directory' then search_dir(path) @@ -76,9 +76,7 @@ function find.find_in_files(utf8_dir) search_dir(dir) if #matches == 1 then matches[2] = L('No results found') end matches[#matches + 1] = '' - if buffer._type ~= L('[Files Found Buffer]') then - prev_view = _VIEWS[view] - end + if buffer._type ~= L('[Files Found Buffer]') then preferred_view = view end gui._print(L('[Files Found Buffer]'), table.concat(matches, '\n')) end end @@ -300,7 +298,6 @@ 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. @@ -314,27 +311,8 @@ local function goto_file(pos, line_num) buffer:marker_set_back(MARK_FIND, MARK_FIND_COLOR) buffer:marker_add(line_num, MARK_FIND) buffer:goto_pos(buffer.current_pos) - if #_VIEWS == 1 then - view:split() - prev_view = 2 -- second view - else - local clicked_view = view - if prev_view then gui.goto_view(prev_view) end - if buffer._type == L('[Files Found Buffer]') then - -- There are at least two find in files views; find one of those views - -- that the file was not selected from and focus it. - for i, v in ipairs(_VIEWS) do - if v ~= clicked_view then - prev_view = i - gui.goto_view(i) - break - end - end - end - end - io.open_file(file) - buffer:ensure_visible_enforce_policy(file_line_num - 1) - buffer:goto_line(file_line_num - 1) + gui.goto_file(file, true, preferred_view) + _m.textadept.editing.goto_line(file_line_num) end end end diff --git a/modules/textadept/run.lua b/modules/textadept/run.lua index 39a71db1..8e1561f3 100644 --- a/modules/textadept/run.lua +++ b/modules/textadept/run.lua @@ -135,24 +135,26 @@ error_detail = {} -- @param line_num The line double-clicked. -- @see error_detail function goto_error(pos, line_num) - local type = buffer._type - if type == L('[Message Buffer]') or type == L('[Error Buffer]') then - line = buffer:get_line(line_num) - for _, error_detail in pairs(error_detail) do - local captures = { line:match(error_detail.pattern) } - if #captures > 0 then - local utf8_filename = captures[error_detail.filename] - local filename = utf8_filename:iconv(_CHARSET, 'UTF-8') - if lfs.attributes(filename) then - io.open_file(utf8_filename) - _m.textadept.editing.goto_line(captures[error_detail.line]) - local msg = captures[error_detail.message] - if msg then buffer:call_tip_show(buffer.current_pos, msg) end - else - error(string.format('"%s" %s', utf8_filename, L('does not exist'))) - end - break + if buffer._type ~= L('[Message Buffer]') and + buffer._type ~= L('[Error Buffer]') then + return + end + local buffer = buffer + line = buffer:get_line(line_num) + for _, error_detail in pairs(error_detail) do + local captures = { line:match(error_detail.pattern) } + if #captures > 0 then + local utf8_filename = captures[error_detail.filename] + local filename = utf8_filename:iconv(_CHARSET, 'UTF-8') + if lfs.attributes(filename) then + gui.goto_file(utf8_filename, true) + _m.textadept.editing.goto_line(captures[error_detail.line]) + local msg = captures[error_detail.message] + if msg then buffer:call_tip_show(buffer.current_pos, msg) end + else + error(string.format('"%s" %s', utf8_filename, L('does not exist'))) end + return end end end |