Ensure to always return a marker when inserting links to other items

`org-refile-get-location` sometimes only returns a point and not a marker.  In
that case, manually convert the point to a marker to ensure that calling
functions now where to go to.  Additionally, ensure that
`db/org-default-org-file` is opened if not already done so, and error out if the
current buffer is not associated with a file and no default Org file exists.
This commit is contained in:
Daniel - 2021-03-27 10:19:01 +01:00
parent 00ca4c0276
commit 732323edfc
No known key found for this signature in database
GPG Key ID: 1C7071A75BB72D64
1 changed files with 36 additions and 24 deletions

View File

@ -662,15 +662,26 @@ not."
(defun db/org--get-location (&optional arg) (defun db/org--get-location (&optional arg)
"Interactively query for location and return mark. "Interactively query for location and return mark.
Searches through the current file, and through all files in the
variables `org-agenda-files', Searches through the current buffer if that one is associated
`org-agenda-text-search-extra-files', and the current buffer, if with a file, or `db/org-default-org-file'. When ARG is non-nil,
ARG is non-nil. Search is always conducted up to level 9. If search through all files in the variables `org-agenda-files',
the selected location does not have an associated mark, error `org-agenda-text-search-extra-files', and the current file or
out. Disable refile cache and any active refile filter hooks to `db/org-default-org-file'.
allow linking to any item."
Search is always conducted up to level 9. If the selected
location does not have an associated point or mark, error out.
Disable refile cache and any active refile filter hooks to allow
linking to any item."
(let ((org-refile-target-verify-function nil) (let ((org-refile-target-verify-function nil)
(org-refile-use-cache nil)) (org-refile-use-cache nil)
;; If the current buffer is associated with a file, search through it;
;; otherwise, use the default Org Mode file as default buffer
(default-buffer (if (buffer-file-name)
(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 (if arg (let* ((org-refile-targets (if arg
`((org-agenda-files :maxlevel . 9) `((org-agenda-files :maxlevel . 9)
(,(cl-remove-if-not (,(cl-remove-if-not
@ -678,15 +689,16 @@ allow linking to any item."
:maxlevel . 9) :maxlevel . 9)
(nil :maxlevel . 9)) (nil :maxlevel . 9))
'((nil :maxlevel . 9)))) '((nil :maxlevel . 9))))
(mrk (nth 3 (org-refile-get-location (pom (nth 3 (org-refile-get-location nil default-buffer))))
nil (cond
;; if the current buffer is associated with a file, search ((markerp pom) pom)
;; through it; otherwise, use the default Org Mode file as ((integerp pom)
;; default buffer ;; Convert point to marker to ensure we are always in the correct buffer
(if (buffer-file-name) (save-mark-and-excursion
nil (with-current-buffer default-buffer
(get-file-buffer db/org-default-org-file)))))) (goto-char pom)
(if mrk mrk (user-error "Invalid location"))))) (point-marker))))
(t (user-error "Invalid location"))))))
(defun db/org-find-links-to-current-item (arg) (defun db/org-find-links-to-current-item (arg)
"Find links to current item. "Find links to current item.