org-goto.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. ;;; org-goto.el --- Fast navigation in an Org buffer -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2019 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. (goto-char (point-min))
  143. (let ((keys (this-command-keys)))
  144. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  145. (isearch-mode t)
  146. (isearch-process-search-char (string-to-char keys))
  147. (org-font-lock-ensure))))
  148. (defun org-goto-ret (&optional _arg)
  149. "Finish `org-goto' by going to the new location."
  150. (interactive "P")
  151. (setq org-goto-selected-point (point))
  152. (setq org-goto-exit-command 'return)
  153. (throw 'exit nil))
  154. (defun org-goto-left ()
  155. "Finish `org-goto' by going to the new location."
  156. (interactive)
  157. (if (org-at-heading-p)
  158. (progn
  159. (beginning-of-line 1)
  160. (setq org-goto-selected-point (point)
  161. org-goto-exit-command 'left)
  162. (throw 'exit nil))
  163. (user-error "Not on a heading")))
  164. (defun org-goto-right ()
  165. "Finish `org-goto' by going to the new location."
  166. (interactive)
  167. (if (org-at-heading-p)
  168. (progn
  169. (setq org-goto-selected-point (point)
  170. org-goto-exit-command 'right)
  171. (throw 'exit nil))
  172. (user-error "Not on a heading")))
  173. (defun org-goto-quit ()
  174. "Finish `org-goto' without cursor motion."
  175. (interactive)
  176. (setq org-goto-selected-point nil)
  177. (setq org-goto-exit-command 'quit)
  178. (throw 'exit nil))
  179. ;;; Public API
  180. ;;;###autoload
  181. (defun org-goto-location (&optional _buf help)
  182. "Let the user select a location in current buffer.
  183. This function uses a recursive edit. It returns the selected
  184. position or nil."
  185. (org-no-popups
  186. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  187. (isearch-hide-immediately nil)
  188. (isearch-search-fun-function
  189. (lambda () #'org-goto--local-search-headings))
  190. (org-goto-selected-point org-goto-exit-command)
  191. (help (or help org-goto-help)))
  192. (save-excursion
  193. (save-window-excursion
  194. (delete-other-windows)
  195. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  196. (pop-to-buffer-same-window
  197. (condition-case nil
  198. (make-indirect-buffer (current-buffer) "*org-goto*")
  199. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  200. (with-output-to-temp-buffer "*Org Help*"
  201. (princ (format help (if org-goto-auto-isearch
  202. " Just type for auto-isearch."
  203. " n/p/f/b/u to navigate, q to quit."))))
  204. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  205. (setq buffer-read-only nil)
  206. (let ((org-startup-truncated t)
  207. (org-startup-folded nil)
  208. (org-startup-align-all-tables nil))
  209. (org-mode)
  210. (org-overview))
  211. (setq buffer-read-only t)
  212. (if (and (boundp 'org-goto-start-pos)
  213. (integer-or-marker-p org-goto-start-pos))
  214. (progn (goto-char org-goto-start-pos)
  215. (when (org-invisible-p)
  216. (org-show-set-visibility 'lineage)))
  217. (goto-char (point-min)))
  218. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  219. (message "Select location and press RET")
  220. (use-local-map org-goto-map)
  221. (recursive-edit)))
  222. (kill-buffer "*org-goto*")
  223. (cons org-goto-selected-point org-goto-exit-command))))
  224. ;;;###autoload
  225. (defun org-goto (&optional alternative-interface)
  226. "Look up a different location in the current file, keeping current visibility.
  227. When you want look-up or go to a different location in a
  228. document, the fastest way is often to fold the entire buffer and
  229. then dive into the tree. This method has the disadvantage, that
  230. the previous location will be folded, which may not be what you
  231. want.
  232. This command works around this by showing a copy of the current
  233. buffer in an indirect buffer, in overview mode. You can dive
  234. into the tree in that copy, use org-occur and incremental search
  235. to find a location. When pressing RET or `Q', the command
  236. returns to the original buffer in which the visibility is still
  237. unchanged. After RET it will also jump to the location selected
  238. in the indirect buffer and expose the headline hierarchy above.
  239. With a prefix argument, use the alternative interface: e.g., if
  240. `org-goto-interface' is `outline' use `outline-path-completion'."
  241. (interactive "P")
  242. (org-goto--set-map)
  243. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  244. (org-refile-use-outline-path t)
  245. (org-refile-target-verify-function nil)
  246. (interface
  247. (if (not alternative-interface)
  248. org-goto-interface
  249. (if (eq org-goto-interface 'outline)
  250. 'outline-path-completion
  251. 'outline)))
  252. (org-goto-start-pos (point))
  253. (selected-point
  254. (if (eq interface 'outline) (car (org-goto-location))
  255. (let ((pa (org-refile-get-location "Goto")))
  256. (org-refile-check-position pa)
  257. (nth 3 pa)))))
  258. (if selected-point
  259. (progn
  260. (org-mark-ring-push org-goto-start-pos)
  261. (goto-char selected-point)
  262. (when (or (org-invisible-p) (org-invisible-p2))
  263. (org-show-context 'org-goto)))
  264. (message "Quit"))))
  265. (provide 'org-goto)
  266. ;;; org-goto.el ends here