Przeglądaj źródła

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.
master
Daniel 2 miesięcy temu
rodzic
commit
732323edfc
Nie znaleziono w bazie danych klucza dla tego podpisu
1 zmienionych plików z 36 dodań i 24 usunięć
  1. 36
    24
      site-lisp/db-org.el

+ 36
- 24
site-lisp/db-org.el Wyświetl plik

@@ -662,31 +662,43 @@ not."
662 662
 
663 663
 (defun db/org--get-location (&optional arg)
664 664
   "Interactively query for location and return mark.
665
-Searches through the current file, and through all files in the
666
-variables `org-agenda-files',
667
-`org-agenda-text-search-extra-files', and the current buffer, if
668
-ARG is non-nil.  Search is always conducted up to level 9.  If
669
-the selected location does not have an associated mark, error
670
-out.  Disable refile cache and any active refile filter hooks to
671
-allow linking to any item."
665
+
666
+Searches through the current buffer if that one is associated
667
+with a file, or `db/org-default-org-file'.  When ARG is non-nil,
668
+search through all files in the variables `org-agenda-files',
669
+`org-agenda-text-search-extra-files', and the current file or
670
+`db/org-default-org-file'.
671
+
672
+Search is always conducted up to level 9.  If the selected
673
+location does not have an associated point or mark, error out.
674
+Disable refile cache and any active refile filter hooks to allow
675
+linking to any item."
672 676
   (let ((org-refile-target-verify-function nil)
673
-        (org-refile-use-cache nil))
674
-   (let* ((org-refile-targets (if arg
675
-                                  `((org-agenda-files :maxlevel . 9)
676
-                                    (,(cl-remove-if-not
677
-                                       #'stringp org-agenda-text-search-extra-files)
678
-                                     :maxlevel . 9)
679
-                                    (nil :maxlevel . 9))
680
-                                '((nil :maxlevel . 9))))
681
-          (mrk (nth 3 (org-refile-get-location
682
-                       nil
683
-                       ;; if the current buffer is associated with a file, search
684
-                       ;; through it; otherwise, use the default Org Mode file as
685
-                       ;; default buffer
686
-                       (if (buffer-file-name)
687
-                           nil
688
-                         (get-file-buffer db/org-default-org-file))))))
689
-     (if mrk mrk (user-error "Invalid location")))))
677
+        (org-refile-use-cache nil)
678
+        ;; If the current buffer is associated with a file, search through it;
679
+        ;; otherwise, use the default Org Mode file as default buffer
680
+        (default-buffer (if (buffer-file-name)
681
+                           (current-buffer)
682
+                         (find-file-noselect db/org-default-org-file))))
683
+    (when (null default-buffer)
684
+      (user-error "Current buffer is not associated with a file and `db/org-default-org-file' does not exist; nothing to search through"))
685
+    (let* ((org-refile-targets (if arg
686
+                                   `((org-agenda-files :maxlevel . 9)
687
+                                     (,(cl-remove-if-not
688
+                                        #'stringp org-agenda-text-search-extra-files)
689
+                                      :maxlevel . 9)
690
+                                     (nil :maxlevel . 9))
691
+                                 '((nil :maxlevel . 9))))
692
+           (pom (nth 3 (org-refile-get-location nil default-buffer))))
693
+      (cond
694
+       ((markerp pom) pom)
695
+       ((integerp pom)
696
+        ;; Convert point to marker to ensure we are always in the correct buffer
697
+        (save-mark-and-excursion
698
+          (with-current-buffer default-buffer
699
+            (goto-char pom)
700
+            (point-marker))))
701
+       (t (user-error "Invalid location"))))))
690 702
 
691 703
 (defun db/org-find-links-to-current-item (arg)
692 704
   "Find links to current item.

Ładowanie…
Anuluj
Zapisz