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.
This commit is contained in:
Daniel - 2023-07-29 20:08:19 +02:00
parent c0660fe0f6
commit e892a849c1
Signed by: dbo
GPG Key ID: 784AA8DF0CCDF625
1 changed files with 41 additions and 7 deletions

View File

@ -1404,9 +1404,12 @@ not."
(t (user-error "Neither ID nor CUSTOM_ID given"))))) (t (user-error "Neither ID nor CUSTOM_ID given")))))
(org-search-view nil query))) (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. "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 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 through the current buffer if that one is an Org buffer and is
associated with a file, and `db/org-default-org-file' otherwise. 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) org-agenda-text-search-extra-files)
:maxlevel . 9))))) :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))) (pom (nth 3 target-pointer)))
(cond (cond
((markerp pom) pom) ((markerp pom) pom)
@ -1537,16 +1568,19 @@ avoids containing a link in the description of the newly inserted
link." link."
(insert (db/org--format-link-from-pom pom))) (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. "Interactively query for item and add link to it at point.
Search through all items of the current buffer, or Search through all items of the current buffer, or
`db/org-default-org-file' if the current buffer is not associated `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 with a file. If USE-ALL-ORG-FILES is non-nil, include all files
variables `org-agenda-files' and in the variables `org-agenda-files' and
`org-agenda-text-search-extra-files' in this search." `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") (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 () (defun db/org-add-link-to-current-clock ()
"Insert link to currently clocked-in item at point. "Insert link to currently clocked-in item at point.