update
[emacs-init.git] / setup / mywin.el
index 1887b52..11891c8 100644 (file)
@@ -22,7 +22,7 @@ to 80."
 window smaller than MIN-HEIGHT lines."
   (interactive)
   ;; this algorithm is copied from window.el / balance-windows()
-  (let ((min-height (or min-height i(+ window-min-height 0)))
+  (let ((min-height (or min-height (+ window-min-height 0)))
         (count -1)
         size)
         ;; Don't count the lines that are above the uppermost windows.
@@ -59,32 +59,45 @@ 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 get-top-windows ()
+  (let (topwindows)
     (walk-windows (function (lambda (w)
                               (let ((e (window-edges w)))
                                 (if (< (nth 1 e) window-min-height)
-                                    (setq topwindows (cons (list (nth 0 e)
-                                                                 w
-                                                                 (window-buffer w)
-                                                                 (window-point w)
-                                                                 (window-start w)
-                                                                 (equal w currentwindow))
-                                                           topwindows))))))
-                            'nomini)
-    (setq topwindows (sort topwindows (function (lambda (a b) (< (car a) (car b))))))
-    (setq topwindows (loop for w in topwindows
-                           for (pos win buf point start iscurrent) = w
-                           if (not (member (buffer-name buf) setup-my-windows-junk-buffers))
-                           collect w))
+                                  (setq topwindows (cons (cons (nth 0 e) w) topwindows)))))))
+    (loop for w in (sort topwindows (function (lambda (a b) (< (car a) (car b)))))
+          collect (cdr w) )))
+
+(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 (loop for w in (get-top-windows)
+                           for b = (window-buffer w)
+                           if (not (member (buffer-name b)
+                                           setup-my-windows-junk-buffers))
+                           collect (list (nth 0 (window-edges w))
+                                         w
+                                         b
+                                         (window-point w)
+                                         (window-start w)
+                                         (equal w currentwindow))))
+         firstwindow newwindow newtopwindows newbottomwindow)
     (delete-other-windows (nth 1 (car topwindows)))
     (save-selected-window
-      (select-window (split-window-vertically
-                      (- (window-height) (max 5 (/ (* (frame-height) 15) 100)) -1)))
+      (setq newbottomwindow (split-window-vertically
+                             (- (window-height) (max 5 (/ (* (frame-height) 15) 100)) -1)))
+      (select-window newbottomwindow)
       (switch-to-buffer (get-buffer-create "*compilation*"))
       (if (eq currentbuffer (current-buffer))
           (setq newwindow (selected-window))))
@@ -135,23 +148,43 @@ 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))))
+        (switch-to-buffer currentbuffer))
+    newbottomwindow))
 
 (defun my-split-window-sensibly (window)
   (if (and (> (window-height window) (- (frame-height (window-frame window)) window-min-height))
            (> (window-height window) (max 5 (/ (* (frame-height) 15) 100))))
       (split-window-sensibly window)))
 
+(defun my-pop-to-buffer (buffer)
+  ;; display buffer in rightmost window if not displayed currently
+  (let ((w (get-buffer-window buffer)))
+    (unless w
+      (setq w (car (last (get-top-windows)))))
+    (select-window w)
+    (switch-to-buffer buffer)))
+
+(defun my-display-at-bottom (&optional buffer)
+  ;; call my-setup-window and display current-buffer or BUFFER in bottom frame
+  (interactive)
+  (if (not buffer) (setq buffer (current-buffer)))
+  (bury-buffer)
+  ;; why does save-selected-window not work here ???
+  (save-selected-window
+    (select-window (setup-my-windows))
+    (switch-to-buffer buffer)))
+
 (setq split-window-preferred-function 'my-split-window-sensibly)
 
 (global-set-key "\C-x7" 'split-window-3-horizontally)
 (global-set-key "\C-x8" (lambda () (interactive) (split-window-n-horizontally 100 50)))
 (global-set-key "\C-x9" 'setup-my-windows)
 (global-set-key "\C-\M-_" (lambda () (interactive) (safe-shrink-window 5)))
+(global-set-key "\C-x_" 'my-display-at-bottom)
 
 (defun my-swap-window-to-right (&optional below)
   "If swap buffer in this window with buffer on the right. If BELOW is set,
@@ -206,5 +239,12 @@ 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)
+  (maximize-window 5)
+  (condition-case nil
+      (safe-max-window-horizontally)
+    (error nil)))
+
+(global-set-key "\C-x=" 'safe-max-window)
+(global-set-key "\C-x-" 'maximize-window-15)