1 ;;; org-pcomplete.el --- In-buffer completion code
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; John Wiegley <johnw at gnu dot org>
8 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; Homepage: http://orgmode.org
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;;;; Require other packages
38 (declare-function org-split-string
"org" (string &optional separators
))
39 (declare-function org-get-current-options
"org-exp" ())
40 (declare-function org-make-org-heading-search-string
"org"
41 (&optional string heading
))
42 (declare-function org-get-buffer-tags
"org" ())
43 (declare-function org-get-tags
"org" ())
44 (declare-function org-buffer-property-keys
"org"
45 (&optional include-specials include-defaults include-columns
))
46 (declare-function org-entry-properties
"org" (&optional pom which specific
))
48 ;;;; Customization variables
50 (defgroup org-complete nil
51 "Outline-based notes management and organizer."
55 (defun org-thing-at-point ()
56 "Examine the thing at point and let the caller know what it is.
57 The return value is a string naming the thing at point."
58 (let ((beg1 (save-excursion
59 (skip-chars-backward (org-re "[:alnum:]_@"))
62 (skip-chars-backward "a-zA-Z0-9_:$")
64 (line-to-here (buffer-substring (point-at-bol) (point))))
66 ((string-match "\\`[ \t]*#\\+begin: clocktable[ \t]+" line-to-here
)
67 (cons "block-option" "clocktable"))
68 ((string-match "\\`[ \t]*#\\+begin_src[ \t]+" line-to-here
)
69 (cons "block-option" "src"))
71 (re-search-backward "^[ \t]*#\\+\\([A-Z_]+\\):.*"
72 (line-beginning-position) t
))
73 (cons "file-option" (match-string-no-properties 1)))
74 ((string-match "\\`[ \t]*#\\+[a-zA-Z]*\\'" line-to-here
)
75 (cons "file-option" nil
))
76 ((equal (char-before beg
) ?\
[)
78 ((equal (char-before beg
) ?
\\)
80 ((string-match "\\`\\*+[ \t]+\\'"
81 (buffer-substring (point-at-bol) beg
))
83 ((equal (char-before beg
) ?
*)
84 (cons "searchhead" nil
))
85 ((and (equal (char-before beg1
) ?
:)
86 (equal (char-after (point-at-bol)) ?
*))
88 ((and (equal (char-before beg1
) ?
:)
89 (not (equal (char-after (point-at-bol)) ?
*)))
93 (defun org-command-at-point ()
94 "Return the qualified name of the Org completion entity at point.
95 When completing for #+STARTUP, for example, this function returns
96 \"file-option/startup\"."
97 (let ((thing (org-thing-at-point)))
99 ((string= "file-option" (car thing
))
100 (concat (car thing
) "/" (downcase (cdr thing
))))
101 ((string= "block-option" (car thing
))
102 (concat (car thing
) "/" (downcase (cdr thing
))))
106 (defun org-parse-arguments ()
107 "Parse whitespace separated arguments in the current region."
108 (let ((begin (line-beginning-position))
109 (end (line-end-position))
112 (narrow-to-region begin end
)
114 (goto-char (point-min))
116 (skip-chars-forward " \t\n[")
117 (setq begins
(cons (point) begins
))
118 (skip-chars-forward "^ \t\n[")
119 (setq args
(cons (buffer-substring-no-properties
120 (car begins
) (point))
122 (cons (reverse args
) (reverse begins
))))))
125 (defun org-pcomplete-initial ()
126 "Calls the right completion function for first argument completions."
128 (funcall (or (pcomplete-find-completion-function
129 (car (org-thing-at-point)))
130 pcomplete-default-completion-function
))))
132 (defvar org-additional-option-like-keywords
)
133 (defun pcomplete/org-mode
/file-option
()
134 "Complete against all valid file options."
137 (org-pcomplete-case-double
139 (if (= ?
: (aref x
(1- (length x
))))
143 (pcomplete-uniqify-list
146 (if (string-match "^#\\+\\([A-Z_]+:?\\)" x
)
148 (org-split-string (org-get-current-options) "\n"))
149 org-additional-option-like-keywords
)))))
150 (substring pcomplete-stub
2)))
152 (defvar org-startup-options
)
153 (defun pcomplete/org-mode
/file-option
/startup
()
154 "Complete arguments for the #+STARTUP file option."
155 (while (pcomplete-here
156 (let ((opts (pcomplete-uniqify-list
157 (mapcar 'car org-startup-options
))))
158 ;; Some options are mutually exclusive, and shouldn't be completed
159 ;; against if certain other options have already been seen.
160 (dolist (arg pcomplete-args
)
162 ((string= arg
"hidestars")
163 (setq opts
(delete "showstars" opts
)))))
166 (defun pcomplete/org-mode
/file-option
/bind
()
167 "Complete arguments for the #+BIND file option, which are variable names"
170 (lambda (a) (if (boundp a
) (setq vars
(cons (symbol-name a
) vars
)))))
171 (pcomplete-here vars
)))
173 (defvar org-link-abbrev-alist-local
)
174 (defvar org-link-abbrev-alist
)
175 (defun pcomplete/org-mode
/link
()
176 "Complete against defined #+LINK patterns."
178 (pcomplete-uniqify-list
180 (append (mapcar 'car org-link-abbrev-alist-local
)
181 (mapcar 'car org-link-abbrev-alist
))))))
183 (defvar org-entities
)
184 (defun pcomplete/org-mode
/tex
()
185 "Complete against TeX-style HTML entity names."
186 (require 'org-entities
)
187 (while (pcomplete-here
188 (pcomplete-uniqify-list (remove nil
(mapcar 'car-safe org-entities
)))
189 (substring pcomplete-stub
1))))
191 (defvar org-todo-keywords-1
)
192 (defun pcomplete/org-mode
/todo
()
193 "Complete against known TODO keywords."
194 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1
))))
196 (defvar org-todo-line-regexp
)
197 (defun pcomplete/org-mode
/searchhead
()
198 "Complete against all headings.
199 This needs more work, to handle headings with lots of spaces in them."
203 (goto-char (point-min))
205 (while (re-search-forward org-todo-line-regexp nil t
)
206 (push (org-make-org-heading-search-string
207 (match-string-no-properties 3) t
)
209 (pcomplete-uniqify-list tbl
)))
210 (substring pcomplete-stub
1))))
212 (defvar org-tag-alist
)
213 (defun pcomplete/org-mode
/tag
()
214 "Complete a tag name. Omit tags already set."
215 (while (pcomplete-here
218 (let ((lst (pcomplete-uniqify-list
222 (and (stringp (car x
)) (car x
)))
224 (mapcar 'car
(org-get-buffer-tags))))))
225 (dolist (tag (org-get-tags))
226 (setq lst
(delete tag lst
)))
228 (and (string-match ".*:" pcomplete-stub
)
229 (substring pcomplete-stub
(match-end 0))))))
231 (defun pcomplete/org-mode
/prop
()
232 "Complete a property name. Omit properties already set."
236 (let ((lst (pcomplete-uniqify-list
238 (org-buffer-property-keys nil t t
)))))
239 (dolist (prop (org-entry-properties))
240 (setq lst
(delete (car prop
) lst
)))
242 (substring pcomplete-stub
1)))
244 (defun pcomplete/org-mode
/block-option
/src
()
245 "Complete the arguments of a begin_src block.
246 Complete a language in the first field, the header arguments and switches."
249 (lambda(x) (symbol-name (nth 3 x
)))
250 (cdr (car (cdr (memq :key-type
(plist-get
252 'org-babel-load-languages
)
254 (while (pcomplete-here
256 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
257 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
258 ":session" ":shebang" ":tangle" ":var"))))
260 (defun pcomplete/org-mode
/block-option
/clocktable
()
261 "Complete keywords in a clocktable line"
262 (while (pcomplete-here '(":maxlevel" ":scope"
263 ":tstart" ":tend" ":block" ":step"
264 ":stepskip0" ":fileskip0"
265 ":emphasize" ":link" ":narrow" ":indent"
266 ":tcolumns" ":level" ":compact" ":timestamp"
267 ":formula" ":formatter"))))
269 (defun org-pcomplete-case-double (list)
270 "Return list with both upcase and downcase version of all strings in LIST."
272 (while (setq e
(pop list
))
273 (setq res
(cons (downcase e
) (cons (upcase e
) res
))))
278 (provide 'org-pcomplete
)
282 ;;; org-pcomplete.el ends here