Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-pcomplete.el
blobaaff7c3245c8e8ccd3476cd9c671038275f7c0eb
1 ;;; org-pcomplete.el --- In-buffer completion code
3 ;; Copyright (C) 2004-2016 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))
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 specials defaults columns ignore-malformed))
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 (line-beginning-position))
98 (org-looking-back org-property-re
99 (line-beginning-position)))))
100 (cons "prop" nil))
101 ((and (equal (char-before beg1) ?:)
102 (not (equal (char-after (point-at-bol)) ?*)))
103 (cons "drawer" nil))
104 (t nil))))
106 (defun org-command-at-point ()
107 "Return the qualified name of the Org completion entity at point.
108 When completing for #+STARTUP, for example, this function returns
109 \"file-option/startup\"."
110 (let ((thing (org-thing-at-point)))
111 (cond
112 ((string= "file-option" (car thing))
113 (concat (car thing)
114 (and (cdr thing) (concat "/" (downcase (cdr thing))))))
115 ((string= "block-option" (car thing))
116 (concat (car thing) "/" (downcase (cdr thing))))
117 (t (car thing)))))
119 (defun org-parse-arguments ()
120 "Parse whitespace separated arguments in the current region."
121 (let ((begin (line-beginning-position))
122 (end (line-end-position))
123 begins args)
124 (save-restriction
125 (narrow-to-region begin end)
126 (save-excursion
127 (goto-char (point-min))
128 (while (not (eobp))
129 (skip-chars-forward " \t\n[")
130 (setq begins (cons (point) begins))
131 (skip-chars-forward "^ \t\n[")
132 (setq args (cons (buffer-substring-no-properties
133 (car begins) (point))
134 args)))
135 (cons (reverse args) (reverse begins))))))
137 (defun org-pcomplete-initial ()
138 "Calls the right completion function for first argument completions."
139 (ignore
140 (funcall (or (pcomplete-find-completion-function
141 (car (org-thing-at-point)))
142 pcomplete-default-completion-function))))
144 (defvar org-options-keywords) ; From org.el
145 (defvar org-element-affiliated-keywords) ; From org-element.el
146 (declare-function org-get-export-keywords "org" ())
147 (defun pcomplete/org-mode/file-option ()
148 "Complete against all valid file options."
149 (require 'org-element)
150 (pcomplete-here
151 (org-pcomplete-case-double
152 (append (mapcar (lambda (keyword) (concat keyword " "))
153 org-options-keywords)
154 (mapcar (lambda (keyword) (concat keyword ": "))
155 org-element-affiliated-keywords)
156 (let (block-names)
157 (dolist (name
158 '("CENTER" "COMMENT" "EXAMPLE" "EXPORT" "QUOTE" "SRC"
159 "VERSE")
160 block-names)
161 (push (format "END_%s" name) block-names)
162 (push (concat "BEGIN_"
163 name
164 ;; Since language is compulsory in
165 ;; export blocks source blocks, add
166 ;; a space.
167 (and (member name '("EXPORT" "SRC")) " "))
168 block-names)
169 (push (format "ATTR_%s: " name) block-names)))
170 (mapcar (lambda (keyword) (concat keyword ": "))
171 (org-get-export-keywords))))
172 (substring pcomplete-stub 2)))
174 (defun pcomplete/org-mode/file-option/author ()
175 "Complete arguments for the #+AUTHOR file option."
176 (pcomplete-here (list user-full-name)))
178 (defvar org-time-stamp-formats)
179 (defun pcomplete/org-mode/file-option/date ()
180 "Complete arguments for the #+DATE file option."
181 (pcomplete-here (list (format-time-string (car org-time-stamp-formats)))))
183 (defun pcomplete/org-mode/file-option/email ()
184 "Complete arguments for the #+EMAIL file option."
185 (pcomplete-here (list user-mail-address)))
187 (defvar org-export-exclude-tags)
188 (defun pcomplete/org-mode/file-option/exclude_tags ()
189 "Complete arguments for the #+EXCLUDE_TAGS file option."
190 (require 'ox)
191 (pcomplete-here
192 (and org-export-exclude-tags
193 (list (mapconcat 'identity org-export-exclude-tags " ")))))
195 (defvar org-file-tags)
196 (defun pcomplete/org-mode/file-option/filetags ()
197 "Complete arguments for the #+FILETAGS file option."
198 (pcomplete-here (and org-file-tags (mapconcat 'identity org-file-tags " "))))
200 (defvar org-export-default-language)
201 (defun pcomplete/org-mode/file-option/language ()
202 "Complete arguments for the #+LANGUAGE file option."
203 (require 'ox)
204 (pcomplete-here
205 (pcomplete-uniqify-list
206 (list org-export-default-language "en"))))
208 (defvar org-default-priority)
209 (defvar org-highest-priority)
210 (defvar org-lowest-priority)
211 (defun pcomplete/org-mode/file-option/priorities ()
212 "Complete arguments for the #+PRIORITIES file option."
213 (pcomplete-here (list (format "%c %c %c"
214 org-highest-priority
215 org-lowest-priority
216 org-default-priority))))
218 (defvar org-export-select-tags)
219 (defun pcomplete/org-mode/file-option/select_tags ()
220 "Complete arguments for the #+SELECT_TAGS file option."
221 (require 'ox)
222 (pcomplete-here
223 (and org-export-select-tags
224 (list (mapconcat 'identity org-export-select-tags " ")))))
226 (defvar org-startup-options)
227 (defun pcomplete/org-mode/file-option/startup ()
228 "Complete arguments for the #+STARTUP file option."
229 (while (pcomplete-here
230 (let ((opts (pcomplete-uniqify-list
231 (mapcar 'car org-startup-options))))
232 ;; Some options are mutually exclusive, and shouldn't be completed
233 ;; against if certain other options have already been seen.
234 (dolist (arg pcomplete-args)
235 (cond
236 ((string= arg "hidestars")
237 (setq opts (delete "showstars" opts)))))
238 opts))))
240 (defvar org-tag-alist)
241 (defun pcomplete/org-mode/file-option/tags ()
242 "Complete arguments for the #+TAGS file option."
243 (pcomplete-here
244 (list
245 (mapconcat (lambda (x)
246 (cond
247 ((eq :startgroup (car x)) "{")
248 ((eq :endgroup (car x)) "}")
249 ((eq :grouptags (car x)) ":")
250 ((eq :newline (car x)) "\\n")
251 ((cdr x) (format "%s(%c)" (car x) (cdr x)))
252 (t (car x))))
253 org-tag-alist " "))))
255 (defun pcomplete/org-mode/file-option/title ()
256 "Complete arguments for the #+TITLE file option."
257 (pcomplete-here
258 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
259 (list (or (and visited-file
260 (file-name-sans-extension
261 (file-name-nondirectory visited-file)))
262 (buffer-name (buffer-base-buffer)))))))
265 (declare-function org-export-backend-options "org-export" (cl-x))
266 (defun pcomplete/org-mode/file-option/options ()
267 "Complete arguments for the #+OPTIONS file option."
268 (while (pcomplete-here
269 (pcomplete-uniqify-list
270 (append
271 ;; Hard-coded OPTION items always available.
272 '("H:" "\\n:" "num:" "timestamp:" "arch:" "author:" "c:"
273 "creator:" "date:" "d:" "email:" "*:" "e:" "::" "f:"
274 "inline:" "tex:" "p:" "pri:" "':" "-:" "stat:" "^:" "toc:"
275 "|:" "tags:" "tasks:" "<:" "todo:")
276 ;; OPTION items from registered back-ends.
277 (let (items)
278 (dolist (backend (org-bound-and-true-p
279 org-export-registered-backends))
280 (dolist (option (org-export-backend-options backend))
281 (let ((item (nth 2 option)))
282 (when item (push (concat item ":") items)))))
283 items))))))
285 (defun pcomplete/org-mode/file-option/infojs_opt ()
286 "Complete arguments for the #+INFOJS_OPT file option."
287 (while (pcomplete-here
288 (pcomplete-uniqify-list
289 (mapcar (lambda (item) (format "%s:" (car item)))
290 (org-bound-and-true-p org-html-infojs-opts-table))))))
292 (defun pcomplete/org-mode/file-option/bind ()
293 "Complete arguments for the #+BIND file option, which are variable names."
294 (let (vars)
295 (mapatoms
296 (lambda (a) (if (boundp a) (setq vars (cons (symbol-name a) vars)))))
297 (pcomplete-here vars)))
299 (defvar org-link-abbrev-alist-local)
300 (defvar org-link-abbrev-alist)
301 (defun pcomplete/org-mode/link ()
302 "Complete against defined #+LINK patterns."
303 (pcomplete-here
304 (pcomplete-uniqify-list
305 (copy-sequence
306 (append (mapcar 'car org-link-abbrev-alist-local)
307 (mapcar 'car org-link-abbrev-alist))))))
309 (defvar org-entities)
310 (defun pcomplete/org-mode/tex ()
311 "Complete against TeX-style HTML entity names."
312 (require 'org-entities)
313 (while (pcomplete-here
314 (pcomplete-uniqify-list (remove nil (mapcar 'car-safe org-entities)))
315 (substring pcomplete-stub 1))))
317 (defvar org-todo-keywords-1)
318 (defun pcomplete/org-mode/todo ()
319 "Complete against known TODO keywords."
320 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1))))
322 (defvar org-todo-line-regexp)
323 (defun pcomplete/org-mode/searchhead ()
324 "Complete against all headings.
325 This needs more work, to handle headings with lots of spaces in them."
326 (while
327 (pcomplete-here
328 (save-excursion
329 (goto-char (point-min))
330 (let (tbl)
331 (while (re-search-forward org-todo-line-regexp nil t)
332 (push (org-make-org-heading-search-string
333 (match-string-no-properties 3))
334 tbl))
335 (pcomplete-uniqify-list tbl)))
336 (substring pcomplete-stub 1))))
338 (defvar org-tag-alist)
339 (defun pcomplete/org-mode/tag ()
340 "Complete a tag name. Omit tags already set."
341 (while (pcomplete-here
342 (mapcar (lambda (x)
343 (concat x ":"))
344 (let ((lst (pcomplete-uniqify-list
345 (or (remove
347 (mapcar (lambda (x)
348 (and (stringp (car x)) (car x)))
349 org-tag-alist))
350 (mapcar 'car (org-get-buffer-tags))))))
351 (dolist (tag (org-get-tags))
352 (setq lst (delete tag lst)))
353 lst))
354 (and (string-match ".*:" pcomplete-stub)
355 (substring pcomplete-stub (match-end 0))))))
357 (defun pcomplete/org-mode/prop ()
358 "Complete a property name. Omit properties already set."
359 (pcomplete-here
360 (mapcar (lambda (x)
361 (concat x ": "))
362 (let ((lst (pcomplete-uniqify-list
363 (copy-sequence
364 (org-buffer-property-keys nil t t t)))))
365 (dolist (prop (org-entry-properties))
366 (setq lst (delete (car prop) lst)))
367 lst))
368 (substring pcomplete-stub 1)))
370 (defun pcomplete/org-mode/block-option/src ()
371 "Complete the arguments of a begin_src block.
372 Complete a language in the first field, the header arguments and switches."
373 (pcomplete-here
374 (mapcar
375 (lambda(x) (symbol-name (nth 3 x)))
376 (cdr (car (cdr (memq :key-type (plist-get
377 (symbol-plist
378 'org-babel-load-languages)
379 'custom-type)))))))
380 (while (pcomplete-here
381 '("-n" "-r" "-l"
382 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
383 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
384 ":session" ":shebang" ":tangle" ":tangle-mode" ":var"))))
386 (defun pcomplete/org-mode/block-option/clocktable ()
387 "Complete keywords in a clocktable line."
388 (while (pcomplete-here '(":maxlevel" ":scope" ":lang"
389 ":tstart" ":tend" ":block" ":step"
390 ":stepskip0" ":fileskip0"
391 ":emphasize" ":link" ":narrow" ":indent"
392 ":tcolumns" ":level" ":compact" ":timestamp"
393 ":formula" ":formatter" ":wstart" ":mstart"))))
395 (defun org-pcomplete-case-double (list)
396 "Return list with both upcase and downcase version of all strings in LIST."
397 (let (e res)
398 (while (setq e (pop list))
399 (setq res (cons (downcase e) (cons (upcase e) res))))
400 (nreverse res)))
402 ;;;; Finish up
404 (provide 'org-pcomplete)
406 ;;; org-pcomplete.el ends here