1 ;;; erc-fill.el --- Filling IRC messages in various ways
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2006,
4 ;; 2007 Free Software Foundation, Inc.
6 ;; Author: Andreas Fuchs <asf@void.at>
7 ;; Mario Lang <mlang@delysid.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, or (at your option)
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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; This package implements filling of messages sent and received. Use
30 ;; `erc-fill-mode' to switch it on. Customize `erc-fill-function' to
36 (require 'erc-stamp
); for the timestamp stuff
38 (defgroup erc-fill nil
39 "Filling means to reformat long lines in different ways."
42 ;;;###autoload (autoload 'erc-fill-mode "erc-fill" nil t)
43 (erc-define-minor-mode erc-fill-mode
44 "Toggle ERC fill mode.
45 With numeric arg, turn ERC fill mode on if and only if arg is
46 positive. In ERC fill mode, messages in the channel buffers are
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
198 ;; arch-tag: 89224581-c2c2-4e26-92e5-e3a390dc516a