2
votes

For instance, when editing an Emacs lisp file test.el, I would like to mark a text (for instance the name of a function) in the current buffer (I am using a single frame with a single window) then press a keyboard shortcut to split the current window into two parts, one above the other. The top window should display my orginal buffer as it was, the bottom window should display the buffer at the point where the function is defined (like (defun test-fun ...).

I have tried this code:

(defun test-split ()
 (interactive)
 (split-window-below 28)
 (let (( w (next-window)))
   (let ((buf (window-buffer w)))
     (with-current-buffer buf
       (beginning-of-buffer)
       (re-search-forward "defun test-fun ")))))

but it does not work.. (It searches the top window, not the bottom window)

1

1 Answers

4
votes

The two windows are displaying the same buffer, so with-current-buffer is valid for the original window, and hence there is no need for Emacs to switch to another window.

You could modify your code like so:

(defun test-split ()
  (interactive)
  (split-window-below 28)
  (save-selected-window
    (select-window (next-window))
    (beginning-of-buffer)
    (re-search-forward "defun test-fun ")))