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