Refill some long/short copyright headers.
[bpt/emacs.git] / lisp / org / org-complete.el
CommitLineData
acedf35c
CD
1;;; org-complete.el --- In-buffer completion code
2
95df8112 3;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
acedf35c
CD
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;; Version: 7.4
10;;
11;; This file is part of GNU Emacs.
12;;
13;; GNU Emacs is free software: you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26;;
27;;; Code:
28
29;;;; Require other packages
30
31(eval-when-compile
32 (require 'cl))
33
34(require 'org-macs)
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(defgroup org-complete nil
50 "Outline-based notes management and organizer."
51 :tag "Org"
52 :group 'org)
53
54(defun org-thing-at-point ()
55 "Examine the thing at point and let the caller know what it is.
56The return value is a string naming the thing at point."
57 (let ((beg1 (save-excursion
58 (skip-chars-backward (org-re "[:alnum:]_@"))
59 (point)))
60 (beg (save-excursion
61 (skip-chars-backward "a-zA-Z0-9_:$")
62 (point)))
63 (line-to-here (buffer-substring (point-at-bol) (point))))
64 (cond
65 ((string-match "\\`[ \t]*#\\+begin: clocktable[ \t]+" line-to-here)
66 (cons "block-option" "clocktable"))
67 ((string-match "\\`[ \t]*#\\+begin_src[ \t]+" line-to-here)
68 (cons "block-option" "src"))
69 ((save-excursion
70 (re-search-backward "^[ \t]*#\\+\\([A-Z_]+\\):.*"
71 (line-beginning-position) t))
72 (cons "file-option" (match-string-no-properties 1)))
73 ((string-match "\\`[ \t]*#\\+[a-zA-Z]*\\'" line-to-here)
74 (cons "file-option" nil))
75 ((equal (char-before beg) ?\[)
76 (cons "link" nil))
77 ((equal (char-before beg) ?\\)
78 (cons "tex" nil))
79 ((string-match "\\`\\*+[ \t]+\\'"
80 (buffer-substring (point-at-bol) beg))
81 (cons "todo" nil))
82 ((equal (char-before beg) ?*)
83 (cons "searchhead" nil))
84 ((and (equal (char-before beg1) ?:)
85 (equal (char-after (point-at-bol)) ?*))
86 (cons "tag" nil))
87 ((and (equal (char-before beg1) ?:)
88 (not (equal (char-after (point-at-bol)) ?*)))
89 (cons "prop" nil))
90 (t nil))))
91
92(defun org-command-at-point ()
93 "Return the qualified name of the Org completion entity at point.
94When completing for #+STARTUP, for example, this function returns
95\"file-option/startup\"."
96 (let ((thing (org-thing-at-point)))
97 (cond
98 ((string= "file-option" (car thing))
99 (concat (car thing) "/" (downcase (cdr thing))))
100 ((string= "block-option" (car thing))
101 (concat (car thing) "/" (downcase (cdr thing))))
102 (t
103 (car thing)))))
104
105(defun org-parse-arguments ()
106 "Parse whitespace separated arguments in the current region."
107 (let ((begin (line-beginning-position))
108 (end (line-end-position))
109 begins args)
110 (save-restriction
111 (narrow-to-region begin end)
112 (save-excursion
113 (goto-char (point-min))
114 (while (not (eobp))
115 (skip-chars-forward " \t\n[")
116 (setq begins (cons (point) begins))
117 (skip-chars-forward "^ \t\n[")
118 (setq args (cons (buffer-substring-no-properties
119 (car begins) (point))
120 args)))
121 (cons (reverse args) (reverse begins))))))
122
123
124(defun org-complete-initial ()
125 "Calls the right completion function for first argument completions."
126 (ignore
127 (funcall (or (pcomplete-find-completion-function
128 (car (org-thing-at-point)))
129 pcomplete-default-completion-function))))
130
131(defvar org-additional-option-like-keywords)
132(defun pcomplete/org-mode/file-option ()
133 "Complete against all valid file options."
134 (require 'org-exp)
135 (pcomplete-here
136 (org-complete-case-double
137 (mapcar (lambda (x)
138 (if (= ?: (aref x (1- (length x))))
139 (concat x " ")
140 x))
141 (delq nil
142 (pcomplete-uniqify-list
143 (append
144 (mapcar (lambda (x)
145 (if (string-match "^#\\+\\([A-Z_]+:?\\)" x)
146 (match-string 1 x)))
147 (org-split-string (org-get-current-options) "\n"))
148 org-additional-option-like-keywords)))))
149 (substring pcomplete-stub 2)))
150
151(defvar org-startup-options)
152(defun pcomplete/org-mode/file-option/startup ()
153 "Complete arguments for the #+STARTUP file option."
154 (while (pcomplete-here
155 (let ((opts (pcomplete-uniqify-list
156 (mapcar 'car org-startup-options))))
157 ;; Some options are mutually exclusive, and shouldn't be completed
158 ;; against if certain other options have already been seen.
159 (dolist (arg pcomplete-args)
160 (cond
161 ((string= arg "hidestars")
162 (setq opts (delete "showstars" opts)))))
163 opts))))
164
165(defun pcomplete/org-mode/file-option/bind ()
166 "Complete arguments for the #+BIND file option, which are variable names"
167 (let (vars)
168 (mapatoms
169 (lambda (a) (if (boundp a) (setq vars (cons (symbol-name a) vars)))))
170 (pcomplete-here vars)))
171
172(defvar org-link-abbrev-alist-local)
173(defvar org-link-abbrev-alist)
174(defun pcomplete/org-mode/link ()
175 "Complete against defined #+LINK patterns."
176 (pcomplete-here
177 (pcomplete-uniqify-list (append (mapcar 'car org-link-abbrev-alist-local)
178 (mapcar 'car org-link-abbrev-alist)))))
179
180(defvar org-entities)
181(defun pcomplete/org-mode/tex ()
182 "Complete against TeX-style HTML entity names."
183 (require 'org-entities)
184 (while (pcomplete-here
185 (pcomplete-uniqify-list (remove nil (mapcar 'car-safe org-entities)))
186 (substring pcomplete-stub 1))))
187
188(defvar org-todo-keywords-1)
189(defun pcomplete/org-mode/todo ()
190 "Complete against known TODO keywords."
191 (pcomplete-here (pcomplete-uniqify-list org-todo-keywords-1)))
192
193(defvar org-todo-line-regexp)
194(defun pcomplete/org-mode/searchhead ()
195 "Complete against all headings.
196This needs more work, to handle headings with lots of spaces in them."
197 (while
198 (pcomplete-here
199 (save-excursion
200 (goto-char (point-min))
201 (let (tbl)
202 (while (re-search-forward org-todo-line-regexp nil t)
203 (push (org-make-org-heading-search-string
204 (match-string-no-properties 3) t)
205 tbl))
206 (pcomplete-uniqify-list tbl)))
207 (substring pcomplete-stub 1))))
208
209(defvar org-tag-alist)
210(defun pcomplete/org-mode/tag ()
211 "Complete a tag name. Omit tags already set."
212 (while (pcomplete-here
213 (mapcar (lambda (x)
214 (concat x ":"))
215 (let ((lst (pcomplete-uniqify-list
216 (or (remove
217 nil
218 (mapcar (lambda (x)
219 (and (stringp (car x)) (car x)))
220 org-tag-alist))
221 (mapcar 'car (org-get-buffer-tags))))))
222 (dolist (tag (org-get-tags))
223 (setq lst (delete tag lst)))
224 lst))
225 (and (string-match ".*:" pcomplete-stub)
226 (substring pcomplete-stub (match-end 0))))))
227
228(defun pcomplete/org-mode/prop ()
229 "Complete a property name. Omit properties already set."
230 (pcomplete-here
231 (mapcar (lambda (x)
232 (concat x ": "))
233 (let ((lst (pcomplete-uniqify-list
234 (org-buffer-property-keys nil t t))))
235 (dolist (prop (org-entry-properties))
236 (setq lst (delete (car prop) lst)))
237 lst))
238 (substring pcomplete-stub 1)))
239
240(defun pcomplete/org-mode/block-option/src ()
241 "Complete the arguments of a begin_src block.
242Complete a language in the first field, the header arguments and switches."
243 (pcomplete-here
244 (mapcar
245 (lambda(x) (symbol-name (nth 3 x)))
246 (cdr (car (cdr (memq :key-type (plist-get
247 (symbol-plist
248 'org-babel-load-languages)
249 'custom-type)))))))
250 (while (pcomplete-here
251 '("-n" "-r" "-l"
252 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
253 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
254 ":session" ":shebang" ":tangle" ":var"))))
255
256(defun pcomplete/org-mode/block-option/clocktable ()
257 "Complete keywords in a clocktable line"
258 (while (pcomplete-here '(":maxlevel" ":scope"
259 ":tstart" ":tend" ":block" ":step"
260 ":stepskip0" ":fileskip0"
261 ":emphasize" ":link" ":narrow" ":indent"
262 ":tcolumns" ":level" ":compact" ":timestamp"
263 ":formula" ":formatter"))))
264
265(defun org-complete-case-double (list)
266 "Return list with both upcase and downcase version of all strings in LIST."
267 (let (e res)
268 (while (setq e (pop list))
269 (setq res (cons (downcase e) (cons (upcase e) res))))
270 (nreverse res)))
271
272;;;; Finish up
273
274(provide 'org-complete)
275
acedf35c
CD
276
277;;; org-complete.el ends here