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" (&optional pos local
))
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 (unless (fboundp 'pcomplete-uniquify-list
)
42 ;; The misspelled variant was made obsolete in Emacs 27.1
43 (defalias 'pcomplete-uniquify-list
'pcomplete-uniqify-list
))
45 ;;;; Customization variables
47 (defvar org-drawer-regexp
)
48 (defvar org-property-re
)
49 (defvar org-current-tag-alist
)
51 (defun org-thing-at-point ()
52 "Examine the thing at point and let the caller know what it is.
53 The return value is a string naming the thing at point."
54 (let ((beg1 (save-excursion
55 (skip-chars-backward "[:alnum:]-_@")
58 (skip-chars-backward "a-zA-Z0-9-_:$")
60 (line-to-here (buffer-substring (point-at-bol) (point))))
62 ((string-match "\\`[ \t]*#\\+begin: clocktable[ \t]+" line-to-here
)
63 (cons "block-option" "clocktable"))
64 ((string-match "\\`[ \t]*#\\+begin_src[ \t]+" line-to-here
)
65 (cons "block-option" "src"))
67 (re-search-backward "^[ \t]*#\\+\\([A-Z_]+\\):.*"
68 (line-beginning-position) t
))
69 (cons "file-option" (match-string-no-properties 1)))
70 ((string-match "\\`[ \t]*#\\+[a-zA-Z_]*\\'" line-to-here
)
71 (cons "file-option" nil
))
72 ((equal (char-before beg
) ?\
[)
74 ((equal (char-before beg
) ?
\\)
76 ((string-match "\\`\\*+[ \t]+\\'"
77 (buffer-substring (point-at-bol) beg
))
79 ((equal (char-before beg
) ?
*)
80 (cons "searchhead" nil
))
81 ((and (equal (char-before beg1
) ?
:)
82 (equal (char-after (point-at-bol)) ?
*))
84 ((and (equal (char-before beg1
) ?
:)
85 (not (equal (char-after (point-at-bol)) ?
*))
87 (move-beginning-of-line 1)
88 (skip-chars-backward "[ \t\n]")
89 ;; org-drawer-regexp matches a whole line but while
90 ;; looking-back, we just ignore trailing whitespaces
91 (or (looking-back (substring org-drawer-regexp
0 -
1)
92 (line-beginning-position))
93 (looking-back org-property-re
94 (line-beginning-position)))))
96 ((and (equal (char-before beg1
) ?
:)
97 (not (equal (char-after (point-at-bol)) ?
*)))
101 (defun org-command-at-point ()
102 "Return the qualified name of the Org completion entity at point.
103 When completing for #+STARTUP, for example, this function returns
104 \"file-option/startup\"."
105 (let ((thing (org-thing-at-point)))
107 ((string= "file-option" (car thing
))
109 (and (cdr thing
) (concat "/" (downcase (cdr thing
))))))
110 ((string= "block-option" (car thing
))
111 (concat (car thing
) "/" (downcase (cdr thing
))))
114 (defun org-parse-arguments ()
115 "Parse whitespace separated arguments in the current region."
116 (let ((begin (line-beginning-position))
117 (end (line-end-position))
120 (narrow-to-region begin end
)
122 (goto-char (point-min))
124 (skip-chars-forward " \t\n[")
125 (setq begins
(cons (point) begins
))
126 (skip-chars-forward "^ \t\n[")
127 (setq args
(cons (buffer-substring-no-properties
128 (car begins
) (point))
130 (cons (reverse args
) (reverse begins
))))))
132 (defun org-pcomplete-initial ()
133 "Calls the right completion function for first argument completions."
135 (funcall (or (pcomplete-find-completion-function
136 (car (org-thing-at-point)))
137 pcomplete-default-completion-function
))))
139 (defvar org-options-keywords
) ; From org.el
140 (defvar org-element-affiliated-keywords
) ; From org-element.el
141 (declare-function org-get-export-keywords
"org" ())
142 (defun pcomplete/org-mode
/file-option
()
143 "Complete against all valid file options."
144 (require 'org-element
)
146 (org-pcomplete-case-double
147 (append (mapcar (lambda (keyword) (concat keyword
" "))
148 org-options-keywords
)
149 (mapcar (lambda (keyword) (concat keyword
": "))
150 org-element-affiliated-keywords
)
153 '("CENTER" "COMMENT" "EXAMPLE" "EXPORT" "QUOTE" "SRC"
156 (push (format "END_%s" name
) block-names
)
157 (push (concat "BEGIN_"
159 ;; Since language is compulsory in
160 ;; export blocks source blocks, add
162 (and (member name
'("EXPORT" "SRC")) " "))
164 (push (format "ATTR_%s: " name
) block-names
)))
165 (mapcar (lambda (keyword) (concat keyword
": "))
166 (org-get-export-keywords))))
167 (substring pcomplete-stub
2)))
169 (defun pcomplete/org-mode
/file-option
/author
()
170 "Complete arguments for the #+AUTHOR file option."
171 (pcomplete-here (list user-full-name
)))
173 (defvar org-time-stamp-formats
)
174 (defun pcomplete/org-mode
/file-option
/date
()
175 "Complete arguments for the #+DATE file option."
176 (pcomplete-here (list (format-time-string (car org-time-stamp-formats
)))))
178 (defun pcomplete/org-mode
/file-option
/email
()
179 "Complete arguments for the #+EMAIL file option."
180 (pcomplete-here (list user-mail-address
)))
182 (defvar org-export-exclude-tags
)
183 (defun pcomplete/org-mode
/file-option
/exclude_tags
()
184 "Complete arguments for the #+EXCLUDE_TAGS file option."
187 (and org-export-exclude-tags
188 (list (mapconcat 'identity org-export-exclude-tags
" ")))))
190 (defvar org-file-tags
)
191 (defun pcomplete/org-mode
/file-option
/filetags
()
192 "Complete arguments for the #+FILETAGS file option."
193 (pcomplete-here (and org-file-tags
(mapconcat 'identity org-file-tags
" "))))
195 (defvar org-export-default-language
)
196 (defun pcomplete/org-mode
/file-option
/language
()
197 "Complete arguments for the #+LANGUAGE file option."
200 (pcomplete-uniquify-list
201 (list org-export-default-language
"en"))))
203 (defvar org-default-priority
)
204 (defvar org-highest-priority
)
205 (defvar org-lowest-priority
)
206 (defun pcomplete/org-mode
/file-option
/priorities
()
207 "Complete arguments for the #+PRIORITIES file option."
208 (pcomplete-here (list (format "%c %c %c"
211 org-default-priority
))))
213 (defvar org-export-select-tags
)
214 (defun pcomplete/org-mode
/file-option
/select_tags
()
215 "Complete arguments for the #+SELECT_TAGS file option."
218 (and org-export-select-tags
219 (list (mapconcat 'identity org-export-select-tags
" ")))))
221 (defvar org-startup-options
)
222 (defun pcomplete/org-mode
/file-option
/startup
()
223 "Complete arguments for the #+STARTUP file option."
224 (while (pcomplete-here
225 (let ((opts (pcomplete-uniquify-list
226 (mapcar 'car org-startup-options
))))
227 ;; Some options are mutually exclusive, and shouldn't be completed
228 ;; against if certain other options have already been seen.
229 (dolist (arg pcomplete-args
)
231 ((string= arg
"hidestars")
232 (setq opts
(delete "showstars" opts
)))))
235 (defun pcomplete/org-mode
/file-option
/tags
()
236 "Complete arguments for the #+TAGS file option."
238 (list (org-tag-alist-to-string org-current-tag-alist
))))
240 (defun pcomplete/org-mode
/file-option
/title
()
241 "Complete arguments for the #+TITLE file option."
243 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
244 (list (or (and visited-file
245 (file-name-sans-extension
246 (file-name-nondirectory visited-file
)))
247 (buffer-name (buffer-base-buffer)))))))
250 (declare-function org-export-backend-options
"ox" (cl-x) t
)
251 (defun pcomplete/org-mode
/file-option
/options
()
252 "Complete arguments for the #+OPTIONS file option."
253 (while (pcomplete-here
254 (pcomplete-uniquify-list
256 ;; Hard-coded OPTION items always available.
257 '("H:" "\\n:" "num:" "timestamp:" "arch:" "author:" "c:"
258 "creator:" "date:" "d:" "email:" "*:" "e:" "::" "f:"
259 "inline:" "tex:" "p:" "pri:" "':" "-:" "stat:" "^:" "toc:"
260 "|:" "tags:" "tasks:" "<:" "todo:")
261 ;; OPTION items from registered back-ends.
263 (dolist (backend (bound-and-true-p
264 org-export-registered-backends
))
265 (dolist (option (org-export-backend-options backend
))
266 (let ((item (nth 2 option
)))
267 (when item
(push (concat item
":") items
)))))
270 (defun pcomplete/org-mode
/file-option
/infojs_opt
()
271 "Complete arguments for the #+INFOJS_OPT file option."
272 (while (pcomplete-here
273 (pcomplete-uniquify-list
274 (mapcar (lambda (item) (format "%s:" (car item
)))
275 (bound-and-true-p org-html-infojs-opts-table
))))))
277 (defun pcomplete/org-mode
/file-option
/bind
()
278 "Complete arguments for the #+BIND file option, which are variable names."
281 (lambda (a) (when (boundp a
) (setq vars
(cons (symbol-name a
) vars
)))))
282 (pcomplete-here vars
)))
284 (defvar org-link-abbrev-alist-local
)
285 (defvar org-link-abbrev-alist
)
286 (defun pcomplete/org-mode
/link
()
287 "Complete against defined #+LINK patterns."
289 (pcomplete-uniquify-list
291 (append (mapcar 'car org-link-abbrev-alist-local
)
292 (mapcar 'car org-link-abbrev-alist
))))))
294 (defvar org-entities
)
295 (defun pcomplete/org-mode
/tex
()
296 "Complete against TeX-style HTML entity names."
297 (require 'org-entities
)
298 (while (pcomplete-here
299 (pcomplete-uniquify-list (remove nil
(mapcar 'car-safe org-entities
)))
300 (substring pcomplete-stub
1))))
302 (defvar org-todo-keywords-1
)
303 (defun pcomplete/org-mode
/todo
()
304 "Complete against known TODO keywords."
305 (pcomplete-here (pcomplete-uniquify-list (copy-sequence org-todo-keywords-1
))))
307 (defvar org-todo-line-regexp
)
308 (defun pcomplete/org-mode
/searchhead
()
309 "Complete against all headings.
310 This needs more work, to handle headings with lots of spaces in them."
314 (goto-char (point-min))
316 (let ((case-fold-search nil
))
317 (while (re-search-forward org-todo-line-regexp nil t
)
318 (push (org-make-org-heading-search-string
319 (match-string-no-properties 3))
321 (pcomplete-uniquify-list tbl
)))
322 (substring pcomplete-stub
1))))
324 (defun pcomplete/org-mode
/tag
()
325 "Complete a tag name. Omit tags already set."
326 (while (pcomplete-here
327 (mapcar (lambda (x) (concat x
":"))
328 (let ((lst (pcomplete-uniquify-list
331 (mapcar (lambda (x) (org-string-nw-p (car x
)))
332 org-current-tag-alist
))
333 (mapcar #'car
(org-get-buffer-tags))))))
334 (dolist (tag (org-get-tags nil t
))
335 (setq lst
(delete tag lst
)))
337 (and (string-match ".*:" pcomplete-stub
)
338 (substring pcomplete-stub
(match-end 0))))))
340 (defun pcomplete/org-mode
/prop
()
341 "Complete a property name. Omit properties already set."
345 (let ((lst (pcomplete-uniquify-list
346 (copy-sequence (org-buffer-property-keys nil t t
)))))
347 (dolist (prop (org-entry-properties))
348 (setq lst
(delete (car prop
) lst
)))
350 (substring pcomplete-stub
1)))
352 (defun pcomplete/org-mode
/block-option
/src
()
353 "Complete the arguments of a begin_src block.
354 Complete a language in the first field, the header arguments and switches."
357 (lambda(x) (symbol-name (nth 3 x
)))
358 (cdr (car (cdr (memq :key-type
(plist-get
360 'org-babel-load-languages
)
362 (while (pcomplete-here
364 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
365 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
366 ":session" ":shebang" ":tangle" ":tangle-mode" ":var"))))
368 (defun pcomplete/org-mode
/block-option
/clocktable
()
369 "Complete keywords in a clocktable line."
370 (while (pcomplete-here '(":maxlevel" ":scope" ":lang"
371 ":tstart" ":tend" ":block" ":step"
372 ":stepskip0" ":fileskip0"
373 ":emphasize" ":link" ":narrow" ":indent"
374 ":tcolumns" ":level" ":compact" ":timestamp"
375 ":formula" ":formatter" ":wstart" ":mstart"))))
377 (defun org-pcomplete-case-double (list)
378 "Return list with both upcase and downcase version of all strings in LIST."
380 (while (setq e
(pop list
))
381 (setq res
(cons (downcase e
) (cons (upcase e
) res
))))
386 (provide 'org-pcomplete
)
388 ;;; org-pcomplete.el ends here