org-goto.el 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. ;;; org-goto.el --- Fast navigation in an Org buffer -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten.dominik@gmail.com>
  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. (define-key-after map [t] 'org-goto-local-auto-isearch)
  86. (org-defkey map "q" 'org-goto-quit)
  87. (org-defkey map "n" 'outline-next-visible-heading)
  88. (org-defkey map "p" 'outline-previous-visible-heading)
  89. (org-defkey map "f" 'outline-forward-same-level)
  90. (org-defkey map "b" 'outline-backward-same-level)
  91. (org-defkey map "u" 'outline-up-heading))
  92. (org-defkey map "/" 'org-occur)
  93. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  94. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  95. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  96. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  97. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  98. map)))
  99. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  100. (if (fboundp 'isearch-other-control-char)
  101. (progn
  102. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  103. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  104. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  105. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  106. (define-key org-goto-local-auto-isearch-map [return] nil))
  107. (defun org-goto--local-search-headings (string bound noerror)
  108. "Search and make sure that any matches are in headlines."
  109. (catch 'return
  110. (while (if isearch-forward
  111. (search-forward string bound noerror)
  112. (search-backward string bound noerror))
  113. (when (save-match-data
  114. (and (save-excursion
  115. (beginning-of-line)
  116. (looking-at org-complex-heading-regexp))
  117. (or (not (match-beginning 5))
  118. (< (point) (match-beginning 5)))))
  119. (throw 'return (point))))))
  120. (defun org-goto-local-auto-isearch ()
  121. "Start isearch."
  122. (interactive)
  123. (let ((keys (this-command-keys)))
  124. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  125. (isearch-mode t)
  126. (isearch-process-search-char (string-to-char keys))
  127. (font-lock-ensure))))
  128. (defun org-goto-ret (&optional _arg)
  129. "Finish `org-goto' by going to the new location."
  130. (interactive "P")
  131. (setq org-goto-selected-point (point))
  132. (setq org-goto-exit-command 'return)
  133. (throw 'exit nil))
  134. (defun org-goto-left ()
  135. "Finish `org-goto' by going to the new location."
  136. (interactive)
  137. (if (org-at-heading-p)
  138. (progn
  139. (beginning-of-line 1)
  140. (setq org-goto-selected-point (point)
  141. org-goto-exit-command 'left)
  142. (throw 'exit nil))
  143. (user-error "Not on a heading")))
  144. (defun org-goto-right ()
  145. "Finish `org-goto' by going to the new location."
  146. (interactive)
  147. (if (org-at-heading-p)
  148. (progn
  149. (setq org-goto-selected-point (point)
  150. org-goto-exit-command 'right)
  151. (throw 'exit nil))
  152. (user-error "Not on a heading")))
  153. (defun org-goto-quit ()
  154. "Finish `org-goto' without cursor motion."
  155. (interactive)
  156. (setq org-goto-selected-point nil)
  157. (setq org-goto-exit-command 'quit)
  158. (throw 'exit nil))
  159. ;;; Public API
  160. ;;;###autoload
  161. (defun org-goto-location (&optional _buf help)
  162. "Let the user select a location in current buffer.
  163. This function uses a recursive edit. It returns the selected
  164. position or nil."
  165. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  166. (isearch-hide-immediately nil)
  167. (isearch-search-fun-function
  168. (lambda () #'org-goto--local-search-headings))
  169. (help (or help org-goto-help)))
  170. (save-excursion
  171. (save-window-excursion
  172. (delete-other-windows)
  173. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  174. (pop-to-buffer-same-window
  175. (condition-case nil
  176. (make-indirect-buffer (current-buffer) "*org-goto*" t)
  177. (error (make-indirect-buffer (current-buffer) "*org-goto*" t))))
  178. (let (temp-buffer-show-function temp-buffer-show-hook)
  179. (with-output-to-temp-buffer "*Org Help*"
  180. (princ (format help (if org-goto-auto-isearch
  181. " Just type for auto-isearch."
  182. " n/p/f/b/u to navigate, q to quit.")))))
  183. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  184. (org-cycle-overview)
  185. (setq buffer-read-only t)
  186. (if (and (boundp 'org-goto-start-pos)
  187. (integer-or-marker-p org-goto-start-pos))
  188. (progn (goto-char org-goto-start-pos)
  189. (when (org-invisible-p)
  190. (org-fold-show-set-visibility 'lineage)))
  191. (goto-char (point-min)))
  192. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  193. (message "Select location and press RET")
  194. (use-local-map org-goto-map)
  195. (recursive-edit)))
  196. (kill-buffer "*org-goto*")
  197. (cons org-goto-selected-point org-goto-exit-command)))
  198. ;;;###autoload
  199. (defun org-goto (&optional alternative-interface)
  200. "Look up a different location in the current file, keeping current visibility.
  201. When you want look-up or go to a different location in a
  202. document, the fastest way is often to fold the entire buffer and
  203. then dive into the tree. This method has the disadvantage, that
  204. the previous location will be folded, which may not be what you
  205. want.
  206. This command works around this by showing a copy of the current
  207. buffer in an indirect buffer, in overview mode. You can dive
  208. into the tree in that copy, use `org-occur' and incremental search
  209. to find a location. When pressing RET or `Q', the command
  210. returns to the original buffer in which the visibility is still
  211. unchanged. After RET it will also jump to the location selected
  212. in the indirect buffer and expose the headline hierarchy above.
  213. With a prefix argument, use the alternative interface: e.g., if
  214. `org-goto-interface' is `outline' use `outline-path-completion'."
  215. (interactive "P")
  216. (org-goto--set-map)
  217. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  218. (org-refile-use-outline-path t)
  219. (org-refile-target-verify-function nil)
  220. (interface
  221. (if (not alternative-interface)
  222. org-goto-interface
  223. (if (eq org-goto-interface 'outline)
  224. 'outline-path-completion
  225. 'outline)))
  226. (org-goto-start-pos (point))
  227. (selected-point
  228. (if (eq interface 'outline) (car (org-goto-location))
  229. (let ((pa (org-refile-get-location "Goto")))
  230. (org-refile-check-position pa)
  231. (nth 3 pa)))))
  232. (if selected-point
  233. (progn
  234. (org-mark-ring-push org-goto-start-pos)
  235. (goto-char selected-point)
  236. (when (or (org-invisible-p) (org-invisible-p2))
  237. (org-fold-show-context 'org-goto)))
  238. (message "Quit"))))
  239. (provide 'org-goto)
  240. ;; Local variables:
  241. ;; generated-autoload-file: "org-loaddefs.el"
  242. ;; End:
  243. ;;; org-goto.el ends here