1 ;;; erc-fill.el --- Filling IRC messages in various ways
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2006,
4 ;; 2007, 2008, 2009 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 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/>.
27 ;; This package implements filling of messages sent and received. Use
28 ;; `erc-fill-mode' to switch it on. Customize `erc-fill-function' to
34 (require 'erc-stamp
); for the timestamp stuff
36 (defgroup erc-fill nil
37 "Filling means to reformat long lines in different ways."
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 numeric arg, turn ERC fill mode on if and only if arg is
44 positive. In ERC fill mode, messages in the channel buffers are
47 :global t
:group
'erc-fill
52 (defun erc-fill-enable ()
53 "Setup hooks for `erc-fill-mode'."
55 (add-hook 'erc-insert-modify-hook
'erc-fill
)
56 (add-hook 'erc-send-modify-hook
'erc-fill
))
58 (defun erc-fill-disable ()
59 "Cleanup hooks, disable `erc-fill-mode'."
61 (remove-hook 'erc-insert-modify-hook
'erc-fill
)
62 (remove-hook 'erc-send-modify-hook
'erc-fill
))
64 (defcustom erc-fill-prefix nil
65 "Values used as `fill-prefix' for `erc-fill-variable'.
66 nil means fill with space, a string means fill with this string."
68 :type
'(choice (const nil
) string
))
70 (defcustom erc-fill-function
'erc-fill-variable
71 "Function to use for filling messages.
73 Variable Filling with an `erc-fill-prefix' of nil:
75 <shortnick> this is a very very very long message with no
78 Variable Filling with an `erc-fill-prefix' of four spaces:
80 <shortnick> this is a very very very long message with no
83 Static Filling with `erc-fill-static-center' of 27:
85 <shortnick> foo bar baz
86 <a-very-long-nick> foo bar baz quuuuux
87 <shortnick> this is a very very very long message with no
90 These two styles are implemented using `erc-fill-variable' and
91 `erc-fill-static'. You can, of course, define your own filling
92 function. Narrowing to the region in question is in effect while your
95 :type
'(choice (const :tag
"Variable Filling" erc-fill-variable
)
96 (const :tag
"Static Filling" erc-fill-static
)
99 (defcustom erc-fill-static-center
27
100 "Column around which all statically filled messages will be
101 centered. This column denotes the point where the ' ' character
102 between <nickname> and the entered text will be put, thus aligning
103 nick names right and text left."
107 (defcustom erc-fill-variable-maximum-indentation
17
108 "If we indent a line after a long nick, don't indent more then this
109 characters. Set to nil to disable."
113 (defcustom erc-fill-column
78
114 "The column at which a filled paragraph is broken."
120 "Fill a region using the function referenced in `erc-fill-function'.
121 You can put this on `erc-insert-modify-hook' and/or `erc-send-modify-hook'."
122 (unless (erc-string-invisible-p (buffer-substring (point-min) (point-max)))
123 (when erc-fill-function
124 ;; skip initial empty lines
125 (goto-char (point-min))
127 (while (and (looking-at "[ \t\n]*$")
128 (= (forward-line 1) 0))))
131 (narrow-to-region (point) (point-max))
132 (funcall erc-fill-function
))))))
134 (defun erc-fill-static ()
135 "Fills a text such that messages start at column `erc-fill-static-center'."
137 (goto-char (point-min))
138 (looking-at "^\\(\\S-+\\)")
139 (let ((nick (match-string 1)))
140 (let ((fill-column (- erc-fill-column
(erc-timestamp-offset)))
141 (fill-prefix (make-string erc-fill-static-center
32)))
142 (insert (make-string (max 0 (- erc-fill-static-center
145 (erc-fill-regarding-timestamp))
146 (erc-restore-text-properties))))
148 (defun erc-fill-variable ()
149 "Fill from `point-min' to `point-max'."
150 (let ((fill-prefix erc-fill-prefix
)
151 (fill-column (or erc-fill-column fill-column
)))
152 (goto-char (point-min))
154 (let ((first-line-offset (make-string (erc-timestamp-offset) 32)))
155 (insert first-line-offset
)
156 (fill-region (point-min) (point-max) t t
)
157 (goto-char (point-min))
158 (delete-char (length first-line-offset
)))
160 (let* ((nickp (looking-at "^\\(\\S-+\\)"))
164 (fill-column (- erc-fill-column
(erc-timestamp-offset)))
165 (fill-prefix (make-string (min (+ 1 (length nick
))
167 (or erc-fill-variable-maximum-indentation
170 (erc-fill-regarding-timestamp))))
171 (erc-restore-text-properties)))
173 (defun erc-fill-regarding-timestamp ()
174 "Fills a text such that messages start at column `erc-fill-static-center'."
175 (fill-region (point-min) (point-max) t t
)
176 (goto-char (point-min))
178 (indent-rigidly (point) (point-max) (erc-timestamp-offset)))
180 (defun erc-timestamp-offset ()
181 "Get length of timestamp if inserted left."
182 (if (and (boundp 'erc-timestamp-format
)
184 (eq erc-insert-timestamp-function
'erc-insert-timestamp-left
)
185 (not erc-hide-timestamps
))
186 (length (format-time-string erc-timestamp-format
))
191 ;;; erc-fill.el ends here
193 ;; indent-tabs-mode: nil
196 ;; arch-tag: 89224581-c2c2-4e26-92e5-e3a390dc516a