1 ;;; liece.el --- IRC client for Emacsen
2 ;; Copyright (C) 1998-2000 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: IRC, liece
9 ;; This file is part of Liece.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
32 (require 'liece-inlines)
33 (require 'liece-handle)
34 (require 'liece-filter)
35 (require 'liece-hilit)
38 (require 'liece-window)
40 (if (featurep 'xemacs)
41 (require 'liece-xemacs)
42 (require 'liece-emacs))
43 (require 'liece-commands)
45 (autoload 'mule-caesar-region "mule-caesar" nil t)
46 (autoload 'liece-command-browse-url "liece-url" nil t)
47 (autoload 'liece-command-dcc-send "liece-dcc" nil t)
48 (autoload 'liece-command-dcc-receive "liece-dcc" nil t)
49 (autoload 'liece-command-dcc-list "liece-dcc" nil t)
50 (autoload 'liece-command-dcc-chat-listen "liece-dcc" nil t)
51 (autoload 'liece-command-dcc-chat-connect "liece-dcc" nil t)
52 (autoload 'liece-command-dcc-accept "liece-dcc" nil t)
53 (autoload 'liece-command-mail-compose "liece-mail" nil t)
54 (autoload 'liece-command-submit-bug-report "liece-mail" nil t)
57 (defvar liece-server-keyword-map
58 '((:host (getenv "IRCSERVER"))
59 (:service liece-service)
60 (:password liece-password)
63 (:type liece-tcp-connection-type)
65 "Mapping from keywords to default values.
66 All keywords that can be used must be listed here."))
68 (defadvice save-buffers-kill-emacs
69 (before liece-save-buffers-kill-emacs activate)
70 "Prompt user to quit IRC explicitly."
71 (run-hooks 'liece-before-kill-emacs-hook) )
73 (add-hook 'liece-before-kill-emacs-hook 'liece-command-quit)
75 (defvar liece-tmp-server-name nil "Temporaly server name.")
76 (defvar liece-buffer-last-check-time nil)
77 (defvar liece-timers-list-initialized-p nil
78 "Are liece internal timers in place?")
80 (defconst liece-obarray-size 1327
81 "The size of obarray used by liece on channelname and username space.
82 For efficiency this should be prime. See documentation of intern and
83 `make-vector' for more information. Here is a list of some small primes...
85 13, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521,
86 631, 761, 919, 1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839,
87 7013, 8419, 10103, 12143, 14591, 17519, 21023, 25229, 30293, 36353,
88 43627, 52361, 62851, 75431, 90523, 108631, 130363, 156437, 187751,
89 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403, 968897,
90 1162687, 1395263, 1674319, 2009191, 2411033, 2893249.")
92 (defvar liece-channel-list-mode-map (make-sparse-keymap))
93 (defvar liece-nick-mode-map (make-sparse-keymap))
94 (defvar liece-client-query-map (make-sparse-keymap))
95 (defvar liece-dcc-map (make-sparse-keymap))
96 (defvar liece-friends-map (make-sparse-keymap))
98 (defvar liece-dialogue-mode-map
99 (let ((keymap (make-keymap)))
100 (suppress-keymap keymap 'nodigit)
103 (defvar liece-command-mode-map (make-keymap))
104 (defvar liece-command-map (make-sparse-keymap))
106 (defvar liece-command-mode-syntax-table nil)
108 (put 'liece-command-mode 'mode-class 'special)
109 (put 'liece-dialogue-mode 'mode-class 'special)
110 (put 'liece-channel-list-mode 'mode-class 'special)
111 (put 'liece-nick-mode 'mode-class 'special)
112 (put 'liece-channel-mode 'derived-mode-parent 'liece-dialogue-mode)
113 (put 'liece-others-mode 'derived-mode-parent 'liece-dialogue-mode)
115 (defvar liece-buffer-mode-alist
116 '((liece-dialogue-buffer liece-dialogue-mode)
117 (liece-others-buffer liece-others-mode)
118 (liece-channel-list-buffer liece-channel-list-mode)
119 (liece-private-buffer liece-dialogue-mode)
121 (liece-IGNORED-buffer)
122 (liece-WALLOPS-buffer)))
126 (fset (intern (format "liece-switch-to-channel-no-%d" (1+ n)))
129 (funcall #'liece-switch-to-channel-no ,n)))))
131 (defvar liece-select-keys
132 '("1" liece-switch-to-channel-no-1
133 "2" liece-switch-to-channel-no-2
134 "3" liece-switch-to-channel-no-3
135 "4" liece-switch-to-channel-no-4
136 "5" liece-switch-to-channel-no-5
137 "6" liece-switch-to-channel-no-6
138 "7" liece-switch-to-channel-no-7
139 "8" liece-switch-to-channel-no-8
140 "9" liece-switch-to-channel-no-9
141 "0" liece-switch-to-channel-no-10
142 "\C-c1" liece-switch-to-channel-no-11
143 "\C-c2" liece-switch-to-channel-no-12
144 "\C-c3" liece-switch-to-channel-no-13
145 "\C-c4" liece-switch-to-channel-no-14
146 "\C-c5" liece-switch-to-channel-no-15
147 "\C-c6" liece-switch-to-channel-no-16
148 "\C-c7" liece-switch-to-channel-no-17
149 "\C-c8" liece-switch-to-channel-no-18
150 "\C-c9" liece-switch-to-channel-no-19
151 "\C-c0" liece-switch-to-channel-no-20))
153 ;;; Keymap macros. -- borrowd from `gnus-util.el'.
155 (defmacro liece-local-set-keys (&rest plist)
156 "Set the keys in PLIST in the current keymap."
157 `(liece-define-keys-1 (current-local-map) ',plist))
159 (defmacro liece-define-keys (keymap &rest plist)
160 "Assign KEYMAP keys from PLIST."
161 `(liece-define-keys-1 ',keymap ',plist))
163 (defmacro liece-define-keys-safe (keymap &rest plist)
164 "Assign KEYMAP keys from PLIST without overwriting previous definitions."
165 `(liece-define-keys-1 ',keymap ',plist t))
167 (put 'liece-define-keys 'lisp-indent-function 1)
168 (put 'liece-define-keys-safe 'lisp-indent-function 1)
169 (put 'liece-local-set-keys 'lisp-indent-function 1)
171 (defun liece-define-keys-1 (keymap plist &optional safe)
172 "Assign KEYMAP keys from PLIST.
173 If optional argument SAFE is nil, overwrite previous definitions."
175 (error "Can't set keys in a null keymap"))
178 (setq keymap (symbol-value keymap)))
181 (set (car keymap) nil)
182 (define-prefix-command (car keymap))
183 (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
184 (setq keymap (symbol-value (car keymap)))))
187 (when (symbolp (setq key (pop plist)))
188 (setq key (symbol-value key)))
190 (eq (lookup-key keymap key) 'undefined))
191 (define-key keymap key (pop plist))
195 (liece-define-keys liece-dialogue-mode-map
198 [backspace] scroll-down
202 "/" liece-command-generic
204 "<" beginning-of-buffer
205 "!" liece-command-exec
206 "|" liece-command-show-last-kill
207 "a" liece-command-away
208 "b" liece-command-submit-bug-report
209 "B" liece-dialogue-beep
210 "c" liece-command-point-back-to-command-buffer
211 "f" liece-command-finger
212 "F" liece-dialogue-freeze
213 "O" liece-dialogue-own-freeze
214 "i" liece-command-invite
215 "j" liece-command-join
216 "k" liece-command-kill
217 "\C-k" liece-command-kick
218 "l" liece-command-list
219 "L" liece-command-load-vars
220 "S" liece-command-save-vars
221 "m" liece-dialogue-enter-message
222 "M" liece-command-modec
223 "n" liece-command-nickname
225 "p" liece-command-mta-private
226 "P" liece-command-toggle-private
227 "q" liece-command-quit
228 "r" liece-command-reconfigure-windows
229 "x" liece-command-tag-region
230 "t" liece-command-topic
231 "T" liece-command-timestamp
232 "\C-t" liece-command-find-timestamp
233 "u" liece-command-lusers
234 "U" liece-command-userhost
235 "v" liece-command-browse-url
236 "w" liece-command-who)
238 (liece-define-keys (liece-client-query-map "\C-c" liece-dialogue-mode-map)
239 "a" liece-command-ctcp-action
240 "v" liece-command-ctcp-version
241 "u" liece-command-ctcp-userinfo
242 "h" liece-command-ctcp-help
243 "c" liece-command-ctcp-clientinfo
244 "g" liece-command-ctcp-generic
245 "p" liece-command-ctcp-ping
246 "t" liece-command-ctcp-time
247 "x" liece-command-ctcp-x-face
248 "X" liece-command-ctcp-x-face-from-xbm-file
249 "U" liece-command-ctcp-userinfo-from-minibuffer)
251 (liece-define-keys (liece-dcc-map "\C-d" liece-dialogue-mode-map)
252 "s" liece-command-dcc-send
253 "r" liece-command-dcc-receive
254 "l" liece-command-dcc-list
255 "cl" liece-command-dcc-chat-listen
256 "cc" liece-command-dcc-chat-connect
257 "g" liece-command-dcc-accept)
259 (liece-define-keys (liece-friends-map "\C-i" liece-dialogue-mode-map)
260 " " liece-command-ison
261 "a" liece-command-activate-friends
262 "d" liece-command-deactivate-friends
263 "s" liece-command-display-friends)
265 (liece-define-keys liece-command-mode-map
266 "\r" liece-command-enter-message
267 [tab] liece-command-complete
268 [(meta control c) >] liece-command-push
269 [(meta control c) <] liece-command-pop
270 [(meta control c) o] liece-command-mode+o
271 [(meta control c) O] liece-command-mode-o
272 [(meta control c) v] liece-command-mode+v
273 [(meta control c) V] liece-command-mode-v)
275 (liece-define-keys (liece-command-map "\C-c" liece-command-mode-map)
276 "\177" liece-command-scroll-down
277 [delete] liece-command-scroll-down
278 [backspace] liece-command-scroll-down
279 " " liece-command-scroll-up
280 "$" liece-command-end-of-buffer
281 ">" liece-command-next-channel
282 "<" liece-command-previous-channel
283 "a" liece-command-away
284 "c" liece-command-inline
285 "\C-a" liece-command-previous-channel
286 "\C-f" liece-command-freeze
287 "\C-j" liece-command-next-channel
288 "\C-n" liece-command-names
289 "\C-u" liece-command-unread-channel
290 "l" liece-command-list
291 "L" liece-command-load-vars
292 "M" liece-command-own-freeze
293 "\C-m" liece-command-modec
294 "o" liece-command-mode+o
295 "O" liece-command-toggle-nick-buffer-mode
296 "\C-o" liece-command-toggle-channel-buffer-mode
297 "\C-p" liece-command-part
298 "r" liece-command-reconfigure-windows
299 "\C-r" mule-caesar-region
300 "s" liece-command-set-window-style
301 "S" liece-command-save-vars
302 "v" liece-command-mode+v
303 "\C-v" liece-command-browse-url
304 "\C-y" liece-command-yank-send)
305 (set-keymap-parent liece-command-map liece-dialogue-mode-map)
307 (liece-define-keys liece-nick-mode-map
308 "o" liece-command-mode+o
309 "O" liece-command-mode-o
310 "v" liece-command-mode+v
311 "V" liece-command-mode-v
312 "f" liece-command-finger
313 " " liece-command-nick-scroll-up
314 "\177" liece-command-nick-scroll-down
315 [delete] liece-command-nick-scroll-down
316 [backspace] liece-command-nick-scroll-down
317 "m" liece-command-mail-compose
318 "c" liece-command-point-back-to-command-buffer)
320 (liece-define-keys liece-channel-list-mode-map
321 ">" liece-command-next-channel
322 "<" liece-command-previous-channel
323 "u" liece-command-unread-channel
325 "c" liece-command-point-back-to-command-buffer)
327 (liece-define-keys-1 liece-dialogue-mode-map liece-select-keys)
328 (liece-define-keys-1 liece-channel-list-mode-map liece-select-keys))
331 (defmacro liece-server-opened ()
332 "Return server process status.
333 Return non-nil if stream is opened."
334 '(and liece-server-process
335 (memq (process-status liece-server-process)
338 (defun liece-start-server (&optional confirm)
339 "Open network stream to remote irc server.
340 If optional argument CONFIRM is non-nil, ask the host that the server
343 (null (or liece-server
344 (setq liece-server (getenv "IRCSERVER")))))
345 (setq liece-server (completing-read (_ "IRC server: ") liece-server-alist)))
346 (unless (listp liece-server)
347 (let ((entry (assoc liece-server liece-server-alist)))
349 (if (listp (cdr entry))
350 (setq liece-server (cdr entry))
351 (setq liece-server (liece-server-parse-string (cdr entry))))
352 (let ((plist (liece-server-parse-string liece-server)))
353 (set-alist 'liece-server-alist liece-server plist)
354 (setq liece-save-variables-are-dirty t)
355 (setq liece-server plist)))))
356 (when (or (and confirm liece-ask-for-nickname)
357 (null liece-nickname))
358 (setq liece-nickname (read-string (_ "Enter your nickname: ") liece-nickname)))
359 (let ((host (liece-server-host)))
361 (_ "Connecting to IRC server on %s...") host)
362 (liece-open-server liece-server liece-service)))
364 (defun liece-close-server-internal ()
365 "Close connection to chat server."
366 (if (liece-server-opened)
367 (delete-process liece-server-process))
368 (if liece-server-buffer
369 (kill-buffer liece-server-buffer))
370 (setq liece-server-buffer nil
371 liece-server-process nil
375 (defun liece-close-server (&optional quit-string)
379 ;; Unset default sentinel function before closing connection.
380 (when (and liece-server-process
382 (process-sentinel liece-server-process)))
383 (set-process-sentinel liece-server-process nil))
384 ;; We cannot send QUIT command unless the process is running.
385 (when (liece-server-opened)
387 (liece-send "QUIT :%s" quit-string)
388 (liece-send "QUIT"))))
389 (liece-close-server-internal)))
391 (defmacro liece-server-keyword-bind (plist &rest body)
392 "Return a `let' form that binds all variables in PLIST.
393 After this is done, BODY will be executed in the scope
396 The variables bound and their default values are described by
397 the `liece-server-keyword-map' variable."
400 (list (intern (substring (symbol-name (car keyword)) 1))
402 `(or (plist-get ,plist ',(car keyword))
404 `(plist-get ,plist ',(car keyword)))))
405 liece-server-keyword-map)
408 (put 'liece-server-keyword-bind 'lisp-indent-function 1)
409 (put 'liece-server-keyword-bind 'edebug-form-spec '(form body))
411 (defun liece-server-parse-string (string)
412 "Convert a STRING set as `liece-server' and return a property list."
413 (when (or (string-match "^\\[\\([^]]+\\)\\]:?\\([0-9]*\\)" string)
414 (string-match "^\\([^:]+\\):?\\([0-9]*\\)" string))
415 (let ((host (match-string 1 string))
416 (service (match-string 2 string))
417 (password (substring string (match-end 0)))
419 (push `(:host ,host) plist)
420 (unless (string= service "")
421 (push `(:service ,(string-to-int service)) plist))
423 ((string= password ":")
424 (setq liece-ask-for-password t))
425 ((string= password ""))
426 (t (push `(:password ,(substring password 1)) plist)))
427 (apply #'nconc plist))))
429 (defun liece-open-server (host &optional service)
430 "Open chat server on HOST.
431 If HOST is nil, use value of environment variable \"IRCSERVER\".
432 If optional argument SERVICE is non-nil, open by the service name."
433 (liece-server-keyword-bind host
435 (if (fboundp prescript)
437 (call-process shell-file-name nil nil nil
438 shell-command-switch prescript))
439 (when prescript-delay
440 (sleep-for prescript-delay)))
442 (setq liece-ask-for-password nil
443 liece-password password))
444 (if (and (memq type '(rlogin telnet)) relay)
445 (setq liece-tcp-relay-host relay))
446 (setq liece-tmp-server-name host)
447 (setq liece-server-process (liece-open-server-internal host service type))
448 (setq liece-after-registration nil)
450 (if (null (liece-wait-for-response "^:[^ ]+ [4P][5O][1N][ G]"))
452 ;; We have to close connection here, since the function
453 ;; `liece-server-opened' may return incorrect status.
454 (liece-close-server-internal)
455 (error (_ "Connection to %s timed out") host))
456 (set-process-sentinel liece-server-process 'liece-sentinel)
457 (set-process-filter liece-server-process 'liece-filter)
458 (if (or liece-ask-for-password liece-reconnect-with-password)
459 (let ((passwd-echo ?*) password)
460 (setq password (read-passwd (_ "Server Password: ")))
461 (or (string= password "")
462 (setq liece-password password))))
464 (liece-send "PASS %s" liece-password))
465 (setq liece-reconnect-with-password nil)
466 (liece-send "USER %s * * :%s"
467 (or (user-real-login-name) "Nobody")
468 (if (and liece-name (not (string= liece-name "")))
471 (liece-send "NICK %s" liece-nickname)
472 ;; We have to set `liece-real-nickname' here because IRC server doesn't
473 ;; notify the real nickname to the user.
474 (or liece-real-nickname
475 (setq liece-real-nickname
476 (truncate-string liece-nickname liece-nick-max-length)))
477 (setq liece-nickname-last liece-real-nickname
478 liece-nick-accepted 'sent
479 liece-after-registration t))))
481 (defun liece-open-server-internal (host &optional service type)
482 "Open connection to chat server on HOST by SERVICE (default is irc).
483 Optional argument TYPE specifies connection types such as `program'."
484 (let ((liece-tcp-connection-type type)
488 (liece-open-network-stream
489 "IRC" " *IRC*" host (or service "irc"))))
490 (setq liece-server-buffer (process-buffer process))
492 (set-buffer liece-server-buffer)
493 (set-buffer-multibyte nil)
494 (kill-all-local-variables)
495 (buffer-disable-undo)
499 (defun liece-initialize-timers ()
500 "Initialise internal timers."
501 (dolist (timer liece-timers)
503 (cancel-timer (caddr timer))
504 (let ((handler (car timer)) (interval (cadr timer)))
505 (and (liece-functionp handler)
506 (symbolp interval) (boundp interval)
507 (setq interval (symbol-value interval))
509 (list (run-at-time 1 interval handler)))))))
510 (setq liece-timers-list-initialized-p t))
512 (defun liece-read-variables-files (&optional file)
513 "Read variables FILEs."
514 (and (not (file-directory-p liece-directory))
515 (file-exists-p liece-directory)
516 (yes-or-no-p "Upgrade the location of the data files? ")
519 (make-temp-name "liece") temporary-file-directory)))
522 (rename-file liece-directory file 'ok-if-exists)
523 (make-directory liece-directory)
524 (copy-file file (expand-file-name
525 (file-name-nondirectory liece-variables-file)
527 (ignore-errors (delete-file file)))))
528 (or (file-directory-p liece-directory)
529 (make-directory liece-directory))
530 (let ((files (if file
532 (setq liece-variables-file file
533 liece-variables-files (list file)))
534 liece-variables-files)))
536 (if (file-readable-p (expand-file-name file))
537 (load (expand-file-name file) t)))))
540 (defun liece (&optional confirm)
541 "Connect to the IRC server and start chatting.
542 If optional argument CONFIRM is non-nil, ask which IRC server to connect.
543 If already connected, just pop up the windows."
545 (liece-read-variables-files
546 (car command-line-args-left))
547 (pop command-line-args-left)
548 (run-hooks 'liece-after-load-startup-hook)
549 ;; Save initial state of window configuration.
550 (when (interactive-p)
551 (liece-window-configuration-push))
552 (unless liece-intl-message-alist
553 (liece-intl-load-catalogue))
554 (if (liece-server-opened)
555 (liece-configure-windows)
559 (liece-get-buffer-create liece-command-buffer))
560 (unless (eq major-mode 'liece-command-mode)
561 (liece-command-mode))
562 (unless (liece-server-opened)
563 (liece-start-server confirm)))
564 (if (not (liece-server-opened))
566 ;; IRC server is successfully open.
567 (with-current-buffer liece-command-buffer
568 (setq mode-line-process (concat " " (liece-server-host))))
569 (let (buffer-read-only)
570 (unless liece-keep-buffers
574 (liece-initialize-buffers)
575 (liece-configure-windows)
576 (setq liece-current-channels nil)
578 (liece-current-channel
579 (liece-command-join liece-current-channel))
580 (liece-startup-channel
581 (liece-command-join liece-startup-channel))
582 (liece-startup-channel-list
583 (dolist (chnl liece-startup-channel-list)
585 (liece-command-join (car chnl) (cadr chnl))
586 (liece-command-join chnl)))))
587 (unless (string-equal liece-away-message "")
588 (liece-command-away liece-away-message))
589 (run-hooks 'liece-startup-hook)
591 (or liece-obarray (make-vector liece-obarray-size nil)))
592 (unless liece-timers-list-initialized-p
593 (liece-initialize-timers))
594 (liece-command-timestamp)
595 (message (substitute-command-keys
596 "Type \\[describe-mode] for help"))))))
599 (defun liece-command-mode ()
600 "Major mode for Liece. Normal edit function are available.
601 Typing Return or Linefeed enters the current line in the dialogue.
602 The following special commands are available:
603 For a list of the generic commands type \\[liece-command-generic] ? RET.
604 \\{liece-command-mode-map}"
606 (kill-all-local-variables)
608 (setq liece-nick-alist (list (list liece-nickname))
609 major-mode 'liece-command-mode
611 liece-privmsg-partner nil
612 liece-private-indicator nil
613 liece-away-indicator "-"
614 liece-beep-indicator "-"
615 liece-freeze-indicator "-"
616 liece-own-freeze-indicator "-"
617 mode-line-buffer-identification
618 (liece-mode-line-buffer-identification
621 liece-private-indicator
623 "-- " liece-current-channel " " liece-real-nickname)))
624 (liece-suppress-mode-line-format)
625 (use-local-map liece-command-mode-map)
627 (when liece-display-frame-title
628 (make-local-variable 'frame-title-format)
629 (setq frame-title-format 'liece-channel-status-indicator))
631 (unless liece-blink-parens
632 (make-local-variable 'blink-matching-paren)
633 (setq blink-matching-paren nil))
635 (unless liece-command-mode-syntax-table
636 (setq liece-command-mode-syntax-table
637 (copy-syntax-table (syntax-table)))
638 (set-syntax-table liece-command-mode-syntax-table)
640 (function (lambda (c) (modify-syntax-entry c "w")))
643 (run-hooks 'liece-command-mode-hook))
646 (defun liece-dialogue-mode ()
647 "Major mode for displaying the IRC dialogue.
648 All normal editing commands are turned off.
649 Instead, these commands are available:
650 \\{liece-dialogue-mode-map}"
651 (kill-all-local-variables)
653 (make-local-variable 'liece-beep)
654 (make-local-variable 'liece-beep-indicator)
655 (make-local-variable 'liece-freeze)
656 (make-local-variable 'liece-freeze-indicator)
657 (make-local-variable 'liece-own-freeze)
658 (make-local-variable 'liece-own-freeze-indicator)
659 (make-local-variable 'tab-stop-list)
661 (setq liece-beep liece-default-beep
662 liece-beep-indicator (if liece-beep "B" "-")
663 liece-freeze liece-default-freeze
664 liece-freeze-indicator (if liece-freeze "F" "-")
665 liece-own-freeze liece-default-own-freeze
666 liece-own-freeze-indicator (if liece-own-freeze "M" "-")
668 major-mode 'liece-dialogue-mode
670 mode-line-buffer-identification
671 (liece-mode-line-buffer-identification
676 liece-freeze-indicator
677 liece-own-freeze-indicator
678 " " liece-channels-indicator " "))
680 tab-stop-list liece-tab-stop-list)
681 (liece-suppress-mode-line-format)
682 (use-local-map liece-dialogue-mode-map)
683 (buffer-disable-undo)
685 (unless liece-keep-buffers
688 (run-hooks 'liece-dialogue-mode-hook))
691 (define-derived-mode liece-others-mode liece-dialogue-mode
693 "Major mode for displaying the IRC others message except current channel.
694 All normal editing commands are turned off.
695 Instead, these commands are available:
696 \\{liece-others-mode-map}")
699 (define-derived-mode liece-channel-mode liece-dialogue-mode
701 "Major mode for displaying the IRC current channel buffer.
702 All normal editing commands are turned off.
703 Instead, these commands are available:
704 \\{liece-channel-mode-map}"
705 (setq mode-line-buffer-identification
706 (liece-mode-line-buffer-identification
711 liece-freeze-indicator
712 liece-own-freeze-indicator
714 liece-channel-indicator))))
717 (defun liece-channel-list-mode ()
718 "Major mode for displaying channel list.
719 All normal editing commands are turned off."
720 (kill-all-local-variables)
721 (setq major-mode 'liece-channel-list-mode
723 mode-line-buffer-identification
724 (liece-mode-line-buffer-identification
725 '("Liece: " liece-command-buffer-mode-indicator " "))
728 (use-local-map liece-channel-list-mode-map)
729 (run-hooks 'liece-channel-list-mode-hook))
732 (defun liece-nick-mode ()
733 "Major mode for displaying members in the IRC current channel buffer.
734 All normal editing commands are turned off.
735 Instead, these commands are available:
736 \\{liece-nick-mode-map}"
737 (kill-all-local-variables)
738 (setq mode-line-modified "--- "
739 major-mode 'liece-nick-mode
740 mode-name "Liece Channel member"
741 mode-line-buffer-identification
742 (liece-mode-line-buffer-identification
743 '("Liece: " liece-channel-indicator " "))
746 (if (boundp 'transient-mark-mode)
747 (set (make-local-variable 'transient-mark-mode) t))
748 (use-local-map liece-nick-mode-map)
749 (run-hooks 'liece-nick-mode-hook))
751 (fset 'liece-dialogue-beep 'liece-command-beep)
752 (fset 'liece-dialogue-freeze 'liece-command-freeze)
753 (fset 'liece-dialogue-own-freeze 'liece-command-own-freeze)
755 (defun liece-initialize-buffers ()
756 "Initialize buffers."
757 (dolist (spec liece-buffer-mode-alist)
758 (let ((buffer (symbol-value (car spec)))
760 (or (get-buffer buffer)
762 (set-buffer (liece-get-buffer-create buffer))
763 (or (eq major-mode mode)
768 (defun liece-clear-system ()
769 "Clear all Liece variables and buffers."
771 (dolist (buffer liece-buffer-list)
772 (when (and (get-buffer buffer) (buffer-live-p buffer))
773 (funcall liece-buffer-dispose-function buffer)))
774 (if (vectorp liece-obarray)
775 (dotimes (i liece-obarray-size)
776 (aset liece-obarray i nil)))
777 (dolist (timer liece-timers)
779 (cancel-timer (caddr timer)))
781 (setcdr (cdr timer) nil)))
782 (setq liece-channel-buffer-alist nil
783 liece-nick-buffer-alist nil
784 liece-current-channels nil
785 liece-current-channel nil
786 liece-current-chat-partners nil
787 liece-current-chat-partner nil
788 liece-timers-list-initialized-p nil
789 liece-friends-last nil
791 liece-channel-indicator "No channel"))
793 (defun liece-wait-for-response (regexp &optional timeout)
794 "Wait for server response which match REGEXP.
795 Optional argument TIMEOUT specifies connection timeout."
797 (let ((status t) (wait t) (timeout (or timeout liece-connection-timeout)))
798 (set-buffer liece-server-buffer)
799 (with-timeout (timeout nil)
801 (liece-accept-response)
802 (goto-char (point-min))
803 (cond ((looking-at "ERROR") (setq status nil wait nil))
804 ((looking-at ".") (setq wait nil))))
805 ;; Save status message.
807 (setq liece-status-message-string
808 (buffer-substring (point-min) (point)))
811 (goto-char (point-max))
813 (if (looking-at regexp)
815 (liece-message (_ "Reading..."))
816 (liece-accept-response))))
817 ;; Successfully received server response.
820 (defun liece-accept-process-output (process &optional timeout)
821 "Wait for output from PROCESS and message some dots.
822 Optional argument TIMEOUT specifies connection timeout."
824 (set-buffer liece-server-buffer)
825 (accept-process-output process (or timeout 1))))
827 (defun liece-accept-response ()
828 "Read response of server. Only used at startup time."
829 (unless (liece-server-opened)
831 ((not liece-reconnect-automagic)
832 (error "Liece: Connection closed"))
834 (let ((liece-nickname (concat liece-nickname liece-grow-tail)))
838 (liece-accept-process-output liece-server-process)
840 (or (string-equal "select error: Invalid argument" (nth 1 code))
841 (signal (car code) (cdr code))))))
843 (defmacro liece-replace-internal (buffer match defstring oldstring newstring)
844 "Helper function only used from `liece-replace'.
846 Replace in buffer or list of buffers BUFFER with matching MATCH.
847 Argument DEFSTRING used when no matches are there.
848 Argument OLDSTRING is replaced with NEWSTRING."
850 (set-buffer (get-buffer ,buffer))
851 (let (buffer-read-only (inhibit-read-only t))
852 (goto-char (point-max))
853 (previous-line liece-compress-treshold)
855 (if (not (re-search-forward ,match nil t))
856 (liece-insert ,buffer ,defstring)
857 (while (re-search-forward ,match nil t))
859 (if (re-search-forward ,oldstring nil t)
860 (replace-match ,newstring nil t)
861 (liece-insert ,buffer ,defstring))
862 (liece-insert ,buffer ""))))))
865 (defun liece-replace (buffer match defstring oldstring newstring)
866 "Replace in buffer or list of buffers BUFFER with matching MATCH.
867 Argument DEFSTRING used when no matches are there.
868 Argument OLDSTRING is replaced with NEWSTRING."
869 (unless (listp buffer)
870 (setq buffer (list buffer)))
872 (when (get-buffer buf)
873 (liece-replace-internal buf match defstring oldstring newstring))))
875 (defun liece-check-buffers ()
876 "Check if there is a buffer larger than `liece-buffer-max-size'.
877 If such a buffer is found, shrink it."
878 (let ((liece-buffer-check-interval 0))
879 (when (> liece-buffer-max-size 0)
881 (dolist (buffer liece-channel-buffer-alist)
882 (set-buffer (cdr buffer))
883 (when (< liece-buffer-max-size (buffer-size))
884 (let ((inhibit-read-only t)
886 (delete-region (point-min)
888 (goto-char (- (buffer-size)
889 liece-buffer-default-size))
890 (beginning-of-line -1)
893 (setq liece-buffer-last-check-time (current-time)))))))))
895 (defun liece-check-buffers-if-interval-expired ()
896 "Timer handler for `liece-check-buffers'.
897 Only used from `liece-before-insert-functions'."
898 (and (> liece-buffer-check-interval 0)
899 (or (null liece-buffer-last-check-time)
900 (> (liece-time-difference liece-buffer-last-check-time
902 liece-buffer-check-interval))
903 (liece-check-buffers)))
905 (defun liece-refresh-buffer-window (buffer)
906 "Center point in window of BUFFER and redisplay frame."
907 (let ((window (liece-get-buffer-window buffer)))
908 (when (and window (not (pos-visible-in-window-p (point-max) window)))
909 (save-selected-window
910 (select-window window)
911 (goto-char (point-max))
912 (if (null liece-scroll-step)
913 (recenter (- (liece-window-height window) 1))
915 (- (or liece-scroll-step
916 (1+ (/ (liece-window-height window) 2)))
917 (liece-window-height window)))
918 (set-window-start window (point))
919 (goto-char (point-max)))))))
921 (defmacro liece-save-point (&rest body)
922 "Execute BODY, then goto the point that was around before BODY."
923 (let ((liece-save-point (liece-gensym "lsp")))
924 `(let ((,liece-save-point (point-marker)))
927 (goto-char ,liece-save-point)
928 (set-marker ,liece-save-point nil)))))
930 (defvar liece-before-insert-functions
931 '(liece-check-buffers-if-interval-expired
932 liece-command-timestamp-if-interval-expired))
934 (defun liece-insert-internal (buffer string)
935 "Helper function only used from `liece-insert'.
937 Insert before point of BUFFER STRING with decorating."
938 (run-hooks 'liece-before-insert-functions)
939 (with-current-buffer (liece-get-buffer-create buffer)
940 (or (eq (derived-mode-class major-mode) 'liece-dialogue-mode)
941 (liece-dialogue-mode))
943 (let ((inhibit-read-only t)
945 (from (goto-char (point-max))))
946 (unless (liece-is-message-ignored string (current-buffer))
947 (and liece-display-time (not (string-equal string ""))
948 (liece-insert-time-string))
950 (run-hook-with-args 'liece-after-insert-functions from (point)))))
951 (unless (liece-frozen (current-buffer))
952 (liece-refresh-buffer-window (current-buffer)))))
955 (defun liece-insert (buffer string)
956 "Insert before point of BUFFER STRING with decorating."
958 (setq buffer (list buffer)))
960 (when (get-buffer buf)
961 (liece-insert-internal buf string))))
965 ;;; liece.el ends here