1 ;;; org-pcomplete.el --- In-buffer completion code
3 ;; Copyright (C) 2004-2011 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
11 ;; This file is part of GNU Emacs.
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.
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.
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;;;; 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 (defun org-thing-at-point ()
55 "Examine the thing at point and let the caller know what it is.
56 The return value is a string naming the thing at point."
57 (let ((beg1 (save-excursion
58 (skip-chars-backward (org-re "[:alnum:]_@"))
61 (skip-chars-backward "a-zA-Z0-9_:$")
63 (line-to-here (buffer-substring (point-at-bol) (point))))
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"))
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
) ?\
[)
77 ((equal (char-before beg
) ?
\\)
79 ((string-match "\\`\\*+[ \t]+\\'"
80 (buffer-substring (point-at-bol) beg
))
82 ((equal (char-before beg
) ?
*)
83 (cons "searchhead" nil
))
84 ((and (equal (char-before beg1
) ?
:)
85 (equal (char-after (point-at-bol)) ?
*))
87 ((and (equal (char-before beg1
) ?
:)
88 (not (equal (char-after (point-at-bol)) ?
*)))
92 (defun org-command-at-point ()
93 "Return the qualified name of the Org completion entity at point.
94 When completing for #+STARTUP, for example, this function returns
95 \"file-option/startup\"."
96 (let ((thing (org-thing-at-point)))
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
))))
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))
111 (narrow-to-region begin end
)
113 (goto-char (point-min))
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))
121 (cons (reverse args
) (reverse begins
))))))
124 (defun org-pcomplete-initial ()
125 "Calls the right completion function for first argument completions."
127 (funcall (or (pcomplete-find-completion-function
128 (car (org-thing-at-point)))
129 pcomplete-default-completion-function
))))
131 (defvar org-additional-option-like-keywords
)
132 (defun pcomplete/org-mode
/file-option
()
133 "Complete against all valid file options."
136 (org-pcomplete-case-double
138 (if (= ?
: (aref x
(1- (length x
))))
142 (pcomplete-uniqify-list
145 (if (string-match "^#\\+\\([A-Z_]+:?\\)" x
)
147 (org-split-string (org-get-current-options) "\n"))
148 org-additional-option-like-keywords
)))))
149 (substring pcomplete-stub
2)))
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
)
161 ((string= arg
"hidestars")
162 (setq opts
(delete "showstars" opts
)))))
165 (defun pcomplete/org-mode
/file-option
/bind
()
166 "Complete arguments for the #+BIND file option, which are variable names"
169 (lambda (a) (if (boundp a
) (setq vars
(cons (symbol-name a
) vars
)))))
170 (pcomplete-here vars
)))
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."
177 (pcomplete-uniqify-list
179 (append (mapcar 'car org-link-abbrev-alist-local
)
180 (mapcar 'car org-link-abbrev-alist
))))))
182 (defvar org-entities
)
183 (defun pcomplete/org-mode
/tex
()
184 "Complete against TeX-style HTML entity names."
185 (require 'org-entities
)
186 (while (pcomplete-here
187 (pcomplete-uniqify-list (remove nil
(mapcar 'car-safe org-entities
)))
188 (substring pcomplete-stub
1))))
190 (defvar org-todo-keywords-1
)
191 (defun pcomplete/org-mode
/todo
()
192 "Complete against known TODO keywords."
193 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1
))))
195 (defvar org-todo-line-regexp
)
196 (defun pcomplete/org-mode
/searchhead
()
197 "Complete against all headings.
198 This needs more work, to handle headings with lots of spaces in them."
202 (goto-char (point-min))
204 (while (re-search-forward org-todo-line-regexp nil t
)
205 (push (org-make-org-heading-search-string
206 (match-string-no-properties 3) t
)
208 (pcomplete-uniqify-list tbl
)))
209 (substring pcomplete-stub
1))))
211 (defvar org-tag-alist
)
212 (defun pcomplete/org-mode
/tag
()
213 "Complete a tag name. Omit tags already set."
214 (while (pcomplete-here
217 (let ((lst (pcomplete-uniqify-list
221 (and (stringp (car x
)) (car x
)))
223 (mapcar 'car
(org-get-buffer-tags))))))
224 (dolist (tag (org-get-tags))
225 (setq lst
(delete tag lst
)))
227 (and (string-match ".*:" pcomplete-stub
)
228 (substring pcomplete-stub
(match-end 0))))))
230 (defun pcomplete/org-mode
/prop
()
231 "Complete a property name. Omit properties already set."
235 (let ((lst (pcomplete-uniqify-list
237 (org-buffer-property-keys nil t t
)))))
238 (dolist (prop (org-entry-properties))
239 (setq lst
(delete (car prop
) lst
)))
241 (substring pcomplete-stub
1)))
243 (defun pcomplete/org-mode
/block-option
/src
()
244 "Complete the arguments of a begin_src block.
245 Complete a language in the first field, the header arguments and switches."
248 (lambda(x) (symbol-name (nth 3 x
)))
249 (cdr (car (cdr (memq :key-type
(plist-get
251 'org-babel-load-languages
)
253 (while (pcomplete-here
255 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
256 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
257 ":session" ":shebang" ":tangle" ":var"))))
259 (defun pcomplete/org-mode
/block-option
/clocktable
()
260 "Complete keywords in a clocktable line"
261 (while (pcomplete-here '(":maxlevel" ":scope"
262 ":tstart" ":tend" ":block" ":step"
263 ":stepskip0" ":fileskip0"
264 ":emphasize" ":link" ":narrow" ":indent"
265 ":tcolumns" ":level" ":compact" ":timestamp"
266 ":formula" ":formatter"))))
268 (defun org-pcomplete-case-double (list)
269 "Return list with both upcase and downcase version of all strings in LIST."
271 (while (setq e
(pop list
))
272 (setq res
(cons (downcase e
) (cons (upcase e
) res
))))
277 (provide 'org-pcomplete
)
281 ;;; org-pcomplete.el ends here