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