1 ;;; rcirc.el --- default, simple IRC client.
3 ;; Copyright (C) 2005, 2006, 2007 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:
49 (eval-when-compile (require 'cl
))
55 :link
'(custom-manual "(rcirc)")
58 (defcustom rcirc-default-server
"irc.freenode.net"
59 "The default server to connect to."
63 (defcustom rcirc-default-port
6667
64 "The default port to connect to."
68 (defcustom rcirc-default-nick
(user-login-name)
73 (defcustom rcirc-default-user-name
(user-login-name)
74 "Your user name sent to the server when connecting."
78 (defcustom rcirc-default-user-full-name
(if (string= (user-full-name) "")
79 rcirc-default-user-name
81 "The full name sent to the server when connecting."
85 (defcustom rcirc-startup-channels-alist
'(("^irc.freenode.net$" "#rcirc"))
86 "Alist of channels to join at startup.
87 Each element looks like (SERVER-REGEXP . CHANNEL-LIST)."
88 :type
'(alist :key-type string
:value-type
(repeat string
))
91 (defcustom rcirc-fill-flag t
92 "*Non-nil means line-wrap messages printed in channel buffers."
96 (defcustom rcirc-fill-column nil
97 "*Column beyond which automatic line-wrapping should happen.
98 If nil, use value of `fill-column'. If 'frame-width, use the
100 :type
'(choice (const :tag
"Value of `fill-column'")
101 (const :tag
"Full frame width" frame-width
)
102 (integer :tag
"Number of columns"))
105 (defcustom rcirc-fill-prefix nil
106 "*Text to insert before filled lines.
107 If nil, calculate the prefix dynamically to line up text
108 underneath each nick."
109 :type
'(choice (const :tag
"Dynamic" nil
)
110 (string :tag
"Prefix text"))
113 (defvar rcirc-ignore-buffer-activity-flag nil
114 "If non-nil, ignore activity in this buffer.")
115 (make-variable-buffer-local 'rcirc-ignore-buffer-activity-flag
)
117 (defvar rcirc-low-priority-flag nil
118 "If non-nil, activity in this buffer is considered low priority.")
119 (make-variable-buffer-local 'rcirc-low-priority-flag
)
121 (defcustom rcirc-time-format
"%H:%M "
122 "*Describes how timestamps are printed.
123 Used as the first arg to `format-time-string'."
127 (defcustom rcirc-input-ring-size
1024
128 "*Size of input history ring."
132 (defcustom rcirc-read-only-flag t
133 "*Non-nil means make text in IRC buffers read-only."
137 (defcustom rcirc-buffer-maximum-lines nil
138 "*The maximum size in lines for rcirc buffers.
139 Channel buffers are truncated from the top to be no greater than this
140 number. If zero or nil, no truncating is done."
141 :type
'(choice (const :tag
"No truncation" nil
)
142 (integer :tag
"Number of lines"))
145 (defcustom rcirc-scroll-show-maximum-output t
146 "*If non-nil, scroll buffer to keep the point at the bottom of
151 (defcustom rcirc-authinfo nil
152 "List of authentication passwords.
153 Each element of the list is a list with a SERVER-REGEXP string
154 and a method symbol followed by method specific arguments.
156 The valid METHOD symbols are `nickserv', `chanserv' and
159 The required ARGUMENTS for each METHOD symbol are:
160 `nickserv': NICK PASSWORD
161 `chanserv': NICK CHANNEL PASSWORD
162 `bitlbee': NICK PASSWORD
165 ((\"freenode\" nickserv \"bob\" \"p455w0rd\")
166 (\"freenode\" chanserv \"bob\" \"#bobland\" \"passwd99\")
167 (\"bitlbee\" bitlbee \"robert\" \"sekrit\"))"
168 :type
'(alist :key-type
(string :tag
"Server")
169 :value-type
(choice (list :tag
"NickServ"
172 (string :tag
"Password"))
173 (list :tag
"ChanServ"
176 (string :tag
"Channel")
177 (string :tag
"Password"))
181 (string :tag
"Password"))))
184 (defcustom rcirc-auto-authenticate-flag t
185 "*Non-nil means automatically send authentication string to server.
186 See also `rcirc-authinfo'."
190 (defcustom rcirc-prompt
"> "
191 "Prompt string to use in IRC buffers.
193 The following replacements are made:
196 %t is the buffer target, a channel or a user.
198 Setting this alone will not affect the prompt;
199 use either M-x customize or also call `rcirc-update-prompt'."
201 :set
'rcirc-set-changed
202 :initialize
'custom-initialize-default
205 (defcustom rcirc-keywords nil
206 "List of keywords to highlight in message text."
207 :type
'(repeat string
)
210 (defcustom rcirc-ignore-list
()
211 "List of ignored nicks.
212 Use /ignore to list them, use /ignore NICK to add or remove a nick."
213 :type
'(repeat string
)
216 (defvar rcirc-ignore-list-automatic
()
217 "List of ignored nicks added to `rcirc-ignore-list' because of renaming.
218 When an ignored person renames, their nick is added to both lists.
219 Nicks will be removed from the automatic list on follow-up renamings or
222 (defcustom rcirc-bright-nicks nil
223 "List of nicks to be emphasized.
224 See `rcirc-bright-nick' face."
225 :type
'(repeat string
)
228 (defcustom rcirc-dim-nicks nil
229 "List of nicks to be deemphasized.
230 See `rcirc-dim-nick' face."
231 :type
'(repeat string
)
234 (defcustom rcirc-print-hooks nil
235 "Hook run after text is printed.
236 Called with 5 arguments, PROCESS, SENDER, RESPONSE, TARGET and TEXT."
240 (defcustom rcirc-always-use-server-buffer-flag nil
241 "Non-nil means messages without a channel target will go to the server buffer."
245 (defcustom rcirc-decode-coding-system
'utf-8
246 "Coding system used to decode incoming irc messages."
250 (defcustom rcirc-encode-coding-system
'utf-8
251 "Coding system used to encode outgoing irc messages."
255 (defcustom rcirc-coding-system-alist nil
256 "Alist to decide a coding system to use for a channel I/O operation.
257 The format is ((PATTERN . VAL) ...).
258 PATTERN is either a string or a cons of strings.
259 If PATTERN is a string, it is used to match a target.
260 If PATTERN is a cons of strings, the car part is used to match a
261 target, and the cdr part is used to match a server.
262 VAL is either a coding system or a cons of coding systems.
263 If VAL is a coding system, it is used for both decoding and encoding
265 If VAL is a cons of coding systems, the car part is used for decoding,
266 and the cdr part is used for encoding."
267 :type
'(alist :key-type
(choice (string :tag
"Channel Regexp")
268 (cons (string :tag
"Channel Regexp")
269 (string :tag
"Server Regexp")))
270 :value-type
(choice coding-system
271 (cons (coding-system :tag
"Decode")
272 (coding-system :tag
"Encode"))))
275 (defcustom rcirc-multiline-major-mode
'fundamental-mode
276 "Major-mode function to use in multiline edit buffers."
280 (defvar rcirc-nick nil
)
282 (defvar rcirc-prompt-start-marker nil
)
283 (defvar rcirc-prompt-end-marker nil
)
285 (defvar rcirc-nick-table nil
)
287 (defvar rcirc-nick-syntax-table
288 (let ((table (make-syntax-table text-mode-syntax-table
)))
289 (mapc (lambda (c) (modify-syntax-entry c
"w" table
))
291 (modify-syntax-entry ?
' "_" table
)
293 "Syntax table which includes all nick characters as word constituents.")
295 ;; each process has an alist of (target . buffer) pairs
296 (defvar rcirc-buffer-alist nil
)
298 (defvar rcirc-activity nil
299 "List of buffers with unviewed activity.")
301 (defvar rcirc-activity-string
""
302 "String displayed in modeline representing `rcirc-activity'.")
303 (put 'rcirc-activity-string
'risky-local-variable t
)
305 (defvar rcirc-server-buffer nil
306 "The server buffer associated with this channel buffer.")
308 (defvar rcirc-target nil
309 "The channel or user associated with this buffer.")
311 (defvar rcirc-urls nil
312 "List of urls seen in the current buffer.")
313 (put 'rcirc-urls
'permanent-local t
)
315 (defvar rcirc-timeout-seconds
60
316 "Kill connection after this many seconds if there is no activity.")
318 (defconst rcirc-id-string
(concat "rcirc on GNU Emacs " emacs-version
))
320 (defvar rcirc-startup-channels nil
)
324 If ARG is non-nil, prompt for a server to connect to."
327 (let* ((server (read-string "IRC Server: " rcirc-default-server
))
328 (port (read-string "IRC Port: " (number-to-string rcirc-default-port
)))
329 (nick (read-string "IRC Nick: " rcirc-default-nick
))
330 (channels (split-string
331 (read-string "IRC Channels: "
332 (mapconcat 'identity
(rcirc-startup-channels server
) " "))
334 (rcirc-connect server port nick rcirc-default-user-name rcirc-default-user-full-name
336 ;; make new connection using defaults unless already connected to
337 ;; the default rcirc-server
339 (dolist (p (rcirc-process-list))
340 (when (string= rcirc-default-server
(process-name p
))
343 (rcirc-connect rcirc-default-server rcirc-default-port
344 rcirc-default-nick rcirc-default-user-name
345 rcirc-default-user-full-name
346 (rcirc-startup-channels rcirc-default-server
))
347 (switch-to-buffer (process-buffer connected
))
348 (message "Connected to %s"
349 (process-contact (get-buffer-process (current-buffer))
352 (defalias 'irc
'rcirc
)
355 (defvar rcirc-process-output nil
)
356 (defvar rcirc-topic nil
)
357 (defvar rcirc-keepalive-timer nil
)
358 (defvar rcirc-last-server-message-time nil
)
359 (defvar rcirc-server nil
) ; server provided by server
360 (defvar rcirc-server-name nil
) ; server name given by 001 response
361 (defvar rcirc-timeout-timer nil
)
362 (defvar rcirc-user-disconnect nil
)
363 (defvar rcirc-connecting nil
)
364 (defvar rcirc-process nil
)
367 (defun rcirc-connect (&optional server port nick user-name full-name startup-channels
)
369 (message "Connecting to %s..." server
)
370 (let* ((inhibit-eol-conversion)
371 (port-number (if port
373 (string-to-number port
)
376 (server (or server rcirc-default-server
))
377 (nick (or nick rcirc-default-nick
))
378 (user-name (or user-name rcirc-default-user-name
))
379 (full-name (or full-name rcirc-default-user-full-name
))
380 (startup-channels startup-channels
)
381 (process (make-network-process :name server
:host server
:service port-number
)))
383 (set-process-coding-system process
'raw-text
'raw-text
)
384 (switch-to-buffer (rcirc-generate-new-buffer-name process nil
))
385 (set-process-buffer process
(current-buffer))
386 (rcirc-mode process nil
)
387 (set-process-sentinel process
'rcirc-sentinel
)
388 (set-process-filter process
'rcirc-filter
)
389 (make-local-variable 'rcirc-process
)
390 (setq rcirc-process process
)
391 (make-local-variable 'rcirc-server
)
392 (setq rcirc-server server
)
393 (make-local-variable 'rcirc-server-name
)
394 (setq rcirc-server-name server
) ; update when we get 001 response
395 (make-local-variable 'rcirc-buffer-alist
)
396 (setq rcirc-buffer-alist nil
)
397 (make-local-variable 'rcirc-nick-table
)
398 (setq rcirc-nick-table
(make-hash-table :test
'equal
))
399 (make-local-variable 'rcirc-nick
)
400 (setq rcirc-nick nick
)
401 (make-local-variable 'rcirc-process-output
)
402 (setq rcirc-process-output nil
)
403 (make-local-variable 'rcirc-startup-channels
)
404 (setq rcirc-startup-channels startup-channels
)
405 (make-local-variable 'rcirc-last-server-message-time
)
406 (setq rcirc-last-server-message-time
(current-time))
407 (make-local-variable 'rcirc-timeout-timer
)
408 (setq rcirc-timeout-timer nil
)
409 (make-local-variable 'rcirc-user-disconnect
)
410 (setq rcirc-user-disconnect nil
)
411 (make-local-variable 'rcirc-connecting
)
412 (setq rcirc-connecting t
)
415 (rcirc-send-string process
(concat "NICK " nick
))
416 (rcirc-send-string process
(concat "USER " user-name
417 " hostname servername :"
420 ;; setup ping timer if necessary
421 (unless rcirc-keepalive-timer
422 (setq rcirc-keepalive-timer
423 (run-at-time 0 (/ rcirc-timeout-seconds
2) 'rcirc-keepalive
)))
425 (message "Connecting to %s...done" server
)
427 ;; return process object
430 (defmacro with-rcirc-process-buffer
(process &rest body
)
431 (declare (indent 1) (debug t
))
432 `(with-current-buffer (process-buffer ,process
)
435 (defmacro with-rcirc-server-buffer
(&rest body
)
436 (declare (indent 0) (debug t
))
437 `(with-current-buffer rcirc-server-buffer
440 (defun rcirc-keepalive ()
441 "Send keep alive pings to active rcirc processes.
442 Kill processes that have not received a server message since the
444 (if (rcirc-process-list)
445 (mapc (lambda (process)
446 (with-rcirc-process-buffer process
447 (when (not rcirc-connecting
)
448 (rcirc-send-string process
(concat "PING " (rcirc-server-name process
))))))
449 (rcirc-process-list))
450 ;; no processes, clean up timer
451 (cancel-timer rcirc-keepalive-timer
)
452 (setq rcirc-keepalive-timer nil
)))
454 (defvar rcirc-debug-buffer
" *rcirc debug*")
455 (defvar rcirc-debug-flag nil
456 "If non-nil, write information to `rcirc-debug-buffer'.")
457 (defun rcirc-debug (process text
)
458 "Add an entry to the debug log including PROCESS and TEXT.
459 Debug text is written to `rcirc-debug-buffer' if `rcirc-debug-flag'
461 (when rcirc-debug-flag
463 (save-window-excursion
464 (set-buffer (get-buffer-create rcirc-debug-buffer
))
465 (goto-char (point-max))
468 (format-time-string "%Y-%m-%dT%T ") (process-name process
)
472 (defvar rcirc-sentinel-hooks nil
473 "Hook functions called when the process sentinel is called.
474 Functions are called with PROCESS and SENTINEL arguments.")
476 (defun rcirc-sentinel (process sentinel
)
477 "Called when PROCESS receives SENTINEL."
478 (let ((sentinel (replace-regexp-in-string "\n" "" sentinel
)))
479 (rcirc-debug process
(format "SENTINEL: %S %S\n" process sentinel
))
480 (with-rcirc-process-buffer process
481 (dolist (buffer (cons nil
(mapcar 'cdr rcirc-buffer-alist
)))
482 (with-current-buffer (or buffer
(current-buffer))
483 (rcirc-print process
"rcirc.el" "ERROR" rcirc-target
484 (format "%s: %s (%S)"
485 (process-name process
)
487 (process-status process
)) (not rcirc-target
))
488 ;; remove the prompt from buffers
489 (let ((inhibit-read-only t
))
490 (delete-region rcirc-prompt-start-marker
491 rcirc-prompt-end-marker
))))
492 (run-hook-with-args 'rcirc-sentinel-hooks process sentinel
))))
494 (defun rcirc-process-list ()
495 "Return a list of rcirc processes."
498 (when (buffer-live-p (process-buffer p
))
499 (with-rcirc-process-buffer p
500 (when (eq major-mode
'rcirc-mode
)
501 (setq ps
(cons p ps
))))))
505 (defvar rcirc-receive-message-hooks nil
506 "Hook functions run when a message is received from server.
507 Function is called with PROCESS, COMMAND, SENDER, ARGS and LINE.")
508 (defun rcirc-filter (process output
)
509 "Called when PROCESS receives OUTPUT."
510 (rcirc-debug process output
)
511 (rcirc-reschedule-timeout process
)
512 (with-rcirc-process-buffer process
513 (setq rcirc-last-server-message-time
(current-time))
514 (setq rcirc-process-output
(concat rcirc-process-output output
))
515 (when (= (aref rcirc-process-output
516 (1- (length rcirc-process-output
))) ?
\n)
518 (rcirc-process-server-response process line
))
519 (split-string rcirc-process-output
"[\n\r]" t
))
520 (setq rcirc-process-output nil
))))
522 (defun rcirc-reschedule-timeout (process)
523 (with-rcirc-process-buffer process
524 (when (not rcirc-connecting
)
525 (with-rcirc-process-buffer process
526 (when rcirc-timeout-timer
(cancel-timer rcirc-timeout-timer
))
527 (setq rcirc-timeout-timer
(run-at-time rcirc-timeout-seconds nil
528 'rcirc-delete-process
531 (defun rcirc-delete-process (process)
532 (message "delete process %S" process
)
533 (delete-process process
))
535 (defvar rcirc-trap-errors-flag t
)
536 (defun rcirc-process-server-response (process text
)
537 (if rcirc-trap-errors-flag
539 (rcirc-process-server-response-1 process text
)
541 (rcirc-print process
"RCIRC" "ERROR" nil
542 (format "\"%s\" %s" text err
) t
)))
543 (rcirc-process-server-response-1 process text
)))
545 (defun rcirc-process-server-response-1 (process text
)
546 (if (string-match "^\\(:\\([^ ]+\\) \\)?\\([^ ]+\\) \\(.+\\)$" text
)
547 (let* ((user (match-string 2 text
))
548 (sender (rcirc-user-nick user
))
549 (cmd (match-string 3 text
))
550 (args (match-string 4 text
))
551 (handler (intern-soft (concat "rcirc-handler-" cmd
))))
552 (string-match "^\\([^:]*\\):?\\(.+\\)?$" args
)
553 (let* ((args1 (match-string 1 args
))
554 (args2 (match-string 2 args
))
555 (args (delq nil
(append (split-string args1
" " t
)
557 (if (not (fboundp handler
))
558 (rcirc-handler-generic process cmd sender args text
)
559 (funcall handler process sender args text
))
560 (run-hook-with-args 'rcirc-receive-message-hooks
561 process cmd sender args text
)))
562 (message "UNHANDLED: %s" text
)))
564 (defvar rcirc-responses-no-activity
'("305" "306")
565 "Responses that don't trigger activity in the mode-line indicator.")
567 (defun rcirc-handler-generic (process response sender args text
)
568 "Generic server response handler."
569 (rcirc-print process sender response nil
570 (mapconcat 'identity
(cdr args
) " ")
571 (not (member response rcirc-responses-no-activity
))))
573 (defun rcirc-send-string (process string
)
574 "Send PROCESS a STRING plus a newline."
575 (let ((string (concat (encode-coding-string string rcirc-encode-coding-system
)
577 (unless (eq (process-status process
) 'open
)
578 (error "Network connection to %s is not open"
579 (process-name process
)))
580 (rcirc-debug process string
)
581 (process-send-string process string
)))
583 (defun rcirc-buffer-process (&optional buffer
)
584 "Return the process associated with channel BUFFER.
585 With no argument or nil as argument, use the current buffer."
586 (or (get-buffer-process (if buffer
587 (with-current-buffer buffer
589 rcirc-server-buffer
))
592 (defun rcirc-server-name (process)
593 "Return PROCESS server name, given by the 001 response."
594 (with-rcirc-process-buffer process
595 (or rcirc-server-name rcirc-default-server
)))
597 (defun rcirc-nick (process)
598 "Return PROCESS nick."
599 (with-rcirc-process-buffer process
600 (or rcirc-nick rcirc-default-nick
)))
602 (defun rcirc-buffer-nick (&optional buffer
)
603 "Return the nick associated with BUFFER.
604 With no argument or nil as argument, use the current buffer."
605 (with-current-buffer (or buffer
(current-buffer))
606 (with-current-buffer rcirc-server-buffer
607 (or rcirc-nick rcirc-default-nick
))))
609 (defvar rcirc-max-message-length
420
610 "Messages longer than this value will be split.")
612 (defun rcirc-send-message (process target message
&optional noticep
)
613 "Send TARGET associated with PROCESS a privmsg with text MESSAGE.
614 If NOTICEP is non-nil, send a notice instead of privmsg."
615 ;; max message length is 512 including CRLF
616 (let* ((response (if noticep
"NOTICE" "PRIVMSG"))
617 (oversize (> (length message
) rcirc-max-message-length
))
619 (substring message
0 rcirc-max-message-length
)
621 (text (if (string= text
"")
625 (substring message rcirc-max-message-length
))))
626 (rcirc-get-buffer-create process target
)
627 (rcirc-print process
(rcirc-nick process
) response target text
)
628 (rcirc-send-string process
(concat response
" " target
" :" text
))
629 (when more
(rcirc-send-message process target more noticep
))))
631 (defvar rcirc-input-ring nil
)
632 (defvar rcirc-input-ring-index
0)
633 (defun rcirc-prev-input-string (arg)
634 (ring-ref rcirc-input-ring
(+ rcirc-input-ring-index arg
)))
636 (defun rcirc-insert-prev-input (arg)
638 (when (<= rcirc-prompt-end-marker
(point))
639 (delete-region rcirc-prompt-end-marker
(point-max))
640 (insert (rcirc-prev-input-string 0))
641 (setq rcirc-input-ring-index
(1+ rcirc-input-ring-index
))))
643 (defun rcirc-insert-next-input (arg)
645 (when (<= rcirc-prompt-end-marker
(point))
646 (delete-region rcirc-prompt-end-marker
(point-max))
647 (setq rcirc-input-ring-index
(1- rcirc-input-ring-index
))
648 (insert (rcirc-prev-input-string -
1))))
650 (defvar rcirc-nick-completions nil
)
651 (defvar rcirc-nick-completion-start-offset nil
)
653 (defun rcirc-complete-nick ()
654 "Cycle through nick completions from list of nicks in channel."
656 (if (eq last-command this-command
)
657 (setq rcirc-nick-completions
658 (append (cdr rcirc-nick-completions
)
659 (list (car rcirc-nick-completions
))))
660 (setq rcirc-nick-completion-start-offset
662 (if (re-search-backward " " rcirc-prompt-end-marker t
)
664 rcirc-prompt-end-marker
))
665 rcirc-prompt-end-marker
))
666 (setq rcirc-nick-completions
667 (let ((completion-ignore-case t
))
670 (+ rcirc-prompt-end-marker
671 rcirc-nick-completion-start-offset
)
673 (mapcar (lambda (x) (cons x nil
))
674 (rcirc-channel-nicks (rcirc-buffer-process)
676 (let ((completion (car rcirc-nick-completions
)))
678 (rcirc-put-nick-channel (rcirc-buffer-process) completion rcirc-target
)
679 (delete-region (+ rcirc-prompt-end-marker
680 rcirc-nick-completion-start-offset
)
682 (insert (concat completion
683 (if (= (+ rcirc-prompt-end-marker
684 rcirc-nick-completion-start-offset
)
685 rcirc-prompt-end-marker
)
688 (defun set-rcirc-decode-coding-system (coding-system)
689 "Set the decode coding system used in this channel."
690 (interactive "zCoding system for incoming messages: ")
691 (setq rcirc-decode-coding-system coding-system
))
693 (defun set-rcirc-encode-coding-system (coding-system)
694 "Set the encode coding system used in this channel."
695 (interactive "zCoding system for outgoing messages: ")
696 (setq rcirc-encode-coding-system coding-system
))
698 (defvar rcirc-mode-map
(make-sparse-keymap)
699 "Keymap for rcirc mode.")
701 (define-key rcirc-mode-map
(kbd "RET") 'rcirc-send-input
)
702 (define-key rcirc-mode-map
(kbd "M-p") 'rcirc-insert-prev-input
)
703 (define-key rcirc-mode-map
(kbd "M-n") 'rcirc-insert-next-input
)
704 (define-key rcirc-mode-map
(kbd "TAB") 'rcirc-complete-nick
)
705 (define-key rcirc-mode-map
(kbd "C-c C-b") 'rcirc-browse-url
)
706 (define-key rcirc-mode-map
(kbd "C-c C-c") 'rcirc-edit-multiline
)
707 (define-key rcirc-mode-map
(kbd "C-c C-j") 'rcirc-cmd-join
)
708 (define-key rcirc-mode-map
(kbd "C-c C-k") 'rcirc-cmd-kick
)
709 (define-key rcirc-mode-map
(kbd "C-c C-l") 'rcirc-toggle-low-priority
)
710 (define-key rcirc-mode-map
(kbd "C-c C-d") 'rcirc-cmd-mode
)
711 (define-key rcirc-mode-map
(kbd "C-c C-m") 'rcirc-cmd-msg
)
712 (define-key rcirc-mode-map
(kbd "C-c C-r") 'rcirc-cmd-nick
) ; rename
713 (define-key rcirc-mode-map
(kbd "C-c C-o") 'rcirc-cmd-oper
)
714 (define-key rcirc-mode-map
(kbd "C-c C-p") 'rcirc-cmd-part
)
715 (define-key rcirc-mode-map
(kbd "C-c C-q") 'rcirc-cmd-query
)
716 (define-key rcirc-mode-map
(kbd "C-c C-t") 'rcirc-cmd-topic
)
717 (define-key rcirc-mode-map
(kbd "C-c C-n") 'rcirc-cmd-names
)
718 (define-key rcirc-mode-map
(kbd "C-c C-w") 'rcirc-cmd-whois
)
719 (define-key rcirc-mode-map
(kbd "C-c C-x") 'rcirc-cmd-quit
)
720 (define-key rcirc-mode-map
(kbd "C-c TAB") ; C-i
721 'rcirc-toggle-ignore-buffer-activity
)
722 (define-key rcirc-mode-map
(kbd "C-c C-s") 'rcirc-switch-to-server-buffer
)
723 (define-key rcirc-mode-map
(kbd "C-c C-a") 'rcirc-jump-to-first-unread-line
)
725 (defvar rcirc-browse-url-map
(make-sparse-keymap)
726 "Keymap used for browsing URLs in `rcirc-mode'.")
728 (define-key rcirc-browse-url-map
(kbd "RET") 'rcirc-browse-url-at-point
)
729 (define-key rcirc-browse-url-map
(kbd "<mouse-2>") 'rcirc-browse-url-at-mouse
)
731 (defvar rcirc-short-buffer-name nil
732 "Generated abbreviation to use to indicate buffer activity.")
734 (defvar rcirc-mode-hook nil
735 "Hook run when setting up rcirc buffer.")
737 (defvar rcirc-last-post-time nil
)
739 (defun rcirc-mode (process target
)
740 "Major mode for IRC channel buffers.
743 (kill-all-local-variables)
744 (use-local-map rcirc-mode-map
)
745 (setq mode-name
"rcirc")
746 (setq major-mode
'rcirc-mode
)
748 (make-local-variable 'rcirc-input-ring
)
749 (setq rcirc-input-ring
(make-ring rcirc-input-ring-size
))
750 (make-local-variable 'rcirc-server-buffer
)
751 (setq rcirc-server-buffer
(process-buffer process
))
752 (make-local-variable 'rcirc-target
)
753 (setq rcirc-target target
)
754 (make-local-variable 'rcirc-topic
)
755 (setq rcirc-topic nil
)
756 (make-local-variable 'rcirc-last-post-time
)
757 (setq rcirc-last-post-time
(current-time))
759 (make-local-variable 'rcirc-short-buffer-name
)
760 (setq rcirc-short-buffer-name nil
)
761 (make-local-variable 'rcirc-urls
)
762 (setq use-hard-newlines t
)
764 (make-local-variable 'rcirc-decode-coding-system
)
765 (make-local-variable 'rcirc-encode-coding-system
)
766 (dolist (i rcirc-coding-system-alist
)
767 (let ((chan (if (consp (car i
)) (caar i
) (car i
)))
768 (serv (if (consp (car i
)) (cdar i
) "")))
769 (when (and (string-match chan
(or target
""))
770 (string-match serv
(rcirc-server-name process
)))
771 (setq rcirc-decode-coding-system
(if (consp (cdr i
)) (cadr i
) (cdr i
))
772 rcirc-encode-coding-system
(if (consp (cdr i
)) (cddr i
) (cdr i
))))))
774 ;; setup the prompt and markers
775 (make-local-variable 'rcirc-prompt-start-marker
)
776 (setq rcirc-prompt-start-marker
(make-marker))
777 (set-marker rcirc-prompt-start-marker
(point-max))
778 (make-local-variable 'rcirc-prompt-end-marker
)
779 (setq rcirc-prompt-end-marker
(make-marker))
780 (set-marker rcirc-prompt-end-marker
(point-max))
781 (rcirc-update-prompt)
782 (goto-char rcirc-prompt-end-marker
)
783 (make-local-variable 'overlay-arrow-position
)
784 (setq overlay-arrow-position
(make-marker))
785 (set-marker overlay-arrow-position nil
)
787 ;; if the user changes the major mode or kills the buffer, there is
788 ;; cleanup work to do
789 (add-hook 'change-major-mode-hook
'rcirc-change-major-mode-hook nil t
)
790 (add-hook 'kill-buffer-hook
'rcirc-kill-buffer-hook nil t
)
792 ;; add to buffer list, and update buffer abbrevs
793 (when target
; skip server buffer
794 (let ((buffer (current-buffer)))
795 (with-rcirc-process-buffer process
796 (setq rcirc-buffer-alist
(cons (cons target buffer
)
797 rcirc-buffer-alist
))))
798 (rcirc-update-short-buffer-names))
800 (run-hooks 'rcirc-mode-hook
))
802 (defun rcirc-update-prompt (&optional all
)
803 "Reset the prompt string in the current buffer.
805 If ALL is non-nil, update prompts in all IRC buffers."
807 (mapc (lambda (process)
808 (mapc (lambda (buffer)
809 (with-current-buffer buffer
810 (rcirc-update-prompt)))
811 (with-rcirc-process-buffer process
812 (mapcar 'cdr rcirc-buffer-alist
))))
813 (rcirc-process-list))
814 (let ((inhibit-read-only t
)
815 (prompt (or rcirc-prompt
"")))
818 (replace-regexp-in-string (car rep
) (cdr rep
) prompt
)))
819 (list (cons "%n" (rcirc-buffer-nick))
820 (cons "%s" (with-rcirc-server-buffer rcirc-server-name
))
821 (cons "%t" (or rcirc-target
""))))
823 (delete-region rcirc-prompt-start-marker rcirc-prompt-end-marker
)
824 (goto-char rcirc-prompt-start-marker
)
825 (let ((start (point)))
826 (insert-before-markers prompt
)
827 (set-marker rcirc-prompt-start-marker start
)
828 (when (not (zerop (- rcirc-prompt-end-marker
829 rcirc-prompt-start-marker
)))
830 (add-text-properties rcirc-prompt-start-marker
831 rcirc-prompt-end-marker
832 (list 'face
'rcirc-prompt
833 'read-only t
'field t
834 'front-sticky t
'rear-nonsticky t
))))))))
836 (defun rcirc-set-changed (option value
)
837 "Set OPTION to VALUE and do updates after a customization change."
838 (set-default option value
)
839 (cond ((eq option
'rcirc-prompt
)
840 (rcirc-update-prompt 'all
))
842 (error "Bad option %s" option
))))
844 (defun rcirc-channel-p (target)
845 "Return t if TARGET is a channel name."
847 (not (zerop (length target
)))
848 (or (eq (aref target
0) ?
#)
849 (eq (aref target
0) ?
&))))
851 (defun rcirc-kill-buffer-hook ()
852 "Part the channel when killing an rcirc buffer."
853 (when (eq major-mode
'rcirc-mode
)
854 (rcirc-clean-up-buffer "Killed buffer")))
856 (defun rcirc-change-major-mode-hook ()
857 "Part the channel when changing the major-mode."
858 (rcirc-clean-up-buffer "Changed major mode"))
860 (defun rcirc-clean-up-buffer (reason)
861 (let ((buffer (current-buffer)))
862 (rcirc-clear-activity buffer
)
863 (when (and (rcirc-buffer-process)
864 (eq (process-status (rcirc-buffer-process)) 'open
))
865 (with-rcirc-server-buffer
866 (setq rcirc-buffer-alist
867 (rassq-delete-all buffer rcirc-buffer-alist
)))
868 (rcirc-update-short-buffer-names)
869 (if (rcirc-channel-p rcirc-target
)
870 (rcirc-send-string (rcirc-buffer-process)
871 (concat "PART " rcirc-target
" :" reason
))
873 (rcirc-remove-nick-channel (rcirc-buffer-process)
877 (defun rcirc-generate-new-buffer-name (process target
)
878 "Return a buffer name based on PROCESS and TARGET.
879 This is used for the initial name given to IRC buffers."
881 (concat target
"@" (process-name process
))
882 (concat "*" (process-name process
) "*")))
884 (defun rcirc-get-buffer (process target
&optional server
)
885 "Return the buffer associated with the PROCESS and TARGET.
887 If optional argument SERVER is non-nil, return the server buffer
888 if there is no existing buffer for TARGET, otherwise return nil."
889 (with-rcirc-process-buffer process
892 (let ((buffer (cdr (assoc-string target rcirc-buffer-alist t
))))
893 (or buffer
(when server
(current-buffer)))))))
895 (defun rcirc-get-buffer-create (process target
)
896 "Return the buffer associated with the PROCESS and TARGET.
897 Create the buffer if it doesn't exist."
898 (let ((buffer (rcirc-get-buffer process target
)))
899 (if (and buffer
(buffer-live-p buffer
))
900 (with-current-buffer buffer
901 (when (not rcirc-target
)
902 (setq rcirc-target target
))
905 (with-rcirc-process-buffer process
906 (let ((new-buffer (get-buffer-create
907 (rcirc-generate-new-buffer-name process target
))))
908 (with-current-buffer new-buffer
909 (rcirc-mode process target
))
910 (rcirc-put-nick-channel process
(rcirc-nick process
) target
)
913 (defun rcirc-send-input ()
914 "Send input to target associated with the current buffer."
916 (if (< (point) rcirc-prompt-end-marker
)
917 ;; copy the line down to the input area
920 (let ((start (if (eq (point) (point-min))
922 (if (get-text-property (1- (point)) 'hard
)
924 (previous-single-property-change (point) 'hard
))))
925 (end (next-single-property-change (1+ (point)) 'hard
)))
926 (goto-char (point-max))
927 (insert (replace-regexp-in-string
929 (buffer-substring-no-properties start end
)))))
931 (goto-char (point-max))
932 (when (not (equal 0 (- (point) rcirc-prompt-end-marker
)))
933 ;; delete a trailing newline
934 (when (eq (point) (point-at-bol))
935 (delete-backward-char 1))
936 (let ((input (buffer-substring-no-properties
937 rcirc-prompt-end-marker
(point))))
938 (dolist (line (split-string input
"\n"))
939 (rcirc-process-input-line line
))
942 (ring-insert rcirc-input-ring input
)
943 (setq rcirc-input-ring-index
0))))))
945 (defun rcirc-process-input-line (line)
946 (if (string-match "^/\\([^ ]+\\) ?\\(.*\\)$" line
)
947 (rcirc-process-command (match-string 1 line
)
948 (match-string 2 line
)
950 (rcirc-process-message line
)))
952 (defun rcirc-process-message (line)
953 (if (not rcirc-target
)
954 (message "Not joined (no target)")
955 (delete-region rcirc-prompt-end-marker
(point))
956 (rcirc-send-message (rcirc-buffer-process) rcirc-target line
)
957 (setq rcirc-last-post-time
(current-time))))
959 (defun rcirc-process-command (command args line
)
960 (if (eq (aref command
0) ?
/)
961 ;; "//text" will send "/text" as a message
962 (rcirc-process-message (substring line
1))
963 (let ((fun (intern-soft (concat "rcirc-cmd-" command
)))
964 (process (rcirc-buffer-process)))
966 (with-current-buffer (current-buffer)
967 (delete-region rcirc-prompt-end-marker
(point))
968 (if (string= command
"me")
969 (rcirc-print process
(rcirc-buffer-nick)
970 "ACTION" rcirc-target args
)
971 (rcirc-print process
(rcirc-buffer-nick)
972 "COMMAND" rcirc-target line
))
973 (set-marker rcirc-prompt-end-marker
(point))
975 (funcall fun args process rcirc-target
)
976 (rcirc-send-string process
977 (concat command
" :" args
)))))))
979 (defvar rcirc-parent-buffer nil
)
980 (defvar rcirc-window-configuration nil
)
981 (defun rcirc-edit-multiline ()
982 "Move current edit to a dedicated buffer."
984 (let ((pos (1+ (- (point) rcirc-prompt-end-marker
))))
985 (goto-char (point-max))
986 (let ((text (buffer-substring rcirc-prompt-end-marker
(point)))
987 (parent (buffer-name)))
988 (delete-region rcirc-prompt-end-marker
(point))
989 (setq rcirc-window-configuration
(current-window-configuration))
990 (pop-to-buffer (concat "*multiline " parent
"*"))
991 (funcall rcirc-multiline-major-mode
)
992 (rcirc-multiline-minor-mode 1)
993 (setq rcirc-parent-buffer parent
)
995 (and (> pos
0) (goto-char pos
))
996 (message "Type C-c C-c to return text to %s, or C-c C-k to cancel" parent
))))
998 (defvar rcirc-multiline-minor-mode-map
(make-sparse-keymap)
999 "Keymap for multiline mode in rcirc.")
1000 (define-key rcirc-multiline-minor-mode-map
1001 (kbd "C-c C-c") 'rcirc-multiline-minor-submit
)
1002 (define-key rcirc-multiline-minor-mode-map
1003 (kbd "C-x C-s") 'rcirc-multiline-minor-submit
)
1004 (define-key rcirc-multiline-minor-mode-map
1005 (kbd "C-c C-k") 'rcirc-multiline-minor-cancel
)
1006 (define-key rcirc-multiline-minor-mode-map
1007 (kbd "ESC ESC ESC") 'rcirc-multiline-minor-cancel
)
1009 (define-minor-mode rcirc-multiline-minor-mode
1010 "Minor mode for editing multiple lines in rcirc."
1012 :lighter
" rcirc-mline"
1013 :keymap rcirc-multiline-minor-mode-map
1016 (make-local-variable 'rcirc-parent-buffer
)
1017 (put 'rcirc-parent-buffer
'permanent-local t
)
1018 (setq fill-column rcirc-max-message-length
))
1020 (defun rcirc-multiline-minor-submit ()
1021 "Send the text in buffer back to parent buffer."
1023 (assert rcirc-parent-buffer
)
1024 (untabify (point-min) (point-max))
1025 (let ((text (buffer-substring (point-min) (point-max)))
1026 (buffer (current-buffer))
1028 (set-buffer rcirc-parent-buffer
)
1029 (goto-char (point-max))
1031 (kill-buffer buffer
)
1032 (set-window-configuration rcirc-window-configuration
)
1033 (goto-char (+ rcirc-prompt-end-marker
(1- pos
)))))
1035 (defun rcirc-multiline-minor-cancel ()
1036 "Cancel the multiline edit."
1038 (kill-buffer (current-buffer))
1039 (set-window-configuration rcirc-window-configuration
))
1041 (defun rcirc-any-buffer (process)
1042 "Return a buffer for PROCESS, either the one selected or the process buffer."
1043 (if rcirc-always-use-server-buffer-flag
1044 (process-buffer process
)
1045 (let ((buffer (window-buffer (selected-window))))
1047 (with-current-buffer buffer
1048 (and (eq major-mode
'rcirc-mode
)
1049 (eq (rcirc-buffer-process) process
))))
1051 (process-buffer process
)))))
1053 (defcustom rcirc-response-formats
1054 '(("PRIVMSG" .
"%T<%N> %m")
1055 ("NOTICE" .
"%T-%N- %m")
1056 ("ACTION" .
"%T[%N %m]")
1057 ("COMMAND" .
"%T%m")
1058 ("ERROR" .
"%T%fw!!! %m")
1059 (t .
"%T%fp*** %fs%n %r %m"))
1060 "An alist of formats used for printing responses.
1061 The format is looked up using the response-type as a key;
1062 if no match is found, the default entry (with a key of `t') is used.
1064 The entry's value part should be a string, which is inserted with
1065 the of the following escape sequences replaced by the described values:
1068 %n The sender's nick
1069 %N The sender's nick (with face `rcirc-my-nick' or `rcirc-other-nick')
1070 %r The response-type
1071 %T The timestamp (with face `rcirc-timestamp')
1073 %fw Following text uses the face `font-lock-warning-face'
1074 %fp Following text uses the face `rcirc-server-prefix'
1075 %fs Following text uses the face `rcirc-server'
1076 %f[FACE] Following text uses the face FACE
1077 %f- Following text uses the default face
1078 %% A literal `%' character"
1079 :type
'(alist :key-type
(choice (string :tag
"Type")
1080 (const :tag
"Default" t
))
1084 (defun rcirc-format-response-string (process sender response target text
)
1085 "Return a nicely-formatted response string, incorporating TEXT
1086 \(and perhaps other arguments). The specific formatting used
1087 is found by looking up RESPONSE in `rcirc-response-formats'."
1089 (split-string (or (cdr (assoc response rcirc-response-formats
))
1090 (cdr (assq t rcirc-response-formats
)))
1092 (sender (or sender
""))
1096 (when (equal (car chunks
) "")
1098 (dolist (chunk chunks
)
1099 (if (equal chunk
"")
1101 (setq key
(aref chunk
0))
1102 (setq chunk
(substring chunk
1)))
1105 ;; %% -- literal % character
1107 ((or (eq key ?n
) (eq key ?N
))
1109 (let ((nick (concat (if (string= (rcirc-server-name process
)
1113 (and target
(concat "," target
)))))
1117 (cond ((string= sender
(rcirc-nick process
))
1119 ((and rcirc-bright-nicks
1121 (regexp-opt rcirc-bright-nicks
)
1124 ((and rcirc-dim-nicks
1126 (regexp-opt rcirc-dim-nicks
)
1130 'rcirc-other-nick
))))))
1134 (format-time-string rcirc-time-format
(current-time))
1137 ;; %m -- message text
1138 (rcirc-markup-text process sender response
(rcirc-facify text face
)))
1141 (rcirc-facify (or rcirc-target
"") face
))
1144 (rcirc-facify response face
))
1146 ;; %f -- change face
1147 (setq face-key
(aref chunk
0))
1148 (setq chunk
(substring chunk
1))
1149 (cond ((eq face-key ?w
)
1150 ;; %fw -- warning face
1151 (setq face
'font-lock-warning-face
))
1153 ;; %fp -- server-prefix face
1154 (setq face
'rcirc-server-prefix
))
1156 ;; %fs -- warning face
1157 (setq face
'rcirc-server
))
1159 ;; %fs -- warning face
1161 ((and (eq face-key ?\
[)
1162 (string-match "^\\([^]]*\\)[]]" chunk
)
1163 (facep (match-string 1 chunk
)))
1164 ;; %f[...] -- named face
1165 (setq face
(intern (match-string 1 chunk
)))
1166 (setq chunk
(substring chunk
(match-end 0)))))
1168 (setq result
(concat result repl
(rcirc-facify chunk face
))))
1171 (defun rcirc-target-buffer (process sender response target text
)
1172 "Return a buffer to print the server response."
1173 (assert (not (bufferp target
)))
1174 (with-rcirc-process-buffer process
1176 (rcirc-any-buffer process
))
1177 ((not (rcirc-channel-p target
))
1178 ;; message from another user
1179 (if (string= response
"PRIVMSG")
1180 (rcirc-get-buffer-create process
(if (string= sender rcirc-nick
)
1183 (rcirc-get-buffer process target t
)))
1184 ((or (rcirc-get-buffer process target
)
1185 (rcirc-any-buffer process
))))))
1187 (defvar rcirc-activity-types nil
)
1188 (make-variable-buffer-local 'rcirc-activity-types
)
1189 (defvar rcirc-last-sender nil
)
1190 (make-variable-buffer-local 'rcirc-last-sender
)
1192 (defun rcirc-print (process sender response target text
&optional activity
)
1193 "Print TEXT in the buffer associated with TARGET.
1194 Format based on SENDER and RESPONSE. If ACTIVITY is non-nil,
1196 (or text
(setq text
""))
1197 (unless (or (member sender rcirc-ignore-list
)
1198 (member (with-syntax-table rcirc-nick-syntax-table
1199 (when (string-match "^\\([^/]\\w*\\)[:,]" text
)
1200 (match-string 1 text
)))
1202 (let* ((buffer (rcirc-target-buffer process sender response target text
))
1203 (inhibit-read-only t
))
1204 (with-current-buffer buffer
1205 (let ((moving (= (point) rcirc-prompt-end-marker
))
1206 (old-point (point-marker))
1207 (fill-start (marker-position rcirc-prompt-start-marker
)))
1209 (unless (string= sender
(rcirc-nick process
))
1210 ;; only decode text from other senders, not ours
1211 (setq text
(decode-coding-string text rcirc-decode-coding-system
))
1212 ;; mark the line with overlay arrow
1213 (unless (or (marker-position overlay-arrow-position
)
1214 (get-buffer-window (current-buffer)))
1215 (set-marker overlay-arrow-position
1216 (marker-position rcirc-prompt-start-marker
))))
1218 ;; temporarily set the marker insertion-type because
1219 ;; insert-before-markers results in hidden text in new buffers
1220 (goto-char rcirc-prompt-start-marker
)
1221 (set-marker-insertion-type rcirc-prompt-start-marker t
)
1222 (set-marker-insertion-type rcirc-prompt-end-marker t
)
1225 (rcirc-format-response-string process sender response nil
1228 (insert fmted-text
(propertize "\n" 'hard t
))
1229 (set-marker-insertion-type rcirc-prompt-start-marker nil
)
1230 (set-marker-insertion-type rcirc-prompt-end-marker nil
)
1232 (let ((text-start (make-marker)))
1233 (set-marker text-start
1234 (or (next-single-property-change fill-start
1236 rcirc-prompt-end-marker
))
1237 ;; squeeze spaces out of text before rcirc-text
1238 (fill-region fill-start
(1- text-start
))
1240 ;; fill the text we just inserted, maybe
1241 (when (and rcirc-fill-flag
1242 (not (string= response
"372"))) ;/motd
1244 (or rcirc-fill-prefix
1245 (make-string (- text-start fill-start
) ?\s
)))
1246 (fill-column (cond ((eq rcirc-fill-column
'frame-width
)
1251 (fill-region fill-start rcirc-prompt-start-marker
'left t
)))))
1253 ;; set inserted text to be read-only
1254 (when rcirc-read-only-flag
1255 (put-text-property rcirc-prompt-start-marker fill-start
'read-only t
)
1256 (let ((inhibit-read-only t
))
1257 (put-text-property rcirc-prompt-start-marker fill-start
1259 (put-text-property (1- (point)) (point) 'rear-nonsticky t
)))
1261 ;; truncate buffer if it is very long
1263 (when (and rcirc-buffer-maximum-lines
1264 (> rcirc-buffer-maximum-lines
0)
1265 (= (forward-line (- rcirc-buffer-maximum-lines
)) 0))
1266 (delete-region (point-min) (point))))
1268 ;; set the window point for buffers show in windows
1269 (walk-windows (lambda (w)
1270 (when (and (not (eq (selected-window) w
))
1271 (eq (current-buffer)
1273 (>= (window-point w
)
1274 rcirc-prompt-end-marker
))
1275 (set-window-point w
(point-max))))
1278 ;; restore the point
1279 (goto-char (if moving rcirc-prompt-end-marker old-point
))
1281 ;; keep window on bottom line if it was already there
1282 (when rcirc-scroll-show-maximum-output
1283 (walk-windows (lambda (w)
1284 (when (eq (window-buffer w
) (current-buffer))
1285 (with-current-buffer (window-buffer w
)
1286 (when (eq major-mode
'rcirc-mode
)
1287 (with-selected-window w
1288 (when (<= (- (window-height)
1297 ;; flush undo (can we do something smarter here?)
1298 (buffer-disable-undo)
1299 (buffer-enable-undo))
1301 ;; record modeline activity
1303 (not rcirc-ignore-buffer-activity-flag
)
1304 (not (and rcirc-dim-nicks sender
1305 (string-match (regexp-opt rcirc-dim-nicks
) sender
))))
1306 (rcirc-record-activity (current-buffer)
1307 (when (not (rcirc-channel-p rcirc-target
))
1310 (sit-for 0) ; displayed text before hook
1311 (run-hook-with-args 'rcirc-print-hooks
1312 process sender response target text
)))))
1314 (defun rcirc-startup-channels (server)
1315 "Return the list of startup channels for SERVER."
1317 (dolist (i rcirc-startup-channels-alist
)
1318 (if (string-match (car i
) server
)
1319 (setq channels
(append channels
(cdr i
)))))
1322 (defun rcirc-join-channels (process channels
)
1324 (save-window-excursion
1325 (dolist (channel channels
)
1326 (with-rcirc-process-buffer process
1327 (rcirc-cmd-join channel process
)))))
1330 (defvar rcirc-nick-prefix-chars
"~&@%+")
1331 (defun rcirc-user-nick (user)
1332 "Return the nick from USER. Remove any non-nick junk."
1334 (if (string-match (concat "^[" rcirc-nick-prefix-chars
1335 "]?\\([^! ]+\\)!?") (or user
""))
1336 (match-string 1 user
)
1339 (defun rcirc-nick-channels (process nick
)
1340 "Return list of channels for NICK."
1341 (with-rcirc-process-buffer process
1342 (mapcar (lambda (x) (car x
))
1343 (gethash nick rcirc-nick-table
))))
1345 (defun rcirc-put-nick-channel (process nick channel
)
1346 "Add CHANNEL to list associated with NICK."
1347 (let ((nick (rcirc-user-nick nick
)))
1348 (with-rcirc-process-buffer process
1349 (let* ((chans (gethash nick rcirc-nick-table
))
1350 (record (assoc-string channel chans t
)))
1352 (setcdr record
(current-time))
1353 (puthash nick
(cons (cons channel
(current-time))
1355 rcirc-nick-table
))))))
1357 (defun rcirc-nick-remove (process nick
)
1358 "Remove NICK from table."
1359 (with-rcirc-process-buffer process
1360 (remhash nick rcirc-nick-table
)))
1362 (defun rcirc-remove-nick-channel (process nick channel
)
1363 "Remove the CHANNEL from list associated with NICK."
1364 (with-rcirc-process-buffer process
1365 (let* ((chans (gethash nick rcirc-nick-table
))
1367 ;; instead of assoc-string-delete-all:
1368 (let ((record (assoc-string channel chans t
)))
1370 (setcar record
'delete
)
1371 (assq-delete-all 'delete chans
)))))
1373 (puthash nick newchans rcirc-nick-table
)
1374 (remhash nick rcirc-nick-table
)))))
1376 (defun rcirc-channel-nicks (process target
)
1377 "Return the list of nicks associated with TARGET sorted by last activity."
1379 (if (rcirc-channel-p target
)
1380 (with-rcirc-process-buffer process
1384 (let ((record (assoc-string target v t
)))
1386 (setq nicks
(cons (cons k
(cdr record
)) nicks
)))))
1388 (mapcar (lambda (x) (car x
))
1389 (sort nicks
(lambda (x y
) (time-less-p (cdr y
) (cdr x
)))))))
1392 (defun rcirc-ignore-update-automatic (nick)
1393 "Remove NICK from `rcirc-ignore-list'
1394 if NICK is also on `rcirc-ignore-list-automatic'."
1395 (when (member nick rcirc-ignore-list-automatic
)
1396 (setq rcirc-ignore-list-automatic
1397 (delete nick rcirc-ignore-list-automatic
)
1399 (delete nick rcirc-ignore-list
))))
1401 ;;; activity tracking
1402 (defvar rcirc-track-minor-mode-map
(make-sparse-keymap)
1403 "Keymap for rcirc track minor mode.")
1405 (define-key rcirc-track-minor-mode-map
(kbd "C-c `") 'rcirc-next-active-buffer
)
1406 (define-key rcirc-track-minor-mode-map
(kbd "C-c C-@") 'rcirc-next-active-buffer
)
1407 (define-key rcirc-track-minor-mode-map
(kbd "C-c C-SPC") 'rcirc-next-active-buffer
)
1410 (define-minor-mode rcirc-track-minor-mode
1411 "Global minor mode for tracking activity in rcirc buffers."
1414 :keymap rcirc-track-minor-mode-map
1417 (or global-mode-string
(setq global-mode-string
'("")))
1418 ;; toggle the mode-line channel indicator
1419 (if rcirc-track-minor-mode
1421 (and (not (memq 'rcirc-activity-string global-mode-string
))
1422 (setq global-mode-string
1423 (append global-mode-string
'(rcirc-activity-string))))
1424 (add-hook 'window-configuration-change-hook
1425 'rcirc-window-configuration-change
))
1426 (setq global-mode-string
1427 (delete 'rcirc-activity-string global-mode-string
))
1428 (remove-hook 'window-configuration-change-hook
1429 'rcirc-window-configuration-change
)))
1431 (or (assq 'rcirc-ignore-buffer-activity-flag minor-mode-alist
)
1432 (setq minor-mode-alist
1433 (cons '(rcirc-ignore-buffer-activity-flag " Ignore") minor-mode-alist
)))
1434 (or (assq 'rcirc-low-priority-flag minor-mode-alist
)
1435 (setq minor-mode-alist
1436 (cons '(rcirc-low-priority-flag " LowPri") minor-mode-alist
)))
1438 (defun rcirc-toggle-ignore-buffer-activity ()
1439 "Toggle the value of `rcirc-ignore-buffer-activity-flag'."
1441 (setq rcirc-ignore-buffer-activity-flag
1442 (not rcirc-ignore-buffer-activity-flag
))
1443 (message (if rcirc-ignore-buffer-activity-flag
1444 "Ignore activity in this buffer"
1445 "Notice activity in this buffer"))
1446 (force-mode-line-update))
1448 (defun rcirc-toggle-low-priority ()
1449 "Toggle the value of `rcirc-low-priority-flag'."
1451 (setq rcirc-low-priority-flag
1452 (not rcirc-low-priority-flag
))
1453 (message (if rcirc-low-priority-flag
1454 "Activity in this buffer is low priority"
1455 "Activity in this buffer is normal priority"))
1456 (force-mode-line-update))
1458 (defvar rcirc-switch-to-buffer-function
'switch-to-buffer
1459 "Function to use when switching buffers.
1460 Possible values are `switch-to-buffer', `pop-to-buffer', and
1463 (defun rcirc-switch-to-server-buffer ()
1464 "Switch to the server buffer associated with current channel buffer."
1466 (funcall rcirc-switch-to-buffer-function rcirc-server-buffer
))
1468 (defun rcirc-jump-to-first-unread-line ()
1469 "Move the point to the first unread line in this buffer."
1471 (when (marker-position overlay-arrow-position
)
1472 (goto-char overlay-arrow-position
)))
1474 (defvar rcirc-last-non-irc-buffer nil
1475 "The buffer to switch to when there is no more activity.")
1477 (defun rcirc-next-active-buffer (arg)
1478 "Go to the next rcirc buffer with activity.
1479 With prefix ARG, go to the next low priority buffer with activity.
1480 The function given by `rcirc-switch-to-buffer-function' is used to
1483 (let* ((pair (rcirc-split-activity rcirc-activity
))
1486 (if (or (and (not arg
) hipri
)
1489 (unless (eq major-mode
'rcirc-mode
)
1490 (setq rcirc-last-non-irc-buffer
(current-buffer)))
1491 (funcall rcirc-switch-to-buffer-function
1492 (car (if arg lopri hipri
))))
1493 (if (eq major-mode
'rcirc-mode
)
1494 (if (not (and rcirc-last-non-irc-buffer
1495 (buffer-live-p rcirc-last-non-irc-buffer
)))
1496 (message "No IRC activity. Start something.")
1497 (message "No more IRC activity. Go back to work.")
1498 (funcall rcirc-switch-to-buffer-function rcirc-last-non-irc-buffer
)
1499 (setq rcirc-last-non-irc-buffer nil
))
1505 (key-description (this-command-keys))
1506 " for low priority activity."))))))))
1508 (defvar rcirc-activity-hooks nil
1509 "Hook to be run when there is channel activity.
1511 Functions are called with a single argument, the buffer with the
1512 activity. Only run if the buffer is not visible and
1513 `rcirc-ignore-buffer-activity-flag' is non-nil.")
1515 (defun rcirc-record-activity (buffer &optional type
)
1516 "Record BUFFER activity with TYPE."
1517 (with-current-buffer buffer
1518 (when (not (get-buffer-window (current-buffer) t
))
1519 (setq rcirc-activity
1520 (sort (add-to-list 'rcirc-activity
(current-buffer))
1522 (let ((t1 (with-current-buffer b1 rcirc-last-post-time
))
1523 (t2 (with-current-buffer b2 rcirc-last-post-time
)))
1524 (time-less-p t2 t1
)))))
1525 (pushnew type rcirc-activity-types
)
1526 (rcirc-update-activity-string)))
1527 (run-hook-with-args 'rcirc-activity-hooks buffer
))
1529 (defun rcirc-clear-activity (buffer)
1530 "Clear the BUFFER activity."
1531 (setq rcirc-activity
(delete buffer rcirc-activity
))
1532 (with-current-buffer buffer
1533 (setq rcirc-activity-types nil
)))
1535 (defun rcirc-split-activity (activity)
1536 "Return a cons cell with ACTIVITY split into (lopri . hipri)."
1538 (dolist (buf rcirc-activity
)
1539 (with-current-buffer buf
1540 (if (and rcirc-low-priority-flag
1541 (not (member 'nick rcirc-activity-types
)))
1542 (add-to-list 'lopri buf t
)
1543 (add-to-list 'hipri buf t
))))
1544 (cons lopri hipri
)))
1546 ;; TODO: add mouse properties
1547 (defun rcirc-update-activity-string ()
1548 "Update mode-line string."
1549 (let* ((pair (rcirc-split-activity rcirc-activity
))
1552 (setq rcirc-activity-string
1553 (cond ((or hipri lopri
)
1556 (rcirc-activity-string hipri
)
1557 (and hipri lopri
",")
1560 (rcirc-activity-string lopri
)
1564 ((not (null (rcirc-process-list)))
1568 (defun rcirc-activity-string (buffers)
1569 (mapconcat (lambda (b)
1570 (let ((s (substring-no-properties (rcirc-short-buffer-name b
))))
1571 (with-current-buffer b
1572 (dolist (type rcirc-activity-types
)
1573 (rcirc-add-face 0 (length s
)
1575 (nick 'rcirc-track-nick
)
1576 (keyword 'rcirc-track-keyword
))
1581 (defun rcirc-short-buffer-name (buffer)
1582 "Return a short name for BUFFER to use in the modeline indicator."
1583 (with-current-buffer buffer
1584 (or rcirc-short-buffer-name
(buffer-name))))
1586 (defvar rcirc-current-buffer nil
)
1587 (defun rcirc-window-configuration-change ()
1588 "Go through visible windows and remove buffers from activity list.
1589 Also, clear the overlay arrow if the current buffer is now hidden."
1590 (let ((current-now-hidden t
))
1591 (walk-windows (lambda (w)
1592 (let ((buf (window-buffer w
)))
1593 (with-current-buffer buf
1594 (when (eq major-mode
'rcirc-mode
)
1595 (rcirc-clear-activity buf
)))
1596 (when (eq buf rcirc-current-buffer
)
1597 (setq current-now-hidden nil
)))))
1598 ;; add overlay arrow if the buffer isn't displayed
1599 (when (and current-now-hidden
1600 rcirc-current-buffer
1601 (buffer-live-p rcirc-current-buffer
))
1602 (with-current-buffer rcirc-current-buffer
1603 (when (and (eq major-mode
'rcirc-mode
)
1604 (marker-position overlay-arrow-position
))
1605 (set-marker overlay-arrow-position nil
)))))
1607 ;; remove any killed buffers from list
1608 (setq rcirc-activity
1609 (delq nil
(mapcar (lambda (buf) (when (buffer-live-p buf
) buf
))
1611 (rcirc-update-activity-string)
1612 (setq rcirc-current-buffer
(current-buffer)))
1615 ;;; buffer name abbreviation
1616 (defun rcirc-update-short-buffer-names ()
1618 (apply 'append
(mapcar (lambda (process)
1619 (with-rcirc-process-buffer process
1620 rcirc-buffer-alist
))
1621 (rcirc-process-list)))))
1622 (dolist (i (rcirc-abbreviate bufalist
))
1623 (when (buffer-live-p (cdr i
))
1624 (with-current-buffer (cdr i
)
1625 (setq rcirc-short-buffer-name
(car i
)))))))
1627 (defun rcirc-abbreviate (pairs)
1628 (apply 'append
(mapcar 'rcirc-rebuild-tree
(rcirc-make-trees pairs
))))
1630 (defun rcirc-rebuild-tree (tree &optional acc
)
1631 (let ((ch (char-to-string (car tree
))))
1632 (dolist (x (cdr tree
))
1634 (setq acc
(append acc
1636 (cons (concat ch
(car y
))
1638 (rcirc-rebuild-tree x
))))
1639 (setq acc
(cons (cons ch x
) acc
))))
1642 (defun rcirc-make-trees (pairs)
1644 (mapc (lambda (pair)
1646 (let* ((str (car pair
))
1648 (char (unless (zerop (length str
))
1650 (rest (unless (zerop (length str
))
1652 (part (if char
(assq char alist
))))
1654 ;; existing partition
1655 (setcdr part
(cons (cons rest data
) (cdr part
)))
1657 (setq alist
(cons (if char
1658 (list char
(cons rest data
))
1661 (setq alist
(cons pair alist
))))
1663 ;; recurse into cdrs of alist
1665 (when (and (listp x
) (listp (cadr x
)))
1666 (setcdr x
(if (> (length (cdr x
)) 1)
1667 (rcirc-make-trees (cdr x
))
1668 (setcdr x
(list (cdadr x
)))))))
1671 ;;; /commands these are called with 3 args: PROCESS, TARGET, which is
1672 ;; the current buffer/channel/user, and ARGS, which is a string
1673 ;; containing the text following the /cmd.
1675 (defmacro defun-rcirc-command
(command argument docstring interactive-form
1678 `(defun ,(intern (concat "rcirc-cmd-" (symbol-name command
)))
1679 (,@argument
&optional process target
)
1680 ,(concat docstring
"\n\nNote: If PROCESS or TARGET are nil, the values given"
1681 "\nby `rcirc-buffer-process' and `rcirc-target' will be used.")
1683 (let ((process (or process
(rcirc-buffer-process)))
1684 (target (or target rcirc-target
)))
1687 (defun-rcirc-command msg
(message)
1688 "Send private MESSAGE to TARGET."
1692 (setq target
(completing-read "Message nick: "
1693 (with-rcirc-server-buffer
1695 (when (> (length target
) 0)
1696 (setq message
(read-string (format "Message %s: " target
)))
1697 (when (> (length message
) 0)
1698 (rcirc-send-message process target message
))))
1699 (if (not (string-match "\\([^ ]+\\) \\(.+\\)" message
))
1700 (message "Not enough args, or something.")
1701 (setq target
(match-string 1 message
)
1702 message
(match-string 2 message
))
1703 (rcirc-send-message process target message
))))
1705 (defun-rcirc-command query
(nick)
1706 "Open a private chat buffer to NICK."
1707 (interactive (list (completing-read "Query nick: "
1708 (with-rcirc-server-buffer rcirc-nick-table
))))
1709 (let ((existing-buffer (rcirc-get-buffer process nick
)))
1710 (switch-to-buffer (or existing-buffer
1711 (rcirc-get-buffer-create process nick
)))
1712 (when (not existing-buffer
)
1713 (rcirc-cmd-whois nick
))))
1715 (defun-rcirc-command join
(channel)
1717 (interactive "sJoin channel: ")
1718 (let ((buffer (rcirc-get-buffer-create process
1719 (car (split-string channel
)))))
1720 (rcirc-send-string process
(concat "JOIN " channel
))
1721 (when (not (eq (selected-window) (minibuffer-window)))
1722 (funcall rcirc-switch-to-buffer-function buffer
))))
1724 (defun-rcirc-command part
(channel)
1726 (interactive "sPart channel: ")
1727 (let ((channel (if (> (length channel
) 0) channel target
)))
1728 (rcirc-send-string process
(concat "PART " channel
" :" rcirc-id-string
))))
1730 (defun-rcirc-command quit
(reason)
1731 "Send a quit message to server with REASON."
1732 (interactive "sQuit reason: ")
1733 (rcirc-send-string process
(concat "QUIT :"
1734 (if (not (zerop (length reason
)))
1738 (defun-rcirc-command nick
(nick)
1739 "Change nick to NICK."
1742 (setq nick
(read-string "New nick: " (rcirc-nick process
))))
1743 (rcirc-send-string process
(concat "NICK " nick
)))
1745 (defun-rcirc-command names
(channel)
1746 "Display list of names in CHANNEL or in current channel if CHANNEL is nil.
1747 If called interactively, prompt for a channel when prefix arg is supplied."
1751 (setq channel
(read-string "List names in channel: " target
))))
1752 (let ((channel (if (> (length channel
) 0)
1755 (rcirc-send-string process
(concat "NAMES " channel
))))
1757 (defun-rcirc-command topic
(topic)
1758 "List TOPIC for the TARGET channel.
1759 With a prefix arg, prompt for new topic."
1761 (if (and (interactive-p) topic
)
1762 (setq topic
(read-string "New Topic: " rcirc-topic
)))
1763 (rcirc-send-string process
(concat "TOPIC " target
1764 (when (> (length topic
) 0)
1765 (concat " :" topic
)))))
1767 (defun-rcirc-command whois
(nick)
1768 "Request information from server about NICK."
1770 (completing-read "Whois: "
1771 (with-rcirc-server-buffer rcirc-nick-table
))))
1772 (rcirc-send-string process
(concat "WHOIS " nick
)))
1774 (defun-rcirc-command mode
(args)
1775 "Set mode with ARGS."
1776 (interactive (list (concat (read-string "Mode nick or channel: ")
1777 " " (read-string "Mode: "))))
1778 (rcirc-send-string process
(concat "MODE " args
)))
1780 (defun-rcirc-command list
(channels)
1781 "Request information on CHANNELS from server."
1782 (interactive "sList Channels: ")
1783 (rcirc-send-string process
(concat "LIST " channels
)))
1785 (defun-rcirc-command oper
(args)
1786 "Send operator command to server."
1787 (interactive "sOper args: ")
1788 (rcirc-send-string process
(concat "OPER " args
)))
1790 (defun-rcirc-command quote
(message)
1791 "Send MESSAGE literally to server."
1792 (interactive "sServer message: ")
1793 (rcirc-send-string process message
))
1795 (defun-rcirc-command kick
(arg)
1796 "Kick NICK from current channel."
1798 (concat (completing-read "Kick nick: "
1799 (rcirc-channel-nicks
1800 (rcirc-buffer-process)
1802 (read-from-minibuffer "Kick reason: "))))
1803 (let* ((arglist (split-string arg
))
1804 (argstring (concat (car arglist
) " :"
1805 (mapconcat 'identity
(cdr arglist
) " "))))
1806 (rcirc-send-string process
(concat "KICK " target
" " argstring
))))
1808 (defun rcirc-cmd-ctcp (args &optional process target
)
1809 (if (string-match "^\\([^ ]+\\)\\s-+\\(.+\\)$" args
)
1810 (let ((target (match-string 1 args
))
1811 (request (match-string 2 args
)))
1812 (rcirc-send-string process
1813 (format "PRIVMSG %s \C-a%s\C-a"
1814 target
(upcase request
))))
1815 (rcirc-print process
(rcirc-nick process
) "ERROR" nil
1816 "usage: /ctcp NICK REQUEST")))
1818 (defun rcirc-cmd-me (args &optional process target
)
1819 (rcirc-send-string process
(format "PRIVMSG %s :\C-aACTION %s\C-a"
1822 (defun rcirc-add-or-remove (set &optional elt
)
1823 (if (and elt
(not (string= "" elt
)))
1824 (if (member-ignore-case elt set
)
1829 (defun-rcirc-command ignore
(nick)
1830 "Manage the ignore list.
1831 Ignore NICK, unignore NICK if already ignored, or list ignored
1832 nicks when no NICK is given. When listing ignored nicks, the
1833 ones added to the list automatically are marked with an asterisk."
1834 (interactive "sToggle ignoring of nick: ")
1835 (setq rcirc-ignore-list
(rcirc-add-or-remove rcirc-ignore-list nick
))
1836 (rcirc-print process nil
"IGNORE" target
1840 (if (member nick rcirc-ignore-list-automatic
)
1842 rcirc-ignore-list
" ")))
1844 (defun-rcirc-command bright
(nick)
1845 "Manage the bright nick list."
1846 (interactive "sToggle emphasis of nick: ")
1847 (setq rcirc-bright-nicks
(rcirc-add-or-remove rcirc-bright-nicks nick
))
1848 (rcirc-print process nil
"BRIGHT" target
1849 (mapconcat 'identity rcirc-bright-nicks
" ")))
1851 (defun-rcirc-command dim
(nick)
1852 "Manage the dim nick list."
1853 (interactive "sToggle deemphasis of nick: ")
1854 (setq rcirc-dim-nicks
(rcirc-add-or-remove rcirc-dim-nicks nick
))
1855 (rcirc-print process nil
"DIM" target
1856 (mapconcat 'identity rcirc-dim-nicks
" ")))
1858 (defun-rcirc-command keyword
(keyword)
1859 "Manage the keyword list.
1860 Mark KEYWORD, unmark KEYWORD if already marked, or list marked
1861 keywords when no KEYWORD is given."
1862 (interactive "sToggle highlighting of keyword: ")
1863 (setq rcirc-keywords
(rcirc-add-or-remove rcirc-keywords keyword
))
1864 (rcirc-print process nil
"KEYWORD" target
1865 (mapconcat 'identity rcirc-keywords
" ")))
1868 (defun rcirc-add-face (start end name
&optional object
)
1869 "Add face NAME to the face text property of the text from START to END."
1874 (setq prop
(get-text-property pos
'face object
)
1875 next
(next-single-property-change pos
'face object end
))
1876 (unless (member name
(get-text-property pos
'face object
))
1877 (add-text-properties pos next
(list 'face
(cons name prop
)) object
))
1880 (defun rcirc-facify (string face
)
1881 "Return a copy of STRING with FACE property added."
1882 (let ((string (or string
"")))
1883 (rcirc-add-face 0 (length string
) face string
)
1886 (defvar rcirc-url-regexp
1890 (or (and (or "http" "https" "ftp" "file" "gopher" "news"
1891 "telnet" "wais" "mailto")
1894 (1+ (char "-a-zA-Z0-9_."))
1895 (1+ (char "-a-zA-Z0-9_"))
1896 (optional ":" (1+ (char "0-9"))))
1897 (and (1+ (char "-a-zA-Z0-9_."))
1898 (or ".com" ".net" ".org")
1902 (1+ (char "-a-zA-Z0-9_=!?#$\@~`%&*+|\\/:;.,{}[]()"))
1903 (char "-a-zA-Z0-9_=#$\@~`%&*+|\\/:;{}[]()")))))
1904 "Regexp matching URLs. Set to nil to disable URL features in rcirc.")
1906 (defun rcirc-browse-url (&optional arg
)
1907 "Prompt for URL to browse based on URLs in buffer."
1909 (let ((completions (mapcar (lambda (x) (cons x nil
)) rcirc-urls
))
1910 (initial-input (car rcirc-urls
))
1911 (history (cdr rcirc-urls
)))
1912 (browse-url (completing-read "rcirc browse-url: "
1913 completions nil nil initial-input
'history
)
1916 (defun rcirc-browse-url-at-point (point)
1917 "Send URL at point to `browse-url'."
1919 (let ((beg (previous-single-property-change (1+ point
) 'mouse-face
))
1920 (end (next-single-property-change point
'mouse-face
)))
1921 (browse-url (buffer-substring-no-properties beg end
))))
1923 (defun rcirc-browse-url-at-mouse (event)
1924 "Send URL at mouse click to `browse-url'."
1926 (let ((position (event-end event
)))
1927 (with-current-buffer (window-buffer (posn-window position
))
1928 (rcirc-browse-url-at-point (posn-point position
)))))
1931 (defvar rcirc-markup-text-functions
1932 '(rcirc-markup-body-text
1933 rcirc-markup-attributes
1934 rcirc-markup-my-nick
1936 rcirc-markup-keywords
1937 rcirc-markup-bright-nicks
)
1938 "List of functions used to manipulate text before it is printed.
1940 Each function takes three arguments, PROCESS, SENDER, RESPONSE
1941 and CHANNEL-BUFFER. The current buffer is temporary buffer that
1942 contains the text to manipulate. Each function works on the text
1945 (defun rcirc-markup-text (process sender response text
)
1946 "Return TEXT with properties added based on various patterns."
1947 (let ((channel-buffer (current-buffer)))
1950 (goto-char (point-min))
1951 (dolist (fn rcirc-markup-text-functions
)
1953 (funcall fn process sender response channel-buffer
)))
1954 (buffer-substring (point-min) (point-max)))))
1956 (defun rcirc-markup-body-text (process sender response channel-buffer
)
1957 ;; We add the text property `rcirc-text' to identify this as the
1959 (add-text-properties (point-min) (point-max)
1960 (list 'rcirc-text
(buffer-substring-no-properties
1961 (point-min) (point-max)))))
1963 (defun rcirc-markup-attributes (process sender response channel-buffer
)
1964 (while (re-search-forward "\\([\C-b\C-_\C-v]\\).*?\\(\\1\\|\C-o\\)" nil t
)
1965 (rcirc-add-face (match-beginning 0) (match-end 0)
1966 (case (char-after (match-beginning 1))
1969 (?\C-_
'underline
)))
1970 ;; keep the ^O since it could terminate other attributes
1971 (when (not (eq ?\C-o
(char-before (match-end 2))))
1972 (delete-region (match-beginning 2) (match-end 2)))
1973 (delete-region (match-beginning 1) (match-end 1))
1974 (goto-char (1+ (match-beginning 1))))
1975 ;; remove the ^O characters now
1976 (while (re-search-forward "\C-o+" nil t
)
1977 (delete-region (match-beginning 0) (match-end 0))))
1979 (defun rcirc-markup-my-nick (process sender response channel-buffer
)
1980 (with-syntax-table rcirc-nick-syntax-table
1981 (while (re-search-forward (concat "\\b"
1982 (regexp-quote (rcirc-nick process
))
1985 (rcirc-add-face (match-beginning 0) (match-end 0)
1986 'rcirc-nick-in-message
)
1987 (when (string= response
"PRIVMSG")
1988 (rcirc-add-face (point-min) (point-max) 'rcirc-nick-in-message-full-line
)
1989 (rcirc-record-activity channel-buffer
'nick
)))))
1991 (defun rcirc-markup-urls (process sender response channel-buffer
)
1992 (while (re-search-forward rcirc-url-regexp nil t
)
1993 (let ((start (match-beginning 0))
1994 (end (match-end 0)))
1995 (rcirc-add-face start end
'rcirc-url
)
1996 (add-text-properties start end
(list 'mouse-face
'highlight
1997 'keymap rcirc-browse-url-map
))
1999 (let ((url (buffer-substring-no-properties start end
)))
2000 (with-current-buffer channel-buffer
2001 (push url rcirc-urls
))))))
2003 (defun rcirc-markup-keywords (process sender response channel-buffer
)
2004 (let* ((target (with-current-buffer channel-buffer
(or rcirc-target
"")))
2005 (keywords (delq nil
(mapcar (lambda (keyword)
2006 (when (not (string-match keyword target
))
2010 (while (re-search-forward (regexp-opt keywords
'words
) nil t
)
2011 (rcirc-add-face (match-beginning 0) (match-end 0) 'rcirc-keyword
)
2012 (when (and (string= response
"PRIVMSG")
2013 (not (string= sender
(rcirc-nick process
))))
2014 (rcirc-record-activity channel-buffer
'keyword
))))))
2016 (defun rcirc-markup-bright-nicks (process sender response channel-buffer
)
2017 (when (and rcirc-bright-nicks
2018 (string= response
"NAMES"))
2019 (with-syntax-table rcirc-nick-syntax-table
2020 (while (re-search-forward (regexp-opt rcirc-bright-nicks
'words
) nil t
)
2021 (rcirc-add-face (match-beginning 0) (match-end 0)
2022 'rcirc-bright-nick
)))))
2025 ;; these are called with the server PROCESS, the SENDER, which is a
2026 ;; server or a user, depending on the command, the ARGS, which is a
2027 ;; list of strings, and the TEXT, which is the original server text,
2029 (defun rcirc-handler-001 (process sender args text
)
2030 (rcirc-handler-generic process
"001" sender args text
)
2031 ;; set the real server name
2032 (with-rcirc-process-buffer process
2033 (setq rcirc-connecting nil
)
2034 (rcirc-reschedule-timeout process
)
2035 (setq rcirc-server-name sender
)
2036 (setq rcirc-nick
(car args
))
2037 (rcirc-update-prompt)
2038 (when rcirc-auto-authenticate-flag
(rcirc-authenticate))
2039 (rcirc-join-channels process rcirc-startup-channels
)))
2041 (defun rcirc-handler-PRIVMSG (process sender args text
)
2042 (let ((target (if (rcirc-channel-p (car args
))
2045 (message (or (cadr args
) "")))
2046 (if (string-match "^\C-a\\(.*\\)\C-a$" message
)
2047 (rcirc-handler-CTCP process target sender
(match-string 1 message
))
2048 (rcirc-print process sender
"PRIVMSG" target message t
))
2049 ;; update nick timestamp
2050 (if (member target
(rcirc-nick-channels process sender
))
2051 (rcirc-put-nick-channel process sender target
))))
2053 (defun rcirc-handler-NOTICE (process sender args text
)
2054 (let ((target (car args
))
2055 (message (cadr args
)))
2056 (if (string-match "^\C-a\\(.*\\)\C-a$" message
)
2057 (rcirc-handler-CTCP-response process target sender
2058 (match-string 1 message
))
2059 (rcirc-print process sender
"NOTICE"
2060 (cond ((rcirc-channel-p target
)
2062 ;;; -ChanServ- [#gnu] Welcome...
2063 ((string-match "\\[\\(#[^\] ]+\\)\\]" message
)
2064 (match-string 1 message
))
2066 (if (string= sender
(rcirc-server-name process
))
2071 (defun rcirc-handler-WALLOPS (process sender args text
)
2072 (rcirc-print process sender
"WALLOPS" sender
(car args
) t
))
2074 (defun rcirc-handler-JOIN (process sender args text
)
2075 (let ((channel (car args
)))
2076 (rcirc-get-buffer-create process channel
)
2077 (rcirc-print process sender
"JOIN" channel
"")
2079 ;; print in private chat buffer if it exists
2080 (when (rcirc-get-buffer (rcirc-buffer-process) sender
)
2081 (rcirc-print process sender
"JOIN" sender channel
))
2083 (rcirc-put-nick-channel process sender channel
)))
2085 ;; PART and KICK are handled the same way
2086 (defun rcirc-handler-PART-or-KICK (process response channel sender nick args
)
2087 (rcirc-ignore-update-automatic nick
)
2088 (if (not (string= nick
(rcirc-nick process
)))
2089 ;; this is someone else leaving
2090 (rcirc-remove-nick-channel process nick channel
)
2091 ;; this is us leaving
2093 (rcirc-remove-nick-channel process n channel
))
2094 (rcirc-channel-nicks process channel
))
2096 ;; if the buffer is still around, make it inactive
2097 (let ((buffer (rcirc-get-buffer process channel
)))
2099 (with-current-buffer buffer
2100 (setq rcirc-target nil
))))))
2102 (defun rcirc-handler-PART (process sender args text
)
2103 (let* ((channel (car args
))
2104 (reason (cadr args
))
2105 (message (concat channel
" " reason
)))
2106 (rcirc-print process sender
"PART" channel message
)
2107 ;; print in private chat buffer if it exists
2108 (when (rcirc-get-buffer (rcirc-buffer-process) sender
)
2109 (rcirc-print process sender
"PART" sender message
))
2111 (rcirc-handler-PART-or-KICK process
"PART" channel sender sender reason
)))
2113 (defun rcirc-handler-KICK (process sender args text
)
2114 (let* ((channel (car args
))
2116 (reason (caddr args
))
2117 (message (concat nick
" " channel
" " reason
)))
2118 (rcirc-print process sender
"KICK" channel message t
)
2119 ;; print in private chat buffer if it exists
2120 (when (rcirc-get-buffer (rcirc-buffer-process) nick
)
2121 (rcirc-print process sender
"KICK" nick message
))
2123 (rcirc-handler-PART-or-KICK process
"KICK" channel sender nick reason
)))
2125 (defun rcirc-handler-QUIT (process sender args text
)
2126 (rcirc-ignore-update-automatic sender
)
2127 (mapc (lambda (channel)
2128 (rcirc-print process sender
"QUIT" channel
(apply 'concat args
)))
2129 (rcirc-nick-channels process sender
))
2131 ;; print in private chat buffer if it exists
2132 (when (rcirc-get-buffer (rcirc-buffer-process) sender
)
2133 (rcirc-print process sender
"QUIT" sender
(apply 'concat args
)))
2135 (rcirc-nick-remove process sender
))
2137 (defun rcirc-handler-NICK (process sender args text
)
2138 (let* ((old-nick sender
)
2139 (new-nick (car args
))
2140 (channels (rcirc-nick-channels process old-nick
)))
2141 ;; update list of ignored nicks
2142 (rcirc-ignore-update-automatic old-nick
)
2143 (when (member old-nick rcirc-ignore-list
)
2144 (add-to-list 'rcirc-ignore-list new-nick
)
2145 (add-to-list 'rcirc-ignore-list-automatic new-nick
))
2146 ;; print message to nick's channels
2147 (dolist (target channels
)
2148 (rcirc-print process sender
"NICK" target new-nick
))
2149 ;; update private chat buffer, if it exists
2150 (let ((chat-buffer (rcirc-get-buffer process old-nick
)))
2152 (with-current-buffer chat-buffer
2153 (rcirc-print process sender
"NICK" old-nick new-nick
)
2154 (setq rcirc-target new-nick
)
2155 (rename-buffer (rcirc-generate-new-buffer-name process new-nick
)))))
2156 ;; remove old nick and add new one
2157 (with-rcirc-process-buffer process
2158 (let ((v (gethash old-nick rcirc-nick-table
)))
2159 (remhash old-nick rcirc-nick-table
)
2160 (puthash new-nick v rcirc-nick-table
))
2161 ;; if this is our nick...
2162 (when (string= old-nick rcirc-nick
)
2163 (setq rcirc-nick new-nick
)
2164 (rcirc-update-prompt t
)
2166 (when rcirc-auto-authenticate-flag
(rcirc-authenticate))))))
2168 (defun rcirc-handler-PING (process sender args text
)
2169 (rcirc-send-string process
(concat "PONG " (car args
))))
2171 (defun rcirc-handler-PONG (process sender args text
)
2175 (defun rcirc-handler-TOPIC (process sender args text
)
2176 (let ((topic (cadr args
)))
2177 (rcirc-print process sender
"TOPIC" (car args
) topic
)
2178 (with-current-buffer (rcirc-get-buffer process
(car args
))
2179 (setq rcirc-topic topic
))))
2181 (defvar rcirc-nick-away-alist nil
)
2182 (defun rcirc-handler-301 (process sender args text
)
2184 (let* ((nick (cadr args
))
2185 (rec (assoc-string nick rcirc-nick-away-alist
))
2186 (away-message (caddr args
)))
2188 (not (string= (cdr rec
) away-message
)))
2189 ;; away message has changed
2190 (rcirc-handler-generic process
"AWAY" nick
(cdr args
) text
)
2192 (setcdr rec away-message
)
2193 (setq rcirc-nick-away-alist
(cons (cons nick away-message
)
2194 rcirc-nick-away-alist
))))))
2196 (defun rcirc-handler-332 (process sender args text
)
2198 (let ((buffer (or (rcirc-get-buffer process
(cadr args
))
2199 (rcirc-get-temp-buffer-create process
(cadr args
)))))
2200 (with-current-buffer buffer
2201 (setq rcirc-topic
(caddr args
)))))
2203 (defun rcirc-handler-333 (process sender args text
)
2204 "Not in rfc1459.txt"
2205 (let ((buffer (or (rcirc-get-buffer process
(cadr args
))
2206 (rcirc-get-temp-buffer-create process
(cadr args
)))))
2207 (with-current-buffer buffer
2208 (let ((setter (caddr args
))
2209 (time (current-time-string
2211 (string-to-number (cadddr args
))))))
2212 (rcirc-print process sender
"TOPIC" (cadr args
)
2213 (format "%s (%s on %s)" rcirc-topic setter time
))))))
2215 (defun rcirc-handler-477 (process sender args text
)
2217 (rcirc-print process sender
"477" (cadr args
) (caddr args
)))
2219 (defun rcirc-handler-MODE (process sender args text
)
2220 (let ((target (car args
))
2221 (msg (mapconcat 'identity
(cdr args
) " ")))
2222 (rcirc-print process sender
"MODE"
2223 (if (string= target
(rcirc-nick process
))
2228 ;; print in private chat buffers if they exist
2229 (mapc (lambda (nick)
2230 (when (rcirc-get-buffer process nick
)
2231 (rcirc-print process sender
"MODE" nick msg
)))
2234 (defun rcirc-get-temp-buffer-create (process channel
)
2235 "Return a buffer based on PROCESS and CHANNEL."
2236 (let ((tmpnam (concat " " (downcase channel
) "TMP" (process-name process
))))
2237 (get-buffer-create tmpnam
)))
2239 (defun rcirc-handler-353 (process sender args text
)
2241 (let ((channel (caddr args
)))
2242 (mapc (lambda (nick)
2243 (rcirc-put-nick-channel process nick channel
))
2244 (split-string (cadddr args
) " " t
))
2245 (with-current-buffer (rcirc-get-temp-buffer-create process channel
)
2246 (goto-char (point-max))
2247 (insert (car (last args
)) " "))))
2249 (defun rcirc-handler-366 (process sender args text
)
2251 (let* ((channel (cadr args
))
2252 (buffer (rcirc-get-temp-buffer-create process channel
)))
2253 (with-current-buffer buffer
2254 (rcirc-print process sender
"NAMES" channel
2255 (buffer-substring (point-min) (point-max))))
2256 (kill-buffer buffer
)))
2258 (defun rcirc-handler-433 (process sender args text
)
2260 (rcirc-handler-generic process
"433" sender args text
)
2261 (let* ((new-nick (concat (cadr args
) "`")))
2262 (with-rcirc-process-buffer process
2263 (rcirc-cmd-nick new-nick nil process
))))
2265 (defun rcirc-authenticate ()
2266 "Send authentication to process associated with current buffer.
2267 Passwords are stored in `rcirc-authinfo' (which see)."
2269 (with-rcirc-server-buffer
2270 (dolist (i rcirc-authinfo
)
2271 (let ((process (rcirc-buffer-process))
2276 (when (and (string-match server rcirc-server
)
2277 (string-match nick rcirc-nick
))
2278 (cond ((equal method
'nickserv
)
2282 "PRIVMSG nickserv :identify "
2284 ((equal method
'chanserv
)
2288 "PRIVMSG chanserv :identify "
2289 (cadr args
) " " (car args
))))
2290 ((equal method
'bitlbee
)
2293 (concat "PRIVMSG &bitlbee :identify " (car args
))))
2295 (message "No %S authentication method defined"
2298 (defun rcirc-handler-INVITE (process sender args text
)
2299 (rcirc-print process sender
"INVITE" nil
(mapconcat 'identity args
" ") t
))
2301 (defun rcirc-handler-ERROR (process sender args text
)
2302 (rcirc-print process sender
"ERROR" nil
(mapconcat 'identity args
" ")))
2304 (defun rcirc-handler-CTCP (process target sender text
)
2305 (if (string-match "^\\([^ ]+\\) *\\(.*\\)$" text
)
2306 (let* ((request (upcase (match-string 1 text
)))
2307 (args (match-string 2 text
))
2308 (handler (intern-soft (concat "rcirc-handler-ctcp-" request
))))
2309 (if (not (fboundp handler
))
2310 (rcirc-print process sender
"ERROR" target
2311 (format "%s sent unsupported ctcp: %s" sender text
)
2313 (funcall handler process target sender args
)
2314 (if (not (string= request
"ACTION"))
2315 (rcirc-print process sender
"CTCP" target
2316 (format "%s" text
) t
))))))
2318 (defun rcirc-handler-ctcp-VERSION (process target sender args
)
2319 (rcirc-send-string process
2320 (concat "NOTICE " sender
2321 " :\C-aVERSION " rcirc-id-string
2324 (defun rcirc-handler-ctcp-ACTION (process target sender args
)
2325 (rcirc-print process sender
"ACTION" target args t
))
2327 (defun rcirc-handler-ctcp-TIME (process target sender args
)
2328 (rcirc-send-string process
2329 (concat "NOTICE " sender
2330 " :\C-aTIME " (current-time-string) "\C-a")))
2332 (defun rcirc-handler-CTCP-response (process target sender message
)
2333 (rcirc-print process sender
"CTCP" nil message t
))
2335 (defgroup rcirc-faces nil
2340 (defface rcirc-my-nick
; font-lock-function-name-face
2341 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
2342 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
2343 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
2344 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
2345 (((class color
) (min-colors 8)) (:foreground
"blue" :weight bold
))
2346 (t (:inverse-video t
:weight bold
)))
2347 "The face used to highlight my messages."
2348 :group
'rcirc-faces
)
2350 (defface rcirc-other-nick
; font-lock-variable-name-face
2351 '((((class grayscale
) (background light
))
2352 (:foreground
"Gray90" :weight bold
:slant italic
))
2353 (((class grayscale
) (background dark
))
2354 (:foreground
"DimGray" :weight bold
:slant italic
))
2355 (((class color
) (min-colors 88) (background light
)) (:foreground
"DarkGoldenrod"))
2356 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightGoldenrod"))
2357 (((class color
) (min-colors 16) (background light
)) (:foreground
"DarkGoldenrod"))
2358 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightGoldenrod"))
2359 (((class color
) (min-colors 8)) (:foreground
"yellow" :weight light
))
2360 (t (:weight bold
:slant italic
)))
2361 "The face used to highlight other messages."
2362 :group
'rcirc-faces
)
2364 (defface rcirc-bright-nick
2365 '((((class grayscale
) (background light
))
2366 (:foreground
"LightGray" :weight bold
:underline t
))
2367 (((class grayscale
) (background dark
))
2368 (:foreground
"Gray50" :weight bold
:underline t
))
2369 (((class color
) (min-colors 88) (background light
)) (:foreground
"CadetBlue"))
2370 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Aquamarine"))
2371 (((class color
) (min-colors 16) (background light
)) (:foreground
"CadetBlue"))
2372 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Aquamarine"))
2373 (((class color
) (min-colors 8)) (:foreground
"magenta"))
2374 (t (:weight bold
:underline t
)))
2375 "Face used for nicks matched by `rcirc-bright-nicks'."
2376 :group
'rcirc-faces
)
2378 (defface rcirc-dim-nick
2379 '((t :inherit default
))
2380 "Face used for nicks in `rcirc-dim-nicks'."
2381 :group
'rcirc-faces
)
2383 (defface rcirc-server
; font-lock-comment-face
2384 '((((class grayscale
) (background light
))
2385 (:foreground
"DimGray" :weight bold
:slant italic
))
2386 (((class grayscale
) (background dark
))
2387 (:foreground
"LightGray" :weight bold
:slant italic
))
2388 (((class color
) (min-colors 88) (background light
))
2389 (:foreground
"Firebrick"))
2390 (((class color
) (min-colors 88) (background dark
))
2391 (:foreground
"chocolate1"))
2392 (((class color
) (min-colors 16) (background light
))
2393 (:foreground
"red"))
2394 (((class color
) (min-colors 16) (background dark
))
2395 (:foreground
"red1"))
2396 (((class color
) (min-colors 8) (background light
))
2398 (((class color
) (min-colors 8) (background dark
))
2400 (t (:weight bold
:slant italic
)))
2401 "The face used to highlight server messages."
2402 :group
'rcirc-faces
)
2404 (defface rcirc-server-prefix
; font-lock-comment-delimiter-face
2405 '((default :inherit rcirc-server
)
2406 (((class grayscale
)))
2407 (((class color
) (min-colors 16)))
2408 (((class color
) (min-colors 8) (background light
))
2410 (((class color
) (min-colors 8) (background dark
))
2411 :foreground
"red1"))
2412 "The face used to highlight server prefixes."
2413 :group
'rcirc-faces
)
2415 (defface rcirc-timestamp
2416 '((t (:inherit default
)))
2417 "The face used to highlight timestamps."
2418 :group
'rcirc-faces
)
2420 (defface rcirc-nick-in-message
; font-lock-keyword-face
2421 '((((class grayscale
) (background light
)) (:foreground
"LightGray" :weight bold
))
2422 (((class grayscale
) (background dark
)) (:foreground
"DimGray" :weight bold
))
2423 (((class color
) (min-colors 88) (background light
)) (:foreground
"Purple"))
2424 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Cyan1"))
2425 (((class color
) (min-colors 16) (background light
)) (:foreground
"Purple"))
2426 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Cyan"))
2427 (((class color
) (min-colors 8)) (:foreground
"cyan" :weight bold
))
2429 "The face used to highlight instances of your nick within messages."
2430 :group
'rcirc-faces
)
2432 (defface rcirc-nick-in-message-full-line
2434 "The face used emphasize the entire message when your nick is mentioned."
2435 :group
'rcirc-faces
)
2437 (defface rcirc-prompt
; comint-highlight-prompt
2438 '((((min-colors 88) (background dark
)) (:foreground
"cyan1"))
2439 (((background dark
)) (:foreground
"cyan"))
2440 (t (:foreground
"dark blue")))
2441 "The face used to highlight prompts."
2442 :group
'rcirc-faces
)
2444 (defface rcirc-track-nick
2445 '((((type tty
)) (:inherit default
))
2446 (t (:inverse-video t
)))
2447 "The face used in the mode-line when your nick is mentioned."
2448 :group
'rcirc-faces
)
2450 (defface rcirc-track-keyword
2452 "The face used in the mode-line when keywords are mentioned."
2453 :group
'rcirc-faces
)
2457 "The face used to highlight urls."
2458 :group
'rcirc-faces
)
2460 (defface rcirc-keyword
2461 '((t (:inherit highlight
)))
2462 "The face used to highlight keywords."
2463 :group
'rcirc-faces
)
2466 ;; When using M-x flyspell-mode, only check words after the prompt
2467 (put 'rcirc-mode
'flyspell-mode-predicate
'rcirc-looking-at-input
)
2468 (defun rcirc-looking-at-input ()
2469 "Returns true if point is past the input marker."
2470 (>= (point) rcirc-prompt-end-marker
))
2475 ;; arch-tag: b471b7e8-6b5a-4399-b2c6-a3c78dfc8ffb
2476 ;;; rcirc.el ends here