1

I am using AucTeX with GNU Emacs 30.0.50. It has a shortcut C-c C-l for viewing the processing of the TeX file. It seems to be using split-window-below command for opening the new buffer. Is there any way with which I can change this behavior and always open the new buffer with split-window-right command? What should I put in my .emacs for this?

2 Answers 2

3

Make split-window-below behave like split-window-right each time you call TeX-recenter-output-buffer which is bound to C-c C-l:

(advice-add 'TeX-recenter-output-buffer :around
            (lambda (fn &rest args)
              (let ((old-def (symbol-function 'split-window-below)))
                (fset 'split-window-below #'split-window-right)
                (unwind-protect
                    (apply fn args)
                  (fset 'split-window-below old-def)))))

You could put it in your .emacs.


See 13.11 Advising Emacs Lisp Functions.


Since C-c C-l is bound to the function TeX-recenter-output-buffer, the main job is to modify the behavior of that function.

What advice-add does is like a piece of advice: it adds some code to the definition of the function (by wrapping it in this case);
the :around keyword indicates that: when you invoke TeX-recenter-output-buffer, it will be executed in the body of advice-add's 3rd argument.

You can think of what (advice-add FUNCTION-A :around FUNCTION-B) does as:

You call FUNCTION-A with some arguments> (FUNCTION-A args...)
What Emacs Lisp actually does> (FUNCTION-B FUNCTION-A args...)

The original behavior is split-window-below, so FUNCTION-B's job is to make the symbol split-window-below represent another function, i.e., split-window-right:

;; Bind "split-window-below" to another function -- "split-window-right"
(fset 'split-window-below #'split-window-right)

But don't forget to restore split-window-below's original binding after executing TeX-recenter-output-buffer:

(fset 'split-window-below old-def)

where old-def means what split-window-below represented originally.
The unwind-protect form makes sure that the above piece of code will always be executed whatever happens (e.g., an error is signaled).

For more details, use C-h f.

5
  • Thanks, this works correctly, can you please explain briefly what we did with this code? I would like to understand how this worked out.
    – Niranjan
    Commented Apr 12, 2023 at 14:12
  • @Niranjan: No problem. I'm washing up now, and I'll come back later to refine my answer by adding some comments to explain it.
    – shynur
    Commented Apr 12, 2023 at 14:18
  • Great! Please take your time and thanks again :-)
    – Niranjan
    Commented Apr 12, 2023 at 14:18
  • @Niranjan: Updated
    – shynur
    Commented Apr 12, 2023 at 14:59
  • Amazing! Thanks for explaining all the details.
    – Niranjan
    Commented Apr 12, 2023 at 15:04
1

C-h v split-window-preferred-function - it is probably set to split-window-sensibly but you can change it to e.g. split-window-right:

(setq split-window-preferred-function #'split-window-right)

but that will always split to the right (given a choice), so it may or may not work for your purposes.

You can always advise the function that is bound to C-c C-l (I guess that's TeX-recenter-output-buffer) to let-bind split-window-preferred-function around it, so that only this function will be affected:

(defun TeX-recenter-output-buffer-right (orig-fun &rest args)
  (let ((split-window-preferred-function #'split-window-right))
    (apply orig-fun args)))


(advice-add 'TeX-recenter-output-buffer :around #'TeX-recenter-output-buffer-right)

This will limit the effect to just this function. BTW, I think this is right but I have not tested it. If it causes problems, you can always remove the advice to get back to the previous state:

(advice-remove 'TeX-recenter-output-buffer #'TeX-recenter-output-buffer-right)

See Advising Emacs Lisp Functions in the Emacs Lisp Reference manual.

6
  • Hello, thanks for the response, but your first code (the global one) opens the new buffer in a new window altogether, it doesn't create a buffer in the same window.
    – Niranjan
    Commented Apr 12, 2023 at 14:08
  • Second one too :(
    – Niranjan
    Commented Apr 12, 2023 at 14:09
  • The other answer seems to work well though...
    – Niranjan
    Commented Apr 12, 2023 at 14:12
  • I thought you wanted a new window: "... I can change this behavior and always open the new buffer with split-window-right command?" But in any case, you choose which answer to accept, so if the other answer works for you, accept that one!
    – NickD
    Commented Apr 12, 2023 at 14:21
  • 1
    If I understood what you mean I would, but I don't currently. So I'll take a look at the behavior and test it a bit as well (it is untested :-) ) and maybe add some explanations. But the important thing is that you got an answer that you are happy with!
    – NickD
    Commented Apr 12, 2023 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.