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 (defun org-thing-at-point ()
54 "Examine the thing at point and let the caller know what it is.
55 The return value is a string naming the thing at point."
56 (let ((beg1 (save-excursion
57 (skip-chars-backward (org-re "[:alnum:]_@"))
60 (skip-chars-backward "a-zA-Z0-9_:$")
62 (line-to-here (buffer-substring (point-at-bol) (point))))
64 ((string-match "\\`[ \t]*#\\+begin: clocktable[ \t]+" line-to-here
)
65 (cons "block-option" "clocktable"))
66 ((string-match "\\`[ \t]*#\\+begin_src[ \t]+" line-to-here
)
67 (cons "block-option" "src"))
69 (re-search-backward "^[ \t]*#\\+\\([A-Z_]+\\):.*"
70 (line-beginning-position) t
))
71 (cons "file-option" (match-string-no-properties 1)))
72 ((string-match "\\`[ \t]*#\\+[a-zA-Z]*\\'" line-to-here
)
73 (cons "file-option" nil
))
74 ((equal (char-before beg
) ?\
[)
76 ((equal (char-before beg
) ?
\\)
78 ((string-match "\\`\\*+[ \t]+\\'"
79 (buffer-substring (point-at-bol) beg
))
81 ((equal (char-before beg
) ?
*)
82 (cons "searchhead" nil
))
83 ((and (equal (char-before beg1
) ?
:)
84 (equal (char-after (point-at-bol)) ?
*))
86 ((and (equal (char-before beg1
) ?
:)
87 (not (equal (char-after (point-at-bol)) ?
*))
89 (move-beginning-of-line 1)
90 (skip-chars-backward "[ \t\n]")
91 (or (looking-back org-drawer-regexp
)
92 (looking-back org-property-re
))))
94 ((and (equal (char-before beg1
) ?
:)
95 (not (equal (char-after (point-at-bol)) ?
*)))
99 (defun org-command-at-point ()
100 "Return the qualified name of the Org completion entity at point.
101 When completing for #+STARTUP, for example, this function returns
102 \"file-option/startup\"."
103 (let ((thing (org-thing-at-point)))
105 ((string= "file-option" (car thing
))
106 (concat (car thing
) "/" (downcase (cdr thing
))))
107 ((string= "block-option" (car thing
))
108 (concat (car thing
) "/" (downcase (cdr thing
))))
112 (defun org-parse-arguments ()
113 "Parse whitespace separated arguments in the current region."
114 (let ((begin (line-beginning-position))
115 (end (line-end-position))
118 (narrow-to-region begin end
)
120 (goto-char (point-min))
122 (skip-chars-forward " \t\n[")
123 (setq begins
(cons (point) begins
))
124 (skip-chars-forward "^ \t\n[")
125 (setq args
(cons (buffer-substring-no-properties
126 (car begins
) (point))
128 (cons (reverse args
) (reverse begins
))))))
131 (defun org-pcomplete-initial ()
132 "Calls the right completion function for first argument completions."
134 (funcall (or (pcomplete-find-completion-function
135 (car (org-thing-at-point)))
136 pcomplete-default-completion-function
))))
138 (defvar org-additional-option-like-keywords
)
139 (defun pcomplete/org-mode
/file-option
()
140 "Complete against all valid file options."
143 (org-pcomplete-case-double
145 (if (= ?
: (aref x
(1- (length x
))))
149 (pcomplete-uniqify-list
152 (if (string-match "^#\\+\\([A-Z_]+:?\\)" x
)
154 (org-split-string (org-get-current-options) "\n"))
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 (defun pcomplete/org-mode
/file-option
/bind
()
173 "Complete arguments for the #+BIND file option, which are variable names"
176 (lambda (a) (if (boundp a
) (setq vars
(cons (symbol-name a
) vars
)))))
177 (pcomplete-here vars
)))
179 (defvar org-link-abbrev-alist-local
)
180 (defvar org-link-abbrev-alist
)
181 (defun pcomplete/org-mode
/link
()
182 "Complete against defined #+LINK patterns."
184 (pcomplete-uniqify-list
186 (append (mapcar 'car org-link-abbrev-alist-local
)
187 (mapcar 'car org-link-abbrev-alist
))))))
189 (defvar org-entities
)
190 (defun pcomplete/org-mode
/tex
()
191 "Complete against TeX-style HTML entity names."
192 (require 'org-entities
)
193 (while (pcomplete-here
194 (pcomplete-uniqify-list (remove nil
(mapcar 'car-safe org-entities
)))
195 (substring pcomplete-stub
1))))
197 (defvar org-todo-keywords-1
)
198 (defun pcomplete/org-mode
/todo
()
199 "Complete against known TODO keywords."
200 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1
))))
202 (defvar org-todo-line-regexp
)
203 (defun pcomplete/org-mode
/searchhead
()
204 "Complete against all headings.
205 This needs more work, to handle headings with lots of spaces in them."
209 (goto-char (point-min))
211 (while (re-search-forward org-todo-line-regexp nil t
)
212 (push (org-make-org-heading-search-string
213 (match-string-no-properties 3) t
)
215 (pcomplete-uniqify-list tbl
)))
216 (substring pcomplete-stub
1))))
218 (defvar org-tag-alist
)
219 (defun pcomplete/org-mode
/tag
()
220 "Complete a tag name. Omit tags already set."
221 (while (pcomplete-here
224 (let ((lst (pcomplete-uniqify-list
228 (and (stringp (car x
)) (car x
)))
230 (mapcar 'car
(org-get-buffer-tags))))))
231 (dolist (tag (org-get-tags))
232 (setq lst
(delete tag lst
)))
234 (and (string-match ".*:" pcomplete-stub
)
235 (substring pcomplete-stub
(match-end 0))))))
237 (defun pcomplete/org-mode
/prop
()
238 "Complete a property name. Omit properties already set."
242 (let ((lst (pcomplete-uniqify-list
244 (org-buffer-property-keys nil t t
)))))
245 (dolist (prop (org-entry-properties))
246 (setq lst
(delete (car prop
) lst
)))
248 (substring pcomplete-stub
1)))
250 (defun pcomplete/org-mode
/drawer
()
251 "Complete a drawer name."
252 (let ((spc (save-excursion
253 (move-beginning-of-line 1)
254 (looking-at "^\\([ \t]*\\):")
256 (cpllist (mapcar (lambda (x) (concat x
": ")) org-drawers
)))
257 (pcomplete-here cpllist
258 (substring pcomplete-stub
1)
259 (unless (or (not (delete
262 (string-match (substring pcomplete-stub
1) x
))
264 (looking-at "[ \t]*\n.*:END:"))
265 (save-excursion (insert "\n" spc
":END:"))))))
267 (defun pcomplete/org-mode
/block-option
/src
()
268 "Complete the arguments of a begin_src block.
269 Complete a language in the first field, the header arguments and switches."
272 (lambda(x) (symbol-name (nth 3 x
)))
273 (cdr (car (cdr (memq :key-type
(plist-get
275 'org-babel-load-languages
)
277 (while (pcomplete-here
279 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
280 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
281 ":session" ":shebang" ":tangle" ":var"))))
283 (defun pcomplete/org-mode
/block-option
/clocktable
()
284 "Complete keywords in a clocktable line"
285 (while (pcomplete-here '(":maxlevel" ":scope"
286 ":tstart" ":tend" ":block" ":step"
287 ":stepskip0" ":fileskip0"
288 ":emphasize" ":link" ":narrow" ":indent"
289 ":tcolumns" ":level" ":compact" ":timestamp"
290 ":formula" ":formatter"))))
292 (defun org-pcomplete-case-double (list)
293 "Return list with both upcase and downcase version of all strings in LIST."
295 (while (setq e
(pop list
))
296 (setq res
(cons (downcase e
) (cons (upcase e
) res
))))
301 (provide 'org-pcomplete
)
303 ;;; org-pcomplete.el ends here