org-goto.el 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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-macs)
  18. (org-assert-version)
  19. (require 'org)
  20. (require 'org-refile)
  21. (defvar org-goto-exit-command nil)
  22. (defvar org-goto-map nil)
  23. (defvar org-goto-marker nil)
  24. (defvar org-goto-selected-point nil)
  25. (defvar org-goto-start-pos nil)
  26. (defvar org-goto-window-configuration nil)
  27. (defconst org-goto-local-auto-isearch-map (make-sparse-keymap))
  28. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  29. (defconst org-goto-help
  30. "Browse buffer copy, to find location or copy text.%s
  31. RET=jump to location C-g=quit and return to previous location
  32. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  33. ;;; Customization
  34. (defgroup org-goto nil
  35. "Options concerning Org Goto navigation interface."
  36. :tag "Org Goto"
  37. :group 'org)
  38. (defcustom org-goto-interface 'outline
  39. "The default interface to be used for `org-goto'.
  40. Allowed values are:
  41. `outline'
  42. The interface shows an outline of the relevant file and the
  43. correct heading is found by moving through the outline or by
  44. searching with incremental search.
  45. `outline-path-completion'
  46. Headlines in the current buffer are offered via completion.
  47. This is the interface also used by the refile command."
  48. :group 'org-goto
  49. :type '(choice
  50. (const :tag "Outline" outline)
  51. (const :tag "Outline-path-completion" outline-path-completion)))
  52. (defcustom org-goto-max-level 5
  53. "Maximum target level when running `org-goto' with refile interface."
  54. :group 'org-goto
  55. :type 'integer)
  56. (defcustom org-goto-auto-isearch t
  57. "Non-nil means typing characters in `org-goto' starts incremental search.
  58. When nil, you can use these keybindings to navigate the buffer:
  59. q Quit the Org Goto interface
  60. n Go to the next visible heading
  61. p Go to the previous visible heading
  62. f Go one heading forward on same level
  63. b Go one heading backward on same level
  64. u Go one heading up"
  65. :group 'org-goto
  66. :type 'boolean)
  67. ;;; Internal functions
  68. (defun org-goto--set-map ()
  69. "Set the keymap `org-goto'."
  70. (setq org-goto-map
  71. (let ((map (make-sparse-keymap)))
  72. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  73. mouse-drag-region universal-argument org-occur)))
  74. (dolist (cmd cmds)
  75. (substitute-key-definition cmd cmd map global-map)))
  76. (suppress-keymap map)
  77. (org-defkey map "\C-m" 'org-goto-ret)
  78. (org-defkey map [(return)] 'org-goto-ret)
  79. (org-defkey map [(left)] 'org-goto-left)
  80. (org-defkey map [(right)] 'org-goto-right)
  81. (org-defkey map [(control ?g)] 'org-goto-quit)
  82. (org-defkey map "\C-i" 'org-cycle)
  83. (org-defkey map [(tab)] 'org-cycle)
  84. (org-defkey map [(down)] 'outline-next-visible-heading)
  85. (org-defkey map [(up)] 'outline-previous-visible-heading)
  86. (if org-goto-auto-isearch
  87. (define-key-after map [t] 'org-goto-local-auto-isearch)
  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. (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. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  168. (isearch-hide-immediately nil)
  169. (isearch-search-fun-function
  170. (lambda () #'org-goto--local-search-headings))
  171. (help (or help org-goto-help)))
  172. (save-excursion
  173. (save-window-excursion
  174. (delete-other-windows)
  175. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  176. (pop-to-buffer-same-window
  177. (condition-case nil
  178. (make-indirect-buffer (current-buffer) "*org-goto*" t)
  179. (error (make-indirect-buffer (current-buffer) "*org-goto*" t))))
  180. (let (temp-buffer-show-function temp-buffer-show-hook)
  181. (with-output-to-temp-buffer "*Org Help*"
  182. (princ (format help (if org-goto-auto-isearch
  183. " Just type for auto-isearch."
  184. " n/p/f/b/u to navigate, q to quit.")))))
  185. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  186. (org-cycle-overview)
  187. (setq buffer-read-only t)
  188. (if (and (boundp 'org-goto-start-pos)
  189. (integer-or-marker-p org-goto-start-pos))
  190. (progn (goto-char org-goto-start-pos)
  191. (when (org-invisible-p)
  192. (org-fold-show-set-visibility 'lineage)))
  193. (goto-char (point-min)))
  194. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  195. (message "Select location and press RET")
  196. (use-local-map org-goto-map)
  197. (recursive-edit)))
  198. (kill-buffer "*org-goto*")
  199. (cons org-goto-selected-point org-goto-exit-command)))
  200. ;;;###autoload
  201. (defun org-goto (&optional alternative-interface)
  202. "Look up a different location in the current file, keeping current visibility.
  203. When you want look-up or go to a different location in a
  204. document, the fastest way is often to fold the entire buffer and
  205. then dive into the tree. This method has the disadvantage, that
  206. the previous location will be folded, which may not be what you
  207. want.
  208. This command works around this by showing a copy of the current
  209. buffer in an indirect buffer, in overview mode. You can dive
  210. into the tree in that copy, use `org-occur' and incremental search
  211. to find a location. When pressing RET or `Q', the command
  212. returns to the original buffer in which the visibility is still
  213. unchanged. After RET it will also jump to the location selected
  214. in the indirect buffer and expose the headline hierarchy above.
  215. With a prefix argument, use the alternative interface: e.g., if
  216. `org-goto-interface' is `outline' use `outline-path-completion'."
  217. (interactive "P")
  218. (org-goto--set-map)
  219. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  220. (org-refile-use-outline-path t)
  221. (org-refile-target-verify-function nil)
  222. (interface
  223. (if (not alternative-interface)
  224. org-goto-interface
  225. (if (eq org-goto-interface 'outline)
  226. 'outline-path-completion
  227. 'outline)))
  228. (org-goto-start-pos (point))
  229. (selected-point
  230. (if (eq interface 'outline) (car (org-goto-location))
  231. (let ((pa (org-refile-get-location "Goto")))
  232. (org-refile-check-position pa)
  233. (nth 3 pa)))))
  234. (if selected-point
  235. (progn
  236. (org-mark-ring-push org-goto-start-pos)
  237. (goto-char selected-point)
  238. (when (or (org-invisible-p) (org-invisible-p2))
  239. (org-fold-show-context 'org-goto)))
  240. (message "Quit"))))
  241. (provide 'org-goto)
  242. ;; Local variables:
  243. ;; generated-autoload-file: "org-loaddefs.el"
  244. ;; End:
  245. ;;; org-goto.el ends here