1 ;; erc-goodies.el --- Collection of ERC modules
3 ;; Copyright (C) 2001-2015 Free Software Foundation, Inc.
5 ;; Author: Jorgen Schaefer <forcer@forcix.cx>
6 ;; Maintainer: emacs-devel@gnu.org
8 ;; Most code is taken verbatim from erc.el, see there for the original
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This provides some small but still useful modes for ERC.
36 (defun erc-imenu-setup ()
37 "Setup Imenu support in an ERC buffer."
38 (set (make-local-variable 'imenu-create-index-function
)
39 'erc-create-imenu-index
))
41 (add-hook 'erc-mode-hook
'erc-imenu-setup
)
42 (autoload 'erc-create-imenu-index
"erc-imenu" "Imenu index creation function")
44 ;;; Automatically scroll to bottom
45 (defcustom erc-input-line-position nil
46 "Specify where to position the input line when using `erc-scroll-to-bottom'.
48 This should be an integer specifying the line of the buffer on which
49 the input line should stay. A value of \"-1\" would keep the input
50 line positioned on the last line in the buffer. This is passed as an
51 argument to `recenter'."
53 :type
'(choice integer
(const nil
)))
55 (define-erc-module scrolltobottom nil
56 "This mode causes the prompt to stay at the end of the window."
57 ((add-hook 'erc-mode-hook
'erc-add-scroll-to-bottom
)
58 (dolist (buffer (erc-buffer-list))
59 (with-current-buffer buffer
60 (erc-add-scroll-to-bottom))))
61 ((remove-hook 'erc-mode-hook
'erc-add-scroll-to-bottom
)
62 (dolist (buffer (erc-buffer-list))
63 (with-current-buffer buffer
64 (remove-hook 'post-command-hook
'erc-scroll-to-bottom t
)))))
66 (defun erc-add-scroll-to-bottom ()
67 "A hook function for `erc-mode-hook' to recenter output at bottom of window.
69 If you find that ERC hangs when using this function, try customizing
70 the value of `erc-input-line-position'.
72 This works whenever scrolling happens, so it's added to
73 `window-scroll-functions' rather than `erc-insert-post-hook'."
74 (add-hook 'post-command-hook
'erc-scroll-to-bottom nil t
))
76 (defun erc-scroll-to-bottom ()
77 "Recenter WINDOW so that `point' is on the last line.
79 This is added to `window-scroll-functions' by `erc-add-scroll-to-bottom'.
81 You can control which line is recentered to by customizing the
82 variable `erc-input-line-position'."
83 ;; Temporarily bind resize-mini-windows to nil so that users who have it
84 ;; set to a non-nil value will not suffer from premature minibuffer
85 ;; shrinkage due to the below recenter call. I have no idea why this
86 ;; works, but it solves the problem, and has no negative side effects.
87 ;; (Fran Litterio, 2003/01/07)
88 (let ((resize-mini-windows nil
))
91 (when (and erc-insert-marker
92 ;; we're editing a line. Scroll.
93 (> (point) erc-insert-marker
))
95 (goto-char (point-max))
96 (recenter (or erc-input-line-position -
1)))))))
99 (define-erc-module readonly nil
100 "This mode causes all inserted text to be read-only."
101 ((add-hook 'erc-insert-post-hook
'erc-make-read-only
)
102 (add-hook 'erc-send-post-hook
'erc-make-read-only
))
103 ((remove-hook 'erc-insert-post-hook
'erc-make-read-only
)
104 (remove-hook 'erc-send-post-hook
'erc-make-read-only
)))
106 (defun erc-make-read-only ()
107 "Make all the text in the current buffer read-only.
108 Put this function on `erc-insert-post-hook' and/or `erc-send-post-hook'."
109 (put-text-property (point-min) (point-max) 'read-only t
)
110 (put-text-property (point-min) (point-max) 'front-sticky t
)
111 (put-text-property (point-min) (point-max) 'rear-nonsticky t
))
113 ;;; Move to prompt when typing text
114 (define-erc-module move-to-prompt nil
115 "This mode causes the point to be moved to the prompt when typing text."
116 ((add-hook 'erc-mode-hook
'erc-move-to-prompt-setup
)
117 (dolist (buffer (erc-buffer-list))
118 (with-current-buffer buffer
119 (erc-move-to-prompt-setup))))
120 ((remove-hook 'erc-mode-hook
'erc-move-to-prompt-setup
)
121 (dolist (buffer (erc-buffer-list))
122 (with-current-buffer buffer
123 (remove-hook 'pre-command-hook
'erc-move-to-prompt t
)))))
125 (defun erc-move-to-prompt ()
126 "Move the point to the ERC prompt if this is a self-inserting command."
127 (when (and erc-input-marker
(< (point) erc-input-marker
)
128 (eq 'self-insert-command this-command
))
131 (goto-char (point-max))))
133 (defun erc-move-to-prompt-setup ()
134 "Initialize the move-to-prompt module for XEmacs."
135 (add-hook 'pre-command-hook
'erc-move-to-prompt nil t
))
137 ;;; Keep place in unvisited channels
138 (define-erc-module keep-place nil
139 "Leave point above un-viewed text in other channels."
140 ((add-hook 'erc-insert-pre-hook
'erc-keep-place
))
141 ((remove-hook 'erc-insert-pre-hook
'erc-keep-place
)))
143 (defun erc-keep-place (ignored)
144 "Move point away from the last line in a non-selected ERC buffer."
145 (when (and (not (eq (window-buffer (selected-window))
147 (>= (point) erc-insert-marker
))
149 (goto-char (erc-beg-of-input-line))
152 ;;; Distinguish non-commands
153 (defvar erc-noncommands-list
'(erc-cmd-ME
159 "List of commands that are aliases for CTCP ACTION or for ERC messages.
161 If a command's function symbol is in this list, the typed command
162 does not appear in the ERC buffer after the user presses ENTER.")
164 (define-erc-module noncommands nil
165 "This mode distinguishes non-commands.
166 Commands listed in `erc-insert-this' know how to display
168 ((add-hook 'erc-send-pre-hook
'erc-send-distinguish-noncommands
))
169 ((remove-hook 'erc-send-pre-hook
'erc-send-distinguish-noncommands
)))
171 (defun erc-send-distinguish-noncommands (str)
172 "If STR is an ERC non-command, set `erc-insert-this' to nil."
173 (let* ((command (erc-extract-command-from-line str
))
174 (cmd-fun (and command
177 (not (string-match "\n.+$" str
))
178 (memq cmd-fun erc-noncommands-list
))
179 (setq erc-insert-this nil
))))
181 ;;; IRC control character processing.
182 (defgroup erc-control-characters nil
183 "Dealing with control characters."
186 (defcustom erc-interpret-controls-p t
187 "If non-nil, display IRC colors and other highlighting effects.
189 If this is set to the symbol `remove', ERC removes all IRC colors and
190 highlighting effects. When this variable is non-nil, it can cause Emacs to run
191 slowly on systems lacking sufficient CPU speed. In chatty channels, or in an
192 emergency (message flood) it can be turned off to save processing time. See
193 `erc-toggle-interpret-controls'."
194 :group
'erc-control-characters
195 :type
'(choice (const :tag
"Highlight control characters" t
)
196 (const :tag
"Remove control characters" remove
)
197 (const :tag
"Display raw control characters" nil
)))
199 (defcustom erc-interpret-mirc-color nil
200 "If non-nil, ERC will interpret mIRC color codes."
201 :group
'erc-control-characters
204 (defcustom erc-beep-p nil
205 "Beep if C-g is in the server message.
206 The value `erc-interpret-controls-p' must also be t for this to work."
207 :group
'erc-control-characters
210 (defface erc-bold-face
'((t :weight bold
))
214 (defface erc-inverse-face
215 '((t :foreground
"White" :background
"Black"))
219 (defface erc-underline-face
'((t :underline t
))
220 "ERC underline face."
223 (defface fg
:erc-color-face0
'((t :foreground
"White"))
226 (defface fg
:erc-color-face1
'((t :foreground
"black"))
229 (defface fg
:erc-color-face2
'((t :foreground
"blue4"))
232 (defface fg
:erc-color-face3
'((t :foreground
"green4"))
235 (defface fg
:erc-color-face4
'((t :foreground
"red"))
238 (defface fg
:erc-color-face5
'((t :foreground
"brown"))
241 (defface fg
:erc-color-face6
'((t :foreground
"purple"))
244 (defface fg
:erc-color-face7
'((t :foreground
"orange"))
247 (defface fg
:erc-color-face8
'((t :foreground
"yellow"))
250 (defface fg
:erc-color-face9
'((t :foreground
"green"))
253 (defface fg
:erc-color-face10
'((t :foreground
"lightblue1"))
256 (defface fg
:erc-color-face11
'((t :foreground
"cyan"))
259 (defface fg
:erc-color-face12
'((t :foreground
"blue"))
262 (defface fg
:erc-color-face13
'((t :foreground
"deeppink"))
265 (defface fg
:erc-color-face14
'((t :foreground
"gray50"))
268 (defface fg
:erc-color-face15
'((t :foreground
"gray90"))
272 (defface bg
:erc-color-face0
'((t :background
"White"))
275 (defface bg
:erc-color-face1
'((t :background
"black"))
278 (defface bg
:erc-color-face2
'((t :background
"blue4"))
281 (defface bg
:erc-color-face3
'((t :background
"green4"))
284 (defface bg
:erc-color-face4
'((t :background
"red"))
287 (defface bg
:erc-color-face5
'((t :background
"brown"))
290 (defface bg
:erc-color-face6
'((t :background
"purple"))
293 (defface bg
:erc-color-face7
'((t :background
"orange"))
296 (defface bg
:erc-color-face8
'((t :background
"yellow"))
299 (defface bg
:erc-color-face9
'((t :background
"green"))
302 (defface bg
:erc-color-face10
'((t :background
"lightblue1"))
305 (defface bg
:erc-color-face11
'((t :background
"cyan"))
308 (defface bg
:erc-color-face12
'((t :background
"blue"))
311 (defface bg
:erc-color-face13
'((t :background
"deeppink"))
314 (defface bg
:erc-color-face14
'((t :background
"gray50"))
317 (defface bg
:erc-color-face15
'((t :background
"gray90"))
321 (defun erc-get-bg-color-face (n)
322 "Fetches the right face for background color N (0-15)."
323 (if (stringp n
) (setq n
(string-to-number n
)))
324 (if (not (numberp n
))
326 (erc-error "erc-get-bg-color-face: n is NaN: %S" n
))
328 (erc-log (format " Wrong color: %s" n
))
331 ((and (>= n
0) (< n
16))
332 (intern (concat "bg:erc-color-face" (number-to-string n
))))
333 (t (erc-log (format " Wrong color: %s" n
)) 'default
))))
335 (defun erc-get-fg-color-face (n)
336 "Fetches the right face for foreground color N (0-15)."
337 (if (stringp n
) (setq n
(string-to-number n
)))
338 (if (not (numberp n
))
340 (erc-error "erc-get-fg-color-face: n is NaN: %S" n
))
342 (erc-log (format " Wrong color: %s" n
))
345 ((and (>= n
0) (< n
16))
346 (intern (concat "fg:erc-color-face" (number-to-string n
))))
347 (t (erc-log (format " Wrong color: %s" n
)) 'default
))))
349 (define-erc-module irccontrols nil
350 "This mode enables the interpretation of IRC control chars."
351 ((add-hook 'erc-insert-modify-hook
'erc-controls-highlight
)
352 (add-hook 'erc-send-modify-hook
'erc-controls-highlight
))
353 ((remove-hook 'erc-insert-modify-hook
'erc-controls-highlight
)
354 (remove-hook 'erc-send-modify-hook
'erc-controls-highlight
)))
356 (defun erc-controls-interpret (str)
357 "Return a copy of STR after dealing with IRC control characters.
358 See `erc-interpret-controls-p' and `erc-interpret-mirc-color' for options."
361 (cond ((eq erc-interpret-controls-p
'remove
)
362 (erc-controls-strip s
))
363 (erc-interpret-controls-p
369 (while (string-match erc-controls-highlight-regexp s
)
370 (let ((control (match-string 1 s
))
371 (fg-color (match-string 2 s
))
372 (bg-color (match-string 4 s
))
373 (start (match-beginning 0))
374 (end (+ (match-beginning 0)
375 (length (match-string 5 s
)))))
376 (setq s
(erc-replace-match-subexpression-in-string
377 "" s control
1 start
))
378 (cond ((and erc-interpret-mirc-color
(or fg-color bg-color
))
381 ((string= control
"\C-b")
382 (setq boldp
(not boldp
)))
383 ((string= control
"\C-v")
384 (setq inversep
(not inversep
)))
385 ((string= control
"\C-_")
386 (setq underlinep
(not underlinep
)))
387 ((string= control
"\C-c")
390 ((string= control
"\C-g")
393 ((string= control
"\C-o")
400 (erc-controls-propertize
401 start end boldp inversep underlinep fg bg s
)))
405 (defun erc-controls-strip (str)
406 "Return a copy of STR with all IRC control characters removed."
409 (while (string-match erc-controls-remove-regexp s
)
410 (setq s
(replace-match "" nil nil s
)))
413 (defvar erc-controls-remove-regexp
414 "\C-b\\|\C-_\\|\C-v\\|\C-g\\|\C-o\\|\C-c[0-9]?[0-9]?\\(,[0-9][0-9]?\\)?"
415 "Regular expression which matches control characters to remove.")
417 (defvar erc-controls-highlight-regexp
418 (concat "\\(\C-b\\|\C-v\\|\C-_\\|\C-g\\|\C-o\\|"
419 "\C-c\\([0-9][0-9]?\\)?\\(,\\([0-9][0-9]?\\)\\)?\\)"
420 "\\([^\C-b\C-v\C-_\C-c\C-g\C-o\n]*\\)")
421 "Regular expression which matches control chars and the text to highlight.")
423 (defun erc-controls-highlight ()
424 "Highlight IRC control chars in the buffer.
425 This is useful for `erc-insert-modify-hook' and `erc-send-modify-hook'.
426 Also see `erc-interpret-controls-p' and `erc-interpret-mirc-color'."
427 (goto-char (point-min))
428 (cond ((eq erc-interpret-controls-p
'remove
)
429 (while (re-search-forward erc-controls-remove-regexp nil t
)
431 (erc-interpret-controls-p
437 (while (re-search-forward erc-controls-highlight-regexp nil t
)
438 (let ((control (match-string 1))
439 (fg-color (match-string 2))
440 (bg-color (match-string 4))
441 (start (match-beginning 0))
442 (end (+ (match-beginning 0) (length (match-string 5)))))
443 (replace-match "" nil nil nil
1)
444 (cond ((and erc-interpret-mirc-color
(or fg-color bg-color
))
447 ((string= control
"\C-b")
448 (setq boldp
(not boldp
)))
449 ((string= control
"\C-v")
450 (setq inversep
(not inversep
)))
451 ((string= control
"\C-_")
452 (setq underlinep
(not underlinep
)))
453 ((string= control
"\C-c")
456 ((string= control
"\C-g")
459 ((string= control
"\C-o")
466 (erc-controls-propertize start end
467 boldp inversep underlinep fg bg
)))))
470 (defun erc-controls-propertize (from to boldp inversep underlinep fg bg
472 "Prepend properties from IRC control characters between FROM and TO.
473 If optional argument STR is provided, apply to STR, otherwise prepend properties
474 to a region in the current buffer."
475 (font-lock-prepend-text-property
486 '(erc-underline-face)
489 (list (erc-get-fg-color-face fg
))
492 (list (erc-get-bg-color-face bg
))
497 (defun erc-toggle-interpret-controls (&optional arg
)
498 "Toggle interpretation of control sequences in messages.
500 If ARG is positive, interpretation is turned on.
501 Else interpretation is turned off."
503 (cond ((and (numberp arg
) (> arg
0))
504 (setq erc-interpret-controls-p t
))
505 (arg (setq erc-interpret-controls-p nil
))
506 (t (setq erc-interpret-controls-p
(not erc-interpret-controls-p
))))
507 (message "ERC color interpretation %s"
508 (if erc-interpret-controls-p
"ON" "OFF")))
511 (define-erc-module smiley nil
512 "This mode translates text-smileys such as :-) into pictures.
513 This requires the function `smiley-region', which is defined in
514 smiley.el, which is part of Gnus."
515 ((add-hook 'erc-insert-modify-hook
'erc-smiley
)
516 (add-hook 'erc-send-modify-hook
'erc-smiley
))
517 ((remove-hook 'erc-insert-modify-hook
'erc-smiley
)
518 (remove-hook 'erc-send-modify-hook
'erc-smiley
)))
522 This function should be used with `erc-insert-modify-hook'."
523 (when (fboundp 'smiley-region
)
524 (smiley-region (point-min) (point-max))))
527 (define-erc-module unmorse nil
528 "This mode causes morse code in the current channel to be unmorsed."
529 ((add-hook 'erc-insert-modify-hook
'erc-unmorse
))
530 ((remove-hook 'erc-insert-modify-hook
'erc-unmorse
)))
532 (defun erc-unmorse ()
534 Add this to `erc-insert-modify-hook' if you happen to be on a
535 channel that has weird people talking in morse to each other.
537 See also `unmorse-region'."
538 (goto-char (point-min))
539 (when (re-search-forward "[.-]+\\([.-]*/? *\\)+[.-]+/?" nil t
)
541 (narrow-to-region (match-beginning 0) (match-end 0))
542 ;; Turn " / " into " "
543 (goto-char (point-min))
544 (while (re-search-forward " / " nil t
)
546 ;; Turn "/ " into "/"
547 (goto-char (point-min))
548 (while (re-search-forward "/ " nil t
)
551 (unmorse-region (point-min) (point-max)))))
554 (defun erc-occur (string &optional proc
)
555 "Search for STRING in all buffers related to current server.
556 If called interactively and prefix argument is given, search on all connected
557 servers. If called from a program, PROC specifies the server process."
559 (list (read-string "Search for: ")
560 (if current-prefix-arg
561 nil erc-server-process
)))
562 (if (fboundp 'multi-occur
)
563 (multi-occur (erc-buffer-list nil proc
) string
)
564 (error "`multi-occur' is not defined as a function")))
566 (provide 'erc-goodies
)
568 ;;; erc-goodies.el ends here