Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with latest org-id version: advise org-id-find rather than overwriting id link #2432

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ricklupton
Copy link

Motivation for this change

There have been recent changes in org-mode to improve org-id-open to be able to apply search strings in id: links. But, org-roam overrides the :follow parameter for id: links to its own org-roam-id-open function and therefore misses this.

The only change that org-roam-id-open makes is to try calling org-roam-id-find before falling back on org-id-find. This commit uses advice to do this directly, thereby integrating better with org-id-open.

There was some discussion on the org-mode list about adding a custom variable to avoid using advice, but this accomplishes the result without requiring changes in org:

https://list.orgmode.org/87jzlxjiuf.fsf@localhost/

There have been recent changes in org-mode to improve `org-id-open' to
be able to apply search strings in id: links.  But, org-roam overrides
the `:follow` parameter for id: links to its own `org-roam-id-open'
function and therefore misses this.

The only change that `org-roam-id-open' makes is to try calling
`org-roam-id-find' before falling back on `org-id-find'.  This commit
uses advice to do this directly, thereby integrating better with
`org-id-open'.

There was some discussion on the org-mode list about adding a custom
variable to avoid using advice, but this accomplishes the result
without requiring changes in org:

https://list.orgmode.org/87jzlxjiuf.fsf@localhost/
@akashpal-21
Copy link

Interesting - I had this functionality by simply using an advice function over both org-id-open and org-roam-id-open.
Commenting to keep track if this gets pushed - my code will become obsolete.

(defun org-id-open--support-search (fn link &rest args)
  "Support ::SEARCH syntax for id: links."
  ;; Save match data to prevent modifications by string matching functions.
  (save-match-data
    ;; Split the link at the '::' delimiter.
    (let* ((parts (split-string link "::"))  ; Split the link into parts.
	   (id (car parts))                  ; Extract the ID part.
	   (search (cadr parts)))            ; Extract the search part.
      ;; Execute the original function and perform search operations.
      (prog1 (funcall fn id args)
	;; Conditionally execute additional actions based on the search part.
	     (cond ((null search))                                   ; Do nothing if search part is null.
		   ((string-match-p "\\`[0-9]+\\'" search)           ; If search part is a number.
		    (forward-line (string-to-number search)))        ; Move N lines after the ID.
		   ((string-match "^/\\([^/]+\\)/$" search)          ; If search part is a regex match.
		    (let ((match (match-string 1 search)))           ; Extract the match string.
		      (save-excursion (org-link-search search))      ; Search for the match.
		      (when (re-search-forward match)                ; When match is found.
			(goto-char (match-beginning 0)))))           ; Move point to the match.
		   ((org-link-search search)))))))                   ; Otherwise, perform org-link-search.
;; Add Advice around id open functions
(advice-add 'org-id-open :around #'org-id-open--support-search)
(advice-add 'org-roam-id-open :around #'org-id-open--support-search)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants