ox: Docstring fix
[org-mode.git] / lisp / org-pcomplete.el
blob0ae41aeb3ebbb3861951178da07c2f62e3531dac
1 ;;; org-pcomplete.el --- In-buffer completion code
3 ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; John Wiegley <johnw at gnu dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;;
10 ;; This file is part of GNU Emacs.
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.
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.
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Code:
28 ;;;; Require other packages
30 (eval-when-compile
31 (require 'cl))
33 (require 'org-macs)
34 (require 'org-compat)
35 (require 'pcomplete)
37 (declare-function org-split-string "org" (string &optional separators))
38 (declare-function org-make-org-heading-search-string "org"
39 (&optional string heading))
40 (declare-function org-get-buffer-tags "org" ())
41 (declare-function org-get-tags "org" ())
42 (declare-function org-buffer-property-keys "org"
43 (&optional include-specials include-defaults include-columns))
44 (declare-function org-entry-properties "org" (&optional pom which specific))
46 ;;;; Customization variables
48 (defgroup org-complete nil
49 "Outline-based notes management and organizer."
50 :tag "Org"
51 :group 'org)
53 (defvar org-drawer-regexp)
54 (defvar org-property-re)
56 (defun org-thing-at-point ()
57 "Examine the thing at point and let the caller know what it is.
58 The return value is a string naming the thing at point."
59 (let ((beg1 (save-excursion
60 (skip-chars-backward (org-re "[:alnum:]-_@"))
61 (point)))
62 (beg (save-excursion
63 (skip-chars-backward "a-zA-Z0-9-_:$")
64 (point)))
65 (line-to-here (buffer-substring (point-at-bol) (point))))
66 (cond
67 ((string-match "\\`[ \t]*#\\+begin: clocktable[ \t]+" line-to-here)
68 (cons "block-option" "clocktable"))
69 ((string-match "\\`[ \t]*#\\+begin_src[ \t]+" line-to-here)
70 (cons "block-option" "src"))
71 ((save-excursion
72 (re-search-backward "^[ \t]*#\\+\\([A-Z_]+\\):.*"
73 (line-beginning-position) t))
74 (cons "file-option" (match-string-no-properties 1)))
75 ((string-match "\\`[ \t]*#\\+[a-zA-Z_]*\\'" line-to-here)
76 (cons "file-option" nil))
77 ((equal (char-before beg) ?\[)
78 (cons "link" nil))
79 ((equal (char-before beg) ?\\)
80 (cons "tex" nil))
81 ((string-match "\\`\\*+[ \t]+\\'"
82 (buffer-substring (point-at-bol) beg))
83 (cons "todo" nil))
84 ((equal (char-before beg) ?*)
85 (cons "searchhead" nil))
86 ((and (equal (char-before beg1) ?:)
87 (equal (char-after (point-at-bol)) ?*))
88 (cons "tag" nil))
89 ((and (equal (char-before beg1) ?:)
90 (not (equal (char-after (point-at-bol)) ?*))
91 (save-excursion
92 (move-beginning-of-line 1)
93 (skip-chars-backward "[ \t\n]")
94 ;; org-drawer-regexp matches a whole line but while
95 ;; looking-back, we just ignore trailing whitespaces
96 (or (org-looking-back (substring org-drawer-regexp 0 -1))
97 (org-looking-back org-property-re))))
98 (cons "prop" nil))
99 ((and (equal (char-before beg1) ?:)
100 (not (equal (char-after (point-at-bol)) ?*)))
101 (cons "drawer" nil))
102 (t nil))))
104 (defun org-command-at-point ()
105 "Return the qualified name of the Org completion entity at point.
106 When completing for #+STARTUP, for example, this function returns
107 \"file-option/startup\"."
108 (let ((thing (org-thing-at-point)))
109 (cond
110 ((string= "file-option" (car thing))
111 (concat (car thing)
112 (and (cdr thing) (concat "/" (downcase (cdr thing))))))
113 ((string= "block-option" (car thing))
114 (concat (car thing) "/" (downcase (cdr thing))))
115 (t (car thing)))))
117 (defun org-parse-arguments ()
118 "Parse whitespace separated arguments in the current region."
119 (let ((begin (line-beginning-position))
120 (end (line-end-position))
121 begins args)
122 (save-restriction
123 (narrow-to-region begin end)
124 (save-excursion
125 (goto-char (point-min))
126 (while (not (eobp))
127 (skip-chars-forward " \t\n[")
128 (setq begins (cons (point) begins))
129 (skip-chars-forward "^ \t\n[")
130 (setq args (cons (buffer-substring-no-properties
131 (car begins) (point))
132 args)))
133 (cons (reverse args) (reverse begins))))))
135 (defun org-pcomplete-initial ()
136 "Calls the right completion function for first argument completions."
137 (ignore
138 (funcall (or (pcomplete-find-completion-function
139 (car (org-thing-at-point)))
140 pcomplete-default-completion-function))))
142 (defvar org-options-keywords) ; From org.el
143 (defvar org-element-block-name-alist) ; From org-element.el
144 (defvar org-element-affiliated-keywords) ; From org-element.el
145 (declare-function org-get-export-keywords "org" ())
146 (defun pcomplete/org-mode/file-option ()
147 "Complete against all valid file options."
148 (pcomplete-here
149 (org-pcomplete-case-double
150 (append (mapcar (lambda (keyword) (concat keyword " "))
151 org-options-keywords)
152 (mapcar (lambda (keyword) (concat keyword ": "))
153 org-element-affiliated-keywords)
154 (let (block-names)
155 (mapc (lambda (block-name)
156 (let ((name (car block-name)))
157 (push (format "END_%s: " name) block-names)
158 (push (format "BEGIN_%s: " name) block-names)
159 (push (format "ATTR_%s: " name) block-names)))
160 org-element-block-name-alist)
161 block-names)
162 (mapcar (lambda (keyword) (concat keyword ": "))
163 (org-get-export-keywords))))
164 (substring pcomplete-stub 2)))
166 (defun pcomplete/org-mode/file-option/author ()
167 "Complete arguments for the #+AUTHOR file option."
168 (pcomplete-here (list user-full-name)))
170 (defvar org-time-stamp-formats)
171 (defun pcomplete/org-mode/file-option/date ()
172 "Complete arguments for the #+DATE file option."
173 (pcomplete-here (list (format-time-string (car org-time-stamp-formats)))))
175 (defun pcomplete/org-mode/file-option/email ()
176 "Complete arguments for the #+EMAIL file option."
177 (pcomplete-here (list user-mail-address)))
179 (defvar org-export-exclude-tags)
180 (defun pcomplete/org-mode/file-option/exclude_tags ()
181 "Complete arguments for the #+EXCLUDE_TAGS file option."
182 (require 'ox)
183 (pcomplete-here
184 (and org-export-exclude-tags
185 (list (mapconcat 'identity org-export-exclude-tags " ")))))
187 (defvar org-file-tags)
188 (defun pcomplete/org-mode/file-option/filetags ()
189 "Complete arguments for the #+FILETAGS file option."
190 (pcomplete-here (and org-file-tags (mapconcat 'identity org-file-tags " "))))
192 (defvar org-export-default-language)
193 (defun pcomplete/org-mode/file-option/language ()
194 "Complete arguments for the #+LANGUAGE file option."
195 (require 'ox)
196 (pcomplete-here
197 (pcomplete-uniqify-list
198 (list org-export-default-language "en"))))
200 (defvar org-default-priority)
201 (defvar org-highest-priority)
202 (defvar org-lowest-priority)
203 (defun pcomplete/org-mode/file-option/priorities ()
204 "Complete arguments for the #+PRIORITIES file option."
205 (pcomplete-here (list (format "%c %c %c"
206 org-highest-priority
207 org-lowest-priority
208 org-default-priority))))
210 (defvar org-export-select-tags)
211 (defun pcomplete/org-mode/file-option/select_tags ()
212 "Complete arguments for the #+SELECT_TAGS file option."
213 (require 'ox)
214 (pcomplete-here
215 (and org-export-select-tags
216 (list (mapconcat 'identity org-export-select-tags " ")))))
218 (defvar org-startup-options)
219 (defun pcomplete/org-mode/file-option/startup ()
220 "Complete arguments for the #+STARTUP file option."
221 (while (pcomplete-here
222 (let ((opts (pcomplete-uniqify-list
223 (mapcar 'car org-startup-options))))
224 ;; Some options are mutually exclusive, and shouldn't be completed
225 ;; against if certain other options have already been seen.
226 (dolist (arg pcomplete-args)
227 (cond
228 ((string= arg "hidestars")
229 (setq opts (delete "showstars" opts)))))
230 opts))))
232 (defvar org-tag-alist)
233 (defun pcomplete/org-mode/file-option/tags ()
234 "Complete arguments for the #+TAGS file option."
235 (pcomplete-here
236 (list
237 (mapconcat (lambda (x)
238 (cond
239 ((eq :startgroup (car x)) "{")
240 ((eq :endgroup (car x)) "}")
241 ((eq :newline (car x)) "\\n")
242 ((cdr x) (format "%s(%c)" (car x) (cdr x)))
243 (t (car x))))
244 org-tag-alist " "))))
246 (defun pcomplete/org-mode/file-option/title ()
247 "Complete arguments for the #+TITLE file option."
248 (pcomplete-here
249 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
250 (list (or (and visited-file
251 (file-name-sans-extension
252 (file-name-nondirectory visited-file)))
253 (buffer-name (buffer-base-buffer)))))))
255 (defun pcomplete/org-mode/file-option/options ()
256 "Complete arguments for the #+OPTIONS file option."
257 (while (pcomplete-here
258 (pcomplete-uniqify-list
259 (append
260 ;; Hard-coded OPTION items always available.
261 '("H:" "\\n:" "num:" "timestamp:" "arch:" "author:" "c:"
262 "creator:" "date:" "d:" "email:" "*:" "e:" "::" "f:"
263 "inline:" "tex:" "p:" "pri:" "':" "-:" "stat:" "^:" "toc:"
264 "|:" "tags:" "tasks:" "<:" "todo:")
265 ;; OPTION items from registered back-ends.
266 (let (items)
267 (dolist (back-end (org-bound-and-true-p
268 org-export-registered-backends))
269 (dolist (option (plist-get (cdr back-end) :options-alist))
270 (let ((item (nth 2 option)))
271 (when item (push (concat item ":") items)))))
272 items))))))
274 (defun pcomplete/org-mode/file-option/infojs_opt ()
275 "Complete arguments for the #+INFOJS_OPT file option."
276 (while (pcomplete-here
277 (pcomplete-uniqify-list
278 (mapcar (lambda (item) (format "%s:" (car item)))
279 (org-bound-and-true-p org-infojs-opts-table))))))
281 (defun pcomplete/org-mode/file-option/bind ()
282 "Complete arguments for the #+BIND file option, which are variable names."
283 (let (vars)
284 (mapatoms
285 (lambda (a) (if (boundp a) (setq vars (cons (symbol-name a) vars)))))
286 (pcomplete-here vars)))
288 (defvar org-link-abbrev-alist-local)
289 (defvar org-link-abbrev-alist)
290 (defun pcomplete/org-mode/link ()
291 "Complete against defined #+LINK patterns."
292 (pcomplete-here
293 (pcomplete-uniqify-list
294 (copy-sequence
295 (append (mapcar 'car org-link-abbrev-alist-local)
296 (mapcar 'car org-link-abbrev-alist))))))
298 (defvar org-entities)
299 (defun pcomplete/org-mode/tex ()
300 "Complete against TeX-style HTML entity names."
301 (require 'org-entities)
302 (while (pcomplete-here
303 (pcomplete-uniqify-list (remove nil (mapcar 'car-safe org-entities)))
304 (substring pcomplete-stub 1))))
306 (defvar org-todo-keywords-1)
307 (defun pcomplete/org-mode/todo ()
308 "Complete against known TODO keywords."
309 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1))))
311 (defvar org-todo-line-regexp)
312 (defun pcomplete/org-mode/searchhead ()
313 "Complete against all headings.
314 This needs more work, to handle headings with lots of spaces in them."
315 (while
316 (pcomplete-here
317 (save-excursion
318 (goto-char (point-min))
319 (let (tbl)
320 (while (re-search-forward org-todo-line-regexp nil t)
321 (push (org-make-org-heading-search-string
322 (match-string-no-properties 3) t)
323 tbl))
324 (pcomplete-uniqify-list tbl)))
325 (substring pcomplete-stub 1))))
327 (defvar org-tag-alist)
328 (defun pcomplete/org-mode/tag ()
329 "Complete a tag name. Omit tags already set."
330 (while (pcomplete-here
331 (mapcar (lambda (x)
332 (concat x ":"))
333 (let ((lst (pcomplete-uniqify-list
334 (or (remove
336 (mapcar (lambda (x)
337 (and (stringp (car x)) (car x)))
338 org-tag-alist))
339 (mapcar 'car (org-get-buffer-tags))))))
340 (dolist (tag (org-get-tags))
341 (setq lst (delete tag lst)))
342 lst))
343 (and (string-match ".*:" pcomplete-stub)
344 (substring pcomplete-stub (match-end 0))))))
346 (defun pcomplete/org-mode/prop ()
347 "Complete a property name. Omit properties already set."
348 (pcomplete-here
349 (mapcar (lambda (x)
350 (concat x ": "))
351 (let ((lst (pcomplete-uniqify-list
352 (copy-sequence
353 (org-buffer-property-keys nil t t)))))
354 (dolist (prop (org-entry-properties))
355 (setq lst (delete (car prop) lst)))
356 lst))
357 (substring pcomplete-stub 1)))
359 (defvar org-drawers)
361 (defun pcomplete/org-mode/drawer ()
362 "Complete a drawer name."
363 (let ((spc (save-excursion
364 (move-beginning-of-line 1)
365 (looking-at "^\\([ \t]*\\):")
366 (match-string 1)))
367 (cpllist (mapcar (lambda (x) (concat x ": ")) org-drawers)))
368 (pcomplete-here cpllist
369 (substring pcomplete-stub 1)
370 (unless (or (not (delq
372 (mapcar (lambda(x)
373 (string-match (substring pcomplete-stub 1) x))
374 cpllist)))
375 (looking-at "[ \t]*\n.*:END:"))
376 (save-excursion (insert "\n" spc ":END:"))))))
378 (defun pcomplete/org-mode/block-option/src ()
379 "Complete the arguments of a begin_src block.
380 Complete a language in the first field, the header arguments and switches."
381 (pcomplete-here
382 (mapcar
383 (lambda(x) (symbol-name (nth 3 x)))
384 (cdr (car (cdr (memq :key-type (plist-get
385 (symbol-plist
386 'org-babel-load-languages)
387 'custom-type)))))))
388 (while (pcomplete-here
389 '("-n" "-r" "-l"
390 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
391 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
392 ":session" ":shebang" ":tangle" ":var"))))
394 (defun pcomplete/org-mode/block-option/clocktable ()
395 "Complete keywords in a clocktable line."
396 (while (pcomplete-here '(":maxlevel" ":scope"
397 ":tstart" ":tend" ":block" ":step"
398 ":stepskip0" ":fileskip0"
399 ":emphasize" ":link" ":narrow" ":indent"
400 ":tcolumns" ":level" ":compact" ":timestamp"
401 ":formula" ":formatter"))))
403 (defun org-pcomplete-case-double (list)
404 "Return list with both upcase and downcase version of all strings in LIST."
405 (let (e res)
406 (while (setq e (pop list))
407 (setq res (cons (downcase e) (cons (upcase e) res))))
408 (nreverse res)))
410 ;;;; Finish up
412 (provide 'org-pcomplete)
414 ;;; org-pcomplete.el ends here