1 ;; erc-goodies.el --- Collection of ERC modules
3 ;; Copyright (C) 2001-2012 Free Software Foundation, Inc.
5 ;; Author: Jorgen Schaefer <forcer@forcix.cx>
7 ;; Most code is taken verbatim from erc.el, see there for the original
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This provides some small but still useful modes for ERC.
35 (defun erc-imenu-setup ()
36 "Setup Imenu support in an ERC buffer."
37 (set (make-local-variable 'imenu-create-index-function
)
38 'erc-create-imenu-index
))
40 (add-hook 'erc-mode-hook
'erc-imenu-setup
)
41 (autoload 'erc-create-imenu-index
"erc-imenu" "Imenu index creation function")
43 ;;; Automatically scroll to bottom
44 (defcustom erc-input-line-position nil
45 "Specify where to position the input line when using `erc-scroll-to-bottom'.
47 This should be an integer specifying the line of the buffer on which
48 the input line should stay. A value of \"-1\" would keep the input
49 line positioned on the last line in the buffer. This is passed as an
50 argument to `recenter'."
52 :type
'(choice integer
(const nil
)))
54 (define-erc-module scrolltobottom nil
55 "This mode causes the prompt to stay at the end of the window."
56 ((add-hook 'erc-mode-hook
'erc-add-scroll-to-bottom
)
57 (dolist (buffer (erc-buffer-list))
58 (with-current-buffer buffer
59 (erc-add-scroll-to-bottom))))
60 ((remove-hook 'erc-mode-hook
'erc-add-scroll-to-bottom
)
61 (dolist (buffer (erc-buffer-list))
62 (with-current-buffer buffer
63 (remove-hook 'post-command-hook
'erc-scroll-to-bottom t
)))))
65 (defun erc-add-scroll-to-bottom ()
66 "A hook function for `erc-mode-hook' to recenter output at bottom of window.
68 If you find that ERC hangs when using this function, try customizing
69 the value of `erc-input-line-position'.
71 This works whenever scrolling happens, so it's added to
72 `window-scroll-functions' rather than `erc-insert-post-hook'."
73 (add-hook 'post-command-hook
'erc-scroll-to-bottom nil t
))
75 (defun erc-scroll-to-bottom ()
76 "Recenter WINDOW so that `point' is on the last line.
78 This is added to `window-scroll-functions' by `erc-add-scroll-to-bottom'.
80 You can control which line is recentered to by customizing the
81 variable `erc-input-line-position'."
82 ;; Temporarily bind resize-mini-windows to nil so that users who have it
83 ;; set to a non-nil value will not suffer from premature minibuffer
84 ;; shrinkage due to the below recenter call. I have no idea why this
85 ;; works, but it solves the problem, and has no negative side effects.
86 ;; (Fran Litterio, 2003/01/07)
87 (let ((resize-mini-windows nil
))
90 (when (and erc-insert-marker
91 ;; we're editing a line. Scroll.
92 (> (point) erc-insert-marker
))
94 (goto-char (point-max))
95 (recenter (or erc-input-line-position -
1)))))))
98 (define-erc-module readonly nil
99 "This mode causes all inserted text to be read-only."
100 ((add-hook 'erc-insert-post-hook
'erc-make-read-only
)
101 (add-hook 'erc-send-post-hook
'erc-make-read-only
))
102 ((remove-hook 'erc-insert-post-hook
'erc-make-read-only
)
103 (remove-hook 'erc-send-post-hook
'erc-make-read-only
)))
105 (defun erc-make-read-only ()
106 "Make all the text in the current buffer read-only.
107 Put this function on `erc-insert-post-hook' and/or `erc-send-post-hook'."
108 (put-text-property (point-min) (point-max) 'read-only t
)
109 (put-text-property (point-min) (point-max) 'front-sticky t
)
110 (put-text-property (point-min) (point-max) 'rear-nonsticky t
))
112 ;;; Move to prompt when typing text
113 (define-erc-module move-to-prompt nil
114 "This mode causes the point to be moved to the prompt when typing text."
115 ((add-hook 'erc-mode-hook
'erc-move-to-prompt-setup
)
116 (dolist (buffer (erc-buffer-list))
117 (with-current-buffer buffer
118 (erc-move-to-prompt-setup))))
119 ((remove-hook 'erc-mode-hook
'erc-move-to-prompt-setup
)
120 (dolist (buffer (erc-buffer-list))
121 (with-current-buffer buffer
122 (remove-hook 'pre-command-hook
'erc-move-to-prompt t
)))))
124 (defun erc-move-to-prompt ()
125 "Move the point to the ERC prompt if this is a self-inserting command."
126 (when (and erc-input-marker
(< (point) erc-input-marker
)
127 (eq 'self-insert-command this-command
))
130 (goto-char (point-max))))
132 (defun erc-move-to-prompt-setup ()
133 "Initialize the move-to-prompt module for XEmacs."
134 (add-hook 'pre-command-hook
'erc-move-to-prompt nil t
))
136 ;;; Keep place in unvisited channels
137 (define-erc-module keep-place nil
138 "Leave point above un-viewed text in other channels."
139 ((add-hook 'erc-insert-pre-hook
'erc-keep-place
))
140 ((remove-hook 'erc-insert-pre-hook
'erc-keep-place
)))
142 (defun erc-keep-place (ignored)
143 "Move point away from the last line in a non-selected ERC buffer."
144 (when (and (not (eq (window-buffer (selected-window))
146 (>= (point) erc-insert-marker
))
148 (goto-char (erc-beg-of-input-line))
151 ;;; Distinguish non-commands
152 (defvar erc-noncommands-list
'(erc-cmd-ME
158 "List of commands that are aliases for CTCP ACTION or for ERC messages.
160 If a command's function symbol is in this list, the typed command
161 does not appear in the ERC buffer after the user presses ENTER.")
163 (define-erc-module noncommands nil
164 "This mode distinguishes non-commands.
165 Commands listed in `erc-insert-this' know how to display
167 ((add-hook 'erc-send-pre-hook
'erc-send-distinguish-noncommands
))
168 ((remove-hook 'erc-send-pre-hook
'erc-send-distinguish-noncommands
)))
170 (defun erc-send-distinguish-noncommands (str)
171 "If STR is an ERC non-command, set `erc-insert-this' to nil."
172 (let* ((command (erc-extract-command-from-line str
))
173 (cmd-fun (and command
176 (not (string-match "\n.+$" str
))
177 (memq cmd-fun erc-noncommands-list
))
178 (setq erc-insert-this nil
))))
180 ;;; IRC control character processing.
181 (defgroup erc-control-characters nil
182 "Dealing with control characters."
185 (defcustom erc-interpret-controls-p t
186 "*If non-nil, display IRC colors and other highlighting effects.
188 If this is set to the symbol `remove', ERC removes all IRC colors and
189 highlighting effects. When this variable is non-nil, it can cause Emacs to run
190 slowly on systems lacking sufficient CPU speed. In chatty channels, or in an
191 emergency (message flood) it can be turned off to save processing time. See
192 `erc-toggle-interpret-controls'."
193 :group
'erc-control-characters
194 :type
'(choice (const :tag
"Highlight control characters" t
)
195 (const :tag
"Remove control characters" remove
)
196 (const :tag
"Display raw control characters" nil
)))
198 (defcustom erc-interpret-mirc-color nil
199 "*If non-nil, ERC will interpret mIRC color codes."
200 :group
'erc-control-characters
203 (defcustom erc-beep-p nil
204 "Beep if C-g is in the server message.
205 The value `erc-interpret-controls-p' must also be t for this to work."
206 :group
'erc-control-characters
209 (defface erc-bold-face
'((t (:bold t
)))
212 (defface erc-inverse-face
213 '((t (:foreground
"White" :background
"Black")))
216 (defface erc-underline-face
'((t (:underline t
)))
217 "ERC underline face."
220 (defface fg
:erc-color-face0
'((t (:foreground
"White")))
223 (defface fg
:erc-color-face1
'((t (:foreground
"black")))
226 (defface fg
:erc-color-face2
'((t (:foreground
"blue4")))
229 (defface fg
:erc-color-face3
'((t (:foreground
"green4")))
232 (defface fg
:erc-color-face4
'((t (:foreground
"red")))
235 (defface fg
:erc-color-face5
'((t (:foreground
"brown")))
238 (defface fg
:erc-color-face6
'((t (:foreground
"purple")))
241 (defface fg
:erc-color-face7
'((t (:foreground
"orange")))
244 (defface fg
:erc-color-face8
'((t (:foreground
"yellow")))
247 (defface fg
:erc-color-face9
'((t (:foreground
"green")))
250 (defface fg
:erc-color-face10
'((t (:foreground
"lightblue1")))
253 (defface fg
:erc-color-face11
'((t (:foreground
"cyan")))
256 (defface fg
:erc-color-face12
'((t (:foreground
"blue")))
259 (defface fg
:erc-color-face13
'((t (:foreground
"deeppink")))
262 (defface fg
:erc-color-face14
'((t (:foreground
"gray50")))
265 (defface fg
:erc-color-face15
'((t (:foreground
"gray90")))
269 (defface bg
:erc-color-face0
'((t (:background
"White")))
272 (defface bg
:erc-color-face1
'((t (:background
"black")))
275 (defface bg
:erc-color-face2
'((t (:background
"blue4")))
278 (defface bg
:erc-color-face3
'((t (:background
"green4")))
281 (defface bg
:erc-color-face4
'((t (:background
"red")))
284 (defface bg
:erc-color-face5
'((t (:background
"brown")))
287 (defface bg
:erc-color-face6
'((t (:background
"purple")))
290 (defface bg
:erc-color-face7
'((t (:background
"orange")))
293 (defface bg
:erc-color-face8
'((t (:background
"yellow")))
296 (defface bg
:erc-color-face9
'((t (:background
"green")))
299 (defface bg
:erc-color-face10
'((t (:background
"lightblue1")))
302 (defface bg
:erc-color-face11
'((t (:background
"cyan")))
305 (defface bg
:erc-color-face12
'((t (:background
"blue")))
308 (defface bg
:erc-color-face13
'((t (:background
"deeppink")))
311 (defface bg
:erc-color-face14
'((t (:background
"gray50")))
314 (defface bg
:erc-color-face15
'((t (:background
"gray90")))
318 (defun erc-get-bg-color-face (n)
319 "Fetches the right face for background color N (0-15)."
320 (if (stringp n
) (setq n
(string-to-number n
)))
321 (if (not (numberp n
))
323 (erc-error "erc-get-bg-color-face: n is NaN: %S" n
))
325 (erc-log (format " Wrong color: %s" n
))
328 ((and (>= n
0) (< n
16))
329 (intern (concat "bg:erc-color-face" (number-to-string n
))))
330 (t (erc-log (format " Wrong color: %s" n
)) 'default
))))
332 (defun erc-get-fg-color-face (n)
333 "Fetches the right face for foreground color N (0-15)."
334 (if (stringp n
) (setq n
(string-to-number n
)))
335 (if (not (numberp n
))
337 (erc-error "erc-get-fg-color-face: n is NaN: %S" n
))
339 (erc-log (format " Wrong color: %s" n
))
342 ((and (>= n
0) (< n
16))
343 (intern (concat "fg:erc-color-face" (number-to-string n
))))
344 (t (erc-log (format " Wrong color: %s" n
)) 'default
))))
346 (define-erc-module irccontrols nil
347 "This mode enables the interpretation of IRC control chars."
348 ((add-hook 'erc-insert-modify-hook
'erc-controls-highlight
)
349 (add-hook 'erc-send-modify-hook
'erc-controls-highlight
))
350 ((remove-hook 'erc-insert-modify-hook
'erc-controls-highlight
)
351 (remove-hook 'erc-send-modify-hook
'erc-controls-highlight
)))
353 (defun erc-controls-interpret (str)
354 "Return a copy of STR after dealing with IRC control characters.
355 See `erc-interpret-controls-p' and `erc-interpret-mirc-color' for options."
358 (cond ((eq erc-interpret-controls-p
'remove
)
359 (erc-controls-strip s
))
360 (erc-interpret-controls-p
366 (while (string-match erc-controls-highlight-regexp s
)
367 (let ((control (match-string 1 s
))
368 (fg-color (match-string 2 s
))
369 (bg-color (match-string 4 s
))
370 (start (match-beginning 0))
371 (end (+ (match-beginning 0)
372 (length (match-string 5 s
)))))
373 (setq s
(erc-replace-match-subexpression-in-string
374 "" s control
1 start
))
375 (cond ((and erc-interpret-mirc-color
(or fg-color bg-color
))
378 ((string= control
"\C-b")
379 (setq boldp
(not boldp
)))
380 ((string= control
"\C-v")
381 (setq inversep
(not inversep
)))
382 ((string= control
"\C-_")
383 (setq underlinep
(not underlinep
)))
384 ((string= control
"\C-c")
387 ((string= control
"\C-g")
390 ((string= control
"\C-o")
397 (erc-controls-propertize
398 start end boldp inversep underlinep fg bg s
)))
402 (defun erc-controls-strip (str)
403 "Return a copy of STR with all IRC control characters removed."
406 (while (string-match erc-controls-remove-regexp s
)
407 (setq s
(replace-match "" nil nil s
)))
410 (defvar erc-controls-remove-regexp
411 "\C-b\\|\C-_\\|\C-v\\|\C-g\\|\C-o\\|\C-c[0-9]?[0-9]?\\(,[0-9][0-9]?\\)?"
412 "Regular expression which matches control characters to remove.")
414 (defvar erc-controls-highlight-regexp
415 (concat "\\(\C-b\\|\C-v\\|\C-_\\|\C-g\\|\C-o\\|"
416 "\C-c\\([0-9][0-9]?\\)?\\(,\\([0-9][0-9]?\\)\\)?\\)"
417 "\\([^\C-b\C-v\C-_\C-c\C-g\C-o\n]*\\)")
418 "Regular expression which matches control chars and the text to highlight.")
420 (defun erc-controls-highlight ()
421 "Highlight IRC control chars in the buffer.
422 This is useful for `erc-insert-modify-hook' and `erc-send-modify-hook'.
423 Also see `erc-interpret-controls-p' and `erc-interpret-mirc-color'."
424 (goto-char (point-min))
425 (cond ((eq erc-interpret-controls-p
'remove
)
426 (while (re-search-forward erc-controls-remove-regexp nil t
)
428 (erc-interpret-controls-p
434 (while (re-search-forward erc-controls-highlight-regexp nil t
)
435 (let ((control (match-string 1))
436 (fg-color (match-string 2))
437 (bg-color (match-string 4))
438 (start (match-beginning 0))
439 (end (+ (match-beginning 0) (length (match-string 5)))))
440 (replace-match "" nil nil nil
1)
441 (cond ((and erc-interpret-mirc-color
(or fg-color bg-color
))
444 ((string= control
"\C-b")
445 (setq boldp
(not boldp
)))
446 ((string= control
"\C-v")
447 (setq inversep
(not inversep
)))
448 ((string= control
"\C-_")
449 (setq underlinep
(not underlinep
)))
450 ((string= control
"\C-c")
453 ((string= control
"\C-g")
456 ((string= control
"\C-o")
463 (erc-controls-propertize start end
464 boldp inversep underlinep fg bg
)))))
467 (defun erc-controls-propertize (from to boldp inversep underlinep fg bg
469 "Prepend properties from IRC control characters between FROM and TO.
470 If optional argument STR is provided, apply to STR, otherwise prepend properties
471 to a region in the current buffer."
472 (font-lock-prepend-text-property
483 '(erc-underline-face)
486 (list (erc-get-fg-color-face fg
))
489 (list (erc-get-bg-color-face bg
))
494 (defun erc-toggle-interpret-controls (&optional arg
)
495 "Toggle interpretation of control sequences in messages.
497 If ARG is positive, interpretation is turned on.
498 Else interpretation is turned off."
500 (cond ((and (numberp arg
) (> arg
0))
501 (setq erc-interpret-controls-p t
))
502 (arg (setq erc-interpret-controls-p nil
))
503 (t (setq erc-interpret-controls-p
(not erc-interpret-controls-p
))))
504 (message "ERC color interpretation %s"
505 (if erc-interpret-controls-p
"ON" "OFF")))
508 (define-erc-module smiley nil
509 "This mode translates text-smileys such as :-) into pictures.
510 This requires the function `smiley-region', which is defined in
511 smiley.el, which is part of Gnus."
512 ((add-hook 'erc-insert-modify-hook
'erc-smiley
)
513 (add-hook 'erc-send-modify-hook
'erc-smiley
))
514 ((remove-hook 'erc-insert-modify-hook
'erc-smiley
)
515 (remove-hook 'erc-send-modify-hook
'erc-smiley
)))
519 This function should be used with `erc-insert-modify-hook'."
520 (when (fboundp 'smiley-region
)
521 (smiley-region (point-min) (point-max))))
524 (define-erc-module unmorse nil
525 "This mode causes morse code in the current channel to be unmorsed."
526 ((add-hook 'erc-insert-modify-hook
'erc-unmorse
))
527 ((remove-hook 'erc-insert-modify-hook
'erc-unmorse
)))
529 (defun erc-unmorse ()
531 Add this to `erc-insert-modify-hook' if you happen to be on a
532 channel that has weird people talking in morse to each other.
534 See also `unmorse-region'."
535 (goto-char (point-min))
536 (when (re-search-forward "[.-]+\\([.-]*/? *\\)+[.-]+/?" nil t
)
538 (narrow-to-region (match-beginning 0) (match-end 0))
539 ;; Turn " / " into " "
540 (goto-char (point-min))
541 (while (re-search-forward " / " nil t
)
543 ;; Turn "/ " into "/"
544 (goto-char (point-min))
545 (while (re-search-forward "/ " nil t
)
548 (unmorse-region (point-min) (point-max)))))
551 (defun erc-occur (string &optional proc
)
552 "Search for STRING in all buffers related to current server.
553 If called interactively and prefix argument is given, search on all connected
554 servers. If called from a program, PROC specifies the server process."
556 (list (read-string "Search for: ")
557 (if current-prefix-arg
558 nil erc-server-process
)))
559 (if (fboundp 'multi-occur
)
560 (multi-occur (erc-buffer-list nil proc
) string
)
561 (error "`multi-occur' is not defined as a function")))
563 (provide 'erc-goodies
)
565 ;;; erc-goodies.el ends here