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