erc.el 250 KB
Newer Older
1
;; erc.el --- An Emacs Internet Relay Chat client  -*- lexical-binding:t -*-
2

3
;; Copyright (C) 1997-2014 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11

;; Author: Alexander L. Belikoff (alexander@belikoff.net)
;; Contributors: Sergey Berezin (sergey.berezin@cs.cmu.edu),
;;               Mario Lang (mlang@delysid.org),
;;               Alex Schroeder (alex@gnu.org)
;;               Andreas Fuchs (afs@void.at)
;;               Gergely Nagy (algernon@midgard.debian.net)
;;               David Edmondson (dme@dme.org)
12
;;               Kelvin White (kwhite@gnu.org)
13
;; Maintainer: emacs-devel@gnu.org
14
;; Keywords: IRC, chat, client, Internet
Kelvin White's avatar
Kelvin White committed
15

16 17 18

;; This file is part of GNU Emacs.

19
;; GNU Emacs is free software: you can redistribute it and/or modify
20
;; it under the terms of the GNU General Public License as published by
21 22
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
23 24 25 26 27 28 29

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
30
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
31 32 33

;;; Commentary:

Miles Bader's avatar
Miles Bader committed
34
;; ERC is a powerful, modular, and extensible IRC client for Emacs.
35 36

;; For more information, see the following URLs:
Miles Bader's avatar
Miles Bader committed
37
;; * http://sv.gnu.org/projects/erc/
Miles Bader's avatar
Miles Bader committed
38
;; * http://www.emacswiki.org/cgi-bin/wiki/ERC
39

Miles Bader's avatar
Miles Bader committed
40 41 42 43
;; As of 2006-06-13, ERC development is now hosted on Savannah
;; (http://sv.gnu.org/projects/erc).  I invite everyone who wants to
;; hack on it to contact me <mwolson@gnu.org> in order to get write
;; access to the shared Arch archive.
44 45 46 47 48 49 50 51 52 53 54 55 56 57

;; Installation:

;; Put erc.el in your load-path, and put (require 'erc) in your .emacs.

;; Configuration:

;; Use M-x customize-group RET erc RET to get an overview
;; of all the variables you can tweak.

;; Usage:

;; To connect to an IRC server, do
;;
Miles Bader's avatar
Miles Bader committed
58
;; M-x erc RET
59 60
;;
;; After you are connected to a server, you can use C-h m or have a look at
Miles Bader's avatar
Miles Bader committed
61
;; the ERC menu.
62 63 64 65

;;; History:
;;

Kelvin White's avatar
Kelvin White committed
66
(defconst erc-version-string (format "\C-bERC\C-b (IRC client for Emacs %s)" emacs-version)
67 68
  "ERC version.  This is used by function `erc-version'.")

Kelvin White's avatar
Kelvin White committed
69 70
;;; Code:

71
(eval-when-compile (require 'cl-lib))
72 73 74
(require 'font-lock)
(require 'pp)
(require 'thingatpt)
Julien Danjou's avatar
Julien Danjou committed
75
(require 'auth-source)
76 77 78
(require 'erc-compat)

(defvar erc-official-location
Miles Bader's avatar
Miles Bader committed
79
  "http://emacswiki.org/cgi-bin/wiki/ERC (mailing list: erc-discuss@gnu.org)"
80 81 82 83
  "Location of the ERC client on the Internet.")

(defgroup erc nil
  "Emacs Internet Relay Chat client."
Miles Bader's avatar
Miles Bader committed
84
  :link '(url-link "http://www.emacswiki.org/cgi-bin/wiki/ERC")
85
  :link '(custom-manual "(erc) Top")
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  :prefix "erc-"
  :group 'applications)

(defgroup erc-buffers nil
  "Creating new ERC buffers"
  :group 'erc)

(defgroup erc-display nil
  "Settings for how various things are displayed"
  :group 'erc)

(defgroup erc-mode-line-and-header nil
  "Displaying information in the mode-line and header"
  :group 'erc-display)

(defgroup erc-ignore nil
  "Ignoring certain messages"
  :group 'erc)

105 106
(defgroup erc-lurker nil
  "Hide specified message types sent by lurkers"
Glenn Morris's avatar
Glenn Morris committed
107
  :version "24.3"
108 109
  :group 'erc-ignore)

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
(defgroup erc-query nil
  "Using separate buffers for private discussions"
  :group 'erc)

(defgroup erc-quit-and-part nil
  "Quitting and parting channels"
  :group 'erc)

(defgroup erc-paranoia nil
  "Know what is sent and received; control the display of sensitive data."
  :group 'erc)

(defgroup erc-scripts nil
  "Running scripts at startup and with /LOAD"
  :group 'erc)

(require 'erc-backend)

;; compatibility with older ERC releases

130 131 132 133 134
(define-obsolete-variable-alias 'erc-announced-server-name
  'erc-server-announced-name "ERC 5.1")
(define-obsolete-variable-alias 'erc-process 'erc-server-process "ERC 5.1")
(define-obsolete-variable-alias 'erc-default-coding-system
  'erc-server-coding-system "ERC 5.1")
135

136 137
(define-obsolete-function-alias 'erc-send-command
  'erc-server-send "ERC 5.1")
138 139 140 141

;; tunable connection and authentication parameters

(defcustom erc-server nil
Miles Bader's avatar
Miles Bader committed
142
  "IRC server to use if one is not provided.
143 144 145
See function `erc-compute-server' for more details on connection
parameters and authentication."
  :group 'erc
Miles Bader's avatar
Miles Bader committed
146
  :type '(choice (const :tag "None" nil)
Kelvin White's avatar
Kelvin White committed
147
                 (string :tag "Server")))
148 149

(defcustom erc-port nil
Miles Bader's avatar
Miles Bader committed
150 151 152
  "IRC port to use if not specified.

This can be either a string or a number."
153
  :group 'erc
Miles Bader's avatar
Miles Bader committed
154
  :type '(choice (const :tag "None" nil)
Kelvin White's avatar
Kelvin White committed
155 156
                 (integer :tag "Port number")
                 (string :tag "Port string")))
157 158

(defcustom erc-nick nil
Miles Bader's avatar
Miles Bader committed
159
  "Nickname to use if one is not provided.
160

Miles Bader's avatar
Miles Bader committed
161
This can be either a string, or a list of strings.
162 163 164 165 166 167
In the latter case, if the first nick in the list is already in use,
other nicks are tried in the list order.

See function `erc-compute-nick' for more details on connection
parameters and authentication."
  :group 'erc
Miles Bader's avatar
Miles Bader committed
168
  :type '(choice (const :tag "None" nil)
Kelvin White's avatar
Kelvin White committed
169 170
                 (string :tag "Nickname")
                 (repeat (string :tag "Nickname"))))
171 172

(defcustom erc-nick-uniquifier "`"
Miles Bader's avatar
Miles Bader committed
173
  "The string to append to the nick if it is already in use."
174 175 176
  :group 'erc
  :type 'string)

Miles Bader's avatar
Miles Bader committed
177 178 179 180 181
(defcustom erc-try-new-nick-p t
  "If the nickname you chose isn't available, and this option is non-nil,
ERC should automatically attempt to connect with another nickname.

You can manually set another nickname with the /NICK command."
182 183 184 185 186 187
  :group 'erc
  :type 'boolean)

(defcustom erc-user-full-name nil
  "User full name.

Miles Bader's avatar
Miles Bader committed
188 189
This can be either a string or a function to call.

190 191 192
See function `erc-compute-full-name' for more details on connection
parameters and authentication."
  :group 'erc
Miles Bader's avatar
Miles Bader committed
193
  :type '(choice (const :tag "No name" nil)
Kelvin White's avatar
Kelvin White committed
194 195
                 (string :tag "Name")
                 (function :tag "Get from function"))
196
  :set (lambda (sym val)
Kelvin White's avatar
Kelvin White committed
197
         (set sym (if (functionp val) (funcall val) val))))
198

199 200 201 202 203
(defcustom erc-rename-buffers nil
  "When this is set to t, buffers will be renamed to network name if available"
  :group 'erc
  :type 'boolean)

204
(defvar erc-password nil
Miles Bader's avatar
Miles Bader committed
205 206 207
  "Password to use when authenticating to an IRC server.
It is not strictly necessary to provide this, since ERC will
prompt you for it.")
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

(defcustom erc-user-mode nil
  "Initial user modes to be set after a connection is established."
  :group 'erc
  :type '(choice (const nil) string function))


(defcustom erc-prompt-for-password t
  "Asks before using the default password, or whether to enter a new one."
  :group 'erc
  :type 'boolean)

(defcustom erc-warn-about-blank-lines t
  "Warn the user if they attempt to send a blank line."
  :group 'erc
  :type 'boolean)

(defcustom erc-send-whitespace-lines nil
  "If set to non-nil, send lines consisting of only whitespace."
  :group 'erc
  :type 'boolean)

(defcustom erc-hide-prompt nil
  "If non-nil, do not display the prompt for commands.

\(A command is any input starting with a '/').

See also the variables `erc-prompt' and `erc-command-indicator'."
  :group 'erc-display
  :type 'boolean)

;; tunable GUI stuff

(defcustom erc-show-my-nick t
  "If non-nil, display one's own nickname when sending a message.

If non-nil, \"<nickname>\" will be shown.
If nil, only \"> \" will be shown."
  :group 'erc-display
  :type 'boolean)

(define-widget 'erc-message-type 'set
  "A set of standard IRC Message types."
  :args '((const "JOIN")
Kelvin White's avatar
Kelvin White committed
252 253 254 255 256 257
          (const "KICK")
          (const "NICK")
          (const "PART")
          (const "QUIT")
          (const "MODE")
          (repeat :inline t :tag "Others" (string :tag "IRC Message Type"))))
258 259

(defcustom erc-hide-list nil
260
  "List of IRC type messages to hide.
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
A typical value would be '(\"JOIN\" \"PART\" \"QUIT\")."
  :group 'erc-ignore
  :type 'erc-message-type)

(defvar erc-session-password nil
  "The password used for the current session.")
(make-variable-buffer-local 'erc-session-password)

(defcustom erc-disconnected-hook nil
  "Run this hook with arguments (NICK IP REASON) when disconnected.
This happens before automatic reconnection.  Note, that
`erc-server-QUIT-functions' might not be run when we disconnect,
simply because we do not necessarily receive the QUIT event."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-complete-functions nil
  "These functions get called when the user hits TAB in ERC.
Each function in turn is called until one returns non-nil to
indicate it has handled the input."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-join-hook nil
  "Hook run when we join a channel.  Hook functions are called
without arguments, with the current buffer set to the buffer of
the new channel.

See also `erc-server-JOIN-functions', `erc-part-hook'."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-quit-hook nil
  "Hook run when processing a quit command directed at our nick.

The hook receives one argument, the current PROCESS.
See also `erc-server-QUIT-functions' and `erc-disconnected-hook'."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-part-hook nil
  "Hook run when processing a PART message directed at our nick.

The hook receives one argument, the current BUFFER.
See also `erc-server-QUIT-functions', `erc-quit-hook' and
`erc-disconnected-hook'."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-kick-hook nil
  "Hook run when processing a KICK message directed at our nick.

The hook receives one argument, the current BUFFER.
See also `erc-server-PART-functions' and `erc-part-hook'."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-nick-changed-functions nil
  "List of functions run when your nick was successfully changed.

Each function should accept two arguments, NEW-NICK and OLD-NICK."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-connect-pre-hook '(erc-initialize-log-marker)
  "Hook called just before `erc' calls `erc-connect'.
Miles Bader's avatar
Miles Bader committed
327
Functions are passed a buffer as the first argument."
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
  :group 'erc-hooks
  :type 'hook)


(defvar erc-channel-users nil
  "A hash table of members in the current channel, which
associates nicknames with cons cells of the form:
\(USER . MEMBER-DATA) where USER is a pointer to an
erc-server-user struct, and MEMBER-DATA is a pointer to an
erc-channel-user struct.")
(make-variable-buffer-local 'erc-channel-users)

(defvar erc-server-users nil
  "A hash table of users on the current server, which associates
nicknames with erc-server-user struct instances.")
(make-variable-buffer-local 'erc-server-users)

(defun erc-downcase (string)
  "Convert STRING to IRC standard conforming downcase."
  (let ((s (downcase string))
Kelvin White's avatar
Kelvin White committed
348 349 350 351
        (c '((?\[ . ?\{)
             (?\] . ?\})
             (?\\ . ?\|)
             (?~  . ?^))))
352 353
    (save-match-data
      (while (string-match "[]\\[~]" s)
Kelvin White's avatar
Kelvin White committed
354 355
        (aset s (match-beginning 0)
              (cdr (assq (aref s (match-beginning 0)) c)))))
356 357
    s))

Miles Bader's avatar
Miles Bader committed
358 359 360
(defmacro erc-with-server-buffer (&rest body)
  "Execute BODY in the current ERC server buffer.
If no server buffer exists, return nil."
361
  (declare (indent 0) (debug (body)))
Miles Bader's avatar
Miles Bader committed
362 363 364
  (let ((buffer (make-symbol "buffer")))
    `(let ((,buffer (erc-server-buffer)))
       (when (buffer-live-p ,buffer)
Kelvin White's avatar
Kelvin White committed
365 366
         (with-current-buffer ,buffer
           ,@body)))))
Miles Bader's avatar
Miles Bader committed
367

368
(cl-defstruct (erc-server-user (:type vector) :named)
369 370 371 372 373 374 375 376 377
  ;; User data
  nickname host login full-name info
  ;; Buffers
  ;;
  ;; This is an alist of the form (BUFFER . CHANNEL-DATA), where
  ;; CHANNEL-DATA is either nil or an erc-channel-user struct.
  (buffers nil)
  )

378
(cl-defstruct (erc-channel-user (:type vector) :named)
Kelvin White's avatar
Kelvin White committed
379
  voice halfop op admin owner
380 381 382 383 384 385 386
  ;; Last message time (in the form of the return value of
  ;; (current-time)
  ;;
  ;; This is useful for ordered name completion.
  (last-message-time nil))

(defsubst erc-get-channel-user (nick)
387
  "Find the (USER . CHANNEL-DATA) element corresponding to NICK
388 389 390 391
in the current buffer's `erc-channel-users' hash table."
  (gethash (erc-downcase nick) erc-channel-users))

(defsubst erc-get-server-user (nick)
392
  "Find the USER corresponding to NICK in the current server's
393
`erc-server-users' hash table."
Miles Bader's avatar
Miles Bader committed
394
  (erc-with-server-buffer
395 396 397 398 399 400
    (gethash (erc-downcase nick) erc-server-users)))

(defsubst erc-add-server-user (nick user)
  "This function is for internal use only.

Adds USER with nickname NICK to the `erc-server-users' hash table."
Miles Bader's avatar
Miles Bader committed
401
  (erc-with-server-buffer
402 403 404 405 406 407 408 409 410 411
    (puthash (erc-downcase nick) user erc-server-users)))

(defsubst erc-remove-server-user (nick)
  "This function is for internal use only.

Removes the user with nickname NICK from the `erc-server-users'
hash table.  This user is not removed from the
`erc-channel-users' lists of other buffers.

See also: `erc-remove-user'."
Miles Bader's avatar
Miles Bader committed
412
  (erc-with-server-buffer
413 414 415 416 417 418 419 420 421 422
    (remhash (erc-downcase nick) erc-server-users)))

(defun erc-change-user-nickname (user new-nick)
  "This function is for internal use only.

Changes the nickname of USER to NEW-NICK in the
`erc-server-users' hash table.  The `erc-channel-users' lists of
other buffers are also changed."
  (let ((nick (erc-server-user-nickname user)))
    (setf (erc-server-user-nickname user) new-nick)
Miles Bader's avatar
Miles Bader committed
423
    (erc-with-server-buffer
424 425 426 427
      (remhash (erc-downcase nick) erc-server-users)
      (puthash (erc-downcase new-nick) user erc-server-users))
    (dolist (buf (erc-server-user-buffers user))
      (if (buffer-live-p buf)
Kelvin White's avatar
Kelvin White committed
428 429 430 431 432
          (with-current-buffer buf
            (let ((cdata (erc-get-channel-user nick)))
              (remhash (erc-downcase nick) erc-channel-users)
              (puthash (erc-downcase new-nick) cdata
                       erc-channel-users)))))))
433 434 435 436 437 438 439 440 441 442 443 444 445

(defun erc-remove-channel-user (nick)
  "This function is for internal use only.

Removes the user with nickname NICK from the `erc-channel-users'
list for this channel.  If this user is not in the
`erc-channel-users' list of any other buffers, the user is also
removed from the server's `erc-server-users' list.

See also: `erc-remove-server-user' and `erc-remove-user'."
  (let ((channel-data (erc-get-channel-user nick)))
    (when channel-data
      (let ((user (car channel-data)))
Kelvin White's avatar
Kelvin White committed
446 447 448 449 450 451
        (setf (erc-server-user-buffers user)
              (delq (current-buffer)
                    (erc-server-user-buffers user)))
        (remhash (erc-downcase nick) erc-channel-users)
        (if (null (erc-server-user-buffers user))
            (erc-remove-server-user nick))))))
452 453 454 455 456 457 458 459 460 461 462 463

(defun erc-remove-user (nick)
  "This function is for internal use only.

Removes the user with nickname NICK from the `erc-server-users'
list as well as from all `erc-channel-users' lists.

See also: `erc-remove-server-user' and
`erc-remove-channel-user'."
  (let ((user (erc-get-server-user nick)))
    (when user
      (let ((buffers (erc-server-user-buffers user)))
Kelvin White's avatar
Kelvin White committed
464 465 466 467 468
        (dolist (buf buffers)
          (if (buffer-live-p buf)
              (with-current-buffer buf
                (remhash (erc-downcase nick) erc-channel-users)
                (run-hooks 'erc-channel-members-changed-hook)))))
469 470 471 472 473 474 475 476
      (erc-remove-server-user nick))))

(defun erc-remove-channel-users ()
  "This function is for internal use only.

Removes all users in the current channel.  This is called by
`erc-server-PART' and `erc-server-QUIT'."
  (when (and erc-server-connected
Kelvin White's avatar
Kelvin White committed
477 478
             (erc-server-process-alive)
             (hash-table-p erc-channel-users))
479
    (maphash (lambda (nick _cdata)
Kelvin White's avatar
Kelvin White committed
480 481
               (erc-remove-channel-user nick))
             erc-channel-users)
482 483
    (clrhash erc-channel-users)))

Kelvin White's avatar
Kelvin White committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
(defsubst erc-channel-user-owner-p (nick)
  "Return non-nil if NICK is an owner of the current channel."
  (and nick
       (hash-table-p erc-channel-users)
       (let ((cdata (erc-get-channel-user nick)))
         (and cdata (cdr cdata)
              (erc-channel-user-owner (cdr cdata))))))

(defsubst erc-channel-user-admin-p (nick)
  "Return non-nil if NICK is an admin in the current channel."
  (and nick
       (hash-table-p erc-channel-users)
       (let ((cdata (erc-get-channel-user nick)))
         (and cdata (cdr cdata)
              (erc-channel-user-admin (cdr cdata))))))

500
(defsubst erc-channel-user-op-p (nick)
Kelvin White's avatar
Kelvin White committed
501 502 503 504 505 506 507 508 509
  "Return non-nil if NICK is an operator in the current channel."
  (and nick
       (hash-table-p erc-channel-users)
       (let ((cdata (erc-get-channel-user nick)))
         (and cdata (cdr cdata)
              (erc-channel-user-op (cdr cdata))))))

(defsubst erc-channel-user-halfop-p (nick)
  "Return non-nil if NICK is a half-operator in the current channel."
510 511 512
  (and nick
       (hash-table-p erc-channel-users)
       (let ((cdata (erc-get-channel-user nick)))
Kelvin White's avatar
Kelvin White committed
513 514
         (and cdata (cdr cdata)
              (erc-channel-user-halfop (cdr cdata))))))
515 516

(defsubst erc-channel-user-voice-p (nick)
Kelvin White's avatar
Kelvin White committed
517
  "Return non-nil if NICK has voice in the current channel."
518 519 520
  (and nick
       (hash-table-p erc-channel-users)
       (let ((cdata (erc-get-channel-user nick)))
Kelvin White's avatar
Kelvin White committed
521 522
         (and cdata (cdr cdata)
              (erc-channel-user-voice (cdr cdata))))))
523 524

(defun erc-get-channel-user-list ()
525
  "Return a list of users in the current channel.  Each element
526
of the list is of the form (USER . CHANNEL-DATA), where USER is
527
an erc-server-user struct, and CHANNEL-DATA is either nil or an
528 529 530 531 532
erc-channel-user struct.

See also: `erc-sort-channel-users-by-activity'"
  (let (users)
    (if (hash-table-p erc-channel-users)
Kelvin White's avatar
Kelvin White committed
533 534 535
        (maphash (lambda (_nick cdata)
                   (setq users (cons cdata users)))
                 erc-channel-users))
536 537 538
    users))

(defun erc-get-server-nickname-list ()
539
  "Return a list of known nicknames on the current server."
Miles Bader's avatar
Miles Bader committed
540 541 542
  (erc-with-server-buffer
    (let (nicks)
      (when (hash-table-p erc-server-users)
Kelvin White's avatar
Kelvin White committed
543 544 545 546 547 548
        (maphash (lambda (_n user)
                   (setq nicks
                         (cons (erc-server-user-nickname user)
                               nicks)))
                 erc-server-users)
        nicks))))
549 550

(defun erc-get-channel-nickname-list ()
551
  "Return a list of known nicknames on the current channel."
552 553
  (let (nicks)
    (when (hash-table-p erc-channel-users)
554
      (maphash (lambda (_n cdata)
Kelvin White's avatar
Kelvin White committed
555 556 557 558
                 (setq nicks
                       (cons (erc-server-user-nickname (car cdata))
                             nicks)))
               erc-channel-users)
559 560 561
      nicks)))

(defun erc-get-server-nickname-alist ()
562
  "Return an alist of known nicknames on the current server."
Miles Bader's avatar
Miles Bader committed
563 564 565
  (erc-with-server-buffer
    (let (nicks)
      (when (hash-table-p erc-server-users)
Kelvin White's avatar
Kelvin White committed
566 567 568 569 570 571
        (maphash (lambda (_n user)
                   (setq nicks
                         (cons (cons (erc-server-user-nickname user) nil)
                               nicks)))
                 erc-server-users)
        nicks))))
572 573

(defun erc-get-channel-nickname-alist ()
574
  "Return an alist of known nicknames on the current channel."
575 576
  (let (nicks)
    (when (hash-table-p erc-channel-users)
577
      (maphash (lambda (_n cdata)
Kelvin White's avatar
Kelvin White committed
578 579 580 581
                 (setq nicks
                       (cons (cons (erc-server-user-nickname (car cdata)) nil)
                             nicks)))
               erc-channel-users)
582 583 584
      nicks)))

(defun erc-sort-channel-users-by-activity (list)
585 586
  "Sort LIST such that users which have spoken most recently are listed first.
LIST must be of the form (USER . CHANNEL-DATA).
587 588 589

See also: `erc-get-channel-user-list'."
  (sort list
Kelvin White's avatar
Kelvin White committed
590 591 592 593 594 595 596
        (lambda (x y)
          (when (and (cdr x) (cdr y))
            (let ((tx (erc-channel-user-last-message-time (cdr x)))
                  (ty (erc-channel-user-last-message-time (cdr y))))
              (and tx
                   (or (not ty)
                       (time-less-p ty tx))))))))
597 598 599 600 601 602 603

(defun erc-sort-channel-users-alphabetically (list)
  "Sort LIST so that users' nicknames are in alphabetical order.
LIST must be of the form (USER . CHANNEL-DATA).

See also: `erc-get-channel-user-list'."
  (sort list
Kelvin White's avatar
Kelvin White committed
604 605 606 607 608 609 610
        (lambda (x y)
          (when (and (cdr x) (cdr y))
            (let ((nickx (downcase (erc-server-user-nickname (car x))))
                  (nicky (downcase (erc-server-user-nickname (car y)))))
              (and nickx
                   (or (not nicky)
                       (string-lessp nickx nicky))))))))
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

(defvar erc-channel-topic nil
  "A topic string for the channel.  Should only be used in channel-buffers.")
(make-variable-buffer-local 'erc-channel-topic)

(defvar erc-channel-modes nil
  "List of strings representing channel modes.
E.g. '(\"i\" \"m\" \"s\" \"b Quake!*@*\")
\(not sure the ban list will be here, but why not)")
(make-variable-buffer-local 'erc-channel-modes)

(defvar erc-insert-marker nil
  "The place where insertion of new text in erc buffers should happen.")
(make-variable-buffer-local 'erc-insert-marker)

(defvar erc-input-marker nil
  "The marker where input should be inserted.")
(make-variable-buffer-local 'erc-input-marker)

(defun erc-string-no-properties (string)
  "Return a copy of STRING will all text-properties removed."
  (let ((newstring (copy-sequence string)))
    (set-text-properties 0 (length newstring) nil newstring)
    newstring))

(defcustom erc-prompt "ERC>"
  "Prompt used by ERC.  Trailing whitespace is not required."
  :group 'erc-display
  :type '(choice string function))

(defun erc-prompt ()
  "Return the input prompt as a string.

See also the variable `erc-prompt'."
  (let ((prompt (if (functionp erc-prompt)
Kelvin White's avatar
Kelvin White committed
646 647
                    (funcall erc-prompt)
                  erc-prompt)))
648
    (if (> (length prompt) 0)
Kelvin White's avatar
Kelvin White committed
649
        (concat prompt " ")
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
      prompt)))

(defcustom erc-command-indicator nil
  "Indicator used by ERC for showing commands.

If non-nil, this will be used in the ERC buffer to indicate
commands (i.e., input starting with a '/').

If nil, the prompt will be constructed from the variable `erc-prompt'."
  :group 'erc-display
  :type '(choice (const nil) string function))

(defun erc-command-indicator ()
  "Return the command indicator prompt as a string.

This only has any meaning if the variable `erc-command-indicator' is non-nil."
  (and erc-command-indicator
       (let ((prompt (if (functionp erc-command-indicator)
Kelvin White's avatar
Kelvin White committed
668 669 670 671 672
                         (funcall erc-command-indicator)
                       erc-command-indicator)))
         (if (> (length prompt) 0)
             (concat prompt " ")
           prompt))))
673 674

(defcustom erc-notice-prefix "*** "
675
  "Prefix for all notices."
676 677 678 679
  :group 'erc-display
  :type 'string)

(defcustom erc-notice-highlight-type 'all
680
  "Determines how to highlight notices.
681 682 683 684 685 686 687 688 689 690
See `erc-notice-prefix'.

The following values are allowed:

    'prefix - highlight notice prefix only
    'all    - highlight the entire notice

Any other value disables notice's highlighting altogether."
  :group 'erc-display
  :type '(choice (const :tag "highlight notice prefix only" prefix)
Kelvin White's avatar
Kelvin White committed
691 692
                 (const :tag "highlight the entire notice" all)
                 (const :tag "don't highlight notices at all" nil)))
693 694

(defcustom erc-echo-notice-hook nil
695 696
  "List of functions to call to echo a private notice.
Each function is called with four arguments, the string
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
to display, the parsed server message, the target buffer (or
nil), and the sender.  The functions are called in order, until a
function evaluates to non-nil.  These hooks are called after
those specified in `erc-echo-notice-always-hook'.

See also: `erc-echo-notice-always-hook',
`erc-echo-notice-in-default-buffer',
`erc-echo-notice-in-target-buffer',
`erc-echo-notice-in-minibuffer',
`erc-echo-notice-in-server-buffer',
`erc-echo-notice-in-active-non-server-buffer',
`erc-echo-notice-in-active-buffer',
`erc-echo-notice-in-user-buffers',
`erc-echo-notice-in-user-and-target-buffers',
`erc-echo-notice-in-first-user-buffer'"
  :group 'erc-hooks
  :type 'hook
  :options '(erc-echo-notice-in-default-buffer
Kelvin White's avatar
Kelvin White committed
715 716 717 718 719 720 721 722
             erc-echo-notice-in-target-buffer
             erc-echo-notice-in-minibuffer
             erc-echo-notice-in-server-buffer
             erc-echo-notice-in-active-non-server-buffer
             erc-echo-notice-in-active-buffer
             erc-echo-notice-in-user-buffers
             erc-echo-notice-in-user-and-target-buffers
             erc-echo-notice-in-first-user-buffer))
723 724 725

(defcustom erc-echo-notice-always-hook
  '(erc-echo-notice-in-default-buffer)
726 727
  "List of functions to call to echo a private notice.
Each function is called with four arguments, the string
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
to display, the parsed server message, the target buffer (or
nil), and the sender.  The functions are called in order, and all
functions are called.  These hooks are called before those
specified in `erc-echo-notice-hook'.

See also: `erc-echo-notice-hook',
`erc-echo-notice-in-default-buffer',
`erc-echo-notice-in-target-buffer',
`erc-echo-notice-in-minibuffer',
`erc-echo-notice-in-server-buffer',
`erc-echo-notice-in-active-non-server-buffer',
`erc-echo-notice-in-active-buffer',
`erc-echo-notice-in-user-buffers',
`erc-echo-notice-in-user-and-target-buffers',
`erc-echo-notice-in-first-user-buffer'"
  :group 'erc-hooks
  :type 'hook
  :options '(erc-echo-notice-in-default-buffer
Kelvin White's avatar
Kelvin White committed
746 747 748 749 750 751 752 753
             erc-echo-notice-in-target-buffer
             erc-echo-notice-in-minibuffer
             erc-echo-notice-in-server-buffer
             erc-echo-notice-in-active-non-server-buffer
             erc-echo-notice-in-active-buffer
             erc-echo-notice-in-user-buffers
             erc-echo-notice-in-user-and-target-buffers
             erc-echo-notice-in-first-user-buffer))
754 755 756 757

;; other tunable parameters

(defcustom erc-whowas-on-nosuchnick nil
758
  "If non-nil, do a whowas on a nick if no such nick."
759 760 761 762
  :group 'erc
  :type 'boolean)

(defcustom erc-verbose-server-ping nil
763
  "If non-nil, show every time you get a PING or PONG from the server."
764 765 766 767
  :group 'erc-paranoia
  :type 'boolean)

(defcustom erc-public-away-p nil
768
  "Let others know you are back when you are no longer marked away.
769 770 771 772 773 774 775 776
This happens in this form:
* <nick> is back (gone for <time>)

Many consider it impolite to do so automatically."
  :group 'erc
  :type 'boolean)

(defcustom erc-away-nickname nil
777
  "The nickname to take when you are marked as being away."
778 779
  :group 'erc
  :type '(choice (const nil)
Kelvin White's avatar
Kelvin White committed
780
                 string))
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800

(defcustom erc-paranoid nil
  "If non-nil, then all incoming CTCP requests will be shown."
  :group 'erc-paranoia
  :type 'boolean)

(defcustom erc-disable-ctcp-replies nil
  "Disable replies to CTCP requests that require a reply.
If non-nil, then all incoming CTCP requests that normally require
an automatic reply (like VERSION or PING) will be ignored.  Good to
set if some hacker is trying to flood you away."
  :group 'erc-paranoia
  :type 'boolean)

(defcustom erc-anonymous-login t
  "Be paranoid, don't give away your machine name."
  :group 'erc-paranoia
  :type 'boolean)

(defcustom erc-prompt-for-channel-key nil
801
  "Prompt for channel key when using `erc-join-channel' interactively."
802 803 804 805 806 807 808 809
  :group 'erc
  :type 'boolean)

(defcustom erc-email-userid "user"
  "Use this as your email user ID."
  :group 'erc
  :type 'string)

Miles Bader's avatar
Miles Bader committed
810 811 812 813 814
(defcustom erc-system-name nil
  "Use this as the name of your system.
If nil, ERC will call `system-name' to get this information."
  :group 'erc
  :type '(choice (const :tag "Default system name" nil)
Kelvin White's avatar
Kelvin White committed
815
                 string))
Miles Bader's avatar
Miles Bader committed
816

817
(defcustom erc-ignore-list nil
818
  "List of regexps matching user identifiers to ignore.
819 820 821 822 823 824 825 826 827

A user identifier has the form \"nick!login@host\".  If an
identifier matches, the message from the person will not be
processed."
  :group 'erc-ignore
  :type '(repeat regexp))
(make-variable-buffer-local 'erc-ignore-list)

(defcustom erc-ignore-reply-list nil
828
  "List of regexps matching user identifiers to ignore completely.
829 830 831 832 833 834 835 836 837 838 839 840

This differs from `erc-ignore-list' in that it also ignores any
messages directed at the user.

A user identifier has the form \"nick!login@host\".

If an identifier matches, or a message is addressed to a nick
whose identifier matches, the message will not be processed.

CAVEAT: ERC doesn't know about the user and host of anyone who
was already in the channel when you joined, but never said
anything, so it won't be able to match the user and host of those
841
people.  You can update the ERC internal info using /WHO *."
842 843 844 845
  :group 'erc-ignore
  :type '(repeat regexp))

(defvar erc-flood-protect t
846
  "If non-nil, flood protection is enabled.
847 848 849 850 851 852 853 854 855
Flooding is sending too much information to the server in too
short of an interval, which may cause the server to terminate the
connection.

See `erc-server-flood-margin' for other flood-related parameters.")

;; Script parameters

(defcustom erc-startup-file-list
856
  (list (concat erc-user-emacs-directory ".ercrc.el")
Kelvin White's avatar
Kelvin White committed
857 858
        (concat erc-user-emacs-directory ".ercrc")
        "~/.ercrc.el" "~/.ercrc" ".ercrc.el" ".ercrc")
859 860 861
  "List of files to try for a startup script.
The first existent and readable one will get executed.

862
If the filename ends with `.el' it is presumed to be an Emacs Lisp
863
script and it gets (load)ed.  Otherwise it is treated as a bunch of
864
regular IRC commands."
865 866 867 868 869 870 871 872 873 874 875
  :group 'erc-scripts
  :type '(repeat file))

(defcustom erc-script-path nil
  "List of directories to look for a script in /load command.
The script is first searched in the current directory, then in each
directory in the list."
  :group 'erc-scripts
  :type '(repeat directory))

(defcustom erc-script-echo t
876
  "If non-nil, echo the IRC script commands locally."
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
  :group 'erc-scripts
  :type 'boolean)

(defvar erc-last-saved-position nil
  "A marker containing the position the current buffer was last saved at.")
(make-variable-buffer-local 'erc-last-saved-position)

(defcustom erc-kill-buffer-on-part nil
  "Kill the channel buffer on PART.
This variable should probably stay nil, as ERC can reuse buffers if
you rejoin them later."
  :group 'erc-quit-and-part
  :type 'boolean)

(defcustom erc-kill-queries-on-quit nil
  "Kill all query (also channel) buffers of this server on QUIT.
See the variable `erc-kill-buffer-on-part' for details."
  :group 'erc-quit-and-part
  :type 'boolean)

(defcustom erc-kill-server-buffer-on-quit nil
  "Kill the server buffer of the process on QUIT."
  :group 'erc-quit-and-part
  :type 'boolean)

(defcustom erc-quit-reason-various-alist nil
  "Alist of possible arguments to the /quit command.

Each element has the form:
  (REGEXP RESULT)

If REGEXP matches the argument to /quit, then its relevant RESULT
will be used.  RESULT may be either a string, or a function.  If
a function, it should return the quit message as a string.

If no elements match, then the empty string is used.

As an example:
  (setq erc-quit-reason-various-alist
916
      '((\"xmms\" dme:now-playing)
Kelvin White's avatar
Kelvin White committed
917 918 919
        (\"version\" erc-quit-reason-normal)
        (\"home\" \"Gone home !\")
        (\"^$\" \"Default Reason\")))
920 921
If the user types \"/quit home\", then \"Gone home !\" will be used
as the quit message."
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
  :group 'erc-quit-and-part
  :type '(repeat (list regexp (choice (string) (function)))))

(defcustom erc-part-reason-various-alist nil
  "Alist of possible arguments to the /part command.

Each element has the form:
  (REGEXP RESULT)

If REGEXP matches the argument to /part, then its relevant RESULT
will be used.  RESULT may be either a string, or a function.  If
a function, it should return the part message as a string.

If no elements match, then the empty string is used.

As an example:
  (setq erc-part-reason-various-alist
939
      '((\"xmms\" dme:now-playing)
Kelvin White's avatar
Kelvin White committed
940 941 942
        (\"version\" erc-part-reason-normal)
        (\"home\" \"Gone home !\")
        (\"^$\" \"Default Reason\")))
943 944
If the user types \"/part home\", then \"Gone home !\" will be used
as the part message."
945 946 947 948
  :group 'erc-quit-and-part
  :type '(repeat (list regexp (choice (string) (function)))))

(defcustom erc-quit-reason 'erc-quit-reason-normal
949
  "A function which returns the reason for quitting.
950 951 952 953 954

The function is passed a single argument, the string typed by the
user after \"/quit\"."
  :group 'erc-quit-and-part
  :type '(choice (const erc-quit-reason-normal)
Kelvin White's avatar
Kelvin White committed
955 956
                 (const erc-quit-reason-various)
                 (symbol)))
957 958 959 960 961 962 963 964

(defcustom erc-part-reason 'erc-part-reason-normal
  "A function which returns the reason for parting a channel.

The function is passed a single argument, the string typed by the
user after \"/PART\"."
  :group 'erc-quit-and-part
  :type '(choice (const erc-part-reason-normal)
Kelvin White's avatar
Kelvin White committed
965 966
                 (const erc-part-reason-various)
                 (symbol)))
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049

(defvar erc-grab-buffer-name "*erc-grab*"
  "The name of the buffer created by `erc-grab-region'.")

;; variables available for IRC scripts

(defvar erc-user-information "ERC User"
  "USER_INFORMATION IRC variable.")

;; Hooks

(defgroup erc-hooks nil
  "Hook variables for fancy customizations of ERC."
  :group 'erc)

(defcustom erc-mode-hook nil
  "Hook run after `erc-mode' setup is finished."
  :group 'erc-hooks
  :type 'hook
  :options '(erc-add-scroll-to-bottom))

(defcustom erc-timer-hook nil
  "Put functions which should get called more or less periodically here.
The idea is that servers always play ping pong with the client, and so there
is no need for any idle-timer games with Emacs."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-insert-pre-hook nil
  "Hook called first when some text is inserted through `erc-display-line'.
It gets called with one argument, STRING.
To be able to modify the inserted text, use `erc-insert-modify-hook' instead.
Filtering functions can set `erc-insert-this' to nil to avoid
display of that particular string at all."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-send-pre-hook nil
  "Hook called first when some text is sent through `erc-send-current-line'.
It gets called with one argument, STRING.

To change the text that will be sent, set the variable STR which is
used in `erc-send-current-line'.

To change the text inserted into the buffer without changing the text
that will be sent, use `erc-send-modify-hook' instead.

Filtering functions can set `erc-send-this' to nil to avoid sending of
that particular string at all and `erc-insert-this' to prevent
inserting that particular string into the buffer.

Note that it's useless to set `erc-send-this' to nil and
`erc-insert-this' to t.  ERC is sane enough to not insert the text
anyway."
  :group 'erc-hooks
  :type 'hook)

(defvar erc-insert-this t
  "Insert the text into the target buffer or not.
Functions on `erc-insert-pre-hook' can set this variable to nil
if they wish to avoid insertion of a particular string.")

(defvar erc-send-this t
  "Send the text to the target or not.
Functions on `erc-send-pre-hook' can set this variable to nil
if they wish to avoid sending of a particular string.")

(defcustom erc-insert-modify-hook ()
  "Insertion hook for functions that will change the text's appearance.
This hook is called just after `erc-insert-pre-hook' when the value
of `erc-insert-this' is t.
While this hook is run, narrowing is in effect and `current-buffer' is
the buffer where the text got inserted.  One possible value to add here
is `erc-fill'."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-insert-post-hook nil
  "This hook is called just after `erc-insert-modify-hook'.
At this point, all modifications from prior hook functions are done."
  :group 'erc-hooks
  :type 'hook
  :options '(erc-truncate-buffer
Kelvin White's avatar
Kelvin White committed
1050 1051
             erc-make-read-only
             erc-save-buffer-in-logs))
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072

(defcustom erc-send-modify-hook nil
  "Sending hook for functions that will change the text's appearance.
This hook is called just after `erc-send-pre-hook' when the values
of `erc-send-this' and `erc-insert-this' are both t.
While this hook is run, narrowing is in effect and `current-buffer' is
the buffer where the text got inserted.

Note that no function in this hook can change the appearance of the
text that is sent.  Only changing the sent text's appearance on the
sending user's screen is possible.  One possible value to add here
is `erc-fill'."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-send-post-hook nil
  "This hook is called just after `erc-send-modify-hook'.
At this point, all modifications from prior hook functions are done.
NOTE: The functions on this hook are called _before_ sending a command
to the server.

1073
This function is called with narrowing, ala `erc-send-modify-hook'."
1074 1075 1076 1077 1078
  :group 'erc-hooks
  :type 'hook
  :options '(erc-make-read-only))

(defcustom erc-send-completed-hook
1079
  (when (fboundp 'emacspeak-auditory-icon)
1080
    (list (byte-compile
Kelvin White's avatar
Kelvin White committed
1081 1082
           (lambda (_str)
             (emacspeak-auditory-icon 'select-object)))))
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
  "Hook called after a message has been parsed by ERC.

The single argument to the functions is the unmodified string
which the local user typed."
  :group 'erc-hooks
  :type 'hook)
;; mode-specific tables

(defvar erc-mode-syntax-table
  (let ((syntax-table (make-syntax-table)))
    (modify-syntax-entry ?\" ".   " syntax-table)
    (modify-syntax-entry ?\\ ".   " syntax-table)
    (modify-syntax-entry ?' "w   " syntax-table)
    ;; Make dabbrev-expand useful for nick names
    (modify-syntax-entry ?< "." syntax-table)
    (modify-syntax-entry ?> "." syntax-table)
    syntax-table)
  "Syntax table used while in ERC mode.")

(defvar erc-mode-abbrev-table nil
  "Abbrev table used while in ERC mode.")
(define-abbrev-table 'erc-mode-abbrev-table ())

(defvar erc-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "\C-m" 'erc-send-current-line)
    (define-key map "\C-a" 'erc-bol)
    (define-key map [home] 'erc-bol)
    (define-key map "\C-c\C-a" 'erc-bol)
    (define-key map "\C-c\C-b" 'erc-iswitchb)
    (define-key map "\C-c\C-c" 'erc-toggle-interpret-controls)
    (define-key map "\C-c\C-d" 'erc-input-action)
    (define-key map "\C-c\C-e" 'erc-toggle-ctcp-autoresponse)
    (define-key map "\C-c\C-f" 'erc-toggle-flood-control)
    (define-key map "\C-c\C-i" 'erc-invite-only-mode)
    (define-key map "\C-c\C-j" 'erc-join-channel)
    (define-key map "\C-c\C-n" 'erc-channel-names)
    (define-key map "\C-c\C-o" 'erc-get-channel-mode-from-keypress)
    (define-key map "\C-c\C-p" 'erc-part-from-channel)
    (define-key map "\C-c\C-q" 'erc-quit-server)
    (define-key map "\C-c\C-r" 'erc-remove-text-properties-region)
    (define-key map "\C-c\C-t" 'erc-set-topic)
    (define-key map "\C-c\C-u" 'erc-kill-input)
Michael Olson's avatar
Michael Olson committed
1126
    (define-key map "\C-c\C-x" 'erc-quit-server)
1127
    (define-key map "\M-\t" 'ispell-complete-word)
1128
    (define-key map "\t" 'completion-at-point)
1129 1130 1131

    ;; Suppress `font-lock-fontify-block' key binding since it
    ;; destroys face properties.
1132
    (define-key map [remap font-lock-fontify-block] 'undefined)
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154

    map)
  "ERC keymap.")

;; Faces

; Honestly, I have a horrible sense of color and the "defaults" below
; are supposed to be really bad. But colors ARE required in IRC to
; convey different parts of conversation. If you think you know better
; defaults - send them to me.

;; Now colors are a bit nicer, at least to my eyes.
;; You may still want to change them to better fit your background.-- S.B.

(defgroup erc-faces nil
  "Faces for ERC."
  :group 'erc)

(defface erc-default-face '((t))
  "ERC default face."
  :group 'erc-faces)

Kelvin White's avatar
Kelvin White committed
1155 1156 1157 1158 1159 1160 1161 1162
(defface erc-nick-prefix-face '((t :inherit erc-nick-default-face :weight bold))
  "ERC face used for user mode prefix."
  :group 'erc-faces)

(defface erc-my-nick-prefix-face '((t :inherit erc-nick-default-face :weight bold))
  "ERC face used for my user mode prefix."
  :group 'erc-faces)

1163
(defface erc-direct-msg-face '((t :foreground "IndianRed"))
1164 1165 1166
  "ERC face used for messages you receive in the main erc buffer."
  :group 'erc-faces)

1167
(defface erc-header-line
1168
  '((t :foreground "grey20" :background "grey90"))
1169 1170 1171 1172 1173
  "ERC face used for the header line.

This will only be used if `erc-header-line-face-method' is non-nil."
  :group 'erc-faces)

1174
(defface erc-input-face '((t :foreground "brown"))
1175 1176 1177 1178
  "ERC face used for your input."
  :group 'erc-faces)

(defface erc-prompt-face
1179
  '((t :weight bold :foreground "Black" :background "lightBlue2"))
1180 1181 1182 1183
  "ERC face for the prompt."
  :group 'erc-faces)

(defface erc-command-indicator-face
1184
  '((t :weight bold))
1185 1186 1187 1188
  "ERC face for the command indicator.
See the variable `erc-command-indicator'."
  :group 'erc-faces)

1189
(defface erc-notice-face
1190 1191 1192
  '((default :weight bold)
    (((class color) (min-colors 88)) :foreground "SlateBlue")
    (t :foreground "blue"))
1193 1194 1195
  "ERC face for notices."
  :group 'erc-faces)

1196
(defface erc-action-face '((t :weight bold))
1197 1198 1199
  "ERC face for actions generated by /ME."
  :group 'erc-faces)

1200
(defface erc-error-face '((t :foreground "red"))
1201 1202 1203
  "ERC face for errors."
  :group 'erc-faces)

Miles Bader's avatar
Miles Bader committed
1204
;; same default color as `erc-input-face'
1205
(defface erc-my-nick-face '((t :weight bold :foreground "brown"))
Miles Bader's avatar
Miles Bader committed
1206 1207 1208 1209
  "ERC face for your current nickname in messages sent by you.
See also `erc-show-my-nick'."
  :group 'erc-faces)

1210
(defface erc-nick-default-face '((t :weight bold))
1211 1212 1213
  "ERC nickname default face."
  :group 'erc-faces)

1214
(defface erc-nick-msg-face '((t :weight bold :foreground "IndianRed"))
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
  "ERC nickname face for private messages."
  :group 'erc-faces)

;; Debugging support

(defvar erc-log-p nil
  "When set to t, generate debug messages in a separate debug buffer.")

(defvar erc-debug-log-file (expand-file-name "ERC.debug")
  "Debug log file name.")

(defvar erc-dbuf nil)
(make-variable-buffer-local 'erc-dbuf)

(defmacro define-erc-module (name alias doc enable-body disable-body
Kelvin White's avatar
Kelvin White committed
1230
                                  &optional local-p)
1231 1232 1233 1234 1235 1236 1237
  "Define a new minor mode using ERC conventions.
Symbol NAME is the name of the module.
Symbol ALIAS is the alias to use, or nil.
DOC is the documentation string to use for the minor mode.
ENABLE-BODY is a list of expressions used to enable the mode.
DISABLE-BODY is a list of expressions used to disable the mode.
If LOCAL-P is non-nil, the mode will be created as a buffer-local
1238
mode, rather than a global one.
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249

This will define a minor mode called erc-NAME-mode, possibly
an alias erc-ALIAS-mode, as well as the helper functions
erc-NAME-enable, and erc-NAME-disable.

Example:

  ;;;###autoload (autoload 'erc-replace-mode \"erc-replace\")
  (define-erc-module replace nil
    \"This mode replaces incoming text according to `erc-replace-alist'.\"
    ((add-hook 'erc-insert-modify-hook
Kelvin White's avatar
Kelvin White committed
1250
               'erc-replace-insert))
1251
    ((remove-hook 'erc-insert-modify-hook
Kelvin White's avatar
Kelvin White committed
1252
                  'erc-replace-insert)))"
1253
  (declare (doc-string 3))
1254
  (let* ((sn (symbol-name name))
Kelvin White's avatar
Kelvin White committed
1255 1256 1257 1258
         (mode (intern (format "erc-%s-mode" (downcase sn))))
         (group (intern (format "erc-%s" (downcase sn))))
         (enable (intern (format "erc-%s-enable" (downcase sn))))
         (disable (intern (format "erc-%s-disable" (downcase sn)))))
1259 1260
    `(progn
       (erc-define-minor-mode
Kelvin White's avatar
Kelvin White committed
1261 1262
        ,mode
        ,(format "Toggle ERC %S mode.
Chong Yidong's avatar
Chong Yidong committed
1263 1264 1265
With a prefix argument ARG, enable %s if ARG is positive,
and disable it otherwise.  If called from Lisp, enable the mode
if ARG is omitted or nil.
1266
%s" name name doc)
Kelvin White's avatar
Kelvin White committed
1267 1268 1269 1270 1271
        nil nil nil
        :global ,(not local-p) :group (quote ,group)
        (if ,mode
            (,enable)
          (,disable)))
1272
       (defun ,enable ()
Kelvin White's avatar
Kelvin White committed
1273 1274 1275 1276 1277 1278
         ,(format "Enable ERC %S mode."
                  name)
         (interactive)
         (add-to-list 'erc-modules (quote ,name))
         (setq ,mode t)
         ,@enable-body)
1279
       (defun ,disable ()
Kelvin White's avatar
Kelvin White committed
1280 1281 1282 1283 1284 1285
         ,(format "Disable ERC %S mode."
                  name)
         (interactive)
         (setq erc-modules (delq (quote ,name) erc-modules))
         (setq ,mode nil)
         ,@disable-body)
1286
       ,(when (and alias (not (eq name alias)))
Kelvin White's avatar
Kelvin White committed
1287 1288 1289 1290 1291 1292 1293
          `(defalias
             (quote
              ,(intern
                (format "erc-%s-mode"
                        (downcase (symbol-name alias)))))
             (quote
              ,mode)))
Miles Bader's avatar
Miles Bader committed
1294 1295 1296 1297
       ;; For find-function and find-variable.
       (put ',mode    'definition-name ',name)
       (put ',enable  'definition-name ',name)
       (put ',disable 'definition-name ',name))))
1298

1299 1300
(defun erc-once-with-server-event (event f)
  "Run function F the next time EVENT occurs in the `current-buffer'.
1301 1302 1303

You should make sure that `current-buffer' is a server buffer.

1304 1305 1306
This function temporarily adds a function to EVENT's hook to call F with
two arguments (`proc' and `parsed').  After F is called, the function is
removed from EVENT's hook.  F should return either nil
1307
or t, where nil indicates that the other functions on EVENT's hook
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
should be run too, and t indicates that other functions should
not be run.

Please be sure to use this function in server-buffers.  In
channel-buffers it may not work at all, as it uses the LOCAL
argument of `add-hook' and `remove-hook' to ensure multiserver
capabilities."
  (unless (erc-server-buffer-p)
    (error
     "You should only run `erc-once-with-server-event' in a server buffer"))
Miles Bader's avatar
Miles Bader committed
1318
  (let ((fun (make-symbol "fun"))
Kelvin White's avatar
Kelvin White committed
1319
        (hook (erc-get-hook event)))
1320 1321
    (put fun 'erc-original-buffer (current-buffer))
    (fset fun (lambda (proc parsed)
Kelvin White's avatar
Kelvin White committed
1322 1323 1324 1325
                (with-current-buffer (get fun 'erc-original-buffer)
                  (remove-hook hook fun t))
                (fmakunbound fun)
                (funcall f proc parsed)))
1326 1327
    (add-hook hook fun nil t)
    fun))
1328

1329
(defsubst erc-log (string)
1330
  "Logs STRING if logging is on (see `erc-log-p')."
1331 1332
  (when erc-log-p
    (erc-log-aux string)))
1333 1334 1335 1336 1337 1338 1339 1340 1341

(defun erc-server-buffer ()
  "Return the server buffer for the current buffer's process.
The buffer-local variable `erc-server-process' is used to find
the process buffer."
  (and (erc-server-buffer-live-p)
       (process-buffer erc-server-process)))

(defun erc-server-buffer-live-p ()
Miles Bader's avatar
Miles Bader committed
1342
  "Return t if the server buffer has not been killed."
1343 1344 1345 1346 1347 1348 1349 1350 1351
  (and (processp erc-server-process)
       (buffer-live-p (process-buffer erc-server-process))))

(defun erc-server-buffer-p (&optional buffer)
  "Return non-nil if argument BUFFER is an ERC server buffer.

If BUFFER is nil, the current buffer is used."
  (with-current-buffer (or buffer (current-buffer))
    (and (eq major-mode 'erc-mode)
Kelvin White's avatar
Kelvin White committed
1352
         (null (erc-default-target)))))
1353

1354
(defun erc-open-server-buffer-p (&optional buffer)
Miles Bader's avatar
Miles Bader committed
1355 1356 1357 1358
  "Return non-nil if argument BUFFER is an ERC server buffer that
has an open IRC process.

If BUFFER is nil, the current buffer is used."
1359 1360
  (and (erc-server-buffer-p buffer)
       (erc-server-process-alive buffer)))
Miles Bader's avatar
Miles Bader committed
1361

1362 1363 1364 1365 1366 1367
(defun erc-query-buffer-p (&optional buffer)
  "Return non-nil if BUFFER is an ERC query buffer.
If BUFFER is nil, the current buffer is used."
  (with-current-buffer (or buffer (current-buffer))
    (let ((target (erc-default-target)))
      (and (eq major-mode 'erc-mode)
Kelvin White's avatar
Kelvin White committed
1368 1369
           target
           (not (memq (aref target 0) '(?# ?& ?+ ?!)))))))
1370 1371 1372 1373

(defun erc-ison-p (nick)
  "Return non-nil if NICK is online."
  (interactive "sNick: ")
Miles Bader's avatar
Miles Bader committed
1374
  (erc-with-server-buffer
1375 1376 1377
    (let ((erc-online-p 'unknown))
      (erc-once-with-server-event
       303
1378
       (lambda (_proc parsed)
Kelvin White's avatar
Kelvin White committed
1379 1380 1381
         (let ((ison (split-string (aref parsed 3))))
           (setq erc-online-p (car (erc-member-ignore-case nick ison)))
           t)))
1382 1383
      (erc-server-send (format "ISON %s" nick))
      (while (eq erc-online-p 'unknown) (accept-process-output))
1384
      (if (called-interactively-p 'interactive)
Kelvin White's avatar
Kelvin White committed
1385 1386 1387 1388
          (message "%s is %sonline"
                   (or erc-online-p nick)
                   (if erc-online-p "" "not "))
        erc-online-p))))