From 4b581b9e57b16e9578f2ee960e150bbd326513ba Mon Sep 17 00:00:00 2001 From: khimaros Date: Tue, 21 Nov 2023 16:07:55 -0800 Subject: [PATCH] pass fzf query or cmd match to editor environment --- docs/tool-editor.md | 34 ++++++++++++++++++++++++++ internal/adapter/editor/editor.go | 6 +++-- internal/adapter/editor/editor_test.go | 10 ++++---- internal/adapter/fzf/fzf.go | 5 +++- internal/adapter/fzf/note_filter.go | 4 +++ internal/cli/cmd/edit.go | 7 +++++- internal/cli/cmd/new.go | 2 +- internal/cli/container.go | 4 +-- 8 files changed, 60 insertions(+), 12 deletions(-) diff --git a/docs/tool-editor.md b/docs/tool-editor.md index 90b23c21..99fb55b1 100644 --- a/docs/tool-editor.md +++ b/docs/tool-editor.md @@ -12,3 +12,37 @@ You can customize which editor to use either from the [configuration file](confi ``` 3. `VISUAL` environment variable 4. `EDITOR` environment variable + +When invoking the editor, `zk` will provide the following environment to the editor: + +`ZK_QUERY`: the query interactively provided to `fzf` or otherwise the value passed with the `-m` flag + +## Advanced editor usage + +It is also possible to create a custom script for your editor, for example to jump to a specific instance of a search term. + +Consider the following script: + +```bash +#!/bin/bash + +set -eou pipefail + +grep -nEv '^[[:space:]]*$' "$1" \ + | fzf \ + --tiebreak=begin \ + --exact \ + --tabstop=4 \ + --height=100% \ + --layout=reverse \ + --no-hscroll \ + --color=hl:-1,hl+:-1 \ + --preview-window=wrap \ + --delimiter=':' \ + --with-nth=2.. \ + --query="${ZK_QUERY}" \ + | sed 's/:.*$//' \ + | xargs -o -I {} vim +{} "$1" +``` + +This script could then be configured as the `editor` in `.zk/config.yaml` to open a second `fzf` instance prepopulated with the query which was previously entered into `zk`. diff --git a/internal/adapter/editor/editor.go b/internal/adapter/editor/editor.go index 39ec0ae1..c7b36bfe 100644 --- a/internal/adapter/editor/editor.go +++ b/internal/adapter/editor/editor.go @@ -15,11 +15,12 @@ import ( // Editor represents an external editor able to edit the notes. type Editor struct { editor string + query string } // NewEditor creates a new Editor from the given editor user setting or the // matching environment variables. -func NewEditor(editor opt.String) (*Editor, error) { +func NewEditor(editor opt.String, query string) (*Editor, error) { editor = osutil.GetOptEnv("ZK_EDITOR"). Or(editor). Or(osutil.GetOptEnv("VISUAL")). @@ -29,7 +30,7 @@ func NewEditor(editor opt.String) (*Editor, error) { return nil, fmt.Errorf("no editor set in config") } - return &Editor{editor.Unwrap()}, nil + return &Editor{editor.Unwrap(), query}, nil } // Open launches the editor with the notes at given paths. @@ -38,6 +39,7 @@ func (e *Editor) Open(paths ...string) error { // initial note content to `zk new`. Without this, Vim doesn't work // properly in this case. // See https://github.com/mickael-menu/zk/issues/4 + os.Setenv("ZK_QUERY", e.query) cmd := executil.CommandFromString(e.editor + " " + shellquote.Join(paths...) + "