2021-04-19 10:21:01 +00:00
|
|
|
|
;;; org-ctags.el --- Integrate Emacs "tags" Facility with Org -*- lexical-binding: t; -*-
|
|
|
|
|
|
2022-01-01 20:10:55 +00:00
|
|
|
|
;; Copyright (C) 2007-2022 Free Software Foundation, Inc.
|
2010-04-07 14:26:10 +00:00
|
|
|
|
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;; Author: Paul Sexton <eeeickythump@gmail.com>
|
2010-04-07 14:26:10 +00:00
|
|
|
|
;; Keywords: org, wp
|
2021-09-16 10:32:43 +00:00
|
|
|
|
|
2010-04-07 14:26:10 +00:00
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
;;
|
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 22:52:52 +00:00
|
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2010-04-07 14:26:10 +00:00
|
|
|
|
|
2021-09-16 10:32:43 +00:00
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;;
|
|
|
|
|
;; Synopsis
|
|
|
|
|
;; ========
|
|
|
|
|
;;
|
2016-08-23 20:13:56 +00:00
|
|
|
|
;; Allows Org mode to make use of the Emacs `etags' system. Defines
|
|
|
|
|
;; tag destinations in Org files as any text between <<double angled
|
|
|
|
|
;; brackets>>. This allows the tags-generation program `exuberant
|
|
|
|
|
;; ctags' to parse these files and create tag tables that record where
|
|
|
|
|
;; these destinations are found. Plain [[links]] in org mode files
|
|
|
|
|
;; which do not have <<matching destinations>> within the same file
|
|
|
|
|
;; will then be interpreted as links to these 'tagged' destinations,
|
|
|
|
|
;; allowing seamless navigation between multiple Org files. Topics
|
|
|
|
|
;; can be created in any org mode file and will always be found by
|
|
|
|
|
;; plain links from other files. Other file types recognized by ctags
|
|
|
|
|
;; (source code files, latex files, etc) will also be available as
|
|
|
|
|
;; destinations for plain links, and similarly, Org links will be
|
|
|
|
|
;; available as tags from source files. Finally, the function
|
|
|
|
|
;; `org-ctags-find-tag-interactive' lets you choose any known tag,
|
|
|
|
|
;; using autocompletion, and quickly jump to it.
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;;
|
|
|
|
|
;; Installation
|
|
|
|
|
;; ============
|
|
|
|
|
;;
|
|
|
|
|
;; Install org mode
|
|
|
|
|
;; Ensure org-ctags.el is somewhere in your emacs load path.
|
|
|
|
|
;; Download and install Exuberant ctags -- "http://ctags.sourceforge.net/"
|
|
|
|
|
;; Edit your .emacs file (see next section) and load emacs.
|
|
|
|
|
|
|
|
|
|
;; To put in your init file (.emacs):
|
|
|
|
|
;; ==================================
|
|
|
|
|
;;
|
|
|
|
|
;; Assuming you already have org mode installed and set up:
|
|
|
|
|
;;
|
|
|
|
|
;; (setq org-ctags-path-to-ctags "/path/to/ctags/executable")
|
|
|
|
|
;; (add-hook 'org-mode-hook
|
|
|
|
|
;; (lambda ()
|
|
|
|
|
;; (define-key org-mode-map "\C-co" 'org-ctags-find-tag-interactive)))
|
|
|
|
|
;;
|
|
|
|
|
;; By default, with org-ctags loaded, org will first try and visit the tag
|
|
|
|
|
;; with the same name as the link; then, if unsuccessful, ask the user if
|
|
|
|
|
;; he/she wants to rebuild the 'TAGS' database and try again; then ask if
|
|
|
|
|
;; the user wishes to append 'tag' as a new toplevel heading at the end of
|
2014-12-26 02:07:15 +00:00
|
|
|
|
;; the buffer; and finally, defer to org's default behavior which is to
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;; search the entire text of the current buffer for 'tag'.
|
|
|
|
|
;;
|
2014-12-26 02:07:15 +00:00
|
|
|
|
;; This behavior can be modified by changing the value of
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;; ORG-CTAGS-OPEN-LINK-FUNCTIONS. For example I have the following in my
|
2014-12-26 02:07:15 +00:00
|
|
|
|
;; .emacs, which describes the same behavior as the above paragraph with
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;; one difference:
|
|
|
|
|
;;
|
|
|
|
|
;; (setq org-ctags-open-link-functions
|
|
|
|
|
;; '(org-ctags-find-tag
|
|
|
|
|
;; org-ctags-ask-rebuild-tags-file-then-find-tag
|
|
|
|
|
;; org-ctags-ask-append-topic
|
2014-12-26 02:07:15 +00:00
|
|
|
|
;; org-ctags-fail-silently)) ; <-- prevents org default behavior
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;;
|
|
|
|
|
;;
|
|
|
|
|
;; Usage
|
|
|
|
|
;; =====
|
|
|
|
|
;;
|
|
|
|
|
;; When you click on a link "[[foo]]" and org cannot find a matching "<<foo>>"
|
2012-08-11 17:10:44 +00:00
|
|
|
|
;; in the current buffer, the tags facility will take over. The file TAGS in
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;; the active directory is examined to see if the tags facility knows about
|
2012-08-11 17:10:44 +00:00
|
|
|
|
;; "<<foo>>" in any other files. If it does, the matching file will be opened
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;; and the cursor will jump to the position of "<<foo>>" in that file.
|
|
|
|
|
;;
|
|
|
|
|
;; User-visible functions:
|
|
|
|
|
;; - `org-ctags-find-tag-interactive': type a tag (plain link) name and visit
|
2012-08-11 17:10:44 +00:00
|
|
|
|
;; it. With autocompletion. Bound to ctrl-O in the above setup.
|
|
|
|
|
;; - All the etags functions should work. These include:
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;;
|
|
|
|
|
;; M-. `find-tag' -- finds the tag at point
|
|
|
|
|
;;
|
|
|
|
|
;; C-M-. find-tag based on regular expression
|
|
|
|
|
;;
|
|
|
|
|
;; M-x tags-search RET -- like C-M-. but searches through ENTIRE TEXT
|
2012-08-11 17:10:44 +00:00
|
|
|
|
;; of ALL the files referenced in the TAGS file. A quick way to
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;; search through an entire 'project'.
|
|
|
|
|
;;
|
2012-08-11 17:10:44 +00:00
|
|
|
|
;; M-* "go back" from a tag jump. Like `org-mark-ring-goto'.
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;; You may need to bind this key yourself with (eg)
|
|
|
|
|
;; (global-set-key (kbd "<M-kp-multiply>") 'pop-tag-mark)
|
|
|
|
|
;;
|
|
|
|
|
;; (see etags chapter in Emacs manual for more)
|
|
|
|
|
;;
|
|
|
|
|
;;
|
|
|
|
|
;; Keeping the TAGS file up to date
|
|
|
|
|
;; ================================
|
|
|
|
|
;;
|
2016-08-23 20:13:56 +00:00
|
|
|
|
;; Tags mode has no way of knowing that you have created new tags by
|
|
|
|
|
;; typing in your Org buffer. New tags make it into the TAGS file in
|
|
|
|
|
;; 3 ways:
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;;
|
|
|
|
|
;; 1. You re-run (org-ctags-create-tags "directory") to rebuild the file.
|
|
|
|
|
;; 2. You put the function `org-ctags-ask-rebuild-tags-file-then-find-tag' in
|
|
|
|
|
;; your `org-open-link-functions' list, as is done in the setup
|
2012-08-11 17:10:44 +00:00
|
|
|
|
;; above. This will cause the TAGS file to be rebuilt whenever a link
|
|
|
|
|
;; cannot be found. This may be slow with large file collections however.
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;; 3. You run the following from the command line (all 1 line):
|
|
|
|
|
;;
|
|
|
|
|
;; ctags --langdef=orgmode --langmap=orgmode:.org
|
|
|
|
|
;; --regex-orgmode="/<<([^>]+)>>/\1/d,definition/"
|
|
|
|
|
;; -f /your/path/TAGS -e -R /your/path/*.org
|
|
|
|
|
;;
|
|
|
|
|
;; If you are paranoid, you might want to run (org-ctags-create-tags
|
|
|
|
|
;; "/path/to/org/files") at startup, by including the following toplevel form
|
2012-08-11 17:10:44 +00:00
|
|
|
|
;; in .emacs. However this can cause a pause of several seconds if ctags has
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;; to scan lots of files.
|
|
|
|
|
;;
|
|
|
|
|
;; (progn
|
|
|
|
|
;; (message "-- rebuilding tags tables...")
|
2013-09-15 04:41:09 +00:00
|
|
|
|
;; (mapc 'org-ctags-create-tags tags-table-list))
|
2010-01-02 07:24:54 +00:00
|
|
|
|
|
2010-07-15 20:26:51 +00:00
|
|
|
|
;;; Code:
|
|
|
|
|
|
2018-03-17 00:41:17 +00:00
|
|
|
|
(eval-when-compile (require 'cl-lib))
|
2010-01-02 07:24:54 +00:00
|
|
|
|
(require 'org)
|
|
|
|
|
|
|
|
|
|
(defgroup org-ctags nil
|
|
|
|
|
"Options concerning use of ctags within org mode."
|
|
|
|
|
:tag "Org-Ctags"
|
|
|
|
|
:group 'org-link)
|
|
|
|
|
|
|
|
|
|
(defvar org-ctags-enabled-p t
|
|
|
|
|
"Activate ctags support in org mode?")
|
|
|
|
|
|
|
|
|
|
(defvar org-ctags-tag-regexp "/<<([^>]+)>>/\\1/d,definition/"
|
2010-07-15 20:26:51 +00:00
|
|
|
|
"Regexp expression used by ctags external program.
|
2016-08-23 20:13:56 +00:00
|
|
|
|
The regexp matches tag destinations in Org files.
|
2010-01-02 07:24:54 +00:00
|
|
|
|
Format is: /REGEXP/TAGNAME/FLAGS,TAGTYPE/
|
|
|
|
|
See the ctags documentation for more information.")
|
|
|
|
|
|
2010-04-01 11:11:54 +00:00
|
|
|
|
(defcustom org-ctags-path-to-ctags
|
2013-05-12 04:08:09 +00:00
|
|
|
|
(if (executable-find "ctags-exuberant") "ctags-exuberant" "ctags")
|
|
|
|
|
"Name of the ctags executable file."
|
2012-03-19 20:38:12 +00:00
|
|
|
|
:version "24.1"
|
2010-01-02 07:24:54 +00:00
|
|
|
|
:type 'file)
|
|
|
|
|
|
|
|
|
|
(defcustom org-ctags-open-link-functions
|
2010-04-01 11:11:54 +00:00
|
|
|
|
'(org-ctags-find-tag
|
2010-01-02 07:24:54 +00:00
|
|
|
|
org-ctags-ask-rebuild-tags-file-then-find-tag
|
|
|
|
|
org-ctags-ask-append-topic)
|
2020-12-19 11:18:11 +00:00
|
|
|
|
"List of functions to be prepended to ORG-OPEN-LINK-FUNCTIONS by ORG-CTAGS."
|
2012-03-19 20:38:12 +00:00
|
|
|
|
:version "24.1"
|
2010-01-02 07:24:54 +00:00
|
|
|
|
:type 'hook
|
2010-04-01 11:11:54 +00:00
|
|
|
|
:options '(org-ctags-find-tag
|
2010-01-02 07:24:54 +00:00
|
|
|
|
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))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defvar org-ctags-tag-list nil
|
2010-07-15 20:26:51 +00:00
|
|
|
|
"List of all tags in the active TAGS file.
|
|
|
|
|
Created as a local variable in each buffer.")
|
2010-01-02 07:24:54 +00:00
|
|
|
|
|
|
|
|
|
(defcustom org-ctags-new-topic-template
|
2012-04-26 20:42:45 +00:00
|
|
|
|
"* <<%t>>\n\n\n\n\n\n"
|
2010-01-02 07:24:54 +00:00
|
|
|
|
"Text to insert when creating a new org file via opening a hyperlink.
|
|
|
|
|
The following patterns are replaced in the string:
|
|
|
|
|
`%t' - replaced with the capitalized title of the hyperlink"
|
2012-03-19 20:38:12 +00:00
|
|
|
|
:version "24.1"
|
2010-01-02 07:24:54 +00:00
|
|
|
|
: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))))))
|
|
|
|
|
|
|
|
|
|
|
Replace all uses of the old `defadvice` with the new `advice-add`
* lisp/org.el (org-run-like-in-org-mode): Strength reduce `eval`
to `cl-progv`.
(org--check-org-structure-template-alist): Strength reduce `eval`
to `symbol-value`.
(org-map-entries, org-eval-in-calendar, org-diary-sexp-entry):
Make sure we use the new lexically scoped dialect.
(org--math-always-on): New function, extracted from advice.
(org-cdlatex-mode): Use it with `advice-add`.
(org-self-insert-command): Simplify `and`+`listp` into `consp`.
(org-submit-bug-report):
Make sure we use the new lexically scoped dialect.
* lisp/org-protocol.el (org-protocol-convert-query-to-plist):
Use `cl-mapcan`.
(org--protocol-detect-protocol-server): New function, extracted
from advice.
(server-visit-files): Use it with `advice-add`.
* lisp/org-mouse.el (org--mouse-dnd-insert-text): New function, extracted
from advice.
(dnd-insert-text): Use it with `advice-add`.
(org--mouse-dnd-open-file): New function, extracted from advice.
(dnd-open-file): Use it with `advice-add`.
(org--mouse-open-at-point): New function, extracted from advice.
(org-mode-hook): Advise `org-open-at-point` with `advice-add`.
* lisp/org-ctags.el (org--ctags-load-tag-list): New function, extracted
from advice.
(visit-tags-table): Use it with `advice-add`.
(org--ctags-set-org-mark-before-finding-tag): New function, extracted
from advice.
(xref-find-definitions): Use it with `advice-add`.
* lisp/org-compat.el (org-bookmark-jump-unhide): Accept (unused) args.
(save-place-find-file-hook): Use `advice-add`.
(org--ecb-show-context): New function, extracted from advice.
(ecb-method-clicked): Use it with `advice-add`.
(org-mark-jump-unhide): Accept (unused) args.
(pop-to-mark-command, exchange-point-and-mark, pop-global-mark):
Use `advice-add`.
Along the way, remove some redundant `:group` args
(redundant because they specify the same group as would be used by
default anyway) and make a few other simplifications.
Also don't bother putting `advice-add` within an eval-after-load
since the advice machinery already takes care of handling it.
2022-04-01 05:50:01 +00:00
|
|
|
|
(advice-add 'visit-tags-table :after #'org--ctags-load-tag-list)
|
|
|
|
|
(defun org--ctags-load-tag-list (&rest _)
|
2010-01-02 07:24:54 +00:00
|
|
|
|
(when (and org-ctags-enabled-p tags-file-name)
|
2015-11-05 16:47:38 +00:00
|
|
|
|
(setq-local org-ctags-tag-list
|
|
|
|
|
(org-ctags-all-tags-in-current-tags-table))))
|
2010-01-02 07:24:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun org-ctags-enable ()
|
|
|
|
|
(put 'org-mode 'find-tag-default-function 'org-ctags-find-tag-at-point)
|
|
|
|
|
(setq org-ctags-enabled-p t)
|
2010-01-18 07:21:33 +00:00
|
|
|
|
(dolist (fn org-ctags-open-link-functions)
|
|
|
|
|
(add-hook 'org-open-link-functions fn t)))
|
2010-04-01 11:11:54 +00:00
|
|
|
|
|
2010-01-02 07:24:54 +00:00
|
|
|
|
|
2010-07-15 20:26:51 +00:00
|
|
|
|
;;; General utility functions. ===============================================
|
2010-07-06 06:25:48 +00:00
|
|
|
|
;; These work outside org-ctags mode.
|
2010-01-02 07:24:54 +00:00
|
|
|
|
|
|
|
|
|
(defun org-ctags-get-filename-for-tag (tag)
|
2010-07-15 20:26:51 +00:00
|
|
|
|
"TAG is a string. Search the active TAGS file for a matching tag.
|
|
|
|
|
If the tag is found, return a list containing the filename, line number, and
|
2010-01-02 07:24:54 +00:00
|
|
|
|
buffer position where the tag is found."
|
|
|
|
|
(interactive "sTag: ")
|
|
|
|
|
(unless tags-file-name
|
|
|
|
|
(call-interactively (visit-tags-table)))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(visit-tags-table-buffer 'same)
|
|
|
|
|
(when tags-file-name
|
|
|
|
|
(with-current-buffer (get-file-buffer tags-file-name)
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(cond
|
2018-03-30 00:41:11 +00:00
|
|
|
|
((re-search-forward (format "^.*\^?%s\^A\\([0-9]+\\),\\([0-9]+\\)$"
|
2010-01-02 07:24:54 +00:00
|
|
|
|
(regexp-quote tag)) nil t)
|
|
|
|
|
(let ((line (string-to-number (match-string 1)))
|
|
|
|
|
(pos (string-to-number (match-string 2))))
|
|
|
|
|
(cond
|
|
|
|
|
((re-search-backward "\n\\(.*\\),[0-9]+\n")
|
|
|
|
|
(list (match-string 1) line pos))
|
|
|
|
|
(t ; can't find a file name preceding the matched
|
2012-08-11 17:10:44 +00:00
|
|
|
|
; tag??
|
2010-01-02 07:24:54 +00:00
|
|
|
|
(error "Malformed TAGS file: %s" (buffer-name))))))
|
|
|
|
|
(t ; tag not found
|
|
|
|
|
nil))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun org-ctags-all-tags-in-current-tags-table ()
|
|
|
|
|
"Read all tags defined in the active TAGS file, into a list of strings.
|
|
|
|
|
Return the list."
|
|
|
|
|
(interactive)
|
|
|
|
|
(let ((taglist nil))
|
|
|
|
|
(unless tags-file-name
|
|
|
|
|
(call-interactively (visit-tags-table)))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(visit-tags-table-buffer 'same)
|
|
|
|
|
(with-current-buffer (get-file-buffer tags-file-name)
|
|
|
|
|
(goto-char (point-min))
|
2018-03-30 00:41:11 +00:00
|
|
|
|
(while (re-search-forward "^.*\^?\\(.*\\)\^A\\([0-9]+\\),\\([0-9]+\\)$"
|
2010-01-02 07:24:54 +00:00
|
|
|
|
nil t)
|
|
|
|
|
(push (substring-no-properties (match-string 1)) taglist)))
|
|
|
|
|
taglist)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun org-ctags-string-search-and-replace (search replace string)
|
|
|
|
|
"Replace all instances of SEARCH with REPLACE in STRING."
|
|
|
|
|
(replace-regexp-in-string (regexp-quote search) replace string t t))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Internal functions =======================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun org-ctags-open-file (name &optional title)
|
2010-07-15 20:26:51 +00:00
|
|
|
|
"Visit or create a file called `NAME.org', and insert a new topic.
|
|
|
|
|
The new topic will be titled NAME (or TITLE if supplied)."
|
2010-01-02 07:24:54 +00:00
|
|
|
|
(interactive "sFile name: ")
|
2016-06-20 13:04:33 +00:00
|
|
|
|
(condition-case v
|
|
|
|
|
(progn
|
|
|
|
|
(org-open-file name t)
|
|
|
|
|
(message "Opened file OK")
|
|
|
|
|
(goto-char (point-max))
|
|
|
|
|
(insert (org-ctags-string-search-and-replace
|
|
|
|
|
"%t" (capitalize (or title name))
|
|
|
|
|
org-ctags-new-topic-template))
|
|
|
|
|
(message "Inserted new file text OK")
|
|
|
|
|
(org-mode-restart))
|
|
|
|
|
(error (error "Error %S in org-ctags-open-file" v))))
|
2010-01-02 07:24:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;; Misc interoperability with etags system =================================
|
|
|
|
|
|
|
|
|
|
|
Replace all uses of the old `defadvice` with the new `advice-add`
* lisp/org.el (org-run-like-in-org-mode): Strength reduce `eval`
to `cl-progv`.
(org--check-org-structure-template-alist): Strength reduce `eval`
to `symbol-value`.
(org-map-entries, org-eval-in-calendar, org-diary-sexp-entry):
Make sure we use the new lexically scoped dialect.
(org--math-always-on): New function, extracted from advice.
(org-cdlatex-mode): Use it with `advice-add`.
(org-self-insert-command): Simplify `and`+`listp` into `consp`.
(org-submit-bug-report):
Make sure we use the new lexically scoped dialect.
* lisp/org-protocol.el (org-protocol-convert-query-to-plist):
Use `cl-mapcan`.
(org--protocol-detect-protocol-server): New function, extracted
from advice.
(server-visit-files): Use it with `advice-add`.
* lisp/org-mouse.el (org--mouse-dnd-insert-text): New function, extracted
from advice.
(dnd-insert-text): Use it with `advice-add`.
(org--mouse-dnd-open-file): New function, extracted from advice.
(dnd-open-file): Use it with `advice-add`.
(org--mouse-open-at-point): New function, extracted from advice.
(org-mode-hook): Advise `org-open-at-point` with `advice-add`.
* lisp/org-ctags.el (org--ctags-load-tag-list): New function, extracted
from advice.
(visit-tags-table): Use it with `advice-add`.
(org--ctags-set-org-mark-before-finding-tag): New function, extracted
from advice.
(xref-find-definitions): Use it with `advice-add`.
* lisp/org-compat.el (org-bookmark-jump-unhide): Accept (unused) args.
(save-place-find-file-hook): Use `advice-add`.
(org--ecb-show-context): New function, extracted from advice.
(ecb-method-clicked): Use it with `advice-add`.
(org-mark-jump-unhide): Accept (unused) args.
(pop-to-mark-command, exchange-point-and-mark, pop-global-mark):
Use `advice-add`.
Along the way, remove some redundant `:group` args
(redundant because they specify the same group as would be used by
default anyway) and make a few other simplifications.
Also don't bother putting `advice-add` within an eval-after-load
since the advice machinery already takes care of handling it.
2022-04-01 05:50:01 +00:00
|
|
|
|
(advice-add 'xref-find-definitions :before
|
|
|
|
|
#'org--ctags-set-org-mark-before-finding-tag)
|
|
|
|
|
(defun org--ctags-set-org-mark-before-finding-tag (&rest _)
|
2010-01-02 07:24:54 +00:00
|
|
|
|
"Before trying to find a tag, save our current position on org mark ring."
|
|
|
|
|
(save-excursion
|
2015-11-13 22:21:26 +00:00
|
|
|
|
(when (and (derived-mode-p 'org-mode) org-ctags-enabled-p)
|
|
|
|
|
(org-mark-ring-push))))
|
2010-01-02 07:24:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun org-ctags-find-tag-at-point ()
|
|
|
|
|
"Determine default tag to search for, based on text at point.
|
|
|
|
|
If there is no plausible default, return nil."
|
|
|
|
|
(let (from to bound)
|
|
|
|
|
(when (or (ignore-errors
|
|
|
|
|
;; Look for hyperlink around `point'.
|
|
|
|
|
(save-excursion
|
|
|
|
|
(search-backward "[[") (setq from (+ 2 (point))))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char from)
|
|
|
|
|
(search-forward "]") (setq to (- (point) 1)))
|
|
|
|
|
(and (> to from) (>= (point) from) (<= (point) to)))
|
|
|
|
|
(progn
|
|
|
|
|
;; Look at text around `point'.
|
|
|
|
|
(save-excursion
|
|
|
|
|
(skip-syntax-backward "w_") (setq from (point)))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(skip-syntax-forward "w_") (setq to (point)))
|
|
|
|
|
(> to from))
|
|
|
|
|
;; Look between `line-beginning-position' and `point'.
|
|
|
|
|
(save-excursion
|
|
|
|
|
(and (setq bound (line-beginning-position))
|
|
|
|
|
(skip-syntax-backward "^w_" bound)
|
|
|
|
|
(> (setq to (point)) bound)
|
|
|
|
|
(skip-syntax-backward "w_")
|
|
|
|
|
(setq from (point))))
|
|
|
|
|
;; Look between `point' and `line-end-position'.
|
|
|
|
|
(save-excursion
|
|
|
|
|
(and (setq bound (line-end-position))
|
|
|
|
|
(skip-syntax-forward "^w_" bound)
|
|
|
|
|
(< (setq from (point)) bound)
|
|
|
|
|
(skip-syntax-forward "w_")
|
|
|
|
|
(setq to (point)))))
|
|
|
|
|
(buffer-substring-no-properties from to))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Functions for use with 'org-open-link-functions' hook =================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun org-ctags-find-tag (name)
|
|
|
|
|
"This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS.
|
2010-07-15 20:26:51 +00:00
|
|
|
|
Look for a tag called `NAME' in the current TAGS table. If it is found,
|
2010-01-02 07:24:54 +00:00
|
|
|
|
visit the file and location where the tag is found."
|
|
|
|
|
(interactive "sTag: ")
|
|
|
|
|
(let ((old-buf (current-buffer))
|
|
|
|
|
(old-pnt (point-marker))
|
|
|
|
|
(old-mark (copy-marker (mark-marker))))
|
|
|
|
|
(condition-case nil
|
2015-11-13 22:21:26 +00:00
|
|
|
|
(progn (xref-find-definitions name)
|
2010-01-02 07:24:54 +00:00
|
|
|
|
t)
|
|
|
|
|
(error
|
|
|
|
|
;; only restore old location if find-tag raises error
|
|
|
|
|
(set-buffer old-buf)
|
|
|
|
|
(goto-char old-pnt)
|
|
|
|
|
(set-marker (mark-marker) old-mark)
|
|
|
|
|
nil))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun org-ctags-visit-buffer-or-file (name &optional create)
|
|
|
|
|
"This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS.
|
2010-07-15 20:26:51 +00:00
|
|
|
|
Visit buffer named `NAME.org'. If there is no such buffer, visit the file
|
|
|
|
|
with the same name if it exists. If the file does not exist, then behavior
|
2010-01-02 07:24:54 +00:00
|
|
|
|
depends on the value of CREATE.
|
|
|
|
|
|
2010-07-15 20:26:51 +00:00
|
|
|
|
If CREATE is nil (default), then return nil. Do not create a new file.
|
2010-01-02 07:24:54 +00:00
|
|
|
|
If CREATE is t, create the new file and visit it.
|
|
|
|
|
If CREATE is the symbol `ask', then ask the user if they wish to create
|
|
|
|
|
the new file."
|
|
|
|
|
(interactive)
|
|
|
|
|
(let ((filename (concat (substitute-in-file-name
|
|
|
|
|
(expand-file-name name))
|
|
|
|
|
".org")))
|
|
|
|
|
(cond
|
|
|
|
|
((get-buffer (concat name ".org"))
|
|
|
|
|
;; Buffer is already open
|
2016-06-23 08:00:00 +00:00
|
|
|
|
(pop-to-buffer-same-window (get-buffer (concat name ".org"))))
|
2010-01-02 07:24:54 +00:00
|
|
|
|
((file-exists-p filename)
|
|
|
|
|
;; File exists but is not open --> open it
|
|
|
|
|
(message "Opening existing org file `%S'..."
|
|
|
|
|
filename)
|
|
|
|
|
(org-open-file filename t))
|
|
|
|
|
((or (eql create t)
|
|
|
|
|
(and (eql create 'ask)
|
2015-09-21 04:23:36 +00:00
|
|
|
|
(y-or-n-p (format-message
|
|
|
|
|
"File `%s.org' not found; create?" name))))
|
2010-01-02 07:24:54 +00:00
|
|
|
|
(org-ctags-open-file filename name))
|
|
|
|
|
(t ;; File does not exist, and we don't want to create it.
|
|
|
|
|
nil))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun org-ctags-ask-visit-buffer-or-file (name)
|
|
|
|
|
"This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS.
|
|
|
|
|
Wrapper for org-ctags-visit-buffer-or-file, which ensures the user is
|
|
|
|
|
asked before creating a new file."
|
|
|
|
|
(org-ctags-visit-buffer-or-file name 'ask))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun org-ctags-append-topic (name &optional narrowp)
|
|
|
|
|
"This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS.
|
2012-08-11 17:10:44 +00:00
|
|
|
|
Append a new toplevel heading to the end of the current buffer. The
|
2010-01-02 07:24:54 +00:00
|
|
|
|
heading contains NAME surrounded by <<angular brackets>>, thus making
|
|
|
|
|
the heading a destination for the tag `NAME'."
|
|
|
|
|
(interactive "sTopic: ")
|
|
|
|
|
(widen)
|
|
|
|
|
(goto-char (point-max))
|
|
|
|
|
(newline 2)
|
|
|
|
|
(message "Adding topic in buffer %s" (buffer-name))
|
|
|
|
|
(insert (org-ctags-string-search-and-replace
|
|
|
|
|
"%t" (capitalize name) org-ctags-new-topic-template))
|
|
|
|
|
(backward-char 4)
|
|
|
|
|
(end-of-line)
|
|
|
|
|
(forward-line 2)
|
|
|
|
|
(when narrowp
|
|
|
|
|
;;(org-tree-to-indirect-buffer 1) ;; opens new frame
|
|
|
|
|
(org-narrow-to-subtree))
|
|
|
|
|
t)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun org-ctags-ask-append-topic (name &optional narrowp)
|
|
|
|
|
"This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS.
|
|
|
|
|
Wrapper for org-ctags-append-topic, which first asks the user if they want
|
|
|
|
|
to append a new topic."
|
2015-09-21 04:23:36 +00:00
|
|
|
|
(if (y-or-n-p (format-message
|
|
|
|
|
"Topic `%s' not found; append to end of buffer?" name))
|
2010-01-02 07:24:54 +00:00
|
|
|
|
(org-ctags-append-topic name narrowp)
|
|
|
|
|
nil))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun org-ctags-rebuild-tags-file-then-find-tag (name)
|
|
|
|
|
"This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS.
|
|
|
|
|
Like ORG-CTAGS-FIND-TAG, but calls the external ctags program first,
|
|
|
|
|
to rebuild (update) the TAGS file."
|
|
|
|
|
(unless tags-file-name
|
|
|
|
|
(call-interactively (visit-tags-table)))
|
|
|
|
|
(when (buffer-file-name)
|
|
|
|
|
(org-ctags-create-tags))
|
|
|
|
|
(org-ctags-find-tag name))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun org-ctags-ask-rebuild-tags-file-then-find-tag (name)
|
|
|
|
|
"This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS.
|
|
|
|
|
Wrapper for org-ctags-rebuild-tags-file-then-find-tag."
|
|
|
|
|
(if (and (buffer-file-name)
|
2012-08-11 17:10:44 +00:00
|
|
|
|
(y-or-n-p
|
2015-09-21 04:23:36 +00:00
|
|
|
|
(format-message
|
2012-08-11 17:10:44 +00:00
|
|
|
|
"Tag `%s' not found. Rebuild table `%s/TAGS' and look again?"
|
|
|
|
|
name
|
|
|
|
|
(file-name-directory (buffer-file-name)))))
|
|
|
|
|
(org-ctags-rebuild-tags-file-then-find-tag name)
|
2010-01-02 07:24:54 +00:00
|
|
|
|
nil))
|
|
|
|
|
|
|
|
|
|
|
2016-06-20 13:04:33 +00:00
|
|
|
|
(defun org-ctags-fail-silently (_name)
|
2010-01-02 07:24:54 +00:00
|
|
|
|
"This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS.
|
2016-06-20 13:04:33 +00:00
|
|
|
|
Put as the last function in the list if you want to prevent Org's
|
|
|
|
|
default behavior of free text search."
|
2010-01-02 07:24:54 +00:00
|
|
|
|
t)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; User-visible functions ===================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun org-ctags-create-tags (&optional directory-name)
|
2010-07-15 20:26:51 +00:00
|
|
|
|
"(Re)create tags file in the directory of the active buffer.
|
|
|
|
|
The file will contain tag definitions for all the files in the
|
|
|
|
|
directory and its subdirectories which are recognized by ctags.
|
|
|
|
|
This will include files ending in `.org' as well as most other
|
|
|
|
|
source files (.C, .H, .EL, .LISP, etc). All the resulting tags
|
|
|
|
|
end up in one file, called TAGS, located in the directory. This
|
|
|
|
|
function may take several seconds to finish if the directory or
|
|
|
|
|
its subdirectories contain large numbers of taggable files."
|
2010-01-02 07:24:54 +00:00
|
|
|
|
(interactive)
|
2016-07-25 14:34:48 +00:00
|
|
|
|
(cl-assert (buffer-file-name))
|
2010-01-02 07:24:54 +00:00
|
|
|
|
(let ((dir-name (or directory-name
|
|
|
|
|
(file-name-directory (buffer-file-name))))
|
|
|
|
|
(exitcode nil))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(setq exitcode
|
|
|
|
|
(shell-command
|
|
|
|
|
(format (concat "%s --langdef=orgmode --langmap=orgmode:.org "
|
|
|
|
|
"--regex-orgmode=\"%s\" -f \"%s\" -e -R \"%s\"")
|
|
|
|
|
org-ctags-path-to-ctags
|
|
|
|
|
org-ctags-tag-regexp
|
|
|
|
|
(expand-file-name (concat dir-name "/TAGS"))
|
2010-01-18 07:21:33 +00:00
|
|
|
|
(expand-file-name (concat dir-name "/*")))))
|
2010-01-02 07:24:54 +00:00
|
|
|
|
(cond
|
|
|
|
|
((eql 0 exitcode)
|
2015-11-05 16:47:38 +00:00
|
|
|
|
(setq-local org-ctags-tag-list
|
|
|
|
|
(org-ctags-all-tags-in-current-tags-table)))
|
2010-01-02 07:24:54 +00:00
|
|
|
|
(t
|
|
|
|
|
;; This seems to behave differently on Linux, so just ignore
|
|
|
|
|
;; error codes for now
|
|
|
|
|
;;(error "Calling ctags executable resulted in error code: %s"
|
|
|
|
|
;; exitcode)
|
|
|
|
|
nil)))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defvar org-ctags-find-tag-history nil
|
|
|
|
|
"History of tags visited by org-ctags-find-tag-interactive.")
|
|
|
|
|
|
|
|
|
|
(defun org-ctags-find-tag-interactive ()
|
2010-07-15 20:26:51 +00:00
|
|
|
|
"Prompt for the name of a tag, with autocompletion, then visit the named tag.
|
|
|
|
|
Uses `ido-mode' if available.
|
2010-01-02 07:24:54 +00:00
|
|
|
|
If the user enters a string that does not match an existing tag, create
|
|
|
|
|
a new topic."
|
|
|
|
|
(interactive)
|
|
|
|
|
(let* ((completing-read-fn (if (fboundp 'ido-completing-read)
|
|
|
|
|
'ido-completing-read
|
|
|
|
|
'completing-read))
|
|
|
|
|
(tag (funcall completing-read-fn "Topic: " org-ctags-tag-list
|
|
|
|
|
nil 'confirm nil 'org-ctags-find-tag-history)))
|
|
|
|
|
(when tag
|
|
|
|
|
(cond
|
|
|
|
|
((member tag org-ctags-tag-list)
|
|
|
|
|
;; Existing tag
|
|
|
|
|
(push tag org-ctags-find-tag-history)
|
2015-11-13 22:21:26 +00:00
|
|
|
|
(xref-find-definitions tag))
|
2010-01-02 07:24:54 +00:00
|
|
|
|
(t
|
|
|
|
|
;; New tag
|
|
|
|
|
(run-hook-with-args-until-success
|
2012-08-11 17:10:44 +00:00
|
|
|
|
'org-open-link-functions tag))))))
|
2010-01-02 07:24:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(org-ctags-enable)
|
|
|
|
|
|
|
|
|
|
(provide 'org-ctags)
|
|
|
|
|
|
|
|
|
|
;;; org-ctags.el ends here
|