Merge branch 'agenda-subtree-kill-with-subtree-feedback'
[org-mode/org-tableheadings.git] / lisp / org-goto.el
blob5ce9b8cb65a193ffb1a878671308bfe50b446c4f
1 ;;; org-goto.el --- Fast navigation in an Org buffer -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2019 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
23 ;;; Code:
25 (require 'org-macs)
26 (require 'org-compat)
28 (declare-function org-at-heading-p "org" (&optional ignored))
29 (declare-function org-beginning-of-line "org" (&optional n))
30 (declare-function org-defkey "org" (keymap key def))
31 (declare-function org-mark-ring-push "org" (&optional pos buffer))
32 (declare-function org-overview "org" ())
33 (declare-function org-refile-check-position "org" (refile-pointer))
34 (declare-function org-refile-get-location "org" (&optional prompt default-buffer new-nodes))
35 (declare-function org-show-context "org" (&optional key))
36 (declare-function org-show-set-visibility "org" (detail))
38 (defvar org-complex-heading-regexp)
39 (defvar org-startup-align-all-tables)
40 (defvar org-startup-folded)
41 (defvar org-startup-truncated)
42 (defvar org-special-ctrl-a/e)
43 (defvar org-refile-target-verify-function)
44 (defvar org-refile-use-outline-path)
45 (defvar org-refile-targets)
47 (defvar org-goto-exit-command nil)
48 (defvar org-goto-map nil)
49 (defvar org-goto-marker nil)
50 (defvar org-goto-selected-point nil)
51 (defvar org-goto-start-pos nil)
52 (defvar org-goto-window-configuration nil)
54 (defconst org-goto-local-auto-isearch-map (make-sparse-keymap))
55 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
57 (defconst org-goto-help
58 "Browse buffer copy, to find location or copy text.%s
59 RET=jump to location C-g=quit and return to previous location
60 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
64 ;;; Customization
66 (defgroup org-goto nil
67 "Options concerning Org Goto navigation interface."
68 :tag "Org Goto"
69 :group 'org)
71 (defcustom org-goto-interface 'outline
72 "The default interface to be used for `org-goto'.
74 Allowed values are:
76 `outline'
78 The interface shows an outline of the relevant file and the
79 correct heading is found by moving through the outline or by
80 searching with incremental search.
82 `outline-path-completion'
84 Headlines in the current buffer are offered via completion.
85 This is the interface also used by the refile command."
86 :group 'org-goto
87 :type '(choice
88 (const :tag "Outline" outline)
89 (const :tag "Outline-path-completion" outline-path-completion)))
91 (defcustom org-goto-max-level 5
92 "Maximum target level when running `org-goto' with refile interface."
93 :group 'org-goto
94 :type 'integer)
96 (defcustom org-goto-auto-isearch t
97 "Non-nil means typing characters in `org-goto' starts incremental search.
98 When nil, you can use these keybindings to navigate the buffer:
100 q Quit the Org Goto interface
101 n Go to the next visible heading
102 p Go to the previous visible heading
103 f Go one heading forward on same level
104 b Go one heading backward on same level
105 u Go one heading up"
106 :group 'org-goto
107 :type 'boolean)
111 ;;; Internal functions
113 (defun org-goto--set-map ()
114 "Set the keymap `org-goto'."
115 (setq org-goto-map
116 (let ((map (make-sparse-keymap)))
117 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
118 mouse-drag-region universal-argument org-occur)))
119 (dolist (cmd cmds)
120 (substitute-key-definition cmd cmd map global-map)))
121 (suppress-keymap map)
122 (org-defkey map "\C-m" 'org-goto-ret)
123 (org-defkey map [(return)] 'org-goto-ret)
124 (org-defkey map [(left)] 'org-goto-left)
125 (org-defkey map [(right)] 'org-goto-right)
126 (org-defkey map [(control ?g)] 'org-goto-quit)
127 (org-defkey map "\C-i" 'org-cycle)
128 (org-defkey map [(tab)] 'org-cycle)
129 (org-defkey map [(down)] 'outline-next-visible-heading)
130 (org-defkey map [(up)] 'outline-previous-visible-heading)
131 (if org-goto-auto-isearch
132 (if (fboundp 'define-key-after)
133 (define-key-after map [t] 'org-goto-local-auto-isearch)
134 nil)
135 (org-defkey map "q" 'org-goto-quit)
136 (org-defkey map "n" 'outline-next-visible-heading)
137 (org-defkey map "p" 'outline-previous-visible-heading)
138 (org-defkey map "f" 'outline-forward-same-level)
139 (org-defkey map "b" 'outline-backward-same-level)
140 (org-defkey map "u" 'outline-up-heading))
141 (org-defkey map "/" 'org-occur)
142 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
143 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
144 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
145 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
146 (org-defkey map "\C-c\C-u" 'outline-up-heading)
147 map)))
149 ;; `isearch-other-control-char' was removed in Emacs 24.4.
150 (if (fboundp 'isearch-other-control-char)
151 (progn
152 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
153 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
154 (define-key org-goto-local-auto-isearch-map "\C-i" nil)
155 (define-key org-goto-local-auto-isearch-map "\C-m" nil)
156 (define-key org-goto-local-auto-isearch-map [return] nil))
158 (defun org-goto--local-search-headings (string bound noerror)
159 "Search and make sure that any matches are in headlines."
160 (catch 'return
161 (while (if isearch-forward
162 (search-forward string bound noerror)
163 (search-backward string bound noerror))
164 (when (save-match-data
165 (and (save-excursion
166 (beginning-of-line)
167 (looking-at org-complex-heading-regexp))
168 (or (not (match-beginning 5))
169 (< (point) (match-beginning 5)))))
170 (throw 'return (point))))))
172 (defun org-goto-local-auto-isearch ()
173 "Start isearch."
174 (interactive)
175 (let ((keys (this-command-keys)))
176 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
177 (isearch-mode t)
178 (isearch-process-search-char (string-to-char keys))
179 (org-font-lock-ensure))))
181 (defun org-goto-ret (&optional _arg)
182 "Finish `org-goto' by going to the new location."
183 (interactive "P")
184 (setq org-goto-selected-point (point))
185 (setq org-goto-exit-command 'return)
186 (throw 'exit nil))
188 (defun org-goto-left ()
189 "Finish `org-goto' by going to the new location."
190 (interactive)
191 (if (org-at-heading-p)
192 (progn
193 (beginning-of-line 1)
194 (setq org-goto-selected-point (point)
195 org-goto-exit-command 'left)
196 (throw 'exit nil))
197 (user-error "Not on a heading")))
199 (defun org-goto-right ()
200 "Finish `org-goto' by going to the new location."
201 (interactive)
202 (if (org-at-heading-p)
203 (progn
204 (setq org-goto-selected-point (point)
205 org-goto-exit-command 'right)
206 (throw 'exit nil))
207 (user-error "Not on a heading")))
209 (defun org-goto-quit ()
210 "Finish `org-goto' without cursor motion."
211 (interactive)
212 (setq org-goto-selected-point nil)
213 (setq org-goto-exit-command 'quit)
214 (throw 'exit nil))
218 ;;; Public API
220 ;;;###autoload
221 (defun org-goto-location (&optional _buf help)
222 "Let the user select a location in current buffer.
223 This function uses a recursive edit. It returns the selected
224 position or nil."
225 (org-no-popups
226 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
227 (isearch-hide-immediately nil)
228 (isearch-search-fun-function
229 (lambda () #'org-goto--local-search-headings))
230 (help (or help org-goto-help)))
231 (save-excursion
232 (save-window-excursion
233 (delete-other-windows)
234 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
235 (pop-to-buffer-same-window
236 (condition-case nil
237 (make-indirect-buffer (current-buffer) "*org-goto*")
238 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
239 (let (temp-buffer-show-function temp-buffer-show-hook)
240 (with-output-to-temp-buffer "*Org Help*"
241 (princ (format help (if org-goto-auto-isearch
242 " Just type for auto-isearch."
243 " n/p/f/b/u to navigate, q to quit.")))))
244 (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
245 (setq buffer-read-only nil)
246 (let ((org-startup-truncated t)
247 (org-startup-folded nil)
248 (org-startup-align-all-tables nil))
249 (org-mode)
250 (org-overview))
251 (setq buffer-read-only t)
252 (if (and (boundp 'org-goto-start-pos)
253 (integer-or-marker-p org-goto-start-pos))
254 (progn (goto-char org-goto-start-pos)
255 (when (org-invisible-p)
256 (org-show-set-visibility 'lineage)))
257 (goto-char (point-min)))
258 (let (org-special-ctrl-a/e) (org-beginning-of-line))
259 (message "Select location and press RET")
260 (use-local-map org-goto-map)
261 (recursive-edit)))
262 (kill-buffer "*org-goto*")
263 (cons org-goto-selected-point org-goto-exit-command))))
265 ;;;###autoload
266 (defun org-goto (&optional alternative-interface)
267 "Look up a different location in the current file, keeping current visibility.
269 When you want look-up or go to a different location in a
270 document, the fastest way is often to fold the entire buffer and
271 then dive into the tree. This method has the disadvantage, that
272 the previous location will be folded, which may not be what you
273 want.
275 This command works around this by showing a copy of the current
276 buffer in an indirect buffer, in overview mode. You can dive
277 into the tree in that copy, use org-occur and incremental search
278 to find a location. When pressing RET or `Q', the command
279 returns to the original buffer in which the visibility is still
280 unchanged. After RET it will also jump to the location selected
281 in the indirect buffer and expose the headline hierarchy above.
283 With a prefix argument, use the alternative interface: e.g., if
284 `org-goto-interface' is `outline' use `outline-path-completion'."
285 (interactive "P")
286 (org-goto--set-map)
287 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
288 (org-refile-use-outline-path t)
289 (org-refile-target-verify-function nil)
290 (interface
291 (if (not alternative-interface)
292 org-goto-interface
293 (if (eq org-goto-interface 'outline)
294 'outline-path-completion
295 'outline)))
296 (org-goto-start-pos (point))
297 (selected-point
298 (if (eq interface 'outline) (car (org-goto-location))
299 (let ((pa (org-refile-get-location "Goto")))
300 (org-refile-check-position pa)
301 (nth 3 pa)))))
302 (if selected-point
303 (progn
304 (org-mark-ring-push org-goto-start-pos)
305 (goto-char selected-point)
306 (when (or (org-invisible-p) (org-invisible-p2))
307 (org-show-context 'org-goto)))
308 (message "Quit"))))
310 (provide 'org-goto)
312 ;;; org-goto.el ends here