org-pcomplete.el 9.1 KB

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