diff options
Diffstat (limited to 'modules/textadept')
-rw-r--r-- | modules/textadept/bookmarks.lua | 84 |
1 files changed, 36 insertions, 48 deletions
diff --git a/modules/textadept/bookmarks.lua b/modules/textadept/bookmarks.lua index 29a68c1c..adf283c7 100644 --- a/modules/textadept/bookmarks.lua +++ b/modules/textadept/bookmarks.lua @@ -12,23 +12,12 @@ module('textadept.bookmarks')]] M.MARK_BOOKMARK = _SCINTILLA.next_marker_number() --- --- Toggles the bookmark on line number *line* or the current line, unless *on* --- is given. --- If *on* is `true` or `false`, adds or removes the bookmark, respectively. --- @param on Optional flag indicating whether to add or remove a bookmark on --- line *line* or the current line. The default value is `nil`, toggling a --- bookmark. --- @param line Optional line number to add or remove a bookmark on. +-- Toggles a bookmark on the current line. -- @name toggle -function M.toggle(on, line) - assert_type(line, 'number/nil', 2) - if not line then line = buffer:line_from_position(buffer.current_pos) end - local f = on and buffer.marker_add or buffer.marker_delete - if on == nil then -- toggle - if buffer:marker_get(line) & 1 << M.MARK_BOOKMARK == 0 then - f = buffer.marker_add - end - end +function M.toggle() + local line = buffer:line_from_position(buffer.current_pos) + local has_mark = buffer:marker_get(line) & 1 << M.MARK_BOOKMARK > 0 + local f = has_mark and buffer.marker_delete or buffer.marker_add f(buffer, line, M.MARK_BOOKMARK) end @@ -47,45 +36,44 @@ function M.clear() buffer:marker_delete_all(M.MARK_BOOKMARK) end -- prompting the user for a bookmarked line to go to. -- @name goto_mark function M.goto_mark(next) - if next == nil then - local utf8_list, buffers = {}, {} - -- List the current buffer's marks, and then all other buffers' marks. - for _, current_buffer_first in ipairs{true, false} do - for _, buffer in ipairs(_BUFFERS) do - if current_buffer_first and buffer == _G.buffer or - not current_buffer_first and buffer ~= _G.buffer then - local basename = (buffer.filename or ''):match('[^/\\]+$') or - buffer._type or _L['Untitled'] - if buffer.filename then - basename = basename:iconv('UTF-8', _CHARSET) - end - local line = buffer:marker_next(0, 1 << M.MARK_BOOKMARK) - while line >= 0 do - local mark = string.format('%s:%d: %s', basename, line + 1, - buffer:get_line(line):match('^[^\r\n]*')) - utf8_list[#utf8_list + 1], buffers[#utf8_list + 1] = mark, buffer - line = buffer:marker_next(line + 1, 1 << M.MARK_BOOKMARK) - end - end - end - end - if #utf8_list == 0 then return end - local button, mark = ui.dialogs.filteredlist{ - title = _L['Select Bookmark'], columns = _L['Bookmark'], items = utf8_list - } - if button ~= 1 or not mark then return end - view:goto_buffer(buffers[mark]) - textadept.editing.goto_line(utf8_list[mark]:match('^[^:]+:(%d+):') - 1) - else + if next ~= nil then local f = next and buffer.marker_next or buffer.marker_previous local current_line = buffer:line_from_position(buffer.current_pos) - local line = f(buffer, current_line + (next and 1 or -1), - 1 << M.MARK_BOOKMARK) + local line = f( + buffer, current_line + (next and 1 or -1), 1 << M.MARK_BOOKMARK) if line == -1 then line = f(buffer, (next and 0 or buffer.line_count), 1 << M.MARK_BOOKMARK) end if line >= 0 then textadept.editing.goto_line(line) end + return + end + local scan_this_buffer, utf8_list, buffers = true, {}, {} + -- List the current buffer's marks, and then all other buffers' marks. + ::rescan:: + for _, buffer in ipairs(_BUFFERS) do + if not (scan_this_buffer == (buffer == _G.buffer)) then goto continue end + local filename = buffer.filename or buffer._type or _L['Untitled'] + if buffer.filename then filename = filename:iconv('UTF-8', _CHARSET) end + local basename = buffer.filename and filename:match('[^/\\]+$') or filename + local line = buffer:marker_next(0, 1 << M.MARK_BOOKMARK) + while line >= 0 do + utf8_list[#utf8_list + 1] = string.format( + '%s:%d: %s', basename, line + 1, + buffer:get_line(line):match('^[^\r\n]*')) + buffers[#buffers + 1] = buffer + line = buffer:marker_next(line + 1, 1 << M.MARK_BOOKMARK) + end + ::continue:: end + scan_this_buffer = not scan_this_buffer + if not scan_this_buffer then goto rescan end + if #utf8_list == 0 then return end + local button, i = ui.dialogs.filteredlist{ + title = _L['Select Bookmark'], columns = _L['Bookmark'], items = utf8_list + } + if button ~= 1 or not i then return end + view:goto_buffer(buffers[i]) + textadept.editing.goto_line(utf8_list[i]:match('^[^:]+:(%d+):') - 1) end return M |