(eieio-copy-parents-into-subclass): Fix inheritance of initargs
[emacs.git] / lisp / erc / erc-fill.el
blob97c466825f0eba95400dec9fdfca2c1c82b76985
1 ;;; erc-fill.el --- Filling IRC messages in various ways
3 ;; Copyright (C) 2001-2004, 2006-2015 Free Software Foundation, Inc.
5 ;; Author: Andreas Fuchs <asf@void.at>
6 ;; Mario Lang <mlang@delysid.org>
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcFilling
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/>.
25 ;;; Commentary:
27 ;; This package implements filling of messages sent and received. Use
28 ;; `erc-fill-mode' to switch it on. Customize `erc-fill-function' to
29 ;; change the style.
31 ;;; Code:
33 (require 'erc)
34 (require 'erc-stamp); for the timestamp stuff
36 (defgroup erc-fill nil
37 "Filling means to reformat long lines in different ways."
38 :group 'erc)
40 ;;;###autoload (autoload 'erc-fill-mode "erc-fill" nil t)
41 (erc-define-minor-mode erc-fill-mode
42 "Toggle ERC fill mode.
43 With a prefix argument ARG, enable ERC fill mode if ARG is
44 positive, and disable it otherwise. If called from Lisp, enable
45 the mode if ARG is omitted or nil.
47 ERC fill mode is a global minor mode. When enabled, messages in
48 the channel buffers are filled."
49 nil nil nil
50 :global t :group 'erc-fill
51 (if erc-fill-mode
52 (erc-fill-enable)
53 (erc-fill-disable)))
55 (defun erc-fill-enable ()
56 "Setup hooks for `erc-fill-mode'."
57 (interactive)
58 (add-hook 'erc-insert-modify-hook 'erc-fill)
59 (add-hook 'erc-send-modify-hook 'erc-fill))
61 (defun erc-fill-disable ()
62 "Cleanup hooks, disable `erc-fill-mode'."
63 (interactive)
64 (remove-hook 'erc-insert-modify-hook 'erc-fill)
65 (remove-hook 'erc-send-modify-hook 'erc-fill))
67 (defcustom erc-fill-prefix nil
68 "Values used as `fill-prefix' for `erc-fill-variable'.
69 nil means fill with space, a string means fill with this string."
70 :group 'erc-fill
71 :type '(choice (const nil) string))
73 (defcustom erc-fill-function 'erc-fill-variable
74 "Function to use for filling messages.
76 Variable Filling with an `erc-fill-prefix' of nil:
78 <shortnick> this is a very very very long message with no
79 meaning at all
81 Variable Filling with an `erc-fill-prefix' of four spaces:
83 <shortnick> this is a very very very long message with no
84 meaning at all
86 Static Filling with `erc-fill-static-center' of 27:
88 <shortnick> foo bar baz
89 <a-very-long-nick> foo bar baz quuuuux
90 <shortnick> this is a very very very long message with no
91 meaning at all
93 These two styles are implemented using `erc-fill-variable' and
94 `erc-fill-static'. You can, of course, define your own filling
95 function. Narrowing to the region in question is in effect while your
96 function is called."
97 :group 'erc-fill
98 :type '(choice (const :tag "Variable Filling" erc-fill-variable)
99 (const :tag "Static Filling" erc-fill-static)
100 function))
102 (defcustom erc-fill-static-center 27
103 "Column around which all statically filled messages will be
104 centered. This column denotes the point where the ' ' character
105 between <nickname> and the entered text will be put, thus aligning
106 nick names right and text left."
107 :group 'erc-fill
108 :type 'integer)
110 (defcustom erc-fill-variable-maximum-indentation 17
111 "If we indent a line after a long nick, don't indent more then this
112 characters. Set to nil to disable."
113 :group 'erc-fill
114 :type 'integer)
116 (defcustom erc-fill-column 78
117 "The column at which a filled paragraph is broken."
118 :group 'erc-fill
119 :type 'integer)
121 ;;;###autoload
122 (defun erc-fill ()
123 "Fill a region using the function referenced in `erc-fill-function'.
124 You can put this on `erc-insert-modify-hook' and/or `erc-send-modify-hook'."
125 (unless (erc-string-invisible-p (buffer-substring (point-min) (point-max)))
126 (when erc-fill-function
127 ;; skip initial empty lines
128 (goto-char (point-min))
129 (save-match-data
130 (while (and (looking-at "[ \t\n]*$")
131 (= (forward-line 1) 0))))
132 (unless (eobp)
133 (save-restriction
134 (narrow-to-region (point) (point-max))
135 (funcall erc-fill-function))))))
137 (defun erc-fill-static ()
138 "Fills a text such that messages start at column `erc-fill-static-center'."
139 (save-match-data
140 (goto-char (point-min))
141 (looking-at "^\\(\\S-+\\)")
142 (let ((nick (match-string 1)))
143 (let ((fill-column (- erc-fill-column (erc-timestamp-offset)))
144 (fill-prefix (make-string erc-fill-static-center 32)))
145 (insert (make-string (max 0 (- erc-fill-static-center
146 (length nick) 1))
147 32))
148 (erc-fill-regarding-timestamp))
149 (erc-restore-text-properties))))
151 (defun erc-fill-variable ()
152 "Fill from `point-min' to `point-max'."
153 (let ((fill-prefix erc-fill-prefix)
154 (fill-column (or erc-fill-column fill-column)))
155 (goto-char (point-min))
156 (if fill-prefix
157 (let ((first-line-offset (make-string (erc-timestamp-offset) 32)))
158 (insert first-line-offset)
159 (fill-region (point-min) (point-max) t t)
160 (goto-char (point-min))
161 (delete-char (length first-line-offset)))
162 (save-match-data
163 (let* ((nickp (looking-at "^\\(\\S-+\\)"))
164 (nick (if nickp
165 (match-string 1)
166 ""))
167 (fill-column (- erc-fill-column (erc-timestamp-offset)))
168 (fill-prefix (make-string (min (+ 1 (length nick))
169 (- fill-column 1)
170 (or erc-fill-variable-maximum-indentation
171 fill-column))
172 32)))
173 (erc-fill-regarding-timestamp))))
174 (erc-restore-text-properties)))
176 (defun erc-fill-regarding-timestamp ()
177 "Fills a text such that messages start at column `erc-fill-static-center'."
178 (fill-region (point-min) (point-max) t t)
179 (goto-char (point-min))
180 (forward-line)
181 (indent-rigidly (point) (point-max) (erc-timestamp-offset)))
183 (defun erc-timestamp-offset ()
184 "Get length of timestamp if inserted left."
185 (if (and (boundp 'erc-timestamp-format)
186 erc-timestamp-format
187 (eq erc-insert-timestamp-function 'erc-insert-timestamp-left)
188 (not erc-hide-timestamps))
189 (length (format-time-string erc-timestamp-format))
192 (provide 'erc-fill)
194 ;;; erc-fill.el ends here
195 ;; Local Variables:
196 ;; indent-tabs-mode: nil
197 ;; End: