1 ;;; rcirc.el --- default, simple IRC client.
3 ;; Copyright (C) 2005, 2006 Free Software Foundation, Inc.
6 ;; URL: http://www.nongnu.org/rcirc
9 ;; This file is part of GNU Emacs.
11 ;; This file is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This file is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; Internet Relay Chat (IRC) is a form of instant communication over
29 ;; the Internet. It is mainly designed for group (many-to-many)
30 ;; communication in discussion forums called channels, but also allows
31 ;; one-to-one communication.
33 ;; Rcirc has simple defaults and clear and consistent behaviour.
34 ;; Message arrival timestamps, activity notification on the modeline,
35 ;; message filling, nick completion, and keepalive pings are all
36 ;; enabled by default, but can easily be adjusted or turned off. Each
37 ;; discussion takes place in its own buffer and there is a single
38 ;; server buffer per connection.
40 ;; Open a new irc connection with:
47 (eval-when-compile (require 'cl
))
53 :link
'(custom-manual "(rcirc)")
56 (defcustom rcirc-default-server
"irc.freenode.net"
57 "The default server to connect to."
61 (defcustom rcirc-default-port
6667
62 "The default port to connect to."
66 (defcustom rcirc-default-nick
(user-login-name)
71 (defcustom rcirc-default-user-name
(user-login-name)
72 "Your user name sent to the server when connecting."
76 (defcustom rcirc-default-user-full-name
(if (string= (user-full-name) "")
79 "The full name sent to the server when connecting."
83 (defcustom rcirc-startup-channels-alist
'(("^irc.freenode.net$" "#emacs"))
84 "Alist of channels to join at startup.
85 Each element looks like (SERVER-REGEXP . CHANNEL-LIST)."
86 :type
'(alist :key-type string
:value-type
(repeat string
))
89 (defcustom rcirc-fill-flag t
90 "*Non-nil means line-wrap messages printed in channel buffers."
94 (defcustom rcirc-fill-column nil
95 "*Column beyond which automatic line-wrapping should happen.
96 If nil, use value of `fill-column'. If 'frame-width, use the
98 :type
'(choice (const :tag
"Value of `fill-column'")
99 (const :tag
"Full frame width" frame-width
)
100 (integer :tag
"Number of columns"))
103 (defcustom rcirc-fill-prefix nil
104 "*Text to insert before filled lines.
105 If nil, calculate the prefix dynamically to line up text
106 underneath each nick."
107 :type
'(choice (const :tag
"Dynamic" nil
)
108 (string :tag
"Prefix text"))
111 (defvar rcirc-ignore-buffer-activity-flag nil
112 "If non-nil, ignore activity in this buffer.")
113 (make-variable-buffer-local 'rcirc-ignore-buffer-activity-flag
)
115 (defvar rcirc-low-priority-flag nil
116 "If non-nil, activity in this buffer is considered low priority.")
117 (make-variable-buffer-local 'rcirc-low-priority-flag
)
119 (defcustom rcirc-time-format
"%H:%M "
120 "*Describes how timestamps are printed.
121 Used as the first arg to `format-time-string'."
125 (defcustom rcirc-input-ring-size
1024
126 "*Size of input history ring."
130 (defcustom rcirc-read-only-flag t
131 "*Non-nil means make text in IRC buffers read-only."
135 (defcustom rcirc-buffer-maximum-lines nil
136 "*The maximum size in lines for rcirc buffers.
137 Channel buffers are truncated from the top to be no greater than this
138 number. If zero or nil, no truncating is done."
139 :type
'(choice (const :tag
"No truncation" nil
)
140 (integer :tag
"Number of lines"))
143 (defcustom rcirc-authinfo nil
144 "List of authentication passwords.
145 Each element of the list is a list with a SERVER-REGEXP string
146 and a method symbol followed by method specific arguments.
148 The valid METHOD symbols are `nickserv', `chanserv' and
151 The required ARGUMENTS for each METHOD symbol are:
152 `nickserv': NICK PASSWORD
153 `chanserv': NICK CHANNEL PASSWORD
154 `bitlbee': NICK PASSWORD
157 ((\"freenode\" nickserv \"bob\" \"p455w0rd\")
158 (\"freenode\" chanserv \"bob\" \"#bobland\" \"passwd99\")
159 (\"bitlbee\" bitlbee \"robert\" \"sekrit\"))"
160 :type
'(alist :key-type
(string :tag
"Server")
161 :value-type
(choice (list :tag
"NickServ"
164 (string :tag
"Password"))
165 (list :tag
"ChanServ"
168 (string :tag
"Channel")
169 (string :tag
"Password"))
173 (string :tag
"Password"))))
176 (defcustom rcirc-auto-authenticate-flag t
177 "*Non-nil means automatically send authentication string to server.
178 See also `rcirc-authinfo'."
182 (defcustom rcirc-prompt
"> "
183 "Prompt string to use in IRC buffers.
185 The following replacements are made:
188 %t is the buffer target, a channel or a user.
190 Setting this alone will not affect the prompt;
191 use either M-x customize or also call `rcirc-update-prompt'."
193 :set
'rcirc-set-changed
194 :initialize
'custom-initialize-default
197 (defcustom rcirc-ignore-list
()
198 "List of ignored nicks.
199 Use /ignore to list them, use /ignore NICK to add or remove a nick."
200 :type
'(repeat string
)
203 (defvar rcirc-ignore-list-automatic
()
204 "List of ignored nicks added to `rcirc-ignore-list' because of renaming.
205 When an ignored person renames, their nick is added to both lists.
206 Nicks will be removed from the automatic list on follow-up renamings or
209 (defcustom rcirc-print-hooks nil
210 "Hook run after text is printed.
211 Called with 5 arguments, PROCESS, SENDER, RESPONSE, TARGET and TEXT."
215 (defcustom rcirc-always-use-server-buffer-flag nil
216 "Non-nil means messages without a channel target will go to the server buffer."
220 (defcustom rcirc-decode-coding-system
'undecided
221 "Coding system used to decode incoming irc messages."
225 (defcustom rcirc-encode-coding-system
'utf-8
226 "Coding system used to encode outgoing irc messages."
230 (defcustom rcirc-coding-system-alist nil
231 "Alist to decide a coding system to use for a file I/O operation.
232 The format is ((PATTERN . VAL) ...).
233 PATTERN is either a string or a cons of strings.
234 If PATTERN is a string, it is used to match a target.
235 If PATTERN is a cons of strings, the car part is used to match a
236 target, and the cdr part is used to match a server.
237 VAL is either a coding system or a cons of coding systems.
238 If VAL is a coding system, it is used for both decoding and encoding
240 If VAL is a cons of coding systems, the car part is used for decoding,
241 and the cdr part is used for encoding."
242 :type
'(alist :key-type
(choice (string :tag
"Channel Regexp")
243 (cons (string :tag
"Channel Regexp")
244 (string :tag
"Server Regexp")))
245 :value-type
(choice coding-system
246 (cons (coding-system :tag
"Decode")
247 (coding-system :tag
"Encode"))))
250 (defcustom rcirc-multiline-major-mode
'fundamental-mode
251 "Major-mode function to use in multiline edit buffers."
255 (defvar rcirc-nick nil
)
257 (defvar rcirc-prompt-start-marker nil
)
258 (defvar rcirc-prompt-end-marker nil
)
260 (defvar rcirc-nick-table nil
)
262 (defvar rcirc-nick-syntax-table
263 (let ((table (make-syntax-table text-mode-syntax-table
)))
264 (mapc (lambda (c) (modify-syntax-entry c
"w" table
))
266 (modify-syntax-entry ?
' "_" table
)
268 "Syntax table which includes all nick characters as word constituents.")
270 ;; each process has an alist of (target . buffer) pairs
271 (defvar rcirc-buffer-alist nil
)
273 (defvar rcirc-activity nil
274 "List of buffers with unviewed activity.")
276 (defvar rcirc-activity-string
""
277 "String displayed in modeline representing `rcirc-activity'.")
278 (put 'rcirc-activity-string
'risky-local-variable t
)
280 (defvar rcirc-server-buffer nil
281 "The server buffer associated with this channel buffer.")
283 (defvar rcirc-target nil
284 "The channel or user associated with this buffer.")
286 (defvar rcirc-urls nil
287 "List of urls seen in the current buffer.")
289 (defvar rcirc-keepalive-seconds
60
290 "Number of seconds between keepalive pings.
291 If nil, do not send keepalive pings.")
293 (defconst rcirc-id-string
(concat "rcirc on GNU Emacs " emacs-version
))
295 (defvar rcirc-startup-channels nil
)
299 If ARG is non-nil, prompt for a server to connect to."
302 (let* ((server (read-string "IRC Server: " rcirc-default-server
))
303 (port (read-string "IRC Port: " (number-to-string rcirc-default-port
)))
304 (nick (read-string "IRC Nick: " rcirc-default-nick
))
305 (channels (split-string
306 (read-string "IRC Channels: "
307 (mapconcat 'identity
(rcirc-startup-channels server
) " "))
309 (rcirc-connect server port nick rcirc-default-user-name rcirc-default-user-full-name
311 ;; make new connection using defaults unless already connected to
312 ;; the default rcirc-server
314 (dolist (p (rcirc-process-list))
315 (when (string= rcirc-default-server
(process-name p
))
318 (rcirc-connect rcirc-default-server rcirc-default-port
319 rcirc-default-nick rcirc-default-user-name
320 rcirc-default-user-full-name
321 (rcirc-startup-channels rcirc-default-server
))
322 (switch-to-buffer (process-buffer connected
))
323 (message "Connected to %s"
324 (process-contact (get-buffer-process (current-buffer))
327 (defalias 'irc
'rcirc
)
330 (defvar rcirc-process-output nil
)
331 (defvar rcirc-topic nil
)
332 (defvar rcirc-keepalive-timer nil
)
333 (defvar rcirc-last-server-message-time nil
)
334 (defvar rcirc-server nil
)
337 (defun rcirc-connect (&optional server port nick user-name full-name startup-channels
)
339 (message "Connecting to %s..." server
)
340 (let* ((inhibit-eol-conversion)
341 (port-number (if port
343 (string-to-number port
)
346 (server (or server rcirc-default-server
))
347 (nick (or nick rcirc-default-nick
))
348 (user-name (or user-name rcirc-default-user-name
))
349 (full-name (or full-name rcirc-default-user-full-name
))
350 (startup-channels startup-channels
)
351 (process (open-network-stream server nil server port-number
)))
353 (set-process-coding-system process
'raw-text
'raw-text
)
354 (switch-to-buffer (rcirc-generate-new-buffer-name process nil
))
355 (set-process-buffer process
(current-buffer))
356 (rcirc-mode process nil
)
357 (set-process-sentinel process
'rcirc-sentinel
)
358 (set-process-filter process
'rcirc-filter
)
359 (make-local-variable 'rcirc-server
)
360 (setq rcirc-server server
)
361 (make-local-variable 'rcirc-buffer-alist
)
362 (setq rcirc-buffer-alist nil
)
363 (make-local-variable 'rcirc-nick-table
)
364 (setq rcirc-nick-table
(make-hash-table :test
'equal
))
365 (make-local-variable 'rcirc-nick
)
366 (setq rcirc-nick nick
)
367 (make-local-variable 'rcirc-process-output
)
368 (setq rcirc-process-output nil
)
369 (make-local-variable 'rcirc-startup-channels
)
370 (setq rcirc-startup-channels startup-channels
)
371 (make-local-variable 'rcirc-last-server-message-time
)
372 (setq rcirc-last-server-message-time
(current-time))
375 (rcirc-send-string process
(concat "NICK " nick
))
376 (rcirc-send-string process
(concat "USER " user-name
377 " hostname servername :"
380 ;; setup ping timer if necessary
381 (when rcirc-keepalive-seconds
382 (unless rcirc-keepalive-timer
383 (setq rcirc-keepalive-timer
384 (run-at-time 0 rcirc-keepalive-seconds
'rcirc-keepalive
))))
386 (message "Connecting to %s...done" server
)
388 ;; return process object
391 (defmacro with-rcirc-process-buffer
(process &rest body
)
392 (declare (indent 1) (debug t
))
393 `(with-current-buffer (process-buffer ,process
)
396 (defmacro with-rcirc-server-buffer
(&rest body
)
397 (declare (indent 0) (debug t
))
398 `(with-current-buffer rcirc-server-buffer
401 (defun rcirc-keepalive ()
402 "Send keep alive pings to active rcirc processes.
403 Kill processes that have not received a server message since the
405 (if (rcirc-process-list)
406 (mapc (lambda (process)
407 (with-rcirc-process-buffer process
408 (if (> (cadr (time-since rcirc-last-server-message-time
))
409 rcirc-keepalive-seconds
)
410 (kill-process process
)
411 (rcirc-send-string process
(concat "PING " rcirc-server
)))))
412 (rcirc-process-list))
413 (cancel-timer rcirc-keepalive-timer
)
414 (setq rcirc-keepalive-timer nil
)))
416 (defvar rcirc-debug-buffer
" *rcirc debug*")
417 (defvar rcirc-debug-flag nil
418 "If non-nil, write information to `rcirc-debug-buffer'.")
419 (defun rcirc-debug (process text
)
420 "Add an entry to the debug log including PROCESS and TEXT.
421 Debug text is written to `rcirc-debug-buffer' if `rcirc-debug-flag'
423 (when rcirc-debug-flag
425 (save-window-excursion
426 (set-buffer (get-buffer-create rcirc-debug-buffer
))
427 (goto-char (point-max))
430 (format-time-string "%Y-%m-%dT%T ") (process-name process
)
434 (defvar rcirc-sentinel-hooks nil
435 "Hook functions called when the process sentinel is called.
436 Functions are called with PROCESS and SENTINEL arguments.")
438 (defun rcirc-sentinel (process sentinel
)
439 "Called when PROCESS receives SENTINEL."
440 (let ((sentinel (replace-regexp-in-string "\n" "" sentinel
)))
441 (rcirc-debug process
(format "SENTINEL: %S %S\n" process sentinel
))
442 (with-rcirc-process-buffer process
443 (dolist (buffer (cons nil
(mapcar 'cdr rcirc-buffer-alist
)))
444 (with-current-buffer (or buffer
(current-buffer))
445 (rcirc-print process
"rcirc.el" "ERROR" rcirc-target
446 (format "%s: %s (%S)"
447 (process-name process
)
449 (process-status process
)) t
)
450 ;; remove the prompt from buffers
451 (let ((inhibit-read-only t
))
452 (delete-region rcirc-prompt-start-marker
453 rcirc-prompt-end-marker
)))))
454 (run-hook-with-args 'rcirc-sentinel-hooks process sentinel
)))
456 (defun rcirc-process-list ()
457 "Return a list of rcirc processes."
460 (when (process-buffer p
)
461 (with-rcirc-process-buffer p
462 (when (eq major-mode
'rcirc-mode
)
463 (setq ps
(cons p ps
))))))
467 (defvar rcirc-receive-message-hooks nil
468 "Hook functions run when a message is received from server.
469 Function is called with PROCESS, COMMAND, SENDER, ARGS and LINE.")
470 (defun rcirc-filter (process output
)
471 "Called when PROCESS receives OUTPUT."
472 (rcirc-debug process output
)
473 (with-rcirc-process-buffer process
474 (setq rcirc-last-server-message-time
(current-time))
475 (setq rcirc-process-output
(concat rcirc-process-output output
))
476 (when (= (aref rcirc-process-output
477 (1- (length rcirc-process-output
))) ?
\n)
479 (rcirc-process-server-response process line
))
480 (split-string rcirc-process-output
"[\n\r]" t
))
481 (setq rcirc-process-output nil
))))
483 (defvar rcirc-trap-errors-flag t
)
484 (defun rcirc-process-server-response (process text
)
485 (if rcirc-trap-errors-flag
487 (rcirc-process-server-response-1 process text
)
489 (rcirc-print process
"RCIRC" "ERROR" nil
490 (format "\"%s\" %s" text err
) t
)))
491 (rcirc-process-server-response-1 process text
)))
493 (defun rcirc-process-server-response-1 (process text
)
494 (if (string-match "^\\(:\\([^ ]+\\) \\)?\\([^ ]+\\) \\(.+\\)$" text
)
495 (let* ((user (match-string 2 text
))
496 (sender (rcirc-user-nick user
))
497 (cmd (match-string 3 text
))
498 (args (match-string 4 text
))
499 (handler (intern-soft (concat "rcirc-handler-" cmd
))))
500 (string-match "^\\([^:]*\\):?\\(.+\\)?$" args
)
501 (let* ((args1 (match-string 1 args
))
502 (args2 (match-string 2 args
))
503 (args (delq nil
(append (split-string args1
" " t
)
505 (if (not (fboundp handler
))
506 (rcirc-handler-generic process cmd sender args text
)
507 (funcall handler process sender args text
))
508 (run-hook-with-args 'rcirc-receive-message-hooks
509 process cmd sender args text
)))
510 (message "UNHANDLED: %s" text
)))
512 (defun rcirc-handler-generic (process command sender args text
)
513 "Generic server response handler."
514 (rcirc-print process sender command nil
515 (mapconcat 'identity
(cdr args
) " ") t
))
517 (defun rcirc-send-string (process string
)
518 "Send PROCESS a STRING plus a newline."
519 (let ((string (concat (encode-coding-string string rcirc-encode-coding-system
)
521 (unless (eq (process-status process
) 'open
)
522 (error "Network connection to %s is not open"
523 (process-name process
)))
524 (rcirc-debug process string
)
525 (process-send-string process string
)))
527 (defun rcirc-buffer-process (&optional buffer
)
528 "Return the process associated with channel BUFFER.
529 With no argument or nil as argument, use the current buffer."
530 (get-buffer-process (or buffer rcirc-server-buffer
)))
532 (defun rcirc-server-name (process)
533 "Return PROCESS server name, given by the 001 response."
534 (with-rcirc-process-buffer process
535 (or rcirc-server rcirc-default-server
)))
537 (defun rcirc-nick (process)
538 "Return PROCESS nick."
539 (with-rcirc-process-buffer process
540 (or rcirc-nick rcirc-default-nick
)))
542 (defun rcirc-buffer-nick (&optional buffer
)
543 "Return the nick associated with BUFFER.
544 With no argument or nil as argument, use the current buffer."
545 (with-current-buffer (or buffer
(current-buffer))
546 (with-current-buffer rcirc-server-buffer
547 (or rcirc-nick rcirc-default-nick
))))
549 (defvar rcirc-max-message-length
450
550 "Messages longer than this value will be split.")
552 (defun rcirc-send-message (process target message
&optional noticep
)
553 "Send TARGET associated with PROCESS a privmsg with text MESSAGE.
554 If NOTICEP is non-nil, send a notice instead of privmsg."
555 ;; max message length is 512 including CRLF
556 (let* ((response (if noticep
"NOTICE" "PRIVMSG"))
557 (oversize (> (length message
) rcirc-max-message-length
))
559 (substring message
0 rcirc-max-message-length
)
561 (text (if (string= text
"")
565 (substring message rcirc-max-message-length
))))
566 (rcirc-get-buffer-create process target
)
567 (rcirc-print process
(rcirc-nick process
) response target text
)
568 (rcirc-send-string process
(concat response
" " target
" :" text
))
569 (when more
(rcirc-send-message process target more noticep
))))
571 (defvar rcirc-input-ring nil
)
572 (defvar rcirc-input-ring-index
0)
573 (defun rcirc-prev-input-string (arg)
574 (ring-ref rcirc-input-ring
(+ rcirc-input-ring-index arg
)))
576 (defun rcirc-insert-prev-input (arg)
578 (when (<= rcirc-prompt-end-marker
(point))
579 (delete-region rcirc-prompt-end-marker
(point-max))
580 (insert (rcirc-prev-input-string 0))
581 (setq rcirc-input-ring-index
(1+ rcirc-input-ring-index
))))
583 (defun rcirc-insert-next-input (arg)
585 (when (<= rcirc-prompt-end-marker
(point))
586 (delete-region rcirc-prompt-end-marker
(point-max))
587 (setq rcirc-input-ring-index
(1- rcirc-input-ring-index
))
588 (insert (rcirc-prev-input-string -
1))))
590 (defvar rcirc-nick-completions nil
)
591 (defvar rcirc-nick-completion-start-offset nil
)
592 (defun rcirc-complete-nick ()
593 "Cycle through nick completions from list of nicks in channel."
595 (if (eq last-command
'rcirc-complete-nick
)
596 (setq rcirc-nick-completions
597 (append (cdr rcirc-nick-completions
)
598 (list (car rcirc-nick-completions
))))
599 (setq rcirc-nick-completion-start-offset
601 (if (re-search-backward " " rcirc-prompt-end-marker t
)
603 rcirc-prompt-end-marker
))
604 rcirc-prompt-end-marker
))
605 (setq rcirc-nick-completions
606 (let ((completion-ignore-case t
))
609 (+ rcirc-prompt-end-marker
610 rcirc-nick-completion-start-offset
)
612 (mapcar (lambda (x) (cons x nil
))
613 (rcirc-channel-nicks (rcirc-buffer-process)
615 (let ((completion (car rcirc-nick-completions
)))
617 (delete-region (+ rcirc-prompt-end-marker
618 rcirc-nick-completion-start-offset
)
620 (insert (concat completion
621 (if (= (+ rcirc-prompt-end-marker
622 rcirc-nick-completion-start-offset
)
623 rcirc-prompt-end-marker
)
626 (defun set-rcirc-decode-coding-system (coding-system)
627 "Set the decode coding system used in this channel."
628 (interactive "zCoding system for incoming messages: ")
629 (setq rcirc-decode-coding-system coding-system
))
631 (defun set-rcirc-encode-coding-system (coding-system)
632 "Set the encode coding system used in this channel."
633 (interactive "zCoding system for outgoing messages: ")
634 (setq rcirc-encode-coding-system coding-system
))
636 (defvar rcirc-mode-map
(make-sparse-keymap)
637 "Keymap for rcirc mode.")
639 (define-key rcirc-mode-map
(kbd "RET") 'rcirc-send-input
)
640 (define-key rcirc-mode-map
(kbd "M-p") 'rcirc-insert-prev-input
)
641 (define-key rcirc-mode-map
(kbd "M-n") 'rcirc-insert-next-input
)
642 (define-key rcirc-mode-map
(kbd "TAB") 'rcirc-complete-nick
)
643 (define-key rcirc-mode-map
(kbd "C-c C-b") 'rcirc-browse-url
)
644 (define-key rcirc-mode-map
(kbd "C-c C-c") 'rcirc-edit-multiline
)
645 (define-key rcirc-mode-map
(kbd "C-c C-j") 'rcirc-cmd-join
)
646 (define-key rcirc-mode-map
(kbd "C-c C-k") 'rcirc-cmd-kick
)
647 (define-key rcirc-mode-map
(kbd "C-c C-l") 'rcirc-toggle-low-priority
)
648 (define-key rcirc-mode-map
(kbd "C-c C-d") 'rcirc-cmd-mode
)
649 (define-key rcirc-mode-map
(kbd "C-c C-m") 'rcirc-cmd-msg
)
650 (define-key rcirc-mode-map
(kbd "C-c C-r") 'rcirc-cmd-nick
) ; rename
651 (define-key rcirc-mode-map
(kbd "C-c C-o") 'rcirc-cmd-oper
)
652 (define-key rcirc-mode-map
(kbd "C-c C-p") 'rcirc-cmd-part
)
653 (define-key rcirc-mode-map
(kbd "C-c C-q") 'rcirc-cmd-query
)
654 (define-key rcirc-mode-map
(kbd "C-c C-t") 'rcirc-cmd-topic
)
655 (define-key rcirc-mode-map
(kbd "C-c C-n") 'rcirc-cmd-names
)
656 (define-key rcirc-mode-map
(kbd "C-c C-w") 'rcirc-cmd-whois
)
657 (define-key rcirc-mode-map
(kbd "C-c C-x") 'rcirc-cmd-quit
)
658 (define-key rcirc-mode-map
(kbd "C-c TAB") ; C-i
659 'rcirc-toggle-ignore-buffer-activity
)
660 (define-key rcirc-mode-map
(kbd "C-c C-s") 'rcirc-switch-to-server-buffer
)
661 (define-key rcirc-mode-map
(kbd "C-c C-a") 'rcirc-jump-to-first-unread-line
)
663 (defvar rcirc-browse-url-map
(make-sparse-keymap)
664 "Keymap used for browsing URLs in `rcirc-mode'.")
666 (define-key rcirc-browse-url-map
(kbd "RET") 'rcirc-browse-url-at-point
)
667 (define-key rcirc-browse-url-map
(kbd "<mouse-2>") 'rcirc-browse-url-at-mouse
)
669 (defvar rcirc-short-buffer-name nil
670 "Generated abbreviation to use to indicate buffer activity.")
672 (defvar rcirc-mode-hook nil
673 "Hook run when setting up rcirc buffer.")
675 (defvar rcirc-last-post-time nil
)
677 (defun rcirc-mode (process target
)
678 "Major mode for IRC channel buffers.
681 (kill-all-local-variables)
682 (use-local-map rcirc-mode-map
)
683 (setq mode-name
"rcirc")
684 (setq major-mode
'rcirc-mode
)
686 (make-local-variable 'rcirc-input-ring
)
687 (setq rcirc-input-ring
(make-ring rcirc-input-ring-size
))
688 (make-local-variable 'rcirc-server-buffer
)
689 (setq rcirc-server-buffer
(process-buffer process
))
690 (make-local-variable 'rcirc-target
)
691 (setq rcirc-target target
)
692 (make-local-variable 'rcirc-topic
)
693 (setq rcirc-topic nil
)
694 (make-local-variable 'rcirc-last-post-time
)
695 (setq rcirc-last-post-time
(current-time))
697 (make-local-variable 'rcirc-short-buffer-name
)
698 (setq rcirc-short-buffer-name nil
)
699 (make-local-variable 'rcirc-urls
)
700 (setq rcirc-urls nil
)
701 (setq use-hard-newlines t
)
703 (make-local-variable 'rcirc-decode-coding-system
)
704 (make-local-variable 'rcirc-encode-coding-system
)
705 (dolist (i rcirc-coding-system-alist
)
706 (let ((chan (if (consp (car i
)) (caar i
) (car i
)))
707 (serv (if (consp (car i
)) (cdar i
) "")))
708 (when (and (string-match chan
(or target
""))
709 (string-match serv
(rcirc-server-name process
)))
710 (setq rcirc-decode-coding-system
(if (consp (cdr i
)) (cadr i
) i
)
711 rcirc-encode-coding-system
(if (consp (cdr i
)) (cddr i
) i
)))))
713 ;; setup the prompt and markers
714 (make-local-variable 'rcirc-prompt-start-marker
)
715 (setq rcirc-prompt-start-marker
(make-marker))
716 (set-marker rcirc-prompt-start-marker
(point-max))
717 (make-local-variable 'rcirc-prompt-end-marker
)
718 (setq rcirc-prompt-end-marker
(make-marker))
719 (set-marker rcirc-prompt-end-marker
(point-max))
720 (rcirc-update-prompt)
721 (goto-char rcirc-prompt-end-marker
)
722 (make-local-variable 'overlay-arrow-position
)
723 (setq overlay-arrow-position
(make-marker))
724 (set-marker overlay-arrow-position nil
)
726 ;; if the user changes the major mode or kills the buffer, there is
727 ;; cleanup work to do
728 (make-local-variable 'change-major-mode-hook
)
729 (add-hook 'change-major-mode-hook
'rcirc-change-major-mode-hook
)
730 (make-local-variable 'kill-buffer-hook
)
731 (add-hook 'kill-buffer-hook
'rcirc-kill-buffer-hook
)
733 ;; add to buffer list, and update buffer abbrevs
734 (when target
; skip server buffer
735 (let ((buffer (current-buffer)))
736 (with-rcirc-process-buffer process
737 (setq rcirc-buffer-alist
(cons (cons target buffer
)
738 rcirc-buffer-alist
))))
739 (rcirc-update-short-buffer-names))
741 (run-hooks 'rcirc-mode-hook
))
743 (defun rcirc-update-prompt (&optional all
)
744 "Reset the prompt string in the current buffer.
746 If ALL is non-nil, update prompts in all IRC buffers."
748 (mapc (lambda (process)
749 (mapc (lambda (buffer)
750 (with-current-buffer buffer
751 (rcirc-update-prompt)))
752 (with-rcirc-process-buffer process
753 (mapcar 'cdr rcirc-buffer-alist
))))
754 (rcirc-process-list))
755 (let ((inhibit-read-only t
)
756 (prompt (or rcirc-prompt
"")))
759 (replace-regexp-in-string (car rep
) (cdr rep
) prompt
)))
760 (list (cons "%n" (rcirc-buffer-nick))
761 (cons "%s" (with-rcirc-server-buffer (or rcirc-server
"")))
762 (cons "%t" (or rcirc-target
""))))
764 (delete-region rcirc-prompt-start-marker rcirc-prompt-end-marker
)
765 (goto-char rcirc-prompt-start-marker
)
766 (let ((start (point)))
767 (insert-before-markers prompt
)
768 (set-marker rcirc-prompt-start-marker start
)
769 (when (not (zerop (- rcirc-prompt-end-marker
770 rcirc-prompt-start-marker
)))
771 (add-text-properties rcirc-prompt-start-marker
772 rcirc-prompt-end-marker
773 (list 'face
'rcirc-prompt
774 'read-only t
'field t
775 'front-sticky t
'rear-nonsticky t
))))))))
777 (defun rcirc-set-changed (option value
)
778 "Set OPTION to VALUE and do updates after a customization change."
779 (set-default option value
)
780 (cond ((eq option
'rcirc-prompt
)
781 (rcirc-update-prompt 'all
))
783 (error "Bad option %s" option
))))
785 (defun rcirc-channel-p (target)
786 "Return t if TARGET is a channel name."
788 (not (zerop (length target
)))
789 (or (eq (aref target
0) ?
#)
790 (eq (aref target
0) ?
&))))
792 (defun rcirc-kill-buffer-hook ()
793 "Part the channel when killing an rcirc buffer."
794 (when (eq major-mode
'rcirc-mode
)
795 (rcirc-clean-up-buffer "Killed buffer")))
797 (defun rcirc-change-major-mode-hook ()
798 "Part the channel when changing the major-mode."
799 (rcirc-clean-up-buffer "Changed major mode"))
801 (defun rcirc-clean-up-buffer (reason)
802 (let ((buffer (current-buffer)))
803 (rcirc-clear-activity buffer
)
804 (when (and (rcirc-buffer-process)
805 (eq (process-status (rcirc-buffer-process)) 'open
))
806 (with-rcirc-server-buffer
807 (setq rcirc-buffer-alist
808 (rassq-delete-all buffer rcirc-buffer-alist
)))
809 (rcirc-update-short-buffer-names)
810 (if (rcirc-channel-p rcirc-target
)
811 (rcirc-send-string (rcirc-buffer-process)
812 (concat "PART " rcirc-target
" :" reason
))
814 (rcirc-remove-nick-channel (rcirc-buffer-process)
818 (defun rcirc-generate-new-buffer-name (process target
)
819 "Return a buffer name based on PROCESS and TARGET.
820 This is used for the initial name given to IRC buffers."
822 (concat target
"@" (process-name process
))
823 (concat "*" (process-name process
) "*")))
825 (defun rcirc-get-buffer (process target
&optional server
)
826 "Return the buffer associated with the PROCESS and TARGET.
828 If optional argument SERVER is non-nil, return the server buffer
829 if there is no existing buffer for TARGET, otherwise return nil."
830 (with-rcirc-process-buffer process
833 (let ((buffer (cdr (assoc-string target rcirc-buffer-alist t
))))
834 (or buffer
(when server
(current-buffer)))))))
836 (defun rcirc-get-buffer-create (process target
)
837 "Return the buffer associated with the PROCESS and TARGET.
838 Create the buffer if it doesn't exist."
839 (let ((buffer (rcirc-get-buffer process target
)))
840 (if (and buffer
(buffer-live-p buffer
))
841 (with-current-buffer buffer
842 (when (not rcirc-target
)
843 (setq rcirc-target target
))
846 (with-rcirc-process-buffer process
847 (let ((new-buffer (get-buffer-create
848 (rcirc-generate-new-buffer-name process target
))))
849 (with-current-buffer new-buffer
850 (rcirc-mode process target
))
851 (rcirc-put-nick-channel process
(rcirc-nick process
) target
)
854 (defun rcirc-send-input ()
855 "Send input to target associated with the current buffer."
857 (if (< (point) rcirc-prompt-end-marker
)
858 ;; copy the line down to the input area
861 (let ((start (if (eq (point) (point-min))
863 (if (get-text-property (1- (point)) 'hard
)
865 (previous-single-property-change (point) 'hard
))))
866 (end (next-single-property-change (1+ (point)) 'hard
)))
867 (goto-char (point-max))
868 (insert (replace-regexp-in-string
870 (buffer-substring-no-properties start end
)))))
872 (goto-char (point-max))
873 (when (not (equal 0 (- (point) rcirc-prompt-end-marker
)))
874 ;; delete a trailing newline
875 (when (eq (point) (point-at-bol))
876 (delete-backward-char 1))
877 (let ((input (buffer-substring-no-properties
878 rcirc-prompt-end-marker
(point))))
879 (dolist (line (split-string input
"\n"))
880 (rcirc-process-input-line line
))
883 (ring-insert rcirc-input-ring input
)
884 (setq rcirc-input-ring-index
0))))))
886 (defun rcirc-process-input-line (line)
887 (if (string-match "^/\\([^ ]+\\) ?\\(.*\\)$" line
)
888 (rcirc-process-command (match-string 1 line
)
889 (match-string 2 line
)
891 (rcirc-process-message line
)))
893 (defun rcirc-process-message (line)
894 (if (not rcirc-target
)
895 (message "Not joined (no target)")
896 (delete-region rcirc-prompt-end-marker
(point))
897 (rcirc-send-message (rcirc-buffer-process) rcirc-target line
)
898 (setq rcirc-last-post-time
(current-time))))
900 (defun rcirc-process-command (command args line
)
901 (if (eq (aref command
0) ?
/)
902 ;; "//text" will send "/text" as a message
903 (rcirc-process-message (substring line
1))
904 (let ((fun (intern-soft (concat "rcirc-cmd-" command
)))
905 (process (rcirc-buffer-process)))
907 (with-current-buffer (current-buffer)
908 (delete-region rcirc-prompt-end-marker
(point))
909 (if (string= command
"me")
910 (rcirc-print process
(rcirc-buffer-nick)
911 "ACTION" rcirc-target args
)
912 (rcirc-print process
(rcirc-buffer-nick)
913 "COMMAND" rcirc-target line
))
914 (set-marker rcirc-prompt-end-marker
(point))
916 (funcall fun args process rcirc-target
)
917 (rcirc-send-string process
918 (concat command
" " args
)))))))
920 (defvar rcirc-parent-buffer nil
)
921 (defvar rcirc-window-configuration nil
)
922 (defun rcirc-edit-multiline ()
923 "Move current edit to a dedicated buffer."
925 (let ((pos (1+ (- (point) rcirc-prompt-end-marker
))))
926 (goto-char (point-max))
927 (let ((text (buffer-substring rcirc-prompt-end-marker
(point)))
928 (parent (buffer-name)))
929 (delete-region rcirc-prompt-end-marker
(point))
930 (setq rcirc-window-configuration
(current-window-configuration))
931 (pop-to-buffer (concat "*multiline " parent
"*"))
932 (funcall rcirc-multiline-major-mode
)
933 (rcirc-multiline-minor-mode 1)
934 (setq rcirc-parent-buffer parent
)
936 (and (> pos
0) (goto-char pos
))
937 (message "Type C-c C-c to return text to %s, or C-c C-k to cancel" parent
))))
939 (defvar rcirc-multiline-minor-mode-map
(make-sparse-keymap)
940 "Keymap for multiline mode in rcirc.")
941 (define-key rcirc-multiline-minor-mode-map
942 (kbd "C-c C-c") 'rcirc-multiline-minor-submit
)
943 (define-key rcirc-multiline-minor-mode-map
944 (kbd "C-x C-s") 'rcirc-multiline-minor-submit
)
945 (define-key rcirc-multiline-minor-mode-map
946 (kbd "C-c C-k") 'rcirc-multiline-minor-cancel
)
947 (define-key rcirc-multiline-minor-mode-map
948 (kbd "ESC ESC ESC") 'rcirc-multiline-minor-cancel
)
950 (define-minor-mode rcirc-multiline-minor-mode
951 "Minor mode for editing multiple lines in rcirc."
953 :lighter
" rcirc-mline"
954 :keymap rcirc-multiline-minor-mode-map
957 (make-local-variable 'rcirc-parent-buffer
)
958 (put 'rcirc-parent-buffer
'permanent-local t
))
960 (defun rcirc-multiline-minor-submit ()
961 "Send the text in buffer back to parent buffer."
963 (assert rcirc-parent-buffer
)
964 (untabify (point-min) (point-max))
965 (let ((text (buffer-substring (point-min) (point-max)))
966 (buffer (current-buffer))
968 (set-buffer rcirc-parent-buffer
)
969 (goto-char (point-max))
972 (set-window-configuration rcirc-window-configuration
)
973 (goto-char (+ rcirc-prompt-end-marker
(1- pos
)))))
975 (defun rcirc-multiline-minor-cancel ()
976 "Cancel the multiline edit."
978 (kill-buffer (current-buffer))
979 (set-window-configuration rcirc-window-configuration
))
981 (defun rcirc-any-buffer (process)
982 "Return a buffer for PROCESS, either the one selected or the process buffer."
983 (if rcirc-always-use-server-buffer-flag
984 (process-buffer process
)
985 (let ((buffer (window-buffer (selected-window))))
987 (with-current-buffer buffer
988 (and (eq major-mode
'rcirc-mode
)
989 (eq (rcirc-buffer-process) process
))))
991 (process-buffer process
)))))
993 (defcustom rcirc-response-formats
994 '(("PRIVMSG" .
"%T<%N> %m")
995 ("NOTICE" .
"%T-%N- %m")
996 ("ACTION" .
"%T[%N %m]")
998 ("ERROR" .
"%T%fw!!! %m")
999 (t .
"%T%fp*** %fs%n %r %m"))
1000 "An alist of formats used for printing responses.
1001 The format is looked up using the response-type as a key;
1002 if no match is found, the default entry (with a key of `t') is used.
1004 The entry's value part should be a string, which is inserted with
1005 the of the following escape sequences replaced by the described values:
1008 %n The sender's nick
1009 %N The sender's nick (with face `rcirc-my-nick' or `rcirc-other-nick')
1010 %r The response-type
1011 %T The timestamp (with face `rcirc-timestamp')
1013 %fw Following text uses the face `font-lock-warning-face'
1014 %fp Following text uses the face `rcirc-server-prefix'
1015 %fs Following text uses the face `rcirc-server'
1016 %f[FACE] Following text uses the face FACE
1017 %f- Following text uses the default face
1018 %% A literal `%' character"
1019 :type
'(alist :key-type
(choice (string :tag
"Type")
1020 (const :tag
"Default" t
))
1024 (defun rcirc-format-response-string (process sender response target text
)
1025 "Return a nicely-formatted response string, incorporating TEXT
1026 \(and perhaps other arguments). The specific formatting used
1027 is found by looking up RESPONSE in `rcirc-response-formats'."
1029 (split-string (or (cdr (assoc response rcirc-response-formats
))
1030 (cdr (assq t rcirc-response-formats
)))
1035 (when (equal (car chunks
) "")
1037 (dolist (chunk chunks
)
1038 (if (equal chunk
"")
1040 (setq key
(aref chunk
0))
1041 (setq chunk
(substring chunk
1)))
1044 ;; %% -- literal % character
1046 ((or (eq key ?n
) (eq key ?N
))
1048 (let ((nick (concat (if (string= (with-rcirc-process-buffer process
1053 (and target
(concat "," target
)))))
1057 (if (string= sender
(rcirc-nick process
))
1059 'rcirc-other-nick
)))))
1063 (format-time-string rcirc-time-format
(current-time))
1066 ;; %m -- message text
1067 ;; We add the text property `rcirc-text' to identify this
1068 ;; as the body text.
1070 (rcirc-mangle-text process
(rcirc-facify text face
))
1074 (rcirc-facify (or rcirc-target
"") face
))
1077 (rcirc-facify response face
))
1079 ;; %f -- change face
1080 (setq face-key
(aref chunk
0))
1081 (setq chunk
(substring chunk
1))
1082 (cond ((eq face-key ?w
)
1083 ;; %fw -- warning face
1084 (setq face
'font-lock-warning-face
))
1086 ;; %fp -- server-prefix face
1087 (setq face
'rcirc-server-prefix
))
1089 ;; %fs -- warning face
1090 (setq face
'rcirc-server
))
1092 ;; %fs -- warning face
1094 ((and (eq face-key ?\
[)
1095 (string-match "^\\([^]]*\\)[]]" chunk
)
1096 (facep (match-string 1 chunk
)))
1097 ;; %f[...] -- named face
1098 (setq face
(intern (match-string 1 chunk
)))
1099 (setq chunk
(substring chunk
(match-end 0)))))
1101 (setq result
(concat result repl
(rcirc-facify chunk face
))))
1104 (defun rcirc-target-buffer (process sender response target text
)
1105 "Return a buffer to print the server response."
1106 (assert (not (bufferp target
)))
1107 (with-rcirc-process-buffer process
1109 (rcirc-any-buffer process
))
1110 ((not (rcirc-channel-p target
))
1111 ;; message from another user
1112 (if (string= response
"PRIVMSG")
1113 (rcirc-get-buffer-create process
(if (string= sender rcirc-nick
)
1116 (rcirc-get-buffer process target t
)))
1117 ((or (rcirc-get-buffer process target
)
1118 (rcirc-any-buffer process
))))))
1120 (defvar rcirc-activity-type nil
)
1121 (make-variable-buffer-local 'rcirc-activity-type
)
1122 (defvar rcirc-last-sender nil
)
1123 (make-variable-buffer-local 'rcirc-last-sender
)
1124 (defvar rcirc-gray-toggle nil
)
1125 (make-variable-buffer-local 'rcirc-gray-toggle
)
1126 (defun rcirc-print (process sender response target text
&optional activity
)
1127 "Print TEXT in the buffer associated with TARGET.
1128 Format based on SENDER and RESPONSE. If ACTIVITY is non-nil,
1130 (or text
(setq text
""))
1131 (unless (or (member sender rcirc-ignore-list
)
1132 (member (with-syntax-table rcirc-nick-syntax-table
1133 (when (string-match "^\\([^/]\\w*\\)\\b" text
)
1134 (match-string 1 text
))) rcirc-ignore-list
))
1135 (let* ((buffer (rcirc-target-buffer process sender response target text
))
1136 (inhibit-read-only t
))
1137 (with-current-buffer buffer
1138 (let ((moving (= (point) rcirc-prompt-end-marker
))
1139 (old-point (point-marker))
1140 (fill-start (marker-position rcirc-prompt-start-marker
)))
1142 (unless (string= sender
(rcirc-nick process
))
1143 ;; only decode text from other senders, not ours
1144 (setq text
(decode-coding-string text rcirc-decode-coding-system
))
1145 ;; mark the line with overlay arrow
1146 (unless (or (marker-position overlay-arrow-position
)
1147 (get-buffer-window (current-buffer)))
1148 (set-marker overlay-arrow-position
1149 (marker-position rcirc-prompt-start-marker
))))
1151 ;; temporarily set the marker insertion-type because
1152 ;; insert-before-markers results in hidden text in new buffers
1153 (goto-char rcirc-prompt-start-marker
)
1154 (set-marker-insertion-type rcirc-prompt-start-marker t
)
1155 (set-marker-insertion-type rcirc-prompt-end-marker t
)
1158 (rcirc-format-response-string process sender response nil
1161 (insert fmted-text
(propertize "\n" 'hard t
))
1162 (set-marker-insertion-type rcirc-prompt-start-marker nil
)
1163 (set-marker-insertion-type rcirc-prompt-end-marker nil
)
1165 (let ((text-start (make-marker)))
1166 (set-marker text-start
1167 (or (next-single-property-change fill-start
1169 rcirc-prompt-end-marker
))
1170 ;; squeeze spaces out of text before rcirc-text
1171 (fill-region fill-start
(1- text-start
))
1173 ;; fill the text we just inserted, maybe
1174 (when (and rcirc-fill-flag
1175 (not (string= response
"372"))) ;/motd
1177 (or rcirc-fill-prefix
1178 (make-string (- text-start fill-start
) ?\s
)))
1179 (fill-column (cond ((eq rcirc-fill-column
'frame-width
)
1184 (fill-region fill-start rcirc-prompt-start-marker
'left t
)))))
1186 ;; set inserted text to be read-only
1187 (when rcirc-read-only-flag
1188 (put-text-property rcirc-prompt-start-marker fill-start
'read-only t
)
1189 (let ((inhibit-read-only t
))
1190 (put-text-property rcirc-prompt-start-marker fill-start
1192 (put-text-property (1- (point)) (point) 'rear-nonsticky t
)))
1194 ;; truncate buffer if it is very long
1196 (when (and rcirc-buffer-maximum-lines
1197 (> rcirc-buffer-maximum-lines
0)
1198 (= (forward-line (- rcirc-buffer-maximum-lines
)) 0))
1199 (delete-region (point-min) (point))))
1201 ;; set the window point for buffers show in windows
1202 (walk-windows (lambda (w)
1203 (unless (eq (selected-window) w
)
1204 (when (and (eq (current-buffer)
1206 (>= (window-point w
)
1207 rcirc-prompt-end-marker
))
1208 (set-window-point w
(point-max)))))
1211 ;; restore the point
1212 (goto-char (if moving rcirc-prompt-end-marker old-point
))
1214 ;; flush undo (can we do something smarter here?)
1215 (buffer-disable-undo)
1216 (buffer-enable-undo))
1218 ;; record modeline activity
1221 (string-match (concat "\\b"
1222 (regexp-quote (rcirc-nick process
))
1225 (when (or (not rcirc-ignore-buffer-activity-flag
)
1226 ;; always notice when our nick is mentioned, even
1227 ;; if ignoring channel activity
1229 (rcirc-record-activity
1231 (when (or nick-match
(and (not (rcirc-channel-p rcirc-target
))
1232 (not rcirc-low-priority-flag
)))
1235 (sit-for 0) ; displayed text before hook
1236 (run-hook-with-args 'rcirc-print-hooks
1237 process sender response target text
)))))
1239 (defun rcirc-startup-channels (server)
1240 "Return the list of startup channels for SERVER."
1242 (dolist (i rcirc-startup-channels-alist
)
1243 (if (string-match (car i
) server
)
1244 (setq channels
(append channels
(cdr i
)))))
1247 (defun rcirc-join-channels (process channels
)
1249 (save-window-excursion
1250 (dolist (channel channels
)
1251 (with-rcirc-process-buffer process
1252 (rcirc-cmd-join channel process
)))))
1255 (defun rcirc-user-nick (user)
1256 "Return the nick from USER. Remove any non-nick junk."
1258 (if (string-match "^[@%+]?\\([^! ]+\\)!?" (or user
""))
1259 (match-string 1 user
)
1262 (defun rcirc-user-non-nick (user)
1263 "Return the non-nick portion of USER."
1264 (if (string-match "^[@+]?[^! ]+!?\\(.*\\)" (or user
""))
1265 (match-string 1 user
)
1268 (defun rcirc-nick-channels (process nick
)
1269 "Return list of channels for NICK."
1270 (with-rcirc-process-buffer process
1271 (mapcar (lambda (x) (car x
))
1272 (gethash nick rcirc-nick-table
))))
1274 (defun rcirc-put-nick-channel (process nick channel
)
1275 "Add CHANNEL to list associated with NICK."
1276 (let ((nick (rcirc-user-nick nick
)))
1277 (with-rcirc-process-buffer process
1278 (let* ((chans (gethash nick rcirc-nick-table
))
1279 (record (assoc-string channel chans t
)))
1281 (setcdr record
(current-time))
1282 (puthash nick
(cons (cons channel
(current-time))
1284 rcirc-nick-table
))))))
1286 (defun rcirc-nick-remove (process nick
)
1287 "Remove NICK from table."
1288 (with-rcirc-process-buffer process
1289 (remhash nick rcirc-nick-table
)))
1291 (defun rcirc-remove-nick-channel (process nick channel
)
1292 "Remove the CHANNEL from list associated with NICK."
1293 (with-rcirc-process-buffer process
1294 (let* ((chans (gethash nick rcirc-nick-table
))
1296 ;; instead of assoc-string-delete-all:
1297 (let ((record (assoc-string channel chans t
)))
1299 (setcar record
'delete
)
1300 (assq-delete-all 'delete chans
)))))
1302 (puthash nick newchans rcirc-nick-table
)
1303 (remhash nick rcirc-nick-table
)))))
1305 (defun rcirc-channel-nicks (process target
)
1306 "Return the list of nicks associated with TARGET sorted by last activity."
1308 (if (rcirc-channel-p target
)
1309 (with-rcirc-process-buffer process
1313 (let ((record (assoc-string target v t
)))
1315 (setq nicks
(cons (cons k
(cdr record
)) nicks
)))))
1317 (mapcar (lambda (x) (car x
))
1318 (sort nicks
(lambda (x y
) (time-less-p (cdr y
) (cdr x
)))))))
1321 (defun rcirc-ignore-update-automatic (nick)
1322 "Remove NICK from `rcirc-ignore-list'
1323 if NICK is also on `rcirc-ignore-list-automatic'."
1324 (when (member nick rcirc-ignore-list-automatic
)
1325 (setq rcirc-ignore-list-automatic
1326 (delete nick rcirc-ignore-list-automatic
)
1328 (delete nick rcirc-ignore-list
))))
1330 ;;; activity tracking
1331 (defvar rcirc-track-minor-mode-map
(make-sparse-keymap)
1332 "Keymap for rcirc track minor mode.")
1334 (define-key rcirc-track-minor-mode-map
(kbd "C-c `") 'rcirc-next-active-buffer
)
1335 (define-key rcirc-track-minor-mode-map
(kbd "C-c C-@") 'rcirc-next-active-buffer
)
1336 (define-key rcirc-track-minor-mode-map
(kbd "C-c C-SPC") 'rcirc-next-active-buffer
)
1339 (define-minor-mode rcirc-track-minor-mode
1340 "Global minor mode for tracking activity in rcirc buffers."
1343 :keymap rcirc-track-minor-mode-map
1346 (or global-mode-string
(setq global-mode-string
'("")))
1347 ;; toggle the mode-line channel indicator
1348 (if rcirc-track-minor-mode
1350 (and (not (memq 'rcirc-activity-string global-mode-string
))
1351 (setq global-mode-string
1352 (append global-mode-string
'(rcirc-activity-string))))
1353 (add-hook 'window-configuration-change-hook
1354 'rcirc-window-configuration-change
))
1355 (setq global-mode-string
1356 (delete 'rcirc-activity-string global-mode-string
))
1357 (remove-hook 'window-configuration-change-hook
1358 'rcirc-window-configuration-change
)))
1360 (or (assq 'rcirc-ignore-buffer-activity-flag minor-mode-alist
)
1361 (setq minor-mode-alist
1362 (cons '(rcirc-ignore-buffer-activity-flag " Ignore") minor-mode-alist
)))
1363 (or (assq 'rcirc-low-priority-flag minor-mode-alist
)
1364 (setq minor-mode-alist
1365 (cons '(rcirc-low-priority-flag " LowPri") minor-mode-alist
)))
1367 (defun rcirc-toggle-ignore-buffer-activity ()
1368 "Toggle the value of `rcirc-ignore-buffer-activity-flag'."
1370 (setq rcirc-ignore-buffer-activity-flag
1371 (not rcirc-ignore-buffer-activity-flag
))
1372 (message (if rcirc-ignore-buffer-activity-flag
1373 "Ignore activity in this buffer"
1374 "Notice activity in this buffer"))
1375 (force-mode-line-update))
1377 (defun rcirc-toggle-low-priority ()
1378 "Toggle the value of `rcirc-ignore-buffer-activity-flag'."
1380 (setq rcirc-low-priority-flag
1381 (not rcirc-low-priority-flag
))
1382 (message (if rcirc-low-priority-flag
1383 "Activity in this buffer is low priority"
1384 "Activity in this buffer is normal priority"))
1385 (force-mode-line-update))
1387 (defvar rcirc-switch-to-buffer-function
'switch-to-buffer
1388 "Function to use when switching buffers.
1389 Possible values are `switch-to-buffer', `pop-to-buffer', and
1392 (defun rcirc-switch-to-server-buffer ()
1393 "Switch to the server buffer associated with current channel buffer."
1395 (funcall rcirc-switch-to-buffer-function rcirc-server-buffer
))
1397 (defun rcirc-jump-to-first-unread-line ()
1398 "Move the point to the first unread line in this buffer."
1400 (when (marker-position overlay-arrow-position
)
1401 (goto-char overlay-arrow-position
)))
1403 (defvar rcirc-last-non-irc-buffer nil
1404 "The buffer to switch to when there is no more activity.")
1406 (defun rcirc-next-active-buffer (arg)
1407 "Go to the next rcirc buffer with activity.
1408 With prefix ARG, go to the next low priority buffer with activity.
1409 The function given by `rcirc-switch-to-buffer-function' is used to
1412 (let* ((pair (rcirc-split-activity rcirc-activity
))
1415 (if (or (and (not arg
) hipri
)
1418 (unless (eq major-mode
'rcirc-mode
)
1419 (setq rcirc-last-non-irc-buffer
(current-buffer)))
1420 (funcall rcirc-switch-to-buffer-function
1421 (car (if arg lopri hipri
))))
1422 (if (eq major-mode
'rcirc-mode
)
1423 (if (not (and rcirc-last-non-irc-buffer
1424 (buffer-live-p rcirc-last-non-irc-buffer
)))
1425 (message "No IRC activity. Start something.")
1426 (message "No more IRC activity. Go back to work.")
1427 (funcall rcirc-switch-to-buffer-function rcirc-last-non-irc-buffer
)
1428 (setq rcirc-last-non-irc-buffer nil
))
1434 (key-description (this-command-keys))
1435 " for low priority activity."))))))))
1437 (defvar rcirc-activity-hooks nil
1438 "Hook to be run when there is channel activity.
1440 Functions are called with a single argument, the buffer with the
1441 activity. Only run if the buffer is not visible and
1442 `rcirc-ignore-buffer-activity-flag' is non-nil.")
1444 (defun rcirc-record-activity (buffer &optional type
)
1445 "Record BUFFER activity with TYPE."
1446 (with-current-buffer buffer
1447 (when (not (get-buffer-window (current-buffer) t
))
1448 (setq rcirc-activity
1449 (sort (add-to-list 'rcirc-activity
(current-buffer))
1451 (let ((t1 (with-current-buffer b1 rcirc-last-post-time
))
1452 (t2 (with-current-buffer b2 rcirc-last-post-time
)))
1453 (time-less-p t2 t1
)))))
1454 (if (not rcirc-activity-type
)
1455 (setq rcirc-activity-type type
))
1456 (rcirc-update-activity-string)))
1457 (run-hook-with-args 'rcirc-activity-hooks buffer
))
1459 (defun rcirc-clear-activity (buffer)
1460 "Clear the BUFFER activity."
1461 (setq rcirc-activity
(delete buffer rcirc-activity
))
1462 (with-current-buffer buffer
1463 (setq rcirc-activity-type nil
)))
1465 (defun rcirc-split-activity (activity)
1466 "Return a cons cell with ACTIVITY split into (lopri . hipri)."
1468 (dolist (buf rcirc-activity
)
1469 (with-current-buffer buf
1470 (if (and rcirc-low-priority-flag
1471 (not (eq rcirc-activity-type
'nick
)))
1472 (add-to-list 'lopri buf t
)
1473 (add-to-list 'hipri buf t
))))
1474 (cons lopri hipri
)))
1476 ;; TODO: add mouse properties
1477 (defun rcirc-update-activity-string ()
1478 "Update mode-line string."
1479 (let* ((pair (rcirc-split-activity rcirc-activity
))
1482 (setq rcirc-activity-string
1483 (if (or hipri lopri
)
1486 (rcirc-activity-string hipri
)
1487 (and hipri lopri
",")
1490 (rcirc-activity-string lopri
)
1496 (defun rcirc-activity-string (buffers)
1497 (mapconcat (lambda (b)
1498 (let ((s (rcirc-short-buffer-name b
)))
1499 (with-current-buffer b
1500 (if (not (eq rcirc-activity-type
'nick
))
1502 (rcirc-facify s
'rcirc-mode-line-nick
)))))
1505 (defun rcirc-short-buffer-name (buffer)
1506 "Return a short name for BUFFER to use in the modeline indicator."
1507 (with-current-buffer buffer
1508 (or rcirc-short-buffer-name
(buffer-name))))
1510 (defvar rcirc-current-buffer nil
)
1511 (defun rcirc-window-configuration-change ()
1512 "Go through visible windows and remove buffers from activity list.
1513 Also, clear the overlay arrow if the current buffer is now hidden."
1514 (let ((current-now-hidden t
))
1515 (walk-windows (lambda (w)
1516 (let ((buf (window-buffer w
)))
1517 (when (eq major-mode
'rcirc-mode
)
1518 (rcirc-clear-activity buf
)
1519 (when (eq buf rcirc-current-buffer
)
1520 (setq current-now-hidden nil
))))))
1521 ;; add overlay arrow if the buffer isn't displayed
1522 (when (and rcirc-current-buffer current-now-hidden
)
1523 (with-current-buffer rcirc-current-buffer
1524 (when (eq major-mode
'rcirc-mode
)
1525 (marker-position overlay-arrow-position
)
1526 (set-marker overlay-arrow-position nil
)))))
1528 ;; remove any killed buffers from list
1529 (setq rcirc-activity
1530 (delq nil
(mapcar (lambda (buf) (when (buffer-live-p buf
) buf
))
1532 (rcirc-update-activity-string)
1533 (setq rcirc-current-buffer
(current-buffer)))
1536 ;;; buffer name abbreviation
1537 (defun rcirc-update-short-buffer-names ()
1539 (apply 'append
(mapcar (lambda (process)
1540 (with-rcirc-process-buffer process
1541 rcirc-buffer-alist
))
1542 (rcirc-process-list)))))
1543 (dolist (i (rcirc-abbreviate bufalist
))
1544 (when (buffer-live-p (cdr i
))
1545 (with-current-buffer (cdr i
)
1546 (setq rcirc-short-buffer-name
(car i
)))))))
1548 (defun rcirc-abbreviate (pairs)
1549 (apply 'append
(mapcar 'rcirc-rebuild-tree
(rcirc-make-trees pairs
))))
1551 (defun rcirc-rebuild-tree (tree &optional acc
)
1552 (let ((ch (char-to-string (car tree
))))
1553 (dolist (x (cdr tree
))
1555 (setq acc
(append acc
1557 (cons (concat ch
(car y
))
1559 (rcirc-rebuild-tree x
))))
1560 (setq acc
(cons (cons ch x
) acc
))))
1563 (defun rcirc-make-trees (pairs)
1565 (mapc (lambda (pair)
1567 (let* ((str (car pair
))
1569 (char (unless (zerop (length str
))
1571 (rest (unless (zerop (length str
))
1573 (part (if char
(assq char alist
))))
1575 ;; existing partition
1576 (setcdr part
(cons (cons rest data
) (cdr part
)))
1578 (setq alist
(cons (if char
1579 (list char
(cons rest data
))
1582 (setq alist
(cons pair alist
))))
1584 ;; recurse into cdrs of alist
1586 (when (and (listp x
) (listp (cadr x
)))
1587 (setcdr x
(if (> (length (cdr x
)) 1)
1588 (rcirc-make-trees (cdr x
))
1589 (setcdr x
(list (cdadr x
)))))))
1592 ;;; /commands these are called with 3 args: PROCESS, TARGET, which is
1593 ;; the current buffer/channel/user, and ARGS, which is a string
1594 ;; containing the text following the /cmd.
1596 (defmacro defun-rcirc-command
(command argument docstring interactive-form
1599 `(defun ,(intern (concat "rcirc-cmd-" (symbol-name command
)))
1600 (,@argument
&optional process target
)
1601 ,(concat docstring
"\n\nNote: If PROCESS or TARGET are nil, the values given"
1602 "\nby `rcirc-buffer-process' and `rcirc-target' will be used.")
1604 (let ((process (or process
(rcirc-buffer-process)))
1605 (target (or target rcirc-target
)))
1608 (defun-rcirc-command msg
(message)
1609 "Send private MESSAGE to TARGET."
1613 (setq target
(completing-read "Message nick: "
1614 (with-rcirc-server-buffer
1616 (when (> (length target
) 0)
1617 (setq message
(read-string (format "Message %s: " target
)))
1618 (when (> (length message
) 0)
1619 (rcirc-send-message process target message
))))
1620 (if (not (string-match "\\([^ ]+\\) \\(.+\\)" message
))
1621 (message "Not enough args, or something.")
1622 (setq target
(match-string 1 message
)
1623 message
(match-string 2 message
))
1624 (rcirc-send-message process target message
))))
1626 (defun-rcirc-command query
(nick)
1627 "Open a private chat buffer to NICK."
1628 (interactive (list (completing-read "Query nick: "
1629 (with-rcirc-server-buffer rcirc-nick-table
))))
1630 (let ((existing-buffer (rcirc-get-buffer process nick
)))
1631 (switch-to-buffer (or existing-buffer
1632 (rcirc-get-buffer-create process nick
)))
1633 (when (not existing-buffer
)
1634 (rcirc-cmd-whois nick
))))
1636 (defun-rcirc-command join
(channel)
1638 (interactive "sJoin channel: ")
1639 (let ((buffer (rcirc-get-buffer-create process
1640 (car (split-string channel
)))))
1641 (rcirc-send-string process
(concat "JOIN " channel
))
1642 (when (not (eq (selected-window) (minibuffer-window)))
1643 (funcall rcirc-switch-to-buffer-function buffer
))))
1645 (defun-rcirc-command part
(channel)
1647 (interactive "sPart channel: ")
1648 (let ((channel (if (> (length channel
) 0) channel target
)))
1649 (rcirc-send-string process
(concat "PART " channel
" :" rcirc-id-string
))))
1651 (defun-rcirc-command quit
(reason)
1652 "Send a quit message to server with REASON."
1653 (interactive "sQuit reason: ")
1654 (rcirc-send-string process
(concat "QUIT :"
1655 (if (not (zerop (length reason
)))
1659 (defun-rcirc-command nick
(nick)
1660 "Change nick to NICK."
1663 (setq nick
(read-string "New nick: " (rcirc-nick process
))))
1664 (rcirc-send-string process
(concat "NICK " nick
)))
1666 (defun-rcirc-command names
(channel)
1667 "Display list of names in CHANNEL or in current channel if CHANNEL is nil.
1668 If called interactively, prompt for a channel when prefix arg is supplied."
1672 (setq channel
(read-string "List names in channel: " target
))))
1673 (let ((channel (if (> (length channel
) 0)
1676 (rcirc-send-string process
(concat "NAMES " channel
))))
1678 (defun-rcirc-command topic
(topic)
1679 "List TOPIC for the TARGET channel.
1680 With a prefix arg, prompt for new topic."
1682 (if (and (interactive-p) topic
)
1683 (setq topic
(read-string "New Topic: " rcirc-topic
)))
1684 (rcirc-send-string process
(concat "TOPIC " target
1685 (when (> (length topic
) 0)
1686 (concat " :" topic
)))))
1688 (defun-rcirc-command whois
(nick)
1689 "Request information from server about NICK."
1691 (completing-read "Whois: "
1692 (with-rcirc-server-buffer rcirc-nick-table
))))
1693 (rcirc-send-string process
(concat "WHOIS " nick
)))
1695 (defun-rcirc-command mode
(args)
1696 "Set mode with ARGS."
1697 (interactive (list (concat (read-string "Mode nick or channel: ")
1698 " " (read-string "Mode: "))))
1699 (rcirc-send-string process
(concat "MODE " args
)))
1701 (defun-rcirc-command list
(channels)
1702 "Request information on CHANNELS from server."
1703 (interactive "sList Channels: ")
1704 (rcirc-send-string process
(concat "LIST " channels
)))
1706 (defun-rcirc-command oper
(args)
1707 "Send operator command to server."
1708 (interactive "sOper args: ")
1709 (rcirc-send-string process
(concat "OPER " args
)))
1711 (defun-rcirc-command quote
(message)
1712 "Send MESSAGE literally to server."
1713 (interactive "sServer message: ")
1714 (rcirc-send-string process message
))
1716 (defun-rcirc-command kick
(arg)
1717 "Kick NICK from current channel."
1719 (concat (completing-read "Kick nick: "
1720 (rcirc-channel-nicks
1721 (rcirc-buffer-process)
1723 (read-from-minibuffer "Kick reason: "))))
1724 (let* ((arglist (split-string arg
))
1725 (argstring (concat (car arglist
) " :"
1726 (mapconcat 'identity
(cdr arglist
) " "))))
1727 (rcirc-send-string process
(concat "KICK " target
" " argstring
))))
1729 (defun rcirc-cmd-ctcp (args &optional process target
)
1730 (if (string-match "^\\([^ ]+\\)\\s-+\\(.+\\)$" args
)
1731 (let ((target (match-string 1 args
))
1732 (request (match-string 2 args
)))
1733 (rcirc-send-string process
1734 (format "PRIVMSG %s \C-a%s\C-a"
1735 target
(upcase request
))))
1736 (rcirc-print process
(rcirc-nick process
) "ERROR" nil
1737 "usage: /ctcp NICK REQUEST")))
1739 (defun rcirc-cmd-me (args &optional process target
)
1740 (rcirc-send-string process
(format "PRIVMSG %s :\C-aACTION %s\C-a"
1743 (defun-rcirc-command ignore
(nick)
1744 "Manage the ignore list.
1745 Ignore NICK, unignore NICK if already ignored, or list ignored
1746 nicks when no NICK is given. When listing ignored nicks, the
1747 ones added to the list automatically are marked with an asterisk."
1748 (interactive "sToggle ignoring of nick: ")
1749 (when (not (string= "" nick
))
1750 (if (member nick rcirc-ignore-list
)
1751 (setq rcirc-ignore-list
(delete nick rcirc-ignore-list
))
1752 (setq rcirc-ignore-list
(cons nick rcirc-ignore-list
))))
1753 (rcirc-print process
(rcirc-nick process
) "IGNORE" target
1757 (if (member nick rcirc-ignore-list-automatic
)
1759 rcirc-ignore-list
" ")))
1762 (defun rcirc-message-leader (sender face
)
1763 "Return a string with SENDER propertized with FACE."
1764 (rcirc-facify (concat "<" sender
"> ") face
))
1766 (defun rcirc-facify (string face
)
1767 "Return a copy of STRING with FACE property added."
1768 (propertize (or string
"") 'face face
'rear-nonsticky t
))
1770 (defvar rcirc-url-regexp
1774 (or (and (or "http" "https" "ftp" "file" "gopher" "news"
1775 "telnet" "wais" "mailto")
1778 (1+ (char "-a-zA-Z0-9_."))
1779 (optional ":" (1+ (char "0-9"))))
1780 (and (1+ (char "-a-zA-Z0-9_."))
1781 (or ".com" ".net" ".org")
1785 (1+ (char "-a-zA-Z0-9_=!?#$\@~`%&*+|\\/:;.,{}[]"))
1786 (char "-a-zA-Z0-9_=#$\@~`%&*+|\\/:;{}[]")))))
1787 "Regexp matching URLs. Set to nil to disable URL features in rcirc.")
1789 (defun rcirc-browse-url (&optional arg
)
1790 "Prompt for URL to browse based on URLs in buffer."
1792 (let ((completions (mapcar (lambda (x) (cons x nil
)) rcirc-urls
))
1793 (initial-input (car rcirc-urls
))
1794 (history (cdr rcirc-urls
)))
1795 (browse-url (completing-read "rcirc browse-url: "
1796 completions nil nil initial-input
'history
)
1799 (defun rcirc-browse-url-at-point (point)
1800 "Send URL at point to `browse-url'."
1802 (let ((beg (previous-single-property-change point
'mouse-face
))
1803 (end (next-single-property-change point
'mouse-face
)))
1804 (browse-url (buffer-substring-no-properties beg end
))))
1806 (defun rcirc-browse-url-at-mouse (event)
1807 "Send URL at mouse click to `browse-url'."
1809 (let ((position (event-end event
)))
1810 (with-current-buffer (window-buffer (posn-window position
))
1811 (rcirc-browse-url-at-point (posn-point position
)))))
1813 (defun rcirc-map-regexp (function regexp string
)
1814 "Return a copy of STRING after calling FUNCTION for each REGEXP match.
1815 FUNCTION takes 3 arguments, MATCH-START, MATCH-END, and STRING."
1817 (while (string-match regexp string start
)
1818 (setq start
(match-end 0))
1819 (funcall function
(match-beginning 0) (match-end 0) string
)))
1822 (defun rcirc-mangle-text (process text
)
1823 "Return TEXT with properties added based on various patterns."
1827 (lambda (start end string
)
1828 (let ((orig-face (get-text-property start
'face string
)))
1829 (add-text-properties
1831 (list 'face
(if (listp orig-face
)
1834 (list orig-face
'bold
))
1839 ;; TODO: deal with ^_ and ^C colors sequences
1840 (while (string-match "\\(.*\\)[\ 2\ 1]\\(.*\\)" text
)
1841 (setq text
(concat (match-string 1 text
)
1842 (match-string 2 text
))))
1845 (with-syntax-table rcirc-nick-syntax-table
1846 (rcirc-map-regexp (lambda (start end string
)
1847 (add-text-properties
1849 (list 'face
'rcirc-nick-in-message
1853 (regexp-quote (rcirc-nick process
))
1859 (lambda (start end string
)
1860 (let ((orig-face (get-text-property start
'face string
)))
1861 (add-text-properties start end
1862 (list 'face
(if (listp orig-face
)
1865 (list orig-face
'bold
))
1867 'mouse-face
'highlight
1868 'keymap rcirc-browse-url-map
)
1870 (push (substring-no-properties string start end
) rcirc-urls
))
1877 ;; these are called with the server PROCESS, the SENDER, which is a
1878 ;; server or a user, depending on the command, the ARGS, which is a
1879 ;; list of strings, and the TEXT, which is the original server text,
1881 (defun rcirc-handler-001 (process sender args text
)
1882 (rcirc-handler-generic process
"001" sender args text
)
1883 ;; set the real server name
1884 (with-rcirc-process-buffer process
1885 (setq rcirc-server sender
)
1886 (setq rcirc-nick
(car args
))
1887 (rcirc-update-prompt)
1888 (when rcirc-auto-authenticate-flag
(rcirc-authenticate))
1889 (rcirc-join-channels process rcirc-startup-channels
)))
1891 (defun rcirc-handler-PRIVMSG (process sender args text
)
1892 (let ((target (if (rcirc-channel-p (car args
))
1895 (message (or (cadr args
) "")))
1896 (if (string-match "^\C-a\\(.*\\)\C-a$" message
)
1897 (rcirc-handler-CTCP process target sender
(match-string 1 message
))
1898 (rcirc-print process sender
"PRIVMSG" target message t
))
1899 ;; update nick timestamp
1900 (if (member target
(rcirc-nick-channels process sender
))
1901 (rcirc-put-nick-channel process sender target
))))
1903 (defun rcirc-handler-NOTICE (process sender args text
)
1904 (let ((target (car args
))
1905 (message (cadr args
)))
1906 (if (string-match "^\C-a\\(.*\\)\C-a$" message
)
1907 (rcirc-handler-CTCP-response process target sender
1908 (match-string 1 message
))
1909 (rcirc-print process sender
"NOTICE"
1910 (cond ((rcirc-channel-p target
)
1912 ;;; -ChanServ- [#gnu] Welcome...
1913 ((string-match "^\\[\\(#[^ ]+\\)\\]" message
)
1914 (match-string 1 message
))
1916 (if (string= sender
(rcirc-server-name process
))
1921 (defun rcirc-handler-WALLOPS (process sender args text
)
1922 (rcirc-print process sender
"WALLOPS" sender
(car args
) t
))
1924 (defun rcirc-handler-JOIN (process sender args text
)
1925 (let ((channel (car args
)))
1926 (rcirc-get-buffer-create process channel
)
1927 (rcirc-print process sender
"JOIN" channel
"")
1929 ;; print in private chat buffer if it exists
1930 (when (rcirc-get-buffer (rcirc-buffer-process) sender
)
1931 (rcirc-print process sender
"JOIN" sender channel
))
1933 (rcirc-put-nick-channel process sender channel
)))
1935 ;; PART and KICK are handled the same way
1936 (defun rcirc-handler-PART-or-KICK (process response channel sender nick args
)
1937 (rcirc-ignore-update-automatic nick
)
1938 (if (not (string= nick
(rcirc-nick process
)))
1939 ;; this is someone else leaving
1940 (rcirc-remove-nick-channel process nick channel
)
1941 ;; this is us leaving
1943 (rcirc-remove-nick-channel process n channel
))
1944 (rcirc-channel-nicks process channel
))
1946 ;; if the buffer is still around, make it inactive
1947 (let ((buffer (rcirc-get-buffer process channel
)))
1949 (with-current-buffer buffer
1950 (setq rcirc-target nil
))))))
1952 (defun rcirc-handler-PART (process sender args text
)
1953 (let* ((channel (car args
))
1954 (reason (cadr args
))
1955 (message (concat channel
" " reason
)))
1956 (rcirc-print process sender
"PART" channel message
)
1957 ;; print in private chat buffer if it exists
1958 (when (rcirc-get-buffer (rcirc-buffer-process) sender
)
1959 (rcirc-print process sender
"PART" sender message
))
1961 (rcirc-handler-PART-or-KICK process
"PART" channel sender sender reason
)))
1963 (defun rcirc-handler-KICK (process sender args text
)
1964 (let* ((channel (car args
))
1966 (reason (caddr args
))
1967 (message (concat nick
" " channel
" " reason
)))
1968 (rcirc-print process sender
"KICK" channel message t
)
1969 ;; print in private chat buffer if it exists
1970 (when (rcirc-get-buffer (rcirc-buffer-process) nick
)
1971 (rcirc-print process sender
"KICK" nick message
))
1973 (rcirc-handler-PART-or-KICK process
"KICK" channel sender nick reason
)))
1975 (defun rcirc-handler-QUIT (process sender args text
)
1976 (rcirc-ignore-update-automatic sender
)
1977 (mapc (lambda (channel)
1978 (rcirc-print process sender
"QUIT" channel
(apply 'concat args
)))
1979 (rcirc-nick-channels process sender
))
1981 ;; print in private chat buffer if it exists
1982 (when (rcirc-get-buffer (rcirc-buffer-process) sender
)
1983 (rcirc-print process sender
"QUIT" sender
(apply 'concat args
)))
1985 (rcirc-nick-remove process sender
))
1987 (defun rcirc-handler-NICK (process sender args text
)
1988 (let* ((old-nick sender
)
1989 (new-nick (car args
))
1990 (channels (rcirc-nick-channels process old-nick
)))
1991 ;; update list of ignored nicks
1992 (rcirc-ignore-update-automatic old-nick
)
1993 (when (member old-nick rcirc-ignore-list
)
1994 (add-to-list 'rcirc-ignore-list new-nick
)
1995 (add-to-list 'rcirc-ignore-list-automatic new-nick
))
1996 ;; print message to nick's channels
1997 (dolist (target channels
)
1998 (rcirc-print process sender
"NICK" target new-nick
))
1999 ;; update private chat buffer, if it exists
2000 (let ((chat-buffer (rcirc-get-buffer process old-nick
)))
2002 (with-current-buffer chat-buffer
2003 (rcirc-print process sender
"NICK" old-nick new-nick
)
2004 (setq rcirc-target new-nick
)
2005 (rename-buffer (rcirc-generate-new-buffer-name process new-nick
)))))
2006 ;; remove old nick and add new one
2007 (with-rcirc-process-buffer process
2008 (let ((v (gethash old-nick rcirc-nick-table
)))
2009 (remhash old-nick rcirc-nick-table
)
2010 (puthash new-nick v rcirc-nick-table
))
2011 ;; if this is our nick...
2012 (when (string= old-nick rcirc-nick
)
2013 (setq rcirc-nick new-nick
)
2014 (rcirc-update-prompt t
)
2016 (when rcirc-auto-authenticate-flag
(rcirc-authenticate))))))
2018 (defun rcirc-handler-PING (process sender args text
)
2019 (rcirc-send-string process
(concat "PONG " (car args
))))
2021 (defun rcirc-handler-PONG (process sender args text
)
2025 (defun rcirc-handler-TOPIC (process sender args text
)
2026 (let ((topic (cadr args
)))
2027 (rcirc-print process sender
"TOPIC" (car args
) topic
)
2028 (with-current-buffer (rcirc-get-buffer process
(car args
))
2029 (setq rcirc-topic topic
))))
2031 (defvar rcirc-nick-away-alist nil
)
2032 (defun rcirc-handler-301 (process sender args text
)
2034 (let* ((nick (cadr args
))
2035 (rec (assoc-string nick rcirc-nick-away-alist
))
2036 (away-message (caddr args
)))
2038 (not (string= (cdr rec
) away-message
)))
2039 ;; away message has changed
2040 (rcirc-handler-generic process
"AWAY" nick
(cdr args
) text
)
2042 (setcdr rec away-message
)
2043 (setq rcirc-nick-away-alist
(cons (cons nick away-message
)
2044 rcirc-nick-away-alist
))))))
2046 (defun rcirc-handler-332 (process sender args text
)
2048 (let ((buffer (or (rcirc-get-buffer process
(cadr args
))
2049 (rcirc-get-temp-buffer-create process
(cadr args
)))))
2050 (with-current-buffer buffer
2051 (setq rcirc-topic
(caddr args
)))))
2053 (defun rcirc-handler-333 (process sender args text
)
2054 "Not in rfc1459.txt"
2055 (let ((buffer (or (rcirc-get-buffer process
(cadr args
))
2056 (rcirc-get-temp-buffer-create process
(cadr args
)))))
2057 (with-current-buffer buffer
2058 (let ((setter (caddr args
))
2059 (time (current-time-string
2061 (string-to-number (cadddr args
))))))
2062 (rcirc-print process sender
"TOPIC" (cadr args
)
2063 (format "%s (%s on %s)" rcirc-topic setter time
))))))
2065 (defun rcirc-handler-477 (process sender args text
)
2067 (rcirc-print process sender
"477" (cadr args
) (caddr args
)))
2069 (defun rcirc-handler-MODE (process sender args text
)
2070 (let ((target (car args
))
2071 (msg (mapconcat 'identity
(cdr args
) " ")))
2072 (rcirc-print process sender
"MODE"
2073 (if (string= target
(rcirc-nick process
))
2078 ;; print in private chat buffers if they exist
2079 (mapc (lambda (nick)
2080 (when (rcirc-get-buffer process nick
)
2081 (rcirc-print process sender
"MODE" nick msg
)))
2084 (defun rcirc-get-temp-buffer-create (process channel
)
2085 "Return a buffer based on PROCESS and CHANNEL."
2086 (let ((tmpnam (concat " " (downcase channel
) "TMP" (process-name process
))))
2087 (get-buffer-create tmpnam
)))
2089 (defun rcirc-handler-353 (process sender args text
)
2091 (let ((channel (caddr args
)))
2092 (mapc (lambda (nick)
2093 (rcirc-put-nick-channel process nick channel
))
2094 (split-string (cadddr args
) " " t
))
2095 (with-current-buffer (rcirc-get-temp-buffer-create process channel
)
2096 (goto-char (point-max))
2097 (insert (car (last args
)) " "))))
2099 (defun rcirc-handler-366 (process sender args text
)
2101 (let* ((channel (cadr args
))
2102 (buffer (rcirc-get-temp-buffer-create process channel
)))
2103 (with-current-buffer buffer
2104 (rcirc-print process sender
"NAMES" channel
2105 (buffer-substring (point-min) (point-max))))
2106 (kill-buffer buffer
)))
2108 (defun rcirc-handler-433 (process sender args text
)
2110 (rcirc-handler-generic process
"433" sender args text
)
2111 (let* ((new-nick (concat (cadr args
) "`")))
2112 (with-rcirc-process-buffer process
2113 (rcirc-cmd-nick new-nick nil process
))))
2115 (defun rcirc-authenticate ()
2116 "Send authentication to process associated with current buffer.
2117 Passwords are stored in `rcirc-authinfo' (which see)."
2119 (with-rcirc-server-buffer
2120 (dolist (i rcirc-authinfo
)
2121 (let ((process (rcirc-buffer-process))
2126 (when (and (string-match server rcirc-server
)
2127 (string-match nick rcirc-nick
))
2128 (cond ((equal method
'nickserv
)
2132 "PRIVMSG nickserv :identify "
2134 ((equal method
'chanserv
)
2138 "PRIVMSG chanserv :identify "
2139 (cadr args
) " " (car args
))))
2140 ((equal method
'bitlbee
)
2143 (concat "PRIVMSG &bitlbee :identify " (car args
))))
2145 (message "No %S authentication method defined"
2148 (defun rcirc-handler-INVITE (process sender args text
)
2149 (rcirc-print process sender
"INVITE" nil
(mapconcat 'identity args
" ") t
))
2151 (defun rcirc-handler-ERROR (process sender args text
)
2152 (rcirc-print process sender
"ERROR" nil
(mapconcat 'identity args
" ")))
2154 (defun rcirc-handler-CTCP (process target sender text
)
2155 (if (string-match "^\\([^ ]+\\) *\\(.*\\)$" text
)
2156 (let* ((request (upcase (match-string 1 text
)))
2157 (args (match-string 2 text
))
2158 (handler (intern-soft (concat "rcirc-handler-ctcp-" request
))))
2159 (if (not (fboundp handler
))
2160 (rcirc-print process sender
"ERROR" target
2161 (format "%s sent unsupported ctcp: %s" sender text
)
2163 (funcall handler process target sender args
)
2164 (if (not (string= request
"ACTION"))
2165 (rcirc-print process sender
"CTCP" target
2166 (format "%s" text
) t
))))))
2168 (defun rcirc-handler-ctcp-VERSION (process target sender args
)
2169 (rcirc-send-string process
2170 (concat "NOTICE " sender
2171 " :\C-aVERSION " rcirc-id-string
2174 (defun rcirc-handler-ctcp-ACTION (process target sender args
)
2175 (rcirc-print process sender
"ACTION" target args t
))
2177 (defun rcirc-handler-ctcp-TIME (process target sender args
)
2178 (rcirc-send-string process
2179 (concat "NOTICE " sender
2180 " :\C-aTIME " (current-time-string) "\C-a")))
2182 (defun rcirc-handler-CTCP-response (process target sender message
)
2183 (rcirc-print process sender
"CTCP" nil message t
))
2185 (defgroup rcirc-faces nil
2190 (defface rcirc-my-nick
; font-lock-function-name-face
2191 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
2192 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
2193 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
2194 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
2195 (((class color
) (min-colors 8)) (:foreground
"blue" :weight bold
))
2196 (t (:inverse-video t
:weight bold
)))
2197 "The face used to highlight my messages."
2198 :group
'rcirc-faces
)
2200 (defface rcirc-other-nick
; font-lock-variable-name-face
2201 '((((class grayscale
) (background light
))
2202 (:foreground
"Gray90" :weight bold
:slant italic
))
2203 (((class grayscale
) (background dark
))
2204 (:foreground
"DimGray" :weight bold
:slant italic
))
2205 (((class color
) (min-colors 88) (background light
)) (:foreground
"DarkGoldenrod"))
2206 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightGoldenrod"))
2207 (((class color
) (min-colors 16) (background light
)) (:foreground
"DarkGoldenrod"))
2208 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightGoldenrod"))
2209 (((class color
) (min-colors 8)) (:foreground
"yellow" :weight light
))
2210 (t (:weight bold
:slant italic
)))
2211 "The face used to highlight other messages."
2212 :group
'rcirc-faces
)
2214 (defface rcirc-server
; font-lock-comment-face
2215 '((((class grayscale
) (background light
))
2216 (:foreground
"DimGray" :weight bold
:slant italic
))
2217 (((class grayscale
) (background dark
))
2218 (:foreground
"LightGray" :weight bold
:slant italic
))
2219 (((class color
) (min-colors 88) (background light
))
2220 (:foreground
"Firebrick"))
2221 (((class color
) (min-colors 88) (background dark
))
2222 (:foreground
"chocolate1"))
2223 (((class color
) (min-colors 16) (background light
))
2224 (:foreground
"red"))
2225 (((class color
) (min-colors 16) (background dark
))
2226 (:foreground
"red1"))
2227 (((class color
) (min-colors 8) (background light
))
2229 (((class color
) (min-colors 8) (background dark
))
2231 (t (:weight bold
:slant italic
)))
2232 "The face used to highlight server messages."
2233 :group
'rcirc-faces
)
2235 (defface rcirc-server-prefix
; font-lock-comment-delimiter-face
2236 '((default :inherit rcirc-server
)
2237 (((class grayscale
)))
2238 (((class color
) (min-colors 16)))
2239 (((class color
) (min-colors 8) (background light
))
2241 (((class color
) (min-colors 8) (background dark
))
2242 :foreground
"red1"))
2243 "The face used to highlight server prefixes."
2244 :group
'rcirc-faces
)
2246 (defface rcirc-timestamp
2247 '((t (:inherit default
)))
2248 "The face used to highlight timestamps."
2249 :group
'rcirc-faces
)
2251 (defface rcirc-nick-in-message
; font-lock-keyword-face
2252 '((((class grayscale
) (background light
)) (:foreground
"LightGray" :weight bold
))
2253 (((class grayscale
) (background dark
)) (:foreground
"DimGray" :weight bold
))
2254 (((class color
) (min-colors 88) (background light
)) (:foreground
"Purple"))
2255 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Cyan1"))
2256 (((class color
) (min-colors 16) (background light
)) (:foreground
"Purple"))
2257 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Cyan"))
2258 (((class color
) (min-colors 8)) (:foreground
"cyan" :weight bold
))
2260 "The face used to highlight instances of nick within messages."
2261 :group
'rcirc-faces
)
2263 (defface rcirc-prompt
; comint-highlight-prompt
2264 '((((min-colors 88) (background dark
)) (:foreground
"cyan1"))
2265 (((background dark
)) (:foreground
"cyan"))
2266 (t (:foreground
"dark blue")))
2267 "The face used to highlight prompts."
2268 :group
'rcirc-faces
)
2270 (defface rcirc-mode-line-nick
2272 "The face used indicate activity directed at you."
2273 :group
'rcirc-faces
)
2276 ;; When using M-x flyspell-mode, only check words after the prompt
2277 (put 'rcirc-mode
'flyspell-mode-predicate
'rcirc-looking-at-input
)
2278 (defun rcirc-looking-at-input ()
2279 "Returns true if point is past the input marker."
2280 (>= (point) rcirc-prompt-end-marker
))
2285 ;; arch-tag: b471b7e8-6b5a-4399-b2c6-a3c78dfc8ffb
2286 ;;; rcirc.el ends here