test-org.el (test-org/forward-element, test-org/backward-element): Use `regexp-quote'
[org-mode.git] / lisp / org-pcomplete.el
blob3f445f11ccb18085a59500903223ef87a72ab87c
1 ;;; org-pcomplete.el --- In-buffer completion code
3 ;; Copyright (C) 2004-2013 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 heading))
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) "/" (downcase (cdr thing))))
112 ((string= "block-option" (car thing))
113 (concat (car thing) "/" (downcase (cdr thing))))
115 (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 (pcomplete-here
149 (org-pcomplete-case-double
150 (mapcar (lambda (x)
151 (if (= ?: (aref x (1- (length x))))
152 (concat x " ")
154 (append org-options-keywords
155 org-element-affiliated-keywords
156 (let (block-names)
157 (mapc (lambda (block-name)
158 (let ((name (car block-name)))
159 (push (concat "END_" name) block-names)
160 (push (concat "BEGIN_" name) block-names)))
161 org-element-block-name-alist)
162 block-names)
163 (mapcar (lambda (keyword) (concat keyword ":"))
164 (org-get-export-keywords)))))
165 (substring pcomplete-stub 2)))
167 (defvar org-startup-options)
168 (defun pcomplete/org-mode/file-option/startup ()
169 "Complete arguments for the #+STARTUP file option."
170 (while (pcomplete-here
171 (let ((opts (pcomplete-uniqify-list
172 (mapcar 'car org-startup-options))))
173 ;; Some options are mutually exclusive, and shouldn't be completed
174 ;; against if certain other options have already been seen.
175 (dolist (arg pcomplete-args)
176 (cond
177 ((string= arg "hidestars")
178 (setq opts (delete "showstars" opts)))))
179 opts))))
181 (defun pcomplete/org-mode/file-option/options ()
182 "Complete arguments for the #+OPTIONS file option."
183 (while (pcomplete-here
184 (pcomplete-uniqify-list
185 (append
186 ;; Hard-coded OPTION items always available.
187 '("H:" "\\n:" "num:" "timestamp:" "arch:" "author:" "c:"
188 "creator:" "date:" "d:" "email:" "*:" "e:" "::" "f:"
189 "inline:" "tex:" "p:" "pri:" "':" "-:" "stat:" "^:" "toc:"
190 "|:" "tags:" "tasks:" "<:" "todo:")
191 ;; OPTION items from registered back-ends.
192 (let (items)
193 (dolist (back-end (org-bound-and-true-p
194 org-export-registered-backends))
195 (dolist (option (plist-get (cdr back-end) :options-alist))
196 (let ((item (nth 2 option)))
197 (when item (push (concat item ":") items)))))
198 items))))))
200 (defun pcomplete/org-mode/file-option/infojs_opt ()
201 "Complete arguments for the #+INFOJS_OPT file option."
202 (while (pcomplete-here
203 (pcomplete-uniqify-list
204 (mapcar (lambda (item) (format "%s:" (car item)))
205 (org-bound-and-true-p org-infojs-opts-table))))))
207 (defun pcomplete/org-mode/file-option/bind ()
208 "Complete arguments for the #+BIND file option, which are variable names."
209 (let (vars)
210 (mapatoms
211 (lambda (a) (if (boundp a) (setq vars (cons (symbol-name a) vars)))))
212 (pcomplete-here vars)))
214 (defvar org-link-abbrev-alist-local)
215 (defvar org-link-abbrev-alist)
216 (defun pcomplete/org-mode/link ()
217 "Complete against defined #+LINK patterns."
218 (pcomplete-here
219 (pcomplete-uniqify-list
220 (copy-sequence
221 (append (mapcar 'car org-link-abbrev-alist-local)
222 (mapcar 'car org-link-abbrev-alist))))))
224 (defvar org-entities)
225 (defun pcomplete/org-mode/tex ()
226 "Complete against TeX-style HTML entity names."
227 (require 'org-entities)
228 (while (pcomplete-here
229 (pcomplete-uniqify-list (remove nil (mapcar 'car-safe org-entities)))
230 (substring pcomplete-stub 1))))
232 (defvar org-todo-keywords-1)
233 (defun pcomplete/org-mode/todo ()
234 "Complete against known TODO keywords."
235 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1))))
237 (defvar org-todo-line-regexp)
238 (defun pcomplete/org-mode/searchhead ()
239 "Complete against all headings.
240 This needs more work, to handle headings with lots of spaces in them."
241 (while
242 (pcomplete-here
243 (save-excursion
244 (goto-char (point-min))
245 (let (tbl)
246 (while (re-search-forward org-todo-line-regexp nil t)
247 (push (org-make-org-heading-search-string
248 (match-string-no-properties 3) t)
249 tbl))
250 (pcomplete-uniqify-list tbl)))
251 (substring pcomplete-stub 1))))
253 (defvar org-tag-alist)
254 (defun pcomplete/org-mode/tag ()
255 "Complete a tag name. Omit tags already set."
256 (while (pcomplete-here
257 (mapcar (lambda (x)
258 (concat x ":"))
259 (let ((lst (pcomplete-uniqify-list
260 (or (remove
262 (mapcar (lambda (x)
263 (and (stringp (car x)) (car x)))
264 org-tag-alist))
265 (mapcar 'car (org-get-buffer-tags))))))
266 (dolist (tag (org-get-tags))
267 (setq lst (delete tag lst)))
268 lst))
269 (and (string-match ".*:" pcomplete-stub)
270 (substring pcomplete-stub (match-end 0))))))
272 (defun pcomplete/org-mode/prop ()
273 "Complete a property name. Omit properties already set."
274 (pcomplete-here
275 (mapcar (lambda (x)
276 (concat x ": "))
277 (let ((lst (pcomplete-uniqify-list
278 (copy-sequence
279 (org-buffer-property-keys nil t t)))))
280 (dolist (prop (org-entry-properties))
281 (setq lst (delete (car prop) lst)))
282 lst))
283 (substring pcomplete-stub 1)))
285 (defvar org-drawers)
287 (defun pcomplete/org-mode/drawer ()
288 "Complete a drawer name."
289 (let ((spc (save-excursion
290 (move-beginning-of-line 1)
291 (looking-at "^\\([ \t]*\\):")
292 (match-string 1)))
293 (cpllist (mapcar (lambda (x) (concat x ": ")) org-drawers)))
294 (pcomplete-here cpllist
295 (substring pcomplete-stub 1)
296 (unless (or (not (delq
298 (mapcar (lambda(x)
299 (string-match (substring pcomplete-stub 1) x))
300 cpllist)))
301 (looking-at "[ \t]*\n.*:END:"))
302 (save-excursion (insert "\n" spc ":END:"))))))
304 (defun pcomplete/org-mode/block-option/src ()
305 "Complete the arguments of a begin_src block.
306 Complete a language in the first field, the header arguments and switches."
307 (pcomplete-here
308 (mapcar
309 (lambda(x) (symbol-name (nth 3 x)))
310 (cdr (car (cdr (memq :key-type (plist-get
311 (symbol-plist
312 'org-babel-load-languages)
313 'custom-type)))))))
314 (while (pcomplete-here
315 '("-n" "-r" "-l"
316 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
317 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
318 ":session" ":shebang" ":tangle" ":var"))))
320 (defun pcomplete/org-mode/block-option/clocktable ()
321 "Complete keywords in a clocktable line."
322 (while (pcomplete-here '(":maxlevel" ":scope"
323 ":tstart" ":tend" ":block" ":step"
324 ":stepskip0" ":fileskip0"
325 ":emphasize" ":link" ":narrow" ":indent"
326 ":tcolumns" ":level" ":compact" ":timestamp"
327 ":formula" ":formatter"))))
329 (defun org-pcomplete-case-double (list)
330 "Return list with both upcase and downcase version of all strings in LIST."
331 (let (e res)
332 (while (setq e (pop list))
333 (setq res (cons (downcase e) (cons (upcase e) res))))
334 (nreverse res)))
336 ;;;; Finish up
338 (provide 'org-pcomplete)
340 ;;; org-pcomplete.el ends here