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