org-pcomplete.el 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. ;;; org-pcomplete.el --- In-buffer completion code
  2. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  3. ;;
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; John Wiegley <johnw at gnu dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Code:
  24. ;;;; Require other packages
  25. (eval-when-compile
  26. (require 'cl))
  27. (require 'org-macs)
  28. (require 'pcomplete)
  29. (declare-function org-split-string "org" (string &optional separators))
  30. (declare-function org-get-current-options "org-exp" ())
  31. (declare-function org-make-org-heading-search-string "org"
  32. (&optional string heading))
  33. (declare-function org-get-buffer-tags "org" ())
  34. (declare-function org-get-tags "org" ())
  35. (declare-function org-buffer-property-keys "org"
  36. (&optional include-specials include-defaults include-columns))
  37. (declare-function org-entry-properties "org" (&optional pom which specific))
  38. ;;;; Customization variables
  39. (defgroup org-complete nil
  40. "Outline-based notes management and organizer."
  41. :tag "Org"
  42. :group 'org)
  43. (defun org-thing-at-point ()
  44. "Examine the thing at point and let the caller know what it is.
  45. The return value is a string naming the thing at point."
  46. (let ((beg1 (save-excursion
  47. (skip-chars-backward (org-re "[:alnum:]_@"))
  48. (point)))
  49. (beg (save-excursion
  50. (skip-chars-backward "a-zA-Z0-9_:$")
  51. (point)))
  52. (line-to-here (buffer-substring (point-at-bol) (point))))
  53. (cond
  54. ((string-match "\\`[ \t]*#\\+begin: clocktable[ \t]+" line-to-here)
  55. (cons "block-option" "clocktable"))
  56. ((string-match "\\`[ \t]*#\\+begin_src[ \t]+" line-to-here)
  57. (cons "block-option" "src"))
  58. ((save-excursion
  59. (re-search-backward "^[ \t]*#\\+\\([A-Z_]+\\):.*"
  60. (line-beginning-position) t))
  61. (cons "file-option" (match-string-no-properties 1)))
  62. ((string-match "\\`[ \t]*#\\+[a-zA-Z]*\\'" line-to-here)
  63. (cons "file-option" nil))
  64. ((equal (char-before beg) ?\[)
  65. (cons "link" nil))
  66. ((equal (char-before beg) ?\\)
  67. (cons "tex" nil))
  68. ((string-match "\\`\\*+[ \t]+\\'"
  69. (buffer-substring (point-at-bol) beg))
  70. (cons "todo" nil))
  71. ((equal (char-before beg) ?*)
  72. (cons "searchhead" nil))
  73. ((and (equal (char-before beg1) ?:)
  74. (equal (char-after (point-at-bol)) ?*))
  75. (cons "tag" nil))
  76. ((and (equal (char-before beg1) ?:)
  77. (not (equal (char-after (point-at-bol)) ?*)))
  78. (cons "prop" nil))
  79. (t nil))))
  80. (defun org-command-at-point ()
  81. "Return the qualified name of the Org completion entity at point.
  82. When completing for #+STARTUP, for example, this function returns
  83. \"file-option/startup\"."
  84. (let ((thing (org-thing-at-point)))
  85. (cond
  86. ((string= "file-option" (car thing))
  87. (concat (car thing) "/" (downcase (cdr thing))))
  88. ((string= "block-option" (car thing))
  89. (concat (car thing) "/" (downcase (cdr thing))))
  90. (t
  91. (car thing)))))
  92. (defun org-parse-arguments ()
  93. "Parse whitespace separated arguments in the current region."
  94. (let ((begin (line-beginning-position))
  95. (end (line-end-position))
  96. begins args)
  97. (save-restriction
  98. (narrow-to-region begin end)
  99. (save-excursion
  100. (goto-char (point-min))
  101. (while (not (eobp))
  102. (skip-chars-forward " \t\n[")
  103. (setq begins (cons (point) begins))
  104. (skip-chars-forward "^ \t\n[")
  105. (setq args (cons (buffer-substring-no-properties
  106. (car begins) (point))
  107. args)))
  108. (cons (reverse args) (reverse begins))))))
  109. (defun org-pcomplete-initial ()
  110. "Calls the right completion function for first argument completions."
  111. (ignore
  112. (funcall (or (pcomplete-find-completion-function
  113. (car (org-thing-at-point)))
  114. pcomplete-default-completion-function))))
  115. (defvar org-additional-option-like-keywords)
  116. (defun pcomplete/org-mode/file-option ()
  117. "Complete against all valid file options."
  118. (require 'org-exp)
  119. (pcomplete-here
  120. (org-pcomplete-case-double
  121. (mapcar (lambda (x)
  122. (if (= ?: (aref x (1- (length x))))
  123. (concat x " ")
  124. x))
  125. (delq nil
  126. (pcomplete-uniqify-list
  127. (append
  128. (mapcar (lambda (x)
  129. (if (string-match "^#\\+\\([A-Z_]+:?\\)" x)
  130. (match-string 1 x)))
  131. (org-split-string (org-get-current-options) "\n"))
  132. org-additional-option-like-keywords)))))
  133. (substring pcomplete-stub 2)))
  134. (defvar org-startup-options)
  135. (defun pcomplete/org-mode/file-option/startup ()
  136. "Complete arguments for the #+STARTUP file option."
  137. (while (pcomplete-here
  138. (let ((opts (pcomplete-uniqify-list
  139. (mapcar 'car org-startup-options))))
  140. ;; Some options are mutually exclusive, and shouldn't be completed
  141. ;; against if certain other options have already been seen.
  142. (dolist (arg pcomplete-args)
  143. (cond
  144. ((string= arg "hidestars")
  145. (setq opts (delete "showstars" opts)))))
  146. opts))))
  147. (defun pcomplete/org-mode/file-option/bind ()
  148. "Complete arguments for the #+BIND file option, which are variable names"
  149. (let (vars)
  150. (mapatoms
  151. (lambda (a) (if (boundp a) (setq vars (cons (symbol-name a) vars)))))
  152. (pcomplete-here vars)))
  153. (defvar org-link-abbrev-alist-local)
  154. (defvar org-link-abbrev-alist)
  155. (defun pcomplete/org-mode/link ()
  156. "Complete against defined #+LINK patterns."
  157. (pcomplete-here
  158. (pcomplete-uniqify-list
  159. (copy-sequence
  160. (append (mapcar 'car org-link-abbrev-alist-local)
  161. (mapcar 'car org-link-abbrev-alist))))))
  162. (defvar org-entities)
  163. (defun pcomplete/org-mode/tex ()
  164. "Complete against TeX-style HTML entity names."
  165. (require 'org-entities)
  166. (while (pcomplete-here
  167. (pcomplete-uniqify-list (remove nil (mapcar 'car-safe org-entities)))
  168. (substring pcomplete-stub 1))))
  169. (defvar org-todo-keywords-1)
  170. (defun pcomplete/org-mode/todo ()
  171. "Complete against known TODO keywords."
  172. (pcomplete-here (pcomplete-uniqify-list (copy-sequence org-todo-keywords-1))))
  173. (defvar org-todo-line-regexp)
  174. (defun pcomplete/org-mode/searchhead ()
  175. "Complete against all headings.
  176. This needs more work, to handle headings with lots of spaces in them."
  177. (while
  178. (pcomplete-here
  179. (save-excursion
  180. (goto-char (point-min))
  181. (let (tbl)
  182. (while (re-search-forward org-todo-line-regexp nil t)
  183. (push (org-make-org-heading-search-string
  184. (match-string-no-properties 3) t)
  185. tbl))
  186. (pcomplete-uniqify-list tbl)))
  187. (substring pcomplete-stub 1))))
  188. (defvar org-tag-alist)
  189. (defun pcomplete/org-mode/tag ()
  190. "Complete a tag name. Omit tags already set."
  191. (while (pcomplete-here
  192. (mapcar (lambda (x)
  193. (concat x ":"))
  194. (let ((lst (pcomplete-uniqify-list
  195. (or (remove
  196. nil
  197. (mapcar (lambda (x)
  198. (and (stringp (car x)) (car x)))
  199. org-tag-alist))
  200. (mapcar 'car (org-get-buffer-tags))))))
  201. (dolist (tag (org-get-tags))
  202. (setq lst (delete tag lst)))
  203. lst))
  204. (and (string-match ".*:" pcomplete-stub)
  205. (substring pcomplete-stub (match-end 0))))))
  206. (defun pcomplete/org-mode/prop ()
  207. "Complete a property name. Omit properties already set."
  208. (pcomplete-here
  209. (mapcar (lambda (x)
  210. (concat x ": "))
  211. (let ((lst (pcomplete-uniqify-list
  212. (copy-sequence
  213. (org-buffer-property-keys nil t t)))))
  214. (dolist (prop (org-entry-properties))
  215. (setq lst (delete (car prop) lst)))
  216. lst))
  217. (substring pcomplete-stub 1)))
  218. (defun pcomplete/org-mode/block-option/src ()
  219. "Complete the arguments of a begin_src block.
  220. Complete a language in the first field, the header arguments and switches."
  221. (pcomplete-here
  222. (mapcar
  223. (lambda(x) (symbol-name (nth 3 x)))
  224. (cdr (car (cdr (memq :key-type (plist-get
  225. (symbol-plist
  226. 'org-babel-load-languages)
  227. 'custom-type)))))))
  228. (while (pcomplete-here
  229. '("-n" "-r" "-l"
  230. ":cache" ":colnames" ":comments" ":dir" ":eval" ":exports"
  231. ":file" ":hlines" ":no-expand" ":noweb" ":results" ":rownames"
  232. ":session" ":shebang" ":tangle" ":var"))))
  233. (defun pcomplete/org-mode/block-option/clocktable ()
  234. "Complete keywords in a clocktable line"
  235. (while (pcomplete-here '(":maxlevel" ":scope"
  236. ":tstart" ":tend" ":block" ":step"
  237. ":stepskip0" ":fileskip0"
  238. ":emphasize" ":link" ":narrow" ":indent"
  239. ":tcolumns" ":level" ":compact" ":timestamp"
  240. ":formula" ":formatter"))))
  241. (defun org-pcomplete-case-double (list)
  242. "Return list with both upcase and downcase version of all strings in LIST."
  243. (let (e res)
  244. (while (setq e (pop list))
  245. (setq res (cons (downcase e) (cons (upcase e) res))))
  246. (nreverse res)))
  247. ;;;; Finish up
  248. (provide 'org-pcomplete)
  249. ;;; org-pcomplete.el ends here