org-complete.el 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. ;;; org-complete.el --- In-buffer completion code
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  3. ;; 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. ;; Version: 6.31trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (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.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Code:
  26. ;;;; Require other packages
  27. (eval-when-compile
  28. (require 'cl))
  29. (require 'org-macs)
  30. (require 'pcomplete)
  31. ;;;; Customization variables
  32. (defgroup org-complete nil
  33. "Outline-based notes management and organizer."
  34. :tag "Org"
  35. :group 'org)
  36. (defun org-thing-at-point ()
  37. "Examine the thing at point and let the caller know what it is.
  38. The return value is a string naming the thing at point."
  39. (let ((beg1 (save-excursion
  40. (skip-chars-backward (org-re "[:alnum:]_@"))
  41. (point)))
  42. (beg (save-excursion
  43. (skip-chars-backward "a-zA-Z0-9_:$")
  44. (point))))
  45. (cond
  46. ((save-excursion
  47. (re-search-backward "^#\\+\\([A-Z_]+\\):.*"
  48. (line-beginning-position) t))
  49. (cons "file-option" (match-string-no-properties 1)))
  50. ((equal (char-before beg) ?\[)
  51. (cons "link" nil))
  52. ((equal (char-before beg) ?\\)
  53. (cons "tex" nil))
  54. ((string-match "\\`\\*+[ \t]+\\'"
  55. (buffer-substring (point-at-bol) beg))
  56. (cons "todo" nil))
  57. ((equal (char-before beg) ?*)
  58. (cons "searchhead" nil))
  59. ((and (equal (char-before beg1) ?:)
  60. (equal (char-after (point-at-bol)) ?*))
  61. (cons "tag" nil))
  62. ((and (equal (char-before beg1) ?:)
  63. (not (equal (char-after (point-at-bol)) ?*)))
  64. (cons "prop" nil))
  65. (t nil))))
  66. (defun org-command-at-point ()
  67. "Return the qualified name of the Org completion entity at point.
  68. When completing for #+STARTUP, for example, this function returns
  69. \"file-option/STARTUP\"."
  70. (let ((thing (org-thing-at-point)))
  71. (cond
  72. ((string= "file-option" (car thing))
  73. (concat (car thing) "/" (cdr thing)))
  74. (t
  75. (car thing)))))
  76. (defun org-parse-arguments ()
  77. "Parse whitespace separated arguments in the current region."
  78. (let ((begin (line-beginning-position))
  79. (end (line-end-position))
  80. begins args)
  81. (save-restriction
  82. (narrow-to-region begin end)
  83. (save-excursion
  84. (goto-char (point-min))
  85. (while (not (eobp))
  86. (skip-chars-forward " \t\n[")
  87. (setq begins (cons (point) begins))
  88. (skip-chars-forward "^ \t\n[")
  89. (setq args (cons (buffer-substring-no-properties
  90. (car begins) (point))
  91. args)))
  92. (cons (reverse args) (reverse begins))))))
  93. (defun org-complete-initial ()
  94. "Calls the right completion function for first argument completions."
  95. (ignore
  96. (funcall (or (pcomplete-find-completion-function
  97. (car (org-thing-at-point)))
  98. pcomplete-default-completion-function))))
  99. (defun pcomplete/org-mode/file-option ()
  100. "Complete against all valid file options."
  101. (require 'org-exp)
  102. (pcomplete-here
  103. (mapcar (lambda (x)
  104. (if (= ?: (aref x (1- (length x))))
  105. (concat x " ")
  106. x))
  107. (delq nil
  108. (pcomplete-uniqify-list
  109. (append
  110. (mapcar (lambda (x)
  111. (if (string-match "^#\\+\\([A-Z_]+:?\\)" x)
  112. (match-string 1 x)))
  113. (org-split-string (org-get-current-options) "\n"))
  114. org-additional-option-like-keywords))))
  115. (substring pcomplete-stub 2)))
  116. (defun pcomplete/org-mode/file-option/STARTUP ()
  117. "Complete arguments for the #+STARTUP file option."
  118. (while (pcomplete-here
  119. (let ((opts (pcomplete-uniqify-list
  120. (mapcar 'car org-startup-options))))
  121. ;; Some options are mutually exclusive, and shouldn't be completed
  122. ;; against if certain other options have already been seen.
  123. (dolist (arg pcomplete-args)
  124. (cond
  125. ((string= arg "hidestars")
  126. (setq opts (delete "showstars" opts)))))
  127. opts))))
  128. (defun pcomplete/org-mode/link ()
  129. "Complete against defined #+LINK patterns."
  130. (pcomplete-here
  131. (pcomplete-uniqify-list (append (mapcar 'car org-link-abbrev-alist-local)
  132. (mapcar 'car org-link-abbrev-alist)))))
  133. (defun pcomplete/org-mode/tex ()
  134. "Complete against TeX-style HTML entity names."
  135. (while (pcomplete-here
  136. (pcomplete-uniqify-list (mapcar 'car org-html-entities))
  137. (substring pcomplete-stub 1))))
  138. (defun pcomplete/org-mode/todo ()
  139. "Complete against known TODO keywords."
  140. (pcomplete-here (pcomplete-uniqify-list org-todo-keywords-1)))
  141. (defun pcomplete/org-mode/searchhead ()
  142. "Complete against all headings.
  143. This needs more work, to handle headings with lots of spaces in them."
  144. (while
  145. (pcomplete-here
  146. (save-excursion
  147. (goto-char (point-min))
  148. (let (tbl)
  149. (while (re-search-forward org-todo-line-regexp nil t)
  150. (push (org-make-org-heading-search-string
  151. (match-string-no-properties 3) t)
  152. tbl))
  153. (pcomplete-uniqify-list tbl)))
  154. (substring pcomplete-stub 1))))
  155. (defun pcomplete/org-mode/tag ()
  156. "Complete a tag name. Omit tags already set."
  157. (while (pcomplete-here
  158. (mapcar (lambda (x)
  159. (concat x ":"))
  160. (let ((lst (pcomplete-uniqify-list
  161. (or (mapcar 'car org-tag-alist)
  162. (mapcar 'car (org-get-buffer-tags))))))
  163. (dolist (tag (org-get-tags))
  164. (setq lst (delete tag lst)))
  165. lst))
  166. (and (string-match ".*:" pcomplete-stub)
  167. (substring pcomplete-stub (match-end 0))))))
  168. (defun pcomplete/org-mode/prop ()
  169. "Complete a property name. Omit properties already set."
  170. (pcomplete-here
  171. (mapcar (lambda (x)
  172. (concat x ": "))
  173. (let ((lst (pcomplete-uniqify-list
  174. (org-buffer-property-keys nil t t))))
  175. (dolist (prop (org-entry-properties))
  176. (setq lst (delete (car prop) lst)))
  177. lst))
  178. (substring pcomplete-stub 1)))
  179. ;;;; Finish up
  180. (provide 'org-complete)
  181. ;; arch-tag:
  182. ;;; org-complete.el ends here