org-goto.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. ;;; org-goto.el --- Fast navigation in an Org buffer -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2018 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-map nil)
  40. (defvar org-goto-marker nil)
  41. (defvar org-goto-selected-point nil)
  42. (defvar org-goto-start-pos nil)
  43. (defvar org-goto-window-configuration nil)
  44. (defconst org-goto-local-auto-isearch-map (make-sparse-keymap))
  45. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  46. (defconst org-goto-help
  47. "Browse buffer copy, to find location or copy text.%s
  48. RET=jump to location C-g=quit and return to previous location
  49. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  50. ;;; Customization
  51. (defgroup org-goto nil
  52. "Options concerning Org Goto navigation interface."
  53. :tag "Org Goto"
  54. :group 'org)
  55. (defcustom org-goto-interface 'outline
  56. "The default interface to be used for `org-goto'.
  57. Allowed values are:
  58. `outline'
  59. The interface shows an outline of the relevant file and the
  60. correct heading is found by moving through the outline or by
  61. searching with incremental search.
  62. `outline-path-completion'
  63. Headlines in the current buffer are offered via completion.
  64. This is the interface also used by the refile command."
  65. :group 'org-goto
  66. :type '(choice
  67. (const :tag "Outline" outline)
  68. (const :tag "Outline-path-completion" outline-path-completion)))
  69. (defcustom org-goto-max-level 5
  70. "Maximum target level when running `org-goto' with refile interface."
  71. :group 'org-goto
  72. :type 'integer)
  73. (defcustom org-goto-auto-isearch t
  74. "Non-nil means typing characters in `org-goto' starts incremental search.
  75. When nil, you can use these keybindings to navigate the buffer:
  76. q Quit the Org Goto interface
  77. n Go to the next visible heading
  78. p Go to the previous visible heading
  79. f Go one heading forward on same level
  80. b Go one heading backward on same level
  81. u Go one heading up"
  82. :group 'org-goto
  83. :type 'boolean)
  84. ;;; Internal functions
  85. (defun org-goto--set-map ()
  86. "Set the keymap `org-goto'."
  87. (setq org-goto-map
  88. (let ((map (make-sparse-keymap)))
  89. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  90. mouse-drag-region universal-argument org-occur)))
  91. (dolist (cmd cmds)
  92. (substitute-key-definition cmd cmd map global-map)))
  93. (suppress-keymap map)
  94. (org-defkey map "\C-m" 'org-goto-ret)
  95. (org-defkey map [(return)] 'org-goto-ret)
  96. (org-defkey map [(left)] 'org-goto-left)
  97. (org-defkey map [(right)] 'org-goto-right)
  98. (org-defkey map [(control ?g)] 'org-goto-quit)
  99. (org-defkey map "\C-i" 'org-cycle)
  100. (org-defkey map [(tab)] 'org-cycle)
  101. (org-defkey map [(down)] 'outline-next-visible-heading)
  102. (org-defkey map [(up)] 'outline-previous-visible-heading)
  103. (if org-goto-auto-isearch
  104. (if (fboundp 'define-key-after)
  105. (define-key-after map [t] 'org-goto-local-auto-isearch)
  106. nil)
  107. (org-defkey map "q" 'org-goto-quit)
  108. (org-defkey map "n" 'outline-next-visible-heading)
  109. (org-defkey map "p" 'outline-previous-visible-heading)
  110. (org-defkey map "f" 'outline-forward-same-level)
  111. (org-defkey map "b" 'outline-backward-same-level)
  112. (org-defkey map "u" 'outline-up-heading))
  113. (org-defkey map "/" 'org-occur)
  114. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  115. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  116. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  117. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  118. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  119. map)))
  120. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  121. (if (fboundp 'isearch-other-control-char)
  122. (progn
  123. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  124. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  125. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  126. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  127. (define-key org-goto-local-auto-isearch-map [return] nil))
  128. (defun org-goto--local-search-headings (string bound noerror)
  129. "Search and make sure that any matches are in headlines."
  130. (catch 'return
  131. (while (if isearch-forward
  132. (search-forward string bound noerror)
  133. (search-backward string bound noerror))
  134. (when (save-match-data
  135. (and (save-excursion
  136. (beginning-of-line)
  137. (looking-at org-complex-heading-regexp))
  138. (or (not (match-beginning 5))
  139. (< (point) (match-beginning 5)))))
  140. (throw 'return (point))))))
  141. (defun org-goto-local-auto-isearch ()
  142. "Start isearch."
  143. (interactive)
  144. (goto-char (point-min))
  145. (let ((keys (this-command-keys)))
  146. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  147. (isearch-mode t)
  148. (isearch-process-search-char (string-to-char keys)))))
  149. (defun org-goto-ret (&optional _arg)
  150. "Finish `org-goto' by going to the new location."
  151. (interactive "P")
  152. (setq org-goto-selected-point (point))
  153. (setq org-goto-exit-command 'return)
  154. (throw 'exit nil))
  155. (defun org-goto-left ()
  156. "Finish `org-goto' by going to the new location."
  157. (interactive)
  158. (if (org-at-heading-p)
  159. (progn
  160. (beginning-of-line 1)
  161. (setq org-goto-selected-point (point)
  162. org-goto-exit-command 'left)
  163. (throw 'exit nil))
  164. (user-error "Not on a heading")))
  165. (defun org-goto-right ()
  166. "Finish `org-goto' by going to the new location."
  167. (interactive)
  168. (if (org-at-heading-p)
  169. (progn
  170. (setq org-goto-selected-point (point)
  171. org-goto-exit-command 'right)
  172. (throw 'exit nil))
  173. (user-error "Not on a heading")))
  174. (defun org-goto-quit ()
  175. "Finish `org-goto' without cursor motion."
  176. (interactive)
  177. (setq org-goto-selected-point nil)
  178. (setq org-goto-exit-command 'quit)
  179. (throw 'exit nil))
  180. ;;; Public API
  181. ;;;###autoload
  182. (defun org-goto-location (&optional _buf help)
  183. "Let the user select a location in current buffer.
  184. This function uses a recursive edit. It returns the selected
  185. position or nil."
  186. (org-no-popups
  187. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  188. (isearch-hide-immediately nil)
  189. (isearch-search-fun-function
  190. (lambda () #'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