erc.el 218 KB
Newer Older
1 2 3
;; erc.el --- An Emacs Internet Relay Chat client

;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 6 7 8 9 10 11 12

;; 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)
Miles Bader's avatar
Miles Bader committed
13
;; Maintainer: Michael Olson (mwolson@gnu.org)
14 15 16 17
;; Keywords: IRC, chat, client, Internet

;; 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 70 71 72 73 74 75 76
  "ERC version.  This is used by function `erc-version'.")

(eval-when-compile (require 'cl))
(require 'font-lock)
(require 'pp)
(require 'thingatpt)
(require 'erc-compat)

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

(defgroup erc nil
  "Emacs Internet Relay Chat client."
Miles Bader's avatar
Miles Bader committed
82
  :link '(url-link "http://www.emacswiki.org/cgi-bin/wiki/ERC")
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  :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)

(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

(if (fboundp 'defvaralias)
    (progn
      (defvaralias 'erc-announced-server-name 'erc-server-announced-name)
      (erc-make-obsolete-variable 'erc-announced-server-name
				  'erc-server-announced-name
				  "ERC 5.1")
      (defvaralias 'erc-process 'erc-server-process)
      (erc-make-obsolete-variable 'erc-process 'erc-server-process "ERC 5.1")
      (defvaralias 'erc-default-coding-system 'erc-server-coding-system)
      (erc-make-obsolete-variable 'erc-default-coding-system
				  'erc-server-coding-system
				  "ERC 5.1"))
  (message (concat "ERC: The function `defvaralias' is not bound.  See the "
		   "NEWS file for variable name changes since ERC 5.0.4.")))

(defalias 'erc-send-command 'erc-server-send)
(erc-make-obsolete 'erc-send-command 'erc-server-send "ERC 5.1")

;; tunable connection and authentication parameters

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

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

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

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

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

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

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

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

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

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

(defvar erc-password nil
Miles Bader's avatar
Miles Bader committed
203 204 205
  "Password to use when authenticating to an IRC server.
It is not strictly necessary to provide this, since ERC will
prompt you for it.")
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 254 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 321 322 323 324

(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
  "*List of IRC type messages to hide.
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
325
Functions are passed a buffer as the first argument."
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 352 353 354 355
  :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
356 357 358 359 360 361 362 363 364 365 366
(defmacro erc-with-server-buffer (&rest body)
  "Execute BODY in the current ERC server buffer.
If no server buffer exists, return nil."
  (let ((buffer (make-symbol "buffer")))
    `(let ((,buffer (erc-server-buffer)))
       (when (buffer-live-p ,buffer)
	 (with-current-buffer ,buffer
	   ,@body)))))
(put 'erc-with-server-buffer 'lisp-indent-function 0)
(put 'erc-with-server-buffer 'edebug-form-spec '(body))

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
(defstruct (erc-server-user (:type vector) :named)
  ;; 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)
  )

(defstruct (erc-channel-user (:type vector) :named)
  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)
  "Finds the (USER . CHANNEL-DATA) element corresponding to NICK
in the current buffer's `erc-channel-users' hash table."
  (gethash (erc-downcase nick) erc-channel-users))

(defsubst erc-get-server-user (nick)
  "Finds the USER corresponding to NICK in the current server's
`erc-server-users' hash table."
Miles Bader's avatar
Miles Bader committed
393
  (erc-with-server-buffer
394 395 396 397 398 399
    (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
400
  (erc-with-server-buffer
401 402 403 404 405 406 407 408 409 410
    (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
411
  (erc-with-server-buffer
412 413 414 415 416 417 418 419 420 421
    (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
422
    (erc-with-server-buffer
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 473 474 475 476 477 478 479 480 481 482 483
      (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))
    (maphash (lambda (nick cdata)
	       (erc-remove-channel-user nick))
	     erc-channel-users)
    (clrhash erc-channel-users)))

(defsubst erc-channel-user-op-p (nick)
484
  "Return t if NICK is an operator in the current channel."
485 486 487 488 489 490 491
  (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)
492
  "Return t if NICK has voice in the current channel."
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
  (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 ()
  "Returns a list of users in the current channel.  Each element
of the list is of the form (USER . CHANNEL-DATA), where USER is
an erc-server-user struct, and CHANNEL-DATA is either `nil' or an
erc-channel-user struct.

See also: `erc-sort-channel-users-by-activity'"
  (let (users)
    (if (hash-table-p erc-channel-users)
      (maphash (lambda (nick cdata)
		 (setq users (cons cdata users)))
	       erc-channel-users))
    users))

(defun erc-get-server-nickname-list ()
  "Returns a list of known nicknames on the current server."
Miles Bader's avatar
Miles Bader committed
515 516 517 518 519 520 521 522 523
  (erc-with-server-buffer
    (let (nicks)
      (when (hash-table-p erc-server-users)
	(maphash (lambda (n user)
		   (setq nicks
			 (cons (erc-server-user-nickname user)
			       nicks)))
		 erc-server-users)
	nicks))))
524 525 526 527 528 529 530 531 532 533 534 535 536 537

(defun erc-get-channel-nickname-list ()
  "Returns a list of known nicknames on the current channel."
  (let (nicks)
    (when (hash-table-p erc-channel-users)
      (maphash (lambda (n cdata)
		 (setq nicks
		       (cons (erc-server-user-nickname (car cdata))
			     nicks)))
	       erc-channel-users)
      nicks)))

(defun erc-get-server-nickname-alist ()
  "Returns an alist of known nicknames on the current server."
Miles Bader's avatar
Miles Bader committed
538 539 540 541 542 543 544 545 546
  (erc-with-server-buffer
    (let (nicks)
      (when (hash-table-p erc-server-users)
	(maphash (lambda (n user)
		   (setq nicks
			 (cons (cons (erc-server-user-nickname user) nil)
			       nicks)))
		 erc-server-users)
	nicks))))
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 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 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 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 696 697 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 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781

(defun erc-get-channel-nickname-alist ()
  "Returns an alist of known nicknames on the current channel."
  (let (nicks)
    (when (hash-table-p erc-channel-users)
      (maphash (lambda (n cdata)
		 (setq nicks
		       (cons (cons (erc-server-user-nickname (car cdata)) nil)
			     nicks)))
	       erc-channel-users)
      nicks)))

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

See also: `erc-get-channel-user-list'."
  (sort list
	(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))))
	      (if tx
		  (if ty
		      (time-less-p ty tx)
		    t)
		nil))))))

(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)
	  (when (and
		 (cdr x) (cdr y))
	    (let ((nickx (downcase (erc-server-user-nickname (car x))))
		  (nicky (downcase (erc-server-user-nickname (car y)))))
	      (if nickx
		  (if nicky
		      (string-lessp nickx nicky)
		    t)
		nil))))))

(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 "*** "
  "*Prefix for all notices."
  :group 'erc-display
  :type 'string)

(defcustom erc-notice-highlight-type 'all
  "*Determines how to highlight notices.
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
  "*Specifies a list of functions to call to echo a private
notice.  Each function is called with four arguments, the string
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)
  "*Specifies a list of functions to call to echo a private
notice.  Each function is called with four arguments, the string
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
  "*If non-nil, do a whowas on a nick if no such nick."
  :group 'erc
  :type 'boolean)

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

(defcustom erc-public-away-p nil
  "*Let others know you are back when you are no longer marked away.
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
  "*The nickname to take when you are marked as being away."
  :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
782
  "Prompt for channel key when using `erc-join-channel' interactively."
783 784 785 786 787 788 789 790
  :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
791 792 793 794 795 796 797
(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))

798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
(defcustom erc-ignore-list nil
  "*List of regexps matching user identifiers to ignore.

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
  "*List of regexps matching user identifiers to ignore completely.

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
822
people.  You can update the ERC internal info using /WHO *."
823 824 825 826 827 828 829 830 831 832 833 834 835 836
  :group 'erc-ignore
  :type '(repeat regexp))

(defvar erc-flood-protect t
  "*If non-nil, flood protection is enabled.
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
837 838 839
  (list (concat erc-user-emacs-directory ".ercrc.el")
	(concat erc-user-emacs-directory ".ercrc")
	"~/.ercrc.el" "~/.ercrc" ".ercrc.el" ".ercrc")
840 841 842
  "List of files to try for a startup script.
The first existent and readable one will get executed.

843
If the filename ends with `.el' it is presumed to be an Emacs Lisp
844
script and it gets (load)ed.  Otherwise it is treated as a bunch of
845
regular IRC commands."
846 847 848 849 850 851 852 853 854 855 856
  :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
857
  "*If non-nil, echo the IRC script commands locally."
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 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
  :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
      '((\"zippy\" erc-quit-reason-zippy)
	(\"xmms\" dme:now-playing)
	(\"version\" erc-quit-reason-normal)
	(\"home\" \"Gone home !\")
Miles Bader's avatar
Miles Bader committed
901
	(\"^$\" \"Default Reason\")))
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
If the user types \"/quit zippy\", then a Zippy the Pinhead quotation
will be used as the quit message."
  :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
      '((\"zippy\" erc-part-reason-zippy)
	(\"xmms\" dme:now-playing)
	(\"version\" erc-part-reason-normal)
	(\"home\" \"Gone home !\")
Miles Bader's avatar
Miles Bader committed
925
	(\"^$\" \"Default Reason\")))
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 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 1050 1051 1052 1053 1054 1055 1056 1057
If the user types \"/part zippy\", then a Zippy the Pinhead quotation
will be used as the part message."
  :group 'erc-quit-and-part
  :type '(repeat (list regexp (choice (string) (function)))))

(defcustom erc-quit-reason 'erc-quit-reason-normal
  "*A function which returns the reason for quitting.

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

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

1058
This function is called with narrowing, ala `erc-send-modify-hook'."
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 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
  :group 'erc-hooks
  :type 'hook
  :options '(erc-make-read-only))

(defcustom erc-send-completed-hook
  (when (featurep 'emacspeak)
    (list (byte-compile
	   (lambda (str)
	     (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
1111
    (define-key map "\C-c\C-x" 'erc-quit-server)
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
    (define-key map "\M-\t" 'ispell-complete-word)
    (define-key map "\t" 'erc-complete-word)

    ;; Suppress `font-lock-fontify-block' key binding since it
    ;; destroys face properties.
    (if (fboundp 'command-remapping)
	(define-key map [remap font-lock-fontify-block] 'undefined)
      (substitute-key-definition
       'font-lock-fontify-block 'undefined map global-map))

    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)

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

1147 1148 1149 1150 1151 1152 1153
(defface erc-header-line
  '((t (:foreground "grey20" :background "grey90")))
  "ERC face used for the header line.

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

1154 1155 1156 1157 1158
(defface erc-input-face '((t (:foreground "brown")))
  "ERC face used for your input."
  :group 'erc-faces)

(defface erc-prompt-face
1159
  '((t (:bold t :foreground "Black" :background "lightBlue2")))
1160 1161 1162 1163 1164 1165 1166 1167 1168
  "ERC face for the prompt."
  :group 'erc-faces)

(defface erc-command-indicator-face
    '((t (:bold t)))
  "ERC face for the command indicator.
See the variable `erc-command-indicator'."
  :group 'erc-faces)

1169
(defface erc-notice-face
Michael Olson's avatar
Michael Olson committed
1170 1171
  (if (or (featurep 'xemacs)
	  (< emacs-major-version 22))
1172 1173 1174 1175
      '((t (:bold t :foreground "blue")))
    '((((class color) (min-colors 88))
       (:bold t :foreground "SlateBlue"))
      (t (:bold t :foreground "blue"))))
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
  "ERC face for notices."
  :group 'erc-faces)

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

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

Miles Bader's avatar
Miles Bader committed
1187 1188 1189 1190 1191 1192
;; same default color as `erc-input-face'
(defface erc-my-nick-face '((t (:bold t :foreground "brown")))
  "ERC face for your current nickname in messages sent by you.
See also `erc-show-my-nick'."
  :group 'erc-faces)

1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
(defface erc-nick-default-face '((t (:bold t)))
  "ERC nickname default face."
  :group 'erc-faces)

(defface erc-nick-msg-face '((t (:bold t :foreground "IndianRed")))
  "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
1221
mode, rather than a global one.
1222 1223 1224 1225 1226 1227 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 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272

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)))"
  (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.
With arg, turn ERC %S mode on if and only if arg is positive.
%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
1273 1274 1275 1276 1277
	      ,mode)))
       ;; For find-function and find-variable.
       (put ',mode    'definition-name ',name)
       (put ',enable  'definition-name ',name)
       (put ',disable 'definition-name ',name))))
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288

(put 'define-erc-module 'doc-string-elt 3)

(defun erc-once-with-server-event (event &rest forms)
  "Execute FORMS the next time EVENT occurs in the `current-buffer'.

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

This function temporarily adds a function to EVENT's hook to
execute FORMS.  After FORMS are run, the function is removed from
EVENT's hook.  The last expression of FORMS should be either nil
1289
or t, where nil indicates that the other functions on EVENT's hook
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
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
1300
  (let ((fun (make-symbol "fun"))
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
	(hook (erc-get-hook event)))
     (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)
		  ,@forms))
     (add-hook hook fun nil t)
     fun))

(defun erc-once-with-server-event-global (event &rest forms)
  "Execute FORMS the next time EVENT occurs in any server buffer.

This function temporarily prepends a function to EVENT's hook to
execute FORMS.  After FORMS are run, the function is removed from
EVENT's hook.  The last expression of FORMS should be either nil
1317
or t, where nil indicates that the other functions on EVENT's hook
1318 1319 1320 1321 1322
should be run too, and t indicates that other functions should
not be run.

When FORMS execute, the current buffer is the server buffer associated with the
connection over which the data was received that triggered EVENT."
Miles Bader's avatar
Miles Bader committed
1323
  (let ((fun (make-symbol "fun"))
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
	(hook (erc-get-hook event)))
     (fset fun `(lambda (proc parsed)
		  (remove-hook ',hook ',fun)
		  (fmakunbound ',fun)
		  ,@forms))
     (add-hook hook fun nil nil)
     fun))

(defmacro erc-log (string)
  "Logs STRING if logging is on (see `erc-log-p')."
  `(when erc-log-p
     (erc-log-aux ,string)))

(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
1345
  "Return t if the server buffer has not been killed."
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
  (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)))))

Miles Bader's avatar
Miles Bader committed
1357 1358 1359 1360 1361 1362 1363 1364
(defun erc-open-server-buffer-p (&optional buffer)
  "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."
  (and (erc-server-buffer-p)
       (erc-server-process-alive)))

1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
(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
1377
  (erc-with-server-buffer
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
    (let ((erc-online-p 'unknown))
      (erc-once-with-server-event
       303
       `(let ((ison (split-string (aref parsed 3))))
	  (setq erc-online-p (car (erc-member-ignore-case ,nick ison)))
	  t))
      (erc-server-send (format "ISON %s" nick))
      (while (eq erc-online-p 'unknown) (accept-process-output))
      (if (interactive-p)
	  (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
1421
server buffer.")
1422 1423 1424 1425 1426
(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
1427
  (erc-with-server-buffer
Miles Bader's avatar
Miles Bader committed
1428
    (if (buffer-live-p erc-active-buffer)
Miles Bader's avatar
Miles Bader committed
1429 1430
	erc-active-buffer
      (setq erc-active-buffer (current-buffer)))))
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468

(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

(defun erc-mode ()
  "Major mode for Emacs IRC.
Special commands:

\\{erc-mode-map}

Turning on `erc-mode' runs the hook `erc-mode-hook'."
  (kill-all-local-variables)
  (use-local-map erc-mode-map)
  (setq mode-name "ERC"
	major-mode 'erc-mode
	local-abbrev-table erc-mode-abbrev-table)
  (set-syntax-table erc-mode-syntax-table)
  (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)
       (concat "\\(" (regexp-quote (erc-prompt)) "\\)"))
  ;; Run the mode hooks
  (run-hooks 'erc-mode-hook))

;; activation

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

1469
(defconst erc-default-port 6667
1470 1471 1472
  "IRC port to use if it cannot be detected otherwise.")

(defcustom erc-join-buffer 'buffer
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
  "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."
1483
  :group 'erc-buffers
1484 1485 1486 1487 1488 1489
  :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)))
1490 1491 1492

(defcustom erc-frame-alist nil
  "*Alist of frame parameters for creating erc frames.
1493
A value of nil means to use `default-frame-alist'."
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
  :group 'erc-buffers
  :type '(repeat (cons :format "%v"
		       (symbol :tag "Parameter")
		       (sexp :tag "Value"))))

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

1505 1506 1507 1508 1509 1510 1511 1512
(defcustom erc-reuse-frames t
  "*Determines whether new frames are always created.
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)

1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
(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
  "*If nil, create new buffers on joining a channel/query.
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)))

(defun erc-generate-new-buffer-name (server port target &optional proc)
  "Create a new buffer name based on the arguments."
  (when (numberp port) (setq port (number-to-string port)))
  (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*"))))
    ;; 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.
    (if (and erc-reuse-buffers
	     (get-buffer buf-name)
	     (or target
		 (with-current-buffer (get-buffer buf-name)
		   (and (erc-server-buffer-p)
Miles Bader's avatar
Miles Bader committed
1583
			(not (erc-server-process-alive)))))
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
	     (with-current-buffer (get-buffer buf-name)
	       (and (string= erc-session-server server)
		    (erc-port-equal erc-session-port port))))
	buf-name
      (generate-new-buffer-name buf-name))))

(defun erc-get-buffer-create (server port target &optional proc)
  "Create a new buffer based on the arguments."
  (get-buffer-create (erc-generate-new-buffer-name server port target proc)))


(defun erc-member-ignore-case (string list)
  "Return non-nil if STRING is a member of LIST.

All strings are compared according to IRC protocol case rules, see
`erc-downcase'."
  (setq string (erc-downcase string))
  (catch 'result
    (while list
      (if (string= string (erc-downcase (car list)))
Miles Bader's avatar
Miles Bader committed
1604 1605
	  (throw 'result list)
	(setq list (cdr list))))))
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616

(defmacro erc-with-buffer (spec &rest body)
  "Execute BODY in the buffer associated with SPEC.

SPEC should have the form

 (TARGET [PROCESS])

If TARGET is a buffer, use it.  Otherwise, use the buffer
matching TARGET in the process specified by PROCESS.

1617
If PROCESS is nil, use the current `erc-server-process'.
1618 1619 1620 1621 1622
See `erc-get-buffer' for details.

See also `with-current-buffer'.

\(fn (TARGET [PROCESS]) BODY...)"
Miles Bader's avatar
Miles Bader committed
1623 1624 1625 1626
  (let ((buf (make-symbol "buf"))
	(proc (make-symbol "proc"))
	(target (make-symbol "target"))
	(process (make-symbol "process")))
1627 1628 1629 1630 1631 1632 1633 1634 1635
    `(let* ((,target ,(car spec))
	    (,process ,(cadr spec))
	    (,buf (if (bufferp ,target)
		      ,target
		    (let ((,proc (or ,process
				     (and (processp erc-server-process)
					  erc-server-process))))
		      (if (and ,target ,proc)
			  (erc-get-buffer ,target ,proc))))))
Miles Bader's avatar
Miles Bader committed
1636
       (when (buffer-live-p ,buf)
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
	 (with-current-buffer ,buf
	   ,@body)))))
(put 'erc-with-buffer 'lisp-indent-function 1)
(put 'erc-with-buffer 'edebug-form-spec '((form &optional form) body))

(defun erc-get-buffer (target &optional proc)
  "Return the buffer matching TARGET in the process PROC.
If PROC is not supplied, all processes are searched."
  (let ((downcased-target (erc-downcase target)))
    (catch 'buffer
      (erc-buffer-filter
       (lambda ()
	 (let ((current (erc-default-target)))
	   (and (stringp current)
		(string-equal downcased-target (erc-downcase current))
		(throw 'buffer (current-buffer)))))
       proc))))

(defun erc-buffer-filter (predicate &optional proc)
  "Return a list of `erc-mode' buffers matching certain criteria.
PREDICATE is a function executed with each buffer, if it returns t, that buffer
is considered a valid match.

PROC is either an `erc-server-process', identifying a certain
server connection, or nil which means all open connections."
  (save-excursion
    (delq
     nil
     (mapcar (lambda (buf)
Miles Bader's avatar
Miles Bader committed
1666 1667 1668 1669 1670 1671 1672
	       (when (buffer-live-p buf)
		 (with-current-buffer buf
		   (and (eq major-mode 'erc-mode)
			(or (not proc)
			    (eq proc erc-server-process))
			(funcall predicate)
			buf))))
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
	     (buffer-list)))))

(defun erc-buffer-list (&optional predicate proc)
  "Return a list of ERC buffers.
PREDICATE is a function which executes with every buffer satisfying
the predicate.  If PREDICATE is passed as nil, return a list of all ERC
buffers.  If PROC is given, the buffers local variable `erc-server-process'
needs to match PROC."
  (unless predicate
    (setq predicate (lambda () t)))
  (erc-buffer-filter predicate proc))

(defmacro erc-with-all-buffers-of-server (process pred &rest forms)
  "Execute FORMS in all buffers which have same process as this server.
FORMS will be evaluated in all buffers having the process PROCESS and
where PRED matches or in all buffers of the server process if PRED is
nil."
  ;; Make the evaluation have the correct order
Miles Bader's avatar
Miles Bader committed
1691 1692
  (let ((pre (make-symbol "pre"))
	(pro (make-symbol "pro")))
1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
    `(let* ((,pro ,process)
	    (,pre ,pred)
	    (res (mapcar (lambda (buffer)
			   (with-current-buffer buffer
			     ,@forms))
			 (erc-buffer-list ,pre
					  ,pro))))
       ;; Silence the byte-compiler by binding the result of mapcar to
       ;; a variable.
       res)))
1703 1704 1705
(put 'erc-with-all-buffers-of-server 'lisp-indent-function 1)
(put 'erc-with-all-buffers-of-server 'edebug-form-spec '(form form body))

Glenn Morris's avatar
Glenn Morris committed
1706
;; (iswitchb-mode) will autoload iswitchb.el
1707 1708 1709 1710
(defvar iswitchb-temp-buflist)
(declare-function iswitchb-read-buffer "iswitchb"
		 (prompt &optional default require-match start matches-set))

Miles Bader's avatar