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