emacs-pretest-bug
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Incorrect advice how to restore previous window


From: Kim F. Storm
Subject: Incorrect advice how to restore previous window
Date: Wed, 21 Feb 2007 00:22:32 +0100 (CET)

emacs -q

C-h C-n
C-x 2
M->

.. Now we have two windows showing different parts of the NEWS buffer.

C-h k k

.. The lower window now shows the *Help* buffer.

.. And the echo area says:
    Type C-x 4 C-o to restore the other window.

But that's not true - for several reasons:

1) It suggests displaying the *scratch* buffer, not NEWS.

2) Even if I explicitly enters NEWS to the prompt, nothing changes,
   as NEWS is already shown in the upper window.

3) And even _if_ it would have shown NEWS in the lower window,
   it would have shown the wrong part of the file, namely the
   same portion as is shown in the upper window.


Except for a lot of book-keeping via the pre-command-hook, there is
currently NO WAY Emacs can restore that lower window.

To be able to do that, we need some hook which allows us to record
what was shown in the window before displaying something else in it,
and what the window point was in that window.

Other settings might also need to be saved, but that's the minimum
we need.

The following patch add a "change-window-buffer-functions" hook:


*** window.c    11 Feb 2007 21:37:29 +0100      1.573
--- window.c    20 Feb 2007 17:11:57 +0100      
***************
*** 201,207 ****
  
  static int window_initialized;
  
! /* Hook to run when window config changes.  */
  
  Lisp_Object Qwindow_configuration_change_hook;
  Lisp_Object Vwindow_configuration_change_hook;
--- 201,213 ----
  
  static int window_initialized;
  
! 
! /* Hook to run before setting window buffer.  */
! 
! Lisp_Object Qchange_window_buffer_functions;
! Lisp_Object Vchange_window_buffer_functions;
! 
! /* Hook to run after window config changes.  */
  
  Lisp_Object Qwindow_configuration_change_hook;
  Lisp_Object Vwindow_configuration_change_hook;
***************
*** 3275,3280 ****
--- 3281,3293 ----
    struct buffer *b = XBUFFER (buffer);
    int count = SPECPDL_INDEX ();
  
+   if (run_hooks_p
+       && BUFFERP (w->buffer)
+       && ! NILP (Vrun_hooks)
+       && ! NILP (Vchange_window_buffer_functions))
+     run_hook_with_args_2 (Qchange_window_buffer_functions,
+                         window, buffer);
+ 
    w->buffer = buffer;
  
    if (EQ (window, selected_window))
***************
*** 7289,7294 ****
--- 7302,7311 ----
    Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
    staticpro (&Qtemp_buffer_show_hook);
  
+   Qchange_window_buffer_functions
+     = intern ("change-window-buffer-functions");
+   staticpro (&Qchange_window_buffer_functions);
+ 
    staticpro (&Vwindow_list);
  
    minibuf_selected_window = Qnil;
***************
*** 7489,7494 ****
--- 7506,7518 ----
  The selected frame is the one whose configuration has changed.  */);
    Vwindow_configuration_change_hook = Qnil;
  
+   DEFVAR_LISP ("change-window-buffer-functions",
+              &Vchange_window_buffer_functions,
+     doc: /* List of functions to call before changing buffer in window.
+ Each function is called with two arguments, the existing window and
+ its new buffer.  */);
+   Vchange_window_buffer_functions = Qnil;
+ 
    defsubr (&Sselected_window);
    defsubr (&Sminibuffer_window);
    defsubr (&Swindow_minibuffer_p);



One way to use this hook would be to keep a "restore-window-buffer-ring"
which could allow gradually "undoing" previous switch-to-buffer commands.
I've not written the code to do that (there are many ways this could
be done)

So here is just a trivial example of using the new hook to restore
the window used for the *Help* buffer.

(defvar last-help-window-buffer nil)
(defun my-change-window-buffer-hook (w b)
  (if (string= (buffer-name b) "*Help*")
      (setq last-help-window-buffer (list w (window-buffer w) (window-point 
w)))))
(add-hook 'change-window-buffer-functions 'my-change-window-buffer-hook)

(defun restore-help-window-buffer ()
  (interactive)
  (if (and last-help-window-buffer
           (window-live-p (car last-help-window-buffer))
           (buffer-live-p (cadr last-help-window-buffer)))
      (progn
        (set-window-buffer (car last-help-window-buffer)
                           (cadr last-help-window-buffer))
        (set-window-point (car last-help-window-buffer)
                          (nth 2 last-help-window-buffer)))
    (call-interactively 'display-buffer)))
(global-set-key [kp-divide] 'restore-help-window-buffer)


--
Kim F. Storm <address@hidden> http://www.cua.dk





reply via email to

[Prev in Thread] Current Thread [Next in Thread]