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
342 (if (liece-server-opened)
343 ;; Stream is already opened.
346 (when (or confirm (null liece-server))
348 (liece-minibuffer-completing-default-read
350 liece-server-alist)))
352 liece-ask-for-nickname
354 (read-string (_ "Enter your nickname: ") liece-nickname)))
355 ;; If no server name is given, local host is assumed.
357 (stringp liece-server)
358 (string-equal liece-server "")
359 (setq liece-server (system-name)))
360 (let ((host (liece-server-host)))
362 (_ "Connecting to IRC server on %s...") host)
364 ((liece-open-server liece-server liece-service))
365 ((and (stringp liece-status-message-string)
366 (> (length liece-status-message-string) 0))
367 ;; Show valuable message if available.
368 (error liece-status-message-string))
369 (t (error (_ "Cannot open IRC server on %s") host))))))
371 (defun liece-close-server-internal ()
372 "Close connection to chat server."
373 (if (liece-server-opened)
374 (delete-process liece-server-process))
375 (if liece-server-buffer
376 (kill-buffer liece-server-buffer))
377 (setq liece-server-buffer nil
378 liece-server-process nil
382 (defun liece-close-server ()
386 ;; Unset default sentinel function before closing connection.
389 (eq (quote liece-sentinel)
390 (process-sentinel liece-server-process))
391 (set-process-sentinel liece-server-process nil))
392 ;; We cannot send QUIT command unless the process is running.
393 (if (liece-server-opened)
394 (liece-send "QUIT")))
395 (liece-close-server-internal)))
397 (defmacro liece-server-keyword-bind (plist &rest body)
398 "Return a `let' form that binds all variables in PLIST.
399 After this is done, BODY will be executed in the scope
402 The variables bound and their default values are described by
403 the `liece-server-keyword-map' variable."
406 (list (intern (substring (symbol-name (car keyword)) 1))
408 `(or (plist-get plist ',(car keyword))
410 `(plist-get plist ',(car keyword)))))
411 liece-server-keyword-map)
414 (put 'liece-server-keyword-bind 'lisp-indent-function 1)
415 (put 'liece-server-keyword-bind 'edebug-form-spec '(form body))
417 (defun liece-server-parse-string (string)
418 "Convert a STRING set as `liece-server' and return a property list."
419 (when (or (string-match "^\\[\\([^]]+\\)\\]:?\\([0-9]*\\)" string)
420 (string-match "^\\([^:]+\\):?\\([0-9]*\\)" string))
421 (let ((host (match-string 1 string))
422 (service (match-string 2 string))
423 (password (substring string (match-end 0)))
425 (push `(:host ,host) plist)
426 (unless (string= service "")
427 (push `(:service ,(string-to-int service)) plist))
429 ((string= password ":")
430 (setq liece-ask-for-password t))
431 ((string= password ""))
432 (t (push `(:password ,(substring password 1)) plist)))
433 (apply #'nconc plist))))
435 (defun liece-open-server (host &optional service)
436 "Open chat server on HOST.
437 If HOST is nil, use value of environment variable \"IRCSERVER\".
438 If optional argument SERVICE is non-nil, open by the service name."
439 (let* ((host (or host (getenv "IRCSERVER")))
443 (or (cdr (string-assoc-ignore-case host liece-server-alist))
444 (liece-server-parse-string host))))
446 (setq liece-status-message-string "")
447 (when (stringp plist) ;; Old style server entry...
448 (setq plist (liece-server-parse-string host)))
449 (when (and (stringp host)
450 (null (string-assoc-ignore-case host liece-server-alist)))
451 (push (cons host plist) liece-server-alist)
452 (setq liece-save-variables-are-dirty t))
453 (liece-server-keyword-bind plist
454 ;; Execute preconnecting script
456 (if (fboundp prescript)
458 (call-process shell-file-name nil nil nil
459 shell-command-switch prescript))
460 (when prescript-delay
461 (sleep-for prescript-delay)))
463 (setq liece-ask-for-password nil
464 liece-password password))
465 (if (and (memq type '(rlogin telnet)) relay)
466 (setq liece-tcp-relay-host relay))
467 (setq liece-tmp-server-name host);; temporary
468 (liece-message (_ "Connecting to IRC server %s...") host)
471 (setq liece-status-message-string
472 (_ "IRC server is not specified.")))
473 ((liece-open-server-internal host service type)
474 (setq liece-after-registration nil)
476 (setq status (liece-wait-for-response "^:[^ ]+ [4P][5O][1N][ G]"))
479 (setq liece-status-message-string
480 (format (_ "Connection to %s timed out") host))
481 ;; We have to close connection here, since the function
482 ;; `liece-server-opened' may return incorrect status.
483 (liece-close-server-internal))
484 (setq liece-after-registration t)
485 (set-process-sentinel liece-server-process 'liece-sentinel)
486 (set-process-filter liece-server-process 'liece-filter)
487 (if (or liece-ask-for-password liece-reconnect-with-password)
488 (let ((passwd-echo ?*) password)
489 (setq password (read-passwd (_ "Server Password: ")))
490 (or (string= password "")
491 (setq liece-password password))))
493 (liece-send "PASS %s" liece-password))
494 (setq liece-reconnect-with-password nil)
495 (liece-send "USER %s * * :%s"
496 (or (user-real-login-name) "Nobody")
497 (if (and liece-name (not (string= liece-name "")))
500 (or liece-real-nickname
501 (setq liece-real-nickname liece-nickname))
502 (setq liece-real-nickname
503 (truncate-string liece-real-nickname liece-nick-max-length)
504 liece-nickname-last liece-real-nickname
505 liece-nick-accepted 'sent
506 liece-after-registration t)
507 (liece-send "NICK %s" liece-real-nickname)))))
510 (defun liece-open-server-internal (host &optional service type)
511 "Open connection to chat server on HOST by SERVICE (default is irc).
512 Optional argument TYPE specifies connection types such as `program'."
513 ;; canonicalize host representation
514 (unless (string-match "^[^\\[]" host)
515 (setq host (substring host 1 (1- (length host)))))
516 (condition-case error
518 (let ((liece-tcp-connection-type type))
520 (setq liece-server-process
521 (liece-open-network-stream
522 "IRC" " *IRC*" host (or service "irc"))))
523 (setq liece-server-buffer (process-buffer liece-server-process))
524 (set-buffer liece-server-buffer)
525 (set-buffer-multibyte nil)
526 (kill-all-local-variables)
527 (buffer-disable-undo)
529 (setq liece-server-name host)
530 (run-hooks 'liece-server-hook)
531 ;; return the server process
532 liece-server-process)
534 (setq liece-status-message-string (cadr error)
535 liece-server-process nil)))))
537 (defun liece-initialize-timers ()
538 "Initialise internal timers."
539 (dolist (timer liece-timers)
541 (cancel-timer (caddr timer))
542 (let ((handler (car timer)) (interval (cadr timer)))
543 (and (liece-functionp handler)
544 (symbolp interval) (boundp interval)
545 (setq interval (symbol-value interval))
547 (list (run-at-time 1 interval handler)))))))
548 (setq liece-timers-list-initialized-p t))
550 (defun liece-read-variables-files (&optional file)
551 "Read variables FILEs."
552 (and (not (file-directory-p liece-directory))
553 (file-exists-p liece-directory)
554 (yes-or-no-p "Upgrade the location of the data files? ")
557 (make-temp-name "liece") temporary-file-directory)))
560 (rename-file liece-directory file 'ok-if-exists)
561 (make-directory liece-directory)
562 (copy-file file (expand-file-name
563 (file-name-nondirectory liece-variables-file)
565 (ignore-errors (delete-file file)))))
566 (or (file-directory-p liece-directory)
567 (make-directory liece-directory))
568 (let ((files (if file
570 (setq liece-variables-file file
571 liece-variables-files (list file)))
572 liece-variables-files)))
574 (if (file-readable-p (expand-file-name file))
575 (load (expand-file-name file) t)))))
578 (defun liece (&optional confirm)
579 "Connect to the IRC server and start chatting.
580 If optional argument CONFIRM is non-nil, ask which IRC server to connect.
581 If already connected, just pop up the windows."
583 (liece-read-variables-files
584 (car command-line-args-left))
585 (pop command-line-args-left)
586 (run-hooks 'liece-after-load-startup-hook)
587 ;; Save initial state of window configuration.
588 (when (interactive-p)
589 (liece-window-configuration-push))
590 (unless liece-intl-message-alist
591 (liece-intl-load-catalogue))
592 (if (liece-server-opened)
593 (liece-configure-windows)
597 (liece-get-buffer-create liece-command-buffer))
598 (unless (eq major-mode 'liece-command-mode)
599 (liece-command-mode))
600 (liece-start-server confirm))
601 (if (not (liece-server-opened))
603 ;; IRC server is successfully open.
604 (with-current-buffer liece-command-buffer
605 (setq mode-line-process (concat " " (liece-server-host))))
606 (let (buffer-read-only)
607 (unless liece-keep-buffers
611 (liece-initialize-buffers)
612 (liece-configure-windows)
613 (setq liece-current-channels nil)
615 (liece-current-channel
616 (liece-command-join liece-current-channel))
617 (liece-startup-channel
618 (liece-command-join liece-startup-channel))
619 (liece-startup-channel-list
620 (dolist (chnl liece-startup-channel-list)
622 (liece-command-join (car chnl) (cadr chnl))
623 (liece-command-join chnl)))))
624 (unless (string-equal liece-away-message "")
625 (liece-command-away liece-away-message))
626 (run-hooks 'liece-startup-hook)
628 (or liece-obarray (make-vector liece-obarray-size nil)))
629 (unless liece-timers-list-initialized-p
630 (liece-initialize-timers))
631 (liece-command-timestamp)
632 (message (substitute-command-keys
633 "Type \\[describe-mode] for help"))))))
636 (defun liece-command-mode ()
637 "Major mode for Liece. Normal edit function are available.
638 Typing Return or Linefeed enters the current line in the dialogue.
639 The following special commands are available:
640 For a list of the generic commands type \\[liece-command-generic] ? RET.
641 \\{liece-command-mode-map}"
643 (kill-all-local-variables)
645 (setq liece-nick-alist (list (list liece-nickname))
646 major-mode 'liece-command-mode
648 liece-privmsg-partner nil
649 liece-private-indicator nil
650 liece-away-indicator "-"
651 liece-beep-indicator "-"
652 liece-freeze-indicator "-"
653 liece-own-freeze-indicator "-"
654 mode-line-buffer-identification
655 (liece-mode-line-buffer-identification
658 liece-private-indicator
660 "-- " liece-current-channel " " liece-real-nickname)))
661 (liece-suppress-mode-line-format)
662 (use-local-map liece-command-mode-map)
664 (when liece-display-frame-title
665 (make-local-variable 'frame-title-format)
666 (setq frame-title-format 'liece-channel-status-indicator))
668 (unless liece-blink-parens
669 (make-local-variable 'blink-matching-paren)
670 (setq blink-matching-paren nil))
672 (unless liece-command-mode-syntax-table
673 (setq liece-command-mode-syntax-table
674 (copy-syntax-table (syntax-table)))
675 (set-syntax-table liece-command-mode-syntax-table)
677 (function (lambda (c) (modify-syntax-entry c "w")))
680 (run-hooks 'liece-command-mode-hook))
683 (defun liece-dialogue-mode ()
684 "Major mode for displaying the IRC dialogue.
685 All normal editing commands are turned off.
686 Instead, these commands are available:
687 \\{liece-dialogue-mode-map}"
688 (kill-all-local-variables)
690 (make-local-variable 'liece-beep)
691 (make-local-variable 'liece-beep-indicator)
692 (make-local-variable 'liece-freeze)
693 (make-local-variable 'liece-freeze-indicator)
694 (make-local-variable 'liece-own-freeze)
695 (make-local-variable 'liece-own-freeze-indicator)
696 (make-local-variable 'tab-stop-list)
698 (setq liece-beep liece-default-beep
699 liece-beep-indicator (if liece-beep "B" "-")
700 liece-freeze liece-default-freeze
701 liece-freeze-indicator (if liece-freeze "F" "-")
702 liece-own-freeze liece-default-own-freeze
703 liece-own-freeze-indicator (if liece-own-freeze "M" "-")
705 major-mode 'liece-dialogue-mode
707 mode-line-buffer-identification
708 (liece-mode-line-buffer-identification
713 liece-freeze-indicator
714 liece-own-freeze-indicator
715 " " liece-channels-indicator " "))
717 tab-stop-list liece-tab-stop-list)
718 (liece-suppress-mode-line-format)
719 (use-local-map liece-dialogue-mode-map)
720 (buffer-disable-undo)
722 (unless liece-keep-buffers
725 (run-hooks 'liece-dialogue-mode-hook))
728 (define-derived-mode liece-others-mode liece-dialogue-mode
730 "Major mode for displaying the IRC others message except current channel.
731 All normal editing commands are turned off.
732 Instead, these commands are available:
733 \\{liece-others-mode-map}")
736 (define-derived-mode liece-channel-mode liece-dialogue-mode
738 "Major mode for displaying the IRC current channel buffer.
739 All normal editing commands are turned off.
740 Instead, these commands are available:
741 \\{liece-channel-mode-map}"
742 (setq mode-line-buffer-identification
743 (liece-mode-line-buffer-identification
748 liece-freeze-indicator
749 liece-own-freeze-indicator
751 liece-channel-indicator))))
754 (defun liece-channel-list-mode ()
755 "Major mode for displaying channel list.
756 All normal editing commands are turned off."
757 (kill-all-local-variables)
758 (setq major-mode 'liece-channel-list-mode
760 mode-line-buffer-identification
761 (liece-mode-line-buffer-identification
762 '("Liece: " liece-command-buffer-mode-indicator " "))
765 (use-local-map liece-channel-list-mode-map)
766 (run-hooks 'liece-channel-list-mode-hook))
769 (defun liece-nick-mode ()
770 "Major mode for displaying members in the IRC current channel buffer.
771 All normal editing commands are turned off.
772 Instead, these commands are available:
773 \\{liece-nick-mode-map}"
774 (kill-all-local-variables)
775 (setq mode-line-modified "--- "
776 major-mode 'liece-nick-mode
777 mode-name "Liece Channel member"
778 mode-line-buffer-identification
779 (liece-mode-line-buffer-identification
780 '("Liece: " liece-channel-indicator " "))
783 (if (boundp 'transient-mark-mode)
784 (set (make-local-variable 'transient-mark-mode) t))
785 (use-local-map liece-nick-mode-map)
786 (run-hooks 'liece-nick-mode-hook))
788 (fset 'liece-dialogue-beep 'liece-command-beep)
789 (fset 'liece-dialogue-freeze 'liece-command-freeze)
790 (fset 'liece-dialogue-own-freeze 'liece-command-own-freeze)
792 (defun liece-initialize-buffers ()
793 "Initialize buffers."
794 (dolist (spec liece-buffer-mode-alist)
795 (let ((buffer (symbol-value (car spec)))
797 (or (get-buffer buffer)
799 (set-buffer (liece-get-buffer-create buffer))
800 (or (eq major-mode mode)
806 (defun liece-clear-system ()
807 "Clear all Liece variables and buffers."
809 (dolist (buffer liece-buffer-list)
810 (when (and (get-buffer buffer) (buffer-live-p buffer))
811 (funcall liece-buffer-dispose-function buffer)))
812 (if (vectorp liece-obarray)
813 (dotimes (i liece-obarray-size)
814 (aset liece-obarray i nil)))
815 (dolist (timer liece-timers)
817 (cancel-timer (caddr timer)))
819 (setcdr (cdr timer) nil)))
820 (setq liece-channel-buffer-alist nil
821 liece-nick-buffer-alist nil
822 liece-current-channels nil
823 liece-current-channel nil
824 liece-current-chat-partners nil
825 liece-current-chat-partner nil
826 liece-timers-list-initialized-p nil
827 liece-friends-last nil
829 liece-channel-indicator "No channel"))
831 (defun liece-wait-for-response (regexp &optional timeout)
832 "Wait for server response which match REGEXP.
833 Optional argument TIMEOUT specifies connection timeout."
835 (let ((status t) (wait t) (timeout (or timeout liece-connection-timeout)))
836 (set-buffer liece-server-buffer)
837 (with-timeout (timeout nil)
839 (liece-accept-response)
840 (goto-char (point-min))
841 (cond ((looking-at "ERROR") (setq status nil wait nil))
842 ((looking-at ".") (setq wait nil))))
843 ;; Save status message.
845 (setq liece-status-message-string
846 (buffer-substring (point-min) (point)))
849 (goto-char (point-max))
851 (if (looking-at regexp)
853 (liece-message (_ "Reading..."))
854 (liece-accept-response))))
855 ;; Successfully received server response.
858 (defun liece-accept-process-output (process &optional timeout)
859 "Wait for output from PROCESS and message some dots.
860 Optional argument TIMEOUT specifies connection timeout."
862 (set-buffer liece-server-buffer)
863 (accept-process-output process (or timeout 1))))
865 (defun liece-accept-response ()
866 "Read response of server. Only used at startup time."
867 (unless (liece-server-opened)
869 ((not liece-reconnect-automagic)
870 (error "Liece: Connection closed"))
872 (let ((liece-nickname (concat liece-nickname liece-grow-tail)))
876 (liece-accept-process-output liece-server-process)
878 (or (string-equal "select error: Invalid argument" (nth 1 code))
879 (signal (car code) (cdr code))))))
881 (defmacro liece-replace-internal (buffer match defstring oldstring newstring)
882 "Helper function only used from `liece-replace'.
884 Replace in buffer or list of buffers BUFFER with matching MATCH.
885 Argument DEFSTRING used when no matches are there.
886 Argument OLDSTRING is replaced with NEWSTRING."
888 (set-buffer (get-buffer ,buffer))
889 (let (buffer-read-only (inhibit-read-only t))
890 (goto-char (point-max))
891 (previous-line liece-compress-treshold)
893 (if (not (re-search-forward ,match nil t))
894 (liece-insert ,buffer ,defstring)
895 (while (re-search-forward ,match nil t))
897 (if (re-search-forward ,oldstring nil t)
898 (replace-match ,newstring nil t)
899 (liece-insert ,buffer ,defstring))
900 (liece-insert ,buffer ""))))))
903 (defun liece-replace (buffer match defstring oldstring newstring)
904 "Replace in buffer or list of buffers BUFFER with matching MATCH.
905 Argument DEFSTRING used when no matches are there.
906 Argument OLDSTRING is replaced with NEWSTRING."
907 (unless (listp buffer)
908 (setq buffer (list buffer)))
910 (when (get-buffer buf)
911 (liece-replace-internal buf match defstring oldstring newstring))))
913 (defun liece-check-buffers ()
914 "Check if there is a buffer larger than `liece-buffer-max-size'.
915 If such a buffer is found, shrink it."
916 (let ((liece-buffer-check-interval 0))
917 (when (> liece-buffer-max-size 0)
919 (dolist (buffer liece-channel-buffer-alist)
920 (set-buffer (cdr buffer))
921 (when (< liece-buffer-max-size (buffer-size))
922 (let ((inhibit-read-only t)
924 (delete-region (point-min)
926 (goto-char (- (buffer-size)
927 liece-buffer-default-size))
928 (beginning-of-line -1)
931 (setq liece-buffer-last-check-time (current-time)))))))))
933 (defun liece-check-buffers-if-interval-expired ()
934 "Timer handler for `liece-check-buffers'.
935 Only used from `liece-before-insert-hook'."
936 (and (> liece-buffer-check-interval 0)
937 (or (null liece-buffer-last-check-time)
938 (> (liece-time-difference liece-buffer-last-check-time
940 liece-buffer-check-interval))
941 (liece-check-buffers)))
943 (defun liece-refresh-buffer-window (buffer)
944 "Center point in window of BUFFER and redisplay frame."
945 (let ((window (liece-get-buffer-window buffer)))
946 (when (and window (not (pos-visible-in-window-p (point-max) window)))
947 (save-selected-window
948 (select-window window)
949 (goto-char (point-max))
950 (if (null liece-scroll-step)
951 (recenter (- (liece-window-height window) 1))
953 (- (or liece-scroll-step
954 (1+ (/ (liece-window-height window) 2)))
955 (liece-window-height window)))
956 (set-window-start window (point))
957 (goto-char (point-max)))))))
959 (defmacro liece-save-point (&rest body)
960 "Execute BODY, then goto the point that was around before BODY."
961 (let ((liece-save-point (liece-gensym "lsp")))
962 `(let ((,liece-save-point (point-marker)))
965 (goto-char ,liece-save-point)
966 (set-marker ,liece-save-point nil)))))
968 (defvar liece-before-insert-hook
969 '(liece-check-buffers-if-interval-expired
970 liece-command-timestamp-if-interval-expired))
972 (defun liece-insert-internal (buffer string)
973 "Helper function only used from `liece-insert'.
975 Insert before point of BUFFER STRING with decorating."
976 (run-hooks 'liece-before-insert-hook)
977 (with-current-buffer (liece-get-buffer-create buffer)
978 (or (eq (derived-mode-class major-mode) 'liece-dialogue-mode)
979 (liece-dialogue-mode))
981 (let ((inhibit-read-only t)
983 (from (goto-char (point-max))))
984 (unless (liece-is-message-ignored string (current-buffer))
985 (and liece-display-time (not (string-equal string ""))
986 (liece-insert-time-string))
988 (run-hook-with-args 'liece-insert-hook from (point)))))
989 (unless (liece-frozen (current-buffer))
990 (liece-refresh-buffer-window (current-buffer)))))
993 (defun liece-insert (buffer string)
994 "Insert before point of BUFFER STRING with decorating."
996 (setq buffer (list buffer)))
998 (when (get-buffer buf)
999 (liece-insert-internal buf string))))
1003 ;;; liece.el ends here