Document reserved keys
[emacs.git] / lisp / erc / erc-goodies.el
blob2d5aede6c8226b3bbd9043eac95549ac23f03a1d
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
9 ;; authors.
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/>.
26 ;;; Commentary:
28 ;; This provides some small but still useful modes for ERC.
30 ;;; Code:
32 (require 'erc)
34 ;;; Imenu support
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'."
52 :group 'erc-display
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))
89 (save-restriction
90 (widen)
91 (when (and erc-insert-marker
92 ;; we're editing a line. Scroll.
93 (> (point) erc-insert-marker))
94 (save-excursion
95 (goto-char (point-max))
96 (recenter (or erc-input-line-position -1)))))))
98 ;;; Make read only
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))
129 (deactivate-mark)
130 (push-mark)
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))
146 (current-buffer)))
147 (>= (point) erc-insert-marker))
148 (deactivate-mark)
149 (goto-char (erc-beg-of-input-line))
150 (forward-line -1)
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))
156 (walk-window-tree
157 (lambda (window)
158 (let ((prev (assq (current-buffer)
159 (window-prev-buffers window))))
160 (when prev
161 (setf (nth 2 prev) (point-marker)))))
162 frame nil 'nominibuf)))))
164 ;;; Distinguish non-commands
165 (defvar erc-noncommands-list '(erc-cmd-ME
166 erc-cmd-COUNTRY
167 erc-cmd-SV
168 erc-cmd-SM
169 erc-cmd-SMV
170 erc-cmd-LASTLOG)
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
179 themselves."
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
187 (car command))))
188 (when (and cmd-fun
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."
196 :group 'erc)
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
214 :type 'boolean)
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
220 :type 'boolean)
222 (defface erc-bold-face '((t :weight bold))
223 "ERC bold face."
224 :group 'erc-faces)
226 (defface erc-inverse-face
227 '((t :foreground "White" :background "Black"))
228 "ERC inverse face."
229 :group 'erc-faces)
231 (defface erc-underline-face '((t :underline t))
232 "ERC underline face."
233 :group 'erc-faces)
235 (defface fg:erc-color-face0 '((t :foreground "White"))
236 "ERC face."
237 :group 'erc-faces)
238 (defface fg:erc-color-face1 '((t :foreground "black"))
239 "ERC face."
240 :group 'erc-faces)
241 (defface fg:erc-color-face2 '((t :foreground "blue4"))
242 "ERC face."
243 :group 'erc-faces)
244 (defface fg:erc-color-face3 '((t :foreground "green4"))
245 "ERC face."
246 :group 'erc-faces)
247 (defface fg:erc-color-face4 '((t :foreground "red"))
248 "ERC face."
249 :group 'erc-faces)
250 (defface fg:erc-color-face5 '((t :foreground "brown"))
251 "ERC face."
252 :group 'erc-faces)
253 (defface fg:erc-color-face6 '((t :foreground "purple"))
254 "ERC face."
255 :group 'erc-faces)
256 (defface fg:erc-color-face7 '((t :foreground "orange"))
257 "ERC face."
258 :group 'erc-faces)
259 (defface fg:erc-color-face8 '((t :foreground "yellow"))
260 "ERC face."
261 :group 'erc-faces)
262 (defface fg:erc-color-face9 '((t :foreground "green"))
263 "ERC face."
264 :group 'erc-faces)
265 (defface fg:erc-color-face10 '((t :foreground "lightblue1"))
266 "ERC face."
267 :group 'erc-faces)
268 (defface fg:erc-color-face11 '((t :foreground "cyan"))
269 "ERC face."
270 :group 'erc-faces)
271 (defface fg:erc-color-face12 '((t :foreground "blue"))
272 "ERC face."
273 :group 'erc-faces)
274 (defface fg:erc-color-face13 '((t :foreground "deeppink"))
275 "ERC face."
276 :group 'erc-faces)
277 (defface fg:erc-color-face14 '((t :foreground "gray50"))
278 "ERC face."
279 :group 'erc-faces)
280 (defface fg:erc-color-face15 '((t :foreground "gray90"))
281 "ERC face."
282 :group 'erc-faces)
284 (defface bg:erc-color-face0 '((t :background "White"))
285 "ERC face."
286 :group 'erc-faces)
287 (defface bg:erc-color-face1 '((t :background "black"))
288 "ERC face."
289 :group 'erc-faces)
290 (defface bg:erc-color-face2 '((t :background "blue4"))
291 "ERC face."
292 :group 'erc-faces)
293 (defface bg:erc-color-face3 '((t :background "green4"))
294 "ERC face."
295 :group 'erc-faces)
296 (defface bg:erc-color-face4 '((t :background "red"))
297 "ERC face."
298 :group 'erc-faces)
299 (defface bg:erc-color-face5 '((t :background "brown"))
300 "ERC face."
301 :group 'erc-faces)
302 (defface bg:erc-color-face6 '((t :background "purple"))
303 "ERC face."
304 :group 'erc-faces)
305 (defface bg:erc-color-face7 '((t :background "orange"))
306 "ERC face."
307 :group 'erc-faces)
308 (defface bg:erc-color-face8 '((t :background "yellow"))
309 "ERC face."
310 :group 'erc-faces)
311 (defface bg:erc-color-face9 '((t :background "green"))
312 "ERC face."
313 :group 'erc-faces)
314 (defface bg:erc-color-face10 '((t :background "lightblue1"))
315 "ERC face."
316 :group 'erc-faces)
317 (defface bg:erc-color-face11 '((t :background "cyan"))
318 "ERC face."
319 :group 'erc-faces)
320 (defface bg:erc-color-face12 '((t :background "blue"))
321 "ERC face."
322 :group 'erc-faces)
323 (defface bg:erc-color-face13 '((t :background "deeppink"))
324 "ERC face."
325 :group 'erc-faces)
326 (defface bg:erc-color-face14 '((t :background "gray50"))
327 "ERC face."
328 :group 'erc-faces)
329 (defface bg:erc-color-face15 '((t :background "gray90"))
330 "ERC face."
331 :group 'erc-faces)
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))
337 (prog1 'default
338 (erc-error "erc-get-bg-color-face: n is NaN: %S" n))
339 (when (> n 16)
340 (erc-log (format " Wrong color: %s" n))
341 (setq n (mod n 16)))
342 (cond
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))
351 (prog1 'default
352 (erc-error "erc-get-fg-color-face: n is NaN: %S" n))
353 (when (> n 16)
354 (erc-log (format " Wrong color: %s" n))
355 (setq n (mod n 16)))
356 (cond
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."
371 (when str
372 (let ((s str))
373 (cond ((eq erc-interpret-controls-p 'remove)
374 (erc-controls-strip s))
375 (erc-interpret-controls-p
376 (let ((boldp nil)
377 (inversep nil)
378 (underlinep nil)
379 (fg nil)
380 (bg nil))
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))
391 (setq fg fg-color)
392 (setq bg 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")
400 (setq fg nil
401 bg nil))
402 ((string= control "\C-g")
403 (when erc-beep-p
404 (ding)))
405 ((string= control "\C-o")
406 (setq boldp nil
407 inversep nil
408 underlinep nil
409 fg nil
410 bg nil))
411 (t nil))
412 (erc-controls-propertize
413 start end boldp inversep underlinep fg bg s)))
415 (t s)))))
417 (defun erc-controls-strip (str)
418 "Return a copy of STR with all IRC control characters removed."
419 (when str
420 (let ((s str))
421 (while (string-match erc-controls-remove-regexp s)
422 (setq s (replace-match "" nil nil s)))
423 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)
442 (replace-match "")))
443 (erc-interpret-controls-p
444 (let ((boldp nil)
445 (inversep nil)
446 (underlinep nil)
447 (fg nil)
448 (bg nil))
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))
457 (setq fg fg-color)
458 (setq bg 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")
466 (setq fg nil
467 bg nil))
468 ((string= control "\C-g")
469 (when erc-beep-p
470 (ding)))
471 ((string= control "\C-o")
472 (setq boldp nil
473 inversep nil
474 underlinep nil
475 fg nil
476 bg nil))
477 (t nil))
478 (erc-controls-propertize start end
479 boldp inversep underlinep fg bg)))))
480 (t nil)))
482 (defun erc-controls-propertize (from to boldp inversep underlinep fg bg
483 &optional str)
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
488 from
490 'font-lock-face
491 (append (if boldp
492 '(erc-bold-face)
493 nil)
494 (if inversep
495 '(erc-inverse-face)
496 nil)
497 (if underlinep
498 '(erc-underline-face)
499 nil)
500 (if fg
501 (list (erc-get-fg-color-face fg))
502 nil)
503 (if bg
504 (list (erc-get-bg-color-face bg))
505 nil))
506 str)
507 str)
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."
514 (interactive "P")
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")))
522 ;; Smiley
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)))
532 (defun erc-smiley ()
533 "Smilify a region.
534 This function should be used with `erc-insert-modify-hook'."
535 (when (fboundp 'smiley-region)
536 (smiley-region (point-min) (point-max))))
538 ;; Unmorse
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 ()
545 "Unmorse some text.
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)
552 (save-restriction
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)
557 (replace-match " "))
558 ;; Turn "/ " into "/"
559 (goto-char (point-min))
560 (while (re-search-forward "/ " nil t)
561 (replace-match "/"))
562 ;; Unmorse region
563 (unmorse-region (point-min) (point-max)))))
565 ;;; erc-occur
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."
570 (interactive
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