1 ;;; riece.el --- IRC client for Emacsen
2 ;; Copyright (C) 1998-2003 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: IRC, riece
8 ;; This file is part of Riece.
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)
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.
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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 (require 'riece-display)
28 (require 'riece-server)
29 (require 'riece-compat)
30 (require 'riece-commands)
31 (require 'riece-addon)
32 (require 'riece-signal)
34 (autoload 'derived-mode-class "derived")
36 (defvar riece-channel-list-mode-map (make-sparse-keymap))
37 (defvar riece-user-list-mode-map (make-sparse-keymap))
39 (defvar riece-dialogue-mode-map
40 (let ((keymap (make-keymap)))
41 (suppress-keymap keymap 'nodigit)
44 (defvar riece-command-mode-map (make-keymap))
45 (defvar riece-command-map (make-sparse-keymap))
47 (defvar riece-command-mode-syntax-table nil)
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)
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 (riece-temp-buffer " *Temp*")
63 (riece-debug-buffer "*Debug*")))
65 (defvar riece-select-keys
66 `("#" riece-command-switch-to-channel-by-number
67 "1" riece-command-switch-to-channel-by-number-1
68 "2" riece-command-switch-to-channel-by-number-2
69 "3" riece-command-switch-to-channel-by-number-3
70 "4" riece-command-switch-to-channel-by-number-4
71 "5" riece-command-switch-to-channel-by-number-5
72 "6" riece-command-switch-to-channel-by-number-6
73 "7" riece-command-switch-to-channel-by-number-7
74 "8" riece-command-switch-to-channel-by-number-8
75 "9" riece-command-switch-to-channel-by-number-9
76 "0" riece-command-switch-to-channel-by-number-10
77 ,(concat riece-command-prefix "1")
78 riece-command-switch-to-channel-by-number-11
79 ,(concat riece-command-prefix "2")
80 riece-command-switch-to-channel-by-number-12
81 ,(concat riece-command-prefix "3")
82 riece-command-switch-to-channel-by-number-13
83 ,(concat riece-command-prefix "4")
84 riece-command-switch-to-channel-by-number-14
85 ,(concat riece-command-prefix "5")
86 riece-command-switch-to-channel-by-number-15
87 ,(concat riece-command-prefix "6")
88 riece-command-switch-to-channel-by-number-16
89 ,(concat riece-command-prefix "7")
90 riece-command-switch-to-channel-by-number-17
91 ,(concat riece-command-prefix "8")
92 riece-command-switch-to-channel-by-number-18
93 ,(concat riece-command-prefix "9")
94 riece-command-switch-to-channel-by-number-19
95 ,(concat riece-command-prefix "0")
96 riece-command-switch-to-channel-by-number-20))
98 ;;; Keymap macros. -- borrowed from `gnus-util.el'.
99 (defmacro riece-local-set-keys (&rest plist)
100 "Set the keys in PLIST in the current keymap."
101 `(riece-define-keys-1 (current-local-map) ',plist))
103 (defmacro riece-define-keys (keymap &rest plist)
104 "Assign KEYMAP keys from PLIST."
105 `(riece-define-keys-1 ',keymap ',plist))
107 (defmacro riece-define-keys-safe (keymap &rest plist)
108 "Assign KEYMAP keys from PLIST without overwriting previous definitions."
109 `(riece-define-keys-1 ',keymap ',plist t))
111 (put 'riece-define-keys 'lisp-indent-function 1)
112 (put 'riece-define-keys-safe 'lisp-indent-function 1)
113 (put 'riece-local-set-keys 'lisp-indent-function 1)
115 (defun riece-define-keys-1 (keymap plist &optional safe)
116 "Assign KEYMAP keys from PLIST.
117 If optional argument SAFE is nil, overwrite previous definitions."
119 (error "Can't set keys in a null keymap"))
122 (setq keymap (symbol-value keymap)))
125 (set (car keymap) nil)
126 (define-prefix-command (car keymap))
127 (define-key (symbol-value (nth 2 keymap))
128 (if (symbolp (nth 1 keymap))
129 (symbol-value (nth 1 keymap))
132 (setq keymap (symbol-value (car keymap)))))
135 (if (symbolp (setq key (car plist)))
136 (setq key (symbol-value key)))
137 (setq plist (cdr plist))
139 (eq (lookup-key keymap key) 'undefined))
140 (define-key keymap key (car plist))
142 (setq plist (cdr plist)))))
145 (riece-define-keys riece-dialogue-mode-map
148 [backspace] scroll-down
151 [home] beginning-of-buffer
154 "/" riece-command-raw
156 "<" beginning-of-buffer
157 "^" riece-command-list-addons
158 "\C-ta" riece-command-toggle-away
159 "c" riece-command-select-command-buffer
160 "f" riece-command-finger
161 "\C-tf" riece-command-toggle-freeze
162 "\C-to" riece-command-toggle-own-freeze
163 "\C-tO" riece-command-toggle-others-buffer-mode
164 "\C-tu" riece-command-toggle-user-list-buffer-mode
165 "\C-tc" riece-command-toggle-channel-buffer-mode
166 "\C-tC" riece-command-toggle-channel-list-buffer-mode
167 "\C-tl" riece-command-change-layout
168 "i" riece-command-invite
169 "j" riece-command-join
170 "\C-k" riece-command-kick
171 "l" riece-command-list
172 "M" riece-command-change-mode
173 "n" riece-command-change-nickname
174 "N" riece-command-names
176 "O" riece-command-open-server
177 "C" riece-command-close-server
178 "M" riece-command-universal-server-name-argument
179 "p" riece-command-enter-message-to-user
180 "q" riece-command-quit
181 "r" riece-command-configure-windows
182 "s" riece-command-save-variables
183 "x" riece-command-copy-region
184 "t" riece-command-topic
185 "w" riece-command-who
186 "z" riece-command-suspend-resume)
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)
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
213 (set-keymap-parent riece-command-map riece-dialogue-mode-map)
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)
225 (riece-define-keys riece-channel-list-mode-map
226 ">" riece-command-next-channel
227 "<" riece-command-previous-channel
229 "c" riece-command-select-command-buffer)
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))
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)))
244 (load (expand-file-name (car files)))
246 (setq files (cdr files)))))
248 (defvar print-quoted)
249 (defvar print-escape-multibyte)
250 (defun riece-save-variables-files ()
251 "Save current settings to `riece-saved-variables-file'."
252 (with-temp-file riece-saved-variables-file
253 (let ((print-quoted t)
255 print-escape-multibyte
258 (variables riece-saved-forms))
260 (prin1 `(setq ,(car variables)
261 ',(symbol-value (car variables)))
264 (setq variables (cdr variables)))))
265 (setq riece-save-variables-are-dirty nil))
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."
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 (modify-frame-parameters (selected-frame)
278 (list (cons 'riece-window-configuration
279 (current-window-configuration))))
280 (setq riece-addon-dependencies (riece-resolve-addons
281 (copy-sequence riece-addons)))
282 (let ((pointer riece-addon-dependencies))
284 (riece-insinuate-addon (car (car pointer)) riece-debug)
285 (setq pointer (cdr pointer))))
286 (if (or confirm (null riece-server))
287 (setq riece-server (completing-read "Server: " riece-server-alist)))
288 (if (stringp riece-server)
289 (setq riece-server (riece-server-name-to-server riece-server)))
290 (riece-create-buffers)
291 (switch-to-buffer riece-command-buffer)
292 (riece-display-connect-signals)
293 (riece-redisplay-buffers)
294 (riece-open-server riece-server "")
295 ;; If no server process is available, exit.
296 (if (null riece-server-process-alist)
298 (let ((server-list riece-startup-server-list))
300 (riece-command-open-server (car server-list))
301 (setq server-list (cdr server-list))))
302 (let ((channel-list riece-startup-channel-list)
305 (setq server (riece-identity-server
306 (riece-parse-identity (car channel-list))))
307 (unless (riece-server-opened server)
308 (riece-command-open-server server))
309 (setq channel-list (cdr channel-list))))
310 (let ((pointer riece-addon-dependencies))
312 (unless (get (car (car pointer)) 'riece-addon-default-disabled)
313 (riece-enable-addon (car (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")))))
320 (if riece-save-variables-are-dirty
321 (riece-save-variables-files))
322 (while riece-buffer-list
323 (if (and (get-buffer (car riece-buffer-list))
324 (buffer-live-p (car riece-buffer-list)))
325 (funcall riece-buffer-dispose-function (car riece-buffer-list)))
326 (setq riece-buffer-list (cdr riece-buffer-list)))
327 (riece-clear-signal-slots)
328 (setq riece-server nil
329 riece-current-channels nil
330 riece-current-channel nil
331 riece-channel-buffer nil
332 riece-channel-buffer-alist nil
333 riece-user-indicator nil
334 riece-long-channel-indicator "None"
335 riece-channel-list-indicator "No channel"
336 riece-away-indicator "-"
337 riece-operator-indicator "-"
338 riece-channel-status-indicator "-"
339 riece-freeze-indicator "-")
340 (modify-frame-parameters (selected-frame)
341 (list (list 'riece-window-configuration)))
342 (delete-other-windows)
343 (run-hooks 'riece-exit-hook))
345 (defun riece-command-mode ()
346 "Major mode for Riece. Normal edit function are available.
347 Typing Return or Linefeed enters the current line in the dialogue.
348 The following special commands are available:
349 For a list of the generic commands type \\[riece-command-generic] ? RET.
350 \\{riece-command-mode-map}"
352 (kill-all-local-variables)
354 ;; Make `truncate-partial-width-windows' buffer local and set it to
355 ;; nil. This causes `truncate-lines' to directly control line
357 (make-local-variable 'truncate-partial-width-windows)
358 (setq truncate-partial-width-windows nil)
360 (make-local-variable 'riece-mode-line-buffer-identification)
361 (setq riece-away-indicator "-"
362 riece-operator-indicator "-"
363 riece-channel-status-indicator "-"
364 major-mode 'riece-command-mode
366 riece-mode-line-buffer-identification
369 riece-operator-indicator
370 riece-channel-status-indicator
374 riece-channel-indicator)
375 mode-line-buffer-identification
376 (riece-mode-line-buffer-identification
377 riece-mode-line-buffer-identification)
379 (riece-simplify-mode-line-format)
380 (use-local-map riece-command-mode-map)
382 (unless riece-command-mode-syntax-table
383 (setq riece-command-mode-syntax-table
384 (copy-syntax-table (syntax-table)))
385 (set-syntax-table riece-command-mode-syntax-table)
387 (lambda (c) (modify-syntax-entry c "w"))
390 (run-hooks 'riece-command-mode-hook))
392 (defun riece-dialogue-mode ()
393 "Major mode for displaying the IRC dialogue.
394 All normal editing commands are turned off.
395 Instead, these commands are available:
396 \\{riece-dialogue-mode-map}"
397 (kill-all-local-variables)
398 (make-local-variable 'riece-freeze)
399 (make-local-variable 'riece-freeze-indicator)
401 ;; Make `truncate-partial-width-windows' buffer local and set it to
402 ;; nil. This causes `truncate-lines' to directly control line truncation.
403 (make-local-variable 'truncate-partial-width-windows)
404 (setq truncate-partial-width-windows nil)
406 (make-local-variable 'riece-mode-line-buffer-identification)
407 (setq riece-freeze riece-default-freeze
408 riece-away-indicator "-"
409 riece-operator-indicator "-"
410 riece-channel-status-indicator "-"
411 major-mode 'riece-dialogue-mode
413 riece-mode-line-buffer-identification
416 riece-operator-indicator
417 riece-freeze-indicator
418 riece-channel-status-indicator
420 riece-channel-list-indicator " ")
421 mode-line-buffer-identification
422 (riece-mode-line-buffer-identification
423 riece-mode-line-buffer-identification)
426 (riece-simplify-mode-line-format)
427 (use-local-map riece-dialogue-mode-map)
428 (buffer-disable-undo)
429 (run-hooks 'riece-dialogue-mode-hook))
431 (define-derived-mode riece-others-mode riece-dialogue-mode
433 "Major mode for displaying the IRC others message except current channel.
434 All normal editing commands are turned off.
435 Instead, these commands are available:
436 \\{riece-others-mode-map}")
438 (define-derived-mode riece-channel-mode riece-dialogue-mode
440 "Major mode for displaying the IRC current channel buffer.
441 All normal editing commands are turned off.
442 Instead, these commands are available:
443 \\{riece-channel-mode-map}"
444 (make-local-variable 'riece-channel-buffer-window-point)
445 (make-local-variable 'riece-mode-line-buffer-identification)
446 (setq riece-mode-line-buffer-identification
449 riece-operator-indicator
450 riece-freeze-indicator
451 riece-channel-status-indicator
453 riece-long-channel-indicator)
454 mode-line-buffer-identification
455 (riece-mode-line-buffer-identification
456 riece-mode-line-buffer-identification)))
458 (defun riece-channel-list-mode ()
459 "Major mode for displaying channel list.
460 All normal editing commands are turned off."
461 (kill-all-local-variables)
462 (buffer-disable-undo)
464 ;; Make `truncate-partial-width-windows' buffer local and set it to
465 ;; nil. This causes `truncate-lines' to directly control line truncation.
466 (make-local-variable 'truncate-partial-width-windows)
467 (setq truncate-partial-width-windows nil)
469 (make-local-variable 'riece-mode-line-buffer-identification)
470 (setq major-mode 'riece-channel-list-mode
472 riece-mode-line-buffer-identification '("Riece: ")
473 mode-line-buffer-identification
474 (riece-mode-line-buffer-identification
475 riece-mode-line-buffer-identification)
478 (make-local-hook 'riece-update-buffer-functions)
479 (add-hook 'riece-update-buffer-functions
480 'riece-update-channel-list-buffer nil t)
481 (use-local-map riece-channel-list-mode-map)
482 (run-hooks 'riece-channel-list-mode-hook))
484 (defun riece-user-list-mode ()
485 "Major mode for displaying members in the IRC current channel buffer.
486 All normal editing commands are turned off.
487 Instead, these commands are available:
488 \\{riece-user-list-mode-map}"
489 (kill-all-local-variables)
490 (buffer-disable-undo)
492 ;; Make `truncate-partial-width-windows' buffer local and set it to
493 ;; nil. This causes `truncate-lines' to directly control line truncation.
494 (make-local-variable 'truncate-partial-width-windows)
495 (setq truncate-partial-width-windows nil)
497 (make-local-variable 'riece-mode-line-buffer-identification)
498 (setq major-mode 'riece-user-list-mode
500 riece-mode-line-buffer-identification
501 '("Riece: " riece-long-channel-indicator " ")
502 mode-line-buffer-identification
503 (riece-mode-line-buffer-identification
504 riece-mode-line-buffer-identification)
507 (if (boundp 'transient-mark-mode)
508 (set (make-local-variable 'transient-mark-mode) t))
509 (make-local-hook 'riece-update-buffer-functions)
510 (add-hook 'riece-update-buffer-functions
511 'riece-update-user-list-buffer nil t)
512 (use-local-map riece-user-list-mode-map)
513 (run-hooks 'riece-user-list-mode-hook))
515 (defun riece-create-buffers ()
516 (let ((alist riece-buffer-alist))
519 (set-buffer (apply #'riece-get-buffer-create
521 (set (car (car alist)) (current-buffer))
522 (unless (or (null (nth 2 (car alist)))
523 (eq major-mode (nth 2 (car alist))))
524 (funcall (nth 2 (car alist))))
525 (setq alist (cdr alist))))))
527 (defvar reporter-prompt-for-summary-p)
528 (defun riece-submit-bug-report (&optional recent-messages recent-keys)
529 "Submit via mail a bug report on Riece."
530 ;; This strange form ensures that (recent-keys) is the value before
531 ;; the bug subject string is read.
532 (interactive (list (riece-recent-messages 20) (recent-keys)))
533 (message "Querying server version...")
534 (let ((pointer riece-server-process-alist)
537 (when (riece-server-process-opened (cdr (car pointer)))
538 (process-send-string (cdr (car pointer)) "VERSION\r\n")
540 (with-current-buffer (process-buffer (cdr (car pointer)))
541 riece-real-nickname))
544 (format "PRIVMSG %s :\1VERSION\1\r\n" nickname))))
545 (setq pointer (cdr pointer))))
547 (message "Querying server version...done")
549 (let ((reporter-prompt-for-summary-p t))
551 (error "Please turn on riece-debug and restart Riece."))
552 (reporter-submit-bug-report
558 "This bug report will be sent to the Riece Development Team,
559 not to your local site managers!!
561 Please write in Japanese or English, because the Riece maintainers do
562 not have translators to read other languages for them.
564 Please describe as succinctly as possible:
566 \t- What you thought should have happened.
567 \t- Precisely what you were doing at the time.
569 Also include a reliable recipe for triggering the bug, as well as
570 any lisp back-traces that you may have.
571 \(setq stack-trace-on-error t\), or \(setq debug-on-error t\) if you
572 are familiar with the debugger, to get a lisp back-trace.")
573 (delete-other-windows)
575 (goto-char (point-max))
579 (save-window-excursion
581 (riece-command-list-addons)
582 (search-forward "\n\n")
583 (buffer-substring (point-min) (point)))))
584 (insert "Recent messages from servers:\n"
585 "--------------------------")
586 (let ((pointer riece-server-process-alist))
588 (insert "\n- \"" (car (car pointer)) "\", \n"
589 (format "%S" (if (equal (car (car pointer)) "")
591 (cdr (assoc (car (car pointer))
592 riece-server-alist))))
594 (if (riece-server-process-opened (cdr (car pointer)))
596 (set-buffer (process-buffer (cdr (car pointer))))
597 (goto-char (point-max))
598 (beginning-of-line -60)
599 (buffer-substring (point) (point-max)))
601 (setq pointer (cdr pointer))))
602 (insert "\n\nRecent debug messages:\n"
603 "-----------------------------------\n"
604 (with-current-buffer riece-debug-buffer
606 ;; Insert recent keystrokes.
607 (insert "\n\nRecent keystrokes:\n"
608 "-----------------\n\n")
609 (let ((before-keys (point)))
610 (insert (key-description recent-keys))
612 (narrow-to-region before-keys (point))
613 (goto-char before-keys)
614 (while (progn (move-to-column 50) (not (eobp)))
615 (search-forward " " nil t)
617 ;; Insert recent minibuffer messages.
618 (insert "\n\nRecent messages (most recent first):\n"
619 "-----------------------------------\n"
624 ;;; riece.el ends here