org.el (org-time-stamp): C-u C-u C-c . inserts a timestamp with no prompt.
[org-mode.git] / lisp / org-pcomplete.el
blob3e029ff26c15bd3cbfcd1aa14db77d6da4a598dd
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 (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 (or (looking-back org-drawer-regexp)
95 (looking-back org-property-re))))
96 (cons "prop" nil))
97 ((and (equal (char-before beg1) ?:)
98 (not (equal (char-after (point-at-bol)) ?*)))
99 (cons "drawer" nil))
100 (t nil))))
102 (defun org-command-at-point ()
103 "Return the qualified name of the Org completion entity at point.
104 When completing for #+STARTUP, for example, this function returns
105 \"file-option/startup\"."
106 (let ((thing (org-thing-at-point)))
107 (cond
108 ((string= "file-option" (car thing))
109 (concat (car thing) "/" (downcase (cdr thing))))
110 ((string= "block-option" (car thing))
111 (concat (car thing) "/" (downcase (cdr thing))))
113 (car thing)))))
115 (defun org-parse-arguments ()
116 "Parse whitespace separated arguments in the current region."
117 (let ((begin (line-beginning-position))
118 (end (line-end-position))
119 begins args)
120 (save-restriction
121 (narrow-to-region begin end)
122 (save-excursion
123 (goto-char (point-min))
124 (while (not (eobp))
125 (skip-chars-forward " \t\n[")
126 (setq begins (cons (point) begins))
127 (skip-chars-forward "^ \t\n[")
128 (setq args (cons (buffer-substring-no-properties
129 (car begins) (point))
130 args)))
131 (cons (reverse args) (reverse begins))))))
134 (defun org-pcomplete-initial ()
135 "Calls the right completion function for first argument completions."
136 (ignore
137 (funcall (or (pcomplete-find-completion-function
138 (car (org-thing-at-point)))
139 pcomplete-default-completion-function))))
141 (defvar org-additional-option-like-keywords)
142 (defun pcomplete/org-mode/file-option ()
143 "Complete against all valid file options."
144 (require 'org-exp)
145 (pcomplete-here
146 (org-pcomplete-case-double
147 (mapcar (lambda (x)
148 (if (= ?: (aref x (1- (length x))))
149 (concat x " ")
151 (delq nil
152 (pcomplete-uniqify-list
153 (append
154 (mapcar (lambda (x)
155 (if (string-match "^#\\+\\([A-Z_]+:?\\)" x)
156 (match-string 1 x)))
157 (org-split-string (org-get-current-options) "\n"))
158 (copy-sequence org-additional-option-like-keywords))))))
159 (substring pcomplete-stub 2)))
161 (defvar org-startup-options)
162 (defun pcomplete/org-mode/file-option/startup ()
163 "Complete arguments for the #+STARTUP file option."
164 (while (pcomplete-here
165 (let ((opts (pcomplete-uniqify-list
166 (mapcar 'car org-startup-options))))
167 ;; Some options are mutually exclusive, and shouldn't be completed
168 ;; against if certain other options have already been seen.
169 (dolist (arg pcomplete-args)
170 (cond
171 ((string= arg "hidestars")
172 (setq opts (delete "showstars" opts)))))
173 opts))))
175 (defun pcomplete/org-mode/file-option/bind ()
176 "Complete arguments for the #+BIND file option, which are variable names"
177 (let (vars)
178 (mapatoms
179 (lambda (a) (if (boundp a) (setq vars (cons (symbol-name a) vars)))))
180 (pcomplete-here vars)))
182 (defvar org-link-abbrev-alist-local)
183 (defvar org-link-abbrev-alist)
184 (defun pcomplete/org-mode/link ()
185 "Complete against defined #+LINK patterns."
186 (pcomplete-here
187 (pcomplete-uniqify-list
188 (copy-sequence
189 (append (mapcar 'car org-link-abbrev-alist-local)
190 (mapcar 'car org-link-abbrev-alist))))))
192 (defvar org-entities)
193 (defun pcomplete/org-mode/tex ()
194 "Complete against TeX-style HTML entity names."
195 (require 'org-entities)
196 (while (pcomplete-here
197 (pcomplete-uniqify-list (remove nil (mapcar 'car-safe org-entities)))
198 (substring pcomplete-stub 1))))
200 (defvar org-todo-keywords-1)
201 (defun pcomplete/org-mode/todo ()
202 "Complete against known TODO keywords."
203 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1))))
205 (defvar org-todo-line-regexp)
206 (defun pcomplete/org-mode/searchhead ()
207 "Complete against all headings.
208 This needs more work, to handle headings with lots of spaces in them."
209 (while
210 (pcomplete-here
211 (save-excursion
212 (goto-char (point-min))
213 (let (tbl)
214 (while (re-search-forward org-todo-line-regexp nil t)
215 (push (org-make-org-heading-search-string
216 (match-string-no-properties 3) t)
217 tbl))
218 (pcomplete-uniqify-list tbl)))
219 (substring pcomplete-stub 1))))
221 (defvar org-tag-alist)
222 (defun pcomplete/org-mode/tag ()
223 "Complete a tag name. Omit tags already set."
224 (while (pcomplete-here
225 (mapcar (lambda (x)
226 (concat x ":"))
227 (let ((lst (pcomplete-uniqify-list
228 (or (remove
230 (mapcar (lambda (x)
231 (and (stringp (car x)) (car x)))
232 org-tag-alist))
233 (mapcar 'car (org-get-buffer-tags))))))
234 (dolist (tag (org-get-tags))
235 (setq lst (delete tag lst)))
236 lst))
237 (and (string-match ".*:" pcomplete-stub)
238 (substring pcomplete-stub (match-end 0))))))
240 (defun pcomplete/org-mode/prop ()
241 "Complete a property name. Omit properties already set."
242 (pcomplete-here
243 (mapcar (lambda (x)
244 (concat x ": "))
245 (let ((lst (pcomplete-uniqify-list
246 (copy-sequence
247 (org-buffer-property-keys nil t t)))))
248 (dolist (prop (org-entry-properties))
249 (setq lst (delete (car prop) lst)))
250 lst))
251 (substring pcomplete-stub 1)))
253 (defvar org-drawers)
255 (defun pcomplete/org-mode/drawer ()
256 "Complete a drawer name."
257 (let ((spc (save-excursion
258 (move-beginning-of-line 1)
259 (looking-at "^\\([ \t]*\\):")
260 (match-string 1)))
261 (cpllist (mapcar (lambda (x) (concat x ": ")) org-drawers)))
262 (pcomplete-here cpllist
263 (substring pcomplete-stub 1)
264 (unless (or (not (delete
266 (mapcar (lambda(x)
267 (string-match (substring pcomplete-stub 1) x))
268 cpllist)))
269 (looking-at "[ \t]*\n.*:END:"))
270 (save-excursion (insert "\n" spc ":END:"))))))
272 (defun pcomplete/org-mode/block-option/src ()
273 "Complete the arguments of a begin_src block.
274 Complete a language in the first field, the header arguments and switches."
275 (pcomplete-here
276 (mapcar
277 (lambda(x) (symbol-name (nth 3 x)))
278 (cdr (car (cdr (memq :key-type (plist-get
279 (symbol-plist
280 'org-babel-load-languages)
281 'custom-type)))))))
282 (while (pcomplete-here
283 '("-n" "-r" "-l"
284 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
285 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
286 ":session" ":shebang" ":tangle" ":var"))))
288 (defun pcomplete/org-mode/block-option/clocktable ()
289 "Complete keywords in a clocktable line"
290 (while (pcomplete-here '(":maxlevel" ":scope"
291 ":tstart" ":tend" ":block" ":step"
292 ":stepskip0" ":fileskip0"
293 ":emphasize" ":link" ":narrow" ":indent"
294 ":tcolumns" ":level" ":compact" ":timestamp"
295 ":formula" ":formatter"))))
297 (defun org-pcomplete-case-double (list)
298 "Return list with both upcase and downcase version of all strings in LIST."
299 (let (e res)
300 (while (setq e (pop list))
301 (setq res (cons (downcase e) (cons (upcase e) res))))
302 (nreverse res)))
304 ;;;; Finish up
306 (provide 'org-pcomplete)
308 ;;; org-pcomplete.el ends here