* lisp/net/rcirc.el: New customizable nick completion format.
[emacs.git] / lisp / net / rcirc.el
blob678aba598b79c0509e80f67be04719678c1b34cf
1 ;;; rcirc.el --- default, simple IRC client.
3 ;; Copyright (C) 2005-2011 Free Software Foundation, Inc.
5 ;; Author: Ryan Yeske <rcyeske@gmail.com>
6 ;; Maintainers: Ryan Yeske <rcyeske@gmail.com>,
7 ;; Deniz Dogan <deniz.a.m.dogan@gmail.com>
8 ;; Keywords: comm
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Internet Relay Chat (IRC) is a form of instant communication over
28 ;; the Internet. It is mainly designed for group (many-to-many)
29 ;; communication in discussion forums called channels, but also allows
30 ;; one-to-one communication.
32 ;; Rcirc has simple defaults and clear and consistent behavior.
33 ;; Message arrival timestamps, activity notification on the modeline,
34 ;; message filling, nick completion, and keepalive pings are all
35 ;; enabled by default, but can easily be adjusted or turned off. Each
36 ;; discussion takes place in its own buffer and there is a single
37 ;; server buffer per connection.
39 ;; Open a new irc connection with:
40 ;; M-x irc RET
42 ;;; Todo:
44 ;;; Code:
46 (require 'ring)
47 (require 'time-date)
48 (eval-when-compile (require 'cl))
50 (defgroup rcirc nil
51 "Simple IRC client."
52 :version "22.1"
53 :prefix "rcirc-"
54 :link '(custom-manual "(rcirc)")
55 :group 'applications)
57 (defcustom rcirc-server-alist
58 '(("irc.freenode.net" :channels ("#rcirc")))
59 "An alist of IRC connections to establish when running `rcirc'.
60 Each element looks like (SERVER-NAME PARAMETERS).
62 SERVER-NAME is a string describing the server to connect
63 to.
65 The optional PARAMETERS come in pairs PARAMETER VALUE.
67 The following parameters are recognized:
69 `:nick'
71 VALUE must be a string. If absent, `rcirc-default-nick' is used
72 for this connection.
74 `:port'
76 VALUE must be a number or string. If absent,
77 `rcirc-default-port' is used.
79 `:user-name'
81 VALUE must be a string. If absent, `rcirc-default-user-name' is
82 used.
84 `:password'
86 VALUE must be a string. If absent, no PASS command will be sent
87 to the server.
89 `:full-name'
91 VALUE must be a string. If absent, `rcirc-default-full-name' is
92 used.
94 `:channels'
96 VALUE must be a list of strings describing which channels to join
97 when connecting to this server. If absent, no channels will be
98 connected to automatically."
99 :type '(alist :key-type string
100 :value-type (plist :options ((:nick string)
101 (:port integer)
102 (:user-name string)
103 (:password string)
104 (:full-name string)
105 (:channels (repeat string)))))
106 :group 'rcirc)
108 (defcustom rcirc-default-port 6667
109 "The default port to connect to."
110 :type 'integer
111 :group 'rcirc)
113 (defcustom rcirc-default-nick (user-login-name)
114 "Your nick."
115 :type 'string
116 :group 'rcirc)
118 (defcustom rcirc-default-user-name "user"
119 "Your user name sent to the server when connecting."
120 :version "24.1" ; changed default
121 :type 'string
122 :group 'rcirc)
124 (defcustom rcirc-default-full-name "unknown"
125 "The full name sent to the server when connecting."
126 :version "24.1" ; changed default
127 :type 'string
128 :group 'rcirc)
130 (defcustom rcirc-fill-flag t
131 "*Non-nil means line-wrap messages printed in channel buffers."
132 :type 'boolean
133 :group 'rcirc)
135 (defcustom rcirc-fill-column nil
136 "*Column beyond which automatic line-wrapping should happen.
137 If nil, use value of `fill-column'. If 'frame-width, use the
138 maximum frame width."
139 :type '(choice (const :tag "Value of `fill-column'")
140 (const :tag "Full frame width" frame-width)
141 (integer :tag "Number of columns"))
142 :group 'rcirc)
144 (defcustom rcirc-fill-prefix nil
145 "*Text to insert before filled lines.
146 If nil, calculate the prefix dynamically to line up text
147 underneath each nick."
148 :type '(choice (const :tag "Dynamic" nil)
149 (string :tag "Prefix text"))
150 :group 'rcirc)
152 (defvar rcirc-ignore-buffer-activity-flag nil
153 "If non-nil, ignore activity in this buffer.")
154 (make-variable-buffer-local 'rcirc-ignore-buffer-activity-flag)
156 (defvar rcirc-low-priority-flag nil
157 "If non-nil, activity in this buffer is considered low priority.")
158 (make-variable-buffer-local 'rcirc-low-priority-flag)
160 (defvar rcirc-omit-mode nil
161 "Non-nil if Rcirc-Omit mode is enabled.
162 Use the command `rcirc-omit-mode' to change this variable.")
163 (make-variable-buffer-local 'rcirc-omit-mode)
165 (defcustom rcirc-time-format "%H:%M "
166 "*Describes how timestamps are printed.
167 Used as the first arg to `format-time-string'."
168 :type 'string
169 :group 'rcirc)
171 (defcustom rcirc-input-ring-size 1024
172 "*Size of input history ring."
173 :type 'integer
174 :group 'rcirc)
176 (defcustom rcirc-read-only-flag t
177 "*Non-nil means make text in IRC buffers read-only."
178 :type 'boolean
179 :group 'rcirc)
181 (defcustom rcirc-buffer-maximum-lines nil
182 "*The maximum size in lines for rcirc buffers.
183 Channel buffers are truncated from the top to be no greater than this
184 number. If zero or nil, no truncating is done."
185 :type '(choice (const :tag "No truncation" nil)
186 (integer :tag "Number of lines"))
187 :group 'rcirc)
189 (defcustom rcirc-scroll-show-maximum-output t
190 "*If non-nil, scroll buffer to keep the point at the bottom of
191 the window."
192 :type 'boolean
193 :group 'rcirc)
195 (defcustom rcirc-authinfo nil
196 "List of authentication passwords.
197 Each element of the list is a list with a SERVER-REGEXP string
198 and a method symbol followed by method specific arguments.
200 The valid METHOD symbols are `nickserv', `chanserv' and
201 `bitlbee'.
203 The ARGUMENTS for each METHOD symbol are:
204 `nickserv': NICK PASSWORD [NICKSERV-NICK]
205 `chanserv': NICK CHANNEL PASSWORD
206 `bitlbee': NICK PASSWORD
208 Examples:
209 ((\"freenode\" nickserv \"bob\" \"p455w0rd\")
210 (\"freenode\" chanserv \"bob\" \"#bobland\" \"passwd99\")
211 (\"bitlbee\" bitlbee \"robert\" \"sekrit\")
212 (\"dal.net\" nickserv \"bob\" \"sekrit\" \"NickServ@services.dal.net\"))"
213 :type '(alist :key-type (string :tag "Server")
214 :value-type (choice (list :tag "NickServ"
215 (const nickserv)
216 (string :tag "Nick")
217 (string :tag "Password"))
218 (list :tag "ChanServ"
219 (const chanserv)
220 (string :tag "Nick")
221 (string :tag "Channel")
222 (string :tag "Password"))
223 (list :tag "BitlBee"
224 (const bitlbee)
225 (string :tag "Nick")
226 (string :tag "Password"))))
227 :group 'rcirc)
229 (defcustom rcirc-auto-authenticate-flag t
230 "*Non-nil means automatically send authentication string to server.
231 See also `rcirc-authinfo'."
232 :type 'boolean
233 :group 'rcirc)
235 (defcustom rcirc-prompt "> "
236 "Prompt string to use in IRC buffers.
238 The following replacements are made:
239 %n is your nick.
240 %s is the server.
241 %t is the buffer target, a channel or a user.
243 Setting this alone will not affect the prompt;
244 use either M-x customize or also call `rcirc-update-prompt'."
245 :type 'string
246 :set 'rcirc-set-changed
247 :initialize 'custom-initialize-default
248 :group 'rcirc)
250 (defcustom rcirc-keywords nil
251 "List of keywords to highlight in message text."
252 :type '(repeat string)
253 :group 'rcirc)
255 (defcustom rcirc-ignore-list ()
256 "List of ignored nicks.
257 Use /ignore to list them, use /ignore NICK to add or remove a nick."
258 :type '(repeat string)
259 :group 'rcirc)
261 (defvar rcirc-ignore-list-automatic ()
262 "List of ignored nicks added to `rcirc-ignore-list' because of renaming.
263 When an ignored person renames, their nick is added to both lists.
264 Nicks will be removed from the automatic list on follow-up renamings or
265 parts.")
267 (defcustom rcirc-bright-nicks nil
268 "List of nicks to be emphasized.
269 See `rcirc-bright-nick' face."
270 :type '(repeat string)
271 :group 'rcirc)
273 (defcustom rcirc-dim-nicks nil
274 "List of nicks to be deemphasized.
275 See `rcirc-dim-nick' face."
276 :type '(repeat string)
277 :group 'rcirc)
279 (defcustom rcirc-print-hooks nil
280 "Hook run after text is printed.
281 Called with 5 arguments, PROCESS, SENDER, RESPONSE, TARGET and TEXT."
282 :type 'hook
283 :group 'rcirc)
285 (defcustom rcirc-always-use-server-buffer-flag nil
286 "Non-nil means messages without a channel target will go to the server buffer."
287 :type 'boolean
288 :group 'rcirc)
290 (defcustom rcirc-decode-coding-system 'utf-8
291 "Coding system used to decode incoming irc messages."
292 :type 'coding-system
293 :group 'rcirc)
295 (defcustom rcirc-encode-coding-system 'utf-8
296 "Coding system used to encode outgoing irc messages."
297 :type 'coding-system
298 :group 'rcirc)
300 (defcustom rcirc-coding-system-alist nil
301 "Alist to decide a coding system to use for a channel I/O operation.
302 The format is ((PATTERN . VAL) ...).
303 PATTERN is either a string or a cons of strings.
304 If PATTERN is a string, it is used to match a target.
305 If PATTERN is a cons of strings, the car part is used to match a
306 target, and the cdr part is used to match a server.
307 VAL is either a coding system or a cons of coding systems.
308 If VAL is a coding system, it is used for both decoding and encoding
309 messages.
310 If VAL is a cons of coding systems, the car part is used for decoding,
311 and the cdr part is used for encoding."
312 :type '(alist :key-type (choice (string :tag "Channel Regexp")
313 (cons (string :tag "Channel Regexp")
314 (string :tag "Server Regexp")))
315 :value-type (choice coding-system
316 (cons (coding-system :tag "Decode")
317 (coding-system :tag "Encode"))))
318 :group 'rcirc)
320 (defcustom rcirc-multiline-major-mode 'fundamental-mode
321 "Major-mode function to use in multiline edit buffers."
322 :type 'function
323 :group 'rcirc)
325 (defcustom rcirc-nick-completion-format "%s: "
326 "Format string to use in nick completions.
328 The format string is only used when completing at the beginning
329 of a line. The string is passed as the first argument to
330 `format' with the nickname as the second argument."
331 :type 'string
332 :group 'rcirc)
334 (defvar rcirc-nick nil)
336 (defvar rcirc-prompt-start-marker nil)
337 (defvar rcirc-prompt-end-marker nil)
339 (defvar rcirc-nick-table nil)
341 (defvar rcirc-recent-quit-alist nil
342 "Alist of nicks that have recently quit or parted the channel.")
344 (defvar rcirc-nick-syntax-table
345 (let ((table (make-syntax-table text-mode-syntax-table)))
346 (mapc (lambda (c) (modify-syntax-entry c "w" table))
347 "[]\\`_^{|}-")
348 (modify-syntax-entry ?' "_" table)
349 table)
350 "Syntax table which includes all nick characters as word constituents.")
352 ;; each process has an alist of (target . buffer) pairs
353 (defvar rcirc-buffer-alist nil)
355 (defvar rcirc-activity nil
356 "List of buffers with unviewed activity.")
358 (defvar rcirc-activity-string ""
359 "String displayed in modeline representing `rcirc-activity'.")
360 (put 'rcirc-activity-string 'risky-local-variable t)
362 (defvar rcirc-server-buffer nil
363 "The server buffer associated with this channel buffer.")
365 (defvar rcirc-target nil
366 "The channel or user associated with this buffer.")
368 (defvar rcirc-urls nil
369 "List of urls seen in the current buffer.")
370 (put 'rcirc-urls 'permanent-local t)
372 (defvar rcirc-timeout-seconds 600
373 "Kill connection after this many seconds if there is no activity.")
375 (defconst rcirc-id-string (concat "rcirc on GNU Emacs " emacs-version))
377 (defvar rcirc-startup-channels nil)
379 (defvar rcirc-server-name-history nil
380 "History variable for \\[rcirc] call.")
382 (defvar rcirc-server-port-history nil
383 "History variable for \\[rcirc] call.")
385 (defvar rcirc-nick-name-history nil
386 "History variable for \\[rcirc] call.")
388 (defvar rcirc-user-name-history nil
389 "History variable for \\[rcirc] call.")
391 ;;;###autoload
392 (defun rcirc (arg)
393 "Connect to all servers in `rcirc-server-alist'.
395 Do not connect to a server if it is already connected.
397 If ARG is non-nil, instead prompt for connection parameters."
398 (interactive "P")
399 (if arg
400 (let* ((server (completing-read "IRC Server: "
401 rcirc-server-alist
402 nil nil
403 (caar rcirc-server-alist)
404 'rcirc-server-name-history))
405 (server-plist (cdr (assoc-string server rcirc-server-alist)))
406 (port (read-string "IRC Port: "
407 (number-to-string
408 (or (plist-get server-plist :port)
409 rcirc-default-port))
410 'rcirc-server-port-history))
411 (nick (read-string "IRC Nick: "
412 (or (plist-get server-plist :nick)
413 rcirc-default-nick)
414 'rcirc-nick-name-history))
415 (user-name (read-string "IRC Username: "
416 (or (plist-get server-plist :user-name)
417 rcirc-default-user-name)
418 'rcirc-user-name-history))
419 (password (read-passwd "IRC Password: " nil
420 (plist-get server-plist :password)))
421 (channels (split-string
422 (read-string "IRC Channels: "
423 (mapconcat 'identity
424 (plist-get server-plist
425 :channels)
426 " "))
427 "[, ]+" t)))
428 (rcirc-connect server port nick user-name
429 rcirc-default-full-name
430 channels password))
431 ;; connect to servers in `rcirc-server-alist'
432 (let (connected-servers)
433 (dolist (c rcirc-server-alist)
434 (let ((server (car c))
435 (nick (or (plist-get (cdr c) :nick) rcirc-default-nick))
436 (port (or (plist-get (cdr c) :port) rcirc-default-port))
437 (user-name (or (plist-get (cdr c) :user-name)
438 rcirc-default-user-name))
439 (full-name (or (plist-get (cdr c) :full-name)
440 rcirc-default-full-name))
441 (channels (plist-get (cdr c) :channels))
442 (password (plist-get (cdr c) :password)))
443 (when server
444 (let (connected)
445 (dolist (p (rcirc-process-list))
446 (when (string= server (process-name p))
447 (setq connected p)))
448 (if (not connected)
449 (condition-case e
450 (rcirc-connect server port nick user-name
451 full-name channels password)
452 (quit (message "Quit connecting to %s" server)))
453 (with-current-buffer (process-buffer connected)
454 (setq connected-servers
455 (cons (process-contact (get-buffer-process
456 (current-buffer)) :host)
457 connected-servers))))))))
458 (when connected-servers
459 (message "Already connected to %s"
460 (if (cdr connected-servers)
461 (concat (mapconcat 'identity (butlast connected-servers) ", ")
462 ", and "
463 (car (last connected-servers)))
464 (car connected-servers)))))))
466 ;;;###autoload
467 (defalias 'irc 'rcirc)
470 (defvar rcirc-process-output nil)
471 (defvar rcirc-topic nil)
472 (defvar rcirc-keepalive-timer nil)
473 (defvar rcirc-last-server-message-time nil)
474 (defvar rcirc-server nil) ; server provided by server
475 (defvar rcirc-server-name nil) ; server name given by 001 response
476 (defvar rcirc-timeout-timer nil)
477 (defvar rcirc-user-disconnect nil)
478 (defvar rcirc-connecting nil)
479 (defvar rcirc-process nil)
481 ;;;###autoload
482 (defun rcirc-connect (server &optional port nick user-name
483 full-name startup-channels password)
484 (save-excursion
485 (message "Connecting to %s..." server)
486 (let* ((inhibit-eol-conversion)
487 (port-number (if port
488 (if (stringp port)
489 (string-to-number port)
490 port)
491 rcirc-default-port))
492 (nick (or nick rcirc-default-nick))
493 (user-name (or user-name rcirc-default-user-name))
494 (full-name (or full-name rcirc-default-full-name))
495 (startup-channels startup-channels)
496 (process (make-network-process :name server :host server :service port-number)))
497 ;; set up process
498 (set-process-coding-system process 'raw-text 'raw-text)
499 (switch-to-buffer (rcirc-generate-new-buffer-name process nil))
500 (set-process-buffer process (current-buffer))
501 (rcirc-mode process nil)
502 (set-process-sentinel process 'rcirc-sentinel)
503 (set-process-filter process 'rcirc-filter)
504 (make-local-variable 'rcirc-process)
505 (setq rcirc-process process)
506 (make-local-variable 'rcirc-server)
507 (setq rcirc-server server)
508 (make-local-variable 'rcirc-server-name)
509 (setq rcirc-server-name server) ; update when we get 001 response
510 (make-local-variable 'rcirc-buffer-alist)
511 (setq rcirc-buffer-alist nil)
512 (make-local-variable 'rcirc-nick-table)
513 (setq rcirc-nick-table (make-hash-table :test 'equal))
514 (make-local-variable 'rcirc-nick)
515 (setq rcirc-nick nick)
516 (make-local-variable 'rcirc-process-output)
517 (setq rcirc-process-output nil)
518 (make-local-variable 'rcirc-startup-channels)
519 (setq rcirc-startup-channels startup-channels)
520 (make-local-variable 'rcirc-last-server-message-time)
521 (setq rcirc-last-server-message-time (current-time))
522 (make-local-variable 'rcirc-timeout-timer)
523 (setq rcirc-timeout-timer nil)
524 (make-local-variable 'rcirc-user-disconnect)
525 (setq rcirc-user-disconnect nil)
526 (make-local-variable 'rcirc-connecting)
527 (setq rcirc-connecting t)
529 (add-hook 'auto-save-hook 'rcirc-log-write)
531 ;; identify
532 (when password
533 (rcirc-send-string process (concat "PASS " password)))
534 (rcirc-send-string process (concat "NICK " nick))
535 (rcirc-send-string process (concat "USER " user-name
536 " 0 * :" full-name))
538 ;; setup ping timer if necessary
539 (unless rcirc-keepalive-timer
540 (setq rcirc-keepalive-timer
541 (run-at-time 0 (/ rcirc-timeout-seconds 2) 'rcirc-keepalive)))
543 (message "Connecting to %s...done" server)
545 ;; return process object
546 process)))
548 (defmacro with-rcirc-process-buffer (process &rest body)
549 (declare (indent 1) (debug t))
550 `(with-current-buffer (process-buffer ,process)
551 ,@body))
553 (defmacro with-rcirc-server-buffer (&rest body)
554 (declare (indent 0) (debug t))
555 `(with-current-buffer rcirc-server-buffer
556 ,@body))
558 (defun rcirc-keepalive ()
559 "Send keep alive pings to active rcirc processes.
560 Kill processes that have not received a server message since the
561 last ping."
562 (if (rcirc-process-list)
563 (mapc (lambda (process)
564 (with-rcirc-process-buffer process
565 (when (not rcirc-connecting)
566 (rcirc-send-string process
567 (format "PRIVMSG %s :\C-aKEEPALIVE %f\C-a"
568 rcirc-nick
569 (if (featurep 'xemacs)
570 (time-to-seconds
571 (current-time))
572 (float-time)))))))
573 (rcirc-process-list))
574 ;; no processes, clean up timer
575 (cancel-timer rcirc-keepalive-timer)
576 (setq rcirc-keepalive-timer nil)))
578 (defun rcirc-handler-ctcp-KEEPALIVE (process target sender message)
579 (with-rcirc-process-buffer process
580 (setq header-line-format (format "%f" (- (if (featurep 'xemacs)
581 (time-to-seconds
582 (current-time))
583 (float-time))
584 (string-to-number message))))))
586 (defvar rcirc-debug-buffer " *rcirc debug*")
587 (defvar rcirc-debug-flag nil
588 "If non-nil, write information to `rcirc-debug-buffer'.")
589 (defun rcirc-debug (process text)
590 "Add an entry to the debug log including PROCESS and TEXT.
591 Debug text is written to `rcirc-debug-buffer' if `rcirc-debug-flag'
592 is non-nil."
593 (when rcirc-debug-flag
594 (with-current-buffer (get-buffer-create rcirc-debug-buffer)
595 (goto-char (point-max))
596 (insert (concat
598 (format-time-string "%Y-%m-%dT%T ") (process-name process)
599 "] "
600 text)))))
602 (defvar rcirc-sentinel-hooks nil
603 "Hook functions called when the process sentinel is called.
604 Functions are called with PROCESS and SENTINEL arguments.")
606 (defun rcirc-sentinel (process sentinel)
607 "Called when PROCESS receives SENTINEL."
608 (let ((sentinel (replace-regexp-in-string "\n" "" sentinel)))
609 (rcirc-debug process (format "SENTINEL: %S %S\n" process sentinel))
610 (with-rcirc-process-buffer process
611 (dolist (buffer (cons nil (mapcar 'cdr rcirc-buffer-alist)))
612 (with-current-buffer (or buffer (current-buffer))
613 (rcirc-print process "rcirc.el" "ERROR" rcirc-target
614 (format "%s: %s (%S)"
615 (process-name process)
616 sentinel
617 (process-status process)) (not rcirc-target))
618 (rcirc-disconnect-buffer)))
619 (run-hook-with-args 'rcirc-sentinel-hooks process sentinel))))
621 (defun rcirc-disconnect-buffer (&optional buffer)
622 (with-current-buffer (or buffer (current-buffer))
623 ;; set rcirc-target to nil for each channel so cleanup
624 ;; doesnt happen when we reconnect
625 (setq rcirc-target nil)
626 (setq mode-line-process ":disconnected")))
628 (defun rcirc-process-list ()
629 "Return a list of rcirc processes."
630 (let (ps)
631 (mapc (lambda (p)
632 (when (buffer-live-p (process-buffer p))
633 (with-rcirc-process-buffer p
634 (when (eq major-mode 'rcirc-mode)
635 (setq ps (cons p ps))))))
636 (process-list))
637 ps))
639 (defvar rcirc-receive-message-hooks nil
640 "Hook functions run when a message is received from server.
641 Function is called with PROCESS, COMMAND, SENDER, ARGS and LINE.")
642 (defun rcirc-filter (process output)
643 "Called when PROCESS receives OUTPUT."
644 (rcirc-debug process output)
645 (rcirc-reschedule-timeout process)
646 (with-rcirc-process-buffer process
647 (setq rcirc-last-server-message-time (current-time))
648 (setq rcirc-process-output (concat rcirc-process-output output))
649 (when (= (aref rcirc-process-output
650 (1- (length rcirc-process-output))) ?\n)
651 (mapc (lambda (line)
652 (rcirc-process-server-response process line))
653 (split-string rcirc-process-output "[\n\r]" t))
654 (setq rcirc-process-output nil))))
656 (defun rcirc-reschedule-timeout (process)
657 (with-rcirc-process-buffer process
658 (when (not rcirc-connecting)
659 (with-rcirc-process-buffer process
660 (when rcirc-timeout-timer (cancel-timer rcirc-timeout-timer))
661 (setq rcirc-timeout-timer (run-at-time rcirc-timeout-seconds nil
662 'rcirc-delete-process
663 process))))))
665 (defun rcirc-delete-process (process)
666 (delete-process process))
668 (defvar rcirc-trap-errors-flag t)
669 (defun rcirc-process-server-response (process text)
670 (if rcirc-trap-errors-flag
671 (condition-case err
672 (rcirc-process-server-response-1 process text)
673 (error
674 (rcirc-print process "RCIRC" "ERROR" nil
675 (format "\"%s\" %s" text err) t)))
676 (rcirc-process-server-response-1 process text)))
678 (defun rcirc-process-server-response-1 (process text)
679 (if (string-match "^\\(:\\([^ ]+\\) \\)?\\([^ ]+\\) \\(.+\\)$" text)
680 (let* ((user (match-string 2 text))
681 (sender (rcirc-user-nick user))
682 (cmd (match-string 3 text))
683 (args (match-string 4 text))
684 (handler (intern-soft (concat "rcirc-handler-" cmd))))
685 (string-match "^\\([^:]*\\):?\\(.+\\)?$" args)
686 (let* ((args1 (match-string 1 args))
687 (args2 (match-string 2 args))
688 (args (delq nil (append (split-string args1 " " t)
689 (list args2)))))
690 (if (not (fboundp handler))
691 (rcirc-handler-generic process cmd sender args text)
692 (funcall handler process sender args text))
693 (run-hook-with-args 'rcirc-receive-message-hooks
694 process cmd sender args text)))
695 (message "UNHANDLED: %s" text)))
697 (defvar rcirc-responses-no-activity '("305" "306")
698 "Responses that don't trigger activity in the mode-line indicator.")
700 (defun rcirc-handler-generic (process response sender args text)
701 "Generic server response handler."
702 (rcirc-print process sender response nil
703 (mapconcat 'identity (cdr args) " ")
704 (not (member response rcirc-responses-no-activity))))
706 (defun rcirc-send-string (process string)
707 "Send PROCESS a STRING plus a newline."
708 (let ((string (concat (encode-coding-string string rcirc-encode-coding-system)
709 "\n")))
710 (unless (eq (process-status process) 'open)
711 (error "Network connection to %s is not open"
712 (process-name process)))
713 (rcirc-debug process string)
714 (process-send-string process string)))
716 (defun rcirc-buffer-process (&optional buffer)
717 "Return the process associated with channel BUFFER.
718 With no argument or nil as argument, use the current buffer."
719 (or (get-buffer-process (if buffer
720 (with-current-buffer buffer
721 rcirc-server-buffer)
722 rcirc-server-buffer))
723 rcirc-process))
725 (defun rcirc-server-name (process)
726 "Return PROCESS server name, given by the 001 response."
727 (with-rcirc-process-buffer process
728 (or rcirc-server-name
729 (warn "server name for process %S unknown" process))))
731 (defun rcirc-nick (process)
732 "Return PROCESS nick."
733 (with-rcirc-process-buffer process
734 (or rcirc-nick rcirc-default-nick)))
736 (defun rcirc-buffer-nick (&optional buffer)
737 "Return the nick associated with BUFFER.
738 With no argument or nil as argument, use the current buffer."
739 (with-current-buffer (or buffer (current-buffer))
740 (with-current-buffer rcirc-server-buffer
741 (or rcirc-nick rcirc-default-nick))))
743 (defvar rcirc-max-message-length 420
744 "Messages longer than this value will be split.")
746 (defun rcirc-send-message (process target message &optional noticep silent)
747 "Send TARGET associated with PROCESS a privmsg with text MESSAGE.
748 If NOTICEP is non-nil, send a notice instead of privmsg.
749 If SILENT is non-nil, do not print the message in any irc buffer."
750 ;; max message length is 512 including CRLF
751 (let* ((response (if noticep "NOTICE" "PRIVMSG"))
752 (oversize (> (length message) rcirc-max-message-length))
753 (text (if oversize
754 (substring message 0 rcirc-max-message-length)
755 message))
756 (text (if (string= text "")
758 text))
759 (more (if oversize
760 (substring message rcirc-max-message-length))))
761 (rcirc-get-buffer-create process target)
762 (rcirc-send-string process (concat response " " target " :" text))
763 (unless silent
764 (rcirc-print process (rcirc-nick process) response target text))
765 (when more (rcirc-send-message process target more noticep))))
767 (defvar rcirc-input-ring nil)
768 (defvar rcirc-input-ring-index 0)
769 (defun rcirc-prev-input-string (arg)
770 (ring-ref rcirc-input-ring (+ rcirc-input-ring-index arg)))
772 (defun rcirc-insert-prev-input (arg)
773 (interactive "p")
774 (when (<= rcirc-prompt-end-marker (point))
775 (delete-region rcirc-prompt-end-marker (point-max))
776 (insert (rcirc-prev-input-string 0))
777 (setq rcirc-input-ring-index (1+ rcirc-input-ring-index))))
779 (defun rcirc-insert-next-input (arg)
780 (interactive "p")
781 (when (<= rcirc-prompt-end-marker (point))
782 (delete-region rcirc-prompt-end-marker (point-max))
783 (setq rcirc-input-ring-index (1- rcirc-input-ring-index))
784 (insert (rcirc-prev-input-string -1))))
786 (defvar rcirc-server-commands
787 '("/admin" "/away" "/connect" "/die" "/error" "/info"
788 "/invite" "/ison" "/join" "/kick" "/kill" "/links"
789 "/list" "/lusers" "/mode" "/motd" "/names" "/nick"
790 "/notice" "/oper" "/part" "/pass" "/ping" "/pong"
791 "/privmsg" "/quit" "/rehash" "/restart" "/service" "/servlist"
792 "/server" "/squery" "/squit" "/stats" "/summon" "/time"
793 "/topic" "/trace" "/user" "/userhost" "/users" "/version"
794 "/wallops" "/who" "/whois" "/whowas")
795 "A list of user commands by IRC server.
796 The value defaults to RFCs 1459 and 2812.")
798 ;; /me and /ctcp are not defined by `defun-rcirc-command'.
799 (defvar rcirc-client-commands '("/me" "/ctcp")
800 "A list of user commands defined by IRC client rcirc.
801 The list is updated automatically by `defun-rcirc-command'.")
803 (defun rcirc-completion-at-point ()
804 "Function used for `completion-at-point-functions' in `rcirc-mode'."
805 (let* ((beg (save-excursion
806 (if (re-search-backward " " rcirc-prompt-end-marker t)
807 (1+ (point))
808 rcirc-prompt-end-marker)))
809 (table (if (and (= beg rcirc-prompt-end-marker)
810 (eq (char-after beg) ?/))
811 (delete-dups
812 (nconc
813 (sort (copy-sequence rcirc-client-commands) 'string-lessp)
814 (sort (copy-sequence rcirc-server-commands) 'string-lessp)))
815 (rcirc-channel-nicks (rcirc-buffer-process) rcirc-target))))
816 (list beg (point) table)))
818 (defvar rcirc-completions nil)
819 (defvar rcirc-completion-start nil)
821 (defun rcirc-complete ()
822 "Cycle through completions from list of nicks in channel or IRC commands.
823 IRC command completion is performed only if '/' is the first input char."
824 (interactive)
825 (if (eq last-command this-command)
826 (setq rcirc-completions
827 (append (cdr rcirc-completions) (list (car rcirc-completions))))
828 (let ((completion-ignore-case t)
829 (table (rcirc-completion-at-point)))
830 (setq rcirc-completion-start (car table))
831 (setq rcirc-completions
832 (all-completions (buffer-substring rcirc-completion-start
833 (cadr table))
834 (nth 2 table)))))
835 (let ((completion (car rcirc-completions)))
836 (when completion
837 (delete-region rcirc-completion-start (point))
838 (insert
839 (cond
840 ((= (aref completion 0) ?/) (concat completion " "))
841 ((= rcirc-completion-start rcirc-prompt-end-marker)
842 (format rcirc-nick-completion-format completion))
843 (t completion))))))
845 (defun set-rcirc-decode-coding-system (coding-system)
846 "Set the decode coding system used in this channel."
847 (interactive "zCoding system for incoming messages: ")
848 (setq rcirc-decode-coding-system coding-system))
850 (defun set-rcirc-encode-coding-system (coding-system)
851 "Set the encode coding system used in this channel."
852 (interactive "zCoding system for outgoing messages: ")
853 (setq rcirc-encode-coding-system coding-system))
855 (defvar rcirc-mode-map (make-sparse-keymap)
856 "Keymap for rcirc mode.")
858 (define-key rcirc-mode-map (kbd "RET") 'rcirc-send-input)
859 (define-key rcirc-mode-map (kbd "M-p") 'rcirc-insert-prev-input)
860 (define-key rcirc-mode-map (kbd "M-n") 'rcirc-insert-next-input)
861 (define-key rcirc-mode-map (kbd "TAB") 'rcirc-complete)
862 (define-key rcirc-mode-map (kbd "C-c C-b") 'rcirc-browse-url)
863 (define-key rcirc-mode-map (kbd "C-c C-c") 'rcirc-edit-multiline)
864 (define-key rcirc-mode-map (kbd "C-c C-j") 'rcirc-cmd-join)
865 (define-key rcirc-mode-map (kbd "C-c C-k") 'rcirc-cmd-kick)
866 (define-key rcirc-mode-map (kbd "C-c C-l") 'rcirc-toggle-low-priority)
867 (define-key rcirc-mode-map (kbd "C-c C-d") 'rcirc-cmd-mode)
868 (define-key rcirc-mode-map (kbd "C-c C-m") 'rcirc-cmd-msg)
869 (define-key rcirc-mode-map (kbd "C-c C-r") 'rcirc-cmd-nick) ; rename
870 (define-key rcirc-mode-map (kbd "C-c C-o") 'rcirc-omit-mode)
871 (define-key rcirc-mode-map (kbd "M-o") 'rcirc-omit-mode)
872 (define-key rcirc-mode-map (kbd "C-c C-p") 'rcirc-cmd-part)
873 (define-key rcirc-mode-map (kbd "C-c C-q") 'rcirc-cmd-query)
874 (define-key rcirc-mode-map (kbd "C-c C-t") 'rcirc-cmd-topic)
875 (define-key rcirc-mode-map (kbd "C-c C-n") 'rcirc-cmd-names)
876 (define-key rcirc-mode-map (kbd "C-c C-w") 'rcirc-cmd-whois)
877 (define-key rcirc-mode-map (kbd "C-c C-x") 'rcirc-cmd-quit)
878 (define-key rcirc-mode-map (kbd "C-c TAB") ; C-i
879 'rcirc-toggle-ignore-buffer-activity)
880 (define-key rcirc-mode-map (kbd "C-c C-s") 'rcirc-switch-to-server-buffer)
881 (define-key rcirc-mode-map (kbd "C-c C-a") 'rcirc-jump-to-first-unread-line)
883 (defvar rcirc-browse-url-map (make-sparse-keymap)
884 "Keymap used for browsing URLs in `rcirc-mode'.")
886 (define-key rcirc-browse-url-map (kbd "RET") 'rcirc-browse-url-at-point)
887 (define-key rcirc-browse-url-map (kbd "<mouse-2>") 'rcirc-browse-url-at-mouse)
888 (define-key rcirc-browse-url-map [follow-link] 'mouse-face)
890 (defvar rcirc-short-buffer-name nil
891 "Generated abbreviation to use to indicate buffer activity.")
893 (defvar rcirc-mode-hook nil
894 "Hook run when setting up rcirc buffer.")
896 (defvar rcirc-last-post-time nil)
898 (defvar rcirc-log-alist nil
899 "Alist of lines to log to disk when `rcirc-log-flag' is non-nil.
900 Each element looks like (FILENAME . TEXT).")
902 (defvar rcirc-current-line 0
903 "The current number of responses printed in this channel.
904 This number is independent of the number of lines in the buffer.")
906 (defun rcirc-mode (process target)
907 ;; FIXME: Use define-derived-mode.
908 "Major mode for IRC channel buffers.
910 \\{rcirc-mode-map}"
911 (kill-all-local-variables)
912 (use-local-map rcirc-mode-map)
913 (setq mode-name "rcirc")
914 (setq major-mode 'rcirc-mode)
915 (setq mode-line-process nil)
917 (make-local-variable 'rcirc-input-ring)
918 (setq rcirc-input-ring (make-ring rcirc-input-ring-size))
919 (make-local-variable 'rcirc-server-buffer)
920 (setq rcirc-server-buffer (process-buffer process))
921 (make-local-variable 'rcirc-target)
922 (setq rcirc-target target)
923 (make-local-variable 'rcirc-topic)
924 (setq rcirc-topic nil)
925 (make-local-variable 'rcirc-last-post-time)
926 (setq rcirc-last-post-time (current-time))
927 (make-local-variable 'fill-paragraph-function)
928 (setq fill-paragraph-function 'rcirc-fill-paragraph)
929 (make-local-variable 'rcirc-recent-quit-alist)
930 (setq rcirc-recent-quit-alist nil)
931 (make-local-variable 'rcirc-current-line)
932 (setq rcirc-current-line 0)
934 (make-local-variable 'rcirc-short-buffer-name)
935 (setq rcirc-short-buffer-name nil)
936 (make-local-variable 'rcirc-urls)
937 (setq use-hard-newlines t)
939 ;; setup for omitting responses
940 (setq buffer-invisibility-spec '())
941 (setq buffer-display-table (make-display-table))
942 (set-display-table-slot buffer-display-table 4
943 (let ((glyph (make-glyph-code
944 ?. 'font-lock-keyword-face)))
945 (make-vector 3 glyph)))
947 (make-local-variable 'rcirc-decode-coding-system)
948 (make-local-variable 'rcirc-encode-coding-system)
949 (dolist (i rcirc-coding-system-alist)
950 (let ((chan (if (consp (car i)) (caar i) (car i)))
951 (serv (if (consp (car i)) (cdar i) "")))
952 (when (and (string-match chan (or target ""))
953 (string-match serv (rcirc-server-name process)))
954 (setq rcirc-decode-coding-system (if (consp (cdr i)) (cadr i) (cdr i))
955 rcirc-encode-coding-system (if (consp (cdr i)) (cddr i) (cdr i))))))
957 ;; setup the prompt and markers
958 (make-local-variable 'rcirc-prompt-start-marker)
959 (setq rcirc-prompt-start-marker (make-marker))
960 (set-marker rcirc-prompt-start-marker (point-max))
961 (make-local-variable 'rcirc-prompt-end-marker)
962 (setq rcirc-prompt-end-marker (make-marker))
963 (set-marker rcirc-prompt-end-marker (point-max))
964 (rcirc-update-prompt)
965 (goto-char rcirc-prompt-end-marker)
966 (make-local-variable 'overlay-arrow-position)
967 (setq overlay-arrow-position (make-marker))
968 (set-marker overlay-arrow-position nil)
970 ;; if the user changes the major mode or kills the buffer, there is
971 ;; cleanup work to do
972 (add-hook 'change-major-mode-hook 'rcirc-change-major-mode-hook nil t)
973 (add-hook 'kill-buffer-hook 'rcirc-kill-buffer-hook nil t)
975 ;; add to buffer list, and update buffer abbrevs
976 (when target ; skip server buffer
977 (let ((buffer (current-buffer)))
978 (with-rcirc-process-buffer process
979 (setq rcirc-buffer-alist (cons (cons target buffer)
980 rcirc-buffer-alist))))
981 (rcirc-update-short-buffer-names))
983 (add-hook 'completion-at-point-functions
984 'rcirc-completion-at-point nil 'local)
986 (run-mode-hooks 'rcirc-mode-hook))
988 (defun rcirc-update-prompt (&optional all)
989 "Reset the prompt string in the current buffer.
991 If ALL is non-nil, update prompts in all IRC buffers."
992 (if all
993 (mapc (lambda (process)
994 (mapc (lambda (buffer)
995 (with-current-buffer buffer
996 (rcirc-update-prompt)))
997 (with-rcirc-process-buffer process
998 (mapcar 'cdr rcirc-buffer-alist))))
999 (rcirc-process-list))
1000 (let ((inhibit-read-only t)
1001 (prompt (or rcirc-prompt "")))
1002 (mapc (lambda (rep)
1003 (setq prompt
1004 (replace-regexp-in-string (car rep) (cdr rep) prompt)))
1005 (list (cons "%n" (rcirc-buffer-nick))
1006 (cons "%s" (with-rcirc-server-buffer rcirc-server-name))
1007 (cons "%t" (or rcirc-target ""))))
1008 (save-excursion
1009 (delete-region rcirc-prompt-start-marker rcirc-prompt-end-marker)
1010 (goto-char rcirc-prompt-start-marker)
1011 (let ((start (point)))
1012 (insert-before-markers prompt)
1013 (set-marker rcirc-prompt-start-marker start)
1014 (when (not (zerop (- rcirc-prompt-end-marker
1015 rcirc-prompt-start-marker)))
1016 (add-text-properties rcirc-prompt-start-marker
1017 rcirc-prompt-end-marker
1018 (list 'face 'rcirc-prompt
1019 'read-only t 'field t
1020 'front-sticky t 'rear-nonsticky t))))))))
1022 (defun rcirc-set-changed (option value)
1023 "Set OPTION to VALUE and do updates after a customization change."
1024 (set-default option value)
1025 (cond ((eq option 'rcirc-prompt)
1026 (rcirc-update-prompt 'all))
1028 (error "Bad option %s" option))))
1030 (defun rcirc-channel-p (target)
1031 "Return t if TARGET is a channel name."
1032 (and target
1033 (not (zerop (length target)))
1034 (or (eq (aref target 0) ?#)
1035 (eq (aref target 0) ?&))))
1037 (defun rcirc-kill-buffer-hook ()
1038 "Part the channel when killing an rcirc buffer."
1039 (when (eq major-mode 'rcirc-mode)
1040 (when (and rcirc-log-flag
1041 rcirc-log-directory)
1042 (rcirc-log-write))
1043 (rcirc-clean-up-buffer "Killed buffer")))
1045 (defun rcirc-change-major-mode-hook ()
1046 "Part the channel when changing the major-mode."
1047 (rcirc-clean-up-buffer "Changed major mode"))
1049 (defun rcirc-clean-up-buffer (reason)
1050 (let ((buffer (current-buffer)))
1051 (rcirc-clear-activity buffer)
1052 (when (and (rcirc-buffer-process)
1053 (eq (process-status (rcirc-buffer-process)) 'open))
1054 (with-rcirc-server-buffer
1055 (setq rcirc-buffer-alist
1056 (rassq-delete-all buffer rcirc-buffer-alist)))
1057 (rcirc-update-short-buffer-names)
1058 (if (rcirc-channel-p rcirc-target)
1059 (rcirc-send-string (rcirc-buffer-process)
1060 (concat "PART " rcirc-target " :" reason))
1061 (when rcirc-target
1062 (rcirc-remove-nick-channel (rcirc-buffer-process)
1063 (rcirc-buffer-nick)
1064 rcirc-target))))
1065 (setq rcirc-target nil)))
1067 (defun rcirc-generate-new-buffer-name (process target)
1068 "Return a buffer name based on PROCESS and TARGET.
1069 This is used for the initial name given to IRC buffers."
1070 (substring-no-properties
1071 (if target
1072 (concat target "@" (process-name process))
1073 (concat "*" (process-name process) "*"))))
1075 (defun rcirc-get-buffer (process target &optional server)
1076 "Return the buffer associated with the PROCESS and TARGET.
1078 If optional argument SERVER is non-nil, return the server buffer
1079 if there is no existing buffer for TARGET, otherwise return nil."
1080 (with-rcirc-process-buffer process
1081 (if (null target)
1082 (current-buffer)
1083 (let ((buffer (cdr (assoc-string target rcirc-buffer-alist t))))
1084 (or buffer (when server (current-buffer)))))))
1086 (defun rcirc-get-buffer-create (process target)
1087 "Return the buffer associated with the PROCESS and TARGET.
1088 Create the buffer if it doesn't exist."
1089 (let ((buffer (rcirc-get-buffer process target)))
1090 (if (and buffer (buffer-live-p buffer))
1091 (with-current-buffer buffer
1092 (when (not rcirc-target)
1093 (setq rcirc-target target))
1094 buffer)
1095 ;; create the buffer
1096 (with-rcirc-process-buffer process
1097 (let ((new-buffer (get-buffer-create
1098 (rcirc-generate-new-buffer-name process target))))
1099 (with-current-buffer new-buffer
1100 (rcirc-mode process target)
1101 (rcirc-put-nick-channel process (rcirc-nick process) target
1102 rcirc-current-line))
1103 new-buffer)))))
1105 (defun rcirc-send-input ()
1106 "Send input to target associated with the current buffer."
1107 (interactive)
1108 (if (< (point) rcirc-prompt-end-marker)
1109 ;; copy the line down to the input area
1110 (progn
1111 (forward-line 0)
1112 (let ((start (if (eq (point) (point-min))
1113 (point)
1114 (if (get-text-property (1- (point)) 'hard)
1115 (point)
1116 (previous-single-property-change (point) 'hard))))
1117 (end (next-single-property-change (1+ (point)) 'hard)))
1118 (goto-char (point-max))
1119 (insert (replace-regexp-in-string
1120 "\n\\s-+" " "
1121 (buffer-substring-no-properties start end)))))
1122 ;; process input
1123 (goto-char (point-max))
1124 (when (not (equal 0 (- (point) rcirc-prompt-end-marker)))
1125 ;; delete a trailing newline
1126 (when (eq (point) (point-at-bol))
1127 (delete-char -1))
1128 (let ((input (buffer-substring-no-properties
1129 rcirc-prompt-end-marker (point))))
1130 (dolist (line (split-string input "\n"))
1131 (rcirc-process-input-line line))
1132 ;; add to input-ring
1133 (save-excursion
1134 (ring-insert rcirc-input-ring input)
1135 (setq rcirc-input-ring-index 0))))))
1137 (defun rcirc-fill-paragraph (&optional arg)
1138 (interactive "p")
1139 (when (> (point) rcirc-prompt-end-marker)
1140 (save-restriction
1141 (narrow-to-region rcirc-prompt-end-marker (point-max))
1142 (let ((fill-column rcirc-max-message-length))
1143 (fill-region (point-min) (point-max))))))
1145 (defun rcirc-process-input-line (line)
1146 (if (string-match "^/\\([^ ]+\\) ?\\(.*\\)$" line)
1147 (rcirc-process-command (match-string 1 line)
1148 (match-string 2 line)
1149 line)
1150 (rcirc-process-message line)))
1152 (defun rcirc-process-message (line)
1153 (if (not rcirc-target)
1154 (message "Not joined (no target)")
1155 (delete-region rcirc-prompt-end-marker (point))
1156 (rcirc-send-message (rcirc-buffer-process) rcirc-target line)
1157 (setq rcirc-last-post-time (current-time))))
1159 (defun rcirc-process-command (command args line)
1160 (if (eq (aref command 0) ?/)
1161 ;; "//text" will send "/text" as a message
1162 (rcirc-process-message (substring line 1))
1163 (let ((fun (intern-soft (concat "rcirc-cmd-" command)))
1164 (process (rcirc-buffer-process)))
1165 (newline)
1166 (with-current-buffer (current-buffer)
1167 (delete-region rcirc-prompt-end-marker (point))
1168 (if (string= command "me")
1169 (rcirc-print process (rcirc-buffer-nick)
1170 "ACTION" rcirc-target args)
1171 (rcirc-print process (rcirc-buffer-nick)
1172 "COMMAND" rcirc-target line))
1173 (set-marker rcirc-prompt-end-marker (point))
1174 (if (fboundp fun)
1175 (funcall fun args process rcirc-target)
1176 (rcirc-send-string process
1177 (concat command " :" args)))))))
1179 (defvar rcirc-parent-buffer nil)
1180 (defvar rcirc-window-configuration nil)
1181 (defun rcirc-edit-multiline ()
1182 "Move current edit to a dedicated buffer."
1183 (interactive)
1184 (let ((pos (1+ (- (point) rcirc-prompt-end-marker))))
1185 (goto-char (point-max))
1186 (let ((text (buffer-substring-no-properties rcirc-prompt-end-marker
1187 (point)))
1188 (parent (buffer-name)))
1189 (delete-region rcirc-prompt-end-marker (point))
1190 (setq rcirc-window-configuration (current-window-configuration))
1191 (pop-to-buffer (concat "*multiline " parent "*"))
1192 (funcall rcirc-multiline-major-mode)
1193 (rcirc-multiline-minor-mode 1)
1194 (setq rcirc-parent-buffer parent)
1195 (insert text)
1196 (and (> pos 0) (goto-char pos))
1197 (message "Type C-c C-c to return text to %s, or C-c C-k to cancel" parent))))
1199 (defvar rcirc-multiline-minor-mode-map (make-sparse-keymap)
1200 "Keymap for multiline mode in rcirc.")
1201 (define-key rcirc-multiline-minor-mode-map
1202 (kbd "C-c C-c") 'rcirc-multiline-minor-submit)
1203 (define-key rcirc-multiline-minor-mode-map
1204 (kbd "C-x C-s") 'rcirc-multiline-minor-submit)
1205 (define-key rcirc-multiline-minor-mode-map
1206 (kbd "C-c C-k") 'rcirc-multiline-minor-cancel)
1207 (define-key rcirc-multiline-minor-mode-map
1208 (kbd "ESC ESC ESC") 'rcirc-multiline-minor-cancel)
1210 (define-minor-mode rcirc-multiline-minor-mode
1211 "Minor mode for editing multiple lines in rcirc."
1212 :init-value nil
1213 :lighter " rcirc-mline"
1214 :keymap rcirc-multiline-minor-mode-map
1215 :global nil
1216 :group 'rcirc
1217 (make-local-variable 'rcirc-parent-buffer)
1218 (put 'rcirc-parent-buffer 'permanent-local t)
1219 (setq fill-column rcirc-max-message-length))
1221 (defun rcirc-multiline-minor-submit ()
1222 "Send the text in buffer back to parent buffer."
1223 (interactive)
1224 (untabify (point-min) (point-max))
1225 (let ((text (buffer-substring (point-min) (point-max)))
1226 (buffer (current-buffer))
1227 (pos (point)))
1228 (set-buffer rcirc-parent-buffer)
1229 (goto-char (point-max))
1230 (insert text)
1231 (kill-buffer buffer)
1232 (set-window-configuration rcirc-window-configuration)
1233 (goto-char (+ rcirc-prompt-end-marker (1- pos)))))
1235 (defun rcirc-multiline-minor-cancel ()
1236 "Cancel the multiline edit."
1237 (interactive)
1238 (kill-buffer (current-buffer))
1239 (set-window-configuration rcirc-window-configuration))
1241 (defun rcirc-any-buffer (process)
1242 "Return a buffer for PROCESS, either the one selected or the process buffer."
1243 (if rcirc-always-use-server-buffer-flag
1244 (process-buffer process)
1245 (let ((buffer (window-buffer (selected-window))))
1246 (if (and buffer
1247 (with-current-buffer buffer
1248 (and (eq major-mode 'rcirc-mode)
1249 (eq (rcirc-buffer-process) process))))
1250 buffer
1251 (process-buffer process)))))
1253 (defcustom rcirc-response-formats
1254 '(("PRIVMSG" . "<%N> %m")
1255 ("NOTICE" . "-%N- %m")
1256 ("ACTION" . "[%N %m]")
1257 ("COMMAND" . "%m")
1258 ("ERROR" . "%fw!!! %m")
1259 (t . "%fp*** %fs%n %r %m"))
1260 "An alist of formats used for printing responses.
1261 The format is looked up using the response-type as a key;
1262 if no match is found, the default entry (with a key of `t') is used.
1264 The entry's value part should be a string, which is inserted with
1265 the of the following escape sequences replaced by the described values:
1267 %m The message text
1268 %n The sender's nick
1269 %N The sender's nick (with face `rcirc-my-nick' or `rcirc-other-nick')
1270 %r The response-type
1271 %t The target
1272 %fw Following text uses the face `font-lock-warning-face'
1273 %fp Following text uses the face `rcirc-server-prefix'
1274 %fs Following text uses the face `rcirc-server'
1275 %f[FACE] Following text uses the face FACE
1276 %f- Following text uses the default face
1277 %% A literal `%' character"
1278 :type '(alist :key-type (choice (string :tag "Type")
1279 (const :tag "Default" t))
1280 :value-type string)
1281 :group 'rcirc)
1283 (defcustom rcirc-omit-responses
1284 '("JOIN" "PART" "QUIT" "NICK")
1285 "Responses which will be hidden when `rcirc-omit-mode' is enabled."
1286 :type '(repeat string)
1287 :group 'rcirc)
1289 (defun rcirc-format-response-string (process sender response target text)
1290 "Return a nicely-formatted response string, incorporating TEXT
1291 \(and perhaps other arguments). The specific formatting used
1292 is found by looking up RESPONSE in `rcirc-response-formats'."
1293 (with-temp-buffer
1294 (insert (or (cdr (assoc response rcirc-response-formats))
1295 (cdr (assq t rcirc-response-formats))))
1296 (goto-char (point-min))
1297 (let ((start (point-min))
1298 (sender (if (or (not sender)
1299 (string= (rcirc-server-name process) sender))
1301 sender))
1302 face)
1303 (while (re-search-forward "%\\(\\(f\\(.\\)\\)\\|\\(.\\)\\)" nil t)
1304 (rcirc-add-face start (match-beginning 0) face)
1305 (setq start (match-beginning 0))
1306 (replace-match
1307 (case (aref (match-string 1) 0)
1308 (?f (setq face
1309 (case (string-to-char (match-string 3))
1310 (?w 'font-lock-warning-face)
1311 (?p 'rcirc-server-prefix)
1312 (?s 'rcirc-server)
1313 (t nil)))
1315 (?n sender)
1316 (?N (let ((my-nick (rcirc-nick process)))
1317 (save-match-data
1318 (with-syntax-table rcirc-nick-syntax-table
1319 (rcirc-facify sender
1320 (cond ((string= sender my-nick)
1321 'rcirc-my-nick)
1322 ((and rcirc-bright-nicks
1323 (string-match
1324 (regexp-opt rcirc-bright-nicks
1325 'words)
1326 sender))
1327 'rcirc-bright-nick)
1328 ((and rcirc-dim-nicks
1329 (string-match
1330 (regexp-opt rcirc-dim-nicks
1331 'words)
1332 sender))
1333 'rcirc-dim-nick)
1335 'rcirc-other-nick)))))))
1336 (?m (propertize text 'rcirc-text text))
1337 (?r response)
1338 (?t (or target ""))
1339 (t (concat "UNKNOWN CODE:" (match-string 0))))
1340 t t nil 0)
1341 (rcirc-add-face (match-beginning 0) (match-end 0) face))
1342 (rcirc-add-face start (match-beginning 0) face))
1343 (buffer-substring (point-min) (point-max))))
1345 (defun rcirc-target-buffer (process sender response target text)
1346 "Return a buffer to print the server response."
1347 (assert (not (bufferp target)))
1348 (with-rcirc-process-buffer process
1349 (cond ((not target)
1350 (rcirc-any-buffer process))
1351 ((not (rcirc-channel-p target))
1352 ;; message from another user
1353 (if (or (string= response "PRIVMSG")
1354 (string= response "ACTION"))
1355 (rcirc-get-buffer-create process (if (string= sender rcirc-nick)
1356 target
1357 sender))
1358 (rcirc-get-buffer process target t)))
1359 ((or (rcirc-get-buffer process target)
1360 (rcirc-any-buffer process))))))
1362 (defvar rcirc-activity-types nil)
1363 (make-variable-buffer-local 'rcirc-activity-types)
1364 (defvar rcirc-last-sender nil)
1365 (make-variable-buffer-local 'rcirc-last-sender)
1367 (defcustom rcirc-log-directory "~/.emacs.d/rcirc-log"
1368 "Directory to keep IRC logfiles."
1369 :type 'directory
1370 :group 'rcirc)
1372 (defcustom rcirc-log-flag nil
1373 "Non-nil means log IRC activity to disk.
1374 Logfiles are kept in `rcirc-log-directory'."
1375 :type 'boolean
1376 :group 'rcirc)
1378 (defcustom rcirc-omit-threshold 100
1379 "Number of lines since last activity from a nick before `rcirc-omit-responses' are omitted."
1380 :type 'integer
1381 :group 'rcirc)
1383 (defcustom rcirc-log-process-buffers nil
1384 "Non-nil if rcirc process buffers should be logged to disk."
1385 :group 'rcirc
1386 :type 'boolean
1387 :version "24.1")
1389 (defun rcirc-last-quit-line (process nick target)
1390 "Return the line number where NICK left TARGET.
1391 Returns nil if the information is not recorded."
1392 (let ((chanbuf (rcirc-get-buffer process target)))
1393 (when chanbuf
1394 (cdr (assoc-string nick (with-current-buffer chanbuf
1395 rcirc-recent-quit-alist))))))
1397 (defun rcirc-last-line (process nick target)
1398 "Return the line from the last activity from NICK in TARGET."
1399 (let* ((chanbuf (rcirc-get-buffer process target))
1400 (line (or (cdr (assoc-string target
1401 (gethash nick (with-rcirc-server-buffer
1402 rcirc-nick-table)) t))
1403 (rcirc-last-quit-line process nick target))))
1404 (if line
1405 line
1406 ;;(message "line is nil for %s in %s" nick target)
1407 nil)))
1409 (defun rcirc-elapsed-lines (process nick target)
1410 "Return the number of lines since activity from NICK in TARGET."
1411 (let ((last-activity-line (rcirc-last-line process nick target)))
1412 (when (and last-activity-line
1413 (> last-activity-line 0))
1414 (- rcirc-current-line last-activity-line))))
1416 (defvar rcirc-markup-text-functions
1417 '(rcirc-markup-attributes
1418 rcirc-markup-my-nick
1419 rcirc-markup-urls
1420 rcirc-markup-keywords
1421 rcirc-markup-bright-nicks)
1423 "List of functions used to manipulate text before it is printed.
1425 Each function takes two arguments, SENDER, and RESPONSE. The
1426 buffer is narrowed with the text to be printed and the point is
1427 at the beginning of the `rcirc-text' propertized text.")
1429 (defun rcirc-print (process sender response target text &optional activity)
1430 "Print TEXT in the buffer associated with TARGET.
1431 Format based on SENDER and RESPONSE. If ACTIVITY is non-nil,
1432 record activity."
1433 (or text (setq text ""))
1434 (unless (and (or (member sender rcirc-ignore-list)
1435 (member (with-syntax-table rcirc-nick-syntax-table
1436 (when (string-match "^\\([^/]\\w*\\)[:,]" text)
1437 (match-string 1 text)))
1438 rcirc-ignore-list))
1439 ;; do not ignore if we sent the message
1440 (not (string= sender (rcirc-nick process))))
1441 (let* ((buffer (rcirc-target-buffer process sender response target text))
1442 (inhibit-read-only t))
1443 (with-current-buffer buffer
1444 (let ((moving (= (point) rcirc-prompt-end-marker))
1445 (old-point (point-marker))
1446 (fill-start (marker-position rcirc-prompt-start-marker)))
1448 (unless (string= sender (rcirc-nick process))
1449 ;; only decode text from other senders, not ours
1450 (setq text (decode-coding-string text rcirc-decode-coding-system))
1451 ;; mark the line with overlay arrow
1452 (unless (or (marker-position overlay-arrow-position)
1453 (get-buffer-window (current-buffer))
1454 (member response rcirc-omit-responses))
1455 (set-marker overlay-arrow-position
1456 (marker-position rcirc-prompt-start-marker))))
1458 ;; temporarily set the marker insertion-type because
1459 ;; insert-before-markers results in hidden text in new buffers
1460 (goto-char rcirc-prompt-start-marker)
1461 (set-marker-insertion-type rcirc-prompt-start-marker t)
1462 (set-marker-insertion-type rcirc-prompt-end-marker t)
1464 (let ((start (point)))
1465 (insert (rcirc-format-response-string process sender response nil
1466 text)
1467 (propertize "\n" 'hard t))
1469 ;; squeeze spaces out of text before rcirc-text
1470 (fill-region fill-start
1471 (1- (or (next-single-property-change fill-start
1472 'rcirc-text)
1473 rcirc-prompt-end-marker)))
1475 ;; run markup functions
1476 (save-excursion
1477 (save-restriction
1478 (narrow-to-region start rcirc-prompt-start-marker)
1479 (goto-char (or (next-single-property-change start 'rcirc-text)
1480 (point)))
1481 (when (rcirc-buffer-process)
1482 (save-excursion (rcirc-markup-timestamp sender response))
1483 (dolist (fn rcirc-markup-text-functions)
1484 (save-excursion (funcall fn sender response)))
1485 (when rcirc-fill-flag
1486 (save-excursion (rcirc-markup-fill sender response))))
1488 (when rcirc-read-only-flag
1489 (add-text-properties (point-min) (point-max)
1490 '(read-only t front-sticky t))))
1491 ;; make text omittable
1492 (let ((last-activity-lines (rcirc-elapsed-lines process sender target)))
1493 (if (and (not (string= (rcirc-nick process) sender))
1494 (member response rcirc-omit-responses)
1495 (or (not last-activity-lines)
1496 (< rcirc-omit-threshold last-activity-lines)))
1497 (put-text-property (1- start) (1- rcirc-prompt-start-marker)
1498 'invisible 'rcirc-omit)
1499 ;; otherwise increment the line count
1500 (setq rcirc-current-line (1+ rcirc-current-line))))))
1502 (set-marker-insertion-type rcirc-prompt-start-marker nil)
1503 (set-marker-insertion-type rcirc-prompt-end-marker nil)
1505 ;; truncate buffer if it is very long
1506 (save-excursion
1507 (when (and rcirc-buffer-maximum-lines
1508 (> rcirc-buffer-maximum-lines 0)
1509 (= (forward-line (- rcirc-buffer-maximum-lines)) 0))
1510 (delete-region (point-min) (point))))
1512 ;; set the window point for buffers show in windows
1513 (walk-windows (lambda (w)
1514 (when (and (not (eq (selected-window) w))
1515 (eq (current-buffer)
1516 (window-buffer w))
1517 (>= (window-point w)
1518 rcirc-prompt-end-marker))
1519 (set-window-point w (point-max))))
1520 nil t)
1522 ;; restore the point
1523 (goto-char (if moving rcirc-prompt-end-marker old-point))
1525 ;; keep window on bottom line if it was already there
1526 (when rcirc-scroll-show-maximum-output
1527 (walk-windows (lambda (w)
1528 (when (eq (window-buffer w) (current-buffer))
1529 (with-current-buffer (window-buffer w)
1530 (when (eq major-mode 'rcirc-mode)
1531 (with-selected-window w
1532 (when (<= (- (window-height)
1533 (count-screen-lines (window-point)
1534 (window-start))
1537 (recenter -1)))))))
1538 nil t))
1540 ;; flush undo (can we do something smarter here?)
1541 (buffer-disable-undo)
1542 (buffer-enable-undo))
1544 ;; record modeline activity
1545 (when (and activity
1546 (not rcirc-ignore-buffer-activity-flag)
1547 (not (and rcirc-dim-nicks sender
1548 (string-match (regexp-opt rcirc-dim-nicks) sender)
1549 (rcirc-channel-p target))))
1550 (rcirc-record-activity (current-buffer)
1551 (when (not (rcirc-channel-p rcirc-target))
1552 'nick)))
1554 (when (and rcirc-log-flag
1555 (or target
1556 rcirc-log-process-buffers))
1557 (rcirc-log process sender response target text))
1559 (sit-for 0) ; displayed text before hook
1560 (run-hook-with-args 'rcirc-print-hooks
1561 process sender response target text)))))
1563 (defun rcirc-generate-log-filename (process target)
1564 (if target
1565 (rcirc-generate-new-buffer-name process target)
1566 (process-name process)))
1568 (defcustom rcirc-log-filename-function 'rcirc-generate-log-filename
1569 "A function to generate the filename used by rcirc's logging facility.
1571 It is called with two arguments, PROCESS and TARGET (see
1572 `rcirc-generate-new-buffer-name' for their meaning), and should
1573 return the filename, or nil if no logging is desired for this
1574 session.
1576 If the returned filename is absolute (`file-name-absolute-p'
1577 returns t), then it is used as-is, otherwise the resulting file
1578 is put into `rcirc-log-directory'.
1580 The filename is then cleaned using `convert-standard-filename' to
1581 guarantee valid filenames for the current OS."
1582 :group 'rcirc
1583 :type 'function)
1585 (defun rcirc-log (process sender response target text)
1586 "Record line in `rcirc-log', to be later written to disk."
1587 (let ((filename (funcall rcirc-log-filename-function process target)))
1588 (unless (null filename)
1589 (let ((cell (assoc-string filename rcirc-log-alist))
1590 (line (concat (format-time-string rcirc-time-format)
1591 (substring-no-properties
1592 (rcirc-format-response-string process sender
1593 response target text))
1594 "\n")))
1595 (if cell
1596 (setcdr cell (concat (cdr cell) line))
1597 (setq rcirc-log-alist
1598 (cons (cons filename line) rcirc-log-alist)))))))
1600 (defun rcirc-log-write ()
1601 "Flush `rcirc-log-alist' data to disk.
1603 Log data is written to `rcirc-log-directory', except for
1604 log-files with absolute names (see `rcirc-log-filename-function')."
1605 (dolist (cell rcirc-log-alist)
1606 (let ((filename (convert-standard-filename
1607 (expand-file-name (car cell)
1608 rcirc-log-directory)))
1609 (coding-system-for-write 'utf-8))
1610 (make-directory (file-name-directory filename) t)
1611 (with-temp-buffer
1612 (insert (cdr cell))
1613 (write-region (point-min) (point-max) filename t 'quiet))))
1614 (setq rcirc-log-alist nil))
1616 (defun rcirc-view-log-file ()
1617 "View logfile corresponding to the current buffer."
1618 (interactive)
1619 (find-file-other-window
1620 (expand-file-name (funcall rcirc-log-filename-function
1621 (rcirc-buffer-process) rcirc-target)
1622 rcirc-log-directory)))
1624 (defun rcirc-join-channels (process channels)
1625 "Join CHANNELS."
1626 (save-window-excursion
1627 (dolist (channel channels)
1628 (with-rcirc-process-buffer process
1629 (rcirc-cmd-join channel process)))))
1631 ;;; nick management
1632 (defvar rcirc-nick-prefix-chars "~&@%+")
1633 (defun rcirc-user-nick (user)
1634 "Return the nick from USER. Remove any non-nick junk."
1635 (save-match-data
1636 (if (string-match (concat "^[" rcirc-nick-prefix-chars
1637 "]?\\([^! ]+\\)!?") (or user ""))
1638 (match-string 1 user)
1639 user)))
1641 (defun rcirc-nick-channels (process nick)
1642 "Return list of channels for NICK."
1643 (with-rcirc-process-buffer process
1644 (mapcar (lambda (x) (car x))
1645 (gethash nick rcirc-nick-table))))
1647 (defun rcirc-put-nick-channel (process nick channel &optional line)
1648 "Add CHANNEL to list associated with NICK.
1649 Update the associated linestamp if LINE is non-nil.
1651 If the record doesn't exist, and LINE is nil, set the linestamp
1652 to zero."
1653 (let ((nick (rcirc-user-nick nick)))
1654 (with-rcirc-process-buffer process
1655 (let* ((chans (gethash nick rcirc-nick-table))
1656 (record (assoc-string channel chans t)))
1657 (if record
1658 (when line (setcdr record line))
1659 (puthash nick (cons (cons channel (or line 0))
1660 chans)
1661 rcirc-nick-table))))))
1663 (defun rcirc-nick-remove (process nick)
1664 "Remove NICK from table."
1665 (with-rcirc-process-buffer process
1666 (remhash nick rcirc-nick-table)))
1668 (defun rcirc-remove-nick-channel (process nick channel)
1669 "Remove the CHANNEL from list associated with NICK."
1670 (with-rcirc-process-buffer process
1671 (let* ((chans (gethash nick rcirc-nick-table))
1672 (newchans
1673 ;; instead of assoc-string-delete-all:
1674 (let ((record (assoc-string channel chans t)))
1675 (when record
1676 (setcar record 'delete)
1677 (assq-delete-all 'delete chans)))))
1678 (if newchans
1679 (puthash nick newchans rcirc-nick-table)
1680 (remhash nick rcirc-nick-table)))))
1682 (defun rcirc-channel-nicks (process target)
1683 "Return the list of nicks associated with TARGET sorted by last activity."
1684 (when target
1685 (if (rcirc-channel-p target)
1686 (with-rcirc-process-buffer process
1687 (let (nicks)
1688 (maphash
1689 (lambda (k v)
1690 (let ((record (assoc-string target v t)))
1691 (if record
1692 (setq nicks (cons (cons k (cdr record)) nicks)))))
1693 rcirc-nick-table)
1694 (mapcar (lambda (x) (car x))
1695 (sort nicks (lambda (x y)
1696 (let ((lx (or (cdr x) 0))
1697 (ly (or (cdr y) 0)))
1698 (< ly lx)))))))
1699 (list target))))
1701 (defun rcirc-ignore-update-automatic (nick)
1702 "Remove NICK from `rcirc-ignore-list'
1703 if NICK is also on `rcirc-ignore-list-automatic'."
1704 (when (member nick rcirc-ignore-list-automatic)
1705 (setq rcirc-ignore-list-automatic
1706 (delete nick rcirc-ignore-list-automatic)
1707 rcirc-ignore-list
1708 (delete nick rcirc-ignore-list))))
1710 (defun rcirc-nickname< (s1 s2)
1711 "Return t if IRC nickname S1 is less than S2, and nil otherwise.
1712 Operator nicknames (@) are considered less than voiced
1713 nicknames (+). Any other nicknames are greater than voiced
1714 nicknames. The comparison is case-insensitive."
1715 (setq s1 (downcase s1)
1716 s2 (downcase s2))
1717 (let* ((s1-op (eq ?@ (string-to-char s1)))
1718 (s2-op (eq ?@ (string-to-char s2))))
1719 (if s1-op
1720 (if s2-op
1721 (string< (substring s1 1) (substring s2 1))
1723 (if s2-op
1725 (string< s1 s2)))))
1727 (defun rcirc-sort-nicknames-join (input sep)
1728 "Return a string of sorted nicknames.
1729 INPUT is a string containing nicknames separated by SEP.
1730 This function does not alter the INPUT string."
1731 (let* ((parts (split-string input sep t))
1732 (sorted (sort parts 'rcirc-nickname<)))
1733 (mapconcat 'identity sorted sep)))
1735 ;;; activity tracking
1736 (defvar rcirc-track-minor-mode-map (make-sparse-keymap)
1737 "Keymap for rcirc track minor mode.")
1739 (define-key rcirc-track-minor-mode-map (kbd "C-c C-@") 'rcirc-next-active-buffer)
1740 (define-key rcirc-track-minor-mode-map (kbd "C-c C-SPC") 'rcirc-next-active-buffer)
1742 ;;;###autoload
1743 (define-minor-mode rcirc-track-minor-mode
1744 "Global minor mode for tracking activity in rcirc buffers."
1745 :init-value nil
1746 :lighter ""
1747 :keymap rcirc-track-minor-mode-map
1748 :global t
1749 :group 'rcirc
1750 (or global-mode-string (setq global-mode-string '("")))
1751 ;; toggle the mode-line channel indicator
1752 (if rcirc-track-minor-mode
1753 (progn
1754 (and (not (memq 'rcirc-activity-string global-mode-string))
1755 (setq global-mode-string
1756 (append global-mode-string '(rcirc-activity-string))))
1757 (add-hook 'window-configuration-change-hook
1758 'rcirc-window-configuration-change))
1759 (setq global-mode-string
1760 (delete 'rcirc-activity-string global-mode-string))
1761 (remove-hook 'window-configuration-change-hook
1762 'rcirc-window-configuration-change)))
1764 (or (assq 'rcirc-ignore-buffer-activity-flag minor-mode-alist)
1765 (setq minor-mode-alist
1766 (cons '(rcirc-ignore-buffer-activity-flag " Ignore") minor-mode-alist)))
1767 (or (assq 'rcirc-low-priority-flag minor-mode-alist)
1768 (setq minor-mode-alist
1769 (cons '(rcirc-low-priority-flag " LowPri") minor-mode-alist)))
1770 (or (assq 'rcirc-omit-mode minor-mode-alist)
1771 (setq minor-mode-alist
1772 (cons '(rcirc-omit-mode " Omit") minor-mode-alist)))
1774 (defun rcirc-toggle-ignore-buffer-activity ()
1775 "Toggle the value of `rcirc-ignore-buffer-activity-flag'."
1776 (interactive)
1777 (setq rcirc-ignore-buffer-activity-flag
1778 (not rcirc-ignore-buffer-activity-flag))
1779 (message (if rcirc-ignore-buffer-activity-flag
1780 "Ignore activity in this buffer"
1781 "Notice activity in this buffer"))
1782 (force-mode-line-update))
1784 (defun rcirc-toggle-low-priority ()
1785 "Toggle the value of `rcirc-low-priority-flag'."
1786 (interactive)
1787 (setq rcirc-low-priority-flag
1788 (not rcirc-low-priority-flag))
1789 (message (if rcirc-low-priority-flag
1790 "Activity in this buffer is low priority"
1791 "Activity in this buffer is normal priority"))
1792 (force-mode-line-update))
1794 (defun rcirc-omit-mode ()
1795 "Toggle the Rcirc-Omit mode.
1796 If enabled, \"uninteresting\" lines are not shown.
1797 Uninteresting lines are those whose responses are listed in
1798 `rcirc-omit-responses'."
1799 (interactive)
1800 (setq rcirc-omit-mode (not rcirc-omit-mode))
1801 (if rcirc-omit-mode
1802 (progn
1803 (add-to-invisibility-spec '(rcirc-omit . nil))
1804 (message "Rcirc-Omit mode enabled"))
1805 (remove-from-invisibility-spec '(rcirc-omit . nil))
1806 (message "Rcirc-Omit mode disabled"))
1807 (recenter (when (> (point) rcirc-prompt-start-marker) -1)))
1809 (defun rcirc-switch-to-server-buffer ()
1810 "Switch to the server buffer associated with current channel buffer."
1811 (interactive)
1812 (switch-to-buffer rcirc-server-buffer))
1814 (defun rcirc-jump-to-first-unread-line ()
1815 "Move the point to the first unread line in this buffer."
1816 (interactive)
1817 (if (marker-position overlay-arrow-position)
1818 (goto-char overlay-arrow-position)
1819 (message "No unread messages")))
1821 (defun rcirc-non-irc-buffer ()
1822 (let ((buflist (buffer-list))
1823 buffer)
1824 (while (and buflist (not buffer))
1825 (with-current-buffer (car buflist)
1826 (unless (or (eq major-mode 'rcirc-mode)
1827 (= ?\s (aref (buffer-name) 0)) ; internal buffers
1828 (get-buffer-window (current-buffer)))
1829 (setq buffer (current-buffer))))
1830 (setq buflist (cdr buflist)))
1831 buffer))
1833 (defun rcirc-next-active-buffer (arg)
1834 "Switch to the next rcirc buffer with activity.
1835 With prefix ARG, go to the next low priority buffer with activity."
1836 (interactive "P")
1837 (let* ((pair (rcirc-split-activity rcirc-activity))
1838 (lopri (car pair))
1839 (hipri (cdr pair)))
1840 (if (or (and (not arg) hipri)
1841 (and arg lopri))
1842 (progn
1843 (switch-to-buffer (car (if arg lopri hipri)))
1844 (when (> (point) rcirc-prompt-start-marker)
1845 (recenter -1)))
1846 (if (eq major-mode 'rcirc-mode)
1847 (switch-to-buffer (rcirc-non-irc-buffer))
1848 (message "%s" (concat
1849 "No IRC activity."
1850 (when lopri
1851 (concat
1852 " Type C-u "
1853 (key-description (this-command-keys))
1854 " for low priority activity."))))))))
1856 (defvar rcirc-activity-hooks nil
1857 "Hook to be run when there is channel activity.
1859 Functions are called with a single argument, the buffer with the
1860 activity. Only run if the buffer is not visible and
1861 `rcirc-ignore-buffer-activity-flag' is non-nil.")
1863 (defun rcirc-record-activity (buffer &optional type)
1864 "Record BUFFER activity with TYPE."
1865 (with-current-buffer buffer
1866 (let ((old-activity rcirc-activity)
1867 (old-types rcirc-activity-types))
1868 (when (not (get-buffer-window (current-buffer) t))
1869 (setq rcirc-activity
1870 (sort (add-to-list 'rcirc-activity (current-buffer))
1871 (lambda (b1 b2)
1872 (let ((t1 (with-current-buffer b1 rcirc-last-post-time))
1873 (t2 (with-current-buffer b2 rcirc-last-post-time)))
1874 (time-less-p t2 t1)))))
1875 (pushnew type rcirc-activity-types)
1876 (unless (and (equal rcirc-activity old-activity)
1877 (member type old-types))
1878 (rcirc-update-activity-string)))))
1879 (run-hook-with-args 'rcirc-activity-hooks buffer))
1881 (defun rcirc-clear-activity (buffer)
1882 "Clear the BUFFER activity."
1883 (setq rcirc-activity (remove buffer rcirc-activity))
1884 (with-current-buffer buffer
1885 (setq rcirc-activity-types nil)))
1887 (defun rcirc-clear-unread (buffer)
1888 "Erase the last read message arrow from BUFFER."
1889 (when (buffer-live-p buffer)
1890 (with-current-buffer buffer
1891 (set-marker overlay-arrow-position nil))))
1893 (defun rcirc-split-activity (activity)
1894 "Return a cons cell with ACTIVITY split into (lopri . hipri)."
1895 (let (lopri hipri)
1896 (dolist (buf rcirc-activity)
1897 (with-current-buffer buf
1898 (if (and rcirc-low-priority-flag
1899 (not (member 'nick rcirc-activity-types)))
1900 (add-to-list 'lopri buf t)
1901 (add-to-list 'hipri buf t))))
1902 (cons lopri hipri)))
1904 (defvar rcirc-update-activity-string-hook nil
1905 "Hook run whenever the activity string is updated.")
1907 ;; TODO: add mouse properties
1908 (defun rcirc-update-activity-string ()
1909 "Update mode-line string."
1910 (let* ((pair (rcirc-split-activity rcirc-activity))
1911 (lopri (car pair))
1912 (hipri (cdr pair)))
1913 (setq rcirc-activity-string
1914 (cond ((or hipri lopri)
1915 (concat (and hipri "[")
1916 (rcirc-activity-string hipri)
1917 (and hipri lopri ",")
1918 (and lopri
1919 (concat "("
1920 (rcirc-activity-string lopri)
1921 ")"))
1922 (and hipri "]")))
1923 ((not (null (rcirc-process-list)))
1924 "[]")
1925 (t "[]")))
1926 (run-hooks 'rcirc-update-activity-string-hook)))
1928 (defun rcirc-activity-string (buffers)
1929 (mapconcat (lambda (b)
1930 (let ((s (substring-no-properties (rcirc-short-buffer-name b))))
1931 (with-current-buffer b
1932 (dolist (type rcirc-activity-types)
1933 (rcirc-add-face 0 (length s)
1934 (case type
1935 (nick 'rcirc-track-nick)
1936 (keyword 'rcirc-track-keyword))
1937 s)))
1939 buffers ","))
1941 (defun rcirc-short-buffer-name (buffer)
1942 "Return a short name for BUFFER to use in the modeline indicator."
1943 (with-current-buffer buffer
1944 (or rcirc-short-buffer-name (buffer-name))))
1946 (defun rcirc-visible-buffers ()
1947 "Return a list of the visible buffers that are in rcirc-mode."
1948 (let (acc)
1949 (walk-windows (lambda (w)
1950 (with-current-buffer (window-buffer w)
1951 (when (eq major-mode 'rcirc-mode)
1952 (push (current-buffer) acc)))))
1953 acc))
1955 (defvar rcirc-visible-buffers nil)
1956 (defun rcirc-window-configuration-change ()
1957 (unless (minibuffer-window-active-p (minibuffer-window))
1958 ;; delay this until command has finished to make sure window is
1959 ;; actually visible before clearing activity
1960 (add-hook 'post-command-hook 'rcirc-window-configuration-change-1)))
1962 (defun rcirc-window-configuration-change-1 ()
1963 ;; clear activity and overlay arrows
1964 (let* ((old-activity rcirc-activity)
1965 (hidden-buffers rcirc-visible-buffers))
1967 (setq rcirc-visible-buffers (rcirc-visible-buffers))
1969 (dolist (vbuf rcirc-visible-buffers)
1970 (setq hidden-buffers (delq vbuf hidden-buffers))
1971 ;; clear activity for all visible buffers
1972 (rcirc-clear-activity vbuf))
1974 ;; clear unread arrow from recently hidden buffers
1975 (dolist (hbuf hidden-buffers)
1976 (rcirc-clear-unread hbuf))
1978 ;; remove any killed buffers from list
1979 (setq rcirc-activity
1980 (delq nil (mapcar (lambda (buf) (when (buffer-live-p buf) buf))
1981 rcirc-activity)))
1982 ;; update the mode-line string
1983 (unless (equal old-activity rcirc-activity)
1984 (rcirc-update-activity-string)))
1986 (remove-hook 'post-command-hook 'rcirc-window-configuration-change-1))
1989 ;;; buffer name abbreviation
1990 (defun rcirc-update-short-buffer-names ()
1991 (let ((bufalist
1992 (apply 'append (mapcar (lambda (process)
1993 (with-rcirc-process-buffer process
1994 rcirc-buffer-alist))
1995 (rcirc-process-list)))))
1996 (dolist (i (rcirc-abbreviate bufalist))
1997 (when (buffer-live-p (cdr i))
1998 (with-current-buffer (cdr i)
1999 (setq rcirc-short-buffer-name (car i)))))))
2001 (defun rcirc-abbreviate (pairs)
2002 (apply 'append (mapcar 'rcirc-rebuild-tree (rcirc-make-trees pairs))))
2004 (defun rcirc-rebuild-tree (tree &optional acc)
2005 (let ((ch (char-to-string (car tree))))
2006 (dolist (x (cdr tree))
2007 (if (listp x)
2008 (setq acc (append acc
2009 (mapcar (lambda (y)
2010 (cons (concat ch (car y))
2011 (cdr y)))
2012 (rcirc-rebuild-tree x))))
2013 (setq acc (cons (cons ch x) acc))))
2014 acc))
2016 (defun rcirc-make-trees (pairs)
2017 (let (alist)
2018 (mapc (lambda (pair)
2019 (if (consp pair)
2020 (let* ((str (car pair))
2021 (data (cdr pair))
2022 (char (unless (zerop (length str))
2023 (aref str 0)))
2024 (rest (unless (zerop (length str))
2025 (substring str 1)))
2026 (part (if char (assq char alist))))
2027 (if part
2028 ;; existing partition
2029 (setcdr part (cons (cons rest data) (cdr part)))
2030 ;; new partition
2031 (setq alist (cons (if char
2032 (list char (cons rest data))
2033 data)
2034 alist))))
2035 (setq alist (cons pair alist))))
2036 pairs)
2037 ;; recurse into cdrs of alist
2038 (mapc (lambda (x)
2039 (when (and (listp x) (listp (cadr x)))
2040 (setcdr x (if (> (length (cdr x)) 1)
2041 (rcirc-make-trees (cdr x))
2042 (setcdr x (list (cdadr x)))))))
2043 alist)))
2045 ;;; /commands these are called with 3 args: PROCESS, TARGET, which is
2046 ;; the current buffer/channel/user, and ARGS, which is a string
2047 ;; containing the text following the /cmd.
2049 (defmacro defun-rcirc-command (command argument docstring interactive-form
2050 &rest body)
2051 "Define a command."
2052 `(progn
2053 (add-to-list 'rcirc-client-commands ,(concat "/" (symbol-name command)))
2054 (defun ,(intern (concat "rcirc-cmd-" (symbol-name command)))
2055 (,@argument &optional process target)
2056 ,(concat docstring "\n\nNote: If PROCESS or TARGET are nil, the values given"
2057 "\nby `rcirc-buffer-process' and `rcirc-target' will be used.")
2058 ,interactive-form
2059 (let ((process (or process (rcirc-buffer-process)))
2060 (target (or target rcirc-target)))
2061 ,@body))))
2063 (defun-rcirc-command msg (message)
2064 "Send private MESSAGE to TARGET."
2065 (interactive "i")
2066 (if (null message)
2067 (progn
2068 (setq target (completing-read "Message nick: "
2069 (with-rcirc-server-buffer
2070 rcirc-nick-table)))
2071 (when (> (length target) 0)
2072 (setq message (read-string (format "Message %s: " target)))
2073 (when (> (length message) 0)
2074 (rcirc-send-message process target message))))
2075 (if (not (string-match "\\([^ ]+\\) \\(.+\\)" message))
2076 (message "Not enough args, or something.")
2077 (setq target (match-string 1 message)
2078 message (match-string 2 message))
2079 (rcirc-send-message process target message))))
2081 (defun-rcirc-command query (nick)
2082 "Open a private chat buffer to NICK."
2083 (interactive (list (completing-read "Query nick: "
2084 (with-rcirc-server-buffer rcirc-nick-table))))
2085 (let ((existing-buffer (rcirc-get-buffer process nick)))
2086 (switch-to-buffer (or existing-buffer
2087 (rcirc-get-buffer-create process nick)))
2088 (when (not existing-buffer)
2089 (rcirc-cmd-whois nick))))
2091 (defun-rcirc-command join (channel)
2092 "Join CHANNEL."
2093 (interactive "sJoin channel: ")
2094 (let ((buffer (rcirc-get-buffer-create process
2095 (car (split-string channel)))))
2096 (rcirc-send-string process (concat "JOIN " channel))
2097 (when (not (eq (selected-window) (minibuffer-window)))
2098 (switch-to-buffer buffer))))
2100 ;; TODO: /part #channel reason, or consider removing #channel altogether
2101 (defun-rcirc-command part (channel)
2102 "Part CHANNEL."
2103 (interactive "sPart channel: ")
2104 (let ((channel (if (> (length channel) 0) channel target)))
2105 (rcirc-send-string process (concat "PART " channel " :" rcirc-id-string))))
2107 (defun-rcirc-command quit (reason)
2108 "Send a quit message to server with REASON."
2109 (interactive "sQuit reason: ")
2110 (rcirc-send-string process (concat "QUIT :"
2111 (if (not (zerop (length reason)))
2112 reason
2113 rcirc-id-string))))
2115 (defun-rcirc-command nick (nick)
2116 "Change nick to NICK."
2117 (interactive "i")
2118 (when (null nick)
2119 (setq nick (read-string "New nick: " (rcirc-nick process))))
2120 (rcirc-send-string process (concat "NICK " nick)))
2122 (defun-rcirc-command names (channel)
2123 "Display list of names in CHANNEL or in current channel if CHANNEL is nil.
2124 If called interactively, prompt for a channel when prefix arg is supplied."
2125 (interactive "P")
2126 (if (called-interactively-p 'interactive)
2127 (if channel
2128 (setq channel (read-string "List names in channel: " target))))
2129 (let ((channel (if (> (length channel) 0)
2130 channel
2131 target)))
2132 (rcirc-send-string process (concat "NAMES " channel))))
2134 (defun-rcirc-command topic (topic)
2135 "List TOPIC for the TARGET channel.
2136 With a prefix arg, prompt for new topic."
2137 (interactive "P")
2138 (if (and (called-interactively-p 'interactive) topic)
2139 (setq topic (read-string "New Topic: " rcirc-topic)))
2140 (rcirc-send-string process (concat "TOPIC " target
2141 (when (> (length topic) 0)
2142 (concat " :" topic)))))
2144 (defun-rcirc-command whois (nick)
2145 "Request information from server about NICK."
2146 (interactive (list
2147 (completing-read "Whois: "
2148 (with-rcirc-server-buffer rcirc-nick-table))))
2149 (rcirc-send-string process (concat "WHOIS " nick)))
2151 (defun-rcirc-command mode (args)
2152 "Set mode with ARGS."
2153 (interactive (list (concat (read-string "Mode nick or channel: ")
2154 " " (read-string "Mode: "))))
2155 (rcirc-send-string process (concat "MODE " args)))
2157 (defun-rcirc-command list (channels)
2158 "Request information on CHANNELS from server."
2159 (interactive "sList Channels: ")
2160 (rcirc-send-string process (concat "LIST " channels)))
2162 (defun-rcirc-command oper (args)
2163 "Send operator command to server."
2164 (interactive "sOper args: ")
2165 (rcirc-send-string process (concat "OPER " args)))
2167 (defun-rcirc-command quote (message)
2168 "Send MESSAGE literally to server."
2169 (interactive "sServer message: ")
2170 (rcirc-send-string process message))
2172 (defun-rcirc-command kick (arg)
2173 "Kick NICK from current channel."
2174 (interactive (list
2175 (concat (completing-read "Kick nick: "
2176 (rcirc-channel-nicks
2177 (rcirc-buffer-process)
2178 rcirc-target))
2179 (read-from-minibuffer "Kick reason: "))))
2180 (let* ((arglist (split-string arg))
2181 (argstring (concat (car arglist) " :"
2182 (mapconcat 'identity (cdr arglist) " "))))
2183 (rcirc-send-string process (concat "KICK " target " " argstring))))
2185 (defun rcirc-cmd-ctcp (args &optional process target)
2186 (if (string-match "^\\([^ ]+\\)\\s-+\\(.+\\)$" args)
2187 (let ((target (match-string 1 args))
2188 (request (match-string 2 args)))
2189 (rcirc-send-string process
2190 (format "PRIVMSG %s \C-a%s\C-a"
2191 target (upcase request))))
2192 (rcirc-print process (rcirc-nick process) "ERROR" nil
2193 "usage: /ctcp NICK REQUEST")))
2195 (defun rcirc-cmd-me (args &optional process target)
2196 (rcirc-send-string process (format "PRIVMSG %s :\C-aACTION %s\C-a"
2197 target args)))
2199 (defun rcirc-add-or-remove (set &rest elements)
2200 (dolist (elt elements)
2201 (if (and elt (not (string= "" elt)))
2202 (setq set (if (member-ignore-case elt set)
2203 (delete elt set)
2204 (cons elt set)))))
2205 set)
2207 (defun-rcirc-command ignore (nick)
2208 "Manage the ignore list.
2209 Ignore NICK, unignore NICK if already ignored, or list ignored
2210 nicks when no NICK is given. When listing ignored nicks, the
2211 ones added to the list automatically are marked with an asterisk."
2212 (interactive "sToggle ignoring of nick: ")
2213 (setq rcirc-ignore-list
2214 (apply #'rcirc-add-or-remove rcirc-ignore-list
2215 (split-string nick nil t)))
2216 (rcirc-print process nil "IGNORE" target
2217 (mapconcat
2218 (lambda (nick)
2219 (concat nick
2220 (if (member nick rcirc-ignore-list-automatic)
2221 "*" "")))
2222 rcirc-ignore-list " ")))
2224 (defun-rcirc-command bright (nick)
2225 "Manage the bright nick list."
2226 (interactive "sToggle emphasis of nick: ")
2227 (setq rcirc-bright-nicks
2228 (apply #'rcirc-add-or-remove rcirc-bright-nicks
2229 (split-string nick nil t)))
2230 (rcirc-print process nil "BRIGHT" target
2231 (mapconcat 'identity rcirc-bright-nicks " ")))
2233 (defun-rcirc-command dim (nick)
2234 "Manage the dim nick list."
2235 (interactive "sToggle deemphasis of nick: ")
2236 (setq rcirc-dim-nicks
2237 (apply #'rcirc-add-or-remove rcirc-dim-nicks
2238 (split-string nick nil t)))
2239 (rcirc-print process nil "DIM" target
2240 (mapconcat 'identity rcirc-dim-nicks " ")))
2242 (defun-rcirc-command keyword (keyword)
2243 "Manage the keyword list.
2244 Mark KEYWORD, unmark KEYWORD if already marked, or list marked
2245 keywords when no KEYWORD is given."
2246 (interactive "sToggle highlighting of keyword: ")
2247 (setq rcirc-keywords
2248 (apply #'rcirc-add-or-remove rcirc-keywords
2249 (split-string keyword nil t)))
2250 (rcirc-print process nil "KEYWORD" target
2251 (mapconcat 'identity rcirc-keywords " ")))
2254 (defun rcirc-add-face (start end name &optional object)
2255 "Add face NAME to the face text property of the text from START to END."
2256 (when name
2257 (let ((pos start)
2258 next prop)
2259 (while (< pos end)
2260 (setq prop (get-text-property pos 'face object)
2261 next (next-single-property-change pos 'face object end))
2262 (unless (member name (get-text-property pos 'face object))
2263 (add-text-properties pos next (list 'face (cons name prop)) object))
2264 (setq pos next)))))
2266 (defun rcirc-facify (string face)
2267 "Return a copy of STRING with FACE property added."
2268 (let ((string (or string "")))
2269 (rcirc-add-face 0 (length string) face string)
2270 string))
2272 (defvar rcirc-url-regexp
2273 (concat
2274 "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
2275 "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
2276 "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
2277 (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
2278 (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
2279 (punct "!?:;.,"))
2280 (concat
2281 "\\(?:"
2282 ;; Match paired parentheses, e.g. in Wikipedia URLs:
2283 "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]"
2284 "\\|"
2285 "[" chars punct "]+" "[" chars "]"
2286 "\\)"))
2287 (concat ;; XEmacs 21.4 doesn't support POSIX.
2288 "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
2289 "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
2290 "\\)")
2291 "Regexp matching URLs. Set to nil to disable URL features in rcirc.")
2293 (defun rcirc-browse-url (&optional arg)
2294 "Prompt for URL to browse based on URLs in buffer."
2295 (interactive "P")
2296 (let ((completions (mapcar (lambda (x) (cons x nil)) rcirc-urls))
2297 (initial-input (car rcirc-urls))
2298 (history (cdr rcirc-urls)))
2299 (browse-url (completing-read "rcirc browse-url: "
2300 completions nil nil initial-input 'history)
2301 arg)))
2303 (defun rcirc-browse-url-at-point (point)
2304 "Send URL at point to `browse-url'."
2305 (interactive "d")
2306 (let ((beg (previous-single-property-change (1+ point) 'mouse-face))
2307 (end (next-single-property-change point 'mouse-face)))
2308 (browse-url (buffer-substring-no-properties beg end))))
2310 (defun rcirc-browse-url-at-mouse (event)
2311 "Send URL at mouse click to `browse-url'."
2312 (interactive "e")
2313 (let ((position (event-end event)))
2314 (with-current-buffer (window-buffer (posn-window position))
2315 (rcirc-browse-url-at-point (posn-point position)))))
2318 (defun rcirc-markup-timestamp (sender response)
2319 (goto-char (point-min))
2320 (insert (rcirc-facify (format-time-string rcirc-time-format)
2321 'rcirc-timestamp)))
2323 (defun rcirc-markup-attributes (sender response)
2324 (while (re-search-forward "\\([\C-b\C-_\C-v]\\).*?\\(\\1\\|\C-o\\)" nil t)
2325 (rcirc-add-face (match-beginning 0) (match-end 0)
2326 (case (char-after (match-beginning 1))
2327 (?\C-b 'bold)
2328 (?\C-v 'italic)
2329 (?\C-_ 'underline)))
2330 ;; keep the ^O since it could terminate other attributes
2331 (when (not (eq ?\C-o (char-before (match-end 2))))
2332 (delete-region (match-beginning 2) (match-end 2)))
2333 (delete-region (match-beginning 1) (match-end 1))
2334 (goto-char (match-beginning 1)))
2335 ;; remove the ^O characters now
2336 (while (re-search-forward "\C-o+" nil t)
2337 (delete-region (match-beginning 0) (match-end 0))))
2339 (defun rcirc-markup-my-nick (sender response)
2340 (with-syntax-table rcirc-nick-syntax-table
2341 (while (re-search-forward (concat "\\b"
2342 (regexp-quote (rcirc-nick
2343 (rcirc-buffer-process)))
2344 "\\b")
2345 nil t)
2346 (rcirc-add-face (match-beginning 0) (match-end 0)
2347 'rcirc-nick-in-message)
2348 (when (string= response "PRIVMSG")
2349 (rcirc-add-face (point-min) (point-max)
2350 'rcirc-nick-in-message-full-line)
2351 (rcirc-record-activity (current-buffer) 'nick)))))
2353 (defun rcirc-markup-urls (sender response)
2354 (while (re-search-forward rcirc-url-regexp nil t)
2355 (let ((start (match-beginning 0))
2356 (end (match-end 0)))
2357 (rcirc-add-face start end 'rcirc-url)
2358 (add-text-properties start end (list 'mouse-face 'highlight
2359 'keymap rcirc-browse-url-map))
2360 ;; record the url
2361 (push (buffer-substring-no-properties start end) rcirc-urls))))
2363 (defun rcirc-markup-keywords (sender response)
2364 (when (and (string= response "PRIVMSG")
2365 (not (string= sender (rcirc-nick (rcirc-buffer-process)))))
2366 (let* ((target (or rcirc-target ""))
2367 (keywords (delq nil (mapcar (lambda (keyword)
2368 (when (not (string-match keyword
2369 target))
2370 keyword))
2371 rcirc-keywords))))
2372 (when keywords
2373 (while (re-search-forward (regexp-opt keywords 'words) nil t)
2374 (rcirc-add-face (match-beginning 0) (match-end 0) 'rcirc-keyword)
2375 (rcirc-record-activity (current-buffer) 'keyword))))))
2377 (defun rcirc-markup-bright-nicks (sender response)
2378 (when (and rcirc-bright-nicks
2379 (string= response "NAMES"))
2380 (with-syntax-table rcirc-nick-syntax-table
2381 (while (re-search-forward (regexp-opt rcirc-bright-nicks 'words) nil t)
2382 (rcirc-add-face (match-beginning 0) (match-end 0)
2383 'rcirc-bright-nick)))))
2385 (defun rcirc-markup-fill (sender response)
2386 (when (not (string= response "372")) ; /motd
2387 (let ((fill-prefix
2388 (or rcirc-fill-prefix
2389 (make-string (- (point) (line-beginning-position)) ?\s)))
2390 (fill-column (- (cond ((eq rcirc-fill-column 'frame-width)
2391 (1- (frame-width)))
2392 (rcirc-fill-column
2393 rcirc-fill-column)
2394 (t fill-column))
2395 ;; make sure ... doesn't cause line wrapping
2396 3)))
2397 (fill-region (point) (point-max) nil t))))
2399 ;;; handlers
2400 ;; these are called with the server PROCESS, the SENDER, which is a
2401 ;; server or a user, depending on the command, the ARGS, which is a
2402 ;; list of strings, and the TEXT, which is the original server text,
2403 ;; verbatim
2404 (defun rcirc-handler-001 (process sender args text)
2405 (rcirc-handler-generic process "001" sender args text)
2406 (with-rcirc-process-buffer process
2407 (setq rcirc-connecting nil)
2408 (rcirc-reschedule-timeout process)
2409 (setq rcirc-server-name sender)
2410 (setq rcirc-nick (car args))
2411 (rcirc-update-prompt)
2412 (when rcirc-auto-authenticate-flag (rcirc-authenticate))
2413 (rcirc-join-channels process rcirc-startup-channels)))
2415 (defun rcirc-handler-PRIVMSG (process sender args text)
2416 (let ((target (if (rcirc-channel-p (car args))
2417 (car args)
2418 sender))
2419 (message (or (cadr args) "")))
2420 (if (string-match "^\C-a\\(.*\\)\C-a$" message)
2421 (rcirc-handler-CTCP process target sender (match-string 1 message))
2422 (rcirc-print process sender "PRIVMSG" target message t))
2423 ;; update nick linestamp
2424 (with-current-buffer (rcirc-get-buffer process target t)
2425 (rcirc-put-nick-channel process sender target rcirc-current-line))))
2427 (defun rcirc-handler-NOTICE (process sender args text)
2428 (let ((target (car args))
2429 (message (cadr args)))
2430 (if (string-match "^\C-a\\(.*\\)\C-a$" message)
2431 (rcirc-handler-CTCP-response process target sender
2432 (match-string 1 message))
2433 (rcirc-print process sender "NOTICE"
2434 (cond ((rcirc-channel-p target)
2435 target)
2436 ;;; -ChanServ- [#gnu] Welcome...
2437 ((string-match "\\[\\(#[^\] ]+\\)\\]" message)
2438 (match-string 1 message))
2439 (sender
2440 (if (string= sender (rcirc-server-name process))
2441 nil ; server notice
2442 sender)))
2443 message t))))
2445 (defun rcirc-handler-WALLOPS (process sender args text)
2446 (rcirc-print process sender "WALLOPS" sender (car args) t))
2448 (defun rcirc-handler-JOIN (process sender args text)
2449 (let ((channel (car args)))
2450 (with-current-buffer (rcirc-get-buffer-create process channel)
2451 ;; when recently rejoining, restore the linestamp
2452 (rcirc-put-nick-channel process sender channel
2453 (let ((last-activity-lines
2454 (rcirc-elapsed-lines process sender channel)))
2455 (when (and last-activity-lines
2456 (< last-activity-lines rcirc-omit-threshold))
2457 (rcirc-last-line process sender channel)))))
2459 (rcirc-print process sender "JOIN" channel "")
2461 ;; print in private chat buffer if it exists
2462 (when (rcirc-get-buffer (rcirc-buffer-process) sender)
2463 (rcirc-print process sender "JOIN" sender channel))))
2465 ;; PART and KICK are handled the same way
2466 (defun rcirc-handler-PART-or-KICK (process response channel sender nick args)
2467 (rcirc-ignore-update-automatic nick)
2468 (if (not (string= nick (rcirc-nick process)))
2469 ;; this is someone else leaving
2470 (progn
2471 (rcirc-maybe-remember-nick-quit process nick channel)
2472 (rcirc-remove-nick-channel process nick channel))
2473 ;; this is us leaving
2474 (mapc (lambda (n)
2475 (rcirc-remove-nick-channel process n channel))
2476 (rcirc-channel-nicks process channel))
2478 ;; if the buffer is still around, make it inactive
2479 (let ((buffer (rcirc-get-buffer process channel)))
2480 (when buffer
2481 (rcirc-disconnect-buffer buffer)))))
2483 (defun rcirc-handler-PART (process sender args text)
2484 (let* ((channel (car args))
2485 (reason (cadr args))
2486 (message (concat channel " " reason)))
2487 (rcirc-print process sender "PART" channel message)
2488 ;; print in private chat buffer if it exists
2489 (when (rcirc-get-buffer (rcirc-buffer-process) sender)
2490 (rcirc-print process sender "PART" sender message))
2492 (rcirc-handler-PART-or-KICK process "PART" channel sender sender reason)))
2494 (defun rcirc-handler-KICK (process sender args text)
2495 (let* ((channel (car args))
2496 (nick (cadr args))
2497 (reason (caddr args))
2498 (message (concat nick " " channel " " reason)))
2499 (rcirc-print process sender "KICK" channel message t)
2500 ;; print in private chat buffer if it exists
2501 (when (rcirc-get-buffer (rcirc-buffer-process) nick)
2502 (rcirc-print process sender "KICK" nick message))
2504 (rcirc-handler-PART-or-KICK process "KICK" channel sender nick reason)))
2506 (defun rcirc-maybe-remember-nick-quit (process nick channel)
2507 "Remember NICK as leaving CHANNEL if they recently spoke."
2508 (let ((elapsed-lines (rcirc-elapsed-lines process nick channel)))
2509 (when (and elapsed-lines
2510 (< elapsed-lines rcirc-omit-threshold))
2511 (let ((buffer (rcirc-get-buffer process channel)))
2512 (when buffer
2513 (with-current-buffer buffer
2514 (let ((record (assoc-string nick rcirc-recent-quit-alist t))
2515 (line (rcirc-last-line process nick channel)))
2516 (if record
2517 (setcdr record line)
2518 (setq rcirc-recent-quit-alist
2519 (cons (cons nick line)
2520 rcirc-recent-quit-alist))))))))))
2522 (defun rcirc-handler-QUIT (process sender args text)
2523 (rcirc-ignore-update-automatic sender)
2524 (mapc (lambda (channel)
2525 ;; broadcast quit message each channel
2526 (rcirc-print process sender "QUIT" channel (apply 'concat args))
2527 ;; record nick in quit table if they recently spoke
2528 (rcirc-maybe-remember-nick-quit process sender channel))
2529 (rcirc-nick-channels process sender))
2530 (rcirc-nick-remove process sender))
2532 (defun rcirc-handler-NICK (process sender args text)
2533 (let* ((old-nick sender)
2534 (new-nick (car args))
2535 (channels (rcirc-nick-channels process old-nick)))
2536 ;; update list of ignored nicks
2537 (rcirc-ignore-update-automatic old-nick)
2538 (when (member old-nick rcirc-ignore-list)
2539 (add-to-list 'rcirc-ignore-list new-nick)
2540 (add-to-list 'rcirc-ignore-list-automatic new-nick))
2541 ;; print message to nick's channels
2542 (dolist (target channels)
2543 (rcirc-print process sender "NICK" target new-nick))
2544 ;; update private chat buffer, if it exists
2545 (let ((chat-buffer (rcirc-get-buffer process old-nick)))
2546 (when chat-buffer
2547 (with-current-buffer chat-buffer
2548 (rcirc-print process sender "NICK" old-nick new-nick)
2549 (setq rcirc-target new-nick)
2550 (rename-buffer (rcirc-generate-new-buffer-name process new-nick)))))
2551 ;; remove old nick and add new one
2552 (with-rcirc-process-buffer process
2553 (let ((v (gethash old-nick rcirc-nick-table)))
2554 (remhash old-nick rcirc-nick-table)
2555 (puthash new-nick v rcirc-nick-table))
2556 ;; if this is our nick...
2557 (when (string= old-nick rcirc-nick)
2558 (setq rcirc-nick new-nick)
2559 (rcirc-update-prompt t)
2560 ;; reauthenticate
2561 (when rcirc-auto-authenticate-flag (rcirc-authenticate))))))
2563 (defun rcirc-handler-PING (process sender args text)
2564 (rcirc-send-string process (concat "PONG :" (car args))))
2566 (defun rcirc-handler-PONG (process sender args text)
2567 ;; do nothing
2570 (defun rcirc-handler-TOPIC (process sender args text)
2571 (let ((topic (cadr args)))
2572 (rcirc-print process sender "TOPIC" (car args) topic)
2573 (with-current-buffer (rcirc-get-buffer process (car args))
2574 (setq rcirc-topic topic))))
2576 (defvar rcirc-nick-away-alist nil)
2577 (defun rcirc-handler-301 (process sender args text)
2578 "RPL_AWAY"
2579 (let* ((nick (cadr args))
2580 (rec (assoc-string nick rcirc-nick-away-alist))
2581 (away-message (caddr args)))
2582 (when (or (not rec)
2583 (not (string= (cdr rec) away-message)))
2584 ;; away message has changed
2585 (rcirc-handler-generic process "AWAY" nick (cdr args) text)
2586 (if rec
2587 (setcdr rec away-message)
2588 (setq rcirc-nick-away-alist (cons (cons nick away-message)
2589 rcirc-nick-away-alist))))))
2591 (defun rcirc-handler-332 (process sender args text)
2592 "RPL_TOPIC"
2593 (let ((buffer (or (rcirc-get-buffer process (cadr args))
2594 (rcirc-get-temp-buffer-create process (cadr args)))))
2595 (with-current-buffer buffer
2596 (setq rcirc-topic (caddr args)))))
2598 (defun rcirc-handler-333 (process sender args text)
2599 "Not in rfc1459.txt"
2600 (let ((buffer (or (rcirc-get-buffer process (cadr args))
2601 (rcirc-get-temp-buffer-create process (cadr args)))))
2602 (with-current-buffer buffer
2603 (let ((setter (caddr args))
2604 (time (current-time-string
2605 (seconds-to-time
2606 (string-to-number (cadddr args))))))
2607 (rcirc-print process sender "TOPIC" (cadr args)
2608 (format "%s (%s on %s)" rcirc-topic setter time))))))
2610 (defun rcirc-handler-477 (process sender args text)
2611 "ERR_NOCHANMODES"
2612 (rcirc-print process sender "477" (cadr args) (caddr args)))
2614 (defun rcirc-handler-MODE (process sender args text)
2615 (let ((target (car args))
2616 (msg (mapconcat 'identity (cdr args) " ")))
2617 (rcirc-print process sender "MODE"
2618 (if (string= target (rcirc-nick process))
2620 target)
2621 msg)
2623 ;; print in private chat buffers if they exist
2624 (mapc (lambda (nick)
2625 (when (rcirc-get-buffer process nick)
2626 (rcirc-print process sender "MODE" nick msg)))
2627 (cddr args))))
2629 (defun rcirc-get-temp-buffer-create (process channel)
2630 "Return a buffer based on PROCESS and CHANNEL."
2631 (let ((tmpnam (concat " " (downcase channel) "TMP" (process-name process))))
2632 (get-buffer-create tmpnam)))
2634 (defun rcirc-handler-353 (process sender args text)
2635 "RPL_NAMREPLY"
2636 (let ((channel (caddr args)))
2637 (mapc (lambda (nick)
2638 (rcirc-put-nick-channel process nick channel))
2639 (split-string (cadddr args) " " t))
2640 (with-current-buffer (rcirc-get-temp-buffer-create process channel)
2641 (goto-char (point-max))
2642 (insert (car (last args)) " "))))
2644 (defun rcirc-handler-366 (process sender args text)
2645 "RPL_ENDOFNAMES"
2646 (let* ((channel (cadr args))
2647 (buffer (rcirc-get-temp-buffer-create process channel)))
2648 (with-current-buffer buffer
2649 (rcirc-print process sender "NAMES" channel
2650 (let ((content (buffer-substring (point-min) (point-max))))
2651 (rcirc-sort-nicknames-join content " "))))
2652 (kill-buffer buffer)))
2654 (defun rcirc-handler-433 (process sender args text)
2655 "ERR_NICKNAMEINUSE"
2656 (rcirc-handler-generic process "433" sender args text)
2657 (let* ((new-nick (concat (cadr args) "`")))
2658 (with-rcirc-process-buffer process
2659 (rcirc-cmd-nick new-nick nil process))))
2661 (defun rcirc-authenticate ()
2662 "Send authentication to process associated with current buffer.
2663 Passwords are stored in `rcirc-authinfo' (which see)."
2664 (interactive)
2665 (with-rcirc-server-buffer
2666 (dolist (i rcirc-authinfo)
2667 (let ((process (rcirc-buffer-process))
2668 (server (car i))
2669 (nick (caddr i))
2670 (method (cadr i))
2671 (args (cdddr i)))
2672 (when (and (string-match server rcirc-server)
2673 (string-match nick rcirc-nick))
2674 (cond ((equal method 'nickserv)
2675 (rcirc-send-string
2676 process
2677 (concat "PRIVMSG " (or (cadr args) "nickserv")
2678 " :identify " (car args))))
2679 ((equal method 'chanserv)
2680 (rcirc-send-string
2681 process
2682 (concat
2683 "PRIVMSG chanserv :identify "
2684 (car args) " " (cadr args))))
2685 ((equal method 'bitlbee)
2686 (rcirc-send-string
2687 process
2688 (concat "PRIVMSG &bitlbee :identify " (car args))))
2690 (message "No %S authentication method defined"
2691 method))))))))
2693 (defun rcirc-handler-INVITE (process sender args text)
2694 (rcirc-print process sender "INVITE" nil (mapconcat 'identity args " ") t))
2696 (defun rcirc-handler-ERROR (process sender args text)
2697 (rcirc-print process sender "ERROR" nil (mapconcat 'identity args " ")))
2699 (defun rcirc-handler-CTCP (process target sender text)
2700 (if (string-match "^\\([^ ]+\\) *\\(.*\\)$" text)
2701 (let* ((request (upcase (match-string 1 text)))
2702 (args (match-string 2 text))
2703 (handler (intern-soft (concat "rcirc-handler-ctcp-" request))))
2704 (if (not (fboundp handler))
2705 (rcirc-print process sender "ERROR" target
2706 (format "%s sent unsupported ctcp: %s" sender text)
2708 (funcall handler process target sender args)
2709 (unless (or (string= request "ACTION")
2710 (string= request "KEEPALIVE"))
2711 (rcirc-print process sender "CTCP" target
2712 (format "%s" text) t))))))
2714 (defun rcirc-handler-ctcp-VERSION (process target sender args)
2715 (rcirc-send-string process
2716 (concat "NOTICE " sender
2717 " :\C-aVERSION " rcirc-id-string
2718 "\C-a")))
2720 (defun rcirc-handler-ctcp-ACTION (process target sender args)
2721 (rcirc-print process sender "ACTION" target args t))
2723 (defun rcirc-handler-ctcp-TIME (process target sender args)
2724 (rcirc-send-string process
2725 (concat "NOTICE " sender
2726 " :\C-aTIME " (current-time-string) "\C-a")))
2728 (defun rcirc-handler-CTCP-response (process target sender message)
2729 (rcirc-print process sender "CTCP" nil message t))
2731 (defgroup rcirc-faces nil
2732 "Faces for rcirc."
2733 :group 'rcirc
2734 :group 'faces)
2736 (defface rcirc-my-nick ; font-lock-function-name-face
2737 '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
2738 (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
2739 (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
2740 (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
2741 (((class color) (min-colors 8)) (:foreground "blue" :weight bold))
2742 (t (:inverse-video t :weight bold)))
2743 "The face used to highlight my messages."
2744 :group 'rcirc-faces)
2746 (defface rcirc-other-nick ; font-lock-variable-name-face
2747 '((((class grayscale) (background light))
2748 (:foreground "Gray90" :weight bold :slant italic))
2749 (((class grayscale) (background dark))
2750 (:foreground "DimGray" :weight bold :slant italic))
2751 (((class color) (min-colors 88) (background light)) (:foreground "DarkGoldenrod"))
2752 (((class color) (min-colors 88) (background dark)) (:foreground "LightGoldenrod"))
2753 (((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
2754 (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
2755 (((class color) (min-colors 8)) (:foreground "yellow" :weight light))
2756 (t (:weight bold :slant italic)))
2757 "The face used to highlight other messages."
2758 :group 'rcirc-faces)
2760 (defface rcirc-bright-nick
2761 '((((class grayscale) (background light))
2762 (:foreground "LightGray" :weight bold :underline t))
2763 (((class grayscale) (background dark))
2764 (:foreground "Gray50" :weight bold :underline t))
2765 (((class color) (min-colors 88) (background light)) (:foreground "CadetBlue"))
2766 (((class color) (min-colors 88) (background dark)) (:foreground "Aquamarine"))
2767 (((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
2768 (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
2769 (((class color) (min-colors 8)) (:foreground "magenta"))
2770 (t (:weight bold :underline t)))
2771 "Face used for nicks matched by `rcirc-bright-nicks'."
2772 :group 'rcirc-faces)
2774 (defface rcirc-dim-nick
2775 '((t :inherit default))
2776 "Face used for nicks in `rcirc-dim-nicks'."
2777 :group 'rcirc-faces)
2779 (defface rcirc-server ; font-lock-comment-face
2780 '((((class grayscale) (background light))
2781 (:foreground "DimGray" :weight bold :slant italic))
2782 (((class grayscale) (background dark))
2783 (:foreground "LightGray" :weight bold :slant italic))
2784 (((class color) (min-colors 88) (background light))
2785 (:foreground "Firebrick"))
2786 (((class color) (min-colors 88) (background dark))
2787 (:foreground "chocolate1"))
2788 (((class color) (min-colors 16) (background light))
2789 (:foreground "red"))
2790 (((class color) (min-colors 16) (background dark))
2791 (:foreground "red1"))
2792 (((class color) (min-colors 8) (background light))
2794 (((class color) (min-colors 8) (background dark))
2796 (t (:weight bold :slant italic)))
2797 "The face used to highlight server messages."
2798 :group 'rcirc-faces)
2800 (defface rcirc-server-prefix ; font-lock-comment-delimiter-face
2801 '((default :inherit rcirc-server)
2802 (((class grayscale)))
2803 (((class color) (min-colors 16)))
2804 (((class color) (min-colors 8) (background light))
2805 :foreground "red")
2806 (((class color) (min-colors 8) (background dark))
2807 :foreground "red1"))
2808 "The face used to highlight server prefixes."
2809 :group 'rcirc-faces)
2811 (defface rcirc-timestamp
2812 '((t (:inherit default)))
2813 "The face used to highlight timestamps."
2814 :group 'rcirc-faces)
2816 (defface rcirc-nick-in-message ; font-lock-keyword-face
2817 '((((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
2818 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
2819 (((class color) (min-colors 88) (background light)) (:foreground "Purple"))
2820 (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
2821 (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
2822 (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
2823 (((class color) (min-colors 8)) (:foreground "cyan" :weight bold))
2824 (t (:weight bold)))
2825 "The face used to highlight instances of your nick within messages."
2826 :group 'rcirc-faces)
2828 (defface rcirc-nick-in-message-full-line
2829 '((t (:bold t)))
2830 "The face used emphasize the entire message when your nick is mentioned."
2831 :group 'rcirc-faces)
2833 (defface rcirc-prompt ; comint-highlight-prompt
2834 '((((min-colors 88) (background dark)) (:foreground "cyan1"))
2835 (((background dark)) (:foreground "cyan"))
2836 (t (:foreground "dark blue")))
2837 "The face used to highlight prompts."
2838 :group 'rcirc-faces)
2840 (defface rcirc-track-nick
2841 '((((type tty)) (:inherit default))
2842 (t (:inverse-video t)))
2843 "The face used in the mode-line when your nick is mentioned."
2844 :group 'rcirc-faces)
2846 (defface rcirc-track-keyword
2847 '((t (:bold t )))
2848 "The face used in the mode-line when keywords are mentioned."
2849 :group 'rcirc-faces)
2851 (defface rcirc-url
2852 '((t (:bold t)))
2853 "The face used to highlight urls."
2854 :group 'rcirc-faces)
2856 (defface rcirc-keyword
2857 '((t (:inherit highlight)))
2858 "The face used to highlight keywords."
2859 :group 'rcirc-faces)
2862 ;; When using M-x flyspell-mode, only check words after the prompt
2863 (put 'rcirc-mode 'flyspell-mode-predicate 'rcirc-looking-at-input)
2864 (defun rcirc-looking-at-input ()
2865 "Returns true if point is past the input marker."
2866 (>= (point) rcirc-prompt-end-marker))
2869 (provide 'rcirc)
2871 ;;; rcirc.el ends here