1 ;;; org-pcomplete.el --- In-buffer completion code
3 ;; Copyright (C) 2004-2016 Free Software Foundation, Inc.
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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;;; Require other packages
37 (declare-function org-split-string "org" (string &optional separators))
38 (declare-function org-make-org-heading-search-string "org"
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."
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:]-_@"))
63 (skip-chars-backward "a-zA-Z0-9-_:$")
65 (line-to-here (buffer-substring (point-at-bol) (point))))
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"))
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) ?\[)
79 ((equal (char-before beg) ?\\)
81 ((string-match "\\`\\*+[ \t]+\\'"
82 (buffer-substring (point-at-bol) beg))
84 ((equal (char-before beg) ?*)
85 (cons "searchhead" nil))
86 ((and (equal (char-before beg1) ?:)
87 (equal (char-after (point-at-bol)) ?*))
89 ((and (equal (char-before beg1) ?:)
90 (not (equal (char-after (point-at-bol)) ?*))
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))))
99 ((and (equal (char-before beg1) ?:)
100 (not (equal (char-after (point-at-bol)) ?*)))
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)))
110 ((string= "file-option" (car thing))
112 (and (cdr thing) (concat "/" (downcase (cdr thing))))))
113 ((string= "block-option" (car thing))
114 (concat (car thing) "/" (downcase (cdr 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))
123 (narrow-to-region begin end)
125 (goto-char (point-min))
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))
133 (cons (reverse args) (reverse begins))))))
135 (defun org-pcomplete-initial ()
136 "Calls the right completion function for first argument completions."
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)
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)
156 (dolist (block-info org-element-block-name-alist block-names)
157 (let ((name (car block-info)))
158 (push (format "END_%s" name) block-names)
159 (push (concat "BEGIN_"
161 ;; Since language is compulsory in
162 ;; source blocks, add a space.
163 (and (equal name "SRC") " "))
165 (push (format "ATTR_%s: " name) block-names))))
166 (mapcar (lambda (keyword) (concat keyword ": "))
167 (org-get-export-keywords))))
168 (substring pcomplete-stub 2)))
170 (defun pcomplete/org-mode/file-option/author ()
171 "Complete arguments for the #+AUTHOR file option."
172 (pcomplete-here (list user-full-name)))
174 (defvar org-time-stamp-formats)
175 (defun pcomplete/org-mode/file-option/date ()
176 "Complete arguments for the #+DATE file option."
177 (pcomplete-here (list (format-time-string (car org-time-stamp-formats)))))
179 (defun pcomplete/org-mode/file-option/email ()
180 "Complete arguments for the #+EMAIL file option."
181 (pcomplete-here (list user-mail-address)))
183 (defvar org-export-exclude-tags)
184 (defun pcomplete/org-mode/file-option/exclude_tags ()
185 "Complete arguments for the #+EXCLUDE_TAGS file option."
188 (and org-export-exclude-tags
189 (list (mapconcat 'identity org-export-exclude-tags " ")))))
191 (defvar org-file-tags)
192 (defun pcomplete/org-mode/file-option/filetags ()
193 "Complete arguments for the #+FILETAGS file option."
194 (pcomplete-here (and org-file-tags (mapconcat 'identity org-file-tags " "))))
196 (defvar org-export-default-language)
197 (defun pcomplete/org-mode/file-option/language ()
198 "Complete arguments for the #+LANGUAGE file option."
201 (pcomplete-uniqify-list
202 (list org-export-default-language "en"))))
204 (defvar org-default-priority)
205 (defvar org-highest-priority)
206 (defvar org-lowest-priority)
207 (defun pcomplete/org-mode/file-option/priorities ()
208 "Complete arguments for the #+PRIORITIES file option."
209 (pcomplete-here (list (format "%c %c %c"
212 org-default-priority))))
214 (defvar org-export-select-tags)
215 (defun pcomplete/org-mode/file-option/select_tags ()
216 "Complete arguments for the #+SELECT_TAGS file option."
219 (and org-export-select-tags
220 (list (mapconcat 'identity org-export-select-tags " ")))))
222 (defvar org-startup-options)
223 (defun pcomplete/org-mode/file-option/startup ()
224 "Complete arguments for the #+STARTUP file option."
225 (while (pcomplete-here
226 (let ((opts (pcomplete-uniqify-list
227 (mapcar 'car org-startup-options))))
228 ;; Some options are mutually exclusive, and shouldn't be completed
229 ;; against if certain other options have already been seen.
230 (dolist (arg pcomplete-args)
232 ((string= arg "hidestars")
233 (setq opts (delete "showstars" opts)))))
236 (defvar org-tag-alist)
237 (defun pcomplete/org-mode/file-option/tags ()
238 "Complete arguments for the #+TAGS file option."
241 (mapconcat (lambda (x)
243 ((eq :startgroup (car x)) "{")
244 ((eq :endgroup (car x)) "}")
245 ((eq :grouptags (car x)) ":")
246 ((eq :newline (car x)) "\\n")
247 ((cdr x) (format "%s(%c)" (car x) (cdr x)))
249 org-tag-alist " "))))
251 (defun pcomplete/org-mode/file-option/title ()
252 "Complete arguments for the #+TITLE file option."
254 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
255 (list (or (and visited-file
256 (file-name-sans-extension
257 (file-name-nondirectory visited-file)))
258 (buffer-name (buffer-base-buffer)))))))
261 (declare-function org-export-backend-options "org-export" (cl-x))
262 (defun pcomplete/org-mode/file-option/options ()
263 "Complete arguments for the #+OPTIONS file option."
264 (while (pcomplete-here
265 (pcomplete-uniqify-list
267 ;; Hard-coded OPTION items always available.
268 '("H:" "\\n:" "num:" "timestamp:" "arch:" "author:" "c:"
269 "creator:" "date:" "d:" "email:" "*:" "e:" "::" "f:"
270 "inline:" "tex:" "p:" "pri:" "':" "-:" "stat:" "^:" "toc:"
271 "|:" "tags:" "tasks:" "<:" "todo:")
272 ;; OPTION items from registered back-ends.
274 (dolist (backend (org-bound-and-true-p
275 org-export--registered-backends))
276 (dolist (option (org-export-backend-options backend))
277 (let ((item (nth 2 option)))
278 (when item (push (concat item ":") items)))))
281 (defun pcomplete/org-mode/file-option/infojs_opt ()
282 "Complete arguments for the #+INFOJS_OPT file option."
283 (while (pcomplete-here
284 (pcomplete-uniqify-list
285 (mapcar (lambda (item) (format "%s:" (car item)))
286 (org-bound-and-true-p org-html-infojs-opts-table))))))
288 (defun pcomplete/org-mode/file-option/bind ()
289 "Complete arguments for the #+BIND file option, which are variable names."
292 (lambda (a) (if (boundp a) (setq vars (cons (symbol-name a) vars)))))
293 (pcomplete-here vars)))
295 (defvar org-link-abbrev-alist-local)
296 (defvar org-link-abbrev-alist)
297 (defun pcomplete/org-mode/link ()
298 "Complete against defined #+LINK patterns."
300 (pcomplete-uniqify-list
302 (append (mapcar 'car org-link-abbrev-alist-local)
303 (mapcar 'car org-link-abbrev-alist))))))
305 (defvar org-entities)
306 (defun pcomplete/org-mode/tex ()
307 "Complete against TeX-style HTML entity names."
308 (require 'org-entities)
309 (while (pcomplete-here
310 (pcomplete-uniqify-list (remove nil (mapcar 'car-safe org-entities)))
311 (substring pcomplete-stub 1))))
313 (defvar org-todo-keywords-1)
314 (defun pcomplete/org-mode/todo ()
315 "Complete against known TODO keywords."
316 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1))))
318 (defvar org-todo-line-regexp)
319 (defun pcomplete/org-mode/searchhead ()
320 "Complete against all headings.
321 This needs more work, to handle headings with lots of spaces in them."
325 (goto-char (point-min))
327 (while (re-search-forward org-todo-line-regexp nil t)
328 (push (org-make-org-heading-search-string
329 (match-string-no-properties 3))
331 (pcomplete-uniqify-list tbl)))
332 (substring pcomplete-stub 1))))
334 (defvar org-tag-alist)
335 (defun pcomplete/org-mode/tag ()
336 "Complete a tag name. Omit tags already set."
337 (while (pcomplete-here
340 (let ((lst (pcomplete-uniqify-list
344 (and (stringp (car x)) (car x)))
346 (mapcar 'car (org-get-buffer-tags))))))
347 (dolist (tag (org-get-tags))
348 (setq lst (delete tag lst)))
350 (and (string-match ".*:" pcomplete-stub)
351 (substring pcomplete-stub (match-end 0))))))
353 (defun pcomplete/org-mode/prop ()
354 "Complete a property name. Omit properties already set."
358 (let ((lst (pcomplete-uniqify-list
360 (org-buffer-property-keys nil t t)))))
361 (dolist (prop (org-entry-properties))
362 (setq lst (delete (car prop) lst)))
364 (substring pcomplete-stub 1)))
368 (defun pcomplete/org-mode/drawer ()
369 "Complete a drawer name."
370 (let ((spc (save-excursion
371 (move-beginning-of-line 1)
372 (looking-at "^\\([ \t]*\\):")
374 (cpllist (mapcar (lambda (x) (concat x ": ")) org-drawers)))
375 (pcomplete-here cpllist
376 (substring pcomplete-stub 1)
377 (unless (or (not (delq
380 (string-match (substring pcomplete-stub 1) x))
382 (looking-at "[ \t]*\n.*:END:"))
383 (save-excursion (insert "\n" spc ":END:"))))))
385 (defun pcomplete/org-mode/block-option/src ()
386 "Complete the arguments of a begin_src block.
387 Complete a language in the first field, the header arguments and switches."
390 (lambda(x) (symbol-name (nth 3 x)))
391 (cdr (car (cdr (memq :key-type (plist-get
393 'org-babel-load-languages)
395 (while (pcomplete-here
397 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
398 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
399 ":session" ":shebang" ":tangle" ":tangle-mode" ":var"))))
401 (defun pcomplete/org-mode/block-option/clocktable ()
402 "Complete keywords in a clocktable line."
403 (while (pcomplete-here '(":maxlevel" ":scope" ":lang"
404 ":tstart" ":tend" ":block" ":step"
405 ":stepskip0" ":fileskip0"
406 ":emphasize" ":link" ":narrow" ":indent"
407 ":tcolumns" ":level" ":compact" ":timestamp"
408 ":formula" ":formatter" ":wstart" ":mstart"))))
410 (defun org-pcomplete-case-double (list)
411 "Return list with both upcase and downcase version of all strings in LIST."
413 (while (setq e (pop list))
414 (setq res (cons (downcase e) (cons (upcase e) res))))
419 (provide 'org-pcomplete)
421 ;;; org-pcomplete.el ends here