1 ;;; org-pcomplete.el --- In-buffer completion code
3 ;; Copyright (C) 2004-2013 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
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 ;; Unused. Cf org-completion.
50 (defgroup org-complete nil
51 "Outline-based notes management and organizer."
55 (defvar org-drawer-regexp
)
56 (defvar org-property-re
)
58 (defun org-thing-at-point ()
59 "Examine the thing at point and let the caller know what it is.
60 The return value is a string naming the thing at point."
61 (let ((beg1 (save-excursion
62 (skip-chars-backward (org-re "[:alnum:]-_@"))
65 (skip-chars-backward "a-zA-Z0-9-_:$")
67 (line-to-here (buffer-substring (point-at-bol) (point))))
69 ((string-match "\\`[ \t]*#\\+begin: clocktable[ \t]+" line-to-here
)
70 (cons "block-option" "clocktable"))
71 ((string-match "\\`[ \t]*#\\+begin_src[ \t]+" line-to-here
)
72 (cons "block-option" "src"))
74 (re-search-backward "^[ \t]*#\\+\\([A-Z_]+\\):.*"
75 (line-beginning-position) t
))
76 (cons "file-option" (match-string-no-properties 1)))
77 ((string-match "\\`[ \t]*#\\+[a-zA-Z_]*\\'" line-to-here
)
78 (cons "file-option" nil
))
79 ((equal (char-before beg
) ?\
[)
81 ((equal (char-before beg
) ?
\\)
83 ((string-match "\\`\\*+[ \t]+\\'"
84 (buffer-substring (point-at-bol) beg
))
86 ((equal (char-before beg
) ?
*)
87 (cons "searchhead" nil
))
88 ((and (equal (char-before beg1
) ?
:)
89 (equal (char-after (point-at-bol)) ?
*))
91 ((and (equal (char-before beg1
) ?
:)
92 (not (equal (char-after (point-at-bol)) ?
*))
94 (move-beginning-of-line 1)
95 (skip-chars-backward "[ \t\n]")
96 ;; org-drawer-regexp matches a whole line but while
97 ;; looking-back, we just ignore trailing whitespaces
98 (or (org-looking-back (substring org-drawer-regexp
0 -
1))
99 (org-looking-back org-property-re
))))
101 ((and (equal (char-before beg1
) ?
:)
102 (not (equal (char-after (point-at-bol)) ?
*)))
106 (defun org-command-at-point ()
107 "Return the qualified name of the Org completion entity at point.
108 When completing for #+STARTUP, for example, this function returns
109 \"file-option/startup\"."
110 (let ((thing (org-thing-at-point)))
112 ((string= "file-option" (car thing
))
113 (concat (car thing
) "/" (downcase (cdr thing
))))
114 ((string= "block-option" (car thing
))
115 (concat (car thing
) "/" (downcase (cdr thing
))))
119 (defun org-parse-arguments ()
120 "Parse whitespace separated arguments in the current region."
121 (let ((begin (line-beginning-position))
122 (end (line-end-position))
125 (narrow-to-region begin end
)
127 (goto-char (point-min))
129 (skip-chars-forward " \t\n[")
130 (setq begins
(cons (point) begins
))
131 (skip-chars-forward "^ \t\n[")
132 (setq args
(cons (buffer-substring-no-properties
133 (car begins
) (point))
135 (cons (reverse args
) (reverse begins
))))))
137 (defun org-pcomplete-initial ()
138 "Calls the right completion function for first argument completions."
140 (funcall (or (pcomplete-find-completion-function
141 (car (org-thing-at-point)))
142 pcomplete-default-completion-function
))))
144 (defvar org-options-keywords
) ; From org.el
145 (defvar org-additional-option-like-keywords
) ; From org.el
146 (defun pcomplete/org-mode
/file-option
()
147 "Complete against all valid file options."
150 (org-pcomplete-case-double
152 (if (= ?
: (aref x
(1- (length x
))))
155 (append org-options-keywords
156 org-additional-option-like-keywords
)))
157 (substring pcomplete-stub
2)))
159 (defvar org-startup-options
)
160 (defun pcomplete/org-mode
/file-option
/startup
()
161 "Complete arguments for the #+STARTUP file option."
162 (while (pcomplete-here
163 (let ((opts (pcomplete-uniqify-list
164 (mapcar 'car org-startup-options
))))
165 ;; Some options are mutually exclusive, and shouldn't be completed
166 ;; against if certain other options have already been seen.
167 (dolist (arg pcomplete-args
)
169 ((string= arg
"hidestars")
170 (setq opts
(delete "showstars" opts
)))))
173 (defmacro pcomplete
/org-mode
/file-option
/x
(option)
174 "Complete arguments for OPTION."
177 (pcomplete-uniqify-list
180 (when (string-match (concat "^[ \t]*#\\+"
181 ,option
":[ \t]+\\(.*\\)[ \t]*$") o
)
183 (split-string (org-get-current-options) "\n")))))))
185 (defun pcomplete/org-mode
/file-option
/options
()
186 "Complete arguments for the #+OPTIONS file option."
187 (pcomplete/org-mode
/file-option
/x
"OPTIONS"))
189 (defun pcomplete/org-mode
/file-option
/title
()
190 "Complete arguments for the #+TITLE file option."
191 (pcomplete/org-mode
/file-option
/x
"TITLE"))
193 (defun pcomplete/org-mode
/file-option
/author
()
194 "Complete arguments for the #+AUTHOR file option."
195 (pcomplete/org-mode
/file-option
/x
"AUTHOR"))
197 (defun pcomplete/org-mode
/file-option
/email
()
198 "Complete arguments for the #+EMAIL file option."
199 (pcomplete/org-mode
/file-option
/x
"EMAIL"))
201 (defun pcomplete/org-mode
/file-option
/date
()
202 "Complete arguments for the #+DATE file option."
203 (pcomplete/org-mode
/file-option
/x
"DATE"))
205 (defun pcomplete/org-mode
/file-option
/bind
()
206 "Complete arguments for the #+BIND file option, which are variable names."
209 (lambda (a) (if (boundp a
) (setq vars
(cons (symbol-name a
) vars
)))))
210 (pcomplete-here vars
)))
212 (defvar org-link-abbrev-alist-local
)
213 (defvar org-link-abbrev-alist
)
214 (defun pcomplete/org-mode
/link
()
215 "Complete against defined #+LINK patterns."
217 (pcomplete-uniqify-list
219 (append (mapcar 'car org-link-abbrev-alist-local
)
220 (mapcar 'car org-link-abbrev-alist
))))))
222 (defvar org-entities
)
223 (defun pcomplete/org-mode
/tex
()
224 "Complete against TeX-style HTML entity names."
225 (require 'org-entities
)
226 (while (pcomplete-here
227 (pcomplete-uniqify-list (remove nil
(mapcar 'car-safe org-entities
)))
228 (substring pcomplete-stub
1))))
230 (defvar org-todo-keywords-1
)
231 (defun pcomplete/org-mode
/todo
()
232 "Complete against known TODO keywords."
233 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1
))))
235 (defvar org-todo-line-regexp
)
236 (defun pcomplete/org-mode
/searchhead
()
237 "Complete against all headings.
238 This needs more work, to handle headings with lots of spaces in them."
242 (goto-char (point-min))
244 (while (re-search-forward org-todo-line-regexp nil t
)
245 (push (org-make-org-heading-search-string
246 (match-string-no-properties 3) t
)
248 (pcomplete-uniqify-list tbl
)))
249 (substring pcomplete-stub
1))))
251 (defvar org-tag-alist
)
252 (defun pcomplete/org-mode
/tag
()
253 "Complete a tag name. Omit tags already set."
254 (while (pcomplete-here
257 (let ((lst (pcomplete-uniqify-list
261 (and (stringp (car x
)) (car x
)))
263 (mapcar 'car
(org-get-buffer-tags))))))
264 (dolist (tag (org-get-tags))
265 (setq lst
(delete tag lst
)))
267 (and (string-match ".*:" pcomplete-stub
)
268 (substring pcomplete-stub
(match-end 0))))))
270 (defun pcomplete/org-mode
/prop
()
271 "Complete a property name. Omit properties already set."
275 (let ((lst (pcomplete-uniqify-list
277 (org-buffer-property-keys nil t t
)))))
278 (dolist (prop (org-entry-properties))
279 (setq lst
(delete (car prop
) lst
)))
281 (substring pcomplete-stub
1)))
285 (defun pcomplete/org-mode
/drawer
()
286 "Complete a drawer name."
287 (let ((spc (save-excursion
288 (move-beginning-of-line 1)
289 (looking-at "^\\([ \t]*\\):")
291 (cpllist (mapcar (lambda (x) (concat x
": ")) org-drawers
)))
292 (pcomplete-here cpllist
293 (substring pcomplete-stub
1)
294 (unless (or (not (delete
297 (string-match (substring pcomplete-stub
1) x
))
299 (looking-at "[ \t]*\n.*:END:"))
300 (save-excursion (insert "\n" spc
":END:"))))))
302 (defun pcomplete/org-mode
/block-option
/src
()
303 "Complete the arguments of a begin_src block.
304 Complete a language in the first field, the header arguments and switches."
307 (lambda(x) (symbol-name (nth 3 x
)))
308 (cdr (car (cdr (memq :key-type
(plist-get
310 'org-babel-load-languages
)
312 (while (pcomplete-here
314 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
315 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
316 ":session" ":shebang" ":tangle" ":var"))))
318 (defun pcomplete/org-mode
/block-option
/clocktable
()
319 "Complete keywords in a clocktable line."
320 (while (pcomplete-here '(":maxlevel" ":scope"
321 ":tstart" ":tend" ":block" ":step"
322 ":stepskip0" ":fileskip0"
323 ":emphasize" ":link" ":narrow" ":indent"
324 ":tcolumns" ":level" ":compact" ":timestamp"
325 ":formula" ":formatter"))))
327 (defun org-pcomplete-case-double (list)
328 "Return list with both upcase and downcase version of all strings in LIST."
330 (while (setq e
(pop list
))
331 (setq res
(cons (downcase e
) (cons (upcase e
) res
))))
336 (provide 'org-pcomplete
)
338 ;;; org-pcomplete.el ends here