From 0d260a0f260afb0f407d00b6a53ed2121a240203 Mon Sep 17 00:00:00 2001 From: Max Nikulin Date: Mon, 29 Apr 2024 21:34:13 +0700 Subject: [PATCH 1/2] org-ctags.el: Define unload function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lisp/org-ctags.el (org-ctags-unload-function): New function to cleanup during `unload-feature' call. (org-ctags--open-link-functions-list org-ctags-open-link-functions): Define and use list of options available for `org-open-link-functions'. (org-ctags--visit-tags-table): Give a name to remove the function from `org-mode-hook' on library unload. Prevent the following error after library unloading Symbol’s function definition is void: org-ctags-find-tag --- lisp/org-ctags.el | 60 +++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/lisp/org-ctags.el b/lisp/org-ctags.el index 6431a2765..c6f7fc708 100644 --- a/lisp/org-ctags.el +++ b/lisp/org-ctags.el @@ -161,6 +161,20 @@ (defcustom org-ctags-path-to-ctags :version "24.1" :type 'file) +(defconst org-ctags--open-link-functions-list + (list + #'org-ctags-find-tag + #'org-ctags-ask-rebuild-tags-file-then-find-tag + #'org-ctags-rebuild-tags-file-then-find-tag + #'org-ctags-ask-append-topic + #'org-ctags-append-topic + #'org-ctags-ask-visit-buffer-or-file + #'org-ctags-visit-buffer-or-file + #'org-ctags-fail-silently) + "Options for `org-open-link-functions'. +Ensure that the user option and `unload-feature' +use the same set of functions.") + (defcustom org-ctags-open-link-functions '(org-ctags-find-tag org-ctags-ask-rebuild-tags-file-then-find-tag @@ -168,14 +182,7 @@ (defcustom org-ctags-open-link-functions "List of functions to be prepended to ORG-OPEN-LINK-FUNCTIONS by ORG-CTAGS." :version "24.1" :type 'hook - :options '(org-ctags-find-tag - org-ctags-ask-rebuild-tags-file-then-find-tag - org-ctags-rebuild-tags-file-then-find-tag - org-ctags-ask-append-topic - org-ctags-append-topic - org-ctags-ask-visit-buffer-or-file - org-ctags-visit-buffer-or-file - org-ctags-fail-silently)) + :options org-ctags--open-link-functions-list) (defvar org-ctags-tag-list nil @@ -191,18 +198,20 @@ (defcustom org-ctags-new-topic-template :type 'string) -(add-hook 'org-mode-hook - (lambda () - (when (and org-ctags-enabled-p - (buffer-file-name)) - ;; Make sure this file's directory is added to default - ;; directories in which to search for tags. - (let ((tags-filename - (expand-file-name - (concat (file-name-directory (buffer-file-name)) - "/TAGS")))) - (when (file-exists-p tags-filename) - (visit-tags-table tags-filename)))))) +(defun org-ctags--visit-tags-table () + "Load tags for current file. +A function for `org-mode-hook." + (when (and org-ctags-enabled-p + (buffer-file-name)) + ;; Make sure this file's directory is added to default + ;; directories in which to search for tags. + (let ((tags-filename + (expand-file-name + (concat (file-name-directory (buffer-file-name)) + "/TAGS")))) + (when (file-exists-p tags-filename) + (visit-tags-table tags-filename))))) +(add-hook 'org-mode-hook #'org-ctags--visit-tags-table) (advice-add 'visit-tags-table :after #'org--ctags-load-tag-list) @@ -219,6 +228,17 @@ (defun org-ctags-enable () (add-hook 'org-open-link-functions fn t))) +(defun org-ctags-unload-function () + "Disable `org-ctags' library. +Called by `unload-feature'." + (put 'org-mode 'find-tag-default-function nil) + (advice-remove 'visit-tags-table #'org--ctags-load-tag-list) + (advice-remove 'xref-find-definitions + #'org--ctags-set-org-mark-before-finding-tag) + (dolist (fn org-ctags--open-link-functions-list) + (remove-hook 'org-open-link-functions fn nil))) + + ;;; General utility functions. =============================================== ;; These work outside org-ctags mode. -- 2.39.2