Commit | Line | Data |
---|---|---|
ed21c5c8 | 1 | ;;; org-ctags.el - Integrate Emacs "tags" facility with org mode. |
45d6be0a | 2 | ;; |
b73f1974 | 3 | ;; Copyright (C) 2007-2012 Free Software Foundation, Inc. |
ed21c5c8 | 4 | |
45d6be0a | 5 | ;; Author: Paul Sexton <eeeickythump@gmail.com> |
e66ba1df | 6 | |
ed21c5c8 CD |
7 | |
8 | ;; Keywords: org, wp | |
ed21c5c8 CD |
9 | ;; |
10 | ;; This file is part of GNU Emacs. | |
11 | ;; | |
12 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation, either version 3 of the License, or | |
15 | ;; (at your option) any later version. | |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
24 | ||
45d6be0a SM |
25 | ;; |
26 | ;; Synopsis | |
27 | ;; ======== | |
28 | ;; | |
8223b1d2 | 29 | ;; Allows org-mode to make use of the Emacs `etags' system. Defines tag |
45d6be0a SM |
30 | ;; destinations in org-mode files as any text between <<double angled |
31 | ;; brackets>>. This allows the tags-generation program `exuberant ctags' to | |
32 | ;; parse these files and create tag tables that record where these | |
8223b1d2 | 33 | ;; destinations are found. Plain [[links]] in org mode files which do not have |
45d6be0a SM |
34 | ;; <<matching destinations>> within the same file will then be interpreted as |
35 | ;; links to these 'tagged' destinations, allowing seamless navigation between | |
8223b1d2 BG |
36 | ;; multiple org-mode files. Topics can be created in any org mode file and |
37 | ;; will always be found by plain links from other files. Other file types | |
27e428e7 | 38 | ;; recognized by ctags (source code files, latex files, etc) will also be |
45d6be0a | 39 | ;; available as destinations for plain links, and similarly, org-mode links |
8223b1d2 | 40 | ;; will be available as tags from source files. Finally, the function |
45d6be0a SM |
41 | ;; `org-ctags-find-tag-interactive' lets you choose any known tag, using |
42 | ;; autocompletion, and quickly jump to it. | |
43 | ;; | |
44 | ;; Installation | |
45 | ;; ============ | |
46 | ;; | |
47 | ;; Install org mode | |
48 | ;; Ensure org-ctags.el is somewhere in your emacs load path. | |
49 | ;; Download and install Exuberant ctags -- "http://ctags.sourceforge.net/" | |
50 | ;; Edit your .emacs file (see next section) and load emacs. | |
51 | ||
52 | ;; To put in your init file (.emacs): | |
53 | ;; ================================== | |
54 | ;; | |
55 | ;; Assuming you already have org mode installed and set up: | |
56 | ;; | |
57 | ;; (setq org-ctags-path-to-ctags "/path/to/ctags/executable") | |
58 | ;; (add-hook 'org-mode-hook | |
59 | ;; (lambda () | |
60 | ;; (define-key org-mode-map "\C-co" 'org-ctags-find-tag-interactive))) | |
61 | ;; | |
62 | ;; By default, with org-ctags loaded, org will first try and visit the tag | |
63 | ;; with the same name as the link; then, if unsuccessful, ask the user if | |
64 | ;; he/she wants to rebuild the 'TAGS' database and try again; then ask if | |
65 | ;; the user wishes to append 'tag' as a new toplevel heading at the end of | |
e66ba1df | 66 | ;; the buffer; and finally, defer to org's default behaviour which is to |
45d6be0a SM |
67 | ;; search the entire text of the current buffer for 'tag'. |
68 | ;; | |
e66ba1df | 69 | ;; This behaviour can be modified by changing the value of |
45d6be0a | 70 | ;; ORG-CTAGS-OPEN-LINK-FUNCTIONS. For example I have the following in my |
e66ba1df | 71 | ;; .emacs, which describes the same behaviour as the above paragraph with |
45d6be0a SM |
72 | ;; one difference: |
73 | ;; | |
74 | ;; (setq org-ctags-open-link-functions | |
75 | ;; '(org-ctags-find-tag | |
76 | ;; org-ctags-ask-rebuild-tags-file-then-find-tag | |
77 | ;; org-ctags-ask-append-topic | |
e66ba1df | 78 | ;; org-ctags-fail-silently)) ; <-- prevents org default behaviour |
45d6be0a SM |
79 | ;; |
80 | ;; | |
81 | ;; Usage | |
82 | ;; ===== | |
83 | ;; | |
84 | ;; When you click on a link "[[foo]]" and org cannot find a matching "<<foo>>" | |
8223b1d2 | 85 | ;; in the current buffer, the tags facility will take over. The file TAGS in |
45d6be0a | 86 | ;; the active directory is examined to see if the tags facility knows about |
8223b1d2 | 87 | ;; "<<foo>>" in any other files. If it does, the matching file will be opened |
45d6be0a SM |
88 | ;; and the cursor will jump to the position of "<<foo>>" in that file. |
89 | ;; | |
90 | ;; User-visible functions: | |
91 | ;; - `org-ctags-find-tag-interactive': type a tag (plain link) name and visit | |
8223b1d2 BG |
92 | ;; it. With autocompletion. Bound to ctrl-O in the above setup. |
93 | ;; - All the etags functions should work. These include: | |
45d6be0a SM |
94 | ;; |
95 | ;; M-. `find-tag' -- finds the tag at point | |
96 | ;; | |
97 | ;; C-M-. find-tag based on regular expression | |
98 | ;; | |
99 | ;; M-x tags-search RET -- like C-M-. but searches through ENTIRE TEXT | |
8223b1d2 | 100 | ;; of ALL the files referenced in the TAGS file. A quick way to |
45d6be0a SM |
101 | ;; search through an entire 'project'. |
102 | ;; | |
8223b1d2 | 103 | ;; M-* "go back" from a tag jump. Like `org-mark-ring-goto'. |
45d6be0a SM |
104 | ;; You may need to bind this key yourself with (eg) |
105 | ;; (global-set-key (kbd "<M-kp-multiply>") 'pop-tag-mark) | |
106 | ;; | |
107 | ;; (see etags chapter in Emacs manual for more) | |
108 | ;; | |
109 | ;; | |
110 | ;; Keeping the TAGS file up to date | |
111 | ;; ================================ | |
112 | ;; | |
113 | ;; Tags mode has no way of knowing that you have created new tags by typing in | |
114 | ;; your org-mode buffer. New tags make it into the TAGS file in 3 ways: | |
115 | ;; | |
116 | ;; 1. You re-run (org-ctags-create-tags "directory") to rebuild the file. | |
117 | ;; 2. You put the function `org-ctags-ask-rebuild-tags-file-then-find-tag' in | |
118 | ;; your `org-open-link-functions' list, as is done in the setup | |
8223b1d2 BG |
119 | ;; above. This will cause the TAGS file to be rebuilt whenever a link |
120 | ;; cannot be found. This may be slow with large file collections however. | |
45d6be0a SM |
121 | ;; 3. You run the following from the command line (all 1 line): |
122 | ;; | |
123 | ;; ctags --langdef=orgmode --langmap=orgmode:.org | |
124 | ;; --regex-orgmode="/<<([^>]+)>>/\1/d,definition/" | |
125 | ;; -f /your/path/TAGS -e -R /your/path/*.org | |
126 | ;; | |
127 | ;; If you are paranoid, you might want to run (org-ctags-create-tags | |
128 | ;; "/path/to/org/files") at startup, by including the following toplevel form | |
8223b1d2 | 129 | ;; in .emacs. However this can cause a pause of several seconds if ctags has |
45d6be0a SM |
130 | ;; to scan lots of files. |
131 | ;; | |
132 | ;; (progn | |
133 | ;; (message "-- rebuilding tags tables...") | |
134 | ;; (mapc 'org-create-tags tags-table-list)) | |
ed21c5c8 | 135 | |
86fbb8ca CD |
136 | ;;; Code: |
137 | ||
ed21c5c8 | 138 | (eval-when-compile (require 'cl)) |
86fbb8ca | 139 | |
ed21c5c8 CD |
140 | (require 'org) |
141 | ||
e66ba1df BG |
142 | (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label)) |
143 | ||
ed21c5c8 CD |
144 | (defgroup org-ctags nil |
145 | "Options concerning use of ctags within org mode." | |
146 | :tag "Org-Ctags" | |
147 | :group 'org-link) | |
148 | ||
149 | (defvar org-ctags-enabled-p t | |
150 | "Activate ctags support in org mode?") | |
151 | ||
152 | (defvar org-ctags-tag-regexp "/<<([^>]+)>>/\\1/d,definition/" | |
86fbb8ca CD |
153 | "Regexp expression used by ctags external program. |
154 | The regexp matches tag destinations in org-mode files. | |
ed21c5c8 CD |
155 | Format is: /REGEXP/TAGNAME/FLAGS,TAGTYPE/ |
156 | See the ctags documentation for more information.") | |
157 | ||
158 | (defcustom org-ctags-path-to-ctags | |
159 | (case system-type | |
160 | (windows-nt "ctags.exe") | |
161 | (darwin "ctags-exuberant") | |
162 | (t "ctags-exuberant")) | |
163 | "Full path to the ctags executable file." | |
164 | :group 'org-ctags | |
372d7b21 | 165 | :version "24.1" |
ed21c5c8 CD |
166 | :type 'file) |
167 | ||
168 | (defcustom org-ctags-open-link-functions | |
169 | '(org-ctags-find-tag | |
170 | org-ctags-ask-rebuild-tags-file-then-find-tag | |
171 | org-ctags-ask-append-topic) | |
86fbb8ca | 172 | "List of functions to be prepended to ORG-OPEN-LINK-FUNCTIONS when ORG-CTAGS is active." |
ed21c5c8 | 173 | :group 'org-ctags |
372d7b21 | 174 | :version "24.1" |
ed21c5c8 CD |
175 | :type 'hook |
176 | :options '(org-ctags-find-tag | |
177 | org-ctags-ask-rebuild-tags-file-then-find-tag | |
178 | org-ctags-rebuild-tags-file-then-find-tag | |
179 | org-ctags-ask-append-topic | |
180 | org-ctags-append-topic | |
181 | org-ctags-ask-visit-buffer-or-file | |
182 | org-ctags-visit-buffer-or-file | |
183 | org-ctags-fail-silently)) | |
184 | ||
185 | ||
186 | (defvar org-ctags-tag-list nil | |
86fbb8ca CD |
187 | "List of all tags in the active TAGS file. |
188 | Created as a local variable in each buffer.") | |
ed21c5c8 CD |
189 | |
190 | (defcustom org-ctags-new-topic-template | |
0cd38783 | 191 | "* <<%t>>\n\n\n\n\n\n" |
ed21c5c8 CD |
192 | "Text to insert when creating a new org file via opening a hyperlink. |
193 | The following patterns are replaced in the string: | |
194 | `%t' - replaced with the capitalized title of the hyperlink" | |
195 | :group 'org-ctags | |
8223b1d2 | 196 | :version "24.1" |
ed21c5c8 CD |
197 | :type 'string) |
198 | ||
199 | ||
200 | (add-hook 'org-mode-hook | |
201 | (lambda () | |
202 | (when (and org-ctags-enabled-p | |
203 | (buffer-file-name)) | |
204 | ;; Make sure this file's directory is added to default | |
205 | ;; directories in which to search for tags. | |
206 | (let ((tags-filename | |
207 | (expand-file-name | |
208 | (concat (file-name-directory (buffer-file-name)) | |
209 | "/TAGS")))) | |
210 | (when (file-exists-p tags-filename) | |
211 | (visit-tags-table tags-filename)))))) | |
212 | ||
213 | ||
214 | (defadvice visit-tags-table (after org-ctags-load-tag-list activate compile) | |
215 | (when (and org-ctags-enabled-p tags-file-name) | |
216 | (set (make-local-variable 'org-ctags-tag-list) | |
217 | (org-ctags-all-tags-in-current-tags-table)))) | |
218 | ||
219 | ||
220 | (defun org-ctags-enable () | |
221 | (put 'org-mode 'find-tag-default-function 'org-ctags-find-tag-at-point) | |
222 | (setq org-ctags-enabled-p t) | |
223 | (dolist (fn org-ctags-open-link-functions) | |
224 | (add-hook 'org-open-link-functions fn t))) | |
225 | ||
226 | ||
86fbb8ca | 227 | ;;; General utility functions. =============================================== |
45d6be0a | 228 | ;; These work outside org-ctags mode. |
ed21c5c8 CD |
229 | |
230 | (defun org-ctags-get-filename-for-tag (tag) | |
86fbb8ca CD |
231 | "TAG is a string. Search the active TAGS file for a matching tag. |
232 | If the tag is found, return a list containing the filename, line number, and | |
ed21c5c8 CD |
233 | buffer position where the tag is found." |
234 | (interactive "sTag: ") | |
235 | (unless tags-file-name | |
236 | (call-interactively (visit-tags-table))) | |
237 | (save-excursion | |
238 | (visit-tags-table-buffer 'same) | |
239 | (when tags-file-name | |
240 | (with-current-buffer (get-file-buffer tags-file-name) | |
241 | (goto-char (point-min)) | |
242 | (cond | |
243 | ((re-search-forward (format "^.*\7f%s\ 1\\([0-9]+\\),\\([0-9]+\\)$" | |
244 | (regexp-quote tag)) nil t) | |
245 | (let ((line (string-to-number (match-string 1))) | |
246 | (pos (string-to-number (match-string 2)))) | |
247 | (cond | |
248 | ((re-search-backward "\f\n\\(.*\\),[0-9]+\n") | |
249 | (list (match-string 1) line pos)) | |
250 | (t ; can't find a file name preceding the matched | |
8223b1d2 | 251 | ; tag?? |
ed21c5c8 CD |
252 | (error "Malformed TAGS file: %s" (buffer-name)))))) |
253 | (t ; tag not found | |
254 | nil)))))) | |
255 | ||
256 | ||
257 | (defun org-ctags-all-tags-in-current-tags-table () | |
258 | "Read all tags defined in the active TAGS file, into a list of strings. | |
259 | Return the list." | |
260 | (interactive) | |
261 | (let ((taglist nil)) | |
262 | (unless tags-file-name | |
263 | (call-interactively (visit-tags-table))) | |
264 | (save-excursion | |
265 | (visit-tags-table-buffer 'same) | |
266 | (with-current-buffer (get-file-buffer tags-file-name) | |
267 | (goto-char (point-min)) | |
268 | (while (re-search-forward "^.*\7f\\(.*\\)\ 1\\([0-9]+\\),\\([0-9]+\\)$" | |
269 | nil t) | |
270 | (push (substring-no-properties (match-string 1)) taglist))) | |
271 | taglist))) | |
272 | ||
273 | ||
274 | (defun org-ctags-string-search-and-replace (search replace string) | |
275 | "Replace all instances of SEARCH with REPLACE in STRING." | |
276 | (replace-regexp-in-string (regexp-quote search) replace string t t)) | |
277 | ||
278 | ||
279 | (defun y-or-n-minibuffer (prompt) | |
280 | (let ((use-dialog-box nil)) | |
281 | (y-or-n-p prompt))) | |
282 | ||
283 | ||
284 | ;;; Internal functions ======================================================= | |
285 | ||
286 | ||
287 | (defun org-ctags-open-file (name &optional title) | |
86fbb8ca CD |
288 | "Visit or create a file called `NAME.org', and insert a new topic. |
289 | The new topic will be titled NAME (or TITLE if supplied)." | |
ed21c5c8 CD |
290 | (interactive "sFile name: ") |
291 | (let ((filename (substitute-in-file-name (expand-file-name name)))) | |
292 | (condition-case v | |
293 | (progn | |
294 | (org-open-file name t) | |
295 | (message "Opened file OK") | |
296 | (goto-char (point-max)) | |
297 | (insert (org-ctags-string-search-and-replace | |
298 | "%t" (capitalize (or title name)) | |
299 | org-ctags-new-topic-template)) | |
300 | (message "Inserted new file text OK") | |
301 | (org-mode-restart)) | |
302 | (error (error "Error %S in org-ctags-open-file" v))))) | |
303 | ||
304 | ||
305 | ;;;; Misc interoperability with etags system ================================= | |
306 | ||
307 | ||
308 | (defadvice find-tag (before org-ctags-set-org-mark-before-finding-tag | |
309 | activate compile) | |
310 | "Before trying to find a tag, save our current position on org mark ring." | |
311 | (save-excursion | |
8223b1d2 | 312 | (if (and (derived-mode-p 'org-mode) org-ctags-enabled-p) |
ed21c5c8 CD |
313 | (org-mark-ring-push)))) |
314 | ||
315 | ||
316 | ||
317 | (defun org-ctags-find-tag-at-point () | |
318 | "Determine default tag to search for, based on text at point. | |
319 | If there is no plausible default, return nil." | |
320 | (let (from to bound) | |
321 | (when (or (ignore-errors | |
322 | ;; Look for hyperlink around `point'. | |
323 | (save-excursion | |
324 | (search-backward "[[") (setq from (+ 2 (point)))) | |
325 | (save-excursion | |
326 | (goto-char from) | |
327 | (search-forward "]") (setq to (- (point) 1))) | |
328 | (and (> to from) (>= (point) from) (<= (point) to))) | |
329 | (progn | |
330 | ;; Look at text around `point'. | |
331 | (save-excursion | |
332 | (skip-syntax-backward "w_") (setq from (point))) | |
333 | (save-excursion | |
334 | (skip-syntax-forward "w_") (setq to (point))) | |
335 | (> to from)) | |
336 | ;; Look between `line-beginning-position' and `point'. | |
337 | (save-excursion | |
338 | (and (setq bound (line-beginning-position)) | |
339 | (skip-syntax-backward "^w_" bound) | |
340 | (> (setq to (point)) bound) | |
341 | (skip-syntax-backward "w_") | |
342 | (setq from (point)))) | |
343 | ;; Look between `point' and `line-end-position'. | |
344 | (save-excursion | |
345 | (and (setq bound (line-end-position)) | |
346 | (skip-syntax-forward "^w_" bound) | |
347 | (< (setq from (point)) bound) | |
348 | (skip-syntax-forward "w_") | |
349 | (setq to (point))))) | |
350 | (buffer-substring-no-properties from to)))) | |
351 | ||
352 | ||
353 | ;;; Functions for use with 'org-open-link-functions' hook ================= | |
354 | ||
355 | ||
356 | (defun org-ctags-find-tag (name) | |
357 | "This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS. | |
86fbb8ca | 358 | Look for a tag called `NAME' in the current TAGS table. If it is found, |
ed21c5c8 CD |
359 | visit the file and location where the tag is found." |
360 | (interactive "sTag: ") | |
361 | (let ((old-buf (current-buffer)) | |
362 | (old-pnt (point-marker)) | |
363 | (old-mark (copy-marker (mark-marker)))) | |
364 | (condition-case nil | |
365 | (progn (find-tag name) | |
366 | t) | |
367 | (error | |
368 | ;; only restore old location if find-tag raises error | |
369 | (set-buffer old-buf) | |
370 | (goto-char old-pnt) | |
371 | (set-marker (mark-marker) old-mark) | |
372 | nil)))) | |
373 | ||
374 | ||
375 | (defun org-ctags-visit-buffer-or-file (name &optional create) | |
376 | "This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS. | |
86fbb8ca CD |
377 | Visit buffer named `NAME.org'. If there is no such buffer, visit the file |
378 | with the same name if it exists. If the file does not exist, then behavior | |
ed21c5c8 CD |
379 | depends on the value of CREATE. |
380 | ||
86fbb8ca | 381 | If CREATE is nil (default), then return nil. Do not create a new file. |
ed21c5c8 CD |
382 | If CREATE is t, create the new file and visit it. |
383 | If CREATE is the symbol `ask', then ask the user if they wish to create | |
384 | the new file." | |
385 | (interactive) | |
386 | (let ((filename (concat (substitute-in-file-name | |
387 | (expand-file-name name)) | |
388 | ".org"))) | |
389 | (cond | |
390 | ((get-buffer (concat name ".org")) | |
391 | ;; Buffer is already open | |
e66ba1df | 392 | (org-pop-to-buffer-same-window (get-buffer (concat name ".org")))) |
ed21c5c8 CD |
393 | ((file-exists-p filename) |
394 | ;; File exists but is not open --> open it | |
395 | (message "Opening existing org file `%S'..." | |
396 | filename) | |
397 | (org-open-file filename t)) | |
398 | ((or (eql create t) | |
399 | (and (eql create 'ask) | |
400 | (y-or-n-p (format "File `%s.org' not found; create?" name)))) | |
401 | (org-ctags-open-file filename name)) | |
402 | (t ;; File does not exist, and we don't want to create it. | |
403 | nil)))) | |
404 | ||
405 | ||
406 | (defun org-ctags-ask-visit-buffer-or-file (name) | |
407 | "This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS. | |
408 | Wrapper for org-ctags-visit-buffer-or-file, which ensures the user is | |
409 | asked before creating a new file." | |
410 | (org-ctags-visit-buffer-or-file name 'ask)) | |
411 | ||
412 | ||
413 | (defun org-ctags-append-topic (name &optional narrowp) | |
414 | "This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS. | |
8223b1d2 | 415 | Append a new toplevel heading to the end of the current buffer. The |
ed21c5c8 CD |
416 | heading contains NAME surrounded by <<angular brackets>>, thus making |
417 | the heading a destination for the tag `NAME'." | |
418 | (interactive "sTopic: ") | |
419 | (widen) | |
420 | (goto-char (point-max)) | |
421 | (newline 2) | |
422 | (message "Adding topic in buffer %s" (buffer-name)) | |
423 | (insert (org-ctags-string-search-and-replace | |
424 | "%t" (capitalize name) org-ctags-new-topic-template)) | |
425 | (backward-char 4) | |
426 | (org-update-radio-target-regexp) | |
427 | (end-of-line) | |
428 | (forward-line 2) | |
429 | (when narrowp | |
430 | ;;(org-tree-to-indirect-buffer 1) ;; opens new frame | |
431 | (org-narrow-to-subtree)) | |
432 | t) | |
433 | ||
434 | ||
435 | (defun org-ctags-ask-append-topic (name &optional narrowp) | |
436 | "This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS. | |
437 | Wrapper for org-ctags-append-topic, which first asks the user if they want | |
438 | to append a new topic." | |
439 | (if (y-or-n-p (format "Topic `%s' not found; append to end of buffer?" | |
440 | name)) | |
441 | (org-ctags-append-topic name narrowp) | |
442 | nil)) | |
443 | ||
444 | ||
445 | (defun org-ctags-rebuild-tags-file-then-find-tag (name) | |
446 | "This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS. | |
447 | Like ORG-CTAGS-FIND-TAG, but calls the external ctags program first, | |
448 | to rebuild (update) the TAGS file." | |
449 | (unless tags-file-name | |
450 | (call-interactively (visit-tags-table))) | |
451 | (when (buffer-file-name) | |
452 | (org-ctags-create-tags)) | |
453 | (org-ctags-find-tag name)) | |
454 | ||
455 | ||
456 | (defun org-ctags-ask-rebuild-tags-file-then-find-tag (name) | |
457 | "This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS. | |
458 | Wrapper for org-ctags-rebuild-tags-file-then-find-tag." | |
459 | (if (and (buffer-file-name) | |
8223b1d2 BG |
460 | (y-or-n-p |
461 | (format | |
462 | "Tag `%s' not found. Rebuild table `%s/TAGS' and look again?" | |
463 | name | |
464 | (file-name-directory (buffer-file-name))))) | |
465 | (org-ctags-rebuild-tags-file-then-find-tag name) | |
ed21c5c8 CD |
466 | nil)) |
467 | ||
468 | ||
469 | (defun org-ctags-fail-silently (name) | |
470 | "This function is intended to be used in ORG-OPEN-LINK-FUNCTIONS. | |
471 | Put as the last function in the list if you want to prevent org's default | |
86fbb8ca | 472 | behavior of free text search." |
ed21c5c8 CD |
473 | t) |
474 | ||
475 | ||
476 | ;;; User-visible functions =================================================== | |
477 | ||
478 | ||
479 | (defun org-ctags-create-tags (&optional directory-name) | |
86fbb8ca CD |
480 | "(Re)create tags file in the directory of the active buffer. |
481 | The file will contain tag definitions for all the files in the | |
482 | directory and its subdirectories which are recognized by ctags. | |
483 | This will include files ending in `.org' as well as most other | |
484 | source files (.C, .H, .EL, .LISP, etc). All the resulting tags | |
485 | end up in one file, called TAGS, located in the directory. This | |
486 | function may take several seconds to finish if the directory or | |
487 | its subdirectories contain large numbers of taggable files." | |
ed21c5c8 CD |
488 | (interactive) |
489 | (assert (buffer-file-name)) | |
490 | (let ((dir-name (or directory-name | |
491 | (file-name-directory (buffer-file-name)))) | |
492 | (exitcode nil)) | |
493 | (save-excursion | |
494 | (setq exitcode | |
495 | (shell-command | |
496 | (format (concat "%s --langdef=orgmode --langmap=orgmode:.org " | |
497 | "--regex-orgmode=\"%s\" -f \"%s\" -e -R \"%s\"") | |
498 | org-ctags-path-to-ctags | |
499 | org-ctags-tag-regexp | |
500 | (expand-file-name (concat dir-name "/TAGS")) | |
501 | (expand-file-name (concat dir-name "/*"))))) | |
502 | (cond | |
503 | ((eql 0 exitcode) | |
504 | (set (make-local-variable 'org-ctags-tag-list) | |
505 | (org-ctags-all-tags-in-current-tags-table))) | |
506 | (t | |
507 | ;; This seems to behave differently on Linux, so just ignore | |
508 | ;; error codes for now | |
509 | ;;(error "Calling ctags executable resulted in error code: %s" | |
510 | ;; exitcode) | |
511 | nil))))) | |
512 | ||
513 | ||
514 | (defvar org-ctags-find-tag-history nil | |
515 | "History of tags visited by org-ctags-find-tag-interactive.") | |
516 | ||
517 | (defun org-ctags-find-tag-interactive () | |
86fbb8ca CD |
518 | "Prompt for the name of a tag, with autocompletion, then visit the named tag. |
519 | Uses `ido-mode' if available. | |
ed21c5c8 CD |
520 | If the user enters a string that does not match an existing tag, create |
521 | a new topic." | |
522 | (interactive) | |
523 | (let* ((completing-read-fn (if (fboundp 'ido-completing-read) | |
524 | 'ido-completing-read | |
525 | 'completing-read)) | |
526 | (tag (funcall completing-read-fn "Topic: " org-ctags-tag-list | |
527 | nil 'confirm nil 'org-ctags-find-tag-history))) | |
528 | (when tag | |
529 | (cond | |
530 | ((member tag org-ctags-tag-list) | |
531 | ;; Existing tag | |
532 | (push tag org-ctags-find-tag-history) | |
533 | (find-tag tag)) | |
534 | (t | |
535 | ;; New tag | |
536 | (run-hook-with-args-until-success | |
8223b1d2 | 537 | 'org-open-link-functions tag)))))) |
ed21c5c8 CD |
538 | |
539 | ||
540 | (org-ctags-enable) | |
541 | ||
542 | (provide 'org-ctags) | |
543 | ||
ed21c5c8 | 544 | ;;; org-ctags.el ends here |