#+name: org-id-remap
#+begin_src emacs-lisp
(lambda (&rest mappings)
"Org ID 重映射.
零参时, toggle org-id-remap.
单参时:
'reset 重置重映射;
'enable? 查询使能情况;
nil 禁用重映射;其他使能。
其他情况时,建立 id 映射,调用形式如下:
(org-id-remap
\"id1\" \"[[id:real-taret1]]\"
\"id2\" \"[[id:real-taret2]]\"
...)
"
(interactive)
(declare (indent 0))
(cond
;; 无参时, toggle org-id-remap.
((length= mappings 0)
(cond
((advice-member-p open 'org-id-open)
(advice-remove 'org-id-open open)
(message "Org ID remap disable."))
(t
(advice-add 'org-id-open :around open)
(message "Org ID remap enable."))))
;; 单参时,
;; 'reset 重置重映射;
;; nil 禁用重映射,
;; 其他使能重映射。
((length= mappings 1)
(cond
((null (car mappings))
(advice-remove 'org-id-open open)
(message "Org ID remap disable."))
((eq 'enable? (car mappings))
(not
(not
(advice-member-p open 'org-id-open))))
((eq 'reset (car mappings))
(clrhash id->ids)
(clrhash id->urls)
(message "Org ID mapping reset."))
(t
(advice-add 'org-id-open :around open)
(message "Org ID remap enable."))))
;; 其他情况时,建立 id 映射,调用形式如下:
;; (org-id-remap
;; "id1" "[[id:real-taret1]]"
;; "id2" "[[id:real-taret2]]"
;; ...)
(t
(let ((remap
(lambda (id target)
(cond
((string-match-p
"\\[\\[id:" target)
(cl-pushnew
target (gethash id id->ids)
:test #'equal))
((string-match-p
"^\\[\\[http[s]?:" target)
(cl-pushnew
target (gethash id id->urls)
:test #'equal))))))
(dotimes (i (/ (length mappings) 2))
(funcall
remap
(nth (* 2 i) mappings)
(nth (1+ (* 2 i)) mappings)))))))
#+end_src