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