* riece-addon.el (riece-resolve-addons): Fixed
[riece] / lisp / riece-layout.el
1 ;;; riece-layout.el --- layout management
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;;      TAKAHASHI "beatmaria" Kaoru <kaoru@kaisei.org>
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'riece-globals)
28 (require 'riece-misc)
29
30 (defgroup riece-layout nil
31   "Manage window layouts"
32   :prefix "riece-"
33   :group 'riece)
34
35 (defcustom riece-layout "default"
36   "Current layout setting."
37   :type 'string
38   :group 'riece-layout)
39
40 (defcustom riece-layout-alist
41   '(("middle-right" riece-configure-windows right middle)
42     ("middle-left" riece-configure-windows left middle)
43     ("top-right" riece-configure-windows right top)
44     ("top-left" riece-configure-windows left top)
45     ("bottom-right" riece-configure-windows right bottom)
46     ("bottom-left" riece-configure-windows left bottom)
47     ("top" riece-configure-windows-top)
48     ("default" . "middle-right"))
49   "An alist mapping the names to layout functions.
50 An element of this alist is either in the following forms:
51
52 (NAME CONFIGURE-FUNCTION [PARAMETERS])
53 (NAME1 . NAME2)
54
55 In the first form, NAME is a string which specifies the layout
56 setting, and CONFIGURE-FUNCTION is a function which does window
57 splitting, etc.  PARAMETERS are collected and passed to CONFIGURE-FUNCTION.
58 In the second form, NAME1 is an alias for NAME2."
59   :type 'list
60   :group 'riece-layout)
61
62 (defun riece-redraw-layout (&optional force)
63   "Reconfigure windows with the current layout.
64 If optional argument FORCE is non-nil, window reconfiguration will
65 happen unconditionally."
66   (let ((layout (cdr (assoc riece-layout riece-layout-alist))))
67     (unless layout
68       (error "No such layout!"))
69     (if (stringp layout)
70         (let ((riece-layout layout))
71           (riece-redraw-layout force))
72       (if (or force
73               (riece-reconfigure-windows-predicate))
74           (apply (car layout) (cdr layout))))))
75
76 (defun riece-set-window-points ()
77   (if (get-buffer-window riece-user-list-buffer)
78       (with-current-buffer riece-user-list-buffer
79         (unless (riece-frozen riece-user-list-buffer)
80           (set-window-start (get-buffer-window riece-user-list-buffer)
81                             (point-min)))))
82   (if (get-buffer-window riece-channel-list-buffer)
83       (with-current-buffer riece-channel-list-buffer
84         (unless (riece-frozen riece-channel-list-buffer)
85           (set-window-start (get-buffer-window riece-channel-list-buffer)
86                             (point-min)))))
87   (if (and riece-channel-buffer
88            (get-buffer-window riece-channel-buffer))
89       (with-current-buffer riece-channel-buffer
90         (if (riece-frozen riece-channel-buffer)
91             (if riece-channel-buffer-window-point
92                 (set-window-point (get-buffer-window riece-channel-buffer)
93                                   riece-channel-buffer-window-point))
94           (set-window-point (get-buffer-window riece-channel-buffer)
95                             (point-max)))))
96   (if (get-buffer-window riece-others-buffer)
97       (with-current-buffer riece-others-buffer
98         (unless (riece-frozen riece-others-buffer)
99           (set-window-point (get-buffer-window riece-others-buffer)
100                             (point-max)))))
101   (if (get-buffer-window riece-dialogue-buffer)
102       (with-current-buffer riece-dialogue-buffer
103         (unless (riece-frozen riece-dialogue-buffer)
104           (set-window-point (get-buffer-window riece-dialogue-buffer)
105                             (point-max))))))
106
107 (defun riece-reconfigure-windows-predicate ()
108   "Return t, if window reconfiguration is needed.
109 This function is used by \"default\" layout."
110   (memq (window-buffer (selected-window))
111         riece-buffer-list))
112
113 (defun riece-configure-windows (hpos vpos)
114   (let ((buffer (window-buffer))
115         (show-user-list
116          (and riece-user-list-buffer-mode
117               riece-current-channel
118               ;; User list buffer is nuisance for private conversation.
119               (riece-channel-p (riece-identity-prefix
120                                 riece-current-channel)))))
121     ;; Can't expand minibuffer to full frame.
122     (if (eq (selected-window) (minibuffer-window))
123         (other-window 1))
124     (delete-other-windows)
125     (if (and riece-current-channel
126              (or show-user-list riece-channel-list-buffer-mode))
127         (let ((rest-window (split-window (selected-window)
128                                          (if (eq hpos 'left)
129                                              (- (window-width)
130                                                 (/ (window-width) 5))
131                                            (/ (window-width) 5))
132                                          t)))
133           (when (eq hpos 'left)
134             (setq rest-window (selected-window))
135             (other-window 1))
136           (if (and show-user-list riece-channel-list-buffer-mode)
137               (progn
138                 (set-window-buffer (split-window)
139                                    riece-channel-list-buffer)
140                 (set-window-buffer (selected-window)
141                                    riece-user-list-buffer))
142             (if show-user-list
143                 (set-window-buffer (selected-window)
144                                    riece-user-list-buffer)
145               (if riece-channel-list-buffer-mode
146                   (set-window-buffer (selected-window)
147                                      riece-channel-list-buffer))))
148           (select-window rest-window)))
149     (if (and riece-current-channel
150              riece-channel-buffer-mode)
151         (progn
152           (if (eq vpos 'top)
153               (let ((rest-window (split-window nil 4)))
154                 (set-window-buffer (selected-window)
155                                    riece-command-buffer)
156                 (select-window rest-window)
157                 (set-window-buffer (split-window rest-window)
158                                    riece-others-buffer)
159                 (set-window-buffer (selected-window)
160                                    riece-channel-buffer))
161             (if (eq vpos 'middle)
162                 (let ((rest-window (split-window)))
163                   (set-window-buffer (selected-window)
164                                      riece-channel-buffer)
165                   (set-window-buffer (split-window rest-window 4)
166                                      riece-others-buffer)
167                   (set-window-buffer rest-window
168                                      riece-command-buffer))
169               (let ((rest-window (split-window nil (- (window-height) 4))))
170                 (set-window-buffer (selected-window)
171                                    riece-others-buffer)
172                 (set-window-buffer (split-window)
173                                    riece-channel-buffer)
174                 (set-window-buffer rest-window
175                                    riece-command-buffer)))))
176       (if (eq vpos 'bottom)
177           (progn
178             (set-window-buffer (selected-window)
179                                riece-command-buffer)
180             (set-window-buffer (split-window (selected-window) 4)
181                                riece-dialogue-buffer))
182         (set-window-buffer (split-window (selected-window) 4)
183                            riece-dialogue-buffer)
184         (set-window-buffer (selected-window)
185                            riece-command-buffer)))
186     (riece-set-window-points)
187     (select-window (or (get-buffer-window buffer)
188                        (get-buffer-window riece-command-buffer)))))
189
190 (defun riece-configure-windows-top (&rest plist)
191   "Candidate of `riece-configure-windows-function'.
192 PLIST accept :command-height, :user-list-width, and :channel-list-width."
193   (let ((command-height (or (plist-get plist :command-height) 4))
194         (user-list-width (or (plist-get plist :user-list-width) (+ 9 1 1)))
195         (channel-list-width (or (plist-get plist :channel-list-width) 18))
196         (buffer (window-buffer))
197         (show-user-list
198          (and riece-user-list-buffer-mode
199               riece-current-channel
200               ;; User list buffer is nuisance for private conversation.
201               (riece-channel-p (riece-identity-prefix
202                                 riece-current-channel)))))
203     ;; Can't expand minibuffer to full frame.
204     (when (eq (selected-window) (minibuffer-window))
205       (other-window 1))
206     (delete-other-windows)
207     ;; top of frame
208     (let ((rest-window (split-window (selected-window) command-height)))
209       (set-window-buffer (selected-window)
210                          riece-command-buffer)
211       (select-window rest-window))
212     ;; middle of frame (vertical-spilit when need)
213     (when (or (and riece-current-channel riece-channel-buffer-mode)
214               show-user-list)
215       (let ((rest-window
216              (split-window (selected-window)
217                            (/ (* 5 (+ (window-height) command-height)) 8))))
218         (cond
219          ;; channel-buffer + user-list
220          ((and show-user-list
221                (and riece-current-channel riece-channel-buffer-mode))
222           (let ((user-list-window (split-window (selected-window) nil t)))
223             (set-window-buffer (selected-window) riece-channel-buffer)
224             (set-window-buffer user-list-window riece-user-list-buffer)
225             (select-window user-list-window)
226             (shrink-window-horizontally (- (window-width) user-list-width))))
227          ;; only user-list
228          (show-user-list
229           (set-window-buffer (selected-window) riece-user-list-buffer))
230          ;; only channel-buffer
231          (riece-channel-buffer-mode
232           (set-window-buffer (selected-window) riece-channel-buffer)))
233         (select-window rest-window)))
234     ;; bottom of frame
235     (if (and riece-current-channel
236              riece-channel-list-buffer-mode)
237         (let ((channel-list-window (split-window (selected-window) nil t)))
238           (set-window-buffer (selected-window) riece-others-buffer)
239           (set-window-buffer channel-list-window riece-channel-list-buffer)
240           (select-window channel-list-window)
241           (shrink-window-horizontally (- (window-width) channel-list-width)))
242       (set-window-buffer (selected-window) riece-dialogue-buffer))
243     (riece-set-window-points)
244     (select-window (or (get-buffer-window buffer)
245                        (get-buffer-window riece-command-buffer)))))
246
247 (provide 'riece-layout)
248
249 ;;; riece-layout.el ends here