org-goto.el 11 KB

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