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
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."
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:]_@"))
63 (skip-chars-backward "a-zA-Z0-9_:$")
65 (line-to-here (buffer-substring (point-at-bol) (point))))
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"))
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
) ?\
[)
79 ((equal (char-before beg
) ?
\\)
81 ((string-match "\\`\\*+[ \t]+\\'"
82 (buffer-substring (point-at-bol) beg
))
84 ((equal (char-before beg
) ?
*)
85 (cons "searchhead" nil
))
86 ((and (equal (char-before beg1
) ?
:)
87 (equal (char-after (point-at-bol)) ?
*))
89 ((and (equal (char-before beg1
) ?
:)
90 (not (equal (char-after (point-at-bol)) ?
*))
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
))))
97 ((and (equal (char-before beg1
) ?
:)
98 (not (equal (char-after (point-at-bol)) ?
*)))
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)))
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
))))
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))
121 (narrow-to-region begin end
)
123 (goto-char (point-min))
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))
131 (cons (reverse args
) (reverse begins
))))))
134 (defun org-pcomplete-initial ()
135 "Calls the right completion function for first argument completions."
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."
146 (org-pcomplete-case-double
148 (if (= ?
: (aref x
(1- (length x
))))
152 (pcomplete-uniqify-list
155 (if (string-match "^#\\+\\([A-Z_]+:?\\)" 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
)
171 ((string= arg
"hidestars")
172 (setq opts
(delete "showstars" opts
)))))
175 (defun pcomplete/org-mode
/file-option
/bind
()
176 "Complete arguments for the #+BIND file option, which are variable names"
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."
187 (pcomplete-uniqify-list
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."
212 (goto-char (point-min))
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
)
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
227 (let ((lst (pcomplete-uniqify-list
231 (and (stringp (car x
)) (car x
)))
233 (mapcar 'car
(org-get-buffer-tags))))))
234 (dolist (tag (org-get-tags))
235 (setq lst
(delete tag 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."
245 (let ((lst (pcomplete-uniqify-list
247 (org-buffer-property-keys nil t t
)))))
248 (dolist (prop (org-entry-properties))
249 (setq lst
(delete (car prop
) lst
)))
251 (substring pcomplete-stub
1)))
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]*\\):")
261 (cpllist (mapcar (lambda (x) (concat x
": ")) org-drawers
)))
262 (pcomplete-here cpllist
263 (substring pcomplete-stub
1)
264 (unless (or (not (delete
267 (string-match (substring pcomplete-stub
1) x
))
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."
277 (lambda(x) (symbol-name (nth 3 x
)))
278 (cdr (car (cdr (memq :key-type
(plist-get
280 'org-babel-load-languages
)
282 (while (pcomplete-here
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."
300 (while (setq e
(pop list
))
301 (setq res
(cons (downcase e
) (cons (upcase e
) res
))))
306 (provide 'org-pcomplete
)
308 ;;; org-pcomplete.el ends here