Sublime Forum

View bookmarks only in current file

#1

I have been using this plug-in View Bookmarks. When you go to see bookmarks it displays bookmarks from all open files(from all tabs). I would like it to display only bookmarks of file that I’m currently looking at(current tab):

This is the plug-in’s code:

import sublime, sublime_plugin
import os, re

class ViewBookmarksCommand(sublime_plugin.WindowCommand):
locations=[]

def run(self):
    items=[]
    self.locations=[]
    all_files = list(filter(None, map(lambda f : f.file_name(), sublime.active_window().views())))
    common_prefix = os.path.commonprefix(all_files)
    for view in sublime.active_window().views():
        prefix=""
        if view.name():
            prefix=view.name()+":"
        elif view.file_name():
            prefix=view.file_name()+":"
            if prefix.startswith(common_prefix):
                prefix = prefix[len(common_prefix):]
        for region in view.get_regions("bookmarks"):
            row,_=view.rowcol(region.a)
            line=re.sub('\s+', ' ', view.substr(view.line(region))).strip()
            items.append([prefix+str(row+1), line])
            self.locations.append((view, region))
    if len(items) > 0:
        sublime.active_window().show_quick_panel(items, self.go_there, sublime.MONOSPACE_FONT)
    else:
        sublime.active_window().show_quick_panel(["No bookmarks found"], None, sublime.MONOSPACE_FONT)

def go_there(self, i):
    if i < 0 or i >= len(self.locations):
        return
    view, region = self.locations[i]
    sublime.active_window().focus_view(view)
    view.show_at_center(region)
    view.sel().clear()
    view.sel().add(region)

I tried to edit it, but its beyond my knowledge xD.

Also there is no need for file name in the pop-up since it’s the current file.

0 Likes

#2

Quick and dirty edit:

import sublime, sublime_plugin
import os, re

class ViewBookmarksCommand(sublime_plugin.WindowCommand):
    locations=[]

    def run(self):
        items=[]
        view = self.window.active_view()

        for region in view.get_regions("bookmarks"):
            row,_=view.rowcol(region.begin())
            line=re.sub('\s+', ' ', view.substr(view.line(region))).strip()
            items.append([str(row+1), line])
            self.locations.append((view, region))
        if len(items) > 0:
            sublime.active_window().show_quick_panel(items, self.go_there, sublime.MONOSPACE_FONT)
        else:
            sublime.active_window().show_quick_panel(["No bookmarks found"], None, sublime.MONOSPACE_FONT)

    def go_there(self, i):
        if i < 0 or i >= len(self.locations):
            return
        view, region = self.locations[i]
        sublime.active_window().focus_view(view)
        view.show_at_center(region)
        view.sel().clear()
        view.sel().add(region)
1 Like

#3

I just test it. “Go To” functionally does not work. It goes not go anywhere.

0 Likes