org-e-publish: Move publishing functions into back-end libraries
[org-mode.git] / contrib / lisp / org-elisp-symbol.el
blob96b0e5d126bb93282e048949ee402052344395a4
1 ;;; org-elisp-symbol.el --- Org links to emacs-lisp symbols
2 ;;
3 ;; Copyright 2007-2012 Free Software Foundation, Inc.
4 ;;
5 ;; Author: bzg AT gnu DOT org
6 ;; Version: 0.2
7 ;; Keywords: org, remember, lisp
8 ;; URL: http://www.cognition.ens.fr/~guerry/u/org-elisp-symbol.el
9 ;;
10 ;; This file is not part of GNU Emacs.
12 ;; This program 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, or (at your option)
15 ;; any later version.
17 ;; This program 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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program; if not, write to the Free Software
24 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;;; Commentary:
28 ;; Org-mode already lets you store/insert links to emacs-lisp files,
29 ;; just like any other file. This package lets you precisely link to
30 ;; any emacs-lisp symbol and access useful information about the symbol.
32 ;; Here is the list of available properties when linking from a elisp-symbol:
34 ;; :name The symbol's name.
35 ;; :stype The symbol's type (commandp, function, etc.)
36 ;; :def The function used to set the symbol's value (defun, etc.)
37 ;; :keys The keys associated with the command.
38 ;; :args The arguments of the function.
39 ;; :docstring The docstring of the symbol.
40 ;; :doc The first line of the dostring.
41 ;; :comment A comment line just above the sexp, if any.
42 ;; :fixme A FIXME comment line just above the sexp, if any.
44 ;; Let's say we have a defun like this one:
46 ;; ;; FIXME update docstring
47 ;; (defun org-export-latex-lists ()
48 ;; "Convert lists to LaTeX."
49 ;; (goto-char (point-min))
50 ;; (while (re-search-forward org-export-latex-list-beginning-re nil t)
51 ;; (beginning-of-line)
52 ;; (insert (org-list-to-latex (org-list-parse-list t)) "\n")))
54 ;; And a remember template like:
56 ;; (setq org-remember-templates
57 ;; '((?s "* DEBUG `%:name' (%:args)\n\n%?\n\nFixme: %:fixme\n \
58 ;; Doc: \"%:doc\"\n\n%a")))
60 ;; Then M-x `org-remember' on this sexp will produce this buffer:
62 ;; =====================================================================
63 ;; * DEBUG `org-export-latex-lists' ()
65 ;; <== point
67 ;; Fixme: update the docstring
68 ;; Doc: "Convert lists to LaTeX."
70 ;; [[file:~/path/file.el::defun%20my-func][Function: my-func]]
71 ;; =====================================================================
73 ;; Put this file into your load-path and the following into your ~/.emacs:
74 ;; (require 'org-elisp-symbol)
76 ;;; Code:
78 (provide 'org-elisp-symbol)
80 (require 'org)
82 (org-add-link-type "elisp-symbol" 'org-elisp-symbol-open)
83 (add-hook 'org-store-link-functions 'org-elisp-symbol-store-link)
85 (defun org-elisp-symbol-open (path)
86 "Visit the emacs-lisp elisp-symbol at PATH."
87 (let* ((search (when (string-match "::\\(.+\\)\\'" path)
88 (match-string 1 path)))
89 (path (substring path 0 (match-beginning 0))))
90 (org-open-file path t nil search)))
92 (defun org-elisp-symbol-store-link ()
93 "Store a link to an emacs-lisp elisp-symbol."
94 (when (eq major-mode 'emacs-lisp-mode)
95 (save-excursion
96 (or (looking-at "^(") (beginning-of-defun))
97 (looking-at "^(\\([a-z]+\\) \\([^)\n ]+\\) ?\n?[ \t]*\\(?:(\\(.*\\))\\)?")
98 (let* ((end (save-excursion
99 (save-match-data
100 (end-of-defun) (point))))
101 (def (match-string 1))
102 (name (match-string 2))
103 (sym-name (intern-soft name))
104 (stype (cond ((commandp sym-name) "Command")
105 ((functionp sym-name) "Function")
106 ((user-variable-p sym-name) "User variable")
107 ((string= def "defvar") "Variable")
108 ((string= def "defmacro") "Macro")
109 ((string= def "defun") "Function or command")
110 (t "Symbol")))
111 (args (if (match-string 3)
112 (mapconcat (lambda (a) (unless (string-match "^&" a) a))
113 (split-string (match-string 3)) " ")
114 "no arg"))
115 (docstring (cond ((functionp sym-name)
116 (or (documentation sym-name)
117 "[no documentation]"))
118 ((string-match "[Vv]ariable" stype)
119 (documentation-property sym-name
120 'variable-documentation))
121 (t "no documentation")))
122 (doc (and (string-match "^\\([^\n]+\\)$" docstring)
123 (match-string 1 docstring)))
124 (fixme (save-excursion
125 (beginning-of-defun) (end-of-defun)
126 (if (re-search-forward "^;+ ?FIXME[ :]*\\(.*\\)$" end t)
127 (match-string 1) "nothing to fix")))
128 (comment (save-excursion
129 (beginning-of-defun) (end-of-defun)
130 (if (re-search-forward "^;;+ ?\\(.*\\)$" end t)
131 (match-string 1) "no comment")))
132 keys keys-desc link description)
133 (if (equal stype "Command")
134 (setq keys (where-is-internal sym-name)
135 keys-desc
136 (if keys (mapconcat 'key-description keys " ") "none")))
137 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
138 "::" def " " name))
139 (setq description (concat stype ": " name))
140 (org-store-link-props
141 :type "elisp-symbol"
142 :link link
143 :description description
144 :def def
145 :name name
146 :stype stype
147 :args args
148 :keys keys-desc
149 :docstring docstring
150 :doc doc
151 :fixme fixme
152 :comment comment)))))
154 (provide 'org-elisp-symbol)
157 ;;;;##########################################################################
158 ;;;; User Options, Variables
159 ;;;;##########################################################################
161 ;;; org-elisp-symbol.el ends here