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" (&optional specials defaults columns
))
38 (declare-function org-entry-properties
"org" (&optional pom which
))
39 (declare-function org-tag-alist-to-string
"org" (alist &optional skip-key
))
41 ;;;; Customization variables
43 (defvar org-drawer-regexp
)
44 (defvar org-property-re
)
45 (defvar org-current-tag-alist
)
47 (defun org-thing-at-point ()
48 "Examine the thing at point and let the caller know what it is.
49 The return value is a string naming the thing at point."
50 (let ((beg1 (save-excursion
51 (skip-chars-backward "[:alnum:]-_@")
54 (skip-chars-backward "a-zA-Z0-9-_:$")
56 (line-to-here (buffer-substring (point-at-bol) (point))))
58 ((string-match "\\`[ \t]*#\\+begin: clocktable[ \t]+" line-to-here
)
59 (cons "block-option" "clocktable"))
60 ((string-match "\\`[ \t]*#\\+begin_src[ \t]+" line-to-here
)
61 (cons "block-option" "src"))
63 (re-search-backward "^[ \t]*#\\+\\([A-Z_]+\\):.*"
64 (line-beginning-position) t
))
65 (cons "file-option" (match-string-no-properties 1)))
66 ((string-match "\\`[ \t]*#\\+[a-zA-Z_]*\\'" line-to-here
)
67 (cons "file-option" nil
))
68 ((equal (char-before beg
) ?\
[)
70 ((equal (char-before beg
) ?
\\)
72 ((string-match "\\`\\*+[ \t]+\\'"
73 (buffer-substring (point-at-bol) beg
))
75 ((equal (char-before beg
) ?
*)
76 (cons "searchhead" nil
))
77 ((and (equal (char-before beg1
) ?
:)
78 (equal (char-after (point-at-bol)) ?
*))
80 ((and (equal (char-before beg1
) ?
:)
81 (not (equal (char-after (point-at-bol)) ?
*))
83 (move-beginning-of-line 1)
84 (skip-chars-backward "[ \t\n]")
85 ;; org-drawer-regexp matches a whole line but while
86 ;; looking-back, we just ignore trailing whitespaces
87 (or (looking-back (substring org-drawer-regexp
0 -
1)
88 (line-beginning-position))
89 (looking-back org-property-re
90 (line-beginning-position)))))
92 ((and (equal (char-before beg1
) ?
:)
93 (not (equal (char-after (point-at-bol)) ?
*)))
97 (defun org-command-at-point ()
98 "Return the qualified name of the Org completion entity at point.
99 When completing for #+STARTUP, for example, this function returns
100 \"file-option/startup\"."
101 (let ((thing (org-thing-at-point)))
103 ((string= "file-option" (car thing
))
105 (and (cdr thing
) (concat "/" (downcase (cdr thing
))))))
106 ((string= "block-option" (car thing
))
107 (concat (car thing
) "/" (downcase (cdr thing
))))
110 (defun org-parse-arguments ()
111 "Parse whitespace separated arguments in the current region."
112 (let ((begin (line-beginning-position))
113 (end (line-end-position))
116 (narrow-to-region begin end
)
118 (goto-char (point-min))
120 (skip-chars-forward " \t\n[")
121 (setq begins
(cons (point) begins
))
122 (skip-chars-forward "^ \t\n[")
123 (setq args
(cons (buffer-substring-no-properties
124 (car begins
) (point))
126 (cons (reverse args
) (reverse begins
))))))
128 (defun org-pcomplete-initial ()
129 "Calls the right completion function for first argument completions."
131 (funcall (or (pcomplete-find-completion-function
132 (car (org-thing-at-point)))
133 pcomplete-default-completion-function
))))
135 (defvar org-options-keywords
) ; From org.el
136 (defvar org-element-affiliated-keywords
) ; From org-element.el
137 (declare-function org-get-export-keywords
"org" ())
138 (defun pcomplete/org-mode
/file-option
()
139 "Complete against all valid file options."
140 (require 'org-element
)
142 (org-pcomplete-case-double
143 (append (mapcar (lambda (keyword) (concat keyword
" "))
144 org-options-keywords
)
145 (mapcar (lambda (keyword) (concat keyword
": "))
146 org-element-affiliated-keywords
)
149 '("CENTER" "COMMENT" "EXAMPLE" "EXPORT" "QUOTE" "SRC"
152 (push (format "END_%s" name
) block-names
)
153 (push (concat "BEGIN_"
155 ;; Since language is compulsory in
156 ;; export blocks source blocks, add
158 (and (member name
'("EXPORT" "SRC")) " "))
160 (push (format "ATTR_%s: " name
) block-names
)))
161 (mapcar (lambda (keyword) (concat keyword
": "))
162 (org-get-export-keywords))))
163 (substring pcomplete-stub
2)))
165 (defun pcomplete/org-mode
/file-option
/author
()
166 "Complete arguments for the #+AUTHOR file option."
167 (pcomplete-here (list user-full-name
)))
169 (defvar org-time-stamp-formats
)
170 (defun pcomplete/org-mode
/file-option
/date
()
171 "Complete arguments for the #+DATE file option."
172 (pcomplete-here (list (format-time-string (car org-time-stamp-formats
)))))
174 (defun pcomplete/org-mode
/file-option
/email
()
175 "Complete arguments for the #+EMAIL file option."
176 (pcomplete-here (list user-mail-address
)))
178 (defvar org-export-exclude-tags
)
179 (defun pcomplete/org-mode
/file-option
/exclude_tags
()
180 "Complete arguments for the #+EXCLUDE_TAGS file option."
183 (and org-export-exclude-tags
184 (list (mapconcat 'identity org-export-exclude-tags
" ")))))
186 (defvar org-file-tags
)
187 (defun pcomplete/org-mode
/file-option
/filetags
()
188 "Complete arguments for the #+FILETAGS file option."
189 (pcomplete-here (and org-file-tags
(mapconcat 'identity org-file-tags
" "))))
191 (defvar org-export-default-language
)
192 (defun pcomplete/org-mode
/file-option
/language
()
193 "Complete arguments for the #+LANGUAGE file option."
196 (pcomplete-uniqify-list
197 (list org-export-default-language
"en"))))
199 (defvar org-default-priority
)
200 (defvar org-highest-priority
)
201 (defvar org-lowest-priority
)
202 (defun pcomplete/org-mode
/file-option
/priorities
()
203 "Complete arguments for the #+PRIORITIES file option."
204 (pcomplete-here (list (format "%c %c %c"
207 org-default-priority
))))
209 (defvar org-export-select-tags
)
210 (defun pcomplete/org-mode
/file-option
/select_tags
()
211 "Complete arguments for the #+SELECT_TAGS file option."
214 (and org-export-select-tags
215 (list (mapconcat 'identity org-export-select-tags
" ")))))
217 (defvar org-startup-options
)
218 (defun pcomplete/org-mode
/file-option
/startup
()
219 "Complete arguments for the #+STARTUP file option."
220 (while (pcomplete-here
221 (let ((opts (pcomplete-uniqify-list
222 (mapcar 'car org-startup-options
))))
223 ;; Some options are mutually exclusive, and shouldn't be completed
224 ;; against if certain other options have already been seen.
225 (dolist (arg pcomplete-args
)
227 ((string= arg
"hidestars")
228 (setq opts
(delete "showstars" opts
)))))
231 (defun pcomplete/org-mode
/file-option
/tags
()
232 "Complete arguments for the #+TAGS file option."
234 (list (org-tag-alist-to-string org-current-tag-alist
))))
236 (defun pcomplete/org-mode
/file-option
/title
()
237 "Complete arguments for the #+TITLE file option."
239 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
240 (list (or (and visited-file
241 (file-name-sans-extension
242 (file-name-nondirectory visited-file
)))
243 (buffer-name (buffer-base-buffer)))))))
246 (declare-function org-export-backend-options
"ox" (cl-x) t
)
247 (defun pcomplete/org-mode
/file-option
/options
()
248 "Complete arguments for the #+OPTIONS file option."
249 (while (pcomplete-here
250 (pcomplete-uniqify-list
252 ;; Hard-coded OPTION items always available.
253 '("H:" "\\n:" "num:" "timestamp:" "arch:" "author:" "c:"
254 "creator:" "date:" "d:" "email:" "*:" "e:" "::" "f:"
255 "inline:" "tex:" "p:" "pri:" "':" "-:" "stat:" "^:" "toc:"
256 "|:" "tags:" "tasks:" "<:" "todo:")
257 ;; OPTION items from registered back-ends.
259 (dolist (backend (bound-and-true-p
260 org-export-registered-backends
))
261 (dolist (option (org-export-backend-options backend
))
262 (let ((item (nth 2 option
)))
263 (when item
(push (concat item
":") items
)))))
266 (defun pcomplete/org-mode
/file-option
/infojs_opt
()
267 "Complete arguments for the #+INFOJS_OPT file option."
268 (while (pcomplete-here
269 (pcomplete-uniqify-list
270 (mapcar (lambda (item) (format "%s:" (car item
)))
271 (bound-and-true-p org-html-infojs-opts-table
))))))
273 (defun pcomplete/org-mode
/file-option
/bind
()
274 "Complete arguments for the #+BIND file option, which are variable names."
277 (lambda (a) (if (boundp a
) (setq vars
(cons (symbol-name a
) vars
)))))
278 (pcomplete-here vars
)))
280 (defvar org-link-abbrev-alist-local
)
281 (defvar org-link-abbrev-alist
)
282 (defun pcomplete/org-mode
/link
()
283 "Complete against defined #+LINK patterns."
285 (pcomplete-uniqify-list
287 (append (mapcar 'car org-link-abbrev-alist-local
)
288 (mapcar 'car org-link-abbrev-alist
))))))
290 (defvar org-entities
)
291 (defun pcomplete/org-mode
/tex
()
292 "Complete against TeX-style HTML entity names."
293 (require 'org-entities
)
294 (while (pcomplete-here
295 (pcomplete-uniqify-list (remove nil
(mapcar 'car-safe org-entities
)))
296 (substring pcomplete-stub
1))))
298 (defvar org-todo-keywords-1
)
299 (defun pcomplete/org-mode
/todo
()
300 "Complete against known TODO keywords."
301 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1
))))
303 (defvar org-todo-line-regexp
)
304 (defun pcomplete/org-mode
/searchhead
()
305 "Complete against all headings.
306 This needs more work, to handle headings with lots of spaces in them."
310 (goto-char (point-min))
312 (let ((case-fold-search nil
))
313 (while (re-search-forward org-todo-line-regexp nil t
)
314 (push (org-make-org-heading-search-string
315 (match-string-no-properties 3))
317 (pcomplete-uniqify-list tbl
)))
318 (substring pcomplete-stub
1))))
320 (defun pcomplete/org-mode
/tag
()
321 "Complete a tag name. Omit tags already set."
322 (while (pcomplete-here
323 (mapcar (lambda (x) (concat x
":"))
324 (let ((lst (pcomplete-uniqify-list
327 (mapcar (lambda (x) (org-string-nw-p (car x
)))
328 org-current-tag-alist
))
329 (mapcar #'car
(org-get-buffer-tags))))))
330 (dolist (tag (org-get-tags))
331 (setq lst
(delete tag lst
)))
333 (and (string-match ".*:" pcomplete-stub
)
334 (substring pcomplete-stub
(match-end 0))))))
336 (defun pcomplete/org-mode
/prop
()
337 "Complete a property name. Omit properties already set."
341 (let ((lst (pcomplete-uniqify-list
342 (copy-sequence (org-buffer-property-keys nil t t
)))))
343 (dolist (prop (org-entry-properties))
344 (setq lst
(delete (car prop
) lst
)))
346 (substring pcomplete-stub
1)))
348 (defun pcomplete/org-mode
/block-option
/src
()
349 "Complete the arguments of a begin_src block.
350 Complete a language in the first field, the header arguments and switches."
353 (lambda(x) (symbol-name (nth 3 x
)))
354 (cdr (car (cdr (memq :key-type
(plist-get
356 'org-babel-load-languages
)
358 (while (pcomplete-here
360 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
361 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
362 ":session" ":shebang" ":tangle" ":tangle-mode" ":var"))))
364 (defun pcomplete/org-mode
/block-option
/clocktable
()
365 "Complete keywords in a clocktable line."
366 (while (pcomplete-here '(":maxlevel" ":scope" ":lang"
367 ":tstart" ":tend" ":block" ":step"
368 ":stepskip0" ":fileskip0"
369 ":emphasize" ":link" ":narrow" ":indent"
370 ":tcolumns" ":level" ":compact" ":timestamp"
371 ":formula" ":formatter" ":wstart" ":mstart"))))
373 (defun org-pcomplete-case-double (list)
374 "Return list with both upcase and downcase version of all strings in LIST."
376 (while (setq e
(pop list
))
377 (setq res
(cons (downcase e
) (cons (upcase e
) res
))))
382 (provide 'org-pcomplete
)
384 ;;; org-pcomplete.el ends here