Skip to content

Add function and key binding for find "parent" notes #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Default (OSX).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{ "keys": ["shift+enter"], "command": "zk_new_zettel", "context": [ { "key": "sublime_zk", "operand": "true"}] },
{ "keys": ["ctrl+enter"], "command": "zk_follow_wiki_link", "context": [ { "key": "sublime_zk", "operand": "true"}]},
{ "keys": ["alt+enter"], "command": "zk_show_referencing_notes", "context": [ { "key": "sublime_zk", "operand": "true"}]},
{"keys": ["super+alt+enter"], "command": "zk_show_parent_notes", "context": [ { "key": "sublime_zk", "operand": "true"}] },
{ "keys": ["[", "["], "command": "zk_get_wiki_link"},
{ "keys": ["#", "?"], "command": "zk_tag_selector"},
{ "keys": ["#", "!"], "command": "zk_show_all_tags"},
Expand Down
78 changes: 78 additions & 0 deletions sublime_zk.py
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,84 @@ def run(self, edit):
return


class ZkShowParentNotesCommand(sublime_plugin.TextCommand):
"""
Command searching for notes referencing the current note
** directly adapted from ZkShowReferencingNotesCommand
* if ag is not installed, opens a find-in-files for link under cursor.
* if ag is installed, it will show results:
* in an overlay if external search results are disabled
* else in the external search file
"""

def on_done(self, selection):
"""
Called when a note was selected from the overlay:
Open the selected note, if any.
"""
if selection == -1:
return
the_file = os.path.join(self.folder, self.friend_note_files[selection])
new_view = self.view.window().open_file(the_file)

def run(self, edit):
"""
Try to select note link if present. Search for notes as described above.
"""
print("ZK: finding all parent notes")
global F_EXT_SEARCH
global PANE_FOR_OPENING_RESULTS
current_note_name = self.view.file_name()
note_id = get_note_id_of_file(current_note_name)

print('*** current note id: ', note_id)

# linestart_till_cursor_str, link_region = select_link_in(self.view)

# print(linestart_till_cursor_str, link_region)
if not note_id:
return

settings = get_settings()
if F_EXT_SEARCH:
extension = settings.get('wiki_extension')
folder = get_path_for(self.view)
if not folder:
return
self.folder = folder
# note_id = cut_after_note_id(self.view.substr(link_region))
self.friend_note_files = ExternalSearch.search_friend_notes(
folder, extension, note_id)
self.friend_note_files = [os.path.basename(f) for f in
self.friend_note_files]
if ExternalSearch.EXTERNALIZE:
nv = self.view.window().open_file(ExternalSearch.external_file(
folder))
self.view.window().set_view_index(nv,
PANE_FOR_OPENING_RESULTS, 0)

else:
self.view.window().show_quick_panel(self.friend_note_files,
self.on_done)
else:
new_tab = settings.get('show_search_results_in_new_tab')

# hack for the find in files panel: select tag in view, copy it
selection = self.view.sel()
selection.clear()
# note_id = cut_after_note_id(self.view.substr(link_region))
selection.add(sublime.Region(link_region.a,
link_region.a + len(note_id)))
self.view.window().run_command("copy")
self.view.window().run_command("show_panel",
{"panel": "find_in_files",
"where": get_path_for(self.view),
"use_buffer": new_tab, })
# now paste the note-id --> it will land in the "find" field
self.view.window().run_command("paste")
return


class ZkReplaceSelectedTextCommand(sublime_plugin.TextCommand):

def run(self, edit, args):
Expand Down