From 95920f8291384006ad375a2976571b4a3eaa6a7e Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Fri, 20 Jan 2023 18:39:00 +0100 Subject: [PATCH] Extend default scope for `db/org-get-location` when in agenda files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When inside a file that is part of `org-agenda-file`, the default scope to search for locations via `db/org-get-location` (used for example when interactively inserting links) is extended to include all agenda files, and not only the current buffer. The idea behind this is to consider all agenda files as one large data collection, and not only individual files. This inhibits the usual error, when trying to insert links at items located in refile.org, that links are only displayed for items in refile.org itself – which are usually not many – when instead links to items in the default org files where actually requested. This should not be an issue for performance, as searching through all agenda files for valid locations is fast even on Windows. --- site-lisp/db-org.el | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/site-lisp/db-org.el b/site-lisp/db-org.el index 6afb5bd..ac6d074 100644 --- a/site-lisp/db-org.el +++ b/site-lisp/db-org.el @@ -1188,11 +1188,17 @@ not." (defun db/org-get-location (&optional arg) "Interactively query for location and return mark. -Searches through the current buffer if that one is an Org buffer -and is associated with a file, or `db/org-default-org-file'. +When ARG 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. If the +current buffer is associated with a file from `org-agenda-files', +though, the search is extended through all agenda files (the +rationale being that Org agenda files are always considered to be +one large data collection). + When ARG is non-nil, search through all files in the variables `org-agenda-files', `org-agenda-text-search-extra-files', and the -current file or `db/org-default-org-file'. +current file or `db/org-default-org-file' as described above. Search is always conducted up to level 9. If the selected location does not have an associated point or mark, error out. @@ -1206,14 +1212,34 @@ linking to any item." (default-buffer (if (and (buffer-file-name) (derived-mode-p 'org-mode)) (current-buffer) (find-file-noselect db/org-default-org-file)))) + (when (null default-buffer) (user-error "Current buffer is not associated with a file and `db/org-default-org-file' does not exist; nothing to search through")) - (let* ((org-refile-targets (append (and arg - `((org-agenda-files :maxlevel . 9) - (,(cl-remove-if-not #'stringp + + (let* ((current-buffer-is-in-org-agenda-files? (--when-let (buffer-file-name) + (-any (-partial #'file-equal-p it) + org-agenda-files))) + + ;; Default file(s) to search through; note that `default-buffer' is + ;; provided later to `org-refile-get-location' as additional argument + (org-refile-targets (append (if current-buffer-is-in-org-agenda-files? + '((org-agenda-files :maxlevel . 9)) + '((nil :maxlevel . 9))) + + ;; When ARG is non-nil, add all agenda + ;; files, but only if not already done + ;; so. + (and arg + (not current-buffer-is-in-org-agenda-files?) + '((org-agenda-files :maxlevel . 9))) + + ;; When ARG is non-nil, add extra file + ;; files to search though. + (and arg + `((,(cl-remove-if-not #'stringp org-agenda-text-search-extra-files) - :maxlevel . 9))) - '((nil :maxlevel . 9)))) + :maxlevel . 9))))) + (target-pointer (org-refile-get-location nil default-buffer)) (pom (nth 3 target-pointer))) (cond