1 ;;; org-pcomplete.el --- In-buffer Completion Code -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2018 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-make-org-heading-search-string
"org" (&optional string
))
35 (declare-function org-get-buffer-tags
"org" ())
36 (declare-function org-get-tags
"org" ())
37 (declare-function org-buffer-property-keys
"org"
38 (&optional specials defaults columns ignore-malformed
))
39 (declare-function org-entry-properties
"org" (&optional pom which
))
40 (declare-function org-tag-alist-to-string
"org" (alist &optional skip-key
))
42 ;;;; Customization variables
44 (defvar org-drawer-regexp
)
45 (defvar org-property-re
)
46 (defvar org-current-tag-alist
)
48 (defun org-thing-at-point ()
49 "Examine the thing at point and let the caller know what it is.
50 The return value is a string naming the thing at point."
51 (let ((beg1 (save-excursion
52 (skip-chars-backward "[:alnum:]-_@")
55 (skip-chars-backward "a-zA-Z0-9-_:$")
57 (line-to-here (buffer-substring (point-at-bol) (point))))
59 ((string-match "\\`[ \t]*#\\+begin: clocktable[ \t]+" line-to-here
)
60 (cons "block-option" "clocktable"))
61 ((string-match "\\`[ \t]*#\\+begin_src[ \t]+" line-to-here
)
62 (cons "block-option" "src"))
64 (re-search-backward "^[ \t]*#\\+\\([A-Z_]+\\):.*"
65 (line-beginning-position) t
))
66 (cons "file-option" (match-string-no-properties 1)))
67 ((string-match "\\`[ \t]*#\\+[a-zA-Z_]*\\'" line-to-here
)
68 (cons "file-option" nil
))
69 ((equal (char-before beg
) ?\
[)
71 ((equal (char-before beg
) ?
\\)
73 ((string-match "\\`\\*+[ \t]+\\'"
74 (buffer-substring (point-at-bol) beg
))
76 ((equal (char-before beg
) ?
*)
77 (cons "searchhead" nil
))
78 ((and (equal (char-before beg1
) ?
:)
79 (equal (char-after (point-at-bol)) ?
*))
81 ((and (equal (char-before beg1
) ?
:)
82 (not (equal (char-after (point-at-bol)) ?
*))
84 (move-beginning-of-line 1)
85 (skip-chars-backward "[ \t\n]")
86 ;; org-drawer-regexp matches a whole line but while
87 ;; looking-back, we just ignore trailing whitespaces
88 (or (looking-back (substring org-drawer-regexp
0 -
1)
89 (line-beginning-position))
90 (looking-back org-property-re
91 (line-beginning-position)))))
93 ((and (equal (char-before beg1
) ?
:)
94 (not (equal (char-after (point-at-bol)) ?
*)))
98 (defun org-command-at-point ()
99 "Return the qualified name of the Org completion entity at point.
100 When completing for #+STARTUP, for example, this function returns
101 \"file-option/startup\"."
102 (let ((thing (org-thing-at-point)))
104 ((string= "file-option" (car thing
))
106 (and (cdr thing
) (concat "/" (downcase (cdr thing
))))))
107 ((string= "block-option" (car thing
))
108 (concat (car thing
) "/" (downcase (cdr thing
))))
111 (defun org-parse-arguments ()
112 "Parse whitespace separated arguments in the current region."
113 (let ((begin (line-beginning-position))
114 (end (line-end-position))
117 (narrow-to-region begin end
)
119 (goto-char (point-min))
121 (skip-chars-forward " \t\n[")
122 (setq begins
(cons (point) begins
))
123 (skip-chars-forward "^ \t\n[")
124 (setq args
(cons (buffer-substring-no-properties
125 (car begins
) (point))
127 (cons (reverse args
) (reverse begins
))))))
129 (defun org-pcomplete-initial ()
130 "Calls the right completion function for first argument completions."
132 (funcall (or (pcomplete-find-completion-function
133 (car (org-thing-at-point)))
134 pcomplete-default-completion-function
))))
136 (defvar org-options-keywords
) ; From org.el
137 (defvar org-element-affiliated-keywords
) ; From org-element.el
138 (declare-function org-get-export-keywords
"org" ())
139 (defun pcomplete/org-mode
/file-option
()
140 "Complete against all valid file options."
141 (require 'org-element
)
143 (org-pcomplete-case-double
144 (append (mapcar (lambda (keyword) (concat keyword
" "))
145 org-options-keywords
)
146 (mapcar (lambda (keyword) (concat keyword
": "))
147 org-element-affiliated-keywords
)
150 '("CENTER" "COMMENT" "EXAMPLE" "EXPORT" "QUOTE" "SRC"
153 (push (format "END_%s" name
) block-names
)
154 (push (concat "BEGIN_"
156 ;; Since language is compulsory in
157 ;; export blocks source blocks, add
159 (and (member name
'("EXPORT" "SRC")) " "))
161 (push (format "ATTR_%s: " name
) block-names
)))
162 (mapcar (lambda (keyword) (concat keyword
": "))
163 (org-get-export-keywords))))
164 (substring pcomplete-stub
2)))
166 (defun pcomplete/org-mode
/file-option
/author
()
167 "Complete arguments for the #+AUTHOR file option."
168 (pcomplete-here (list user-full-name
)))
170 (defvar org-time-stamp-formats
)
171 (defun pcomplete/org-mode
/file-option
/date
()
172 "Complete arguments for the #+DATE file option."
173 (pcomplete-here (list (format-time-string (car org-time-stamp-formats
)))))
175 (defun pcomplete/org-mode
/file-option
/email
()
176 "Complete arguments for the #+EMAIL file option."
177 (pcomplete-here (list user-mail-address
)))
179 (defvar org-export-exclude-tags
)
180 (defun pcomplete/org-mode
/file-option
/exclude_tags
()
181 "Complete arguments for the #+EXCLUDE_TAGS file option."
184 (and org-export-exclude-tags
185 (list (mapconcat 'identity org-export-exclude-tags
" ")))))
187 (defvar org-file-tags
)
188 (defun pcomplete/org-mode
/file-option
/filetags
()
189 "Complete arguments for the #+FILETAGS file option."
190 (pcomplete-here (and org-file-tags
(mapconcat 'identity org-file-tags
" "))))
192 (defvar org-export-default-language
)
193 (defun pcomplete/org-mode
/file-option
/language
()
194 "Complete arguments for the #+LANGUAGE file option."
197 (pcomplete-uniqify-list
198 (list org-export-default-language
"en"))))
200 (defvar org-default-priority
)
201 (defvar org-highest-priority
)
202 (defvar org-lowest-priority
)
203 (defun pcomplete/org-mode
/file-option
/priorities
()
204 "Complete arguments for the #+PRIORITIES file option."
205 (pcomplete-here (list (format "%c %c %c"
208 org-default-priority
))))
210 (defvar org-export-select-tags
)
211 (defun pcomplete/org-mode
/file-option
/select_tags
()
212 "Complete arguments for the #+SELECT_TAGS file option."
215 (and org-export-select-tags
216 (list (mapconcat 'identity org-export-select-tags
" ")))))
218 (defvar org-startup-options
)
219 (defun pcomplete/org-mode
/file-option
/startup
()
220 "Complete arguments for the #+STARTUP file option."
221 (while (pcomplete-here
222 (let ((opts (pcomplete-uniqify-list
223 (mapcar 'car org-startup-options
))))
224 ;; Some options are mutually exclusive, and shouldn't be completed
225 ;; against if certain other options have already been seen.
226 (dolist (arg pcomplete-args
)
228 ((string= arg
"hidestars")
229 (setq opts
(delete "showstars" opts
)))))
232 (defun pcomplete/org-mode
/file-option
/tags
()
233 "Complete arguments for the #+TAGS file option."
235 (list (org-tag-alist-to-string org-current-tag-alist
))))
237 (defun pcomplete/org-mode
/file-option
/title
()
238 "Complete arguments for the #+TITLE file option."
240 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
241 (list (or (and visited-file
242 (file-name-sans-extension
243 (file-name-nondirectory visited-file
)))
244 (buffer-name (buffer-base-buffer)))))))
247 (declare-function org-export-backend-options
"ox" (cl-x) t
)
248 (defun pcomplete/org-mode
/file-option
/options
()
249 "Complete arguments for the #+OPTIONS file option."
250 (while (pcomplete-here
251 (pcomplete-uniqify-list
253 ;; Hard-coded OPTION items always available.
254 '("H:" "\\n:" "num:" "timestamp:" "arch:" "author:" "c:"
255 "creator:" "date:" "d:" "email:" "*:" "e:" "::" "f:"
256 "inline:" "tex:" "p:" "pri:" "':" "-:" "stat:" "^:" "toc:"
257 "|:" "tags:" "tasks:" "<:" "todo:")
258 ;; OPTION items from registered back-ends.
260 (dolist (backend (bound-and-true-p
261 org-export-registered-backends
))
262 (dolist (option (org-export-backend-options backend
))
263 (let ((item (nth 2 option
)))
264 (when item
(push (concat item
":") items
)))))
267 (defun pcomplete/org-mode
/file-option
/infojs_opt
()
268 "Complete arguments for the #+INFOJS_OPT file option."
269 (while (pcomplete-here
270 (pcomplete-uniqify-list
271 (mapcar (lambda (item) (format "%s:" (car item
)))
272 (bound-and-true-p org-html-infojs-opts-table
))))))
274 (defun pcomplete/org-mode
/file-option
/bind
()
275 "Complete arguments for the #+BIND file option, which are variable names."
278 (lambda (a) (if (boundp a
) (setq vars
(cons (symbol-name a
) vars
)))))
279 (pcomplete-here vars
)))
281 (defvar org-link-abbrev-alist-local
)
282 (defvar org-link-abbrev-alist
)
283 (defun pcomplete/org-mode
/link
()
284 "Complete against defined #+LINK patterns."
286 (pcomplete-uniqify-list
288 (append (mapcar 'car org-link-abbrev-alist-local
)
289 (mapcar 'car org-link-abbrev-alist
))))))
291 (defvar org-entities
)
292 (defun pcomplete/org-mode
/tex
()
293 "Complete against TeX-style HTML entity names."
294 (require 'org-entities
)
295 (while (pcomplete-here
296 (pcomplete-uniqify-list (remove nil
(mapcar 'car-safe org-entities
)))
297 (substring pcomplete-stub
1))))
299 (defvar org-todo-keywords-1
)
300 (defun pcomplete/org-mode
/todo
()
301 "Complete against known TODO keywords."
302 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1
))))
304 (defvar org-todo-line-regexp
)
305 (defun pcomplete/org-mode
/searchhead
()
306 "Complete against all headings.
307 This needs more work, to handle headings with lots of spaces in them."
311 (goto-char (point-min))
313 (let ((case-fold-search nil
))
314 (while (re-search-forward org-todo-line-regexp nil t
)
315 (push (org-make-org-heading-search-string
316 (match-string-no-properties 3))
318 (pcomplete-uniqify-list tbl
)))
319 (substring pcomplete-stub
1))))
321 (defun pcomplete/org-mode
/tag
()
322 "Complete a tag name. Omit tags already set."
323 (while (pcomplete-here
324 (mapcar (lambda (x) (concat x
":"))
325 (let ((lst (pcomplete-uniqify-list
328 (mapcar (lambda (x) (org-string-nw-p (car x
)))
329 org-current-tag-alist
))
330 (mapcar #'car
(org-get-buffer-tags))))))
331 (dolist (tag (org-get-tags))
332 (setq lst
(delete tag lst
)))
334 (and (string-match ".*:" pcomplete-stub
)
335 (substring pcomplete-stub
(match-end 0))))))
337 (defun pcomplete/org-mode
/prop
()
338 "Complete a property name. Omit properties already set."
342 (let ((lst (pcomplete-uniqify-list
344 (org-buffer-property-keys nil t t t
)))))
345 (dolist (prop (org-entry-properties))
346 (setq lst
(delete (car prop
) lst
)))
348 (substring pcomplete-stub
1)))
350 (defun pcomplete/org-mode
/block-option
/src
()
351 "Complete the arguments of a begin_src block.
352 Complete a language in the first field, the header arguments and switches."
355 (lambda(x) (symbol-name (nth 3 x
)))
356 (cdr (car (cdr (memq :key-type
(plist-get
358 'org-babel-load-languages
)
360 (while (pcomplete-here
362 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
363 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
364 ":session" ":shebang" ":tangle" ":tangle-mode" ":var"))))
366 (defun pcomplete/org-mode
/block-option
/clocktable
()
367 "Complete keywords in a clocktable line."
368 (while (pcomplete-here '(":maxlevel" ":scope" ":lang"
369 ":tstart" ":tend" ":block" ":step"
370 ":stepskip0" ":fileskip0"
371 ":emphasize" ":link" ":narrow" ":indent"
372 ":tcolumns" ":level" ":compact" ":timestamp"
373 ":formula" ":formatter" ":wstart" ":mstart"))))
375 (defun org-pcomplete-case-double (list)
376 "Return list with both upcase and downcase version of all strings in LIST."
378 (while (setq e
(pop list
))
379 (setq res
(cons (downcase e
) (cons (upcase e
) res
))))
384 (provide 'org-pcomplete
)
386 ;;; org-pcomplete.el ends here