Merge branch 'maint'
[org-mode.git] / lisp / org-pcomplete.el
blobc8387b4cb2689ec9e5e78174d8aa26373da32624
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))
45 (declare-function org-tag-alist-to-string "org" (alist &optional skip-key))
47 ;;;; Customization variables
49 (defgroup org-complete nil
50 "Outline-based notes management and organizer."
51 :tag "Org"
52 :group 'org)
54 (defvar org-drawer-regexp)
55 (defvar org-property-re)
56 (defvar org-current-tag-alist)
58 (defun org-thing-at-point ()
59 "Examine the thing at point and let the caller know what it is.
60 The return value is a string naming the thing at point."
61 (let ((beg1 (save-excursion
62 (skip-chars-backward (org-re "[:alnum:]-_@"))
63 (point)))
64 (beg (save-excursion
65 (skip-chars-backward "a-zA-Z0-9-_:$")
66 (point)))
67 (line-to-here (buffer-substring (point-at-bol) (point))))
68 (cond
69 ((string-match "\\`[ \t]*#\\+begin: clocktable[ \t]+" line-to-here)
70 (cons "block-option" "clocktable"))
71 ((string-match "\\`[ \t]*#\\+begin_src[ \t]+" line-to-here)
72 (cons "block-option" "src"))
73 ((save-excursion
74 (re-search-backward "^[ \t]*#\\+\\([A-Z_]+\\):.*"
75 (line-beginning-position) t))
76 (cons "file-option" (match-string-no-properties 1)))
77 ((string-match "\\`[ \t]*#\\+[a-zA-Z_]*\\'" line-to-here)
78 (cons "file-option" nil))
79 ((equal (char-before beg) ?\[)
80 (cons "link" nil))
81 ((equal (char-before beg) ?\\)
82 (cons "tex" nil))
83 ((string-match "\\`\\*+[ \t]+\\'"
84 (buffer-substring (point-at-bol) beg))
85 (cons "todo" nil))
86 ((equal (char-before beg) ?*)
87 (cons "searchhead" nil))
88 ((and (equal (char-before beg1) ?:)
89 (equal (char-after (point-at-bol)) ?*))
90 (cons "tag" nil))
91 ((and (equal (char-before beg1) ?:)
92 (not (equal (char-after (point-at-bol)) ?*))
93 (save-excursion
94 (move-beginning-of-line 1)
95 (skip-chars-backward "[ \t\n]")
96 ;; org-drawer-regexp matches a whole line but while
97 ;; looking-back, we just ignore trailing whitespaces
98 (or (org-looking-back (substring org-drawer-regexp 0 -1)
99 (line-beginning-position))
100 (org-looking-back org-property-re
101 (line-beginning-position)))))
102 (cons "prop" nil))
103 ((and (equal (char-before beg1) ?:)
104 (not (equal (char-after (point-at-bol)) ?*)))
105 (cons "drawer" nil))
106 (t nil))))
108 (defun org-command-at-point ()
109 "Return the qualified name of the Org completion entity at point.
110 When completing for #+STARTUP, for example, this function returns
111 \"file-option/startup\"."
112 (let ((thing (org-thing-at-point)))
113 (cond
114 ((string= "file-option" (car thing))
115 (concat (car thing)
116 (and (cdr thing) (concat "/" (downcase (cdr thing))))))
117 ((string= "block-option" (car thing))
118 (concat (car thing) "/" (downcase (cdr thing))))
119 (t (car thing)))))
121 (defun org-parse-arguments ()
122 "Parse whitespace separated arguments in the current region."
123 (let ((begin (line-beginning-position))
124 (end (line-end-position))
125 begins args)
126 (save-restriction
127 (narrow-to-region begin end)
128 (save-excursion
129 (goto-char (point-min))
130 (while (not (eobp))
131 (skip-chars-forward " \t\n[")
132 (setq begins (cons (point) begins))
133 (skip-chars-forward "^ \t\n[")
134 (setq args (cons (buffer-substring-no-properties
135 (car begins) (point))
136 args)))
137 (cons (reverse args) (reverse begins))))))
139 (defun org-pcomplete-initial ()
140 "Calls the right completion function for first argument completions."
141 (ignore
142 (funcall (or (pcomplete-find-completion-function
143 (car (org-thing-at-point)))
144 pcomplete-default-completion-function))))
146 (defvar org-options-keywords) ; From org.el
147 (defvar org-element-affiliated-keywords) ; From org-element.el
148 (declare-function org-get-export-keywords "org" ())
149 (defun pcomplete/org-mode/file-option ()
150 "Complete against all valid file options."
151 (require 'org-element)
152 (pcomplete-here
153 (org-pcomplete-case-double
154 (append (mapcar (lambda (keyword) (concat keyword " "))
155 org-options-keywords)
156 (mapcar (lambda (keyword) (concat keyword ": "))
157 org-element-affiliated-keywords)
158 (let (block-names)
159 (dolist (name
160 '("CENTER" "COMMENT" "EXAMPLE" "EXPORT" "QUOTE" "SRC"
161 "VERSE")
162 block-names)
163 (push (format "END_%s" name) block-names)
164 (push (concat "BEGIN_"
165 name
166 ;; Since language is compulsory in
167 ;; export blocks source blocks, add
168 ;; a space.
169 (and (member name '("EXPORT" "SRC")) " "))
170 block-names)
171 (push (format "ATTR_%s: " name) block-names)))
172 (mapcar (lambda (keyword) (concat keyword ": "))
173 (org-get-export-keywords))))
174 (substring pcomplete-stub 2)))
176 (defun pcomplete/org-mode/file-option/author ()
177 "Complete arguments for the #+AUTHOR file option."
178 (pcomplete-here (list user-full-name)))
180 (defvar org-time-stamp-formats)
181 (defun pcomplete/org-mode/file-option/date ()
182 "Complete arguments for the #+DATE file option."
183 (pcomplete-here (list (format-time-string (car org-time-stamp-formats)))))
185 (defun pcomplete/org-mode/file-option/email ()
186 "Complete arguments for the #+EMAIL file option."
187 (pcomplete-here (list user-mail-address)))
189 (defvar org-export-exclude-tags)
190 (defun pcomplete/org-mode/file-option/exclude_tags ()
191 "Complete arguments for the #+EXCLUDE_TAGS file option."
192 (require 'ox)
193 (pcomplete-here
194 (and org-export-exclude-tags
195 (list (mapconcat 'identity org-export-exclude-tags " ")))))
197 (defvar org-file-tags)
198 (defun pcomplete/org-mode/file-option/filetags ()
199 "Complete arguments for the #+FILETAGS file option."
200 (pcomplete-here (and org-file-tags (mapconcat 'identity org-file-tags " "))))
202 (defvar org-export-default-language)
203 (defun pcomplete/org-mode/file-option/language ()
204 "Complete arguments for the #+LANGUAGE file option."
205 (require 'ox)
206 (pcomplete-here
207 (pcomplete-uniqify-list
208 (list org-export-default-language "en"))))
210 (defvar org-default-priority)
211 (defvar org-highest-priority)
212 (defvar org-lowest-priority)
213 (defun pcomplete/org-mode/file-option/priorities ()
214 "Complete arguments for the #+PRIORITIES file option."
215 (pcomplete-here (list (format "%c %c %c"
216 org-highest-priority
217 org-lowest-priority
218 org-default-priority))))
220 (defvar org-export-select-tags)
221 (defun pcomplete/org-mode/file-option/select_tags ()
222 "Complete arguments for the #+SELECT_TAGS file option."
223 (require 'ox)
224 (pcomplete-here
225 (and org-export-select-tags
226 (list (mapconcat 'identity org-export-select-tags " ")))))
228 (defvar org-startup-options)
229 (defun pcomplete/org-mode/file-option/startup ()
230 "Complete arguments for the #+STARTUP file option."
231 (while (pcomplete-here
232 (let ((opts (pcomplete-uniqify-list
233 (mapcar 'car org-startup-options))))
234 ;; Some options are mutually exclusive, and shouldn't be completed
235 ;; against if certain other options have already been seen.
236 (dolist (arg pcomplete-args)
237 (cond
238 ((string= arg "hidestars")
239 (setq opts (delete "showstars" opts)))))
240 opts))))
242 (defun pcomplete/org-mode/file-option/tags ()
243 "Complete arguments for the #+TAGS file option."
244 (pcomplete-here
245 (list (org-tag-alist-to-string org-current-tag-alist))))
247 (defun pcomplete/org-mode/file-option/title ()
248 "Complete arguments for the #+TITLE file option."
249 (pcomplete-here
250 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
251 (list (or (and visited-file
252 (file-name-sans-extension
253 (file-name-nondirectory visited-file)))
254 (buffer-name (buffer-base-buffer)))))))
257 (declare-function org-export-backend-options "org-export" (cl-x))
258 (defun pcomplete/org-mode/file-option/options ()
259 "Complete arguments for the #+OPTIONS file option."
260 (while (pcomplete-here
261 (pcomplete-uniqify-list
262 (append
263 ;; Hard-coded OPTION items always available.
264 '("H:" "\\n:" "num:" "timestamp:" "arch:" "author:" "c:"
265 "creator:" "date:" "d:" "email:" "*:" "e:" "::" "f:"
266 "inline:" "tex:" "p:" "pri:" "':" "-:" "stat:" "^:" "toc:"
267 "|:" "tags:" "tasks:" "<:" "todo:")
268 ;; OPTION items from registered back-ends.
269 (let (items)
270 (dolist (backend (org-bound-and-true-p
271 org-export-registered-backends))
272 (dolist (option (org-export-backend-options backend))
273 (let ((item (nth 2 option)))
274 (when item (push (concat item ":") items)))))
275 items))))))
277 (defun pcomplete/org-mode/file-option/infojs_opt ()
278 "Complete arguments for the #+INFOJS_OPT file option."
279 (while (pcomplete-here
280 (pcomplete-uniqify-list
281 (mapcar (lambda (item) (format "%s:" (car item)))
282 (org-bound-and-true-p org-html-infojs-opts-table))))))
284 (defun pcomplete/org-mode/file-option/bind ()
285 "Complete arguments for the #+BIND file option, which are variable names."
286 (let (vars)
287 (mapatoms
288 (lambda (a) (if (boundp a) (setq vars (cons (symbol-name a) vars)))))
289 (pcomplete-here vars)))
291 (defvar org-link-abbrev-alist-local)
292 (defvar org-link-abbrev-alist)
293 (defun pcomplete/org-mode/link ()
294 "Complete against defined #+LINK patterns."
295 (pcomplete-here
296 (pcomplete-uniqify-list
297 (copy-sequence
298 (append (mapcar 'car org-link-abbrev-alist-local)
299 (mapcar 'car org-link-abbrev-alist))))))
301 (defvar org-entities)
302 (defun pcomplete/org-mode/tex ()
303 "Complete against TeX-style HTML entity names."
304 (require 'org-entities)
305 (while (pcomplete-here
306 (pcomplete-uniqify-list (remove nil (mapcar 'car-safe org-entities)))
307 (substring pcomplete-stub 1))))
309 (defvar org-todo-keywords-1)
310 (defun pcomplete/org-mode/todo ()
311 "Complete against known TODO keywords."
312 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1))))
314 (defvar org-todo-line-regexp)
315 (defun pcomplete/org-mode/searchhead ()
316 "Complete against all headings.
317 This needs more work, to handle headings with lots of spaces in them."
318 (while
319 (pcomplete-here
320 (save-excursion
321 (goto-char (point-min))
322 (let (tbl)
323 (while (re-search-forward org-todo-line-regexp nil t)
324 (push (org-make-org-heading-search-string
325 (match-string-no-properties 3))
326 tbl))
327 (pcomplete-uniqify-list tbl)))
328 (substring pcomplete-stub 1))))
330 (defun pcomplete/org-mode/tag ()
331 "Complete a tag name. Omit tags already set."
332 (while (pcomplete-here
333 (mapcar (lambda (x) (concat x ":"))
334 (let ((lst (pcomplete-uniqify-list
335 (or (remq
337 (mapcar (lambda (x) (org-string-nw-p (car x)))
338 org-current-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 t)))))
354 (dolist (prop (org-entry-properties))
355 (setq lst (delete (car prop) lst)))
356 lst))
357 (substring pcomplete-stub 1)))
359 (defun pcomplete/org-mode/block-option/src ()
360 "Complete the arguments of a begin_src block.
361 Complete a language in the first field, the header arguments and switches."
362 (pcomplete-here
363 (mapcar
364 (lambda(x) (symbol-name (nth 3 x)))
365 (cdr (car (cdr (memq :key-type (plist-get
366 (symbol-plist
367 'org-babel-load-languages)
368 'custom-type)))))))
369 (while (pcomplete-here
370 '("-n" "-r" "-l"
371 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
372 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
373 ":session" ":shebang" ":tangle" ":tangle-mode" ":var"))))
375 (defun pcomplete/org-mode/block-option/clocktable ()
376 "Complete keywords in a clocktable line."
377 (while (pcomplete-here '(":maxlevel" ":scope" ":lang"
378 ":tstart" ":tend" ":block" ":step"
379 ":stepskip0" ":fileskip0"
380 ":emphasize" ":link" ":narrow" ":indent"
381 ":tcolumns" ":level" ":compact" ":timestamp"
382 ":formula" ":formatter" ":wstart" ":mstart"))))
384 (defun org-pcomplete-case-double (list)
385 "Return list with both upcase and downcase version of all strings in LIST."
386 (let (e res)
387 (while (setq e (pop list))
388 (setq res (cons (downcase e) (cons (upcase e) res))))
389 (nreverse res)))
391 ;;;; Finish up
393 (provide 'org-pcomplete)
395 ;;; org-pcomplete.el ends here