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