1;;; erc-fill.el --- Filling IRC messages in various ways
2
3;; Copyright (C) 2001, 2002, 2003, 2004, 2006,
4;;   2007 Free Software Foundation, Inc.
5
6;; Author: Andreas Fuchs <asf@void.at>
7;;         Mario Lang <mlang@delysid.org>
8;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcFilling
9
10;; This file is part of GNU Emacs.
11
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 2, or (at your option)
15;; any later version.
16
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.
21
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.
26
27;;; Commentary:
28
29;; This package implements filling of messages sent and received.  Use
30;; `erc-fill-mode' to switch it on.  Customize `erc-fill-function' to
31;; change the style.
32
33;;; Code:
34
35(require 'erc)
36(require 'erc-stamp); for the timestamp stuff
37
38(defgroup erc-fill nil
39  "Filling means to reformat long lines in different ways."
40  :group 'erc)
41
42;;;###autoload (autoload 'erc-fill-mode "erc-fill" nil t)
43(erc-define-minor-mode erc-fill-mode
44  "Toggle ERC fill mode.
45With numeric arg, turn ERC fill mode on if and only if arg is
46positive.  In ERC fill mode, messages in the channel buffers are
47filled."
48  nil nil nil
49  :global t :group 'erc-fill
50  (if erc-fill-mode
51      (erc-fill-enable)
52    (erc-fill-disable)))
53
54(defun erc-fill-enable ()
55  "Setup hooks for `erc-fill-mode'."
56  (interactive)
57  (add-hook 'erc-insert-modify-hook 'erc-fill)
58  (add-hook 'erc-send-modify-hook 'erc-fill))
59
60(defun erc-fill-disable ()
61  "Cleanup hooks, disable `erc-fill-mode'."
62  (interactive)
63  (remove-hook 'erc-insert-modify-hook 'erc-fill)
64  (remove-hook 'erc-send-modify-hook 'erc-fill))
65
66(defcustom erc-fill-prefix nil
67  "Values used as `fill-prefix' for `erc-fill-variable'.
68nil means fill with space, a string means fill with this string."
69  :group 'erc-fill
70  :type '(choice (const nil) string))
71
72(defcustom erc-fill-function 'erc-fill-variable
73  "Function to use for filling messages.
74
75Variable Filling with an `erc-fill-prefix' of nil:
76
77<shortnick> this is a very very very long message with no
78	    meaning at all
79
80Variable Filling with an `erc-fill-prefix' of four spaces:
81
82<shortnick> this is a very very very long message with no
83    meaning at all
84
85Static Filling with `erc-fill-static-center' of 27:
86
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
90			    meaning at all
91
92These two styles are implemented using `erc-fill-variable' and
93`erc-fill-static'.  You can, of course, define your own filling
94function.  Narrowing to the region in question is in effect while your
95function is called."
96  :group 'erc-fill
97  :type '(choice (const :tag "Variable Filling" erc-fill-variable)
98                 (const :tag "Static Filling" erc-fill-static)
99                 function))
100
101(defcustom erc-fill-static-center 27
102  "Column around which all statically filled messages will be
103centered.  This column denotes the point where the ' ' character
104between <nickname> and the entered text will be put, thus aligning
105nick names right and text left."
106  :group 'erc-fill
107  :type 'integer)
108
109(defcustom erc-fill-variable-maximum-indentation 17
110  "If we indent a line after a long nick, don't indent more then this
111characters. Set to nil to disable."
112  :group 'erc-fill
113  :type 'integer)
114
115(defcustom erc-fill-column 78
116  "The column at which a filled paragraph is broken."
117  :group 'erc-fill
118  :type 'integer)
119
120;;;###autoload
121(defun erc-fill ()
122  "Fill a region using the function referenced in `erc-fill-function'.
123You 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))
128      (save-match-data
129        (while (and (looking-at "[ \t\n]*$")
130                    (= (forward-line 1) 0))))
131      (unless (eobp)
132        (save-restriction
133          (narrow-to-region (point) (point-max))
134          (funcall erc-fill-function))))))
135
136(defun erc-fill-static ()
137  "Fills a text such that messages start at column `erc-fill-static-center'."
138  (save-match-data
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
145                                         (length nick) 1))
146                               32))
147          (erc-fill-regarding-timestamp))
148        (erc-restore-text-properties))))
149
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))
155    (if fill-prefix
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)))
161      (save-match-data
162        (let* ((nickp (looking-at "^\\(\\S-+\\)"))
163               (nick (if nickp
164                         (match-string 1)
165                       ""))
166               (fill-column (- erc-fill-column (erc-timestamp-offset)))
167               (fill-prefix (make-string (min (+ 1 (length nick))
168                                              (- fill-column 1)
169                                              (or erc-fill-variable-maximum-indentation
170                                                  fill-column))
171                                         32)))
172          (erc-fill-regarding-timestamp))))
173    (erc-restore-text-properties)))
174
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))
179  (forward-line)
180  (indent-rigidly (point) (point-max) (erc-timestamp-offset)))
181
182(defun erc-timestamp-offset ()
183  "Get length of timestamp if inserted left."
184  (if (and (boundp 'erc-timestamp-format)
185           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))
189    0))
190
191(provide 'erc-fill)
192
193;;; erc-fill.el ends here
194;; Local Variables:
195;; indent-tabs-mode: nil
196;; End:
197
198;; arch-tag: 89224581-c2c2-4e26-92e5-e3a390dc516a
199