Home Download Buy Blog Forum Support

Add Bookmark (not toggle) based upon find results?

Add Bookmark (not toggle) based upon find results?

Postby robertcollier4 on Sun Feb 24, 2013 6:07 pm

I would like to be able to add bookmarks (not toggle) to all lines matching results of a find operation. Currently in SublimeText I have figured out I can do Ctrl+F, type in search term, do "find_all" (Alt+Enter), do "toggle_bookmark" (Ctrl+F2). However there are a few problems with this.

1. I am looking for a keyboard shortcut command that is "add_bookmark" and not "toggle_bookmark". The problem is that if I have some lines already bookmarked and they ALSO match the results of my find, then I would not like the bookmark to be removed from these lines. I would like bookmarks to only be added based on the results of the find, not toggled and not ever removed.

2. I would like to do it all in one keystroke. Thus I propose a new keyboard shortcut command "find_all_addbookmark" which will do "find_all" followed by a "add_bookmark" operation based upon a selected word or entry in the find bar. I think it would be helpful to simple users to even have this as a button on the find bar since it is so handy.

Thanks.
Last edited by robertcollier4 on Tue Feb 26, 2013 2:48 pm, edited 3 times in total.
robertcollier4
 
Posts: 154
Joined: Sun Feb 24, 2013 5:37 pm

Re: Add Bookmark (not toggle) based upon find results?

Postby robertcollier4 on Tue Feb 26, 2013 4:22 am

Okay figured out a temporary solution via doing multiple commands in a plugin. Though it has the shortcomings that "Find All" changes your cursor position to the first found item. And also it is necessary to clear all previous bookmarks since there is no "Add Bookmark" functionality in Sublime other than "Toggle Bookmark" (which would unbookmark lines that were previously bookmarked and also match the search text).

Preferably, a properly done implementation of find_all_bookmark would not change the cursor position - it would just add bookmarks to the lines which match the search text and leave the original cursor position alone. Then the user can use "Next Bookmark" and "Prev Bookmark" to go through the search results as bookmarks from the original cursor position.

Create a file named find_all_bookmark.py
Code: Select all
import sublime, sublime_plugin

class FindAllBookmarkCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      context = self.view.window()
      # context.run_command("find_all", {"close_panel": true}, {"context": "window"}) # NameError: global name 'true' is not defined
      context.run_command("find_all", {"context": "window"})
      context.run_command("hide_panel")
      context.run_command("clear_bookmarks")
      context.run_command("toggle_bookmark")
      context.run_command("single_selection")


Add the following to Key Bindings - User
Code: Select all
{ "keys": ["ctrl+enter"], "command": "find_all_bookmark", "context": [{"key": "panel", "operand": "find"}, {"key": "panel_has_focus"}]},
robertcollier4
 
Posts: 154
Joined: Sun Feb 24, 2013 5:37 pm

Re: find_all_bookmark - add bookmarks to lines that contain text

Postby bizoo on Tue Feb 26, 2013 10:20 am

You can add bookmarks manually with:
Code: Select all
view.add_regions("bookmarks", [view.sel()[0]], "bookmarks", "bookmark", sublime.HIDDEN | sublime.PERSISTENT)

And remove them (all) with:
Code: Select all
view.erase_regions("bookmarks")

Using the view.find_all() API, you can set bookmarks where you want without using the find_all command and moving the cursor.

If I add to do it myself, I'll probably not use bookmarks but a custom region name and create another command to replace the "Next Bookmark" and "Prev Bookmark" to navigate.
bizoo
 
Posts: 880
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: find_all_bookmark - add bookmarks to lines that contain text

Postby robertcollier4 on Tue Feb 26, 2013 2:29 pm

Thanks a million bizoo. Your suggestions were spot on. I elected to re-use and append to the bookmarks region key so I can cumulatively add to the bookmarks based on multiple searches. Also I can clear and toggle the bookmarks with my existing hotkeys. Though it is a fantastic idea that you can set custom bookmark keys and assign separate hotkeys for them as well.

Here is mark_lines_containing.py for anyone else who might enjoy:
Code: Select all
{ "keys": ["alt+m"], "command": "mark_lines_containing" },

Code: Select all
import sublime, sublime_plugin

class MarkLinesContainingCommand(sublime_plugin.WindowCommand):

   def run(self):
      self.window.show_input_panel("Mark Lines Containing:", "", self.on_done, None, None)
      pass

   def on_done(self, text):
      ExistingBookmarks = self.window.active_view().get_regions("bookmarks")

      RegionsResult = self.window.active_view().find_all(text, sublime.IGNORECASE)

      for ThisExistingBookmark in ExistingBookmarks:
         RegionsResult.append(ThisExistingBookmark)

      self.window.active_view().add_regions("bookmarks", RegionsResult, "bookmarks", "bookmark", sublime.HIDDEN | sublime.PERSISTENT)
robertcollier4
 
Posts: 154
Joined: Sun Feb 24, 2013 5:37 pm


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot] and 13 guests