better C-x = (safe-maximize-window) binding
[emacs-init.git] / setup / mywin.el
index 1887b52..4fde816 100644 (file)
@@ -59,12 +59,21 @@ window smaller than MIN-HEIGHT lines."
 (defconst setup-my-windows-junk-buffers
   '("*scratch*" "*Messages*" "*Calculator" "*Calc Trail*" "*compilation*" "*fetchmail*"))
 
-(defun setup-my-windows ()
-  (interactive)
-  (let ((width 100) (min 100) (distribute t)
-        (currentbuffer (current-buffer))
-        (currentwindow (selected-window))
-        topwindows firstwindow newwindow newtopwindows)
+(defvar my-windows-count nil)
+
+(defun setup-my-windows (&optional n)
+  (interactive "P")
+  (if n
+      (if (integerp n)
+          (setq my-windows-count n)
+        (setq my-windows-count nil)))
+  (let* ((width (if my-windows-count
+                    (- (/ (frame-width) my-windows-count) 4)
+                  (or whitespace-line-column 100)))
+         (min width) (distribute t)
+         (currentbuffer (current-buffer))
+         (currentwindow (selected-window))
+         topwindows firstwindow newwindow newtopwindows)
     (walk-windows (function (lambda (w)
                               (let ((e (window-edges w)))
                                 (if (< (nth 1 e) window-min-height)
@@ -135,8 +144,8 @@ window smaller than MIN-HEIGHT lines."
                     if (and (= (aref name 0) ?*)
                             (not (member name setup-my-windows-precious-buffers))) return w)
               firstwindow))
-    (if (and distribute (> (length newtopwindows) 1))
-        (balance-windows firstwindow))
+    (when (and distribute (> (length newtopwindows) 1))
+      (pjb-balance-windows t))
     (select-window newwindow)
     (if (not (member (buffer-name currentbuffer) setup-my-windows-junk-buffers))
         (switch-to-buffer currentbuffer))))
@@ -206,5 +215,10 @@ the buffer stack in the current window."
         (error "Current window is not a top window"))
     (shrink-window-horizontally (- (- width (window-width) (* window-min-width (1- count)))))))
 
-(global-set-key "\C-x=" 'safe-max-window-horizontally)
-(global-set-key "\C-x-" 'maximize-window-15)
\ No newline at end of file
+(defun safe-max-window ()
+  (interactive)
+  (safe-max-window-horizontally)
+  (maximize-window 5))
+
+(global-set-key "\C-x=" 'safe-max-window)
+(global-set-key "\C-x-" 'maximize-window-15)