1 ;;; org-pcomplete.el --- In-buffer Completion Code -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2017 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 <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 (defgroup org-complete nil
44 "Outline-based notes management and organizer."
48 (defvar org-drawer-regexp
)
49 (defvar org-property-re
)
50 (defvar org-current-tag-alist
)
52 (defun org-thing-at-point ()
53 "Examine the thing at point and let the caller know what it is.
54 The return value is a string naming the thing at point."
55 (let ((beg1 (save-excursion
56 (skip-chars-backward "[:alnum:]-_@")
59 (skip-chars-backward "a-zA-Z0-9-_:$")
61 (line-to-here (buffer-substring (point-at-bol) (point))))
63 ((string-match "\\`[ \t]*#\\+begin: clocktable[ \t]+" line-to-here
)
64 (cons "block-option" "clocktable"))
65 ((string-match "\\`[ \t]*#\\+begin_src[ \t]+" line-to-here
)
66 (cons "block-option" "src"))
68 (re-search-backward "^[ \t]*#\\+\\([A-Z_]+\\):.*"
69 (line-beginning-position) t
))
70 (cons "file-option" (match-string-no-properties 1)))
71 ((string-match "\\`[ \t]*#\\+[a-zA-Z_]*\\'" line-to-here
)
72 (cons "file-option" nil
))
73 ((equal (char-before beg
) ?\
[)
75 ((equal (char-before beg
) ?
\\)
77 ((string-match "\\`\\*+[ \t]+\\'"
78 (buffer-substring (point-at-bol) beg
))
80 ((equal (char-before beg
) ?
*)
81 (cons "searchhead" nil
))
82 ((and (equal (char-before beg1
) ?
:)
83 (equal (char-after (point-at-bol)) ?
*))
85 ((and (equal (char-before beg1
) ?
:)
86 (not (equal (char-after (point-at-bol)) ?
*))
88 (move-beginning-of-line 1)
89 (skip-chars-backward "[ \t\n]")
90 ;; org-drawer-regexp matches a whole line but while
91 ;; looking-back, we just ignore trailing whitespaces
92 (or (looking-back (substring org-drawer-regexp
0 -
1)
93 (line-beginning-position))
94 (looking-back org-property-re
95 (line-beginning-position)))))
97 ((and (equal (char-before beg1
) ?
:)
98 (not (equal (char-after (point-at-bol)) ?
*)))
102 (defun org-command-at-point ()
103 "Return the qualified name of the Org completion entity at point.
104 When completing for #+STARTUP, for example, this function returns
105 \"file-option/startup\"."
106 (let ((thing (org-thing-at-point)))
108 ((string= "file-option" (car thing
))
110 (and (cdr thing
) (concat "/" (downcase (cdr thing
))))))
111 ((string= "block-option" (car thing
))
112 (concat (car thing
) "/" (downcase (cdr thing
))))
115 (defun org-parse-arguments ()
116 "Parse whitespace separated arguments in the current region."
117 (let ((begin (line-beginning-position))
118 (end (line-end-position))
121 (narrow-to-region begin end
)
123 (goto-char (point-min))
125 (skip-chars-forward " \t\n[")
126 (setq begins
(cons (point) begins
))
127 (skip-chars-forward "^ \t\n[")
128 (setq args
(cons (buffer-substring-no-properties
129 (car begins
) (point))
131 (cons (reverse args
) (reverse begins
))))))
133 (defun org-pcomplete-initial ()
134 "Calls the right completion function for first argument completions."
136 (funcall (or (pcomplete-find-completion-function
137 (car (org-thing-at-point)))
138 pcomplete-default-completion-function
))))
140 (defvar org-options-keywords
) ; From org.el
141 (defvar org-element-affiliated-keywords
) ; From org-element.el
142 (declare-function org-get-export-keywords
"org" ())
143 (defun pcomplete/org-mode
/file-option
()
144 "Complete against all valid file options."
145 (require 'org-element
)
147 (org-pcomplete-case-double
148 (append (mapcar (lambda (keyword) (concat keyword
" "))
149 org-options-keywords
)
150 (mapcar (lambda (keyword) (concat keyword
": "))
151 org-element-affiliated-keywords
)
154 '("CENTER" "COMMENT" "EXAMPLE" "EXPORT" "QUOTE" "SRC"
157 (push (format "END_%s" name
) block-names
)
158 (push (concat "BEGIN_"
160 ;; Since language is compulsory in
161 ;; export blocks source blocks, add
163 (and (member name
'("EXPORT" "SRC")) " "))
165 (push (format "ATTR_%s: " name
) block-names
)))
166 (mapcar (lambda (keyword) (concat keyword
": "))
167 (org-get-export-keywords))))
168 (substring pcomplete-stub
2)))
170 (defun pcomplete/org-mode
/file-option
/author
()
171 "Complete arguments for the #+AUTHOR file option."
172 (pcomplete-here (list user-full-name
)))
174 (defvar org-time-stamp-formats
)
175 (defun pcomplete/org-mode
/file-option
/date
()
176 "Complete arguments for the #+DATE file option."
177 (pcomplete-here (list (format-time-string (car org-time-stamp-formats
)))))
179 (defun pcomplete/org-mode
/file-option
/email
()
180 "Complete arguments for the #+EMAIL file option."
181 (pcomplete-here (list user-mail-address
)))
183 (defvar org-export-exclude-tags
)
184 (defun pcomplete/org-mode
/file-option
/exclude_tags
()
185 "Complete arguments for the #+EXCLUDE_TAGS file option."
188 (and org-export-exclude-tags
189 (list (mapconcat 'identity org-export-exclude-tags
" ")))))
191 (defvar org-file-tags
)
192 (defun pcomplete/org-mode
/file-option
/filetags
()
193 "Complete arguments for the #+FILETAGS file option."
194 (pcomplete-here (and org-file-tags
(mapconcat 'identity org-file-tags
" "))))
196 (defvar org-export-default-language
)
197 (defun pcomplete/org-mode
/file-option
/language
()
198 "Complete arguments for the #+LANGUAGE file option."
201 (pcomplete-uniqify-list
202 (list org-export-default-language
"en"))))
204 (defvar org-default-priority
)
205 (defvar org-highest-priority
)
206 (defvar org-lowest-priority
)
207 (defun pcomplete/org-mode
/file-option
/priorities
()
208 "Complete arguments for the #+PRIORITIES file option."
209 (pcomplete-here (list (format "%c %c %c"
212 org-default-priority
))))
214 (defvar org-export-select-tags
)
215 (defun pcomplete/org-mode
/file-option
/select_tags
()
216 "Complete arguments for the #+SELECT_TAGS file option."
219 (and org-export-select-tags
220 (list (mapconcat 'identity org-export-select-tags
" ")))))
222 (defvar org-startup-options
)
223 (defun pcomplete/org-mode
/file-option
/startup
()
224 "Complete arguments for the #+STARTUP file option."
225 (while (pcomplete-here
226 (let ((opts (pcomplete-uniqify-list
227 (mapcar 'car org-startup-options
))))
228 ;; Some options are mutually exclusive, and shouldn't be completed
229 ;; against if certain other options have already been seen.
230 (dolist (arg pcomplete-args
)
232 ((string= arg
"hidestars")
233 (setq opts
(delete "showstars" opts
)))))
236 (defun pcomplete/org-mode
/file-option
/tags
()
237 "Complete arguments for the #+TAGS file option."
239 (list (org-tag-alist-to-string org-current-tag-alist
))))
241 (defun pcomplete/org-mode
/file-option
/title
()
242 "Complete arguments for the #+TITLE file option."
244 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
245 (list (or (and visited-file
246 (file-name-sans-extension
247 (file-name-nondirectory visited-file
)))
248 (buffer-name (buffer-base-buffer)))))))
251 (declare-function org-export-backend-options
"ox" (cl-x) t
)
252 (defun pcomplete/org-mode
/file-option
/options
()
253 "Complete arguments for the #+OPTIONS file option."
254 (while (pcomplete-here
255 (pcomplete-uniqify-list
257 ;; Hard-coded OPTION items always available.
258 '("H:" "\\n:" "num:" "timestamp:" "arch:" "author:" "c:"
259 "creator:" "date:" "d:" "email:" "*:" "e:" "::" "f:"
260 "inline:" "tex:" "p:" "pri:" "':" "-:" "stat:" "^:" "toc:"
261 "|:" "tags:" "tasks:" "<:" "todo:")
262 ;; OPTION items from registered back-ends.
264 (dolist (backend (bound-and-true-p
265 org-export-registered-backends
))
266 (dolist (option (org-export-backend-options backend
))
267 (let ((item (nth 2 option
)))
268 (when item
(push (concat item
":") items
)))))
271 (defun pcomplete/org-mode
/file-option
/infojs_opt
()
272 "Complete arguments for the #+INFOJS_OPT file option."
273 (while (pcomplete-here
274 (pcomplete-uniqify-list
275 (mapcar (lambda (item) (format "%s:" (car item
)))
276 (bound-and-true-p org-html-infojs-opts-table
))))))
278 (defun pcomplete/org-mode
/file-option
/bind
()
279 "Complete arguments for the #+BIND file option, which are variable names."
282 (lambda (a) (if (boundp a
) (setq vars
(cons (symbol-name a
) vars
)))))
283 (pcomplete-here vars
)))
285 (defvar org-link-abbrev-alist-local
)
286 (defvar org-link-abbrev-alist
)
287 (defun pcomplete/org-mode
/link
()
288 "Complete against defined #+LINK patterns."
290 (pcomplete-uniqify-list
292 (append (mapcar 'car org-link-abbrev-alist-local
)
293 (mapcar 'car org-link-abbrev-alist
))))))
295 (defvar org-entities
)
296 (defun pcomplete/org-mode
/tex
()
297 "Complete against TeX-style HTML entity names."
298 (require 'org-entities
)
299 (while (pcomplete-here
300 (pcomplete-uniqify-list (remove nil
(mapcar 'car-safe org-entities
)))
301 (substring pcomplete-stub
1))))
303 (defvar org-todo-keywords-1
)
304 (defun pcomplete/org-mode
/todo
()
305 "Complete against known TODO keywords."
306 (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1
))))
308 (defvar org-todo-line-regexp
)
309 (defun pcomplete/org-mode
/searchhead
()
310 "Complete against all headings.
311 This needs more work, to handle headings with lots of spaces in them."
315 (goto-char (point-min))
317 (let ((case-fold-search nil
))
318 (while (re-search-forward org-todo-line-regexp nil t
)
319 (push (org-make-org-heading-search-string
320 (match-string-no-properties 3))
322 (pcomplete-uniqify-list tbl
)))
323 (substring pcomplete-stub
1))))
325 (defun pcomplete/org-mode
/tag
()
326 "Complete a tag name. Omit tags already set."
327 (while (pcomplete-here
328 (mapcar (lambda (x) (concat x
":"))
329 (let ((lst (pcomplete-uniqify-list
332 (mapcar (lambda (x) (org-string-nw-p (car x
)))
333 org-current-tag-alist
))
334 (mapcar #'car
(org-get-buffer-tags))))))
335 (dolist (tag (org-get-tags))
336 (setq lst
(delete tag lst
)))
338 (and (string-match ".*:" pcomplete-stub
)
339 (substring pcomplete-stub
(match-end 0))))))
341 (defun pcomplete/org-mode
/prop
()
342 "Complete a property name. Omit properties already set."
346 (let ((lst (pcomplete-uniqify-list
347 (copy-sequence (org-buffer-property-keys nil t t
)))))
348 (dolist (prop (org-entry-properties))
349 (setq lst
(delete (car prop
) lst
)))
351 (substring pcomplete-stub
1)))
353 (defun pcomplete/org-mode
/block-option
/src
()
354 "Complete the arguments of a begin_src block.
355 Complete a language in the first field, the header arguments and switches."
358 (lambda(x) (symbol-name (nth 3 x
)))
359 (cdr (car (cdr (memq :key-type
(plist-get
361 'org-babel-load-languages
)
363 (while (pcomplete-here
365 ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
366 ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
367 ":session" ":shebang" ":tangle" ":tangle-mode" ":var"))))
369 (defun pcomplete/org-mode
/block-option
/clocktable
()
370 "Complete keywords in a clocktable line."
371 (while (pcomplete-here '(":maxlevel" ":scope" ":lang"
372 ":tstart" ":tend" ":block" ":step"
373 ":stepskip0" ":fileskip0"
374 ":emphasize" ":link" ":narrow" ":indent"
375 ":tcolumns" ":level" ":compact" ":timestamp"
376 ":formula" ":formatter" ":wstart" ":mstart"))))
378 (defun org-pcomplete-case-double (list)
379 "Return list with both upcase and downcase version of all strings in LIST."
381 (while (setq e
(pop list
))
382 (setq res
(cons (downcase e
) (cons (upcase e
) res
))))
387 (provide 'org-pcomplete
)
389 ;;; org-pcomplete.el ends here