e1f0ac25f14b70d42868fa5390a8c5c6fa3a5d36
[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     (let ((server-list riece-startup-server-list))
307       (while server-list
308         (riece-command-open-server (car server-list))
309         (setq server-list (cdr server-list))))
310     (let ((pointer riece-addons))
311       (while pointer
312         (unless (get (car pointer) 'riece-addon-default-disabled)
313           (riece-enable-addon (car pointer) riece-debug))
314         (setq pointer (cdr pointer))))
315     (run-hooks 'riece-startup-hook)
316     (message "%s" (substitute-command-keys
317                    "Type \\[describe-mode] for help"))))
318
319 (defun riece-shrink-buffer (buffer)
320   (save-excursion
321     (set-buffer buffer)
322     (goto-char (point-min))
323     (while (> (buffer-size) riece-max-buffer-size)
324       (let* ((inhibit-read-only t)
325              buffer-read-only
326              (start (point))
327              (end (progn (beginning-of-line 2) (point)))
328              (overlays (riece-overlays-in start end)))
329         (while overlays
330           (riece-delete-overlay (car overlays))
331           (setq overlays (cdr overlays)))
332         (delete-region start end)))))
333
334 (defun riece-exit ()
335   (if riece-save-variables-are-dirty
336       (riece-save-variables-files))
337   (while riece-buffer-list
338     (if (and (get-buffer (car riece-buffer-list))
339              (buffer-live-p (car riece-buffer-list)))
340         (funcall riece-buffer-dispose-function (car riece-buffer-list)))
341     (setq riece-buffer-list (cdr riece-buffer-list)))
342   (if riece-shrink-buffer-idle-timer
343       (riece-cancel-timer riece-shrink-buffer-idle-timer))
344   (riece-clear-signal-slots)
345   (setq riece-server nil
346         riece-current-channels nil
347         riece-current-channel nil
348         riece-channel-buffer nil
349         riece-channel-buffer-alist nil
350         riece-user-indicator nil
351         riece-long-channel-indicator "None"
352         riece-channel-list-indicator "No channel"
353         riece-away-indicator "-"
354         riece-operator-indicator "-"
355         riece-freeze-indicator "-")
356   (delete-other-windows)
357   (run-hooks 'riece-exit-hook))
358
359 (defun riece-command-mode ()
360   "Major mode for Riece.  Normal edit function are available.
361 Typing Return or Linefeed enters the current line in the dialogue.
362 The following special commands are available:
363 For a list of the generic commands type \\[riece-command-generic] ? RET.
364 \\{riece-command-mode-map}"
365   (interactive)
366   (kill-all-local-variables)
367
368   (setq riece-away-indicator "-"
369         riece-operator-indicator "-"
370         major-mode 'riece-command-mode
371         mode-name "Command"
372         mode-line-buffer-identification
373         (riece-mode-line-buffer-identification
374          '("Riece: "
375            riece-away-indicator
376            riece-operator-indicator
377            " "
378            riece-user-indicator
379            " "
380            riece-channel-indicator)))
381   (riece-simplify-mode-line-format)
382   (use-local-map riece-command-mode-map)
383
384   (unless riece-command-mode-syntax-table
385     (setq riece-command-mode-syntax-table
386           (copy-syntax-table (syntax-table)))
387     (set-syntax-table riece-command-mode-syntax-table)
388     (mapcar
389      (lambda (c) (modify-syntax-entry c "w"))
390      "^[]{}'`"))
391
392   (run-hooks 'riece-command-mode-hook))
393
394 (defun riece-dialogue-mode ()
395   "Major mode for displaying the IRC dialogue.
396 All normal editing commands are turned off.
397 Instead, these commands are available:
398 \\{riece-dialogue-mode-map}"
399   (kill-all-local-variables)
400   (make-local-variable 'riece-freeze)
401   (make-local-variable 'riece-freeze-indicator)
402   (setq riece-freeze riece-default-freeze
403         riece-away-indicator "-"
404         riece-operator-indicator "-"
405         major-mode 'riece-dialogue-mode
406         mode-name "Dialogue"
407         mode-line-buffer-identification
408         (riece-mode-line-buffer-identification
409          '("Riece: "
410            riece-away-indicator
411            riece-operator-indicator
412            riece-freeze-indicator
413            " "
414            riece-channel-list-indicator " "))
415         buffer-read-only t)
416   (riece-simplify-mode-line-format)
417   (use-local-map riece-dialogue-mode-map)
418   (buffer-disable-undo)
419   (run-hooks 'riece-dialogue-mode-hook))
420
421 (define-derived-mode riece-others-mode riece-dialogue-mode
422   "Others"
423   "Major mode for displaying the IRC others message except current channel.
424 All normal editing commands are turned off.
425 Instead, these commands are available:
426 \\{riece-others-mode-map}")
427
428 (define-derived-mode riece-channel-mode riece-dialogue-mode
429   "Channel"
430   "Major mode for displaying the IRC current channel buffer.
431 All normal editing commands are turned off.
432 Instead, these commands are available:
433 \\{riece-channel-mode-map}"
434   (make-local-variable 'riece-channel-buffer-window-point)
435   (setq mode-line-buffer-identification
436         (riece-mode-line-buffer-identification
437          '("Riece: "
438            riece-away-indicator
439            riece-operator-indicator
440            riece-freeze-indicator
441            " "
442            riece-long-channel-indicator))))
443
444 (defun riece-channel-list-mode ()
445   "Major mode for displaying channel list.
446 All normal editing commands are turned off."
447   (kill-all-local-variables)
448   (buffer-disable-undo)
449   (setq major-mode 'riece-channel-list-mode
450         mode-name "Channels"
451         mode-line-buffer-identification
452         (riece-mode-line-buffer-identification '("Riece: "))
453         truncate-lines t
454         buffer-read-only t)
455   (make-local-hook 'riece-update-buffer-functions)
456   (add-hook 'riece-update-buffer-functions
457             'riece-update-channel-list-buffer nil t)
458   (use-local-map riece-channel-list-mode-map)
459   (run-hooks 'riece-channel-list-mode-hook))
460
461 (defun riece-user-list-mode ()
462   "Major mode for displaying members in the IRC current channel buffer.
463 All normal editing commands are turned off.
464 Instead, these commands are available:
465 \\{riece-user-list-mode-map}"
466   (kill-all-local-variables)
467   (buffer-disable-undo)
468   (setq major-mode 'riece-user-list-mode
469         mode-name "Users"
470         mode-line-buffer-identification
471         (riece-mode-line-buffer-identification
472          '("Riece: " riece-long-channel-indicator " "))
473         truncate-lines t
474         buffer-read-only t)
475   (if (boundp 'transient-mark-mode)
476       (set (make-local-variable 'transient-mark-mode) t))
477   (make-local-hook 'riece-update-buffer-functions)
478   (add-hook 'riece-update-buffer-functions
479             'riece-update-user-list-buffer nil t)
480   (use-local-map riece-user-list-mode-map)
481   (run-hooks 'riece-user-list-mode-hook))
482
483 (defun riece-create-buffers ()
484   (let ((alist riece-buffer-alist))
485     (while alist
486       (save-excursion
487         (set-buffer (apply #'riece-get-buffer-create
488                            (cdr (car alist))))
489         (set (car (car alist)) (current-buffer))
490         (unless (or (null (nth 2 (car alist)))
491                     (eq major-mode (nth 2 (car alist))))
492           (funcall (nth 2 (car alist))))
493         (setq alist (cdr alist))))))
494
495 (provide 'riece)
496
497 ;;; riece.el ends here