Merge branch 'master' of orgmode.org:org-mode
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-pcomplete.el
blob71e0ee0dfab1f051d8db42ffafcd9e9c5a47a722
1 ;;; org-pcomplete.el --- In-buffer completion code
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
5 ;;
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; John Wiegley <johnw at gnu dot org>
8 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; Homepage: http://orgmode.org
10 ;; Version: 7.4
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;; Code:
30 ;;;; Require other packages
32 (eval-when-compile
33 (require 'cl))
35 (require 'org-macs)
36 (require 'pcomplete)
38 (declare-function org-split-string "org" (string &optional separators))
39 (declare-function org-get-current-options "org-exp" ())
40 (declare-function org-make-org-heading-search-string "org"
41 (&optional string heading))
42 (declare-function org-get-buffer-tags "org" ())
43 (declare-function org-get-tags "org" ())
44 (declare-function org-buffer-property-keys "org"
45 (&optional include-specials include-defaults include-columns))
46 (declare-function org-entry-properties "org" (&optional pom which specific))
48 ;;;; Customization variables
50 (defgroup org-complete nil
51 "Outline-based notes management and organizer."
52 :tag "Org"
53 :group 'org)
55 (defun org-thing-at-point ()
56 "Examine the thing at point and let the caller know what it is.
57 The return value is a string naming the thing at point."
58 (let ((beg1 (save-excursion
59 (skip-chars-backward (org-re "[:alnum:]_@"))
60 (point)))
61 (beg (save-excursion
62 (skip-chars-backward "a-zA-Z0-9_:$")
63 (point)))
64 (line-to-here (buffer-substring (point-at-bol) (point))))
65 (cond
66 ((string-match "\\`[ \t]*#\\+begin: clocktable[ \t]+" line-to-here)
67 (cons "block-option" "clocktable"))
68 ((string-match "\\`[ \t]*#\\+begin_src[ \t]+" line-to-here)
69 (cons "block-option" "src"))
70 ((save-excursion
71 (re-search-backward "^[ \t]*#\\+\\([A-Z_]+\\):.*"
72 (line-beginning-position) t))
73 (cons "file-option" (match-string-no-properties 1)))
74 ((string-match "\\`[ \t]*#\\+[a-zA-Z]*\\'" line-to-here)
75 (cons "file-option" nil))
76 ((equal (char-before beg) ?\[)
77 (cons "link" nil))
78 ((equal (char-before beg) ?\\)
79 (cons "tex" nil))
80 ((string-match "\\`\\*+[ \t]+\\'"
81 (buffer-substring (point-at-bol) beg))
82 (cons "todo" nil))
83 ((equal (char-before beg) ?*)
84 (cons "searchhead" nil))
85 ((and (equal (char-before beg1) ?:)
86 (equal (char-after (point-at-bol)) ?*))
87 (cons "tag" nil))
88 ((and (equal (char-before beg1) ?:)
89 (not (equal (char-after (point-at-bol)) ?*)))
90 (cons "prop" nil))
91 (t nil))))
93 (defun org-command-at-point ()
94 "Return the qualified name of the Org completion entity at point.
95 When completing for #+STARTUP, for example, this function returns
96 \"file-option/startup\"."
97 (let ((thing (org-thing-at-point)))
98 (cond
99 ((string= "file-option" (car thing))
100 (concat (car thing) "/" (downcase (cdr thing))))
101 ((string= "block-option" (car thing))
102 (concat (car thing) "/" (downcase (cdr thing))))
104 (car thing)))))
106 (defun org-parse-arguments ()
107 "Parse whitespace separated arguments in the current region."
108 (let ((begin (line-beginning-position))
109 (end (line-end-position))
110 begins args)
111 (save-restriction
112 (narrow-to-region begin end)
113 (save-excursion
114 (goto-char (point-min))
115 (while (not (eobp))
116 (skip-chars-forward " \t\n[")
117 (setq begins (cons (point) begins))
118 (skip-chars-forward "^ \t\n[")
119 (setq args (cons (buffer-substring-no-properties
120 (car begins) (point))
121 args)))
122 (cons (reverse args) (reverse begins))))))
125 (defun org-pcomplete-initial ()
126 "Calls the right completion function for first argument completions."
127 (ignore
128 (funcall (or (pcomplete-find-completion-function
129 (car (org-thing-at-point)))
130 pcomplete-default-completion-function))))
132 (defvar org-additional-option-like-keywords)
133 (defun pcomplete/org-mode/file-option ()
134 "Complete against all valid file options."
135 (require 'org-exp)
136 (pcomplete-here
137 (org-pcomplete-case-double
138 (mapcar (lambda (x)
139 (if (= ?: (aref x (1- (length x))))
140 (concat x " ")
142 (delq nil
143 (pcomplete-uniqify-list
144 (append
145 (mapcar (lambda (x)
146 (if (string-match "^#\\+\\([A-Z_]+:?\\)" x)
147 (match-string 1 x)))
148 (org-split-string (org-get-current-options) "\n"))
149 org-additional-option-like-keywords)))))
150 (substring pcomplete-stub 2)))
152 (defvar org-startup-options)
153 (defun pcomplete/org-mode/file-option/startup ()
154 "Complete arguments for the #+STARTUP file option."
155 (while (pcomplete-here
156 (let ((opts (pcomplete-uniqify-list
157 (mapcar 'car org-startup-options))))
158 ;; Some options are mutually exclusive, and shouldn't be completed
159 ;; against if certain other options have already been seen.
160 (dolist (arg pcomplete-args)
161 (cond
162 ((string= arg "hidestars")
163 (setq opts (delete "showstars" opts)))))
164 opts))))
166 (defun pcomplete/org-mode/file-option/bind ()
167 "Complete arguments for the #+BIND file option, which are variable names"
168 (let (vars)
169 (mapatoms
170 (lambda (a) (if (boundp a) (setq vars (cons (symbol-name a) vars)))))
171 (pcomplete-here vars)))
173 (defvar org-link-abbrev-alist-local)
174 (defvar org-link-abbrev-alist)
175 (defun pcomplete/org-mode/link ()
176 "Complete against defined #+LINK patterns."
177 (pcomplete-here
178 (pcomplete-uniqify-list
179 (copy-sequence
180 (append (mapcar 'car org-link-abbrev-alist-local)
181 (mapcar 'car org-link-abbrev-alist))))))
183 (defvar org-entities)
184 (defun pcomplete/org-mode/tex ()
185 "Complete against TeX-style HTML entity names."
186 (require 'org-entities)
187 (while (pcomplete-here
188 (pcomplete-uniqify-list (remove nil (mapcar 'car-safe org-entities)))
189 (substring pcomplete-stub 1))))
191 (defvar org-todo-keywords-1)
192 (defun pcomplete/org-mode/todo ()
193 "Complete against known TODO keywords."
194 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1))))
196 (defvar org-todo-line-regexp)
197 (defun pcomplete/org-mode/searchhead ()
198 "Complete against all headings.
199 This needs more work, to handle headings with lots of spaces in them."
200 (while
201 (pcomplete-here
202 (save-excursion
203 (goto-char (point-min))
204 (let (tbl)
205 (while (re-search-forward org-todo-line-regexp nil t)
206 (push (org-make-org-heading-search-string
207 (match-string-no-properties 3) t)
208 tbl))
209 (pcomplete-uniqify-list tbl)))
210 (substring pcomplete-stub 1))))
212 (defvar org-tag-alist)
213 (defun pcomplete/org-mode/tag ()
214 "Complete a tag name. Omit tags already set."
215 (while (pcomplete-here
216 (mapcar (lambda (x)
217 (concat x ":"))
218 (let ((lst (pcomplete-uniqify-list
219 (or (remove
221 (mapcar (lambda (x)
222 (and (stringp (car x)) (car x)))
223 org-tag-alist))
224 (mapcar 'car (org-get-buffer-tags))))))
225 (dolist (tag (org-get-tags))
226 (setq lst (delete tag lst)))
227 lst))
228 (and (string-match ".*:" pcomplete-stub)
229 (substring pcomplete-stub (match-end 0))))))
231 (defun pcomplete/org-mode/prop ()
232 "Complete a property name. Omit properties already set."
233 (pcomplete-here
234 (mapcar (lambda (x)
235 (concat x ": "))
236 (let ((lst (pcomplete-uniqify-list
237 (copy-sequence
238 (org-buffer-property-keys nil t t)))))
239 (dolist (prop (org-entry-properties))
240 (setq lst (delete (car prop) lst)))
241 lst))
242 (substring pcomplete-stub 1)))
244 (defun pcomplete/org-mode/block-option/src ()
245 "Complete the arguments of a begin_src block.
246 Complete a language in the first field, the header arguments and switches."
247 (pcomplete-here
248 (mapcar
249 (lambda(x) (symbol-name (nth 3 x)))
250 (cdr (car (cdr (memq :key-type (plist-get
251 (symbol-plist
252 'org-babel-load-languages)
253 'custom-type)))))))
254 (while (pcomplete-here
255 '("-n" "-r" "-l"
256 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
257 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
258 ":session" ":shebang" ":tangle" ":var"))))
260 (defun pcomplete/org-mode/block-option/clocktable ()
261 "Complete keywords in a clocktable line"
262 (while (pcomplete-here '(":maxlevel" ":scope"
263 ":tstart" ":tend" ":block" ":step"
264 ":stepskip0" ":fileskip0"
265 ":emphasize" ":link" ":narrow" ":indent"
266 ":tcolumns" ":level" ":compact" ":timestamp"
267 ":formula" ":formatter"))))
269 (defun org-pcomplete-case-double (list)
270 "Return list with both upcase and downcase version of all strings in LIST."
271 (let (e res)
272 (while (setq e (pop list))
273 (setq res (cons (downcase e) (cons (upcase e) res))))
274 (nreverse res)))
276 ;;;; Finish up
278 (provide 'org-pcomplete)
280 ;; arch-tag:
282 ;;; org-pcomplete.el ends here