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 :weight bold
))
213 (defface erc-inverse-face
214 '((t :foreground
"White" :background
"Black"))
218 (defface erc-underline-face
'((t :underline t
))
219 "ERC underline face."
222 (defface fg
:erc-color-face0
'((t :foreground
"White"))
225 (defface fg
:erc-color-face1
'((t :foreground
"black"))
228 (defface fg
:erc-color-face2
'((t :foreground
"blue4"))
231 (defface fg
:erc-color-face3
'((t :foreground
"green4"))
234 (defface fg
:erc-color-face4
'((t :foreground
"red"))
237 (defface fg
:erc-color-face5
'((t :foreground
"brown"))
240 (defface fg
:erc-color-face6
'((t :foreground
"purple"))
243 (defface fg
:erc-color-face7
'((t :foreground
"orange"))
246 (defface fg
:erc-color-face8
'((t :foreground
"yellow"))
249 (defface fg
:erc-color-face9
'((t :foreground
"green"))
252 (defface fg
:erc-color-face10
'((t :foreground
"lightblue1"))
255 (defface fg
:erc-color-face11
'((t :foreground
"cyan"))
258 (defface fg
:erc-color-face12
'((t :foreground
"blue"))
261 (defface fg
:erc-color-face13
'((t :foreground
"deeppink"))
264 (defface fg
:erc-color-face14
'((t :foreground
"gray50"))
267 (defface fg
:erc-color-face15
'((t :foreground
"gray90"))
271 (defface bg
:erc-color-face0
'((t :background
"White"))
274 (defface bg
:erc-color-face1
'((t :background
"black"))
277 (defface bg
:erc-color-face2
'((t :background
"blue4"))
280 (defface bg
:erc-color-face3
'((t :background
"green4"))
283 (defface bg
:erc-color-face4
'((t :background
"red"))
286 (defface bg
:erc-color-face5
'((t :background
"brown"))
289 (defface bg
:erc-color-face6
'((t :background
"purple"))
292 (defface bg
:erc-color-face7
'((t :background
"orange"))
295 (defface bg
:erc-color-face8
'((t :background
"yellow"))
298 (defface bg
:erc-color-face9
'((t :background
"green"))
301 (defface bg
:erc-color-face10
'((t :background
"lightblue1"))
304 (defface bg
:erc-color-face11
'((t :background
"cyan"))
307 (defface bg
:erc-color-face12
'((t :background
"blue"))
310 (defface bg
:erc-color-face13
'((t :background
"deeppink"))
313 (defface bg
:erc-color-face14
'((t :background
"gray50"))
316 (defface bg
:erc-color-face15
'((t :background
"gray90"))
320 (defun erc-get-bg-color-face (n)
321 "Fetches the right face for background color N (0-15)."
322 (if (stringp n
) (setq n
(string-to-number n
)))
323 (if (not (numberp n
))
325 (erc-error "erc-get-bg-color-face: n is NaN: %S" n
))
327 (erc-log (format " Wrong color: %s" n
))
330 ((and (>= n
0) (< n
16))
331 (intern (concat "bg:erc-color-face" (number-to-string n
))))
332 (t (erc-log (format " Wrong color: %s" n
)) 'default
))))
334 (defun erc-get-fg-color-face (n)
335 "Fetches the right face for foreground color N (0-15)."
336 (if (stringp n
) (setq n
(string-to-number n
)))
337 (if (not (numberp n
))
339 (erc-error "erc-get-fg-color-face: n is NaN: %S" n
))
341 (erc-log (format " Wrong color: %s" n
))
344 ((and (>= n
0) (< n
16))
345 (intern (concat "fg:erc-color-face" (number-to-string n
))))
346 (t (erc-log (format " Wrong color: %s" n
)) 'default
))))
348 (define-erc-module irccontrols nil
349 "This mode enables the interpretation of IRC control chars."
350 ((add-hook 'erc-insert-modify-hook
'erc-controls-highlight
)
351 (add-hook 'erc-send-modify-hook
'erc-controls-highlight
))
352 ((remove-hook 'erc-insert-modify-hook
'erc-controls-highlight
)
353 (remove-hook 'erc-send-modify-hook
'erc-controls-highlight
)))
355 (defun erc-controls-interpret (str)
356 "Return a copy of STR after dealing with IRC control characters.
357 See `erc-interpret-controls-p' and `erc-interpret-mirc-color' for options."
360 (cond ((eq erc-interpret-controls-p
'remove
)
361 (erc-controls-strip s
))
362 (erc-interpret-controls-p
368 (while (string-match erc-controls-highlight-regexp s
)
369 (let ((control (match-string 1 s
))
370 (fg-color (match-string 2 s
))
371 (bg-color (match-string 4 s
))
372 (start (match-beginning 0))
373 (end (+ (match-beginning 0)
374 (length (match-string 5 s
)))))
375 (setq s
(erc-replace-match-subexpression-in-string
376 "" s control
1 start
))
377 (cond ((and erc-interpret-mirc-color
(or fg-color bg-color
))
380 ((string= control
"\C-b")
381 (setq boldp
(not boldp
)))
382 ((string= control
"\C-v")
383 (setq inversep
(not inversep
)))
384 ((string= control
"\C-_")
385 (setq underlinep
(not underlinep
)))
386 ((string= control
"\C-c")
389 ((string= control
"\C-g")
392 ((string= control
"\C-o")
399 (erc-controls-propertize
400 start end boldp inversep underlinep fg bg s
)))
404 (defun erc-controls-strip (str)
405 "Return a copy of STR with all IRC control characters removed."
408 (while (string-match erc-controls-remove-regexp s
)
409 (setq s
(replace-match "" nil nil s
)))
412 (defvar erc-controls-remove-regexp
413 "\C-b\\|\C-_\\|\C-v\\|\C-g\\|\C-o\\|\C-c[0-9]?[0-9]?\\(,[0-9][0-9]?\\)?"
414 "Regular expression which matches control characters to remove.")
416 (defvar erc-controls-highlight-regexp
417 (concat "\\(\C-b\\|\C-v\\|\C-_\\|\C-g\\|\C-o\\|"
418 "\C-c\\([0-9][0-9]?\\)?\\(,\\([0-9][0-9]?\\)\\)?\\)"
419 "\\([^\C-b\C-v\C-_\C-c\C-g\C-o\n]*\\)")
420 "Regular expression which matches control chars and the text to highlight.")
422 (defun erc-controls-highlight ()
423 "Highlight IRC control chars in the buffer.
424 This is useful for `erc-insert-modify-hook' and `erc-send-modify-hook'.
425 Also see `erc-interpret-controls-p' and `erc-interpret-mirc-color'."
426 (goto-char (point-min))
427 (cond ((eq erc-interpret-controls-p
'remove
)
428 (while (re-search-forward erc-controls-remove-regexp nil t
)
430 (erc-interpret-controls-p
436 (while (re-search-forward erc-controls-highlight-regexp nil t
)
437 (let ((control (match-string 1))
438 (fg-color (match-string 2))
439 (bg-color (match-string 4))
440 (start (match-beginning 0))
441 (end (+ (match-beginning 0) (length (match-string 5)))))
442 (replace-match "" nil nil nil
1)
443 (cond ((and erc-interpret-mirc-color
(or fg-color bg-color
))
446 ((string= control
"\C-b")
447 (setq boldp
(not boldp
)))
448 ((string= control
"\C-v")
449 (setq inversep
(not inversep
)))
450 ((string= control
"\C-_")
451 (setq underlinep
(not underlinep
)))
452 ((string= control
"\C-c")
455 ((string= control
"\C-g")
458 ((string= control
"\C-o")
465 (erc-controls-propertize start end
466 boldp inversep underlinep fg bg
)))))
469 (defun erc-controls-propertize (from to boldp inversep underlinep fg bg
471 "Prepend properties from IRC control characters between FROM and TO.
472 If optional argument STR is provided, apply to STR, otherwise prepend properties
473 to a region in the current buffer."
474 (font-lock-prepend-text-property
485 '(erc-underline-face)
488 (list (erc-get-fg-color-face fg
))
491 (list (erc-get-bg-color-face bg
))
496 (defun erc-toggle-interpret-controls (&optional arg
)
497 "Toggle interpretation of control sequences in messages.
499 If ARG is positive, interpretation is turned on.
500 Else interpretation is turned off."
502 (cond ((and (numberp arg
) (> arg
0))
503 (setq erc-interpret-controls-p t
))
504 (arg (setq erc-interpret-controls-p nil
))
505 (t (setq erc-interpret-controls-p
(not erc-interpret-controls-p
))))
506 (message "ERC color interpretation %s"
507 (if erc-interpret-controls-p
"ON" "OFF")))
510 (define-erc-module smiley nil
511 "This mode translates text-smileys such as :-) into pictures.
512 This requires the function `smiley-region', which is defined in
513 smiley.el, which is part of Gnus."
514 ((add-hook 'erc-insert-modify-hook
'erc-smiley
)
515 (add-hook 'erc-send-modify-hook
'erc-smiley
))
516 ((remove-hook 'erc-insert-modify-hook
'erc-smiley
)
517 (remove-hook 'erc-send-modify-hook
'erc-smiley
)))
521 This function should be used with `erc-insert-modify-hook'."
522 (when (fboundp 'smiley-region
)
523 (smiley-region (point-min) (point-max))))
526 (define-erc-module unmorse nil
527 "This mode causes morse code in the current channel to be unmorsed."
528 ((add-hook 'erc-insert-modify-hook
'erc-unmorse
))
529 ((remove-hook 'erc-insert-modify-hook
'erc-unmorse
)))
531 (defun erc-unmorse ()
533 Add this to `erc-insert-modify-hook' if you happen to be on a
534 channel that has weird people talking in morse to each other.
536 See also `unmorse-region'."
537 (goto-char (point-min))
538 (when (re-search-forward "[.-]+\\([.-]*/? *\\)+[.-]+/?" nil t
)
540 (narrow-to-region (match-beginning 0) (match-end 0))
541 ;; Turn " / " into " "
542 (goto-char (point-min))
543 (while (re-search-forward " / " nil t
)
545 ;; Turn "/ " into "/"
546 (goto-char (point-min))
547 (while (re-search-forward "/ " nil t
)
550 (unmorse-region (point-min) (point-max)))))
553 (defun erc-occur (string &optional proc
)
554 "Search for STRING in all buffers related to current server.
555 If called interactively and prefix argument is given, search on all connected
556 servers. If called from a program, PROC specifies the server process."
558 (list (read-string "Search for: ")
559 (if current-prefix-arg
560 nil erc-server-process
)))
561 (if (fboundp 'multi-occur
)
562 (multi-occur (erc-buffer-list nil proc
) string
)
563 (error "`multi-occur' is not defined as a function")))
565 (provide 'erc-goodies
)
567 ;;; erc-goodies.el ends here