1 ;;; erc-fill.el --- Filling IRC messages in various ways
3 ;; Copyright (C) 2001-2004, 2006-2012 Free Software Foundation, Inc.
5 ;; Author: Andreas Fuchs <asf@void.at>
6 ;; Mario Lang <mlang@delysid.org>
7 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcFilling
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This package implements filling of messages sent and received. Use
27 ;; `erc-fill-mode' to switch it on. Customize `erc-fill-function' to
33 (require 'erc-stamp
); for the timestamp stuff
35 (defgroup erc-fill nil
36 "Filling means to reformat long lines in different ways."
39 ;;;###autoload (autoload 'erc-fill-mode "erc-fill" nil t)
40 (erc-define-minor-mode erc-fill-mode
41 "Toggle ERC fill mode.
42 With a prefix argument ARG, enable ERC fill mode if ARG is
43 positive, and disable it otherwise. If called from Lisp, enable
44 the mode if ARG is omitted or nil.
46 ERC fill mode is a global minor mode. When enabled, messages in
47 the channel buffers are filled."
49 :global t
:group
'erc-fill
54 (defun erc-fill-enable ()
55 "Setup hooks for `erc-fill-mode'."
57 (add-hook 'erc-insert-modify-hook
'erc-fill
)
58 (add-hook 'erc-send-modify-hook
'erc-fill
))
60 (defun erc-fill-disable ()
61 "Cleanup hooks, disable `erc-fill-mode'."
63 (remove-hook 'erc-insert-modify-hook
'erc-fill
)
64 (remove-hook 'erc-send-modify-hook
'erc-fill
))
66 (defcustom erc-fill-prefix nil
67 "Values used as `fill-prefix' for `erc-fill-variable'.
68 nil means fill with space, a string means fill with this string."
70 :type
'(choice (const nil
) string
))
72 (defcustom erc-fill-function
'erc-fill-variable
73 "Function to use for filling messages.
75 Variable Filling with an `erc-fill-prefix' of nil:
77 <shortnick> this is a very very very long message with no
80 Variable Filling with an `erc-fill-prefix' of four spaces:
82 <shortnick> this is a very very very long message with no
85 Static Filling with `erc-fill-static-center' of 27:
87 <shortnick> foo bar baz
88 <a-very-long-nick> foo bar baz quuuuux
89 <shortnick> this is a very very very long message with no
92 These two styles are implemented using `erc-fill-variable' and
93 `erc-fill-static'. You can, of course, define your own filling
94 function. Narrowing to the region in question is in effect while your
97 :type
'(choice (const :tag
"Variable Filling" erc-fill-variable
)
98 (const :tag
"Static Filling" erc-fill-static
)
101 (defcustom erc-fill-static-center
27
102 "Column around which all statically filled messages will be
103 centered. This column denotes the point where the ' ' character
104 between <nickname> and the entered text will be put, thus aligning
105 nick names right and text left."
109 (defcustom erc-fill-variable-maximum-indentation
17
110 "If we indent a line after a long nick, don't indent more then this
111 characters. Set to nil to disable."
115 (defcustom erc-fill-column
78
116 "The column at which a filled paragraph is broken."
122 "Fill a region using the function referenced in `erc-fill-function'.
123 You can put this on `erc-insert-modify-hook' and/or `erc-send-modify-hook'."
124 (unless (erc-string-invisible-p (buffer-substring (point-min) (point-max)))
125 (when erc-fill-function
126 ;; skip initial empty lines
127 (goto-char (point-min))
129 (while (and (looking-at "[ \t\n]*$")
130 (= (forward-line 1) 0))))
133 (narrow-to-region (point) (point-max))
134 (funcall erc-fill-function
))))))
136 (defun erc-fill-static ()
137 "Fills a text such that messages start at column `erc-fill-static-center'."
139 (goto-char (point-min))
140 (looking-at "^\\(\\S-+\\)")
141 (let ((nick (match-string 1)))
142 (let ((fill-column (- erc-fill-column
(erc-timestamp-offset)))
143 (fill-prefix (make-string erc-fill-static-center
32)))
144 (insert (make-string (max 0 (- erc-fill-static-center
147 (erc-fill-regarding-timestamp))
148 (erc-restore-text-properties))))
150 (defun erc-fill-variable ()
151 "Fill from `point-min' to `point-max'."
152 (let ((fill-prefix erc-fill-prefix
)
153 (fill-column (or erc-fill-column fill-column
)))
154 (goto-char (point-min))
156 (let ((first-line-offset (make-string (erc-timestamp-offset) 32)))
157 (insert first-line-offset
)
158 (fill-region (point-min) (point-max) t t
)
159 (goto-char (point-min))
160 (delete-char (length first-line-offset
)))
162 (let* ((nickp (looking-at "^\\(\\S-+\\)"))
166 (fill-column (- erc-fill-column
(erc-timestamp-offset)))
167 (fill-prefix (make-string (min (+ 1 (length nick
))
169 (or erc-fill-variable-maximum-indentation
172 (erc-fill-regarding-timestamp))))
173 (erc-restore-text-properties)))
175 (defun erc-fill-regarding-timestamp ()
176 "Fills a text such that messages start at column `erc-fill-static-center'."
177 (fill-region (point-min) (point-max) t t
)
178 (goto-char (point-min))
180 (indent-rigidly (point) (point-max) (erc-timestamp-offset)))
182 (defun erc-timestamp-offset ()
183 "Get length of timestamp if inserted left."
184 (if (and (boundp 'erc-timestamp-format
)
186 (eq erc-insert-timestamp-function
'erc-insert-timestamp-left
)
187 (not erc-hide-timestamps
))
188 (length (format-time-string erc-timestamp-format
))
193 ;;; erc-fill.el ends here
195 ;; indent-tabs-mode: nil