From e892a849c1f35388b6719f71b26b7fa3f98f981e Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Sat, 29 Jul 2023 20:08:19 +0200 Subject: [PATCH] Allow to set an initial input when inserting a link to another item This will allow to bind `db/org-add-link-to-other-item` with different initial inputs to different keys to have often used selection directly at hand. Setting the initial input is a bit tricky, though, because `org-refile-get-location` does not allow to set it. Instead, we have to temporarily overwrite `completing-read` with the corresponding parameter set directly. --- site-lisp/db-org.el | 48 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/site-lisp/db-org.el b/site-lisp/db-org.el index fcf8a5d..01d5c9e 100644 --- a/site-lisp/db-org.el +++ b/site-lisp/db-org.el @@ -1404,9 +1404,12 @@ not." (t (user-error "Neither ID nor CUSTOM_ID given"))))) (org-search-view nil query))) -(defun db/org-get-location (&optional use-all-org-files) +(defun db/org-get-location (&optional use-all-org-files initial-input) "Interactively query for location and return mark. +Use INITIAL-INPUT as initial input when filtering available +locations. + When USE-ALL-ORG-FILES is nil, this functions by default searches through the current buffer if that one is an Org buffer and is associated with a file, and `db/org-default-org-file' otherwise. @@ -1460,7 +1463,35 @@ linking to any item." org-agenda-text-search-extra-files) :maxlevel . 9))))) - (target-pointer (org-refile-get-location nil default-buffer)) + (target-pointer (let ((old-completing-read (symbol-function 'completing-read))) + ;; We temporarily overwrite `completing-read' to + ;; provide our initial input; this is necessary + ;; because `org-refile-get-location' sets the + ;; initial-input parameter of `completing-read' to + ;; nil without any possibility for a custom string. + (unwind-protect + (progn + (fset 'completing-read #'(lambda (_prompt + _table + &optional + _predicate + _require-match + _initial-input + _hist + _def + _inherit-input-method) + (ignore _initial-input) + (funcall old-completing-read + _prompt + _table + _predicate + _require-match + initial-input + _hist + _def + _inherit-input-method))) + (org-refile-get-location nil default-buffer)) + (fset 'completing-read old-completing-read)))) (pom (nth 3 target-pointer))) (cond ((markerp pom) pom) @@ -1537,16 +1568,19 @@ avoids containing a link in the description of the newly inserted link." (insert (db/org--format-link-from-pom pom))) -(defun db/org-add-link-to-other-item (arg) +(defun db/org-add-link-to-other-item (&optional use-all-org-files initial-input) "Interactively query for item and add link to it at point. Search through all items of the current buffer, or `db/org-default-org-file' if the current buffer is not associated -with a file. If ARG is non-nil, include all files in the -variables `org-agenda-files' and -`org-agenda-text-search-extra-files' in this search." +with a file. If USE-ALL-ORG-FILES is non-nil, include all files +in the variables `org-agenda-files' and +`org-agenda-text-search-extra-files' in this search. + +Use INITIAL-INPUT as initial string to narrow down all available +items during interactive selection." (interactive "P") - (db/org-insert-link-to-pom (db/org-get-location arg))) + (db/org-insert-link-to-pom (db/org-get-location use-all-org-files initial-input))) (defun db/org-add-link-to-current-clock () "Insert link to currently clocked-in item at point.