Commit | Line | Data |
---|---|---|
3ab2c837 BG |
1 | ;;; org-pcomplete.el --- In-buffer completion code |
2 | ||
ba318903 | 3 | ;; Copyright (C) 2004-2014 Free Software Foundation, Inc. |
3ab2c837 BG |
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 | |
3ab2c837 BG |
9 | ;; |
10 | ;; This file is part of GNU Emacs. | |
11 | ;; | |
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. | |
16 | ||
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. | |
21 | ||
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 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
25 | ;; | |
26 | ;;; Code: | |
27 | ||
28 | ;;;; Require other packages | |
29 | ||
30 | (eval-when-compile | |
31 | (require 'cl)) | |
32 | ||
33 | (require 'org-macs) | |
8223b1d2 | 34 | (require 'org-compat) |
3ab2c837 BG |
35 | (require 'pcomplete) |
36 | ||
37 | (declare-function org-split-string "org" (string &optional separators)) | |
3ab2c837 | 38 | (declare-function org-make-org-heading-search-string "org" |
271672fa | 39 | (&optional string)) |
3ab2c837 BG |
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)) | |
45 | ||
46 | ;;;; Customization variables | |
47 | ||
48 | (defgroup org-complete nil | |
49 | "Outline-based notes management and organizer." | |
50 | :tag "Org" | |
51 | :group 'org) | |
52 | ||
8223b1d2 BG |
53 | (defvar org-drawer-regexp) |
54 | (defvar org-property-re) | |
55 | ||
3ab2c837 BG |
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 | |
8223b1d2 | 60 | (skip-chars-backward (org-re "[:alnum:]-_@")) |
3ab2c837 BG |
61 | (point))) |
62 | (beg (save-excursion | |
8223b1d2 | 63 | (skip-chars-backward "a-zA-Z0-9-_:$") |
3ab2c837 BG |
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))) | |
801a68c8 | 75 | ((string-match "\\`[ \t]*#\\+[a-zA-Z_]*\\'" line-to-here) |
3ab2c837 BG |
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) ?:) | |
8223b1d2 BG |
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)))) | |
3ab2c837 | 98 | (cons "prop" nil)) |
8223b1d2 BG |
99 | ((and (equal (char-before beg1) ?:) |
100 | (not (equal (char-after (point-at-bol)) ?*))) | |
101 | (cons "drawer" nil)) | |
3ab2c837 BG |
102 | (t nil)))) |
103 | ||
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)) | |
271672fa BG |
111 | (concat (car thing) |
112 | (and (cdr thing) (concat "/" (downcase (cdr thing)))))) | |
3ab2c837 BG |
113 | ((string= "block-option" (car thing)) |
114 | (concat (car thing) "/" (downcase (cdr thing)))) | |
271672fa | 115 | (t (car thing))))) |
3ab2c837 BG |
116 | |
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)))))) | |
134 | ||
3ab2c837 BG |
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)))) | |
141 | ||
271672fa BG |
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" ()) | |
3ab2c837 BG |
146 | (defun pcomplete/org-mode/file-option () |
147 | "Complete against all valid file options." | |
271672fa | 148 | (require 'org-element) |
3ab2c837 BG |
149 | (pcomplete-here |
150 | (org-pcomplete-case-double | |
271672fa BG |
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)))) | |
3ab2c837 | 168 | (substring pcomplete-stub 2))) |
14e1337f | 169 | |
271672fa BG |
170 | (defun pcomplete/org-mode/file-option/author () |
171 | "Complete arguments for the #+AUTHOR file option." | |
172 | (pcomplete-here (list user-full-name))) | |
173 | ||
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))))) | |
178 | ||
179 | (defun pcomplete/org-mode/file-option/email () | |
180 | "Complete arguments for the #+EMAIL file option." | |
181 | (pcomplete-here (list user-mail-address))) | |
182 | ||
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 " "))))) | |
190 | ||
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 " ")))) | |
195 | ||
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")))) | |
203 | ||
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)))) | |
213 | ||
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 " "))))) | |
221 | ||
3ab2c837 BG |
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)))) | |
235 | ||
271672fa BG |
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 " ")))) | |
8223b1d2 BG |
250 | |
251 | (defun pcomplete/org-mode/file-option/title () | |
252 | "Complete arguments for the #+TITLE file option." | |
271672fa BG |
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))))))) | |
8223b1d2 | 259 | |
8223b1d2 | 260 | |
271672fa BG |
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)))))) | |
280 | ||
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)))))) | |
8223b1d2 | 287 | |
3ab2c837 | 288 | (defun pcomplete/org-mode/file-option/bind () |
8223b1d2 | 289 | "Complete arguments for the #+BIND file option, which are variable names." |
3ab2c837 BG |
290 | (let (vars) |
291 | (mapatoms | |
292 | (lambda (a) (if (boundp a) (setq vars (cons (symbol-name a) vars))))) | |
293 | (pcomplete-here vars))) | |
294 | ||
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)))))) | |
304 | ||
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)))) | |
312 | ||
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)))) | |
317 | ||
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 | |
8223b1d2 BG |
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 | |
271672fa | 329 | (match-string-no-properties 3)) |
8223b1d2 BG |
330 | tbl)) |
331 | (pcomplete-uniqify-list tbl))) | |
332 | (substring pcomplete-stub 1)))) | |
3ab2c837 BG |
333 | |
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 | |
342 | nil | |
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)))))) | |
352 | ||
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))) | |
365 | ||
8223b1d2 BG |
366 | (defvar org-drawers) |
367 | ||
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) | |
271672fa | 377 | (unless (or (not (delq |
8223b1d2 BG |
378 | nil |
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:")))))) | |
384 | ||
3ab2c837 BG |
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" | |
271672fa | 399 | ":session" ":shebang" ":tangle" ":tangle-mode" ":var")))) |
3ab2c837 BG |
400 | |
401 | (defun pcomplete/org-mode/block-option/clocktable () | |
8223b1d2 | 402 | "Complete keywords in a clocktable line." |
271672fa | 403 | (while (pcomplete-here '(":maxlevel" ":scope" ":lang" |
3ab2c837 BG |
404 | ":tstart" ":tend" ":block" ":step" |
405 | ":stepskip0" ":fileskip0" | |
406 | ":emphasize" ":link" ":narrow" ":indent" | |
407 | ":tcolumns" ":level" ":compact" ":timestamp" | |
271672fa | 408 | ":formula" ":formatter" ":wstart" ":mstart")))) |
3ab2c837 BG |
409 | |
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))) | |
416 | ||
417 | ;;;; Finish up | |
418 | ||
419 | (provide 'org-pcomplete) | |
420 | ||
3ab2c837 | 421 | ;;; org-pcomplete.el ends here |