org-goto.el 9.9 KB

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