Remove auto-repair of malformed property drawers
[org-mode/org-tableheadings.git] / lisp / org-pcomplete.el
blobaeaef4b8316ebd238d774d126eec5278cd2d5408
1 ;;; org-pcomplete.el --- In-buffer Completion Code -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
4 ;;
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
9 ;;
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 <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Code:
28 ;;;; Require other packages
30 (require 'org-macs)
31 (require 'org-compat)
32 (require 'pcomplete)
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."
45 :tag "Org"
46 :group 'org)
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:]-_@")
57 (point)))
58 (beg (save-excursion
59 (skip-chars-backward "a-zA-Z0-9-_:$")
60 (point)))
61 (line-to-here (buffer-substring (point-at-bol) (point))))
62 (cond
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"))
67 ((save-excursion
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) ?\[)
74 (cons "link" nil))
75 ((equal (char-before beg) ?\\)
76 (cons "tex" nil))
77 ((string-match "\\`\\*+[ \t]+\\'"
78 (buffer-substring (point-at-bol) beg))
79 (cons "todo" nil))
80 ((equal (char-before beg) ?*)
81 (cons "searchhead" nil))
82 ((and (equal (char-before beg1) ?:)
83 (equal (char-after (point-at-bol)) ?*))
84 (cons "tag" nil))
85 ((and (equal (char-before beg1) ?:)
86 (not (equal (char-after (point-at-bol)) ?*))
87 (save-excursion
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)))))
96 (cons "prop" nil))
97 ((and (equal (char-before beg1) ?:)
98 (not (equal (char-after (point-at-bol)) ?*)))
99 (cons "drawer" nil))
100 (t nil))))
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)))
107 (cond
108 ((string= "file-option" (car thing))
109 (concat (car thing)
110 (and (cdr thing) (concat "/" (downcase (cdr thing))))))
111 ((string= "block-option" (car thing))
112 (concat (car thing) "/" (downcase (cdr thing))))
113 (t (car 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))
119 begins args)
120 (save-restriction
121 (narrow-to-region begin end)
122 (save-excursion
123 (goto-char (point-min))
124 (while (not (eobp))
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))
130 args)))
131 (cons (reverse args) (reverse begins))))))
133 (defun org-pcomplete-initial ()
134 "Calls the right completion function for first argument completions."
135 (ignore
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)
146 (pcomplete-here
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)
152 (let (block-names)
153 (dolist (name
154 '("CENTER" "COMMENT" "EXAMPLE" "EXPORT" "QUOTE" "SRC"
155 "VERSE")
156 block-names)
157 (push (format "END_%s" name) block-names)
158 (push (concat "BEGIN_"
159 name
160 ;; Since language is compulsory in
161 ;; export blocks source blocks, add
162 ;; a space.
163 (and (member name '("EXPORT" "SRC")) " "))
164 block-names)
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."
186 (require 'ox)
187 (pcomplete-here
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."
199 (require 'ox)
200 (pcomplete-here
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"
210 org-highest-priority
211 org-lowest-priority
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."
217 (require 'ox)
218 (pcomplete-here
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)
231 (cond
232 ((string= arg "hidestars")
233 (setq opts (delete "showstars" opts)))))
234 opts))))
236 (defun pcomplete/org-mode/file-option/tags ()
237 "Complete arguments for the #+TAGS file option."
238 (pcomplete-here
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."
243 (pcomplete-here
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
256 (append
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.
263 (let (items)
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)))))
269 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."
280 (let (vars)
281 (mapatoms
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."
289 (pcomplete-here
290 (pcomplete-uniqify-list
291 (copy-sequence
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."
312 (while
313 (pcomplete-here
314 (save-excursion
315 (goto-char (point-min))
316 (let (tbl)
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))
321 tbl)))
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
330 (or (remq
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)))
337 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."
343 (pcomplete-here
344 (mapcar (lambda (x)
345 (concat x ": "))
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)))
350 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."
356 (pcomplete-here
357 (mapcar
358 (lambda(x) (symbol-name (nth 3 x)))
359 (cdr (car (cdr (memq :key-type (plist-get
360 (symbol-plist
361 'org-babel-load-languages)
362 'custom-type)))))))
363 (while (pcomplete-here
364 '("-n" "-r" "-l"
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."
380 (let (e res)
381 (while (setq e (pop list))
382 (setq res (cons (downcase e) (cons (upcase e) res))))
383 (nreverse res)))
385 ;;;; Finish up
387 (provide 'org-pcomplete)
389 ;;; org-pcomplete.el ends here