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 (unless (string-match "^[^\\[]" host)
448 (setq host (substring host 1 (1- (length host)))))
449 (setq liece-server-process (liece-open-server-internal host service type))
450 (setq liece-after-registration nil)
452 (if (null (liece-wait-for-response "^:[^ ]+ [4P][5O][1N][ G]"))
454 ;; We have to close connection here, since the function
455 ;; `liece-server-opened' may return incorrect status.
456 (liece-close-server-internal)
457 (error (_ "Connection to %s timed out") host))
458 (set-process-sentinel liece-server-process 'liece-sentinel)
459 (set-process-filter liece-server-process 'liece-filter)
460 (if (or liece-ask-for-password liece-reconnect-with-password)
461 (let ((passwd-echo ?*) password)
462 (setq password (read-passwd (_ "Server Password: ")))
463 (or (string= password "")
464 (setq liece-password password))))
466 (liece-send "PASS %s" liece-password))
467 (setq liece-reconnect-with-password nil)
468 (liece-send "USER %s * * :%s"
469 (or (user-real-login-name) "Nobody")
470 (if (and liece-name (not (string= liece-name "")))
473 (liece-send "NICK %s" liece-nickname)
474 ;; We have to set `liece-real-nickname' here because IRC server doesn't
475 ;; notify the real nickname to the user.
476 (or liece-real-nickname
477 (setq liece-real-nickname
478 (truncate-string liece-nickname liece-nick-max-length)))
479 (setq liece-nickname-last liece-real-nickname
480 liece-nick-accepted 'sent
481 liece-after-registration t))))
483 (defun liece-open-server-internal (host &optional service type)
484 "Open connection to chat server on HOST by SERVICE (default is irc).
485 Optional argument TYPE specifies connection types such as `program'."
486 (let ((liece-tcp-connection-type type)
490 (liece-open-network-stream
491 "IRC" " *IRC*" host (or service "irc"))))
492 (setq liece-server-buffer (process-buffer process))
494 (set-buffer liece-server-buffer)
495 (set-buffer-multibyte nil)
496 (kill-all-local-variables)
497 (buffer-disable-undo)
501 (defun liece-initialize-timers ()
502 "Initialise internal timers."
503 (dolist (timer liece-timers)
505 (cancel-timer (caddr timer))
506 (let ((handler (car timer)) (interval (cadr timer)))
507 (and (liece-functionp handler)
508 (symbolp interval) (boundp interval)
509 (setq interval (symbol-value interval))
511 (list (run-at-time 1 interval handler)))))))
512 (setq liece-timers-list-initialized-p t))
514 (defun liece-read-variables-files (&optional file)
515 "Read variables FILEs."
516 (and (not (file-directory-p liece-directory))
517 (file-exists-p liece-directory)
518 (yes-or-no-p "Upgrade the location of the data files? ")
521 (make-temp-name "liece") temporary-file-directory)))
524 (rename-file liece-directory file 'ok-if-exists)
525 (make-directory liece-directory)
526 (copy-file file (expand-file-name
527 (file-name-nondirectory liece-variables-file)
529 (ignore-errors (delete-file file)))))
530 (or (file-directory-p liece-directory)
531 (make-directory liece-directory))
532 (let ((files (if file
534 (setq liece-variables-file file
535 liece-variables-files (list file)))
536 liece-variables-files)))
538 (if (file-readable-p (expand-file-name file))
539 (load (expand-file-name file) t)))))
542 (defun liece (&optional confirm)
543 "Connect to the IRC server and start chatting.
544 If optional argument CONFIRM is non-nil, ask which IRC server to connect.
545 If already connected, just pop up the windows."
547 (liece-read-variables-files
548 (car command-line-args-left))
549 (pop command-line-args-left)
550 (run-hooks 'liece-after-load-startup-hook)
551 ;; Save initial state of window configuration.
552 (when (interactive-p)
553 (liece-window-configuration-push))
554 (unless liece-intl-message-alist
555 (liece-intl-load-catalogue))
556 (if (liece-server-opened)
557 (liece-configure-windows)
561 (liece-get-buffer-create liece-command-buffer))
562 (unless (eq major-mode 'liece-command-mode)
563 (liece-command-mode))
564 (unless (liece-server-opened)
565 (liece-start-server confirm)))
566 (if (not (liece-server-opened))
568 ;; IRC server is successfully open.
569 (with-current-buffer liece-command-buffer
570 (setq mode-line-process (concat " " (liece-server-host))))
571 (let (buffer-read-only)
572 (unless liece-keep-buffers
576 (liece-initialize-buffers)
577 (liece-configure-windows)
578 (setq liece-current-channels nil)
580 (liece-current-channel
581 (liece-command-join liece-current-channel))
582 (liece-startup-channel
583 (liece-command-join liece-startup-channel))
584 (liece-startup-channel-list
585 (dolist (chnl liece-startup-channel-list)
587 (liece-command-join (car chnl) (cadr chnl))
588 (liece-command-join chnl)))))
589 (unless (string-equal liece-away-message "")
590 (liece-command-away liece-away-message))
591 (run-hooks 'liece-startup-hook)
593 (or liece-obarray (make-vector liece-obarray-size nil)))
594 (unless liece-timers-list-initialized-p
595 (liece-initialize-timers))
596 (liece-command-timestamp)
597 (message (substitute-command-keys
598 "Type \\[describe-mode] for help"))))))
601 (defun liece-command-mode ()
602 "Major mode for Liece. Normal edit function are available.
603 Typing Return or Linefeed enters the current line in the dialogue.
604 The following special commands are available:
605 For a list of the generic commands type \\[liece-command-generic] ? RET.
606 \\{liece-command-mode-map}"
608 (kill-all-local-variables)
610 (setq liece-nick-alist (list (list liece-nickname))
611 major-mode 'liece-command-mode
613 liece-privmsg-partner nil
614 liece-private-indicator nil
615 liece-away-indicator "-"
616 liece-beep-indicator "-"
617 liece-freeze-indicator "-"
618 liece-own-freeze-indicator "-"
619 mode-line-buffer-identification
620 (liece-mode-line-buffer-identification
623 liece-private-indicator
625 "-- " liece-current-channel " " liece-real-nickname)))
626 (liece-suppress-mode-line-format)
627 (use-local-map liece-command-mode-map)
629 (when liece-display-frame-title
630 (make-local-variable 'frame-title-format)
631 (setq frame-title-format 'liece-channel-status-indicator))
633 (unless liece-blink-parens
634 (make-local-variable 'blink-matching-paren)
635 (setq blink-matching-paren nil))
637 (unless liece-command-mode-syntax-table
638 (setq liece-command-mode-syntax-table
639 (copy-syntax-table (syntax-table)))
640 (set-syntax-table liece-command-mode-syntax-table)
642 (function (lambda (c) (modify-syntax-entry c "w")))
645 (run-hooks 'liece-command-mode-hook))
648 (defun liece-dialogue-mode ()
649 "Major mode for displaying the IRC dialogue.
650 All normal editing commands are turned off.
651 Instead, these commands are available:
652 \\{liece-dialogue-mode-map}"
653 (kill-all-local-variables)
655 (make-local-variable 'liece-beep)
656 (make-local-variable 'liece-beep-indicator)
657 (make-local-variable 'liece-freeze)
658 (make-local-variable 'liece-freeze-indicator)
659 (make-local-variable 'liece-own-freeze)
660 (make-local-variable 'liece-own-freeze-indicator)
661 (make-local-variable 'tab-stop-list)
663 (setq liece-beep liece-default-beep
664 liece-beep-indicator (if liece-beep "B" "-")
665 liece-freeze liece-default-freeze
666 liece-freeze-indicator (if liece-freeze "F" "-")
667 liece-own-freeze liece-default-own-freeze
668 liece-own-freeze-indicator (if liece-own-freeze "M" "-")
670 major-mode 'liece-dialogue-mode
672 mode-line-buffer-identification
673 (liece-mode-line-buffer-identification
678 liece-freeze-indicator
679 liece-own-freeze-indicator
680 " " liece-channels-indicator " "))
682 tab-stop-list liece-tab-stop-list)
683 (liece-suppress-mode-line-format)
684 (use-local-map liece-dialogue-mode-map)
685 (buffer-disable-undo)
687 (unless liece-keep-buffers
690 (run-hooks 'liece-dialogue-mode-hook))
693 (define-derived-mode liece-others-mode liece-dialogue-mode
695 "Major mode for displaying the IRC others message except current channel.
696 All normal editing commands are turned off.
697 Instead, these commands are available:
698 \\{liece-others-mode-map}")
701 (define-derived-mode liece-channel-mode liece-dialogue-mode
703 "Major mode for displaying the IRC current channel buffer.
704 All normal editing commands are turned off.
705 Instead, these commands are available:
706 \\{liece-channel-mode-map}"
707 (setq mode-line-buffer-identification
708 (liece-mode-line-buffer-identification
713 liece-freeze-indicator
714 liece-own-freeze-indicator
716 liece-channel-indicator))))
719 (defun liece-channel-list-mode ()
720 "Major mode for displaying channel list.
721 All normal editing commands are turned off."
722 (kill-all-local-variables)
723 (setq major-mode 'liece-channel-list-mode
725 mode-line-buffer-identification
726 (liece-mode-line-buffer-identification
727 '("Liece: " liece-command-buffer-mode-indicator " "))
730 (use-local-map liece-channel-list-mode-map)
731 (run-hooks 'liece-channel-list-mode-hook))
734 (defun liece-nick-mode ()
735 "Major mode for displaying members in the IRC current channel buffer.
736 All normal editing commands are turned off.
737 Instead, these commands are available:
738 \\{liece-nick-mode-map}"
739 (kill-all-local-variables)
740 (setq mode-line-modified "--- "
741 major-mode 'liece-nick-mode
742 mode-name "Liece Channel member"
743 mode-line-buffer-identification
744 (liece-mode-line-buffer-identification
745 '("Liece: " liece-channel-indicator " "))
748 (if (boundp 'transient-mark-mode)
749 (set (make-local-variable 'transient-mark-mode) t))
750 (use-local-map liece-nick-mode-map)
751 (run-hooks 'liece-nick-mode-hook))
753 (fset 'liece-dialogue-beep 'liece-command-beep)
754 (fset 'liece-dialogue-freeze 'liece-command-freeze)
755 (fset 'liece-dialogue-own-freeze 'liece-command-own-freeze)
757 (defun liece-initialize-buffers ()
758 "Initialize buffers."
759 (dolist (spec liece-buffer-mode-alist)
760 (let ((buffer (symbol-value (car spec)))
762 (or (get-buffer buffer)
764 (set-buffer (liece-get-buffer-create buffer))
765 (or (eq major-mode mode)
770 (defun liece-clear-system ()
771 "Clear all Liece variables and buffers."
773 (dolist (buffer liece-buffer-list)
774 (when (and (get-buffer buffer) (buffer-live-p buffer))
775 (funcall liece-buffer-dispose-function buffer)))
776 (if (vectorp liece-obarray)
777 (dotimes (i liece-obarray-size)
778 (aset liece-obarray i nil)))
779 (dolist (timer liece-timers)
781 (cancel-timer (caddr timer)))
783 (setcdr (cdr timer) nil)))
784 (setq liece-channel-buffer-alist nil
785 liece-nick-buffer-alist nil
786 liece-current-channels nil
787 liece-current-channel nil
788 liece-current-chat-partners nil
789 liece-current-chat-partner nil
790 liece-timers-list-initialized-p nil
791 liece-friends-last nil
793 liece-channel-indicator "No channel"))
795 (defun liece-wait-for-response (regexp &optional timeout)
796 "Wait for server response which match REGEXP.
797 Optional argument TIMEOUT specifies connection timeout."
799 (let ((status t) (wait t) (timeout (or timeout liece-connection-timeout)))
800 (set-buffer liece-server-buffer)
801 (with-timeout (timeout nil)
803 (liece-accept-response)
804 (goto-char (point-min))
805 (cond ((looking-at "ERROR") (setq status nil wait nil))
806 ((looking-at ".") (setq wait nil))))
807 ;; Save status message.
809 (setq liece-status-message-string
810 (buffer-substring (point-min) (point)))
813 (goto-char (point-max))
815 (if (looking-at regexp)
817 (liece-message (_ "Reading..."))
818 (liece-accept-response))))
819 ;; Successfully received server response.
822 (defun liece-accept-process-output (process &optional timeout)
823 "Wait for output from PROCESS and message some dots.
824 Optional argument TIMEOUT specifies connection timeout."
826 (set-buffer liece-server-buffer)
827 (accept-process-output process (or timeout 1))))
829 (defun liece-accept-response ()
830 "Read response of server. Only used at startup time."
831 (unless (liece-server-opened)
833 ((not liece-reconnect-automagic)
834 (error "Liece: Connection closed"))
836 (let ((liece-nickname (concat liece-nickname liece-grow-tail)))
840 (liece-accept-process-output liece-server-process)
842 (or (string-equal "select error: Invalid argument" (nth 1 code))
843 (signal (car code) (cdr code))))))
845 (defmacro liece-replace-internal (buffer match defstring oldstring newstring)
846 "Helper function only used from `liece-replace'.
848 Replace in buffer or list of buffers BUFFER with matching MATCH.
849 Argument DEFSTRING used when no matches are there.
850 Argument OLDSTRING is replaced with NEWSTRING."
852 (set-buffer (get-buffer ,buffer))
853 (let (buffer-read-only (inhibit-read-only t))
854 (goto-char (point-max))
855 (previous-line liece-compress-treshold)
857 (if (not (re-search-forward ,match nil t))
858 (liece-insert ,buffer ,defstring)
859 (while (re-search-forward ,match nil t))
861 (if (re-search-forward ,oldstring nil t)
862 (replace-match ,newstring nil t)
863 (liece-insert ,buffer ,defstring))
864 (liece-insert ,buffer ""))))))
867 (defun liece-replace (buffer match defstring oldstring newstring)
868 "Replace in buffer or list of buffers BUFFER with matching MATCH.
869 Argument DEFSTRING used when no matches are there.
870 Argument OLDSTRING is replaced with NEWSTRING."
871 (unless (listp buffer)
872 (setq buffer (list buffer)))
874 (when (get-buffer buf)
875 (liece-replace-internal buf match defstring oldstring newstring))))
877 (defun liece-check-buffers ()
878 "Check if there is a buffer larger than `liece-buffer-max-size'.
879 If such a buffer is found, shrink it."
880 (let ((liece-buffer-check-interval 0))
881 (when (> liece-buffer-max-size 0)
883 (dolist (buffer liece-channel-buffer-alist)
884 (set-buffer (cdr buffer))
885 (when (< liece-buffer-max-size (buffer-size))
886 (let ((inhibit-read-only t)
888 (delete-region (point-min)
890 (goto-char (- (buffer-size)
891 liece-buffer-default-size))
892 (beginning-of-line -1)
895 (setq liece-buffer-last-check-time (current-time)))))))))
897 (defun liece-check-buffers-if-interval-expired ()
898 "Timer handler for `liece-check-buffers'.
899 Only used from `liece-before-insert-functions'."
900 (and (> liece-buffer-check-interval 0)
901 (or (null liece-buffer-last-check-time)
902 (> (liece-time-difference liece-buffer-last-check-time
904 liece-buffer-check-interval))
905 (liece-check-buffers)))
907 (defun liece-refresh-buffer-window (buffer)
908 "Center point in window of BUFFER and redisplay frame."
909 (let ((window (liece-get-buffer-window buffer)))
910 (when (and window (not (pos-visible-in-window-p (point-max) window)))
911 (save-selected-window
912 (select-window window)
913 (goto-char (point-max))
914 (if (null liece-scroll-step)
915 (recenter (- (liece-window-height window) 1))
917 (- (or liece-scroll-step
918 (1+ (/ (liece-window-height window) 2)))
919 (liece-window-height window)))
920 (set-window-start window (point))
921 (goto-char (point-max)))))))
923 (defmacro liece-save-point (&rest body)
924 "Execute BODY, then goto the point that was around before BODY."
925 (let ((liece-save-point (liece-gensym "lsp")))
926 `(let ((,liece-save-point (point-marker)))
929 (goto-char ,liece-save-point)
930 (set-marker ,liece-save-point nil)))))
932 (defvar liece-before-insert-functions
933 '(liece-check-buffers-if-interval-expired
934 liece-command-timestamp-if-interval-expired))
936 (defun liece-insert-internal (buffer string)
937 "Helper function only used from `liece-insert'.
939 Insert before point of BUFFER STRING with decorating."
940 (run-hooks 'liece-before-insert-functions)
941 (with-current-buffer (liece-get-buffer-create buffer)
942 (or (eq (derived-mode-class major-mode) 'liece-dialogue-mode)
943 (liece-dialogue-mode))
945 (let ((inhibit-read-only t)
947 (from (goto-char (point-max))))
948 (unless (liece-is-message-ignored string (current-buffer))
949 (and liece-display-time (not (string-equal string ""))
950 (liece-insert-time-string))
952 (run-hook-with-args 'liece-after-insert-functions from (point)))))
953 (unless (liece-frozen (current-buffer))
954 (liece-refresh-buffer-window (current-buffer)))))
957 (defun liece-insert (buffer string)
958 "Insert before point of BUFFER STRING with decorating."
960 (setq buffer (list buffer)))
962 (when (get-buffer buf)
963 (liece-insert-internal buf string))))
967 ;;; liece.el ends here