1 ;;; org-pcomplete.el --- In-buffer Completion Code -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2019 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: https://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 <https://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;;; Require other packages
34 (declare-function org-at-heading-p
"org" (&optional ignored
))
35 (declare-function org-before-first-heading-p
"org" ())
36 (declare-function org-buffer-property-keys
"org" (&optional specials defaults columns
))
37 (declare-function org-element-at-point
"org-element" ())
38 (declare-function org-element-property
"org-element" property element
)
39 (declare-function org-element-type
"org-element" (element))
40 (declare-function org-end-of-meta-data
"org" (&optional full
))
41 (declare-function org-entry-properties
"org" (&optional pom which
))
42 (declare-function org-export-backend-options
"ox" (cl-x) t
)
43 (declare-function org-get-buffer-tags
"org" ())
44 (declare-function org-get-export-keywords
"org" ())
45 (declare-function org-get-heading
"org" (&optional no-tags no-todo no-priority no-comment
))
46 (declare-function org-get-tags
"org" (&optional pos local
))
47 (declare-function org-link-heading-search-string
"ol" (&optional string
))
48 (declare-function org-tag-alist-to-string
"org" (alist &optional skip-key
))
50 (defvar org-current-tag-alist
)
51 (defvar org-default-priority
)
52 (defvar org-drawer-regexp
)
53 (defvar org-element-affiliated-keywords
)
55 (defvar org-export-default-language
)
56 (defvar org-export-exclude-tags
)
57 (defvar org-export-select-tags
)
58 (defvar org-file-tags
)
59 (defvar org-highest-priority
)
60 (defvar org-link-abbrev-alist
)
61 (defvar org-link-abbrev-alist-local
)
62 (defvar org-lowest-priority
)
63 (defvar org-options-keywords
)
64 (defvar org-outline-regexp
)
65 (defvar org-property-re
)
66 (defvar org-startup-options
)
68 (defvar org-time-stamp-formats
)
69 (defvar org-todo-keywords-1
)
70 (defvar org-todo-line-regexp
)
73 ;;; Internal Functions
75 (defun org-thing-at-point ()
76 "Examine the thing at point and let the caller know what it is.
77 The return value is a string naming the thing at point."
78 (let ((line-to-here (org-current-line-string t
))
81 ;; Parameters on a clock table opening line.
82 ((org-match-line "[ \t]*#\\+BEGIN: clocktable[ \t]")
83 (cons "block-option" "clocktable"))
84 ;; Flags and parameters on a source block opening line.
85 ((org-match-line "[ \t]*#\\+BEGIN_SRC[ \t]")
86 (cons "block-option" "src"))
87 ;; Value for a known keyword.
88 ((org-match-line "[ \t]*#\\+\\(\\S-+\\):")
89 (cons "file-option" (match-string-no-properties 1)))
91 ((and (org-match-line "[ \t]*#\\+[a-zA-Z_]*$")
92 (looking-at-p "[ \t]*$"))
93 (cons "file-option" nil
))
96 (skip-chars-backward "-A-Za-z0-9_")
97 (and (eq ?\
[ (char-before))
98 (eq ?\
[ (char-before (1- (point))))))
100 ;; Entities. Some of them accept numbers, but only at their end.
101 ;; So, we first skip numbers, then letters.
102 ((eq ?
\\ (save-excursion
103 (skip-chars-backward "0-9")
104 (skip-chars-backward "a-zA-Z")
107 ;; Tags on a headline.
108 ((and (org-match-line
109 (format "\\*+ \\(?:.+? \\)?\\(:\\)\\(\\(?::\\|%s\\)+\\)?[ \t]*$"
111 (or (org-point-in-group (point) 2)
112 (= (point) (match-end 1))))
114 ;; TODO keywords on an empty headline.
115 ((and (string-match "^\\*+ +\\S-*$" line-to-here
)
116 (looking-at-p "[ \t]*$"))
118 ;; Heading after a star for search strings or links.
120 (skip-chars-backward "^*" (line-beginning-position))
121 (and (eq ?
* (char-before))
122 (eq (char-before (1- (point))) '?\
[)
123 (eq (char-before (- (point) 2)) '?\
[)))
124 (cons "searchhead" nil
))
125 ;; Property or drawer name, depending on point. If point is at
126 ;; a valid location for a node property, offer completion on all
127 ;; node properties in the buffer. Otherwise, offer completion on
128 ;; all drawer names, including "PROPERTIES".
129 ((and (string-match "^[ \t]*:\\S-*$" line-to-here
)
130 (looking-at-p "[ \t]*$"))
131 (let ((origin (line-beginning-position)))
132 (if (org-before-first-heading-p) (cons "drawer" nil
)
134 (org-end-of-meta-data)
135 (if (or (= origin
(point))
136 (not (org-match-line "[ \t]*:PROPERTIES:[ \t]*$")))
138 (while (org-match-line org-property-re
)
140 (if (= origin
(point)) (cons "prop" nil
)
141 (cons "drawer" nil
)))))))
144 (defun org-pcomplete-case-double (list)
145 "Return list with both upcase and downcase version of all strings in LIST."
147 (while (setq e
(pop list
))
148 (setq res
(cons (downcase e
) (cons (upcase e
) res
))))
154 (defun org-command-at-point ()
155 "Return the qualified name of the Org completion entity at point.
156 When completing for #+STARTUP, for example, this function returns
157 \"file-option/startup\"."
158 (let ((thing (org-thing-at-point)))
160 ((string= "file-option" (car thing
))
162 (and (cdr thing
) (concat "/" (downcase (cdr thing
))))))
163 ((string= "block-option" (car thing
))
164 (concat (car thing
) "/" (downcase (cdr thing
))))
167 (defun org-parse-arguments ()
168 "Parse whitespace separated arguments in the current region."
169 (let ((begin (line-beginning-position))
170 (end (line-end-position))
173 (narrow-to-region begin end
)
175 (goto-char (point-min))
177 (skip-chars-forward " \t\n[")
178 (setq begins
(cons (point) begins
))
179 (skip-chars-forward "^ \t\n[")
180 (setq args
(cons (buffer-substring-no-properties
181 (car begins
) (point))
183 (cons (reverse args
) (reverse begins
))))))
185 (defun org-pcomplete-initial ()
186 "Calls the right completion function for first argument completions."
188 (funcall (or (pcomplete-find-completion-function
189 (car (org-thing-at-point)))
190 pcomplete-default-completion-function
))))
193 ;;; Completion functions
195 (defun pcomplete/org-mode
/file-option
()
196 "Complete against all valid file options."
197 (require 'org-element
)
199 (org-pcomplete-case-double
200 (append (mapcar (lambda (keyword) (concat keyword
" "))
201 org-options-keywords
)
202 (mapcar (lambda (keyword) (concat keyword
": "))
203 org-element-affiliated-keywords
)
206 '("CENTER" "COMMENT" "EXAMPLE" "EXPORT" "QUOTE" "SRC"
209 (push (format "END_%s" name
) block-names
)
210 (push (concat "BEGIN_"
212 ;; Since language is compulsory in
213 ;; export blocks source blocks, add
215 (and (member name
'("EXPORT" "SRC")) " "))
217 (push (format "ATTR_%s: " name
) block-names
)))
218 (mapcar (lambda (keyword) (concat keyword
": "))
219 (org-get-export-keywords))))
220 (substring pcomplete-stub
2)))
222 (defun pcomplete/org-mode
/file-option
/author
()
223 "Complete arguments for the #+AUTHOR file option."
224 (pcomplete-here (list user-full-name
)))
226 (defun pcomplete/org-mode
/file-option
/date
()
227 "Complete arguments for the #+DATE file option."
228 (pcomplete-here (list (format-time-string (car org-time-stamp-formats
)))))
230 (defun pcomplete/org-mode
/file-option
/email
()
231 "Complete arguments for the #+EMAIL file option."
232 (pcomplete-here (list user-mail-address
)))
234 (defun pcomplete/org-mode
/file-option
/exclude_tags
()
235 "Complete arguments for the #+EXCLUDE_TAGS file option."
238 (and org-export-exclude-tags
239 (list (mapconcat 'identity org-export-exclude-tags
" ")))))
241 (defun pcomplete/org-mode
/file-option
/filetags
()
242 "Complete arguments for the #+FILETAGS file option."
243 (pcomplete-here (and org-file-tags
(mapconcat 'identity org-file-tags
" "))))
245 (defun pcomplete/org-mode
/file-option
/language
()
246 "Complete arguments for the #+LANGUAGE file option."
249 (pcomplete-uniquify-list
250 (list org-export-default-language
"en"))))
252 (defun pcomplete/org-mode
/file-option
/priorities
()
253 "Complete arguments for the #+PRIORITIES file option."
254 (pcomplete-here (list (format "%c %c %c"
257 org-default-priority
))))
259 (defun pcomplete/org-mode
/file-option
/select_tags
()
260 "Complete arguments for the #+SELECT_TAGS file option."
263 (and org-export-select-tags
264 (list (mapconcat 'identity org-export-select-tags
" ")))))
266 (defun pcomplete/org-mode
/file-option
/startup
()
267 "Complete arguments for the #+STARTUP file option."
268 (while (pcomplete-here
269 (let ((opts (pcomplete-uniquify-list
270 (mapcar 'car org-startup-options
))))
271 ;; Some options are mutually exclusive, and shouldn't be completed
272 ;; against if certain other options have already been seen.
273 (dolist (arg pcomplete-args
)
275 ((string= arg
"hidestars")
276 (setq opts
(delete "showstars" opts
)))))
279 (defun pcomplete/org-mode
/file-option
/tags
()
280 "Complete arguments for the #+TAGS file option."
282 (list (org-tag-alist-to-string org-current-tag-alist
))))
284 (defun pcomplete/org-mode
/file-option
/title
()
285 "Complete arguments for the #+TITLE file option."
287 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
288 (list (or (and visited-file
289 (file-name-sans-extension
290 (file-name-nondirectory visited-file
)))
291 (buffer-name (buffer-base-buffer)))))))
294 (defun pcomplete/org-mode
/file-option
/options
()
295 "Complete arguments for the #+OPTIONS file option."
296 (while (pcomplete-here
297 (pcomplete-uniquify-list
299 ;; Hard-coded OPTION items always available.
300 '("H:" "\\n:" "num:" "timestamp:" "arch:" "author:" "c:"
301 "creator:" "date:" "d:" "email:" "*:" "e:" "::" "f:"
302 "inline:" "tex:" "p:" "pri:" "':" "-:" "stat:" "^:" "toc:"
303 "|:" "tags:" "tasks:" "<:" "todo:")
304 ;; OPTION items from registered back-ends.
306 (dolist (backend (bound-and-true-p
307 org-export-registered-backends
))
308 (dolist (option (org-export-backend-options backend
))
309 (let ((item (nth 2 option
)))
310 (when item
(push (concat item
":") items
)))))
313 (defun pcomplete/org-mode
/file-option
/infojs_opt
()
314 "Complete arguments for the #+INFOJS_OPT file option."
315 (while (pcomplete-here
316 (pcomplete-uniquify-list
317 (mapcar (lambda (item) (format "%s:" (car item
)))
318 (bound-and-true-p org-html-infojs-opts-table
))))))
320 (defun pcomplete/org-mode
/file-option
/bind
()
321 "Complete arguments for the #+BIND file option, which are variable names."
324 (lambda (a) (when (boundp a
) (setq vars
(cons (symbol-name a
) vars
)))))
325 (pcomplete-here vars
)))
327 (defun pcomplete/org-mode
/link
()
328 "Complete against defined #+LINK patterns."
330 (pcomplete-uniquify-list
332 (mapcar (lambda (e) (concat (car e
) ":"))
333 (append org-link-abbrev-alist-local
334 org-link-abbrev-alist
))))))
336 (defun pcomplete/org-mode
/tex
()
337 "Complete against TeX-style HTML entity names."
338 (require 'org-entities
)
339 (while (pcomplete-here
340 (pcomplete-uniquify-list (remove nil
(mapcar 'car-safe org-entities
)))
341 (substring pcomplete-stub
1))))
343 (defun pcomplete/org-mode
/todo
()
344 "Complete against known TODO keywords."
345 (pcomplete-here (pcomplete-uniquify-list (copy-sequence org-todo-keywords-1
))))
347 (defun pcomplete/org-mode
/searchhead
()
348 "Complete against all headings.
349 This needs more work, to handle headings with lots of spaces in them."
350 (while (pcomplete-here
352 (goto-char (point-min))
354 (while (re-search-forward org-outline-regexp nil t
)
355 (push (org-link-heading-search-string (org-get-heading t t t t
))
357 (pcomplete-uniquify-list tbl
)))
358 ;; When completing a bracketed link, i.e., "[[*", argument
359 ;; starts at the star, so remove this character.
360 (substring pcomplete-stub
1))))
362 (defun pcomplete/org-mode
/tag
()
363 "Complete a tag name. Omit tags already set."
364 (while (pcomplete-here
365 (mapcar (lambda (x) (concat x
":"))
366 (let ((lst (pcomplete-uniquify-list
369 (mapcar (lambda (x) (org-string-nw-p (car x
)))
370 org-current-tag-alist
))
371 (mapcar #'car
(org-get-buffer-tags))))))
372 (dolist (tag (org-get-tags nil t
))
373 (setq lst
(delete tag lst
)))
375 (and (string-match ".*:" pcomplete-stub
)
376 (substring pcomplete-stub
(match-end 0)))
379 (defun pcomplete/org-mode
/drawer
()
380 "Complete a drawer name, including \"PROPERTIES\"."
382 (org-pcomplete-case-double
383 (mapcar (lambda (x) (concat x
":"))
384 (let ((names (list "PROPERTIES")))
386 (goto-char (point-min))
387 (while (re-search-forward org-drawer-regexp nil t
)
388 (let ((drawer (org-element-at-point)))
389 (when (memq (org-element-type drawer
)
390 '(drawer property-drawer
))
391 (push (org-element-property :drawer-name drawer
) names
)
392 (goto-char (org-element-property :end drawer
))))))
393 (pcomplete-uniquify-list names
))))
394 (substring pcomplete-stub
1))) ;remove initial colon
396 (defun pcomplete/org-mode
/prop
()
397 "Complete a property name. Omit properties already set."
399 (org-pcomplete-case-double
402 (let ((lst (pcomplete-uniquify-list
403 (copy-sequence (org-buffer-property-keys nil t t
)))))
404 (dolist (prop (org-entry-properties))
405 (setq lst
(delete (car prop
) lst
)))
407 (substring pcomplete-stub
1)))
409 (defun pcomplete/org-mode
/block-option
/src
()
410 "Complete the arguments of a source block.
411 Complete a language in the first field, the header arguments and
415 (lambda(x) (symbol-name (nth 3 x
)))
416 (cdr (car (cdr (memq :key-type
(plist-get
418 'org-babel-load-languages
)
420 (while (pcomplete-here
422 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
423 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
424 ":session" ":shebang" ":tangle" ":tangle-mode" ":var"))))
426 (defun pcomplete/org-mode
/block-option
/clocktable
()
427 "Complete keywords in a clocktable line."
428 (while (pcomplete-here '(":maxlevel" ":scope" ":lang"
429 ":tstart" ":tend" ":block" ":step"
430 ":stepskip0" ":fileskip0"
431 ":emphasize" ":link" ":narrow" ":indent"
432 ":tcolumns" ":level" ":compact" ":timestamp"
433 ":formula" ":formatter" ":wstart" ":mstart"))))
438 (provide 'org-pcomplete
)
440 ;;; org-pcomplete.el ends here