ox: Fix 8c833ed9fbabae298054dd9eec574984acf3ab8b
[org-mode.git] / lisp / org-pcomplete.el
blobe4646842568eb521d0ece8cfe56a582395abf5ca
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 (require 'org-element)
149 (pcomplete-here
150 (org-pcomplete-case-double
151 (append (mapcar (lambda (keyword) (concat keyword " "))
152 org-options-keywords)
153 (mapcar (lambda (keyword) (concat keyword ": "))
154 org-element-affiliated-keywords)
155 (let (block-names)
156 (dolist (block-info org-element-block-name-alist block-names)
157 (let ((name (car block-info)))
158 (push (format "END_%s" name) block-names)
159 (push (concat "BEGIN_"
160 name
161 ;; Since language is compulsory in
162 ;; source blocks, add a space.
163 (and (equal name "SRC") " "))
164 block-names)
165 (push (format "ATTR_%s: " name) block-names))))
166 (mapcar (lambda (keyword) (concat keyword ": "))
167 (org-get-export-keywords))))
168 (substring pcomplete-stub 2)))
170 (defun pcomplete/org-mode/file-option/author ()
171 "Complete arguments for the #+AUTHOR file option."
172 (pcomplete-here (list user-full-name)))
174 (defvar org-time-stamp-formats)
175 (defun pcomplete/org-mode/file-option/date ()
176 "Complete arguments for the #+DATE file option."
177 (pcomplete-here (list (format-time-string (car org-time-stamp-formats)))))
179 (defun pcomplete/org-mode/file-option/email ()
180 "Complete arguments for the #+EMAIL file option."
181 (pcomplete-here (list user-mail-address)))
183 (defvar org-export-exclude-tags)
184 (defun pcomplete/org-mode/file-option/exclude_tags ()
185 "Complete arguments for the #+EXCLUDE_TAGS file option."
186 (require 'ox)
187 (pcomplete-here
188 (and org-export-exclude-tags
189 (list (mapconcat 'identity org-export-exclude-tags " ")))))
191 (defvar org-file-tags)
192 (defun pcomplete/org-mode/file-option/filetags ()
193 "Complete arguments for the #+FILETAGS file option."
194 (pcomplete-here (and org-file-tags (mapconcat 'identity org-file-tags " "))))
196 (defvar org-export-default-language)
197 (defun pcomplete/org-mode/file-option/language ()
198 "Complete arguments for the #+LANGUAGE file option."
199 (require 'ox)
200 (pcomplete-here
201 (pcomplete-uniqify-list
202 (list org-export-default-language "en"))))
204 (defvar org-default-priority)
205 (defvar org-highest-priority)
206 (defvar org-lowest-priority)
207 (defun pcomplete/org-mode/file-option/priorities ()
208 "Complete arguments for the #+PRIORITIES file option."
209 (pcomplete-here (list (format "%c %c %c"
210 org-highest-priority
211 org-lowest-priority
212 org-default-priority))))
214 (defvar org-export-select-tags)
215 (defun pcomplete/org-mode/file-option/select_tags ()
216 "Complete arguments for the #+SELECT_TAGS file option."
217 (require 'ox)
218 (pcomplete-here
219 (and org-export-select-tags
220 (list (mapconcat 'identity org-export-select-tags " ")))))
222 (defvar org-startup-options)
223 (defun pcomplete/org-mode/file-option/startup ()
224 "Complete arguments for the #+STARTUP file option."
225 (while (pcomplete-here
226 (let ((opts (pcomplete-uniqify-list
227 (mapcar 'car org-startup-options))))
228 ;; Some options are mutually exclusive, and shouldn't be completed
229 ;; against if certain other options have already been seen.
230 (dolist (arg pcomplete-args)
231 (cond
232 ((string= arg "hidestars")
233 (setq opts (delete "showstars" opts)))))
234 opts))))
236 (defvar org-tag-alist)
237 (defun pcomplete/org-mode/file-option/tags ()
238 "Complete arguments for the #+TAGS file option."
239 (pcomplete-here
240 (list
241 (mapconcat (lambda (x)
242 (cond
243 ((eq :startgroup (car x)) "{")
244 ((eq :endgroup (car x)) "}")
245 ((eq :grouptags (car x)) ":")
246 ((eq :newline (car x)) "\\n")
247 ((cdr x) (format "%s(%c)" (car x) (cdr x)))
248 (t (car x))))
249 org-tag-alist " "))))
251 (defun pcomplete/org-mode/file-option/title ()
252 "Complete arguments for the #+TITLE file option."
253 (pcomplete-here
254 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
255 (list (or (and visited-file
256 (file-name-sans-extension
257 (file-name-nondirectory visited-file)))
258 (buffer-name (buffer-base-buffer)))))))
260 (defun pcomplete/org-mode/file-option/options ()
261 "Complete arguments for the #+OPTIONS file option."
262 (while (pcomplete-here
263 (pcomplete-uniqify-list
264 (append
265 ;; Hard-coded OPTION items always available.
266 '("H:" "\\n:" "num:" "timestamp:" "arch:" "author:" "c:"
267 "creator:" "date:" "d:" "email:" "*:" "e:" "::" "f:"
268 "inline:" "tex:" "p:" "pri:" "':" "-:" "stat:" "^:" "toc:"
269 "|:" "tags:" "tasks:" "<:" "todo:")
270 ;; OPTION items from registered back-ends.
271 (let (items)
272 (dolist (back-end (org-bound-and-true-p
273 org-export-registered-backends))
274 (dolist (option (plist-get (cdr back-end) :options-alist))
275 (let ((item (nth 2 option)))
276 (when item (push (concat item ":") items)))))
277 items))))))
279 (defun pcomplete/org-mode/file-option/infojs_opt ()
280 "Complete arguments for the #+INFOJS_OPT file option."
281 (while (pcomplete-here
282 (pcomplete-uniqify-list
283 (mapcar (lambda (item) (format "%s:" (car item)))
284 (org-bound-and-true-p org-html-infojs-opts-table))))))
286 (defun pcomplete/org-mode/file-option/bind ()
287 "Complete arguments for the #+BIND file option, which are variable names."
288 (let (vars)
289 (mapatoms
290 (lambda (a) (if (boundp a) (setq vars (cons (symbol-name a) vars)))))
291 (pcomplete-here vars)))
293 (defvar org-link-abbrev-alist-local)
294 (defvar org-link-abbrev-alist)
295 (defun pcomplete/org-mode/link ()
296 "Complete against defined #+LINK patterns."
297 (pcomplete-here
298 (pcomplete-uniqify-list
299 (copy-sequence
300 (append (mapcar 'car org-link-abbrev-alist-local)
301 (mapcar 'car org-link-abbrev-alist))))))
303 (defvar org-entities)
304 (defun pcomplete/org-mode/tex ()
305 "Complete against TeX-style HTML entity names."
306 (require 'org-entities)
307 (while (pcomplete-here
308 (pcomplete-uniqify-list (remove nil (mapcar 'car-safe org-entities)))
309 (substring pcomplete-stub 1))))
311 (defvar org-todo-keywords-1)
312 (defun pcomplete/org-mode/todo ()
313 "Complete against known TODO keywords."
314 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1))))
316 (defvar org-todo-line-regexp)
317 (defun pcomplete/org-mode/searchhead ()
318 "Complete against all headings.
319 This needs more work, to handle headings with lots of spaces in them."
320 (while
321 (pcomplete-here
322 (save-excursion
323 (goto-char (point-min))
324 (let (tbl)
325 (while (re-search-forward org-todo-line-regexp nil t)
326 (push (org-make-org-heading-search-string
327 (match-string-no-properties 3) t)
328 tbl))
329 (pcomplete-uniqify-list tbl)))
330 (substring pcomplete-stub 1))))
332 (defvar org-tag-alist)
333 (defun pcomplete/org-mode/tag ()
334 "Complete a tag name. Omit tags already set."
335 (while (pcomplete-here
336 (mapcar (lambda (x)
337 (concat x ":"))
338 (let ((lst (pcomplete-uniqify-list
339 (or (remove
341 (mapcar (lambda (x)
342 (and (stringp (car x)) (car x)))
343 org-tag-alist))
344 (mapcar 'car (org-get-buffer-tags))))))
345 (dolist (tag (org-get-tags))
346 (setq lst (delete tag lst)))
347 lst))
348 (and (string-match ".*:" pcomplete-stub)
349 (substring pcomplete-stub (match-end 0))))))
351 (defun pcomplete/org-mode/prop ()
352 "Complete a property name. Omit properties already set."
353 (pcomplete-here
354 (mapcar (lambda (x)
355 (concat x ": "))
356 (let ((lst (pcomplete-uniqify-list
357 (copy-sequence
358 (org-buffer-property-keys nil t t)))))
359 (dolist (prop (org-entry-properties))
360 (setq lst (delete (car prop) lst)))
361 lst))
362 (substring pcomplete-stub 1)))
364 (defvar org-drawers)
366 (defun pcomplete/org-mode/drawer ()
367 "Complete a drawer name."
368 (let ((spc (save-excursion
369 (move-beginning-of-line 1)
370 (looking-at "^\\([ \t]*\\):")
371 (match-string 1)))
372 (cpllist (mapcar (lambda (x) (concat x ": ")) org-drawers)))
373 (pcomplete-here cpllist
374 (substring pcomplete-stub 1)
375 (unless (or (not (delq
377 (mapcar (lambda(x)
378 (string-match (substring pcomplete-stub 1) x))
379 cpllist)))
380 (looking-at "[ \t]*\n.*:END:"))
381 (save-excursion (insert "\n" spc ":END:"))))))
383 (defun pcomplete/org-mode/block-option/src ()
384 "Complete the arguments of a begin_src block.
385 Complete a language in the first field, the header arguments and switches."
386 (pcomplete-here
387 (mapcar
388 (lambda(x) (symbol-name (nth 3 x)))
389 (cdr (car (cdr (memq :key-type (plist-get
390 (symbol-plist
391 'org-babel-load-languages)
392 'custom-type)))))))
393 (while (pcomplete-here
394 '("-n" "-r" "-l"
395 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
396 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
397 ":session" ":shebang" ":tangle" ":var"))))
399 (defun pcomplete/org-mode/block-option/clocktable ()
400 "Complete keywords in a clocktable line."
401 (while (pcomplete-here '(":maxlevel" ":scope" ":lang"
402 ":tstart" ":tend" ":block" ":step"
403 ":stepskip0" ":fileskip0"
404 ":emphasize" ":link" ":narrow" ":indent"
405 ":tcolumns" ":level" ":compact" ":timestamp"
406 ":formula" ":formatter" ":wstart" ":mstart"))))
408 (defun org-pcomplete-case-double (list)
409 "Return list with both upcase and downcase version of all strings in LIST."
410 (let (e res)
411 (while (setq e (pop list))
412 (setq res (cons (downcase e) (cons (upcase e) res))))
413 (nreverse res)))
415 ;;;; Finish up
417 (provide 'org-pcomplete)
419 ;;; org-pcomplete.el ends here