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
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)) ?
*)))
91 (defun org-command-at-point ()
92 "Return the qualified name of the Org completion entity at point.
93 When completing for #+STARTUP, for example, this function returns
94 \"file-option/startup\"."
95 (let ((thing (org-thing-at-point)))
97 ((string= "file-option" (car thing
))
98 (concat (car thing
) "/" (downcase (cdr thing
))))
99 ((string= "block-option" (car thing
))
100 (concat (car thing
) "/" (downcase (cdr thing
))))
104 (defun org-parse-arguments ()
105 "Parse whitespace separated arguments in the current region."
106 (let ((begin (line-beginning-position))
107 (end (line-end-position))
110 (narrow-to-region begin end
)
112 (goto-char (point-min))
114 (skip-chars-forward " \t\n[")
115 (setq begins
(cons (point) begins
))
116 (skip-chars-forward "^ \t\n[")
117 (setq args
(cons (buffer-substring-no-properties
118 (car begins
) (point))
120 (cons (reverse args
) (reverse begins
))))))
123 (defun org-pcomplete-initial ()
124 "Calls the right completion function for first argument completions."
126 (funcall (or (pcomplete-find-completion-function
127 (car (org-thing-at-point)))
128 pcomplete-default-completion-function
))))
130 (defvar org-additional-option-like-keywords
)
131 (defun pcomplete/org-mode
/file-option
()
132 "Complete against all valid file options."
135 (org-pcomplete-case-double
137 (if (= ?
: (aref x
(1- (length x
))))
141 (pcomplete-uniqify-list
144 (if (string-match "^#\\+\\([A-Z_]+:?\\)" x
)
146 (org-split-string (org-get-current-options) "\n"))
147 org-additional-option-like-keywords
)))))
148 (substring pcomplete-stub
2)))
150 (defvar org-startup-options
)
151 (defun pcomplete/org-mode
/file-option
/startup
()
152 "Complete arguments for the #+STARTUP file option."
153 (while (pcomplete-here
154 (let ((opts (pcomplete-uniqify-list
155 (mapcar 'car org-startup-options
))))
156 ;; Some options are mutually exclusive, and shouldn't be completed
157 ;; against if certain other options have already been seen.
158 (dolist (arg pcomplete-args
)
160 ((string= arg
"hidestars")
161 (setq opts
(delete "showstars" opts
)))))
164 (defun pcomplete/org-mode
/file-option
/bind
()
165 "Complete arguments for the #+BIND file option, which are variable names"
168 (lambda (a) (if (boundp a
) (setq vars
(cons (symbol-name a
) vars
)))))
169 (pcomplete-here vars
)))
171 (defvar org-link-abbrev-alist-local
)
172 (defvar org-link-abbrev-alist
)
173 (defun pcomplete/org-mode
/link
()
174 "Complete against defined #+LINK patterns."
176 (pcomplete-uniqify-list
178 (append (mapcar 'car org-link-abbrev-alist-local
)
179 (mapcar 'car org-link-abbrev-alist
))))))
181 (defvar org-entities
)
182 (defun pcomplete/org-mode
/tex
()
183 "Complete against TeX-style HTML entity names."
184 (require 'org-entities
)
185 (while (pcomplete-here
186 (pcomplete-uniqify-list (remove nil
(mapcar 'car-safe org-entities
)))
187 (substring pcomplete-stub
1))))
189 (defvar org-todo-keywords-1
)
190 (defun pcomplete/org-mode
/todo
()
191 "Complete against known TODO keywords."
192 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1
))))
194 (defvar org-todo-line-regexp
)
195 (defun pcomplete/org-mode
/searchhead
()
196 "Complete against all headings.
197 This needs more work, to handle headings with lots of spaces in them."
201 (goto-char (point-min))
203 (while (re-search-forward org-todo-line-regexp nil t
)
204 (push (org-make-org-heading-search-string
205 (match-string-no-properties 3) t
)
207 (pcomplete-uniqify-list tbl
)))
208 (substring pcomplete-stub
1))))
210 (defvar org-tag-alist
)
211 (defun pcomplete/org-mode
/tag
()
212 "Complete a tag name. Omit tags already set."
213 (while (pcomplete-here
216 (let ((lst (pcomplete-uniqify-list
220 (and (stringp (car x
)) (car x
)))
222 (mapcar 'car
(org-get-buffer-tags))))))
223 (dolist (tag (org-get-tags))
224 (setq lst
(delete tag lst
)))
226 (and (string-match ".*:" pcomplete-stub
)
227 (substring pcomplete-stub
(match-end 0))))))
229 (defun pcomplete/org-mode
/prop
()
230 "Complete a property name. Omit properties already set."
234 (let ((lst (pcomplete-uniqify-list
236 (org-buffer-property-keys nil t t
)))))
237 (dolist (prop (org-entry-properties))
238 (setq lst
(delete (car prop
) lst
)))
240 (substring pcomplete-stub
1)))
242 (defun pcomplete/org-mode
/block-option
/src
()
243 "Complete the arguments of a begin_src block.
244 Complete a language in the first field, the header arguments and switches."
247 (lambda(x) (symbol-name (nth 3 x
)))
248 (cdr (car (cdr (memq :key-type
(plist-get
250 'org-babel-load-languages
)
252 (while (pcomplete-here
254 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
255 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
256 ":session" ":shebang" ":tangle" ":var"))))
258 (defun pcomplete/org-mode
/block-option
/clocktable
()
259 "Complete keywords in a clocktable line"
260 (while (pcomplete-here '(":maxlevel" ":scope"
261 ":tstart" ":tend" ":block" ":step"
262 ":stepskip0" ":fileskip0"
263 ":emphasize" ":link" ":narrow" ":indent"
264 ":tcolumns" ":level" ":compact" ":timestamp"
265 ":formula" ":formatter"))))
267 (defun org-pcomplete-case-double (list)
268 "Return list with both upcase and downcase version of all strings in LIST."
270 (while (setq e
(pop list
))
271 (setq res
(cons (downcase e
) (cons (upcase e
) res
))))
276 (provide 'org-pcomplete
)
278 ;;; org-pcomplete.el ends here