erc.el 223 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
;; Maintainer: emacs-devel@gnu.org
13
;; Keywords: IRC, chat, client, Internet
14
;; Version: 5.3
15 16 17

;; This file is part of GNU Emacs.

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

;; 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
29
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
30 31 32

;;; Commentary:

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

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

Miles Bader's avatar
Miles Bader committed
39 40 41 42
;; 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.
43 44 45 46 47 48 49 50 51 52 53 54 55 56

;; 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
57
;; M-x erc RET
58 59
;;
;; 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
60
;; the ERC menu.
61 62 63 64 65 66

;;; History:
;;

;;; Code:

Michael Olson's avatar
Michael Olson committed
67
(defconst erc-version-string "Version 5.3"
68 69
  "ERC version.  This is used by function `erc-version'.")

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

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

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

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

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
(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

129 130 131 132 133
(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")
134

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

;; tunable connection and authentication parameters

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

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

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

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

Miles Bader's avatar
Miles Bader committed
160
This can be either a string, or a list of strings.
161 162 163 164 165 166
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
167
  :type '(choice (const :tag "None" nil)
168
		 (string :tag "Nickname")
Miles Bader's avatar
Miles Bader committed
169
		 (repeat (string :tag "Nickname"))))
170 171

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

Miles Bader's avatar
Miles Bader committed
176 177 178 179 180
(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."
181 182 183 184 185 186
  :group 'erc
  :type 'boolean)

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

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

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

(defvar erc-password nil
Miles Bader's avatar
Miles Bader committed
199 200 201
  "Password to use when authenticating to an IRC server.
It is not strictly necessary to provide this, since ERC will
prompt you for it.")
202 203 204 205 206 207 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 252 253

(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")
	  (const "KICK")
	  (const "NICK")
	  (const "PART")
	  (const "QUIT")
	  (const "MODE")
	  (repeat :inline t :tag "Others" (string :tag "IRC Message Type"))))

(defcustom erc-hide-list nil
254
  "List of IRC type messages to hide.
255 256 257 258 259 260 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
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
321
Functions are passed a buffer as the first argument."
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
  :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))
	(c '((?\[ . ?\{)
	     (?\] . ?\})
	     (?\\ . ?\|)
	     (?~  . ?^))))
    (save-match-data
      (while (string-match "[]\\[~]" s)
	(aset s (match-beginning 0)
	      (cdr (assq (aref s (match-beginning 0)) c)))))
    s))

Miles Bader's avatar
Miles Bader committed
352 353 354
(defmacro erc-with-server-buffer (&rest body)
  "Execute BODY in the current ERC server buffer.
If no server buffer exists, return nil."
355
  (declare (indent 0) (debug (body)))
Miles Bader's avatar
Miles Bader committed
356 357 358 359 360 361
  (let ((buffer (make-symbol "buffer")))
    `(let ((,buffer (erc-server-buffer)))
       (when (buffer-live-p ,buffer)
	 (with-current-buffer ,buffer
	   ,@body)))))

362
(cl-defstruct (erc-server-user (:type vector) :named)
363 364 365 366 367 368 369 370 371
  ;; 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)
  )

372
(cl-defstruct (erc-channel-user (:type vector) :named)
373 374 375 376 377 378 379 380
  op voice
  ;; 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)
381
  "Find the (USER . CHANNEL-DATA) element corresponding to NICK
382 383 384 385
in the current buffer's `erc-channel-users' hash table."
  (gethash (erc-downcase nick) erc-channel-users))

(defsubst erc-get-server-user (nick)
386
  "Find the USER corresponding to NICK in the current server's
387
`erc-server-users' hash table."
Miles Bader's avatar
Miles Bader committed
388
  (erc-with-server-buffer
389 390 391 392 393 394
    (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
395
  (erc-with-server-buffer
396 397 398 399 400 401 402 403 404 405
    (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
406
  (erc-with-server-buffer
407 408 409 410 411 412 413 414 415 416
    (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
417
    (erc-with-server-buffer
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
      (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)
	  (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)))))))

(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)))
	(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))))))

(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)))
	(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)))))
      (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
	     (erc-server-process-alive)
	     (hash-table-p erc-channel-users))
473
    (maphash (lambda (nick _cdata)
474 475 476 477 478
	       (erc-remove-channel-user nick))
	     erc-channel-users)
    (clrhash erc-channel-users)))

(defsubst erc-channel-user-op-p (nick)
479
  "Return t if NICK is an operator in the current channel."
480 481 482 483 484 485 486
  (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-voice-p (nick)
487
  "Return t if NICK has voice in the current channel."
488 489 490 491 492 493 494
  (and nick
       (hash-table-p erc-channel-users)
       (let ((cdata (erc-get-channel-user nick)))
	 (and cdata (cdr cdata)
	      (erc-channel-user-voice (cdr cdata))))))

(defun erc-get-channel-user-list ()
495
  "Return a list of users in the current channel.  Each element
496
of the list is of the form (USER . CHANNEL-DATA), where USER is
497
an erc-server-user struct, and CHANNEL-DATA is either nil or an
498 499 500 501 502
erc-channel-user struct.

See also: `erc-sort-channel-users-by-activity'"
  (let (users)
    (if (hash-table-p erc-channel-users)
503
      (maphash (lambda (_nick cdata)
504 505 506 507 508
		 (setq users (cons cdata users)))
	       erc-channel-users))
    users))

(defun erc-get-server-nickname-list ()
509
  "Return a list of known nicknames on the current server."
Miles Bader's avatar
Miles Bader committed
510 511 512
  (erc-with-server-buffer
    (let (nicks)
      (when (hash-table-p erc-server-users)
513
	(maphash (lambda (_n user)
Miles Bader's avatar
Miles Bader committed
514 515 516 517 518
		   (setq nicks
			 (cons (erc-server-user-nickname user)
			       nicks)))
		 erc-server-users)
	nicks))))
519 520

(defun erc-get-channel-nickname-list ()
521
  "Return a list of known nicknames on the current channel."
522 523
  (let (nicks)
    (when (hash-table-p erc-channel-users)
524
      (maphash (lambda (_n cdata)
525 526 527 528 529 530 531
		 (setq nicks
		       (cons (erc-server-user-nickname (car cdata))
			     nicks)))
	       erc-channel-users)
      nicks)))

(defun erc-get-server-nickname-alist ()
532
  "Return an alist of known nicknames on the current server."
Miles Bader's avatar
Miles Bader committed
533 534 535
  (erc-with-server-buffer
    (let (nicks)
      (when (hash-table-p erc-server-users)
536
	(maphash (lambda (_n user)
Miles Bader's avatar
Miles Bader committed
537 538 539 540 541
		   (setq nicks
			 (cons (cons (erc-server-user-nickname user) nil)
			       nicks)))
		 erc-server-users)
	nicks))))
542 543

(defun erc-get-channel-nickname-alist ()
544
  "Return an alist of known nicknames on the current channel."
545 546
  (let (nicks)
    (when (hash-table-p erc-channel-users)
547
      (maphash (lambda (_n cdata)
548 549 550 551 552 553 554
		 (setq nicks
		       (cons (cons (erc-server-user-nickname (car cdata)) nil)
			     nicks)))
	       erc-channel-users)
      nicks)))

(defun erc-sort-channel-users-by-activity (list)
555 556
  "Sort LIST such that users which have spoken most recently are listed first.
LIST must be of the form (USER . CHANNEL-DATA).
557 558 559 560

See also: `erc-get-channel-user-list'."
  (sort list
	(lambda (x y)
561
	  (when (and (cdr x) (cdr y))
562 563
	    (let ((tx (erc-channel-user-last-message-time (cdr x)))
		  (ty (erc-channel-user-last-message-time (cdr y))))
564 565 566
	      (and tx
		   (or (not ty)
		       (time-less-p ty tx))))))))
567 568 569 570 571 572 573 574

(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
	(lambda (x y)
575
	  (when (and (cdr x) (cdr y))
576 577
	    (let ((nickx (downcase (erc-server-user-nickname (car x))))
		  (nicky (downcase (erc-server-user-nickname (car y)))))
578 579 580
	      (and nickx
		   (or (not nicky)
		       (string-lessp nickx nicky))))))))
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 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

(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)
		    (funcall erc-prompt)
		  erc-prompt)))
    (if (> (length prompt) 0)
	(concat prompt " ")
      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)
			 (funcall erc-command-indicator)
			 erc-command-indicator)))
	 (if (> (length prompt) 0)
	     (concat prompt " ")
	     prompt))))

(defcustom erc-notice-prefix "*** "
645
  "Prefix for all notices."
646 647 648 649
  :group 'erc-display
  :type 'string)

(defcustom erc-notice-highlight-type 'all
650
  "Determines how to highlight notices.
651 652 653 654 655 656 657 658 659 660 661 662 663 664
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)
		 (const :tag "highlight the entire notice" all)
		 (const :tag "don't highlight notices at all" nil)))

(defcustom erc-echo-notice-hook nil
665 666
  "List of functions to call to echo a private notice.
Each function is called with four arguments, the string
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
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
	     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))

(defcustom erc-echo-notice-always-hook
  '(erc-echo-notice-in-default-buffer)
696 697
  "List of functions to call to echo a private notice.
Each function is called with four arguments, the string
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
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
	     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))

;; other tunable parameters

(defcustom erc-whowas-on-nosuchnick nil
728
  "If non-nil, do a whowas on a nick if no such nick."
729 730 731 732
  :group 'erc
  :type 'boolean)

(defcustom erc-verbose-server-ping nil
733
  "If non-nil, show every time you get a PING or PONG from the server."
734 735 736 737
  :group 'erc-paranoia
  :type 'boolean)

(defcustom erc-public-away-p nil
738
  "Let others know you are back when you are no longer marked away.
739 740 741 742 743 744 745 746
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
747
  "The nickname to take when you are marked as being away."
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
  :group 'erc
  :type '(choice (const nil)
		 string))

(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
771
  "Prompt for channel key when using `erc-join-channel' interactively."
772 773 774 775 776 777 778 779
  :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
780 781 782 783 784 785 786
(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)
		 string))

787
(defcustom erc-ignore-list nil
788
  "List of regexps matching user identifiers to ignore.
789 790 791 792 793 794 795 796 797

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
798
  "List of regexps matching user identifiers to ignore completely.
799 800 801 802 803 804 805 806 807 808 809 810

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
811
people.  You can update the ERC internal info using /WHO *."
812 813 814 815
  :group 'erc-ignore
  :type '(repeat regexp))

(defvar erc-flood-protect t
816
  "If non-nil, flood protection is enabled.
817 818 819 820 821 822 823 824 825
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
826 827 828
  (list (concat erc-user-emacs-directory ".ercrc.el")
	(concat erc-user-emacs-directory ".ercrc")
	"~/.ercrc.el" "~/.ercrc" ".ercrc.el" ".ercrc")
829 830 831
  "List of files to try for a startup script.
The first existent and readable one will get executed.

832
If the filename ends with `.el' it is presumed to be an Emacs Lisp
833
script and it gets (load)ed.  Otherwise it is treated as a bunch of
834
regular IRC commands."
835 836 837 838 839 840 841 842 843 844 845
  :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
846
  "If non-nil, echo the IRC script commands locally."
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
  :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
886
      '((\"xmms\" dme:now-playing)
887 888
	(\"version\" erc-quit-reason-normal)
	(\"home\" \"Gone home !\")
Miles Bader's avatar
Miles Bader committed
889
	(\"^$\" \"Default Reason\")))
890 891
If the user types \"/quit home\", then \"Gone home !\" will be used
as the quit message."
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
  :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
909
      '((\"xmms\" dme:now-playing)
910 911
	(\"version\" erc-part-reason-normal)
	(\"home\" \"Gone home !\")
Miles Bader's avatar
Miles Bader committed
912
	(\"^$\" \"Default Reason\")))
913 914
If the user types \"/part home\", then \"Gone home !\" will be used
as the part message."
915 916 917 918
  :group 'erc-quit-and-part
  :type '(repeat (list regexp (choice (string) (function)))))

(defcustom erc-quit-reason 'erc-quit-reason-normal
919
  "A function which returns the reason for quitting.
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977

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)
		 (const erc-quit-reason-various)
		 (symbol)))

(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)
		 (const erc-part-reason-various)
		 (symbol)))

(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.

978
To change the text that will be sent, set the variable `str' which is
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
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
	     erc-make-read-only
	     erc-save-buffer-in-logs))

(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.

1043
This function is called with narrowing, ala `erc-send-modify-hook'."
1044 1045 1046 1047 1048
  :group 'erc-hooks
  :type 'hook
  :options '(erc-make-read-only))

(defcustom erc-send-completed-hook
1049
  (when (fboundp 'emacspeak-auditory-icon)
1050
    (list (byte-compile
1051
	   (lambda (_str)
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
	     (emacspeak-auditory-icon 'select-object)))))
  "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
1096
    (define-key map "\C-c\C-x" 'erc-quit-server)
1097
    (define-key map "\M-\t" 'ispell-complete-word)
1098
    (define-key map "\t" 'completion-at-point)
1099 1100 1101

    ;; Suppress `font-lock-fontify-block' key binding since it
    ;; destroys face properties.
1102
    (define-key map [remap font-lock-fontify-block] 'undefined)
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124

    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)

1125
(defface erc-direct-msg-face '((t :foreground "IndianRed"))
1126 1127 1128
  "ERC face used for messages you receive in the main erc buffer."
  :group 'erc-faces)

1129
(defface erc-header-line
1130
  '((t :foreground "grey20" :background "grey90"))
1131 1132 1133 1134 1135
  "ERC face used for the header line.

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

1136
(defface erc-input-face '((t :foreground "brown"))
1137 1138 1139 1140
  "ERC face used for your input."
  :group 'erc-faces)

(defface erc-prompt-face
1141
  '((t :weight bold :foreground "Black" :background "lightBlue2"))
1142 1143 1144 1145
  "ERC face for the prompt."
  :group 'erc-faces)

(defface erc-command-indicator-face
1146
  '((t :weight bold))
1147 1148 1149 1150
  "ERC face for the command indicator.
See the variable `erc-command-indicator'."
  :group 'erc-faces)

1151
(defface erc-notice-face
1152 1153 1154
  '((default :weight bold)
    (((class color) (min-colors 88)) :foreground "SlateBlue")
    (t :foreground "blue"))
1155 1156 1157
  "ERC face for notices."
  :group 'erc-faces)

1158
(defface erc-action-face '((t :weight bold))
1159 1160 1161
  "ERC face for actions generated by /ME."
  :group 'erc-faces)

1162
(defface erc-error-face '((t :foreground "red"))
1163 1164 1165
  "ERC face for errors."
  :group 'erc-faces)

Miles Bader's avatar
Miles Bader committed
1166
;; same default color as `erc-input-face'
1167
(defface erc-my-nick-face '((t :weight bold :foreground "brown"))
Miles Bader's avatar
Miles Bader committed
1168 1169 1170 1171
  "ERC face for your current nickname in messages sent by you.
See also `erc-show-my-nick'."
  :group 'erc-faces)

1172
(defface erc-nick-default-face '((t :weight bold))
1173 1174 1175
  "ERC nickname default face."
  :group 'erc-faces)

1176
(defface erc-nick-msg-face '((t :weight bold :foreground "IndianRed"))
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
  "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
			     &optional local-p)
  "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
1200
mode, rather than a global one.
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214

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
	       'erc-replace-insert))
    ((remove-hook 'erc-insert-modify-hook
		  'erc-replace-insert)))"
1215
  (declare (doc-string 3))
1216 1217 1218 1219 1220 1221 1222 1223 1224
  (let* ((sn (symbol-name name))
	 (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)))))
    `(progn
       (erc-define-minor-mode
	,mode
	,(format "Toggle ERC %S mode.
Chong Yidong's avatar
Chong Yidong committed
1225 1226 1227
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.
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
%s" name name doc)
	nil nil nil
	:global ,(not local-p) :group (quote ,group)
	(if ,mode
	    (,enable)
	  (,disable)))
       (defun ,enable ()
	 ,(format "Enable ERC %S mode."
		  name)
	 (interactive)
	 (add-to-list 'erc-modules (quote ,name))
	 (setq ,mode t)
	 ,@enable-body)
       (defun ,disable ()
	 ,(format "Disable ERC %S mode."
		  name)
	 (interactive)
	 (setq erc-modules (delq (quote ,name) erc-modules))
	 (setq ,mode nil)
	 ,@disable-body)
       ,(when (and alias (not (eq name alias)))
	  `(defalias
	     (quote
	      ,(intern
		(format "erc-%s-mode"
			(downcase (symbol-name alias)))))
	     (quote
Miles Bader's avatar
Miles Bader committed
1255 1256 1257 1258 1259
	      ,mode)))
       ;; For find-function and find-variable.
       (put ',mode    'definition-name ',name)
       (put ',enable  'definition-name ',name)
       (put ',disable 'definition-name ',name))))
1260

1261 1262
(defun erc-once-with-server-event (event f)
  "Run function F the next time EVENT occurs in the `current-buffer'.
1263 1264 1265

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

1266 1267 1268
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
1269
or t, where nil indicates that the other functions on EVENT's hook
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
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
1280
  (let ((fun (make-symbol "fun"))
1281
	(hook (erc-get-hook event)))
1282 1283 1284 1285 1286 1287 1288 1289
    (put fun 'erc-original-buffer (current-buffer))
    (fset fun (lambda (proc parsed)
		(with-current-buffer (get fun 'erc-original-buffer)
		  (remove-hook hook fun t))
		(fmakunbound fun)
		(funcall f proc parsed)))
    (add-hook hook fun nil t)
    fun))
1290

1291
(defsubst erc-log (string)
1292
  "Logs STRING if logging is on (see `erc-log-p')."
1293 1294
  (when erc-log-p
    (erc-log-aux string)))
1295 1296 1297 1298 1299 1300 1301 1302 1303

(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
1304
  "Return t if the server buffer has not been killed."
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
  (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)
	 (null (erc-default-target)))))

1316
(defun erc-open-server-buffer-p (&optional buffer)
Miles Bader's avatar
Miles Bader committed
1317 1318 1319 1320
  "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."
1321 1322
  (and (erc-server-buffer-p buffer)
       (erc-server-process-alive buffer)))
Miles Bader's avatar
Miles Bader committed
1323

1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
(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)
	   target
	   (not (memq (aref target 0) '(?# ?& ?+ ?!)))))))

(defun erc-ison-p (nick)
  "Return non-nil if NICK is online."
  (interactive "sNick: ")
Miles Bader's avatar
Miles Bader committed
1336
  (erc-with-server-buffer
1337 1338 1339
    (let ((erc-online-p 'unknown))
      (erc-once-with-server-event
       303
1340 1341 1342 1343
       (lambda (_proc parsed)
	 (let ((ison (split-string (aref parsed 3))))
	   (setq erc-online-p (car (erc-member-ignore-case nick ison)))
	   t)))
1344 1345
      (erc-server-send (format "ISON %s" nick))
      (while (eq erc-online-p 'unknown) (accept-process-output))
1346
      (if (called-interactively-p 'interactive)
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
	  (message "%s is %sonline"
		   (or erc-online-p nick)
		   (if erc-online-p "" "not "))
	erc-online-p))))

(defun erc-log-aux (string)
  "Do the debug logging of STRING."
  (let ((cb (current-buffer))
	(point 1)
	(was-eob nil)
	(session-buffer (erc-server-buffer)))
    (if session-buffer
	(progn
	  (set-buffer session-buffer)
	  (if (not (and erc-dbuf (bufferp erc-dbuf) (buffer-live-p erc-dbuf)))
	      (progn
		(setq erc-dbuf (get-buffer-create
				(concat "*ERC-DEBUG: "
					erc-session-server "*")))))
	  (set-buffer erc-dbuf)
	  (setq point (point))
	  (setq was-eob (eobp))
	  (goto-char (point-max))
	  (insert (concat "** " string "\n"))
	  (if was-eob (goto-char (point-max))
	    (goto-char point))
	  (set-buffer cb))
      (message "ERC: ** %s" string))))

;; Last active buffer, to print server messages in the right place

(defvar erc-active-buffer nil
  "The current active buffer, the one where the user typed the last command.
Defaults to the server buffer, and should only be set in the
1381
server buffer.")
1382 1383 1384 1385 1386
(make-variable-buffer-local 'erc-active-buffer)

(defun erc-active-buffer ()
  "Return the value of `erc-active-buffer' for the current server.
Defaults to the server buffer."
Miles Bader's avatar
Miles Bader committed
1387
  (erc-with-server-buffer
Miles Bader's avatar
Miles Bader committed
1388
    (if (buffer-live-p erc-active-buffer)
Miles Bader's avatar
Miles Bader committed
1389 1390
	erc-active-buffer
      (setq erc-active-buffer (current-buffer)))))
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400

(defun erc-set-active-buffer (buffer)
  "Set the value of `erc-active-buffer' to BUFFER."
  (cond ((erc-server-buffer)
	 (with-current-buffer (erc-server-buffer)
	   (setq erc-active-buffer buffer)))
	(t (setq erc-active-buffer buffer))))

;; Mode activation routines

1401 1402 1403
(define-derived-mode erc-mode fundamental-mode "ERC"
  "Major mode for Emacs IRC."
  (setq local-abbrev-table erc-mode-abbrev-table)
1404 1405 1406 1407 1408 1409
  (when (boundp 'next-line-add-newlines)
    (set (make-local-variable 'next-line-add-newlines) nil))
  (setq line-move-ignore-invisible t)
  (set (make-local-variable 'paragraph-separate)
       (concat "\C-l\\|\\(^" (regexp-quote (erc-prompt)) "\\)"))
  (set (make-local-variable 'paragraph-start)
1410 1411
       (concat "\\(" (regexp-quote (erc-prompt)) "\\)"))
  (add-hook 'completion-at-point-functions 'erc-complete-word-at-point nil t))
1412 1413 1414 1415 1416 1417

;; activation

(defconst erc-default-server "irc.freenode.net"
  "IRC server to use if it cannot be detected otherwise.")

1418
(defconst erc-default-port 6667
1419 1420 1421
  "IRC port to use if it cannot be detected otherwise.")

(defcustom erc-join-buffer 'buffer
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
  "Determines how to display a newly created IRC buffer.

The available choices are:

  'window          - in another window,
  'window-noselect - in another window, but don't select that one,
  'frame           - in another frame,
  'bury            - bury it in a new buffer,
  'buffer          - in place of the current buffer,
  any other value  - in place of the current buffer."
1432
  :group 'erc-buffers
1433 1434 1435 1436 1437 1438
  :type '(choice (const :tag "Split window and select" window)
		 (const :tag "Split window, don't select" window-noselect)
		 (const :tag "New frame" frame)
		 (const :tag "Bury in new buffer" bury)
		 (const :tag "Use current buffer" buffer)
		 (const :tag "Use current buffer" t)))
1439 1440

(defcustom erc-frame-alist nil
1441
  "Alist of frame parameters for creating erc frames.
1442
A value of nil means to use `default-frame-alist'."
1443 1444 1445 1446 1447 1448
  :group 'erc-buffers
  :type '(repeat (cons :format "%v"
		       (symbol :tag "Parameter")
		       (sexp :tag "Value"))))

(defcustom erc-frame-dedicated-flag nil
1449
  "Non-nil means the erc frames are dedicated to that buffer.
1450 1451 1452 1453
This only has effect when `erc-join-buffer' is set to `frame'."
  :group 'erc-buffers
  :type 'boolean)

1454
(defcustom erc-reuse-frames t
1455
  "Determines whether new frames are always created.
1456 1457 1458 1459 1460 1461
Non-nil means that a new frame is not created to display an ERC
buffer if there is already a window displaying it.  This only has
effect when `erc-join-buffer' is set to `frame'."
  :group 'erc-buffers
  :type 'boolean)

1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
(defun erc-channel-p (channel)
  "Return non-nil if CHANNEL seems to be an IRC channel name."
  (cond ((stringp channel)
	 (memq (aref channel 0) '(?# ?& ?+ ?!)))
	((and (bufferp channel) (buffer-live-p channel))
	 (with-current-buffer channel
	   (erc-channel-p (erc-default-target))))
	(t nil)))

(defcustom erc-reuse-buffers t
1472
  "If nil, create new buffers on joining a channel/query.
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
If non-nil, a new buffer will only be created when you join
channels with same names on different servers, or have query buffers
open with nicks of the same name on different servers.  Otherwise,
the existing buffers will be reused."
  :group 'erc-buffers
  :type 'boolean)

(defun erc-normalize-port (port)
  "Normalize the port specification PORT to integer form.
PORT may be an integer, a string or a symbol.  If it is a string or a
symbol, it may have these values:
* irc         -> 194
* ircs        -> 994
* ircd        -> 6667
* ircd-dalnet -> 7000"
  (cond
   ((symbolp port)
    (erc-normalize-port (symbol-name port)))
   ((stringp port)
    (let ((port-nr (string-to-number port)))
      (cond
       ((> port-nr 0)
	port-nr)
       ((string-equal port "irc")
	194)
       ((string-equal port "ircs")
	994)
       ((string-equal port "ircd")
	6667)
       ((string-equal port "ircd-dalnet")
	7000)
       (t
	nil))))
   ((numberp port)
    port)
   (t
    nil)))

(defun erc-port-equal (a b)
  "Check whether ports A and B are equal."
  (= (erc-normalize-port a) (erc-normalize-port b)))

1515
(defun erc-generate-new-buffer-name (server port target)
1516 1517
  "Create a new buffer name based on the arguments."
  (when (numberp port) (setq port (number-to-string port)))
1518 1519 1520 1521 1522 1523 1524
  (let ((buf-name (or target
                      (or (let ((name (concat server ":" port)))
                            (when (> (length name) 1)
                              name))
                          ;; This fallback should in fact never happen
                          "*erc-server-buffer*")))
        buffer-name)
1525 1526 1527
    ;; Reuse existing buffers, but not if the buffer is a connected server
    ;; buffer and not if its associated with a different server than the
    ;; current ERC buffer.
1528 1529
    ;; if buf-name is taken by a different connection (or by something !erc)
    ;; then see if "buf-name/server" meets the same criteria