Sunday, August 2, 2026

Some emacs lisp utilities of mine, that have been useful for me.

Several utilities to ease the pain of working with a  single monitor

Working on a small monitor, like a laptop screen, can be a strain.  To make this easier, I wrote some utilities in emacs lisp.  Another one was written by someone named "Gildea" on a mailing list.  They have been useful to me; perhaps they will be useful to others.  They are simple, and certainly could be improved upon. 
 
First open a second window, horizontally (\C-x3).  Then use the first functoin to enlarge one of the windows horizontally.  It is hardwired to enlarge, horizontally, by 20 character widths, which can be changed. One can just as easily change the sizes of windows by dragging with a mouse.
 

I.   Enlarge one of two horizontally split windows

(defun enlarge-window-very-horizontally ()
  "Enlarge this window horizontally."
  (interactive)
  (enlarge-window-horizontally 20))
 

II.  Flip flop: Move to other window and swap widths.

This utility does two things: it moves the cursor to the other window; and it swaps the sizes of the two windows.    
 
(defun flip-flop-and-change-horizontal-windows ()
  "Swap HORIZONTAL window sizes; move into the other window.  This function
enables one to have two windows visible, but with one very narrow."
   (interactive)
   (let ((current-window-width (window-width)) 
     (new-window-width nil)
     (delta-window-width nil))
     (other-window 1)
     (setq new-window-width (window-width))
     (setq delta-window-width (- current-window-width new-window-width))
     (enlarge-window-horizontally delta-window-width)))

 

 III.  Flip flop vertically (like the above)

This version works with vertically split windows.  This might work better in some cases.  These utilities are minimal, written by a naive emacs user.  I feel certain they can be modified and improved in various ways.  I would like to find a way that the tool above, for horizontally split windows, can move back and forth, even when either or both sides is split into two or more windows vertically.  They sky's the limit, but these utilities are simple, and therefore easy to use and remember.

 

(defun flip-flop-and-change-windows ()
  "Swap window sizes; move into the other window.  This function
enables one to have two windows visible, but with one reduced to two lines."
   (interactive)
   (let ((current-window-height (window-height)) 
     (new-window-height nil)
     (delta-window-height nil))
     (other-window 1)
     (setq new-window-height (window-height))
     (setq delta-window-height (- current-window-height new-window-height))
     (enlarge-window delta-window-height)))
 

IV.  Swap positions of two windows with contents. 

 Another tool was written by a "Gildea" on a mailing list in 1988, possibly in answer to a query of mine.  It enables one to just swap the positions of two whole windows. 
 
 (defun swap-window-positions ()
  "Swap the positions of this window and the next one.
(credit to gildea Nov 88)"
  (interactive)
  (let ((other-window (next-window (selected-window) 'no-minibuf)))
    (let ((other-window-buffer (window-buffer other-window))
      (other-window-hscroll (window-hscroll other-window))
      (other-window-point (window-point other-window))
      (other-window-start (window-start other-window)))
      (set-window-buffer other-window (current-buffer))
      (set-window-hscroll other-window (window-hscroll (selected-window)))
      (set-window-point other-window (point))
      (set-window-start other-window (window-start (selected-window)))
      (set-window-buffer (selected-window) other-window-buffer)
      (set-window-hscroll (selected-window) other-window-hscroll)
      (set-window-point (selected-window) other-window-point)
      (set-window-start (selected-window) other-window-start))
    (select-window other-window)))


 I hope these are useful, as they have been useful to me.

No comments:

Some emacs lisp utilities of mine, that have been useful for me.

Several utilities to ease the pain of working with a  single monitor Working on a small monitor, like a laptop screen, can be a strain.  To ...