]> code.delx.au - gnu-emacs/blob - lisp/org/org-pcomplete.el
Merge from trunk.
[gnu-emacs] / lisp / org / org-pcomplete.el
1 ;;; org-pcomplete.el --- In-buffer completion code
2
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.
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)
34 (require 'org-compat)
35 (require 'pcomplete)
36
37 (declare-function org-split-string "org" (string &optional separators))
38 (declare-function org-get-current-options "org-exp" ())
39 (declare-function org-make-org-heading-search-string "org"
40 (&optional string heading))
41 (declare-function org-get-buffer-tags "org" ())
42 (declare-function org-get-tags "org" ())
43 (declare-function org-buffer-property-keys "org"
44 (&optional include-specials include-defaults include-columns))
45 (declare-function org-entry-properties "org" (&optional pom which specific))
46
47 ;;;; Customization variables
48
49 ;; Unused. Cf org-completion.
50 (defgroup org-complete nil
51 "Outline-based notes management and organizer."
52 :tag "Org"
53 :group 'org)
54
55 (defvar org-drawer-regexp)
56 (defvar org-property-re)
57
58 (defun org-thing-at-point ()
59 "Examine the thing at point and let the caller know what it is.
60 The return value is a string naming the thing at point."
61 (let ((beg1 (save-excursion
62 (skip-chars-backward (org-re "[:alnum:]-_@"))
63 (point)))
64 (beg (save-excursion
65 (skip-chars-backward "a-zA-Z0-9-_:$")
66 (point)))
67 (line-to-here (buffer-substring (point-at-bol) (point))))
68 (cond
69 ((string-match "\\`[ \t]*#\\+begin: clocktable[ \t]+" line-to-here)
70 (cons "block-option" "clocktable"))
71 ((string-match "\\`[ \t]*#\\+begin_src[ \t]+" line-to-here)
72 (cons "block-option" "src"))
73 ((save-excursion
74 (re-search-backward "^[ \t]*#\\+\\([A-Z_]+\\):.*"
75 (line-beginning-position) t))
76 (cons "file-option" (match-string-no-properties 1)))
77 ((string-match "\\`[ \t]*#\\+[a-zA-Z_]*\\'" line-to-here)
78 (cons "file-option" nil))
79 ((equal (char-before beg) ?\[)
80 (cons "link" nil))
81 ((equal (char-before beg) ?\\)
82 (cons "tex" nil))
83 ((string-match "\\`\\*+[ \t]+\\'"
84 (buffer-substring (point-at-bol) beg))
85 (cons "todo" nil))
86 ((equal (char-before beg) ?*)
87 (cons "searchhead" nil))
88 ((and (equal (char-before beg1) ?:)
89 (equal (char-after (point-at-bol)) ?*))
90 (cons "tag" nil))
91 ((and (equal (char-before beg1) ?:)
92 (not (equal (char-after (point-at-bol)) ?*))
93 (save-excursion
94 (move-beginning-of-line 1)
95 (skip-chars-backward "[ \t\n]")
96 ;; org-drawer-regexp matches a whole line but while
97 ;; looking-back, we just ignore trailing whitespaces
98 (or (org-looking-back (substring org-drawer-regexp 0 -1))
99 (org-looking-back org-property-re))))
100 (cons "prop" nil))
101 ((and (equal (char-before beg1) ?:)
102 (not (equal (char-after (point-at-bol)) ?*)))
103 (cons "drawer" nil))
104 (t nil))))
105
106 (defun org-command-at-point ()
107 "Return the qualified name of the Org completion entity at point.
108 When completing for #+STARTUP, for example, this function returns
109 \"file-option/startup\"."
110 (let ((thing (org-thing-at-point)))
111 (cond
112 ((string= "file-option" (car thing))
113 (concat (car thing) "/" (downcase (cdr thing))))
114 ((string= "block-option" (car thing))
115 (concat (car thing) "/" (downcase (cdr thing))))
116 (t
117 (car thing)))))
118
119 (defun org-parse-arguments ()
120 "Parse whitespace separated arguments in the current region."
121 (let ((begin (line-beginning-position))
122 (end (line-end-position))
123 begins args)
124 (save-restriction
125 (narrow-to-region begin end)
126 (save-excursion
127 (goto-char (point-min))
128 (while (not (eobp))
129 (skip-chars-forward " \t\n[")
130 (setq begins (cons (point) begins))
131 (skip-chars-forward "^ \t\n[")
132 (setq args (cons (buffer-substring-no-properties
133 (car begins) (point))
134 args)))
135 (cons (reverse args) (reverse begins))))))
136
137 (defun org-pcomplete-initial ()
138 "Calls the right completion function for first argument completions."
139 (ignore
140 (funcall (or (pcomplete-find-completion-function
141 (car (org-thing-at-point)))
142 pcomplete-default-completion-function))))
143
144 (defvar org-options-keywords) ; From org.el
145 (defvar org-additional-option-like-keywords) ; From org.el
146 (defun pcomplete/org-mode/file-option ()
147 "Complete against all valid file options."
148 (require 'org-exp)
149 (pcomplete-here
150 (org-pcomplete-case-double
151 (mapcar (lambda (x)
152 (if (= ?: (aref x (1- (length x))))
153 (concat x " ")
154 x))
155 (append org-options-keywords
156 org-additional-option-like-keywords)))
157 (substring pcomplete-stub 2)))
158
159 (defvar org-startup-options)
160 (defun pcomplete/org-mode/file-option/startup ()
161 "Complete arguments for the #+STARTUP file option."
162 (while (pcomplete-here
163 (let ((opts (pcomplete-uniqify-list
164 (mapcar 'car org-startup-options))))
165 ;; Some options are mutually exclusive, and shouldn't be completed
166 ;; against if certain other options have already been seen.
167 (dolist (arg pcomplete-args)
168 (cond
169 ((string= arg "hidestars")
170 (setq opts (delete "showstars" opts)))))
171 opts))))
172
173 (defmacro pcomplete/org-mode/file-option/x (option)
174 "Complete arguments for OPTION."
175 `(while
176 (pcomplete-here
177 (pcomplete-uniqify-list
178 (delq nil
179 (mapcar (lambda(o)
180 (when (string-match (concat "^[ \t]*#\\+"
181 ,option ":[ \t]+\\(.*\\)[ \t]*$") o)
182 (match-string 1 o)))
183 (split-string (org-get-current-options) "\n")))))))
184
185 (defun pcomplete/org-mode/file-option/options ()
186 "Complete arguments for the #+OPTIONS file option."
187 (pcomplete/org-mode/file-option/x "OPTIONS"))
188
189 (defun pcomplete/org-mode/file-option/title ()
190 "Complete arguments for the #+TITLE file option."
191 (pcomplete/org-mode/file-option/x "TITLE"))
192
193 (defun pcomplete/org-mode/file-option/author ()
194 "Complete arguments for the #+AUTHOR file option."
195 (pcomplete/org-mode/file-option/x "AUTHOR"))
196
197 (defun pcomplete/org-mode/file-option/email ()
198 "Complete arguments for the #+EMAIL file option."
199 (pcomplete/org-mode/file-option/x "EMAIL"))
200
201 (defun pcomplete/org-mode/file-option/date ()
202 "Complete arguments for the #+DATE file option."
203 (pcomplete/org-mode/file-option/x "DATE"))
204
205 (defun pcomplete/org-mode/file-option/bind ()
206 "Complete arguments for the #+BIND file option, which are variable names."
207 (let (vars)
208 (mapatoms
209 (lambda (a) (if (boundp a) (setq vars (cons (symbol-name a) vars)))))
210 (pcomplete-here vars)))
211
212 (defvar org-link-abbrev-alist-local)
213 (defvar org-link-abbrev-alist)
214 (defun pcomplete/org-mode/link ()
215 "Complete against defined #+LINK patterns."
216 (pcomplete-here
217 (pcomplete-uniqify-list
218 (copy-sequence
219 (append (mapcar 'car org-link-abbrev-alist-local)
220 (mapcar 'car org-link-abbrev-alist))))))
221
222 (defvar org-entities)
223 (defun pcomplete/org-mode/tex ()
224 "Complete against TeX-style HTML entity names."
225 (require 'org-entities)
226 (while (pcomplete-here
227 (pcomplete-uniqify-list (remove nil (mapcar 'car-safe org-entities)))
228 (substring pcomplete-stub 1))))
229
230 (defvar org-todo-keywords-1)
231 (defun pcomplete/org-mode/todo ()
232 "Complete against known TODO keywords."
233 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1))))
234
235 (defvar org-todo-line-regexp)
236 (defun pcomplete/org-mode/searchhead ()
237 "Complete against all headings.
238 This needs more work, to handle headings with lots of spaces in them."
239 (while
240 (pcomplete-here
241 (save-excursion
242 (goto-char (point-min))
243 (let (tbl)
244 (while (re-search-forward org-todo-line-regexp nil t)
245 (push (org-make-org-heading-search-string
246 (match-string-no-properties 3) t)
247 tbl))
248 (pcomplete-uniqify-list tbl)))
249 (substring pcomplete-stub 1))))
250
251 (defvar org-tag-alist)
252 (defun pcomplete/org-mode/tag ()
253 "Complete a tag name. Omit tags already set."
254 (while (pcomplete-here
255 (mapcar (lambda (x)
256 (concat x ":"))
257 (let ((lst (pcomplete-uniqify-list
258 (or (remove
259 nil
260 (mapcar (lambda (x)
261 (and (stringp (car x)) (car x)))
262 org-tag-alist))
263 (mapcar 'car (org-get-buffer-tags))))))
264 (dolist (tag (org-get-tags))
265 (setq lst (delete tag lst)))
266 lst))
267 (and (string-match ".*:" pcomplete-stub)
268 (substring pcomplete-stub (match-end 0))))))
269
270 (defun pcomplete/org-mode/prop ()
271 "Complete a property name. Omit properties already set."
272 (pcomplete-here
273 (mapcar (lambda (x)
274 (concat x ": "))
275 (let ((lst (pcomplete-uniqify-list
276 (copy-sequence
277 (org-buffer-property-keys nil t t)))))
278 (dolist (prop (org-entry-properties))
279 (setq lst (delete (car prop) lst)))
280 lst))
281 (substring pcomplete-stub 1)))
282
283 (defvar org-drawers)
284
285 (defun pcomplete/org-mode/drawer ()
286 "Complete a drawer name."
287 (let ((spc (save-excursion
288 (move-beginning-of-line 1)
289 (looking-at "^\\([ \t]*\\):")
290 (match-string 1)))
291 (cpllist (mapcar (lambda (x) (concat x ": ")) org-drawers)))
292 (pcomplete-here cpllist
293 (substring pcomplete-stub 1)
294 (unless (or (not (delete
295 nil
296 (mapcar (lambda(x)
297 (string-match (substring pcomplete-stub 1) x))
298 cpllist)))
299 (looking-at "[ \t]*\n.*:END:"))
300 (save-excursion (insert "\n" spc ":END:"))))))
301
302 (defun pcomplete/org-mode/block-option/src ()
303 "Complete the arguments of a begin_src block.
304 Complete a language in the first field, the header arguments and switches."
305 (pcomplete-here
306 (mapcar
307 (lambda(x) (symbol-name (nth 3 x)))
308 (cdr (car (cdr (memq :key-type (plist-get
309 (symbol-plist
310 'org-babel-load-languages)
311 'custom-type)))))))
312 (while (pcomplete-here
313 '("-n" "-r" "-l"
314 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
315 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
316 ":session" ":shebang" ":tangle" ":var"))))
317
318 (defun pcomplete/org-mode/block-option/clocktable ()
319 "Complete keywords in a clocktable line."
320 (while (pcomplete-here '(":maxlevel" ":scope"
321 ":tstart" ":tend" ":block" ":step"
322 ":stepskip0" ":fileskip0"
323 ":emphasize" ":link" ":narrow" ":indent"
324 ":tcolumns" ":level" ":compact" ":timestamp"
325 ":formula" ":formatter"))))
326
327 (defun org-pcomplete-case-double (list)
328 "Return list with both upcase and downcase version of all strings in LIST."
329 (let (e res)
330 (while (setq e (pop list))
331 (setq res (cons (downcase e) (cons (upcase e) res))))
332 (nreverse res)))
333
334 ;;;; Finish up
335
336 (provide 'org-pcomplete)
337
338 ;;; org-pcomplete.el ends here