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