1 ;;; org-pcomplete.el --- In-buffer completion code
3 ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;;; Require other packages
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."
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:]_@"))
64 (skip-chars-backward "a-zA-Z0-9_:$")
66 (line-to-here (buffer-substring (point-at-bol) (point))))
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"))
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
) ?\
[)
80 ((equal (char-before beg
) ?
\\)
82 ((string-match "\\`\\*+[ \t]+\\'"
83 (buffer-substring (point-at-bol) beg
))
85 ((equal (char-before beg
) ?
*)
86 (cons "searchhead" nil
))
87 ((and (equal (char-before beg1
) ?
:)
88 (equal (char-after (point-at-bol)) ?
*))
90 ((and (equal (char-before beg1
) ?
:)
91 (not (equal (char-after (point-at-bol)) ?
*))
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
))))
100 ((and (equal (char-before beg1
) ?
:)
101 (not (equal (char-after (point-at-bol)) ?
*)))
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)))
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
))))
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))
124 (narrow-to-region begin end
)
126 (goto-char (point-min))
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))
134 (cons (reverse args
) (reverse begins
))))))
136 (defun org-pcomplete-initial ()
137 "Calls the right completion function for first argument completions."
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."
149 (org-pcomplete-case-double
151 (if (= ?
: (aref x
(1- (length 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
)
168 ((string= arg
"hidestars")
169 (setq opts
(delete "showstars" opts
)))))
172 (defmacro pcomplete
/org-mode
/file-option
/x
(option)
173 "Complete arguments for OPTION."
176 (pcomplete-uniqify-list
179 (when (string-match (concat "^[ \t]*#\\+"
180 ,option
":[ \t]+\\(.*\\)[ \t]*$") 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."
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."
216 (pcomplete-uniqify-list
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."
241 (goto-char (point-min))
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
)
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
256 (let ((lst (pcomplete-uniqify-list
260 (and (stringp (car x
)) (car x
)))
262 (mapcar 'car
(org-get-buffer-tags))))))
263 (dolist (tag (org-get-tags))
264 (setq lst
(delete tag 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."
274 (let ((lst (pcomplete-uniqify-list
276 (org-buffer-property-keys nil t t
)))))
277 (dolist (prop (org-entry-properties))
278 (setq lst
(delete (car prop
) lst
)))
280 (substring pcomplete-stub
1)))
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]*\\):")
290 (cpllist (mapcar (lambda (x) (concat x
": ")) org-drawers
)))
291 (pcomplete-here cpllist
292 (substring pcomplete-stub
1)
293 (unless (or (not (delete
296 (string-match (substring pcomplete-stub
1) x
))
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."
306 (lambda(x) (symbol-name (nth 3 x
)))
307 (cdr (car (cdr (memq :key-type
(plist-get
309 'org-babel-load-languages
)
311 (while (pcomplete-here
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."
329 (while (setq e
(pop list
))
330 (setq res
(cons (downcase e
) (cons (upcase e
) res
))))
335 (provide 'org-pcomplete
)
337 ;;; org-pcomplete.el ends here