1 ;; erc-goodies.el --- Collection of ERC modules
3 ;; Copyright (C) 2001-2018 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 <https://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))
151 ;; if `switch-to-buffer-preserve-window-point' is set,
152 ;; we cannot rely on point being saved, and must commit
153 ;; it to window-prev-buffers.
154 (when switch-to-buffer-preserve-window-point
155 (dolist (frame (frame-list))
158 (let ((prev (assq (current-buffer)
159 (window-prev-buffers window
))))
161 (setf (nth 2 prev
) (point-marker)))))
162 frame nil
'nominibuf
)))))
164 ;;; Distinguish non-commands
165 (defvar erc-noncommands-list
'(erc-cmd-ME
171 "List of commands that are aliases for CTCP ACTION or for ERC messages.
173 If a command's function symbol is in this list, the typed command
174 does not appear in the ERC buffer after the user presses ENTER.")
176 (define-erc-module noncommands nil
177 "This mode distinguishes non-commands.
178 Commands listed in `erc-insert-this' know how to display
180 ((add-hook 'erc-send-pre-hook
'erc-send-distinguish-noncommands
))
181 ((remove-hook 'erc-send-pre-hook
'erc-send-distinguish-noncommands
)))
183 (defun erc-send-distinguish-noncommands (str)
184 "If STR is an ERC non-command, set `erc-insert-this' to nil."
185 (let* ((command (erc-extract-command-from-line str
))
186 (cmd-fun (and command
189 (not (string-match "\n.+$" str
))
190 (memq cmd-fun erc-noncommands-list
))
191 (setq erc-insert-this nil
))))
193 ;;; IRC control character processing.
194 (defgroup erc-control-characters nil
195 "Dealing with control characters."
198 (defcustom erc-interpret-controls-p t
199 "If non-nil, display IRC colors and other highlighting effects.
201 If this is set to the symbol `remove', ERC removes all IRC colors and
202 highlighting effects. When this variable is non-nil, it can cause Emacs to run
203 slowly on systems lacking sufficient CPU speed. In chatty channels, or in an
204 emergency (message flood) it can be turned off to save processing time. See
205 `erc-toggle-interpret-controls'."
206 :group
'erc-control-characters
207 :type
'(choice (const :tag
"Highlight control characters" t
)
208 (const :tag
"Remove control characters" remove
)
209 (const :tag
"Display raw control characters" nil
)))
211 (defcustom erc-interpret-mirc-color nil
212 "If non-nil, ERC will interpret mIRC color codes."
213 :group
'erc-control-characters
216 (defcustom erc-beep-p nil
217 "Beep if C-g is in the server message.
218 The value `erc-interpret-controls-p' must also be t for this to work."
219 :group
'erc-control-characters
222 (defface erc-bold-face
'((t :weight bold
))
226 (defface erc-inverse-face
227 '((t :foreground
"White" :background
"Black"))
231 (defface erc-underline-face
'((t :underline t
))
232 "ERC underline face."
235 (defface fg
:erc-color-face0
'((t :foreground
"White"))
238 (defface fg
:erc-color-face1
'((t :foreground
"black"))
241 (defface fg
:erc-color-face2
'((t :foreground
"blue4"))
244 (defface fg
:erc-color-face3
'((t :foreground
"green4"))
247 (defface fg
:erc-color-face4
'((t :foreground
"red"))
250 (defface fg
:erc-color-face5
'((t :foreground
"brown"))
253 (defface fg
:erc-color-face6
'((t :foreground
"purple"))
256 (defface fg
:erc-color-face7
'((t :foreground
"orange"))
259 (defface fg
:erc-color-face8
'((t :foreground
"yellow"))
262 (defface fg
:erc-color-face9
'((t :foreground
"green"))
265 (defface fg
:erc-color-face10
'((t :foreground
"lightblue1"))
268 (defface fg
:erc-color-face11
'((t :foreground
"cyan"))
271 (defface fg
:erc-color-face12
'((t :foreground
"blue"))
274 (defface fg
:erc-color-face13
'((t :foreground
"deeppink"))
277 (defface fg
:erc-color-face14
'((t :foreground
"gray50"))
280 (defface fg
:erc-color-face15
'((t :foreground
"gray90"))
284 (defface bg
:erc-color-face0
'((t :background
"White"))
287 (defface bg
:erc-color-face1
'((t :background
"black"))
290 (defface bg
:erc-color-face2
'((t :background
"blue4"))
293 (defface bg
:erc-color-face3
'((t :background
"green4"))
296 (defface bg
:erc-color-face4
'((t :background
"red"))
299 (defface bg
:erc-color-face5
'((t :background
"brown"))
302 (defface bg
:erc-color-face6
'((t :background
"purple"))
305 (defface bg
:erc-color-face7
'((t :background
"orange"))
308 (defface bg
:erc-color-face8
'((t :background
"yellow"))
311 (defface bg
:erc-color-face9
'((t :background
"green"))
314 (defface bg
:erc-color-face10
'((t :background
"lightblue1"))
317 (defface bg
:erc-color-face11
'((t :background
"cyan"))
320 (defface bg
:erc-color-face12
'((t :background
"blue"))
323 (defface bg
:erc-color-face13
'((t :background
"deeppink"))
326 (defface bg
:erc-color-face14
'((t :background
"gray50"))
329 (defface bg
:erc-color-face15
'((t :background
"gray90"))
333 (defun erc-get-bg-color-face (n)
334 "Fetches the right face for background color N (0-15)."
335 (if (stringp n
) (setq n
(string-to-number n
)))
336 (if (not (numberp n
))
338 (erc-error "erc-get-bg-color-face: n is NaN: %S" n
))
340 (erc-log (format " Wrong color: %s" n
))
343 ((and (>= n
0) (< n
16))
344 (intern (concat "bg:erc-color-face" (number-to-string n
))))
345 (t (erc-log (format " Wrong color: %s" n
)) 'default
))))
347 (defun erc-get-fg-color-face (n)
348 "Fetches the right face for foreground color N (0-15)."
349 (if (stringp n
) (setq n
(string-to-number n
)))
350 (if (not (numberp n
))
352 (erc-error "erc-get-fg-color-face: n is NaN: %S" n
))
354 (erc-log (format " Wrong color: %s" n
))
357 ((and (>= n
0) (< n
16))
358 (intern (concat "fg:erc-color-face" (number-to-string n
))))
359 (t (erc-log (format " Wrong color: %s" n
)) 'default
))))
361 (define-erc-module irccontrols nil
362 "This mode enables the interpretation of IRC control chars."
363 ((add-hook 'erc-insert-modify-hook
'erc-controls-highlight
)
364 (add-hook 'erc-send-modify-hook
'erc-controls-highlight
))
365 ((remove-hook 'erc-insert-modify-hook
'erc-controls-highlight
)
366 (remove-hook 'erc-send-modify-hook
'erc-controls-highlight
)))
368 (defun erc-controls-interpret (str)
369 "Return a copy of STR after dealing with IRC control characters.
370 See `erc-interpret-controls-p' and `erc-interpret-mirc-color' for options."
373 (cond ((eq erc-interpret-controls-p
'remove
)
374 (erc-controls-strip s
))
375 (erc-interpret-controls-p
381 (while (string-match erc-controls-highlight-regexp s
)
382 (let ((control (match-string 1 s
))
383 (fg-color (match-string 2 s
))
384 (bg-color (match-string 4 s
))
385 (start (match-beginning 0))
386 (end (+ (match-beginning 0)
387 (length (match-string 5 s
)))))
388 (setq s
(erc-replace-match-subexpression-in-string
389 "" s control
1 start
))
390 (cond ((and erc-interpret-mirc-color
(or fg-color bg-color
))
393 ((string= control
"\C-b")
394 (setq boldp
(not boldp
)))
395 ((string= control
"\C-v")
396 (setq inversep
(not inversep
)))
397 ((string= control
"\C-_")
398 (setq underlinep
(not underlinep
)))
399 ((string= control
"\C-c")
402 ((string= control
"\C-g")
405 ((string= control
"\C-o")
412 (erc-controls-propertize
413 start end boldp inversep underlinep fg bg s
)))
417 (defun erc-controls-strip (str)
418 "Return a copy of STR with all IRC control characters removed."
421 (while (string-match erc-controls-remove-regexp s
)
422 (setq s
(replace-match "" nil nil s
)))
425 (defvar erc-controls-remove-regexp
426 "\C-b\\|\C-_\\|\C-v\\|\C-g\\|\C-o\\|\C-c[0-9]?[0-9]?\\(,[0-9][0-9]?\\)?"
427 "Regular expression which matches control characters to remove.")
429 (defvar erc-controls-highlight-regexp
430 (concat "\\(\C-b\\|\C-v\\|\C-_\\|\C-g\\|\C-o\\|"
431 "\C-c\\([0-9][0-9]?\\)?\\(,\\([0-9][0-9]?\\)\\)?\\)"
432 "\\([^\C-b\C-v\C-_\C-c\C-g\C-o\n]*\\)")
433 "Regular expression which matches control chars and the text to highlight.")
435 (defun erc-controls-highlight ()
436 "Highlight IRC control chars in the buffer.
437 This is useful for `erc-insert-modify-hook' and `erc-send-modify-hook'.
438 Also see `erc-interpret-controls-p' and `erc-interpret-mirc-color'."
439 (goto-char (point-min))
440 (cond ((eq erc-interpret-controls-p
'remove
)
441 (while (re-search-forward erc-controls-remove-regexp nil t
)
443 (erc-interpret-controls-p
449 (while (re-search-forward erc-controls-highlight-regexp nil t
)
450 (let ((control (match-string 1))
451 (fg-color (match-string 2))
452 (bg-color (match-string 4))
453 (start (match-beginning 0))
454 (end (+ (match-beginning 0) (length (match-string 5)))))
455 (replace-match "" nil nil nil
1)
456 (cond ((and erc-interpret-mirc-color
(or fg-color bg-color
))
459 ((string= control
"\C-b")
460 (setq boldp
(not boldp
)))
461 ((string= control
"\C-v")
462 (setq inversep
(not inversep
)))
463 ((string= control
"\C-_")
464 (setq underlinep
(not underlinep
)))
465 ((string= control
"\C-c")
468 ((string= control
"\C-g")
471 ((string= control
"\C-o")
478 (erc-controls-propertize start end
479 boldp inversep underlinep fg bg
)))))
482 (defun erc-controls-propertize (from to boldp inversep underlinep fg bg
484 "Prepend properties from IRC control characters between FROM and TO.
485 If optional argument STR is provided, apply to STR, otherwise prepend properties
486 to a region in the current buffer."
487 (font-lock-prepend-text-property
498 '(erc-underline-face)
501 (list (erc-get-fg-color-face fg
))
504 (list (erc-get-bg-color-face bg
))
509 (defun erc-toggle-interpret-controls (&optional arg
)
510 "Toggle interpretation of control sequences in messages.
512 If ARG is positive, interpretation is turned on.
513 Else interpretation is turned off."
515 (cond ((and (numberp arg
) (> arg
0))
516 (setq erc-interpret-controls-p t
))
517 (arg (setq erc-interpret-controls-p nil
))
518 (t (setq erc-interpret-controls-p
(not erc-interpret-controls-p
))))
519 (message "ERC color interpretation %s"
520 (if erc-interpret-controls-p
"ON" "OFF")))
523 (define-erc-module smiley nil
524 "This mode translates text-smileys such as :-) into pictures.
525 This requires the function `smiley-region', which is defined in
526 smiley.el, which is part of Gnus."
527 ((add-hook 'erc-insert-modify-hook
'erc-smiley
)
528 (add-hook 'erc-send-modify-hook
'erc-smiley
))
529 ((remove-hook 'erc-insert-modify-hook
'erc-smiley
)
530 (remove-hook 'erc-send-modify-hook
'erc-smiley
)))
534 This function should be used with `erc-insert-modify-hook'."
535 (when (fboundp 'smiley-region
)
536 (smiley-region (point-min) (point-max))))
539 (define-erc-module unmorse nil
540 "This mode causes morse code in the current channel to be unmorsed."
541 ((add-hook 'erc-insert-modify-hook
'erc-unmorse
))
542 ((remove-hook 'erc-insert-modify-hook
'erc-unmorse
)))
544 (defun erc-unmorse ()
546 Add this to `erc-insert-modify-hook' if you happen to be on a
547 channel that has weird people talking in morse to each other.
549 See also `unmorse-region'."
550 (goto-char (point-min))
551 (when (re-search-forward "[.-]+\\([.-]*/? *\\)+[.-]+/?" nil t
)
553 (narrow-to-region (match-beginning 0) (match-end 0))
554 ;; Turn " / " into " "
555 (goto-char (point-min))
556 (while (re-search-forward " / " nil t
)
558 ;; Turn "/ " into "/"
559 (goto-char (point-min))
560 (while (re-search-forward "/ " nil t
)
563 (unmorse-region (point-min) (point-max)))))
566 (defun erc-occur (string &optional proc
)
567 "Search for STRING in all buffers related to current server.
568 If called interactively and prefix argument is given, search on all connected
569 servers. If called from a program, PROC specifies the server process."
571 (list (read-string "Search for: ")
572 (if current-prefix-arg
573 nil erc-server-process
)))
574 (if (fboundp 'multi-occur
)
575 (multi-occur (erc-buffer-list nil proc
) string
)
576 (error "`multi-occur' is not defined as a function")))
578 (provide 'erc-goodies
)
580 ;;; erc-goodies.el ends here