1 ;;; org-pcomplete.el --- In-buffer completion code
3 ;; Copyright (C) 2004-2016 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-make-org-heading-search-string
"org"
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 specials defaults columns ignore-malformed
))
44 (declare-function org-entry-properties
"org" (&optional pom which specific
))
45 (declare-function org-tag-alist-to-string
"org" (alist &optional skip-key
))
47 ;;;; Customization variables
49 (defgroup org-complete nil
50 "Outline-based notes management and organizer."
54 (defvar org-drawer-regexp
)
55 (defvar org-property-re
)
56 (defvar org-current-tag-alist
)
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 (line-beginning-position))
100 (org-looking-back org-property-re
101 (line-beginning-position)))))
103 ((and (equal (char-before beg1
) ?
:)
104 (not (equal (char-after (point-at-bol)) ?
*)))
108 (defun org-command-at-point ()
109 "Return the qualified name of the Org completion entity at point.
110 When completing for #+STARTUP, for example, this function returns
111 \"file-option/startup\"."
112 (let ((thing (org-thing-at-point)))
114 ((string= "file-option" (car thing
))
116 (and (cdr thing
) (concat "/" (downcase (cdr thing
))))))
117 ((string= "block-option" (car thing
))
118 (concat (car thing
) "/" (downcase (cdr thing
))))
121 (defun org-parse-arguments ()
122 "Parse whitespace separated arguments in the current region."
123 (let ((begin (line-beginning-position))
124 (end (line-end-position))
127 (narrow-to-region begin end
)
129 (goto-char (point-min))
131 (skip-chars-forward " \t\n[")
132 (setq begins
(cons (point) begins
))
133 (skip-chars-forward "^ \t\n[")
134 (setq args
(cons (buffer-substring-no-properties
135 (car begins
) (point))
137 (cons (reverse args
) (reverse begins
))))))
139 (defun org-pcomplete-initial ()
140 "Calls the right completion function for first argument completions."
142 (funcall (or (pcomplete-find-completion-function
143 (car (org-thing-at-point)))
144 pcomplete-default-completion-function
))))
146 (defvar org-options-keywords
) ; From org.el
147 (defvar org-element-affiliated-keywords
) ; From org-element.el
148 (declare-function org-get-export-keywords
"org" ())
149 (defun pcomplete/org-mode
/file-option
()
150 "Complete against all valid file options."
151 (require 'org-element
)
153 (org-pcomplete-case-double
154 (append (mapcar (lambda (keyword) (concat keyword
" "))
155 org-options-keywords
)
156 (mapcar (lambda (keyword) (concat keyword
": "))
157 org-element-affiliated-keywords
)
160 '("CENTER" "COMMENT" "EXAMPLE" "EXPORT" "QUOTE" "SRC"
163 (push (format "END_%s" name
) block-names
)
164 (push (concat "BEGIN_"
166 ;; Since language is compulsory in
167 ;; export blocks source blocks, add
169 (and (member name
'("EXPORT" "SRC")) " "))
171 (push (format "ATTR_%s: " name
) block-names
)))
172 (mapcar (lambda (keyword) (concat keyword
": "))
173 (org-get-export-keywords))))
174 (substring pcomplete-stub
2)))
176 (defun pcomplete/org-mode
/file-option
/author
()
177 "Complete arguments for the #+AUTHOR file option."
178 (pcomplete-here (list user-full-name
)))
180 (defvar org-time-stamp-formats
)
181 (defun pcomplete/org-mode
/file-option
/date
()
182 "Complete arguments for the #+DATE file option."
183 (pcomplete-here (list (format-time-string (car org-time-stamp-formats
)))))
185 (defun pcomplete/org-mode
/file-option
/email
()
186 "Complete arguments for the #+EMAIL file option."
187 (pcomplete-here (list user-mail-address
)))
189 (defvar org-export-exclude-tags
)
190 (defun pcomplete/org-mode
/file-option
/exclude_tags
()
191 "Complete arguments for the #+EXCLUDE_TAGS file option."
194 (and org-export-exclude-tags
195 (list (mapconcat 'identity org-export-exclude-tags
" ")))))
197 (defvar org-file-tags
)
198 (defun pcomplete/org-mode
/file-option
/filetags
()
199 "Complete arguments for the #+FILETAGS file option."
200 (pcomplete-here (and org-file-tags
(mapconcat 'identity org-file-tags
" "))))
202 (defvar org-export-default-language
)
203 (defun pcomplete/org-mode
/file-option
/language
()
204 "Complete arguments for the #+LANGUAGE file option."
207 (pcomplete-uniqify-list
208 (list org-export-default-language
"en"))))
210 (defvar org-default-priority
)
211 (defvar org-highest-priority
)
212 (defvar org-lowest-priority
)
213 (defun pcomplete/org-mode
/file-option
/priorities
()
214 "Complete arguments for the #+PRIORITIES file option."
215 (pcomplete-here (list (format "%c %c %c"
218 org-default-priority
))))
220 (defvar org-export-select-tags
)
221 (defun pcomplete/org-mode
/file-option
/select_tags
()
222 "Complete arguments for the #+SELECT_TAGS file option."
225 (and org-export-select-tags
226 (list (mapconcat 'identity org-export-select-tags
" ")))))
228 (defvar org-startup-options
)
229 (defun pcomplete/org-mode
/file-option
/startup
()
230 "Complete arguments for the #+STARTUP file option."
231 (while (pcomplete-here
232 (let ((opts (pcomplete-uniqify-list
233 (mapcar 'car org-startup-options
))))
234 ;; Some options are mutually exclusive, and shouldn't be completed
235 ;; against if certain other options have already been seen.
236 (dolist (arg pcomplete-args
)
238 ((string= arg
"hidestars")
239 (setq opts
(delete "showstars" opts
)))))
242 (defun pcomplete/org-mode
/file-option
/tags
()
243 "Complete arguments for the #+TAGS file option."
245 (list (org-tag-alist-to-string org-current-tag-alist
))))
247 (defun pcomplete/org-mode
/file-option
/title
()
248 "Complete arguments for the #+TITLE file option."
250 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
251 (list (or (and visited-file
252 (file-name-sans-extension
253 (file-name-nondirectory visited-file
)))
254 (buffer-name (buffer-base-buffer)))))))
257 (declare-function org-export-backend-options
"org-export" (cl-x))
258 (defun pcomplete/org-mode
/file-option
/options
()
259 "Complete arguments for the #+OPTIONS file option."
260 (while (pcomplete-here
261 (pcomplete-uniqify-list
263 ;; Hard-coded OPTION items always available.
264 '("H:" "\\n:" "num:" "timestamp:" "arch:" "author:" "c:"
265 "creator:" "date:" "d:" "email:" "*:" "e:" "::" "f:"
266 "inline:" "tex:" "p:" "pri:" "':" "-:" "stat:" "^:" "toc:"
267 "|:" "tags:" "tasks:" "<:" "todo:")
268 ;; OPTION items from registered back-ends.
270 (dolist (backend (org-bound-and-true-p
271 org-export-registered-backends
))
272 (dolist (option (org-export-backend-options backend
))
273 (let ((item (nth 2 option
)))
274 (when item
(push (concat item
":") items
)))))
277 (defun pcomplete/org-mode
/file-option
/infojs_opt
()
278 "Complete arguments for the #+INFOJS_OPT file option."
279 (while (pcomplete-here
280 (pcomplete-uniqify-list
281 (mapcar (lambda (item) (format "%s:" (car item
)))
282 (org-bound-and-true-p org-html-infojs-opts-table
))))))
284 (defun pcomplete/org-mode
/file-option
/bind
()
285 "Complete arguments for the #+BIND file option, which are variable names."
288 (lambda (a) (if (boundp a
) (setq vars
(cons (symbol-name a
) vars
)))))
289 (pcomplete-here vars
)))
291 (defvar org-link-abbrev-alist-local
)
292 (defvar org-link-abbrev-alist
)
293 (defun pcomplete/org-mode
/link
()
294 "Complete against defined #+LINK patterns."
296 (pcomplete-uniqify-list
298 (append (mapcar 'car org-link-abbrev-alist-local
)
299 (mapcar 'car org-link-abbrev-alist
))))))
301 (defvar org-entities
)
302 (defun pcomplete/org-mode
/tex
()
303 "Complete against TeX-style HTML entity names."
304 (require 'org-entities
)
305 (while (pcomplete-here
306 (pcomplete-uniqify-list (remove nil
(mapcar 'car-safe org-entities
)))
307 (substring pcomplete-stub
1))))
309 (defvar org-todo-keywords-1
)
310 (defun pcomplete/org-mode
/todo
()
311 "Complete against known TODO keywords."
312 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1
))))
314 (defvar org-todo-line-regexp
)
315 (defun pcomplete/org-mode
/searchhead
()
316 "Complete against all headings.
317 This needs more work, to handle headings with lots of spaces in them."
321 (goto-char (point-min))
323 (while (re-search-forward org-todo-line-regexp nil t
)
324 (push (org-make-org-heading-search-string
325 (match-string-no-properties 3))
327 (pcomplete-uniqify-list tbl
)))
328 (substring pcomplete-stub
1))))
330 (defun pcomplete/org-mode
/tag
()
331 "Complete a tag name. Omit tags already set."
332 (while (pcomplete-here
333 (mapcar (lambda (x) (concat x
":"))
334 (let ((lst (pcomplete-uniqify-list
337 (mapcar (lambda (x) (org-string-nw-p (car x
)))
338 org-current-tag-alist
))
339 (mapcar #'car
(org-get-buffer-tags))))))
340 (dolist (tag (org-get-tags))
341 (setq lst
(delete tag lst
)))
343 (and (string-match ".*:" pcomplete-stub
)
344 (substring pcomplete-stub
(match-end 0))))))
346 (defun pcomplete/org-mode
/prop
()
347 "Complete a property name. Omit properties already set."
351 (let ((lst (pcomplete-uniqify-list
353 (org-buffer-property-keys nil t t t
)))))
354 (dolist (prop (org-entry-properties))
355 (setq lst
(delete (car prop
) lst
)))
357 (substring pcomplete-stub
1)))
359 (defun pcomplete/org-mode
/block-option
/src
()
360 "Complete the arguments of a begin_src block.
361 Complete a language in the first field, the header arguments and switches."
364 (lambda(x) (symbol-name (nth 3 x
)))
365 (cdr (car (cdr (memq :key-type
(plist-get
367 'org-babel-load-languages
)
369 (while (pcomplete-here
371 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
372 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
373 ":session" ":shebang" ":tangle" ":tangle-mode" ":var"))))
375 (defun pcomplete/org-mode
/block-option
/clocktable
()
376 "Complete keywords in a clocktable line."
377 (while (pcomplete-here '(":maxlevel" ":scope" ":lang"
378 ":tstart" ":tend" ":block" ":step"
379 ":stepskip0" ":fileskip0"
380 ":emphasize" ":link" ":narrow" ":indent"
381 ":tcolumns" ":level" ":compact" ":timestamp"
382 ":formula" ":formatter" ":wstart" ":mstart"))))
384 (defun org-pcomplete-case-double (list)
385 "Return list with both upcase and downcase version of all strings in LIST."
387 (while (setq e
(pop list
))
388 (setq res
(cons (downcase e
) (cons (upcase e
) res
))))
393 (provide 'org-pcomplete
)
395 ;;; org-pcomplete.el ends here