* riece-layout.el (riece-set-window-points): Use (recenter -1) to
[riece] / lisp / riece-layout.el
index f36960b..5970983 100644 (file)
@@ -1,7 +1,8 @@
-;;; riece-layout.el --- layout manager add-on
+;;; riece-layout.el --- layout management
 ;; Copyright (C) 1998-2003 Daiki Ueno
 
 ;; Author: Daiki Ueno <ueno@unixuser.org>
+;;     TAKAHASHI "beatmaria" Kaoru <kaoru@kaisei.org>
 ;; Keywords: IRC, riece
 
 ;; This file is part of Riece.
 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 ;; Boston, MA 02111-1307, USA.
 
-;; This add-on allows you to switch window configurations by their names.
-
-;; To use, add the following line to your ~/.riece/init.el:
-;; (add-to-list 'riece-addons 'riece-layout)
-
 ;;; Code:
 
-(require 'riece-display)
+(require 'riece-globals)
+(require 'riece-misc)
 
 (defgroup riece-layout nil
   "Manage window layouts"
   :prefix "riece-"
   :group 'riece)
 
-(defcustom riece-default-layout "default"
-  "The default layout name."
-  :type 'string
-  :group 'riece-layout)
-
-(defcustom riece-layout riece-default-layout
+(defcustom riece-layout "default"
   "Current layout setting."
   :type 'string
   :group 'riece-layout)
 
 (defcustom riece-layout-alist
-  '(("default" riece-configure-windows riece-configure-windows-predicate)
-    ("top" riece-configure-windows-top riece-configure-windows-predicate))
-  "An alist mapping the names to configure/predicate functions."
+  '(("middle-right" riece-configure-windows right middle)
+    ("middle-left" riece-configure-windows left middle)
+    ("top-right" riece-configure-windows right top)
+    ("top-left" riece-configure-windows left top)
+    ("bottom-right" riece-configure-windows right bottom)
+    ("bottom-left" riece-configure-windows left bottom)
+    ("top" riece-configure-windows-top)
+    ("default" . "middle-right"))
+  "An alist mapping the names to layout functions.
+An element of this alist is either in the following forms:
+
+(NAME CONFIGURE-FUNCTION [PARAMETERS])
+(NAME1 . NAME2)
+
+In the first form, NAME is a string which specifies the layout
+setting, and CONFIGURE-FUNCTION is a function which does window
+splitting, etc.  PARAMETERS are collected and passed to CONFIGURE-FUNCTION.
+In the second form, NAME1 is an alias for NAME2."
   :type 'list
   :group 'riece-layout)
 
-(defun riece-layout-option-set-function (symbol value)
-  "Function called when setting `riece-layout'."
-  (let ((layout (cdr (assoc value riece-layout-alist))))
+(defun riece-redraw-layout (&optional force)
+  "Reconfigure windows with the current layout.
+If optional argument FORCE is non-nil, window reconfiguration will
+happen unconditionally."
+  (let ((layout (cdr (assoc riece-layout riece-layout-alist))))
     (unless layout
       (error "No such layout!"))
-    (setq riece-configure-windows-function (car layout)
-         riece-configure-windows-predicate (nth 1 layout))
-    (riece-redisplay-buffers t)))
-
-(defun riece-command-change-layout (name)
-  "Select a layout-name from all current available layouts and change
-the layout to the selected layout-name."
-  (interactive (list (completing-read "Layout: " riece-layout-alist)))
-  (customize-set-variable 'riece-layout name))
-
-(defvar riece-dialogue-mode-map)
-
-(defun riece-layout-insinuate ()
-  (define-key riece-dialogue-mode-map "\C-tl" #'riece-command-change-layout)
-  ;; Delay setting `riece-layout' using riece-layout-option-set-function.
-  (add-hook 'riece-startup-hook
-           (lambda ()
-             (put 'riece-layout 'custom-set
-                  'riece-layout-option-set-function)
-             (riece-layout-option-set-function 'riece-layout riece-layout))))
+    (if (stringp layout)
+       (let ((riece-layout layout))
+         (riece-redraw-layout force))
+      (if (or force
+             (riece-reconfigure-windows-predicate))
+         (apply (car layout) (cdr layout))))))
+
+(defun riece-set-window-points ()
+  (if (get-buffer-window riece-user-list-buffer)
+      (with-current-buffer riece-user-list-buffer
+       (unless (riece-frozen riece-user-list-buffer)
+         (set-window-start (get-buffer-window riece-user-list-buffer)
+                           (point-min)))))
+  (if (get-buffer-window riece-channel-list-buffer)
+      (with-current-buffer riece-channel-list-buffer
+       (unless (riece-frozen riece-channel-list-buffer)
+         (set-window-start (get-buffer-window riece-channel-list-buffer)
+                           (point-min)))))
+  (if (and riece-channel-buffer
+          (get-buffer-window riece-channel-buffer))
+      (with-current-buffer riece-channel-buffer
+       (if (riece-frozen riece-channel-buffer)
+           (if riece-channel-buffer-window-point
+               (set-window-point (get-buffer-window riece-channel-buffer)
+                                 riece-channel-buffer-window-point))
+         (recenter -1 (get-buffer-window riece-channel-buffer)))))
+  (if (get-buffer-window riece-others-buffer)
+      (with-current-buffer riece-others-buffer
+       (unless (riece-frozen riece-others-buffer)
+         (recenter -1 (get-buffer-window riece-others-buffer)))))
+  (if (get-buffer-window riece-dialogue-buffer)
+      (with-current-buffer riece-dialogue-buffer
+       (unless (riece-frozen riece-dialogue-buffer)
+         (recenter -1 (get-buffer-window riece-dialogue-buffer))))))
+
+(defun riece-reconfigure-windows-predicate ()
+  "Return t, if window reconfiguration is needed.
+This function is used by \"default\" layout."
+  (memq (window-buffer (selected-window))
+       riece-buffer-list))
+
+(defun riece-configure-windows (hpos vpos)
+  (let ((buffer (window-buffer))
+       (show-user-list
+        (and riece-user-list-buffer-mode
+             riece-current-channel
+             ;; User list buffer is nuisance for private conversation.
+             (riece-channel-p (riece-identity-prefix
+                               riece-current-channel)))))
+    ;; Can't expand minibuffer to full frame.
+    (if (eq (selected-window) (minibuffer-window))
+       (other-window 1))
+    (delete-other-windows)
+    (if (and riece-current-channel
+            (or show-user-list riece-channel-list-buffer-mode))
+       (let ((rest-window (split-window (selected-window)
+                                        (if (eq hpos 'left)
+                                            (- (window-width)
+                                               (/ (window-width) 5))
+                                          (/ (window-width) 5))
+                                        t)))
+         (when (eq hpos 'left)
+           (setq rest-window (selected-window))
+           (other-window 1))
+         (if (and show-user-list riece-channel-list-buffer-mode)
+             (progn
+               (set-window-buffer (split-window)
+                                  riece-channel-list-buffer)
+               (set-window-buffer (selected-window)
+                                  riece-user-list-buffer))
+           (if show-user-list
+               (set-window-buffer (selected-window)
+                                  riece-user-list-buffer)
+             (if riece-channel-list-buffer-mode
+                 (set-window-buffer (selected-window)
+                                    riece-channel-list-buffer))))
+         (select-window rest-window)))
+    (if (and riece-current-channel
+            riece-channel-buffer-mode)
+       (progn
+         (if (eq vpos 'top)
+             (let ((rest-window (split-window nil 4)))
+               (set-window-buffer (selected-window)
+                                  riece-command-buffer)
+               (select-window rest-window)
+               (set-window-buffer (split-window rest-window)
+                                  riece-others-buffer)
+               (set-window-buffer (selected-window)
+                                  riece-channel-buffer))
+           (if (eq vpos 'middle)
+               (let ((rest-window (split-window)))
+                 (set-window-buffer (selected-window)
+                                    riece-channel-buffer)
+                 (set-window-buffer (split-window rest-window 4)
+                                    riece-others-buffer)
+                 (set-window-buffer rest-window
+                                    riece-command-buffer))
+             (let ((rest-window (split-window nil (- (window-height) 4))))
+               (set-window-buffer (selected-window)
+                                  riece-others-buffer)
+               (set-window-buffer (split-window)
+                                  riece-channel-buffer)
+               (set-window-buffer rest-window
+                                  riece-command-buffer))))
+         (with-current-buffer riece-channel-buffer
+           (setq truncate-partial-width-windows nil))
+         (with-current-buffer riece-others-buffer
+           (setq truncate-partial-width-windows nil)))
+      (if (eq vpos 'bottom)
+         (progn
+           (set-window-buffer (selected-window)
+                              riece-command-buffer)
+           (set-window-buffer (split-window (selected-window) 4)
+                              riece-dialogue-buffer))
+       (set-window-buffer (split-window (selected-window) 4)
+                          riece-dialogue-buffer)
+       (set-window-buffer (selected-window)
+                          riece-command-buffer)))
+    (riece-set-window-points)
+    (select-window (or (get-buffer-window buffer)
+                      (get-buffer-window riece-command-buffer)))))
+
+(defun riece-configure-windows-top (&rest plist)
+  "Candidate of `riece-configure-windows-function'.
+PLIST accept :command-height, :user-list-width, and :channel-list-width."
+  (let ((command-height (or (plist-get plist :command-height) 4))
+       (user-list-width (or (plist-get plist :user-list-width) (+ 9 1 1)))
+       (channel-list-width (or (plist-get plist :channel-list-width) 18))
+       (buffer (window-buffer))
+       (show-user-list
+        (and riece-user-list-buffer-mode
+             riece-current-channel
+             ;; User list buffer is nuisance for private conversation.
+             (riece-channel-p (riece-identity-prefix
+                               riece-current-channel)))))
+    ;; Can't expand minibuffer to full frame.
+    (when (eq (selected-window) (minibuffer-window))
+      (other-window 1))
+    (delete-other-windows)
+    ;; top of frame
+    (let ((rest-window (split-window (selected-window) command-height)))
+      (set-window-buffer (selected-window)
+                        riece-command-buffer)
+      (select-window rest-window))
+    ;; middle of frame (vertical-spilit when need)
+    (when (or (and riece-current-channel riece-channel-buffer-mode)
+             show-user-list)
+      (let ((rest-window
+            (split-window (selected-window)
+                          (/ (* 5 (+ (window-height) command-height)) 8))))
+       (cond
+        ;; channel-buffer + user-list
+        ((and show-user-list
+              (and riece-current-channel riece-channel-buffer-mode))
+         (let ((user-list-window (split-window (selected-window) nil t)))
+           (set-window-buffer (selected-window) riece-channel-buffer)
+           (set-window-buffer user-list-window riece-user-list-buffer)
+           (select-window user-list-window)
+           (shrink-window-horizontally (- (window-width) user-list-width))
+           (setq truncate-partial-width-windows nil)))
+        ;; only user-list
+        (show-user-list
+         (set-window-buffer (selected-window) riece-user-list-buffer))
+        ;; only channel-buffer
+        (riece-channel-buffer-mode
+         (set-window-buffer (selected-window) riece-channel-buffer)))
+       (select-window rest-window)))
+    ;; bottom of frame
+    (if (and riece-current-channel
+            riece-channel-list-buffer-mode)
+       (let ((channel-list-window (split-window (selected-window) nil t)))
+         (set-window-buffer (selected-window) riece-others-buffer)
+         (set-window-buffer channel-list-window riece-channel-list-buffer)
+         (select-window channel-list-window)
+         (shrink-window-horizontally (- (window-width) channel-list-width))
+         (setq truncate-partial-width-windows nil))
+      (set-window-buffer (selected-window) riece-dialogue-buffer))
+    (riece-set-window-points)
+    (select-window (or (get-buffer-window buffer)
+                      (get-buffer-window riece-command-buffer)))))
 
 (provide 'riece-layout)