(Emacs/W3): Rename from `w3-mode'. Mention that
[bpt/emacs.git] / lisp / net / rcirc.el
CommitLineData
bd43c990
RS
1;;; rcirc.el --- default, simple IRC client.
2
ceb4c4d3 3;; Copyright (C) 2005, 2006 Free Software Foundation, Inc.
bd43c990
RS
4
5;; Author: Ryan Yeske
bd43c990
RS
6;; URL: http://www.nongnu.org/rcirc
7;; Keywords: comm
8
b71cef5c 9;; This file is part of GNU Emacs.
bd43c990
RS
10
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)
14;; any later version.
15
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.
20
21;; You should have received a copy of the GNU General Public License
b71cef5c
RF
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.
bd43c990
RS
25
26;;; Commentary:
27
adf794e4
EZ
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.
32
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.
bd43c990 39
bd43c990
RS
40;; Open a new irc connection with:
41;; M-x irc RET
42
43;;; Code:
44
45(require 'ring)
46(require 'time-date)
47(eval-when-compile (require 'cl))
48
adf794e4
EZ
49(defgroup rcirc nil
50 "Simple IRC client."
51 :version "22.1"
2fbed782 52 :prefix "rcirc-"
e8f10ddb 53 :link '(custom-manual "(rcirc)")
adf794e4
EZ
54 :group 'applications)
55
56(defcustom rcirc-server "irc.freenode.net"
57 "The default server to connect to."
58 :type 'string
59 :group 'rcirc)
bd43c990 60
adf794e4
EZ
61(defcustom rcirc-port 6667
62 "The default port to connect to."
63 :type 'integer
64 :group 'rcirc)
bd43c990 65
adf794e4
EZ
66(defcustom rcirc-nick (user-login-name)
67 "Your nick."
68 :type 'string
69 :group 'rcirc)
bd43c990 70
adf794e4
EZ
71(defcustom rcirc-user-name (user-login-name)
72 "Your user name sent to the server when connecting."
73 :type 'string
74 :group 'rcirc)
bd43c990 75
adf794e4 76(defcustom rcirc-user-full-name (if (string= (user-full-name) "")
bd43c990
RS
77 rcirc-user-name
78 (user-full-name))
adf794e4
EZ
79 "The full name sent to the server when connecting."
80 :type 'string
81 :group 'rcirc)
bd43c990 82
db58efbf 83(defcustom rcirc-startup-channels-alist '(("^irc.freenode.net$" "#emacs"))
bd43c990 84 "Alist of channels to join at startup.
adf794e4
EZ
85Each element looks like (SERVER-REGEXP . CHANNEL-LIST)."
86 :type '(alist :key-type string :value-type (repeat string))
87 :group 'rcirc)
bd43c990 88
adf794e4
EZ
89(defcustom rcirc-fill-flag t
90 "*Non-nil means line-wrap messages printed in channel buffers."
91 :type 'boolean
92 :group 'rcirc)
bd43c990 93
adf794e4
EZ
94(defcustom rcirc-fill-column nil
95 "*Column beyond which automatic line-wrapping should happen.
2e398771 96If nil, use value of `fill-column'. If 'frame-width, use the
adf794e4
EZ
97maximum frame width."
98 :type '(choice (const :tag "Value of `fill-column'")
99 (const :tag "Full frame width" frame-width)
100 (integer :tag "Number of columns"))
101 :group 'rcirc)
bd43c990 102
adf794e4 103(defcustom rcirc-fill-prefix nil
bd43c990
RS
104 "*Text to insert before filled lines.
105If nil, calculate the prefix dynamically to line up text
adf794e4
EZ
106underneath each nick."
107 :type '(choice (const :tag "Dynamic" nil)
108 (string :tag "Prefix text"))
109 :group 'rcirc)
bd43c990 110
adf794e4
EZ
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)
bd43c990 114
adf794e4 115(defcustom rcirc-time-format "%H:%M "
bd43c990 116 "*Describes how timestamps are printed.
adf794e4
EZ
117Used as the first arg to `format-time-string'."
118 :type 'string
119 :group 'rcirc)
bd43c990 120
adf794e4
EZ
121(defcustom rcirc-input-ring-size 1024
122 "*Size of input history ring."
123 :type 'integer
124 :group 'rcirc)
bd43c990 125
adf794e4 126(defcustom rcirc-read-only-flag t
2e398771 127 "*Non-nil means make text in IRC buffers read-only."
adf794e4
EZ
128 :type 'boolean
129 :group 'rcirc)
bd43c990 130
adf794e4 131(defcustom rcirc-buffer-maximum-lines nil
bd43c990
RS
132 "*The maximum size in lines for rcirc buffers.
133Channel buffers are truncated from the top to be no greater than this
adf794e4
EZ
134number. If zero or nil, no truncating is done."
135 :type '(choice (const :tag "No truncation" nil)
136 (integer :tag "Number of lines"))
137 :group 'rcirc)
bd43c990 138
db58efbf
EZ
139(defcustom rcirc-authinfo nil
140 "List of authentication passwords.
141Each element of the list is a list with a SERVER-REGEXP string
142and a method symbol followed by method specific arguments.
143
144The valid METHOD symbols are `nickserv', `chanserv' and
145`bitlbee'.
bd43c990
RS
146
147The required ARGUMENTS for each METHOD symbol are:
db58efbf
EZ
148 `nickserv': NICK PASSWORD
149 `chanserv': NICK CHANNEL PASSWORD
150 `bitlbee': NICK PASSWORD
bd43c990
RS
151
152Example:
db58efbf
EZ
153 ((\"freenode\" nickserv \"bob\" \"p455w0rd\")
154 (\"freenode\" chanserv \"bob\" \"#bobland\" \"passwd99\")
155 (\"bitlbee\" bitlbee \"robert\" \"sekrit\"))"
156 :type '(alist :key-type (string :tag "Server")
157 :value-type (choice (list :tag "NickServ"
158 (const nickserv)
159 (string :tag "Nick")
160 (string :tag "Password"))
161 (list :tag "ChanServ"
162 (const chanserv)
163 (string :tag "Nick")
164 (string :tag "Channel")
165 (string :tag "Password"))
166 (list :tag "BitlBee"
167 (const bitlbee)
168 (string :tag "Nick")
169 (string :tag "Password"))))
adf794e4 170 :group 'rcirc)
bd43c990 171
db58efbf 172(defcustom rcirc-auto-authenticate-flag t
bd43c990 173 "*Non-nil means automatically send authentication string to server.
db58efbf 174See also `rcirc-authinfo'."
adf794e4
EZ
175 :type 'boolean
176 :group 'rcirc)
bd43c990 177
adf794e4 178(defcustom rcirc-prompt "> "
2e398771 179 "Prompt string to use in IRC buffers.
bd43c990
RS
180
181The following replacements are made:
182%n is your nick.
183%s is the server.
184%t is the buffer target, a channel or a user.
185
adf794e4
EZ
186Setting this alone will not affect the prompt;
187use either M-x customize or also call `rcirc-update-prompt'."
188 :type 'string
189 :set 'rcirc-set-changed
190 :initialize 'custom-initialize-default
191 :group 'rcirc)
192
2c8abe90
AS
193(defcustom rcirc-ignore-list ()
194 "List of ignored nicks.
195Use /ignore to list them, use /ignore NICK to add or remove a nick."
196 :type '(repeat string)
197 :group 'rcirc)
198
199(defvar rcirc-ignore-list-automatic ()
200 "List of ignored nicks added to `rcirc-ignore-list' because of renaming.
201When an ignored person renames, their nick is added to both lists.
202Nicks will be removed from the automatic list on follow-up renamings or
203parts.")
204
adf794e4
EZ
205(defcustom rcirc-print-hooks nil
206 "Hook run after text is printed.
207Called with 5 arguments, PROCESS, SENDER, RESPONSE, TARGET and TEXT."
208 :type 'hook
209 :group 'rcirc)
bd43c990 210
db58efbf
EZ
211(defcustom rcirc-always-use-server-buffer-flag nil
212 "Non-nil means messages without a channel target will go to the server buffer."
213 :type 'boolean
214 :group 'rcirc)
215
bd43c990
RS
216(defvar rcirc-prompt-start-marker nil)
217(defvar rcirc-prompt-end-marker nil)
218
219(defvar rcirc-nick-table nil)
220
2c8abe90
AS
221(defvar rcirc-nick-syntax-table
222 (let ((table (make-syntax-table text-mode-syntax-table)))
223 (mapc (lambda (c) (modify-syntax-entry c "w" table))
224 "[]\\`_^{|}-")
225 (modify-syntax-entry ?' "_" table)
226 table)
227 "Syntax table which includes all nick characters as word constituents.")
228
adf794e4
EZ
229;; each process has an alist of (target . buffer) pairs
230(defvar rcirc-buffer-alist nil)
231
bd43c990
RS
232(defvar rcirc-activity nil
233 "List of channels with unviewed activity.")
234
235(defvar rcirc-activity-string ""
236 "String displayed in modeline representing `rcirc-activity'.")
237(put 'rcirc-activity-string 'risky-local-variable t)
238
239(defvar rcirc-process nil
240 "The server process associated with this buffer.")
241
242(defvar rcirc-target nil
243 "The channel or user associated with this buffer.")
244
bd43c990
RS
245(defvar rcirc-urls nil
246 "List of urls seen in the current buffer.")
247
248(defvar rcirc-keepalive-seconds 60
249 "Number of seconds between keepalive pings.")
250
adf794e4 251(defconst rcirc-id-string (concat "rcirc on GNU Emacs " emacs-version))
bd43c990 252\f
bd43c990
RS
253(defvar rcirc-startup-channels nil)
254;;;###autoload
db58efbf 255(defun rcirc (arg)
bd43c990 256 "Connect to IRC.
db58efbf
EZ
257If ARG is non-nil, prompt for a server to connect to."
258 (interactive "P")
259 (if arg
260 (let* ((server (read-string "IRC Server: " rcirc-server))
261 (port (read-string "IRC Port: " (number-to-string rcirc-port)))
262 (nick (read-string "IRC Nick: " rcirc-nick))
263 (channels (split-string
264 (read-string "IRC Channels: "
265 (mapconcat 'identity
266 (rcirc-startup-channels server)
267 " "))
268 "[, ]+" t)))
269 (rcirc-connect server port nick rcirc-user-name rcirc-user-full-name
270 channels))
271 ;; make new connection using defaults unless already connected to
272 ;; the default rcirc-server
273 (let ((default-server (default-value 'rcirc-server))
274 connected)
275 (dolist (p (rcirc-process-list))
276 (when (string= default-server (process-name p))
277 (setq connected p)))
278 (if (not connected)
279 (rcirc-connect rcirc-server rcirc-port rcirc-nick
280 rcirc-user-name rcirc-user-full-name
281 (rcirc-startup-channels rcirc-server))
282 (switch-to-buffer (process-buffer connected))
283 (message "Connected to %s" rcirc-server)))))
bd43c990
RS
284
285;;;###autoload
286(defalias 'irc 'rcirc)
287
288\f
289(defvar rcirc-process-output nil)
bd43c990
RS
290(defvar rcirc-topic nil)
291(defvar rcirc-keepalive-timer nil)
ad8121fe 292(defvar rcirc-last-server-message-time nil)
2fbed782 293(defun rcirc-connect (&optional server port nick user-name full-name startup-channels)
db58efbf
EZ
294 (add-hook 'window-configuration-change-hook
295 'rcirc-window-configuration-change)
bd43c990 296
bd43c990
RS
297 (save-excursion
298 (message "Connecting to %s..." server)
299 (let* ((inhibit-eol-conversion)
2fbed782
EZ
300 (port-number (if port
301 (if (stringp port)
302 (string-to-number port)
303 port)
304 rcirc-port))
305 (server (or server rcirc-server))
306 (nick (or nick rcirc-nick))
307 (user-name (or user-name rcirc-user-name))
308 (full-name (or full-name rcirc-user-full-name))
309 (startup-channels (or startup-channels (rcirc-startup-channels server)))
bd43c990
RS
310 (process (open-network-stream server nil server port-number)))
311 ;; set up process
312 (set-process-coding-system process 'raw-text 'raw-text)
313 (set-process-filter process 'rcirc-filter)
adf794e4 314 (switch-to-buffer (rcirc-generate-new-buffer-name process nil))
bd43c990
RS
315 (set-process-buffer process (current-buffer))
316 (set-process-sentinel process 'rcirc-sentinel)
317 (rcirc-mode process nil)
adf794e4
EZ
318 (make-local-variable 'rcirc-buffer-alist)
319 (setq rcirc-buffer-alist nil)
bd43c990
RS
320 (make-local-variable 'rcirc-nick-table)
321 (setq rcirc-nick-table (make-hash-table :test 'equal))
322 (make-local-variable 'rcirc-server)
323 (setq rcirc-server server)
324 (make-local-variable 'rcirc-nick)
325 (setq rcirc-nick nick)
326 (make-local-variable 'rcirc-process-output)
327 (setq rcirc-process-output nil)
bd43c990
RS
328 (make-local-variable 'rcirc-startup-channels)
329 (setq rcirc-startup-channels startup-channels)
ad8121fe
EZ
330 (make-local-variable 'rcirc-last-server-message-time)
331 (setq rcirc-last-server-message-time (current-time))
bd43c990
RS
332
333 ;; identify
334 (rcirc-send-string process (concat "NICK " nick))
335 (rcirc-send-string process (concat "USER " user-name
336 " hostname servername :"
337 full-name))
338
339 ;; setup ping timer if necessary
340 (unless rcirc-keepalive-timer
341 (setq rcirc-keepalive-timer
342 (run-at-time 0 rcirc-keepalive-seconds 'rcirc-keepalive)))
343
344 (message "Connecting to %s...done" server)
345
346 ;; return process object
347 process)))
348
adf794e4
EZ
349(defmacro with-rcirc-process-buffer (process &rest body)
350 (declare (indent 1) (debug t))
351 `(with-current-buffer (process-buffer ,process)
352 ,@body))
353
bd43c990 354(defun rcirc-keepalive ()
ad8121fe
EZ
355 "Send keep alive pings to active rcirc processes.
356Kill processes that have not received a server message since the
357last ping."
bd43c990
RS
358 (if (rcirc-process-list)
359 (mapc (lambda (process)
adf794e4 360 (with-rcirc-process-buffer process
ad8121fe
EZ
361 (if (> (cadr (time-since rcirc-last-server-message-time))
362 rcirc-keepalive-seconds)
363 (kill-process process)
364 (rcirc-send-string process (concat "PING " rcirc-server)))))
bd43c990
RS
365 (rcirc-process-list))
366 (cancel-timer rcirc-keepalive-timer)
367 (setq rcirc-keepalive-timer nil)))
368
adf794e4
EZ
369(defvar rcirc-debug-buffer " *rcirc debug*")
370(defvar rcirc-debug-flag nil
371 "If non-nil, write information to `rcirc-debug-buffer'.")
372(defun rcirc-debug (process text)
bd43c990 373 "Add an entry to the debug log including PROCESS and TEXT.
2e398771 374Debug text is written to `rcirc-debug-buffer' if `rcirc-debug-flag'
adf794e4
EZ
375is non-nil."
376 (when rcirc-debug-flag
bd43c990
RS
377 (save-excursion
378 (save-window-excursion
adf794e4 379 (set-buffer (get-buffer-create rcirc-debug-buffer))
bd43c990
RS
380 (goto-char (point-max))
381 (insert (concat
382 "["
383 (format-time-string "%Y-%m-%dT%T ") (process-name process)
384 "] "
385 text))))))
adf794e4 386
bd43c990
RS
387(defvar rcirc-sentinel-hooks nil
388 "Hook functions called when the process sentinel is called.
389Functions are called with PROCESS and SENTINEL arguments.")
390
391(defun rcirc-sentinel (process sentinel)
392 "Called when PROCESS receives SENTINEL."
393 (let ((sentinel (replace-regexp-in-string "\n" "" sentinel)))
adf794e4
EZ
394 (rcirc-debug process (format "SENTINEL: %S %S\n" process sentinel))
395 (with-rcirc-process-buffer process
396 (dolist (buffer (cons nil (mapcar 'cdr rcirc-buffer-alist)))
adf794e4 397 (with-current-buffer (or buffer (current-buffer))
db58efbf
EZ
398 (rcirc-print process "rcirc.el" "ERROR" rcirc-target
399 (format "%s: %s (%S)"
400 (process-name process)
401 sentinel
402 (process-status process)) t)
403 ;; remove the prompt from buffers
bd43c990
RS
404 (let ((inhibit-read-only t))
405 (delete-region rcirc-prompt-start-marker
406 rcirc-prompt-end-marker)))))
407 (run-hook-with-args 'rcirc-sentinel-hooks process sentinel)))
408
409(defun rcirc-process-list ()
410 "Return a list of rcirc processes."
411 (let (ps)
412 (mapc (lambda (p)
413 (when (process-buffer p)
adf794e4 414 (with-rcirc-process-buffer p
bd43c990
RS
415 (when (eq major-mode 'rcirc-mode)
416 (setq ps (cons p ps))))))
417 (process-list))
418 ps))
419
420(defvar rcirc-receive-message-hooks nil
2e398771
JB
421 "Hook functions run when a message is received from server.
422Function is called with PROCESS, COMMAND, SENDER, ARGS and LINE.")
bd43c990
RS
423(defun rcirc-filter (process output)
424 "Called when PROCESS receives OUTPUT."
adf794e4
EZ
425 (rcirc-debug process output)
426 (with-rcirc-process-buffer process
ad8121fe 427 (setq rcirc-last-server-message-time (current-time))
bd43c990
RS
428 (setq rcirc-process-output (concat rcirc-process-output output))
429 (when (= (aref rcirc-process-output
430 (1- (length rcirc-process-output))) ?\n)
431 (mapc (lambda (line)
432 (rcirc-process-server-response process line))
adf794e4 433 (split-string rcirc-process-output "[\n\r]" t))
bd43c990
RS
434 (setq rcirc-process-output nil))))
435
adf794e4 436(defvar rcirc-trap-errors-flag t)
bd43c990 437(defun rcirc-process-server-response (process text)
adf794e4 438 (if rcirc-trap-errors-flag
bd43c990
RS
439 (condition-case err
440 (rcirc-process-server-response-1 process text)
441 (error
442 (rcirc-print process "RCIRC" "ERROR" nil
adf794e4 443 (format "\"%s\" %s" text err) t)))
bd43c990
RS
444 (rcirc-process-server-response-1 process text)))
445
446(defun rcirc-process-server-response-1 (process text)
447 (if (string-match "^\\(:\\([^ ]+\\) \\)?\\([^ ]+\\) \\(.+\\)$" text)
db58efbf
EZ
448 (let* ((user (match-string 2 text))
449 (sender (rcirc-user-nick user))
bd43c990
RS
450 (cmd (match-string 3 text))
451 (args (match-string 4 text))
452 (handler (intern-soft (concat "rcirc-handler-" cmd))))
453 (string-match "^\\([^:]*\\):?\\(.+\\)?$" args)
454 (let* ((args1 (match-string 1 args))
455 (args2 (match-string 2 args))
adf794e4
EZ
456 (args (delq nil (append (split-string args1 " " t)
457 (list args2)))))
bd43c990
RS
458 (if (not (fboundp handler))
459 (rcirc-handler-generic process cmd sender args text)
460 (funcall handler process sender args text))
461 (run-hook-with-args 'rcirc-receive-message-hooks
462 process cmd sender args text)))
463 (message "UNHANDLED: %s" text)))
464
465(defun rcirc-handler-generic (process command sender args text)
466 "Generic server response handler."
467 (rcirc-print process sender command nil
adf794e4 468 (mapconcat 'identity (cdr args) " ") t))
bd43c990
RS
469
470(defun rcirc-send-string (process string)
471 "Send PROCESS a STRING plus a newline."
472 (let ((string (concat (encode-coding-string string
473 buffer-file-coding-system)
474 "\n")))
53f831f3
AS
475 (unless (eq (process-status rcirc-process) 'open)
476 (error "Network connection to %s is not open"
477 (process-name rcirc-process)))
adf794e4 478 (rcirc-debug process string)
bd43c990
RS
479 (process-send-string process string)))
480
481(defun rcirc-server (process)
482 "Return PROCESS server, given by the 001 response."
adf794e4 483 (with-rcirc-process-buffer process
bd43c990
RS
484 rcirc-server))
485
486(defun rcirc-nick (process)
487 "Return PROCESS nick."
adf794e4 488 (with-rcirc-process-buffer process
bd43c990
RS
489 rcirc-nick))
490
491(defvar rcirc-max-message-length 450
492 "Messages longer than this value will be split.")
493
494(defun rcirc-send-message (process target message &optional noticep)
495 "Send TARGET associated with PROCESS a privmsg with text MESSAGE.
496If NOTICEP is non-nil, send a notice instead of privmsg."
497 ;; max message length is 512 including CRLF
498 (let* ((response (if noticep "NOTICE" "PRIVMSG"))
499 (oversize (> (length message) rcirc-max-message-length))
500 (text (if oversize
501 (substring message 0 rcirc-max-message-length)
502 message))
503 (text (if (string= text "")
504 " "
505 text))
506 (more (if oversize
507 (substring message rcirc-max-message-length))))
db58efbf
EZ
508 (rcirc-get-buffer-create process target)
509 (rcirc-print process (rcirc-nick process) response target text)
bd43c990 510 (rcirc-send-string process (concat response " " target " :" text))
db58efbf 511 (when more (rcirc-send-message process target more noticep))))
bd43c990
RS
512
513(defvar rcirc-input-ring nil)
514(defvar rcirc-input-ring-index 0)
515(defun rcirc-prev-input-string (arg)
516 (ring-ref rcirc-input-ring (+ rcirc-input-ring-index arg)))
517
518(defun rcirc-insert-prev-input (arg)
519 (interactive "p")
520 (when (<= rcirc-prompt-end-marker (point))
521 (delete-region rcirc-prompt-end-marker (point-max))
522 (insert (rcirc-prev-input-string 0))
523 (setq rcirc-input-ring-index (1+ rcirc-input-ring-index))))
524
525(defun rcirc-insert-next-input (arg)
526 (interactive "p")
527 (when (<= rcirc-prompt-end-marker (point))
528 (delete-region rcirc-prompt-end-marker (point-max))
529 (setq rcirc-input-ring-index (1- rcirc-input-ring-index))
530 (insert (rcirc-prev-input-string -1))))
531
532(defvar rcirc-nick-completions nil)
533(defvar rcirc-nick-completion-start-offset nil)
534(defun rcirc-complete-nick ()
535 "Cycle through nick completions from list of nicks in channel."
536 (interactive)
537 (if (eq last-command 'rcirc-complete-nick)
538 (setq rcirc-nick-completions
539 (append (cdr rcirc-nick-completions)
540 (list (car rcirc-nick-completions))))
541 (setq rcirc-nick-completion-start-offset
542 (- (save-excursion
543 (if (re-search-backward " " rcirc-prompt-end-marker t)
544 (1+ (point))
545 rcirc-prompt-end-marker))
546 rcirc-prompt-end-marker))
547 (setq rcirc-nick-completions
548 (let ((completion-ignore-case t))
adf794e4
EZ
549 (all-completions
550 (buffer-substring
bd43c990
RS
551 (+ rcirc-prompt-end-marker
552 rcirc-nick-completion-start-offset)
553 (point))
554 (mapcar (lambda (x) (cons x nil))
555 (rcirc-channel-nicks rcirc-process
556 (rcirc-buffer-target)))))))
557 (let ((completion (car rcirc-nick-completions)))
558 (when completion
adf794e4 559 (delete-region (+ rcirc-prompt-end-marker
bd43c990
RS
560 rcirc-nick-completion-start-offset)
561 (point))
562 (insert (concat completion
adf794e4 563 (if (= (+ rcirc-prompt-end-marker
bd43c990
RS
564 rcirc-nick-completion-start-offset)
565 rcirc-prompt-end-marker)
566 ": "))))))
567
568(defun rcirc-buffer-target (&optional buffer)
569 "Return the name of target for BUFFER.
570If buffer is nil, return the target of the current buffer."
571 (with-current-buffer (or buffer (current-buffer))
572 rcirc-target))
573
574(defvar rcirc-mode-map (make-sparse-keymap)
575 "Keymap for rcirc mode.")
576
577(define-key rcirc-mode-map (kbd "RET") 'rcirc-send-input)
578(define-key rcirc-mode-map (kbd "M-p") 'rcirc-insert-prev-input)
579(define-key rcirc-mode-map (kbd "M-n") 'rcirc-insert-next-input)
580(define-key rcirc-mode-map (kbd "TAB") 'rcirc-complete-nick)
581(define-key rcirc-mode-map (kbd "C-c C-b") 'rcirc-browse-url)
582(define-key rcirc-mode-map (kbd "C-c C-c") 'rcirc-edit-multiline)
583(define-key rcirc-mode-map (kbd "C-c C-j") 'rcirc-cmd-join)
584(define-key rcirc-mode-map (kbd "C-c C-k") 'rcirc-cmd-kick)
585(define-key rcirc-mode-map (kbd "C-c C-l") 'rcirc-cmd-list)
586(define-key rcirc-mode-map (kbd "C-c C-d") 'rcirc-cmd-mode)
587(define-key rcirc-mode-map (kbd "C-c C-m") 'rcirc-cmd-msg)
588(define-key rcirc-mode-map (kbd "C-c C-r") 'rcirc-cmd-nick) ; rename
589(define-key rcirc-mode-map (kbd "C-c C-o") 'rcirc-cmd-oper)
590(define-key rcirc-mode-map (kbd "C-c C-p") 'rcirc-cmd-part)
591(define-key rcirc-mode-map (kbd "C-c C-q") 'rcirc-cmd-query)
592(define-key rcirc-mode-map (kbd "C-c C-t") 'rcirc-cmd-topic)
593(define-key rcirc-mode-map (kbd "C-c C-n") 'rcirc-cmd-names)
594(define-key rcirc-mode-map (kbd "C-c C-w") 'rcirc-cmd-whois)
595(define-key rcirc-mode-map (kbd "C-c C-x") 'rcirc-cmd-quit)
596(define-key rcirc-mode-map (kbd "C-c TAB") ; C-i
adf794e4 597 'rcirc-toggle-ignore-buffer-activity)
bd43c990
RS
598(define-key rcirc-mode-map (kbd "C-c C-s") 'rcirc-switch-to-server-buffer)
599(define-key rcirc-mode-map (kbd "C-c C-a") 'rcirc-jump-to-first-unread-line)
600
adf794e4 601(defvar rcirc-browse-url-map (make-sparse-keymap)
2e398771 602 "Keymap used for browsing URLs in `rcirc-mode'.")
adf794e4
EZ
603
604(define-key rcirc-browse-url-map (kbd "RET") 'rcirc-browse-url-at-point)
605(define-key rcirc-browse-url-map (kbd "<mouse-2>") 'rcirc-browse-url-at-mouse)
606
607(defvar rcirc-short-buffer-name nil
608 "Generated abbreviation to use to indicate buffer activity.")
609
bd43c990
RS
610(defvar rcirc-mode-hook nil
611 "Hook run when setting up rcirc buffer.")
612
613(defun rcirc-mode (process target)
2e398771 614 "Major mode for IRC channel buffers.
bd43c990
RS
615
616\\{rcirc-mode-map}"
617 (kill-all-local-variables)
618 (use-local-map rcirc-mode-map)
619 (setq mode-name "rcirc")
620 (setq major-mode 'rcirc-mode)
621
622 (make-local-variable 'rcirc-input-ring)
623 (setq rcirc-input-ring (make-ring rcirc-input-ring-size))
624 (make-local-variable 'rcirc-process)
625 (setq rcirc-process process)
626 (make-local-variable 'rcirc-target)
627 (setq rcirc-target target)
ad8121fe
EZ
628 (make-local-variable 'rcirc-topic)
629 (setq rcirc-topic nil)
adf794e4
EZ
630
631 (make-local-variable 'rcirc-short-buffer-name)
632 (setq rcirc-short-buffer-name nil)
bd43c990
RS
633 (make-local-variable 'rcirc-urls)
634 (setq rcirc-urls nil)
635 (setq use-hard-newlines t)
bd43c990
RS
636
637 ;; setup the prompt and markers
638 (make-local-variable 'rcirc-prompt-start-marker)
639 (setq rcirc-prompt-start-marker (make-marker))
640 (set-marker rcirc-prompt-start-marker (point-max))
641 (make-local-variable 'rcirc-prompt-end-marker)
642 (setq rcirc-prompt-end-marker (make-marker))
643 (set-marker rcirc-prompt-end-marker (point-max))
644 (rcirc-update-prompt)
645 (goto-char rcirc-prompt-end-marker)
646 (make-local-variable 'overlay-arrow-position)
647 (setq overlay-arrow-position (make-marker))
648 (set-marker overlay-arrow-position nil)
649
adf794e4
EZ
650 ;; add to buffer list, and update buffer abbrevs
651 (when target ; skip server buffer
652 (let ((buffer (current-buffer)))
653 (with-rcirc-process-buffer process
654 (setq rcirc-buffer-alist (cons (cons target buffer)
655 rcirc-buffer-alist))))
656 (rcirc-update-short-buffer-names))
657
bd43c990
RS
658 (run-hooks 'rcirc-mode-hook))
659
adf794e4
EZ
660(defun rcirc-update-prompt (&optional all)
661 "Reset the prompt string in the current buffer.
c18a54de 662
adf794e4
EZ
663If ALL is non-nil, update prompts in all IRC buffers."
664 (if all
665 (mapc (lambda (process)
666 (mapc (lambda (buffer)
667 (with-current-buffer buffer
668 (rcirc-update-prompt)))
669 (with-rcirc-process-buffer process
670 (mapcar 'cdr rcirc-buffer-alist))))
671 (rcirc-process-list))
672 (let ((inhibit-read-only t)
673 (prompt (or rcirc-prompt "")))
674 (mapc (lambda (rep)
675 (setq prompt
676 (replace-regexp-in-string (car rep) (regexp-quote (cdr rep)) prompt)))
677 (list (cons "%n" (with-rcirc-process-buffer rcirc-process
678 rcirc-nick))
679 (cons "%s" (with-rcirc-process-buffer rcirc-process
680 rcirc-server))
681 (cons "%t" (or rcirc-target ""))))
682 (save-excursion
683 (delete-region rcirc-prompt-start-marker rcirc-prompt-end-marker)
684 (goto-char rcirc-prompt-start-marker)
685 (let ((start (point)))
686 (insert-before-markers prompt)
687 (set-marker rcirc-prompt-start-marker start)
688 (when (not (zerop (- rcirc-prompt-end-marker
689 rcirc-prompt-start-marker)))
690 (add-text-properties rcirc-prompt-start-marker
691 rcirc-prompt-end-marker
692 (list 'face 'rcirc-prompt
693 'read-only t 'field t
694 'front-sticky t 'rear-nonsticky t))))))))
695
696(defun rcirc-set-changed (option value)
697 "Set OPTION to VALUE and do updates after a customization change."
698 (set-default option value)
699 (cond ((eq option 'rcirc-prompt)
700 (rcirc-update-prompt 'all))
701 (t
702 (error "Bad option %s" option))))
bd43c990
RS
703
704(defun rcirc-channel-p (target)
705 "Return t if TARGET is a channel name."
706 (and target
707 (not (zerop (length target)))
708 (or (eq (aref target 0) ?#)
709 (eq (aref target 0) ?&))))
710
711(defun rcirc-kill-buffer-hook ()
712 "Part the channel when killing an rcirc buffer."
713 (when (eq major-mode 'rcirc-mode)
adf794e4
EZ
714 (rcirc-kill-buffer-hook-1)))
715(defun rcirc-kill-buffer-hook-1 ()
716 (let ((buffer (current-buffer)))
717 (rcirc-clear-activity buffer)
bd43c990 718 (when (and rcirc-process
adf794e4
EZ
719 (eq (process-status rcirc-process) 'open))
720 (with-rcirc-process-buffer rcirc-process
721 (setq rcirc-buffer-alist
722 (rassq-delete-all buffer rcirc-buffer-alist)))
723 (rcirc-update-short-buffer-names)
bd43c990 724 (if (rcirc-channel-p rcirc-target)
adf794e4
EZ
725 (rcirc-send-string rcirc-process
726 (concat "PART " rcirc-target
727 " :Killed buffer"))
728 (when rcirc-target
729 (rcirc-remove-nick-channel rcirc-process
730 (rcirc-nick rcirc-process)
731 rcirc-target))))))
732
bd43c990
RS
733(add-hook 'kill-buffer-hook 'rcirc-kill-buffer-hook)
734
adf794e4
EZ
735(defun rcirc-generate-new-buffer-name (process target)
736 "Return a buffer name based on PROCESS and TARGET.
2e398771 737This is used for the initial name given to IRC buffers."
adf794e4
EZ
738 (if target
739 (concat target "@" (process-name process))
740 (concat "*" (process-name process) "*")))
bd43c990 741
adf794e4 742(defun rcirc-get-buffer (process target &optional server)
bd43c990 743 "Return the buffer associated with the PROCESS and TARGET.
adf794e4 744
adf794e4
EZ
745If optional argument SERVER is non-nil, return the server buffer
746if there is no existing buffer for TARGET, otherwise return nil."
747 (with-rcirc-process-buffer process
748 (if (null target)
749 (current-buffer)
750 (let ((buffer (cdr (assoc-string target rcirc-buffer-alist t))))
751 (or buffer (when server (current-buffer)))))))
bd43c990
RS
752
753(defun rcirc-get-buffer-create (process target)
adf794e4
EZ
754 "Return the buffer associated with the PROCESS and TARGET.
755Create the buffer if it doesn't exist."
756 (let ((buffer (rcirc-get-buffer process target)))
db58efbf 757 (if buffer
2fbed782 758 (with-current-buffer buffer
db58efbf 759 (when (not rcirc-target)
2fbed782 760 (setq rcirc-target target))
db58efbf 761 buffer)
adf794e4
EZ
762 ;; create the buffer
763 (with-rcirc-process-buffer process
764 (let ((new-buffer (get-buffer-create
765 (rcirc-generate-new-buffer-name process target))))
766 (with-current-buffer new-buffer
767 (rcirc-mode process target))
768 (rcirc-put-nick-channel process (rcirc-nick process) target)
769 new-buffer)))))
bd43c990
RS
770
771(defun rcirc-send-input ()
772 "Send input to target associated with the current buffer."
773 (interactive)
53f831f3
AS
774 (if (< (point) rcirc-prompt-end-marker)
775 ;; copy the line down to the input area
776 (progn
777 (forward-line 0)
778 (let ((start (if (eq (point) (point-min))
779 (point)
780 (if (get-text-property (1- (point)) 'hard)
781 (point)
782 (previous-single-property-change (point) 'hard))))
783 (end (next-single-property-change (1+ (point)) 'hard)))
784 (goto-char (point-max))
785 (insert (replace-regexp-in-string
786 "\n\\s-+" " "
787 (buffer-substring-no-properties start end)))))
788 ;; process input
789 (goto-char (point-max))
790 (let ((target (rcirc-buffer-target))
791 (start rcirc-prompt-end-marker))
792 (when (not (equal 0 (- (point) start)))
793 ;; delete a trailing newline
794 (when (eq (point) (point-at-bol))
795 (delete-backward-char 1))
796 (let ((input (buffer-substring-no-properties
797 rcirc-prompt-end-marker (point))))
db58efbf
EZ
798 (dolist (line (split-string input "\n"))
799 (rcirc-process-input-line rcirc-process target line))
53f831f3
AS
800 ;; add to input-ring
801 (save-excursion
802 (ring-insert rcirc-input-ring input)
803 (setq rcirc-input-ring-index 0)))))))
bd43c990 804
db58efbf
EZ
805(defun rcirc-process-input-line (process target line)
806 (if (string-match "^/\\([^ ]+\\) ?\\(.*\\)$" line)
807 (rcirc-process-command (match-string 1 line)
808 (match-string 2 line)
809 line)
810 (rcirc-process-message line)))
811
812(defun rcirc-process-message (line)
813 (if (not rcirc-target)
814 (message "Not joined")
815 (delete-region rcirc-prompt-end-marker (point))
816 (rcirc-send-message rcirc-process rcirc-target line)))
817
818(defun rcirc-process-command (command args line)
819 (if (eq (aref command 0) ?/)
820 ;; "//text" will send "/text" as a message
821 (rcirc-process-message (substring line 1))
822 (let* ((fun (intern-soft (concat "rcirc-cmd-" command))))
823 (newline)
824 (with-current-buffer (current-buffer)
825 (delete-region rcirc-prompt-end-marker (point))
826 (if (string= command "me")
827 (rcirc-print rcirc-process (rcirc-nick rcirc-process)
828 "ACTION" rcirc-target args)
829 (rcirc-print rcirc-process (rcirc-nick rcirc-process)
830 "COMMAND" rcirc-target line))
831 (set-marker rcirc-prompt-end-marker (point))
832 (if (fboundp fun)
833 (funcall fun args rcirc-process rcirc-target)
834 (rcirc-send-string rcirc-process
835 (concat command " " args)))))))
836
bd43c990
RS
837(defvar rcirc-parent-buffer nil)
838(defvar rcirc-window-configuration nil)
839(defun rcirc-edit-multiline ()
840 "Move current edit to a dedicated buffer."
841 (interactive)
842 (let ((pos (1+ (- (point) rcirc-prompt-end-marker))))
843 (goto-char (point-max))
844 (let ((text (buffer-substring rcirc-prompt-end-marker (point)))
845 (parent (buffer-name))
846 (process rcirc-process))
847 (delete-region rcirc-prompt-end-marker (point))
848 (setq rcirc-window-configuration (current-window-configuration))
849 (pop-to-buffer (concat "*multiline " parent "*"))
850 (rcirc-multiline-edit-mode)
851 (setq rcirc-parent-buffer parent)
852 (setq rcirc-process process)
853 (insert text)
db58efbf
EZ
854 (and (> pos 0) (goto-char pos))
855 (message "Type C-c C-c to return text to %s, or C-c C-k to cancel" parent))))
bd43c990
RS
856
857(define-derived-mode rcirc-multiline-edit-mode
858 text-mode "rcirc multi"
859 "Major mode for multiline edits
860\\{rcirc-multiline-edit-mode-map}"
861 (make-local-variable 'rcirc-parent-buffer)
862 (make-local-variable 'rcirc-process))
863
864(define-key rcirc-multiline-edit-mode-map
865 (kbd "C-c C-c") 'rcirc-multiline-edit-submit)
866(define-key rcirc-multiline-edit-mode-map
867 (kbd "C-x C-s") 'rcirc-multiline-edit-submit)
868(define-key rcirc-multiline-edit-mode-map
869 (kbd "C-c C-k") 'rcirc-multiline-edit-cancel)
870(define-key rcirc-multiline-edit-mode-map
871 (kbd "ESC ESC ESC") 'rcirc-multiline-edit-cancel)
872
873(defun rcirc-multiline-edit-submit ()
874 "Send the text in buffer back to parent buffer."
875 (interactive)
e8f10ddb 876 (assert (eq major-mode 'rcirc-multiline-edit-mode))
bd43c990 877 (assert rcirc-parent-buffer)
adf794e4 878 (untabify (point-min) (point-max))
bd43c990
RS
879 (let ((text (buffer-substring (point-min) (point-max)))
880 (buffer (current-buffer))
881 (pos (point)))
882 (set-buffer rcirc-parent-buffer)
883 (goto-char (point-max))
884 (insert text)
bd43c990 885 (kill-buffer buffer)
adf794e4
EZ
886 (set-window-configuration rcirc-window-configuration)
887 (goto-char (+ rcirc-prompt-end-marker (1- pos)))))
bd43c990
RS
888
889(defun rcirc-multiline-edit-cancel ()
890 "Cancel the multiline edit."
891 (interactive)
e8f10ddb 892 (assert (eq major-mode 'rcirc-multiline-edit-mode))
bd43c990
RS
893 (kill-buffer (current-buffer))
894 (set-window-configuration rcirc-window-configuration))
895
2fbed782 896(defun rcirc-any-buffer (process)
adf794e4 897 "Return a buffer for PROCESS, either the one selected or the process buffer."
2fbed782
EZ
898 (if rcirc-always-use-server-buffer-flag
899 (process-buffer process)
900 (let ((buffer (window-buffer (selected-window))))
901 (if (and buffer
902 (with-current-buffer buffer
903 (and (eq major-mode 'rcirc-mode)
904 (eq rcirc-process process))))
905 buffer
906 (process-buffer process)))))
bd43c990 907
324e4da7 908(defcustom rcirc-response-formats
2fbed782
EZ
909 '(("PRIVMSG" . "%T<%N> %m")
910 ("NOTICE" . "%T-%N- %m")
911 ("ACTION" . "%T[%N %m]")
324e4da7
MB
912 ("COMMAND" . "%T%m")
913 ("ERROR" . "%T%fw!!! %m")
914 (t . "%T%fp*** %fs%n %r %m"))
915 "An alist of formats used for printing responses.
916The format is looked up using the response-type as a key;
917if no match is found, the default entry (with a key of `t') is used.
918
919The entry's value part should be a string, which is inserted with
920the of the following escape sequences replaced by the described values:
921
922 %m The message text
2fbed782
EZ
923 %n The sender's nick
924 %N The sender's nick (with face `rcirc-my-nick' or `rcirc-other-nick')
324e4da7
MB
925 %r The response-type
926 %T The timestamp (with face `rcirc-timestamp')
927 %t The target
928 %fw Following text uses the face `font-lock-warning-face'
929 %fp Following text uses the face `rcirc-server-prefix'
930 %fs Following text uses the face `rcirc-server'
931 %f[FACE] Following text uses the face FACE
3715419e 932 %f- Following text uses the default face
324e4da7
MB
933 %% A literal `%' character
934"
935 :type '(alist :key-type (choice (string :tag "Type")
936 (const :tag "Default" t))
937 :value-type string)
938 :group 'rcirc)
939
bd43c990 940(defun rcirc-format-response-string (process sender response target text)
324e4da7
MB
941 "Return a nicely-formatted response string, incorporating TEXT
942\(and perhaps other arguments). The specific formatting used
943is found by looking up RESPONSE in `rcirc-response-formats'."
944 (let ((chunks
945 (split-string (or (cdr (assoc response rcirc-response-formats))
946 (cdr (assq t rcirc-response-formats)))
947 "%"))
948 (result "")
949 (face nil)
950 key face-key repl)
951 (when (equal (car chunks) "")
952 (pop chunks))
953 (dolist (chunk chunks)
954 (if (equal chunk "")
955 (setq key ?%)
956 (setq key (aref chunk 0))
957 (setq chunk (substring chunk 1)))
958 (setq repl
959 (cond ((eq key ?%)
3715419e 960 ;; %% -- literal % character
324e4da7 961 "%")
2fbed782
EZ
962 ((or (eq key ?n) (eq key ?N))
963 ;; %n/%N -- nick
964 (let ((nick (concat (if (string= (with-rcirc-process-buffer
965 process rcirc-server)
966 sender)
967 ""
54aba1ee 968 sender)
2fbed782
EZ
969 (and target (concat "," target)))))
970 (rcirc-facify nick
971 (if (eq key ?n)
972 face
973 (if (string= sender (rcirc-nick process))
974 'rcirc-my-nick
975 'rcirc-other-nick)))))
324e4da7 976 ((eq key ?T)
3715419e 977 ;; %T -- timestamp
324e4da7
MB
978 (rcirc-facify
979 (format-time-string rcirc-time-format (current-time))
980 'rcirc-timestamp))
981 ((eq key ?m)
3715419e
MB
982 ;; %m -- message text
983 ;; We add the text property `rcirc-text' to identify this
984 ;; as the body text.
324e4da7
MB
985 (propertize
986 (rcirc-mangle-text process (rcirc-facify text face))
987 'rcirc-text text))
988 ((eq key ?t)
3715419e 989 ;; %t -- target
324e4da7
MB
990 (rcirc-facify (or rcirc-target "") face))
991 ((eq key ?r)
3715419e 992 ;; %r -- response
324e4da7
MB
993 (rcirc-facify response face))
994 ((eq key ?f)
3715419e 995 ;; %f -- change face
324e4da7 996 (setq face-key (aref chunk 0))
3715419e 997 (setq chunk (substring chunk 1))
324e4da7 998 (cond ((eq face-key ?w)
3715419e 999 ;; %fw -- warning face
324e4da7
MB
1000 (setq face 'font-lock-warning-face))
1001 ((eq face-key ?p)
3715419e 1002 ;; %fp -- server-prefix face
324e4da7
MB
1003 (setq face 'rcirc-server-prefix))
1004 ((eq face-key ?s)
3715419e 1005 ;; %fs -- warning face
324e4da7
MB
1006 (setq face 'rcirc-server))
1007 ((eq face-key ?-)
3715419e 1008 ;; %fs -- warning face
324e4da7
MB
1009 (setq face nil))
1010 ((and (eq face-key ?\[)
3715419e 1011 (string-match "^\\([^]]*\\)[]]" chunk)
324e4da7 1012 (facep (match-string 1 chunk)))
3715419e 1013 ;; %f[...] -- named face
324e4da7 1014 (setq face (intern (match-string 1 chunk)))
3715419e
MB
1015 (setq chunk (substring chunk (match-end 0)))))
1016 "")))
324e4da7
MB
1017 (setq result (concat result repl (rcirc-facify chunk face))))
1018 result))
bd43c990 1019
db58efbf
EZ
1020(defun rcirc-target-buffer (process sender response target text)
1021 "Return a buffer to print the server response."
1022 (assert (not (bufferp target)))
1023 (with-rcirc-process-buffer process
1024 (cond ((not target)
2fbed782 1025 (rcirc-any-buffer process))
db58efbf
EZ
1026 ((not (rcirc-channel-p target))
1027 ;; message from another user
1028 (if (string= response "PRIVMSG")
1029 (rcirc-get-buffer-create process (if (string= sender rcirc-nick)
1030 target
1031 sender))
1032 (rcirc-get-buffer process target t)))
1033 ((or (rcirc-get-buffer process target)
2fbed782 1034 (rcirc-any-buffer process))))))
db58efbf 1035
bd43c990
RS
1036(defvar rcirc-activity-type nil)
1037(make-variable-buffer-local 'rcirc-activity-type)
1038(defun rcirc-print (process sender response target text &optional activity)
1039 "Print TEXT in the buffer associated with TARGET.
1040Format based on SENDER and RESPONSE. If ACTIVITY is non-nil,
1041record activity."
db58efbf 1042 (unless (or (member sender rcirc-ignore-list)
2c8abe90
AS
1043 (member (with-syntax-table rcirc-nick-syntax-table
1044 (when (string-match "^\\([^/]\\w*\\)[:,]" text)
1045 (match-string 1 text))) rcirc-ignore-list))
db58efbf 1046 (let* ((buffer (rcirc-target-buffer process sender response target text))
2c8abe90
AS
1047 (inhibit-read-only t))
1048 (with-current-buffer buffer
1049 (let ((moving (= (point) rcirc-prompt-end-marker))
1050 (old-point (point-marker))
1051 (fill-start (marker-position rcirc-prompt-start-marker)))
1052
1053 (unless (string= sender (rcirc-nick process))
1054 ;; only decode text from other senders, not ours
1055 (setq text (decode-coding-string (or text "")
1056 buffer-file-coding-system))
1057 ;; mark the line with overlay arrow
1058 (unless (or (marker-position overlay-arrow-position)
1059 (get-buffer-window (current-buffer)))
1060 (set-marker overlay-arrow-position
1061 (marker-position rcirc-prompt-start-marker))))
1062
1063 ;; temporarily set the marker insertion-type because
1064 ;; insert-before-markers results in hidden text in new buffers
1065 (goto-char rcirc-prompt-start-marker)
1066 (set-marker-insertion-type rcirc-prompt-start-marker t)
1067 (set-marker-insertion-type rcirc-prompt-end-marker t)
324e4da7
MB
1068
1069 (let ((fmted-text
1070 (rcirc-format-response-string process sender response nil
1071 text)))
1072
1073 (insert fmted-text (propertize "\n" 'hard t))
1074 (set-marker-insertion-type rcirc-prompt-start-marker nil)
1075 (set-marker-insertion-type rcirc-prompt-end-marker nil)
1076
2fbed782
EZ
1077 (let ((text-start (make-marker)))
1078 (set-marker text-start
1079 (or (next-single-property-change fill-start
1080 'rcirc-text)
e8f10ddb 1081 rcirc-prompt-end-marker))
2fbed782
EZ
1082 ;; squeeze spaces out of text before rcirc-text
1083 (fill-region fill-start (1- text-start))
1084
1085 ;; fill the text we just inserted, maybe
1086 (when (and rcirc-fill-flag
1087 (not (string= response "372"))) ;/motd
1088 (let ((fill-prefix
1089 (or rcirc-fill-prefix
1090 (make-string (- text-start fill-start) ?\s)))
1091 (fill-column (cond ((eq rcirc-fill-column 'frame-width)
1092 (1- (frame-width)))
1093 (rcirc-fill-column
1094 rcirc-fill-column)
1095 (t fill-column))))
1096 (fill-region fill-start rcirc-prompt-start-marker 'left t)))))
2c8abe90
AS
1097
1098 ;; set inserted text to be read-only
1099 (when rcirc-read-only-flag
1100 (put-text-property rcirc-prompt-start-marker fill-start 'read-only t)
1101 (let ((inhibit-read-only t))
1102 (put-text-property rcirc-prompt-start-marker fill-start
1103 'front-sticky t)
1104 (put-text-property (1- (point)) (point) 'rear-nonsticky t)))
1105
1106 ;; truncate buffer if it is very long
1107 (save-excursion
1108 (when (and rcirc-buffer-maximum-lines
1109 (> rcirc-buffer-maximum-lines 0)
1110 (= (forward-line (- rcirc-buffer-maximum-lines)) 0))
1111 (delete-region (point-min) (point))))
1112
1113 ;; set the window point for buffers show in windows
1114 (walk-windows (lambda (w)
1115 (unless (eq (selected-window) w)
1116 (when (and (eq (current-buffer)
1117 (window-buffer w))
1118 (>= (window-point w)
1119 rcirc-prompt-end-marker))
1120 (set-window-point w (point-max)))))
1121 nil t)
1122
1123 ;; restore the point
1124 (goto-char (if moving rcirc-prompt-end-marker old-point))
1125
1126 ;; flush undo (can we do something smarter here?)
1127 (buffer-disable-undo)
1128 (buffer-enable-undo))
1129
1130 ;; record modeline activity
1131 (when activity
1132 (let ((nick-match
1133 (string-match (concat "\\b"
1134 (regexp-quote (rcirc-nick process))
1135 "\\b")
1136 text)))
1137 (when (or (not rcirc-ignore-buffer-activity-flag)
1138 ;; always notice when our nick is mentioned, even
1139 ;; if ignoring channel activity
1140 nick-match)
1141 (rcirc-record-activity
1142 (current-buffer)
1143 (when (or nick-match (not (rcirc-channel-p rcirc-target)))
1144 'nick)))))
1145
1146 (sit-for 0) ; displayed text before hook
1147 (run-hook-with-args 'rcirc-print-hooks
1148 process sender response target text)))))
bd43c990
RS
1149
1150(defun rcirc-startup-channels (server)
2e398771 1151 "Return the list of startup channels for SERVER."
bd43c990
RS
1152 (let (channels)
1153 (dolist (i rcirc-startup-channels-alist)
1154 (if (string-match (car i) server)
1155 (setq channels (append channels (cdr i)))))
1156 channels))
1157
1158(defun rcirc-join-channels (process channels)
1159 "Join CHANNELS."
1160 (save-window-excursion
db58efbf
EZ
1161 (dolist (channel channels)
1162 (with-rcirc-process-buffer process
1163 (rcirc-cmd-join channel process)))))
bd43c990
RS
1164\f
1165;;; nick management
1166(defun rcirc-user-nick (user)
1167 "Return the nick from USER. Remove any non-nick junk."
db58efbf
EZ
1168 (save-match-data
1169 (if (string-match "^[@%+]?\\([^! ]+\\)!?" (or user ""))
1170 (match-string 1 user)
1171 user)))
bd43c990
RS
1172
1173(defun rcirc-user-non-nick (user)
1174 "Return the non-nick portion of USER."
1175 (if (string-match "^[@+]?[^! ]+!?\\(.*\\)" (or user ""))
1176 (match-string 1 user)
1177 user))
1178
1179(defun rcirc-nick-channels (process nick)
1180 "Return list of channels for NICK."
db58efbf
EZ
1181 (with-rcirc-process-buffer process
1182 (mapcar (lambda (x) (car x))
1183 (gethash nick rcirc-nick-table))))
bd43c990
RS
1184
1185(defun rcirc-put-nick-channel (process nick channel)
1186 "Add CHANNEL to list associated with NICK."
2fbed782
EZ
1187 (let ((nick (rcirc-user-nick nick)))
1188 (with-rcirc-process-buffer process
1189 (let* ((chans (gethash nick rcirc-nick-table))
1190 (record (assoc-string channel chans t)))
1191 (if record
1192 (setcdr record (current-time))
1193 (puthash nick (cons (cons channel (current-time))
1194 chans)
1195 rcirc-nick-table))))))
bd43c990
RS
1196
1197(defun rcirc-nick-remove (process nick)
1198 "Remove NICK from table."
adf794e4 1199 (with-rcirc-process-buffer process
bd43c990
RS
1200 (remhash nick rcirc-nick-table)))
1201
1202(defun rcirc-remove-nick-channel (process nick channel)
1203 "Remove the CHANNEL from list associated with NICK."
adf794e4 1204 (with-rcirc-process-buffer process
db58efbf 1205 (let* ((chans (gethash nick rcirc-nick-table))
adf794e4
EZ
1206 (newchans
1207 ;; instead of assoc-string-delete-all:
1208 (let ((record (assoc-string channel chans t)))
1209 (when record
1210 (setcar record 'delete)
1211 (assq-delete-all 'delete chans)))))
bd43c990
RS
1212 (if newchans
1213 (puthash nick newchans rcirc-nick-table)
1214 (remhash nick rcirc-nick-table)))))
1215
1216(defun rcirc-channel-nicks (process channel)
1217 "Return the list of nicks in CHANNEL sorted by last activity."
adf794e4 1218 (with-rcirc-process-buffer process
bd43c990
RS
1219 (let (nicks)
1220 (maphash
1221 (lambda (k v)
adf794e4 1222 (let ((record (assoc-string channel v t)))
bd43c990
RS
1223 (if record
1224 (setq nicks (cons (cons k (cdr record)) nicks)))))
1225 rcirc-nick-table)
1226 (mapcar (lambda (x) (car x))
1227 (sort nicks (lambda (x y) (time-less-p (cdr y) (cdr x))))))))
2c8abe90
AS
1228
1229(defun rcirc-ignore-update-automatic (nick)
2e398771
JB
1230 "Remove NICK from `rcirc-ignore-list'
1231if NICK is also on `rcirc-ignore-list-automatic'."
2c8abe90
AS
1232 (when (member nick rcirc-ignore-list-automatic)
1233 (setq rcirc-ignore-list-automatic
1234 (delete nick rcirc-ignore-list-automatic)
1235 rcirc-ignore-list
1236 (delete nick rcirc-ignore-list))))
bd43c990
RS
1237\f
1238;;; activity tracking
db58efbf
EZ
1239(defvar rcirc-track-minor-mode-map (make-sparse-keymap)
1240 "Keymap for rcirc track minor mode.")
1241
1242(define-key rcirc-track-minor-mode-map (kbd "C-c `") 'rcirc-next-active-buffer)
1243(define-key rcirc-track-minor-mode-map (kbd "C-c C-@") 'rcirc-next-active-buffer)
1244(define-key rcirc-track-minor-mode-map (kbd "C-c C-SPC") 'rcirc-next-active-buffer)
1245
e8f10ddb 1246;;;###autoload
db58efbf
EZ
1247(define-minor-mode rcirc-track-minor-mode
1248 "Global minor mode for tracking activity in rcirc buffers."
1249 :init-value nil
1250 :lighter ""
1251 :keymap rcirc-track-minor-mode-map
1252 :global t
1253 :group 'rcirc
1254 (or global-mode-string (setq global-mode-string '("")))
1255 ;; toggle the mode-line channel indicator
1256 (if rcirc-track-minor-mode
1257 (and (not (memq 'rcirc-activity-string global-mode-string))
1258 (setq global-mode-string
1259 (append global-mode-string '(rcirc-activity-string))))
1260 (setq global-mode-string
1261 (delete 'rcirc-activity-string global-mode-string))))
1262
adf794e4 1263(or (assq 'rcirc-ignore-buffer-activity-flag minor-mode-alist)
bd43c990 1264 (setq minor-mode-alist
adf794e4 1265 (cons '(rcirc-ignore-buffer-activity-flag " Ignore") minor-mode-alist)))
bd43c990 1266
db58efbf
EZ
1267(defun rcirc-toggle-ignore-buffer-activity ()
1268 "Toggle the value of `rcirc-ignore-buffer-activity-flag'."
1269 (interactive)
1270 (setq rcirc-ignore-buffer-activity-flag
1271 (not rcirc-ignore-buffer-activity-flag))
1272 (message (if rcirc-ignore-buffer-activity-flag
1273 "Ignore activity in this buffer"
1274 "Notice activity in this buffer"))
bd43c990
RS
1275 (force-mode-line-update))
1276
1277(defvar rcirc-switch-to-buffer-function 'switch-to-buffer
1278 "Function to use when switching buffers.
1279Possible values are `switch-to-buffer', `pop-to-buffer', and
1280`display-buffer'.")
1281
1282(defun rcirc-switch-to-server-buffer ()
1283 "Switch to the server buffer associated with current channel buffer."
1284 (interactive)
1285 (funcall rcirc-switch-to-buffer-function (process-buffer rcirc-process)))
1286
1287(defun rcirc-jump-to-first-unread-line ()
1288 "Move the point to the first unread line in this buffer."
1289 (interactive)
1290 (when (marker-position overlay-arrow-position)
1291 (goto-char overlay-arrow-position)))
1292
1293(defvar rcirc-last-non-irc-buffer nil
1294 "The buffer to switch to when there is no more activity.")
1295
1296(defun rcirc-next-active-buffer (arg)
1297 "Go to the ARGth rcirc buffer with activity.
1298The function given by `rcirc-switch-to-buffer-function' is used to
1299show the buffer."
1300 (interactive "p")
1301 (if rcirc-activity
1302 (progn
1303 (unless (eq major-mode 'rcirc-mode)
1304 (setq rcirc-last-non-irc-buffer (current-buffer)))
1305 (if (and (> arg 0)
1306 (<= arg (length rcirc-activity)))
adf794e4 1307 (funcall rcirc-switch-to-buffer-function
bd43c990
RS
1308 (nth (1- arg) rcirc-activity))
1309 (message "Invalid arg: %d" arg)))
1310 (if (eq major-mode 'rcirc-mode)
1311 (if (not (and rcirc-last-non-irc-buffer
1312 (buffer-live-p rcirc-last-non-irc-buffer)))
adf794e4
EZ
1313 (message "No IRC activity. Start something.")
1314 (message "No more IRC activity. Go back to work.")
bd43c990
RS
1315 (funcall rcirc-switch-to-buffer-function rcirc-last-non-irc-buffer)
1316 (setq rcirc-last-non-irc-buffer nil))
adf794e4 1317 (message "No IRC activity."))))
bd43c990
RS
1318
1319(defvar rcirc-activity-hooks nil
1320 "Hook to be run when there is channel activity.
1321
1322Functions are called with a single argument, the buffer with the
1323activity. Only run if the buffer is not visible and
adf794e4 1324`rcirc-ignore-buffer-activity-flag' is non-nil.")
bd43c990
RS
1325
1326(defun rcirc-record-activity (buffer type)
1327 "Record BUFFER activity with TYPE."
1328 (with-current-buffer buffer
1329 (when (not (get-buffer-window (current-buffer) t))
adf794e4 1330 (add-to-list 'rcirc-activity (current-buffer))
bd43c990
RS
1331 (if (not rcirc-activity-type)
1332 (setq rcirc-activity-type type))
1333 (rcirc-update-activity-string)))
1334 (run-hook-with-args 'rcirc-activity-hooks buffer))
1335
1336(defun rcirc-clear-activity (buffer)
1337 "Clear the BUFFER activity."
1338 (setq rcirc-activity (delete buffer rcirc-activity))
1339 (with-current-buffer buffer
1340 (setq rcirc-activity-type nil)))
1341
adf794e4 1342;; TODO: add mouse properties
bd43c990
RS
1343(defun rcirc-update-activity-string ()
1344 "Update mode-line string."
1345 (setq rcirc-activity-string
db58efbf
EZ
1346 (if (not rcirc-activity)
1347 ""
e8f10ddb 1348 (concat "-["
db58efbf
EZ
1349 (mapconcat
1350 (lambda (b)
1351 (let ((s (rcirc-short-buffer-name b)))
1352 (with-current-buffer b
1353 (if (not (eq rcirc-activity-type 'nick))
1354 s
1355 (rcirc-facify s 'rcirc-mode-line-nick)))))
1356 rcirc-activity ",")
e8f10ddb 1357 "]-"))))
bd43c990
RS
1358
1359(defun rcirc-short-buffer-name (buffer)
1360 "Return a short name for BUFFER to use in the modeline indicator."
1361 (with-current-buffer buffer
adf794e4
EZ
1362 (or rcirc-short-buffer-name (buffer-name))))
1363
1364(defvar rcirc-current-buffer nil)
1365(defun rcirc-window-configuration-change ()
1366 "Go through visible windows and remove buffers from activity list.
1367Also, clear the overlay arrow if the current buffer is now hidden."
1368 (let ((current-now-hidden t))
1369 (walk-windows (lambda (w)
1370 (let ((buf (window-buffer w)))
1371 (rcirc-clear-activity buf)
1372 (when (eq buf rcirc-current-buffer)
1373 (setq current-now-hidden nil)))))
1374 (when (and rcirc-current-buffer current-now-hidden)
1375 (with-current-buffer rcirc-current-buffer
1376 (when (eq major-mode 'rcirc-mode)
1377 (marker-position overlay-arrow-position)
1378 (set-marker overlay-arrow-position nil)))))
1379
1380 ;; remove any killed buffers from list
1381 (setq rcirc-activity
1382 (delq nil (mapcar (lambda (buf) (when (buffer-live-p buf) buf))
1383 rcirc-activity)))
1384 (rcirc-update-activity-string)
1385 (setq rcirc-current-buffer (current-buffer)))
bd43c990
RS
1386
1387\f
adf794e4
EZ
1388;;; buffer name abbreviation
1389(defun rcirc-update-short-buffer-names ()
1390 (let ((bufalist
1391 (apply 'append (mapcar (lambda (process)
1392 (with-rcirc-process-buffer process
1393 rcirc-buffer-alist))
1394 (rcirc-process-list)))))
1395 (dolist (i (rcirc-abbreviate bufalist))
1396 (with-current-buffer (cdr i)
1397 (setq rcirc-short-buffer-name (car i))))))
1398
1399(defun rcirc-abbreviate (pairs)
1400 (apply 'append (mapcar 'rcirc-rebuild-tree (rcirc-make-trees pairs))))
1401
1402(defun rcirc-rebuild-tree (tree &optional acc)
1403 (let ((ch (char-to-string (car tree))))
1404 (dolist (x (cdr tree))
1405 (if (listp x)
1406 (setq acc (append acc
1407 (mapcar (lambda (y)
1408 (cons (concat ch (car y))
1409 (cdr y)))
1410 (rcirc-rebuild-tree x))))
1411 (setq acc (cons (cons ch x) acc))))
1412 acc))
1413
1414(defun rcirc-make-trees (pairs)
1415 (let (alist)
1416 (mapc (lambda (pair)
1417 (if (consp pair)
1418 (let* ((str (car pair))
1419 (data (cdr pair))
1420 (char (unless (zerop (length str))
1421 (aref str 0)))
1422 (rest (unless (zerop (length str))
1423 (substring str 1)))
1424 (part (if char (assq char alist))))
1425 (if part
1426 ;; existing partition
1427 (setcdr part (cons (cons rest data) (cdr part)))
1428 ;; new partition
1429 (setq alist (cons (if char
1430 (list char (cons rest data))
1431 data)
1432 alist))))
1433 (setq alist (cons pair alist))))
1434 pairs)
1435 ;; recurse into cdrs of alist
1436 (mapc (lambda (x)
1437 (when (and (listp x) (listp (cadr x)))
1438 (setcdr x (if (> (length (cdr x)) 1)
1439 (rcirc-make-trees (cdr x))
1440 (setcdr x (list (cdadr x)))))))
1441 alist)))
1442\f
bd43c990
RS
1443;;; /commands these are called with 3 args: PROCESS, TARGET, which is
1444;; the current buffer/channel/user, and ARGS, which is a string
1445;; containing the text following the /cmd.
1446
adf794e4 1447(defmacro defun-rcirc-command (command argument docstring interactive-form
bd43c990
RS
1448 &rest body)
1449 "Define a command."
1450 `(defun ,(intern (concat "rcirc-cmd-" (symbol-name command)))
1451 (,@argument &optional process target)
1452 ,(concat docstring "\n\nNote: If PROCESS or TARGET are nil, the values of"
1453 "\nbuffer local variables `rcirc-process' and `rcirc-target',"
1454 "\nwill be used.")
1455 ,interactive-form
1456 (let ((process (or process rcirc-process))
1457 (target (or target rcirc-target)))
1458 ,@body)))
1459
1460(defun-rcirc-command msg (message)
1461 "Send private MESSAGE to TARGET."
1462 (interactive "i")
1463 (if (null message)
1464 (progn
1465 (setq target (completing-read "Message nick: "
adf794e4 1466 (with-rcirc-process-buffer rcirc-process
bd43c990
RS
1467 rcirc-nick-table)))
1468 (when (> (length target) 0)
1469 (setq message (read-string (format "Message %s: " target)))
1470 (when (> (length message) 0)
1471 (rcirc-send-message process target message))))
1472 (if (not (string-match "\\([^ ]+\\) \\(.+\\)" message))
1473 (message "Not enough args, or something.")
1474 (setq target (match-string 1 message)
1475 message (match-string 2 message))
1476 (rcirc-send-message process target message))))
1477
1478(defun-rcirc-command query (nick)
1479 "Open a private chat buffer to NICK."
1480 (interactive (list (completing-read "Query nick: "
adf794e4 1481 (with-rcirc-process-buffer rcirc-process
bd43c990 1482 rcirc-nick-table))))
adf794e4
EZ
1483 (let ((existing-buffer (rcirc-get-buffer process nick)))
1484 (switch-to-buffer (or existing-buffer
1485 (rcirc-get-buffer-create process nick)))
1486 (when (not existing-buffer)
bd43c990
RS
1487 (rcirc-cmd-whois nick))))
1488
f161b079 1489(defun-rcirc-command join (channel)
bd43c990
RS
1490 "Join CHANNEL."
1491 (interactive "sJoin channel: ")
f161b079
JB
1492 (let ((buffer (rcirc-get-buffer-create process
1493 (car (split-string channel)))))
bd43c990
RS
1494 (when (not (eq (selected-window) (minibuffer-window)))
1495 (funcall rcirc-switch-to-buffer-function buffer))
f161b079 1496 (rcirc-send-string process (concat "JOIN " channel))))
bd43c990
RS
1497
1498(defun-rcirc-command part (channel)
1499 "Part CHANNEL."
1500 (interactive "sPart channel: ")
1501 (let ((channel (if (> (length channel) 0) channel target)))
adf794e4 1502 (rcirc-send-string process (concat "PART " channel " :" rcirc-id-string))))
bd43c990
RS
1503
1504(defun-rcirc-command quit (reason)
1505 "Send a quit message to server with REASON."
1506 (interactive "sQuit reason: ")
adf794e4
EZ
1507 (rcirc-send-string process (concat "QUIT :"
1508 (if (not (zerop (length reason)))
1509 reason
1510 rcirc-id-string))))
bd43c990
RS
1511
1512(defun-rcirc-command nick (nick)
1513 "Change nick to NICK."
1514 (interactive "i")
1515 (when (null nick)
1516 (setq nick (read-string "New nick: " (rcirc-nick process))))
1517 (rcirc-send-string process (concat "NICK " nick)))
1518
1519(defun-rcirc-command names (channel)
1520 "Display list of names in CHANNEL or in current channel if CHANNEL is nil.
1521If called interactively, prompt for a channel when prefix arg is supplied."
1522 (interactive "P")
1523 (if (interactive-p)
1524 (if channel
1525 (setq channel (read-string "List names in channel: " target))))
1526 (let ((channel (if (> (length channel) 0)
1527 channel
1528 target)))
1529 (rcirc-send-string process (concat "NAMES " channel))))
1530
1531(defun-rcirc-command topic (topic)
1532 "List TOPIC for the TARGET channel.
1533With a prefix arg, prompt for new topic."
1534 (interactive "P")
1535 (if (and (interactive-p) topic)
1536 (setq topic (read-string "New Topic: " rcirc-topic)))
1537 (rcirc-send-string process (concat "TOPIC " target
1538 (when (> (length topic) 0)
1539 (concat " :" topic)))))
1540
1541(defun-rcirc-command whois (nick)
1542 "Request information from server about NICK."
1543 (interactive (list
1544 (completing-read "Whois: "
adf794e4 1545 (with-rcirc-process-buffer rcirc-process
bd43c990 1546 rcirc-nick-table))))
bd43c990
RS
1547 (rcirc-send-string process (concat "WHOIS " nick)))
1548
1549(defun-rcirc-command mode (args)
1550 "Set mode with ARGS."
1551 (interactive (list (concat (read-string "Mode nick or channel: ")
1552 " " (read-string "Mode: "))))
1553 (rcirc-send-string process (concat "MODE " args)))
1554
1555(defun-rcirc-command list (channels)
1556 "Request information on CHANNELS from server."
1557 (interactive "sList Channels: ")
1558 (rcirc-send-string process (concat "LIST " channels)))
1559
1560(defun-rcirc-command oper (args)
1561 "Send operator command to server."
1562 (interactive "sOper args: ")
1563 (rcirc-send-string process (concat "OPER " args)))
1564
1565(defun-rcirc-command quote (message)
1566 "Send MESSAGE literally to server."
1567 (interactive "sServer message: ")
1568 (rcirc-send-string process message))
1569
1570(defun-rcirc-command kick (arg)
1571 "Kick NICK from current channel."
1572 (interactive (list
1573 (concat (completing-read "Kick nick: "
1574 (rcirc-channel-nicks rcirc-process
1575 rcirc-target))
1576 (read-from-minibuffer "Kick reason: "))))
1577 (let* ((arglist (split-string arg))
adf794e4 1578 (argstring (concat (car arglist) " :"
bd43c990
RS
1579 (mapconcat 'identity (cdr arglist) " "))))
1580 (rcirc-send-string process (concat "KICK " target " " argstring))))
1581
1582(defun rcirc-cmd-ctcp (args &optional process target)
1583 (if (string-match "^\\([^ ]+\\)\\s-+\\(.+\\)$" args)
1584 (let ((target (match-string 1 args))
1585 (request (match-string 2 args)))
adf794e4
EZ
1586 (rcirc-send-string process
1587 (format "PRIVMSG %s \C-a%s\C-a"
1588 target (upcase request))))
1589 (rcirc-print process (rcirc-nick process) "ERROR" nil
bd43c990
RS
1590 "usage: /ctcp NICK REQUEST")))
1591
1592(defun rcirc-cmd-me (args &optional process target)
1593 (rcirc-send-string process (format "PRIVMSG %s :\C-aACTION %s\C-a"
1594 target args)))
2c8abe90
AS
1595
1596(defun-rcirc-command ignore (nick)
1597 "Manage the ignore list.
1598Ignore NICK, unignore NICK if already ignored, or list ignored
1599nicks when no NICK is given. When listing ignored nicks, the
2e398771 1600ones added to the list automatically are marked with an asterisk."
2c8abe90 1601 (interactive "sToggle ignoring of nick: ")
db58efbf 1602 (when (not (string= "" nick))
2c8abe90
AS
1603 (if (member nick rcirc-ignore-list)
1604 (setq rcirc-ignore-list (delete nick rcirc-ignore-list))
db58efbf
EZ
1605 (setq rcirc-ignore-list (cons nick rcirc-ignore-list))))
1606 (rcirc-print process (rcirc-nick process) "IGNORE" target
1607 (mapconcat
1608 (lambda (nick)
1609 (concat nick
1610 (if (member nick rcirc-ignore-list-automatic)
1611 "*" "")))
1612 rcirc-ignore-list " ")))
2c8abe90 1613
bd43c990
RS
1614\f
1615(defun rcirc-message-leader (sender face)
1616 "Return a string with SENDER propertized with FACE."
db58efbf 1617 (rcirc-facify (concat "<" sender "> ") face))
adf794e4 1618
bd43c990
RS
1619(defun rcirc-facify (string face)
1620 "Return a copy of STRING with FACE property added."
1621 (propertize (or string "") 'face face 'rear-nonsticky t))
1622
bd43c990 1623(defvar rcirc-url-regexp
2fbed782
EZ
1624 (rx-to-string
1625 `(and word-boundary
019ed9c7
EZ
1626 (or (and
1627 (or (and (or "http" "https" "ftp" "file" "gopher" "news"
1628 "telnet" "wais" "mailto")
1629 "://")
1630 "www.")
1631 (1+ (char "-a-zA-Z0-9_."))
1632 (optional ":" (1+ (char "0-9"))))
2fbed782
EZ
1633 (and (1+ (char "-a-zA-Z0-9_."))
1634 (or ".com" ".net" ".org")
1635 word-boundary))
1636 (optional
1637 (and "/"
1638 (1+ (char "-a-zA-Z0-9_=!?#$\@~`%&*+|\\/:;.,{}[]"))
1639 (char "-a-zA-Z0-9_=#$\@~`%&*+|\\/:;{}[]")))))
2e398771 1640 "Regexp matching URLs. Set to nil to disable URL features in rcirc.")
bd43c990
RS
1641
1642(defun rcirc-browse-url (&optional arg)
2e398771 1643 "Prompt for URL to browse based on URLs in buffer."
bd43c990
RS
1644 (interactive)
1645 (let ((completions (mapcar (lambda (x) (cons x nil)) rcirc-urls))
1646 (initial-input (car rcirc-urls))
1647 (history (cdr rcirc-urls)))
1648 (browse-url (completing-read "rcirc browse-url: "
1649 completions nil nil initial-input 'history)
1650 arg)))
1651
adf794e4
EZ
1652(defun rcirc-browse-url-at-point (point)
1653 "Send URL at point to `browse-url'."
1654 (interactive "d")
1655 (let ((beg (previous-single-property-change point 'mouse-face))
1656 (end (next-single-property-change point 'mouse-face)))
1657 (browse-url (buffer-substring-no-properties beg end))))
1658
1659(defun rcirc-browse-url-at-mouse (event)
1660 "Send URL at mouse click to `browse-url'."
1661 (interactive "e")
1662 (let ((position (event-end event)))
1663 (with-current-buffer (window-buffer (posn-window position))
1664 (rcirc-browse-url-at-point (posn-point position)))))
1665
bd43c990
RS
1666(defun rcirc-map-regexp (function regexp string)
1667 "Return a copy of STRING after calling FUNCTION for each REGEXP match.
1668FUNCTION takes 3 arguments, MATCH-START, MATCH-END, and STRING."
1669 (let ((start 0))
1670 (while (string-match regexp string start)
1671 (setq start (match-end 0))
1672 (funcall function (match-beginning 0) (match-end 0) string)))
1673 string)
1674
bd43c990
RS
1675(defun rcirc-mangle-text (process text)
1676 "Return TEXT with properties added based on various patterns."
1677 ;; ^B
1678 (setq text
2e398771 1679 (rcirc-map-regexp
ad8121fe
EZ
1680 (lambda (start end string)
1681 (let ((orig-face (get-text-property start 'face string)))
1682 (add-text-properties
1683 start end
1684 (list 'face (if (listp orig-face)
2e398771 1685 (append orig-face
ad8121fe
EZ
1686 (list 'bold))
1687 (list orig-face 'bold))
1688 'rear-nonsticky t)
1689 string)))
1690 "\ 2.*?\ 2"
1691 text))
1692 ;; TODO: deal with ^_ and ^C colors sequences
2e398771 1693 (while (string-match "\\(.*\\)[\ 2\ 1]\\(.*\\)" text)
bd43c990
RS
1694 (setq text (concat (match-string 1 text)
1695 (match-string 2 text))))
1696 ;; my nick
1697 (setq text
1698 (with-syntax-table rcirc-nick-syntax-table
1699 (rcirc-map-regexp (lambda (start end string)
1700 (add-text-properties
1701 start end
adf794e4 1702 (list 'face 'rcirc-nick-in-message
bd43c990
RS
1703 'rear-nonsticky t)
1704 string))
1705 (concat "\\b"
1706 (regexp-quote (rcirc-nick process))
1707 "\\b")
1708 text)))
1709 ;; urls
1710 (setq text
1711 (rcirc-map-regexp
1712 (lambda (start end string)
1713 (let ((orig-face (get-text-property start 'face string)))
1714 (add-text-properties start end
ad8121fe 1715 (list 'face (if (listp orig-face)
2e398771 1716 (append orig-face
ad8121fe
EZ
1717 (list 'bold))
1718 (list orig-face 'bold))
adf794e4
EZ
1719 'rear-nonsticky t
1720 'mouse-face 'highlight
1721 'keymap rcirc-browse-url-map)
bd43c990 1722 string))
db58efbf 1723 (push (substring-no-properties string start end) rcirc-urls))
bd43c990
RS
1724 rcirc-url-regexp
1725 text))
1726 text)
1727
1728\f
1729;;; handlers
1730;; these are called with the server PROCESS, the SENDER, which is a
1731;; server or a user, depending on the command, the ARGS, which is a
1732;; list of strings, and the TEXT, which is the original server text,
1733;; verbatim
1734(defun rcirc-handler-001 (process sender args text)
1735 (rcirc-handler-generic process "001" sender args text)
1736 ;; set the real server name
adf794e4 1737 (with-rcirc-process-buffer process
bd43c990
RS
1738 (setq rcirc-server sender)
1739 (setq rcirc-nick (car args))
1740 (rcirc-update-prompt)
1741 (when rcirc-auto-authenticate-flag (rcirc-authenticate))
adf794e4 1742 (rcirc-join-channels process rcirc-startup-channels)))
bd43c990
RS
1743
1744(defun rcirc-handler-PRIVMSG (process sender args text)
1745 (let ((target (if (rcirc-channel-p (car args))
1746 (car args)
db58efbf 1747 sender))
bd43c990
RS
1748 (message (or (cadr args) "")))
1749 (if (string-match "^\C-a\\(.*\\)\C-a$" message)
1750 (rcirc-handler-CTCP process target sender (match-string 1 message))
1751 (rcirc-print process sender "PRIVMSG" target message t))
1752 ;; update nick timestamp
1753 (if (member target (rcirc-nick-channels process sender))
1754 (rcirc-put-nick-channel process sender target))))
1755
1756(defun rcirc-handler-NOTICE (process sender args text)
1757 (let ((target (car args))
1758 (message (cadr args)))
adf794e4
EZ
1759 (if (string-match "^\C-a\\(.*\\)\C-a$" message)
1760 (rcirc-handler-CTCP-response process target sender
1761 (match-string 1 message))
1762 (rcirc-print process sender "NOTICE"
1763 (cond ((rcirc-channel-p target)
1764 target)
1765 ;;; -ChanServ- [#gnu] Welcome...
1766 ((string-match "^\\[\\(#[^ ]+\\)\\]" message)
1767 (match-string 1 message))
1768 (sender
1769 (if (string= sender (rcirc-server process))
db58efbf
EZ
1770 nil ; server notice
1771 sender)))
adf794e4 1772 message t))))
bd43c990
RS
1773
1774(defun rcirc-handler-WALLOPS (process sender args text)
db58efbf 1775 (rcirc-print process sender "WALLOPS" sender (car args) t))
bd43c990
RS
1776
1777(defun rcirc-handler-JOIN (process sender args text)
db58efbf 1778 (let ((channel (car args)))
bd43c990
RS
1779 (rcirc-get-buffer-create process channel)
1780 (rcirc-print process sender "JOIN" channel "")
1781
1782 ;; print in private chat buffer if it exists
db58efbf
EZ
1783 (when (rcirc-get-buffer rcirc-process sender)
1784 (rcirc-print process sender "JOIN" sender channel))
bd43c990 1785
adf794e4 1786 (rcirc-put-nick-channel process sender channel)))
bd43c990
RS
1787
1788;; PART and KICK are handled the same way
1789(defun rcirc-handler-PART-or-KICK (process response channel sender nick args)
1790 (rcirc-print process sender response channel (concat channel " " args))
1791
1792 ;; print in private chat buffer if it exists
adf794e4 1793 (when (rcirc-get-buffer rcirc-process nick)
bd43c990
RS
1794 (rcirc-print process sender response nick (concat channel " " args)))
1795
1796 (if (not (string= nick (rcirc-nick process)))
1797 ;; this is someone else leaving
adf794e4
EZ
1798 (rcirc-remove-nick-channel process nick channel)
1799 ;; this is us leaving
1800 (mapc (lambda (n)
1801 (rcirc-remove-nick-channel process n channel))
1802 (rcirc-channel-nicks process channel))
1803
1804 ;; if the buffer is still around, make it inactive
1805 (let ((buffer (rcirc-get-buffer process channel)))
1806 (when buffer
1807 (with-current-buffer buffer
1808 (setq rcirc-target nil))))))
bd43c990
RS
1809
1810(defun rcirc-handler-PART (process sender args text)
db58efbf 1811 (rcirc-ignore-update-automatic sender)
bd43c990 1812 (rcirc-handler-PART-or-KICK process "PART"
db58efbf 1813 (car args) sender sender
bd43c990
RS
1814 (cadr args)))
1815
1816(defun rcirc-handler-KICK (process sender args text)
adf794e4 1817 (rcirc-handler-PART-or-KICK process "KICK" (car args) sender (cadr args)
bd43c990
RS
1818 (caddr args)))
1819
1820(defun rcirc-handler-QUIT (process sender args text)
db58efbf
EZ
1821 (rcirc-ignore-update-automatic sender)
1822 (mapc (lambda (channel)
1823 (rcirc-print process sender "QUIT" channel (apply 'concat args)))
1824 (rcirc-nick-channels process sender))
bd43c990 1825
db58efbf
EZ
1826 ;; print in private chat buffer if it exists
1827 (when (rcirc-get-buffer rcirc-process sender)
1828 (rcirc-print process sender "QUIT" sender (apply 'concat args)))
bd43c990 1829
db58efbf 1830 (rcirc-nick-remove process sender))
bd43c990
RS
1831
1832(defun rcirc-handler-NICK (process sender args text)
db58efbf 1833 (let* ((old-nick sender)
bd43c990
RS
1834 (new-nick (car args))
1835 (channels (rcirc-nick-channels process old-nick)))
2c8abe90
AS
1836 ;; update list of ignored nicks
1837 (rcirc-ignore-update-automatic old-nick)
1838 (when (member old-nick rcirc-ignore-list)
1839 (add-to-list 'rcirc-ignore-list new-nick)
1840 (add-to-list 'rcirc-ignore-list-automatic new-nick))
bd43c990
RS
1841 ;; print message to nick's channels
1842 (dolist (target channels)
1843 (rcirc-print process sender "NICK" target new-nick))
1844 ;; update private chat buffer, if it exists
adf794e4
EZ
1845 (let ((chat-buffer (rcirc-get-buffer process old-nick)))
1846 (when chat-buffer
1847 (with-current-buffer chat-buffer
1848 (rcirc-print process sender "NICK" old-nick new-nick)
1849 (setq rcirc-target new-nick)
1850 (rename-buffer (rcirc-generate-new-buffer-name process new-nick)))))
bd43c990 1851 ;; remove old nick and add new one
adf794e4 1852 (with-rcirc-process-buffer process
bd43c990
RS
1853 (let ((v (gethash old-nick rcirc-nick-table)))
1854 (remhash old-nick rcirc-nick-table)
1855 (puthash new-nick v rcirc-nick-table))
1856 ;; if this is our nick...
1857 (when (string= old-nick rcirc-nick)
1858 (setq rcirc-nick new-nick)
adf794e4 1859 (rcirc-update-prompt t)
bd43c990
RS
1860 ;; reauthenticate
1861 (when rcirc-auto-authenticate-flag (rcirc-authenticate))))))
1862
1863(defun rcirc-handler-PING (process sender args text)
1864 (rcirc-send-string process (concat "PONG " (car args))))
1865
1866(defun rcirc-handler-PONG (process sender args text)
1867 ;; do nothing
1868 )
1869
1870(defun rcirc-handler-TOPIC (process sender args text)
1871 (let ((topic (cadr args)))
1872 (rcirc-print process sender "TOPIC" (car args) topic)
1873 (with-current-buffer (rcirc-get-buffer process (car args))
1874 (setq rcirc-topic topic))))
1875
1876(defun rcirc-handler-332 (process sender args text)
1877 "RPL_TOPIC"
adf794e4
EZ
1878 (let ((buffer (or (rcirc-get-buffer process (cadr args))
1879 (rcirc-get-temp-buffer-create process (cadr args)))))
1880 (with-current-buffer buffer
1881 (setq rcirc-topic (caddr args)))))
bd43c990
RS
1882
1883(defun rcirc-handler-333 (process sender args text)
1884 "Not in rfc1459.txt"
adf794e4
EZ
1885 (let ((buffer (or (rcirc-get-buffer process (cadr args))
1886 (rcirc-get-temp-buffer-create process (cadr args)))))
1887 (with-current-buffer buffer
1888 (let ((setter (caddr args))
1889 (time (current-time-string
1890 (seconds-to-time
1891 (string-to-number (cadddr args))))))
1892 (rcirc-print process sender "TOPIC" (cadr args)
1893 (format "%s (%s on %s)" rcirc-topic setter time))))))
bd43c990
RS
1894
1895(defun rcirc-handler-477 (process sender args text)
1896 "ERR_NOCHANMODES"
1897 (rcirc-print process sender "477" (cadr args) (caddr args)))
1898
1899(defun rcirc-handler-MODE (process sender args text)
1900 (let ((target (car args))
1901 (msg (mapconcat 'identity (cdr args) " ")))
1902 (rcirc-print process sender "MODE"
1903 (if (string= target (rcirc-nick process))
1904 nil
1905 target)
1906 msg)
1907
1908 ;; print in private chat buffers if they exist
1909 (mapc (lambda (nick)
db58efbf
EZ
1910 (when (rcirc-get-buffer process nick)
1911 (rcirc-print process sender "MODE" nick msg)))
adf794e4 1912 (cddr args))))
bd43c990
RS
1913
1914(defun rcirc-get-temp-buffer-create (process channel)
1915 "Return a buffer based on PROCESS and CHANNEL."
1916 (let ((tmpnam (concat " " (downcase channel) "TMP" (process-name process))))
1917 (get-buffer-create tmpnam)))
1918
1919(defun rcirc-handler-353 (process sender args text)
1920 "RPL_NAMREPLY"
adf794e4 1921 (let ((channel (caddr args)))
bd43c990
RS
1922 (mapc (lambda (nick)
1923 (rcirc-put-nick-channel process nick channel))
adf794e4 1924 (split-string (cadddr args) " " t))
bd43c990
RS
1925 (with-current-buffer (rcirc-get-temp-buffer-create process channel)
1926 (goto-char (point-max))
1927 (insert (car (last args)) " "))))
1928
1929(defun rcirc-handler-366 (process sender args text)
1930 "RPL_ENDOFNAMES"
1931 (let* ((channel (cadr args))
1932 (buffer (rcirc-get-temp-buffer-create process channel)))
1933 (with-current-buffer buffer
1934 (rcirc-print process sender "NAMES" channel
1935 (buffer-substring (point-min) (point-max))))
1936 (kill-buffer buffer)))
1937
1938(defun rcirc-handler-433 (process sender args text)
1939 "ERR_NICKNAMEINUSE"
1940 (rcirc-handler-generic process "433" sender args text)
1941 (let* ((new-nick (concat (cadr args) "`")))
adf794e4 1942 (with-rcirc-process-buffer process
bd43c990
RS
1943 (rcirc-cmd-nick new-nick nil process))))
1944
1945(defun rcirc-authenticate ()
1946 "Send authentication to process associated with current buffer.
db58efbf 1947Passwords are stored in `rcirc-authinfo' (which see)."
bd43c990 1948 (interactive)
db58efbf
EZ
1949 (with-rcirc-process-buffer rcirc-process
1950 (dolist (i rcirc-authinfo)
1951 (let ((server (car i))
1952 (nick (caddr i))
1953 (method (cadr i))
1954 (args (cdddr i)))
1955 (when (and (string-match server rcirc-server)
1956 (string-match nick rcirc-nick))
1957 (cond ((equal method 'nickserv)
1958 (rcirc-send-string
1959 rcirc-process
1960 (concat
1961 "PRIVMSG nickserv :identify "
1962 (car args))))
1963 ((equal method 'chanserv)
1964 (rcirc-send-string
1965 rcirc-process
1966 (concat
1967 "PRIVMSG chanserv :identify "
1968 (cadr args) " " (car args))))
1969 ((equal method 'bitlbee)
1970 (rcirc-send-string
1971 rcirc-process
1972 (concat "PRIVMSG &bitlbee :identify " (car args))))
1973 (t
1974 (message "No %S authentication method defined"
1975 method))))))))
adf794e4 1976
bd43c990
RS
1977(defun rcirc-handler-INVITE (process sender args text)
1978 (rcirc-print process sender "INVITE" nil (mapconcat 'identity args " ") t))
1979
1980(defun rcirc-handler-ERROR (process sender args text)
1981 (rcirc-print process sender "ERROR" nil (mapconcat 'identity args " ")))
1982
1983(defun rcirc-handler-CTCP (process target sender text)
1984 (if (string-match "^\\([^ ]+\\) *\\(.*\\)$" text)
1985 (let* ((request (upcase (match-string 1 text)))
1986 (args (match-string 2 text))
bd43c990
RS
1987 (handler (intern-soft (concat "rcirc-handler-ctcp-" request))))
1988 (if (not (fboundp handler))
db58efbf
EZ
1989 (rcirc-print process sender "ERROR" target
1990 (format "%s sent unsupported ctcp: %s" sender text)
adf794e4 1991 t)
bd43c990
RS
1992 (funcall handler process target sender args)
1993 (if (not (string= request "ACTION"))
db58efbf 1994 (rcirc-print process sender "CTCP" target
adf794e4 1995 (format "%s" text) t))))))
bd43c990
RS
1996
1997(defun rcirc-handler-ctcp-VERSION (process target sender args)
1998 (rcirc-send-string process
db58efbf 1999 (concat "NOTICE " sender
adf794e4 2000 " :\C-aVERSION " rcirc-id-string
bd43c990
RS
2001 "\C-a")))
2002
2003(defun rcirc-handler-ctcp-ACTION (process target sender args)
2004 (rcirc-print process sender "ACTION" target args t))
2005
2006(defun rcirc-handler-ctcp-TIME (process target sender args)
2007 (rcirc-send-string process
db58efbf 2008 (concat "NOTICE " sender
bd43c990 2009 " :\C-aTIME " (current-time-string) "\C-a")))
adf794e4
EZ
2010
2011(defun rcirc-handler-CTCP-response (process target sender message)
2012 (rcirc-print process sender "CTCP" nil message t))
bd43c990 2013\f
adf794e4
EZ
2014(defgroup rcirc-faces nil
2015 "Faces for rcirc."
2016 :group 'rcirc
2017 :group 'faces)
2018
ad8121fe
EZ
2019(defface rcirc-my-nick ; font-lock-function-name-face
2020 '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
2021 (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
2022 (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
2023 (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
2024 (((class color) (min-colors 8)) (:foreground "blue" :weight bold))
2025 (t (:inverse-video t :weight bold)))
adf794e4
EZ
2026 "The face used to highlight my messages."
2027 :group 'rcirc-faces)
bd43c990 2028
ad8121fe
EZ
2029(defface rcirc-other-nick ; font-lock-variable-name-face
2030 '((((class grayscale) (background light))
2031 (:foreground "Gray90" :weight bold :slant italic))
bd43c990 2032 (((class grayscale) (background dark))
ad8121fe
EZ
2033 (:foreground "DimGray" :weight bold :slant italic))
2034 (((class color) (min-colors 88) (background light)) (:foreground "DarkGoldenrod"))
2035 (((class color) (min-colors 88) (background dark)) (:foreground "LightGoldenrod"))
2036 (((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
2037 (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
2038 (((class color) (min-colors 8)) (:foreground "yellow" :weight light))
2039 (t (:weight bold :slant italic)))
adf794e4
EZ
2040 "The face used to highlight other messages."
2041 :group 'rcirc-faces)
bd43c990 2042
ad8121fe
EZ
2043(defface rcirc-server ; font-lock-comment-face
2044 '((((class grayscale) (background light))
2045 (:foreground "DimGray" :weight bold :slant italic))
bd43c990 2046 (((class grayscale) (background dark))
ad8121fe
EZ
2047 (:foreground "LightGray" :weight bold :slant italic))
2048 (((class color) (min-colors 88) (background light))
2049 (:foreground "Firebrick"))
2050 (((class color) (min-colors 88) (background dark))
2051 (:foreground "chocolate1"))
2052 (((class color) (min-colors 16) (background light))
2053 (:foreground "red"))
2054 (((class color) (min-colors 16) (background dark))
2055 (:foreground "red1"))
2056 (((class color) (min-colors 8) (background light))
2057 )
2058 (((class color) (min-colors 8) (background dark))
2059 )
2060 (t (:weight bold :slant italic)))
adf794e4
EZ
2061 "The face used to highlight server messages."
2062 :group 'rcirc-faces)
bd43c990 2063
ad8121fe 2064(defface rcirc-server-prefix ; font-lock-comment-delimiter-face
db58efbf 2065 '((default :inherit rcirc-server)
ad8121fe
EZ
2066 (((class grayscale)))
2067 (((class color) (min-colors 16)))
2068 (((class color) (min-colors 8) (background light))
2069 :foreground "red")
2070 (((class color) (min-colors 8) (background dark))
2071 :foreground "red1"))
2072 "The face used to highlight server prefixes."
2073 :group 'rcirc-faces)
2074
2075(defface rcirc-timestamp
2076 '((t (:inherit default)))
2077 "The face used to highlight timestamps."
2078 :group 'rcirc-faces)
2079
2080(defface rcirc-nick-in-message ; font-lock-keyword-face
2081 '((((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
2082 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
2083 (((class color) (min-colors 88) (background light)) (:foreground "Purple"))
2084 (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
2085 (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
2086 (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
2087 (((class color) (min-colors 8)) (:foreground "cyan" :weight bold))
2088 (t (:weight bold)))
adf794e4
EZ
2089 "The face used to highlight instances of nick within messages."
2090 :group 'rcirc-faces)
bd43c990 2091
ad8121fe
EZ
2092(defface rcirc-prompt ; comint-highlight-prompt
2093 '((((min-colors 88) (background dark)) (:foreground "cyan1"))
2094 (((background dark)) (:foreground "cyan"))
bd43c990 2095 (t (:foreground "dark blue")))
2e398771 2096 "The face used to highlight prompts."
adf794e4 2097 :group 'rcirc-faces)
bd43c990 2098
adf794e4 2099(defface rcirc-mode-line-nick
bd43c990 2100 '((t (:bold t)))
adf794e4
EZ
2101 "The face used indicate activity directed at you."
2102 :group 'rcirc-faces)
bd43c990 2103\f
adf794e4 2104;; When using M-x flyspell-mode, only check words after the prompt
bd43c990
RS
2105(put 'rcirc-mode 'flyspell-mode-predicate 'rcirc-looking-at-input)
2106(defun rcirc-looking-at-input ()
2107 "Returns true if point is past the input marker."
2108 (>= (point) rcirc-prompt-end-marker))
2109\f
2110
2111(provide 'rcirc)
e636ae15
MB
2112
2113;; arch-tag: b471b7e8-6b5a-4399-b2c6-a3c78dfc8ffb
bd43c990 2114;;; rcirc.el ends here