1 ;;; org-complete.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-complete-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-complete-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 (append (mapcar 'car org-link-abbrev-alist-local
)
178 (mapcar 'car org-link-abbrev-alist
)))))
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))))
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
)))
193 (defvar org-todo-line-regexp
)
194 (defun pcomplete/org-mode
/searchhead
()
195 "Complete against all headings.
196 This needs more work, to handle headings with lots of spaces in them."
200 (goto-char (point-min))
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
)
206 (pcomplete-uniqify-list tbl
)))
207 (substring pcomplete-stub
1))))
209 (defvar org-tag-alist
)
210 (defun pcomplete/org-mode
/tag
()
211 "Complete a tag name. Omit tags already set."
212 (while (pcomplete-here
215 (let ((lst (pcomplete-uniqify-list
219 (and (stringp (car x
)) (car x
)))
221 (mapcar 'car
(org-get-buffer-tags))))))
222 (dolist (tag (org-get-tags))
223 (setq lst
(delete tag lst
)))
225 (and (string-match ".*:" pcomplete-stub
)
226 (substring pcomplete-stub
(match-end 0))))))
228 (defun pcomplete/org-mode
/prop
()
229 "Complete a property name. Omit properties already set."
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
)))
238 (substring pcomplete-stub
1)))
240 (defun pcomplete/org-mode
/block-option
/src
()
241 "Complete the arguments of a begin_src block.
242 Complete a language in the first field, the header arguments and switches."
245 (lambda(x) (symbol-name (nth 3 x
)))
246 (cdr (car (cdr (memq :key-type
(plist-get
248 'org-babel-load-languages
)
250 (while (pcomplete-here
252 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
253 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
254 ":session" ":shebang" ":tangle" ":var"))))
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"))))
265 (defun org-complete-case-double (list)
266 "Return list with both upcase and downcase version of all strings in LIST."
268 (while (setq e
(pop list
))
269 (setq res
(cons (downcase e
) (cons (upcase e
) res
))))
274 (provide 'org-complete
)
277 ;;; org-complete.el ends here