Replace xref-match-bounds with xref-match-length
[emacs.git] / lisp / org / org-pcomplete.el
blob30d3b1cab25c32936ac8f2edae6330d794206956
1 ;;; org-pcomplete.el --- In-buffer completion code
3 ;; Copyright (C) 2004-2015 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 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."
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 (org-looking-back org-property-re))))
98 (cons "prop" nil))
99 ((and (equal (char-before beg1) ?:)
100 (not (equal (char-after (point-at-bol)) ?*)))
101 (cons "drawer" nil))
102 (t nil))))
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)))
109 (cond
110 ((string= "file-option" (car thing))
111 (concat (car thing)
112 (and (cdr thing) (concat "/" (downcase (cdr thing))))))
113 ((string= "block-option" (car thing))
114 (concat (car thing) "/" (downcase (cdr thing))))
115 (t (car 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))
121 begins args)
122 (save-restriction
123 (narrow-to-region begin end)
124 (save-excursion
125 (goto-char (point-min))
126 (while (not (eobp))
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))
132 args)))
133 (cons (reverse args) (reverse begins))))))
135 (defun org-pcomplete-initial ()
136 "Calls the right completion function for first argument completions."
137 (ignore
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)
149 (pcomplete-here
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)
155 (let (block-names)
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_"
160 name
161 ;; Since language is compulsory in
162 ;; source blocks, add a space.
163 (and (equal name "SRC") " "))
164 block-names)
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."
186 (require 'ox)
187 (pcomplete-here
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."
199 (require 'ox)
200 (pcomplete-here
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"
210 org-highest-priority
211 org-lowest-priority
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."
217 (require 'ox)
218 (pcomplete-here
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)
231 (cond
232 ((string= arg "hidestars")
233 (setq opts (delete "showstars" opts)))))
234 opts))))
236 (defvar org-tag-alist)
237 (defun pcomplete/org-mode/file-option/tags ()
238 "Complete arguments for the #+TAGS file option."
239 (pcomplete-here
240 (list
241 (mapconcat (lambda (x)
242 (cond
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)))
248 (t (car x))))
249 org-tag-alist " "))))
251 (defun pcomplete/org-mode/file-option/title ()
252 "Complete arguments for the #+TITLE file option."
253 (pcomplete-here
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
266 (append
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.
273 (let (items)
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)))))
279 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."
290 (let (vars)
291 (mapatoms
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."
299 (pcomplete-here
300 (pcomplete-uniqify-list
301 (copy-sequence
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."
322 (while
323 (pcomplete-here
324 (save-excursion
325 (goto-char (point-min))
326 (let (tbl)
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))
330 tbl))
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
338 (mapcar (lambda (x)
339 (concat x ":"))
340 (let ((lst (pcomplete-uniqify-list
341 (or (remove
343 (mapcar (lambda (x)
344 (and (stringp (car x)) (car x)))
345 org-tag-alist))
346 (mapcar 'car (org-get-buffer-tags))))))
347 (dolist (tag (org-get-tags))
348 (setq lst (delete tag lst)))
349 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."
355 (pcomplete-here
356 (mapcar (lambda (x)
357 (concat x ": "))
358 (let ((lst (pcomplete-uniqify-list
359 (copy-sequence
360 (org-buffer-property-keys nil t t)))))
361 (dolist (prop (org-entry-properties))
362 (setq lst (delete (car prop) lst)))
363 lst))
364 (substring pcomplete-stub 1)))
366 (defvar org-drawers)
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]*\\):")
373 (match-string 1)))
374 (cpllist (mapcar (lambda (x) (concat x ": ")) org-drawers)))
375 (pcomplete-here cpllist
376 (substring pcomplete-stub 1)
377 (unless (or (not (delq
379 (mapcar (lambda(x)
380 (string-match (substring pcomplete-stub 1) x))
381 cpllist)))
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."
388 (pcomplete-here
389 (mapcar
390 (lambda(x) (symbol-name (nth 3 x)))
391 (cdr (car (cdr (memq :key-type (plist-get
392 (symbol-plist
393 'org-babel-load-languages)
394 'custom-type)))))))
395 (while (pcomplete-here
396 '("-n" "-r" "-l"
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."
412 (let (e res)
413 (while (setq e (pop list))
414 (setq res (cons (downcase e) (cons (upcase e) res))))
415 (nreverse res)))
417 ;;;; Finish up
419 (provide 'org-pcomplete)
421 ;;; org-pcomplete.el ends here