org-goto.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. ;;; org-goto.el --- Fast navigation in an Org buffer -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2017 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  16. ;;; Code:
  17. (require 'org-macs)
  18. (declare-function org-at-heading-p "org" (&optional ignored))
  19. (declare-function org-beginning-of-line "org" (&optional n))
  20. (declare-function org-defkey "org" (keymap key def))
  21. (declare-function org-fit-window-to-buffer "org" (&optional window max-height min-height shrink-only))
  22. (declare-function org-invisible-p "org" (&optional pos))
  23. (declare-function org-mark-ring-push "org" (&optional pos buffer))
  24. (declare-function org-no-popups "org" (&rest body))
  25. (declare-function org-overview "org" ())
  26. (declare-function org-refile-check-position "org" (refile-pointer))
  27. (declare-function org-refile-get-location "org" (&optional prompt default-buffer new-nodes))
  28. (declare-function org-show-context "org" (&optional key))
  29. (declare-function org-show-set-visibility "org" (detail))
  30. (defvar org-complex-heading-regexp)
  31. (defvar org-startup-align-all-tables)
  32. (defvar org-startup-folded)
  33. (defvar org-startup-truncated)
  34. (defvar org-special-ctrl-a/e)
  35. (defvar org-refile-target-verify-function)
  36. (defvar org-refile-use-outline-path)
  37. (defvar org-refile-targets)
  38. (defvar org-goto-exit-command nil)
  39. (defvar org-goto-local-auto-isearch-map nil)
  40. (defvar org-goto-map nil)
  41. (defvar org-goto-marker nil)
  42. (defvar org-goto-selected-point nil)
  43. (defvar org-goto-start-pos nil)
  44. (defvar org-goto-window-configuration nil)
  45. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  46. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  47. (defconst org-goto-help
  48. "Browse buffer copy, to find location or copy text.%s
  49. RET=jump to location C-g=quit and return to previous location
  50. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  51. ;;; Customization
  52. (defgroup org-goto nil
  53. "Options concerning Org Goto navigation interface."
  54. :tag "Org Goto"
  55. :group 'org)
  56. (defcustom org-goto-interface 'outline
  57. "The default interface to be used for `org-goto'.
  58. Allowed values are:
  59. `outline'
  60. The interface shows an outline of the relevant file and the
  61. correct heading is found by moving through the outline or by
  62. searching with incremental search.
  63. `outline-path-completion'
  64. Headlines in the current buffer are offered via completion.
  65. This is the interface also used by the refile command."
  66. :group 'org-goto
  67. :type '(choice
  68. (const :tag "Outline" outline)
  69. (const :tag "Outline-path-completion" outline-path-completion)))
  70. (defcustom org-goto-max-level 5
  71. "Maximum target level when running `org-goto' with refile interface."
  72. :group 'org-goto
  73. :type 'integer)
  74. (defcustom org-goto-auto-isearch t
  75. "Non-nil means typing characters in `org-goto' starts incremental search.
  76. When nil, you can use these keybindings to navigate the buffer:
  77. q Quit the Org Goto interface
  78. n Go to the next visible heading
  79. p Go to the previous visible heading
  80. f Go one heading forward on same level
  81. b Go one heading backward on same level
  82. u Go one heading up"
  83. :group 'org-goto
  84. :type 'boolean)
  85. ;;; Internal functions
  86. (defun org-goto--set-map ()
  87. "Set the keymap `org-goto'."
  88. (setq org-goto-map
  89. (let ((map (make-sparse-keymap)))
  90. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  91. mouse-drag-region universal-argument org-occur)))
  92. (dolist (cmd cmds)
  93. (substitute-key-definition cmd cmd map global-map)))
  94. (suppress-keymap map)
  95. (org-defkey map "\C-m" 'org-goto-ret)
  96. (org-defkey map [(return)] 'org-goto-ret)
  97. (org-defkey map [(left)] 'org-goto-left)
  98. (org-defkey map [(right)] 'org-goto-right)
  99. (org-defkey map [(control ?g)] 'org-goto-quit)
  100. (org-defkey map "\C-i" 'org-cycle)
  101. (org-defkey map [(tab)] 'org-cycle)
  102. (org-defkey map [(down)] 'outline-next-visible-heading)
  103. (org-defkey map [(up)] 'outline-previous-visible-heading)
  104. (if org-goto-auto-isearch
  105. (if (fboundp 'define-key-after)
  106. (define-key-after map [t] 'org-goto-local-auto-isearch)
  107. nil)
  108. (org-defkey map "q" 'org-goto-quit)
  109. (org-defkey map "n" 'outline-next-visible-heading)
  110. (org-defkey map "p" 'outline-previous-visible-heading)
  111. (org-defkey map "f" 'outline-forward-same-level)
  112. (org-defkey map "b" 'outline-backward-same-level)
  113. (org-defkey map "u" 'outline-up-heading))
  114. (org-defkey map "/" 'org-occur)
  115. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  116. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  117. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  118. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  119. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  120. map)))
  121. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  122. (if (fboundp 'isearch-other-control-char)
  123. (progn
  124. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  125. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  126. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  127. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  128. (define-key org-goto-local-auto-isearch-map [return] nil))
  129. (defun org-goto--local-search-headings (string bound noerror)
  130. "Search and make sure that any matches are in headlines."
  131. (catch 'return
  132. (while (if isearch-forward
  133. (search-forward string bound noerror)
  134. (search-backward string bound noerror))
  135. (when (save-match-data
  136. (and (save-excursion
  137. (beginning-of-line)
  138. (looking-at org-complex-heading-regexp))
  139. (or (not (match-beginning 5))
  140. (< (point) (match-beginning 5)))))
  141. (throw 'return (point))))))
  142. (defun org-goto-local-auto-isearch ()
  143. "Start isearch."
  144. (interactive)
  145. (goto-char (point-min))
  146. (let ((keys (this-command-keys)))
  147. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  148. (isearch-mode t)
  149. (isearch-process-search-char (string-to-char keys)))))
  150. (defun org-goto-ret (&optional _arg)
  151. "Finish `org-goto' by going to the new location."
  152. (interactive "P")
  153. (setq org-goto-selected-point (point))
  154. (setq org-goto-exit-command 'return)
  155. (throw 'exit nil))
  156. (defun org-goto-left ()
  157. "Finish `org-goto' by going to the new location."
  158. (interactive)
  159. (if (org-at-heading-p)
  160. (progn
  161. (beginning-of-line 1)
  162. (setq org-goto-selected-point (point)
  163. org-goto-exit-command 'left)
  164. (throw 'exit nil))
  165. (user-error "Not on a heading")))
  166. (defun org-goto-right ()
  167. "Finish `org-goto' by going to the new location."
  168. (interactive)
  169. (if (org-at-heading-p)
  170. (progn
  171. (setq org-goto-selected-point (point)
  172. org-goto-exit-command 'right)
  173. (throw 'exit nil))
  174. (user-error "Not on a heading")))
  175. (defun org-goto-quit ()
  176. "Finish `org-goto' without cursor motion."
  177. (interactive)
  178. (setq org-goto-selected-point nil)
  179. (setq org-goto-exit-command 'quit)
  180. (throw 'exit nil))
  181. ;;; Public API
  182. ;;;###autoload
  183. (defun org-goto-location (&optional _buf help)
  184. "Let the user select a location in current buffer.
  185. This function uses a recursive edit. It returns the selected
  186. position or nil."
  187. (org-no-popups
  188. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  189. (isearch-hide-immediately nil)
  190. (isearch-search-fun-function #'org-goto--local-search-headings)
  191. (org-goto-selected-point org-goto-exit-command)
  192. (help (or help org-goto-help)))
  193. (save-excursion
  194. (save-window-excursion
  195. (delete-other-windows)
  196. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  197. (pop-to-buffer-same-window
  198. (condition-case nil
  199. (make-indirect-buffer (current-buffer) "*org-goto*")
  200. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  201. (with-output-to-temp-buffer "*Org Help*"
  202. (princ (format help (if org-goto-auto-isearch
  203. " Just type for auto-isearch."
  204. " n/p/f/b/u to navigate, q to quit."))))
  205. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  206. (setq buffer-read-only nil)
  207. (let ((org-startup-truncated t)
  208. (org-startup-folded nil)
  209. (org-startup-align-all-tables nil))
  210. (org-mode)
  211. (org-overview))
  212. (setq buffer-read-only t)
  213. (if (and (boundp 'org-goto-start-pos)
  214. (integer-or-marker-p org-goto-start-pos))
  215. (progn (goto-char org-goto-start-pos)
  216. (when (org-invisible-p)
  217. (org-show-set-visibility 'lineage)))
  218. (goto-char (point-min)))
  219. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  220. (message "Select location and press RET")
  221. (use-local-map org-goto-map)
  222. (recursive-edit)))
  223. (kill-buffer "*org-goto*")
  224. (cons org-goto-selected-point org-goto-exit-command))))
  225. ;;;###autoload
  226. (defun org-goto (&optional alternative-interface)
  227. "Look up a different location in the current file, keeping current visibility.
  228. When you want look-up or go to a different location in a
  229. document, the fastest way is often to fold the entire buffer and
  230. then dive into the tree. This method has the disadvantage, that
  231. the previous location will be folded, which may not be what you
  232. want.
  233. This command works around this by showing a copy of the current
  234. buffer in an indirect buffer, in overview mode. You can dive
  235. into the tree in that copy, use org-occur and incremental search
  236. to find a location. When pressing RET or `Q', the command
  237. returns to the original buffer in which the visibility is still
  238. unchanged. After RET it will also jump to the location selected
  239. in the indirect buffer and expose the headline hierarchy above.
  240. With a prefix argument, use the alternative interface: e.g., if
  241. `org-goto-interface' is `outline' use `outline-path-completion'."
  242. (interactive "P")
  243. (org-goto--set-map)
  244. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  245. (org-refile-use-outline-path t)
  246. (org-refile-target-verify-function nil)
  247. (interface
  248. (if (not alternative-interface)
  249. org-goto-interface
  250. (if (eq org-goto-interface 'outline)
  251. 'outline-path-completion
  252. 'outline)))
  253. (org-goto-start-pos (point))
  254. (selected-point
  255. (if (eq interface 'outline) (car (org-goto-location))
  256. (let ((pa (org-refile-get-location "Goto")))
  257. (org-refile-check-position pa)
  258. (nth 3 pa)))))
  259. (if selected-point
  260. (progn
  261. (org-mark-ring-push org-goto-start-pos)
  262. (goto-char selected-point)
  263. (when (or (org-invisible-p) (org-invisible-p2))
  264. (org-show-context 'org-goto)))
  265. (message "Quit"))))
  266. (provide 'org-goto)
  267. ;;; org-goto.el ends here