* riece.el (riece): Scan riece-startup-channel-list and open servers.
[riece] / lisp / riece.el
1 ;;; riece.el --- IRC client for Emacsen
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
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-display)
28 (require 'riece-server)
29 (require 'riece-compat)
30 (require 'riece-commands)
31 (require 'riece-addon)
32 (require 'riece-signal)
33
34 (autoload 'derived-mode-class "derived")
35
36 (defvar riece-channel-list-mode-map (make-sparse-keymap))
37 (defvar riece-user-list-mode-map (make-sparse-keymap))
38
39 (defvar riece-dialogue-mode-map
40   (let ((keymap (make-keymap)))
41     (suppress-keymap keymap 'nodigit)
42     keymap))
43
44 (defvar riece-command-mode-map (make-keymap))
45 (defvar riece-command-map (make-sparse-keymap))
46
47 (defvar riece-command-mode-syntax-table nil)
48
49 (put 'riece-command-mode 'mode-class 'special)
50 (put 'riece-dialogue-mode 'mode-class 'special)
51 (put 'riece-others-mode 'derived-mode-parent 'riece-dialogue-mode)
52 (put 'riece-channel-list-mode 'mode-class 'special)
53 (put 'riece-user-list-mode 'mode-class 'special)
54 (put 'riece-channel-mode 'derived-mode-parent 'riece-dialogue-mode)
55
56 (defvar riece-buffer-alist
57   '((riece-command-buffer "*Command*" riece-command-mode)
58     (riece-dialogue-buffer "*Dialogue*" riece-dialogue-mode)
59     (riece-others-buffer "*Others*" riece-others-mode)
60     (riece-channel-list-buffer "*Channels*" riece-channel-list-mode)
61     (riece-user-list-buffer " *Users*" riece-user-list-mode)))
62
63 (defvar riece-shrink-buffer-idle-timer nil
64   "Timer object to periodically shrink channel buffers.")
65
66 (defvar riece-addons-insinuated nil
67   "Non nil if add-ons are already insinuated.")
68
69 (defvar riece-select-keys
70   `("1" riece-command-switch-to-channel-by-number-1
71     "2" riece-command-switch-to-channel-by-number-2
72     "3" riece-command-switch-to-channel-by-number-3
73     "4" riece-command-switch-to-channel-by-number-4
74     "5" riece-command-switch-to-channel-by-number-5
75     "6" riece-command-switch-to-channel-by-number-6
76     "7" riece-command-switch-to-channel-by-number-7
77     "8" riece-command-switch-to-channel-by-number-8
78     "9" riece-command-switch-to-channel-by-number-9
79     "0" riece-command-switch-to-channel-by-number-10
80     ,(concat riece-command-prefix "1")
81     riece-command-switch-to-channel-by-number-11
82     ,(concat riece-command-prefix "2")
83     riece-command-switch-to-channel-by-number-12
84     ,(concat riece-command-prefix "3")
85     riece-command-switch-to-channel-by-number-13
86     ,(concat riece-command-prefix "4")
87     riece-command-switch-to-channel-by-number-14
88     ,(concat riece-command-prefix "5")
89     riece-command-switch-to-channel-by-number-15
90     ,(concat riece-command-prefix "6")
91     riece-command-switch-to-channel-by-number-16
92     ,(concat riece-command-prefix "7")
93     riece-command-switch-to-channel-by-number-17
94     ,(concat riece-command-prefix "8")
95     riece-command-switch-to-channel-by-number-18
96     ,(concat riece-command-prefix "9")
97     riece-command-switch-to-channel-by-number-19
98     ,(concat riece-command-prefix "0")
99     riece-command-switch-to-channel-by-number-20))
100
101 ;;; Keymap macros. -- borrowed from `gnus-util.el'.
102 (defmacro riece-local-set-keys (&rest plist)
103   "Set the keys in PLIST in the current keymap."
104   `(riece-define-keys-1 (current-local-map) ',plist))
105
106 (defmacro riece-define-keys (keymap &rest plist)
107   "Assign KEYMAP keys from PLIST."
108   `(riece-define-keys-1 ',keymap ',plist))
109
110 (defmacro riece-define-keys-safe (keymap &rest plist)
111   "Assign KEYMAP keys from PLIST without overwriting previous definitions."
112   `(riece-define-keys-1 ',keymap ',plist t))
113
114 (put 'riece-define-keys 'lisp-indent-function 1)
115 (put 'riece-define-keys-safe 'lisp-indent-function 1)
116 (put 'riece-local-set-keys 'lisp-indent-function 1)
117
118 (defun riece-define-keys-1 (keymap plist &optional safe)
119   "Assign KEYMAP keys from PLIST.
120 If optional argument SAFE is nil, overwrite previous definitions."
121   (unless keymap
122     (error "Can't set keys in a null keymap"))
123   (cond
124    ((symbolp keymap)
125     (setq keymap (symbol-value keymap)))
126    ((keymapp keymap))
127    ((listp keymap)
128     (set (car keymap) nil)
129     (define-prefix-command (car keymap))
130     (define-key (symbol-value (nth 2 keymap))
131       (if (symbolp (nth 1 keymap))
132           (symbol-value (nth 1 keymap))
133         (nth 1 keymap))
134       (car keymap))
135     (setq keymap (symbol-value (car keymap)))))
136   (let (key)
137     (while plist
138       (if (symbolp (setq key (car plist)))
139           (setq key (symbol-value key)))
140       (setq plist (cdr plist))
141       (if (or (not safe)
142               (eq (lookup-key keymap key) 'undefined))
143           (define-key keymap key (car plist))
144         (car plist))
145       (setq plist (cdr plist)))))
146
147 (when t
148   (riece-define-keys riece-dialogue-mode-map
149     "\177" scroll-down
150     [delete] scroll-down
151     [backspace] scroll-down
152     [return] scroll-up
153     " " scroll-up
154     [home] beginning-of-buffer
155     "$" end-of-buffer
156     [end] end-of-buffer
157     "/" riece-command-raw
158     ">" end-of-buffer
159     "<" beginning-of-buffer
160     "^" riece-command-list-addons
161     "\C-ta" riece-command-toggle-away
162     "c" riece-command-select-command-buffer
163     "f" riece-command-finger
164     "\C-tf" riece-command-toggle-freeze
165     "\C-to" riece-command-toggle-own-freeze
166     "\C-tu" riece-command-toggle-user-list-buffer-mode
167     "\C-tc" riece-command-toggle-channel-buffer-mode
168     "\C-tC" riece-command-toggle-channel-list-buffer-mode
169     "\C-tl" riece-command-change-layout
170     "i" riece-command-invite
171     "j" riece-command-join
172     "\C-k" riece-command-kick
173     "l" riece-command-list
174     "M" riece-command-change-mode
175     "n" riece-command-change-nickname
176     "N" riece-command-names
177     "o" other-window
178     "O" riece-command-open-server
179     "C" riece-command-close-server
180     "M" riece-command-universal-server-name-argument
181     "p" riece-command-enter-message-to-user
182     "q" riece-command-quit
183     "r" riece-command-configure-windows
184     "x" riece-command-copy-region
185     "t" riece-command-topic
186     "w" riece-command-who)
187
188   (riece-define-keys riece-command-mode-map
189     "\r" riece-command-enter-message
190     [(control return)] riece-command-enter-message-as-notice
191     [tab] riece-command-complete-user)
192
193   (riece-define-keys (riece-command-map riece-command-prefix
194                                         riece-command-mode-map)
195     "\177" riece-command-scroll-down
196     [delete] riece-command-scroll-down
197     [backspace] riece-command-scroll-down
198     " " riece-command-scroll-up
199     [home] riece-command-beginning-of-buffer
200     "$" riece-command-end-of-buffer
201     [end] riece-command-end-of-buffer
202     ">" riece-command-next-channel
203     "<" riece-command-previous-channel
204     "\C-j" riece-command-next-channel
205     "\C-n" riece-command-names
206     "l" riece-command-list
207     "\C-m" riece-command-change-mode
208     "o" riece-command-set-operators
209     "\C-p" riece-command-part
210     "r" riece-command-configure-windows
211     "v" riece-command-set-speakers
212     "V" riece-version)
213   (set-keymap-parent riece-command-map riece-dialogue-mode-map)
214
215   (riece-define-keys riece-user-list-mode-map
216     "o" riece-command-set-operators
217     "v" riece-command-set-voices
218     "f" riece-command-finger
219     " " riece-command-user-list-scroll-up
220     "\177" riece-command-user-list-scroll-down
221     [delete] riece-command-user-list-scroll-down
222     [backspace] riece-command-user-list-scroll-down
223     "c" riece-command-select-command-buffer)
224
225   (riece-define-keys riece-channel-list-mode-map
226     ">" riece-command-next-channel
227     "<" riece-command-previous-channel
228     "o" other-window
229     "c" riece-command-select-command-buffer)
230
231   (riece-define-keys-1 riece-dialogue-mode-map riece-select-keys)
232   (riece-define-keys-1 riece-channel-list-mode-map riece-select-keys))
233
234 (defun riece-read-variables-files (&optional file)
235   "Read variables FILEs."
236   (or (file-directory-p riece-directory)
237       (make-directory riece-directory))
238   (let ((files (if file
239                    (setq riece-variables-file file
240                          riece-variables-files (list file))
241                  riece-variables-files)))
242     (while files
243       (condition-case nil
244           (load (expand-file-name (car files)))
245         (file-error nil))
246       (setq files (cdr files)))))
247
248 (defvar print-quoted)
249 (defvar print-escape-multibyte)
250 (defun riece-save-variables-files ()
251   "Save current settings to `riece-variables-file'."
252   (with-temp-file riece-saved-variables-file
253     (let ((print-quoted t)
254           (print-readably t)
255           print-escape-multibyte
256           print-level
257           print-length
258           (variables riece-saved-forms))
259       (while variables
260         (prin1 `(setq ,(car variables)
261                       ',(symbol-value (car variables)))
262                (current-buffer))
263         (insert "\n")
264         (setq variables (cdr variables)))))
265   (setq riece-save-variables-are-dirty nil))
266
267 ;;;###autoload
268 (defun riece (&optional confirm)
269   "Connect to the IRC server and start chatting.
270 If optional argument CONFIRM is non-nil, ask which IRC server to connect."
271   (interactive "P")
272   (riece-read-variables-files (if noninteractive
273                                   (car command-line-args-left)))
274   (run-hooks 'riece-after-load-startup-hook)
275   (if (riece-server-opened)
276       (riece-command-configure-windows)
277     (unless riece-addons-insinuated
278       (setq riece-addons (riece-resolve-addons riece-addons))
279       (let ((pointer riece-addons))
280         (while pointer
281           (riece-insinuate-addon (car pointer) riece-debug)
282           (setq pointer (cdr pointer))))
283       (setq riece-addons-insinuated t))
284     (if (or confirm (null riece-server))
285         (setq riece-server (completing-read "Server: " riece-server-alist)))
286     (if (stringp riece-server)
287         (setq riece-server (riece-server-name-to-server riece-server)))
288     (riece-create-buffers)
289     (if riece-max-buffer-size
290         (setq riece-shrink-buffer-idle-timer
291               (riece-run-with-idle-timer
292                riece-shrink-buffer-idle-time-delay t
293                (lambda ()
294                  (let ((buffers riece-buffer-list))
295                    (while buffers
296                      (if (eq (derived-mode-class
297                               (with-current-buffer (car buffers)
298                                 major-mode))
299                              'riece-dialogue-mode)
300                          (riece-shrink-buffer (car buffers)))
301                      (setq buffers (cdr buffers))))))))
302     (switch-to-buffer riece-command-buffer)
303     (riece-display-connect-signals)
304     (riece-redisplay-buffers)
305     (riece-open-server riece-server "")
306     ;; If no server process is available, exit.
307     (if (null riece-server-process-alist)
308         (riece-exit)
309       (let ((server-list riece-startup-server-list))
310         (while server-list
311           (riece-command-open-server (car server-list))
312           (setq server-list (cdr server-list))))
313       (let ((channel-list riece-startup-channel-list)
314             server)
315         (while channel-list
316           (setq server (riece-identity-server
317                         (riece-parse-identity (car channel-list))))
318           (unless (riece-server-opened server)
319             (riece-command-open-server server))
320           (setq channel-list (cdr channel-list))))
321       (let ((pointer riece-addons))
322         (while pointer
323           (unless (get (car pointer) 'riece-addon-default-disabled)
324             (riece-enable-addon (car pointer) riece-debug))
325           (setq pointer (cdr pointer))))
326       (run-hooks 'riece-startup-hook)
327       (message "%s" (substitute-command-keys
328                      "Type \\[describe-mode] for help")))))
329
330 (defun riece-shrink-buffer (buffer)
331   (save-excursion
332     (set-buffer buffer)
333     (goto-char (point-min))
334     (while (> (buffer-size) riece-max-buffer-size)
335       (let* ((inhibit-read-only t)
336              buffer-read-only
337              (start (point))
338              (end (progn (beginning-of-line 2) (point)))
339              (overlays (riece-overlays-in start end)))
340         (while overlays
341           (riece-delete-overlay (car overlays))
342           (setq overlays (cdr overlays)))
343         (delete-region start end)))))
344
345 (defun riece-exit ()
346   (if riece-save-variables-are-dirty
347       (riece-save-variables-files))
348   (while riece-buffer-list
349     (if (and (get-buffer (car riece-buffer-list))
350              (buffer-live-p (car riece-buffer-list)))
351         (funcall riece-buffer-dispose-function (car riece-buffer-list)))
352     (setq riece-buffer-list (cdr riece-buffer-list)))
353   (if riece-shrink-buffer-idle-timer
354       (riece-cancel-timer riece-shrink-buffer-idle-timer))
355   (riece-clear-signal-slots)
356   (setq riece-server nil
357         riece-current-channels nil
358         riece-current-channel nil
359         riece-channel-buffer nil
360         riece-channel-buffer-alist nil
361         riece-user-indicator nil
362         riece-long-channel-indicator "None"
363         riece-channel-list-indicator "No channel"
364         riece-away-indicator "-"
365         riece-operator-indicator "-"
366         riece-freeze-indicator "-")
367   (delete-other-windows)
368   (run-hooks 'riece-exit-hook))
369
370 (defun riece-command-mode ()
371   "Major mode for Riece.  Normal edit function are available.
372 Typing Return or Linefeed enters the current line in the dialogue.
373 The following special commands are available:
374 For a list of the generic commands type \\[riece-command-generic] ? RET.
375 \\{riece-command-mode-map}"
376   (interactive)
377   (kill-all-local-variables)
378
379   ;; Make `truncate-partial-width-windows' buffer local and set it to
380   ;; nil.  This causes `truncate-lines' to directly control line
381   ;; truncation.
382   (make-local-variable 'truncate-partial-width-windows)
383   (setq truncate-partial-width-windows nil)
384
385   (setq riece-away-indicator "-"
386         riece-operator-indicator "-"
387         major-mode 'riece-command-mode
388         mode-name "Command"
389         mode-line-buffer-identification
390         (riece-mode-line-buffer-identification
391          '("Riece: "
392            riece-away-indicator
393            riece-operator-indicator
394            " "
395            riece-user-indicator
396            " "
397            riece-channel-indicator))
398         truncate-lines nil)
399   (riece-simplify-mode-line-format)
400   (use-local-map riece-command-mode-map)
401
402   (unless riece-command-mode-syntax-table
403     (setq riece-command-mode-syntax-table
404           (copy-syntax-table (syntax-table)))
405     (set-syntax-table riece-command-mode-syntax-table)
406     (mapcar
407      (lambda (c) (modify-syntax-entry c "w"))
408      "^[]{}'`"))
409
410   (run-hooks 'riece-command-mode-hook))
411
412 (defun riece-dialogue-mode ()
413   "Major mode for displaying the IRC dialogue.
414 All normal editing commands are turned off.
415 Instead, these commands are available:
416 \\{riece-dialogue-mode-map}"
417   (kill-all-local-variables)
418   (make-local-variable 'riece-freeze)
419   (make-local-variable 'riece-freeze-indicator)
420
421   ;; Make `truncate-partial-width-windows' buffer local and set it to
422   ;; nil.  This causes `truncate-lines' to directly control line truncation.
423   (make-local-variable 'truncate-partial-width-windows)
424   (setq truncate-partial-width-windows nil)
425
426   (setq riece-freeze riece-default-freeze
427         riece-away-indicator "-"
428         riece-operator-indicator "-"
429         major-mode 'riece-dialogue-mode
430         mode-name "Dialogue"
431         mode-line-buffer-identification
432         (riece-mode-line-buffer-identification
433          '("Riece: "
434            riece-away-indicator
435            riece-operator-indicator
436            riece-freeze-indicator
437            " "
438            riece-channel-list-indicator " "))
439         truncate-lines nil
440         buffer-read-only t)
441   (riece-simplify-mode-line-format)
442   (use-local-map riece-dialogue-mode-map)
443   (buffer-disable-undo)
444   (run-hooks 'riece-dialogue-mode-hook))
445
446 (define-derived-mode riece-others-mode riece-dialogue-mode
447   "Others"
448   "Major mode for displaying the IRC others message except current channel.
449 All normal editing commands are turned off.
450 Instead, these commands are available:
451 \\{riece-others-mode-map}")
452
453 (define-derived-mode riece-channel-mode riece-dialogue-mode
454   "Channel"
455   "Major mode for displaying the IRC current channel buffer.
456 All normal editing commands are turned off.
457 Instead, these commands are available:
458 \\{riece-channel-mode-map}"
459   (make-local-variable 'riece-channel-buffer-window-point)
460   (setq mode-line-buffer-identification
461         (riece-mode-line-buffer-identification
462          '("Riece: "
463            riece-away-indicator
464            riece-operator-indicator
465            riece-freeze-indicator
466            " "
467            riece-long-channel-indicator))))
468
469 (defun riece-channel-list-mode ()
470   "Major mode for displaying channel list.
471 All normal editing commands are turned off."
472   (kill-all-local-variables)
473   (buffer-disable-undo)
474
475   ;; Make `truncate-partial-width-windows' buffer local and set it to
476   ;; nil.  This causes `truncate-lines' to directly control line truncation.
477   (make-local-variable 'truncate-partial-width-windows)
478   (setq truncate-partial-width-windows nil)
479
480   (setq major-mode 'riece-channel-list-mode
481         mode-name "Channels"
482         mode-line-buffer-identification
483         (riece-mode-line-buffer-identification '("Riece: "))
484         truncate-lines t
485         buffer-read-only t)
486   (make-local-hook 'riece-update-buffer-functions)
487   (add-hook 'riece-update-buffer-functions
488             'riece-update-channel-list-buffer nil t)
489   (use-local-map riece-channel-list-mode-map)
490   (run-hooks 'riece-channel-list-mode-hook))
491
492 (defun riece-user-list-mode ()
493   "Major mode for displaying members in the IRC current channel buffer.
494 All normal editing commands are turned off.
495 Instead, these commands are available:
496 \\{riece-user-list-mode-map}"
497   (kill-all-local-variables)
498   (buffer-disable-undo)
499
500   ;; Make `truncate-partial-width-windows' buffer local and set it to
501   ;; nil.  This causes `truncate-lines' to directly control line truncation.
502   (make-local-variable 'truncate-partial-width-windows)
503   (setq truncate-partial-width-windows nil)
504
505   (setq major-mode 'riece-user-list-mode
506         mode-name "Users"
507         mode-line-buffer-identification
508         (riece-mode-line-buffer-identification
509          '("Riece: " riece-long-channel-indicator " "))
510         truncate-lines t
511         buffer-read-only t)
512   (if (boundp 'transient-mark-mode)
513       (set (make-local-variable 'transient-mark-mode) t))
514   (make-local-hook 'riece-update-buffer-functions)
515   (add-hook 'riece-update-buffer-functions
516             'riece-update-user-list-buffer nil t)
517   (use-local-map riece-user-list-mode-map)
518   (run-hooks 'riece-user-list-mode-hook))
519
520 (defun riece-create-buffers ()
521   (let ((alist riece-buffer-alist))
522     (while alist
523       (save-excursion
524         (set-buffer (apply #'riece-get-buffer-create
525                            (cdr (car alist))))
526         (set (car (car alist)) (current-buffer))
527         (unless (or (null (nth 2 (car alist)))
528                     (eq major-mode (nth 2 (car alist))))
529           (funcall (nth 2 (car alist))))
530         (setq alist (cdr alist))))))
531
532 (provide 'riece)
533
534 ;;; riece.el ends here