Commit | Line | Data |
---|---|---|
597993cf MB |
1 | ;;; erc-fill.el --- Filling IRC messages in various ways |
2 | ||
ff59d266 | 3 | ;; Copyright (C) 2001, 2002, 2003, 2004, 2006, |
114f9c96 | 4 | ;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
597993cf MB |
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 | ||
4ee57b2a | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
597993cf | 13 | ;; it under the terms of the GNU General Public License as published by |
4ee57b2a GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
597993cf MB |
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 | |
4ee57b2a | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
597993cf MB |
24 | |
25 | ;;; Commentary: | |
26 | ||
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. | |
30 | ||
31 | ;;; Code: | |
32 | ||
33 | (require 'erc) | |
34 | (require 'erc-stamp); for the timestamp stuff | |
35 | ||
36 | (defgroup erc-fill nil | |
37 | "Filling means to reformat long lines in different ways." | |
38 | :group 'erc) | |
39 | ||
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 | |
45 | filled." | |
46 | nil nil nil | |
47 | :global t :group 'erc-fill | |
48 | (if erc-fill-mode | |
49 | (erc-fill-enable) | |
50 | (erc-fill-disable))) | |
51 | ||
52 | (defun erc-fill-enable () | |
53 | "Setup hooks for `erc-fill-mode'." | |
54 | (interactive) | |
55 | (add-hook 'erc-insert-modify-hook 'erc-fill) | |
56 | (add-hook 'erc-send-modify-hook 'erc-fill)) | |
57 | ||
58 | (defun erc-fill-disable () | |
59 | "Cleanup hooks, disable `erc-fill-mode'." | |
60 | (interactive) | |
61 | (remove-hook 'erc-insert-modify-hook 'erc-fill) | |
62 | (remove-hook 'erc-send-modify-hook 'erc-fill)) | |
63 | ||
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." | |
67 | :group 'erc-fill | |
68 | :type '(choice (const nil) string)) | |
69 | ||
70 | (defcustom erc-fill-function 'erc-fill-variable | |
71 | "Function to use for filling messages. | |
72 | ||
73 | Variable Filling with an `erc-fill-prefix' of nil: | |
74 | ||
75 | <shortnick> this is a very very very long message with no | |
76 | meaning at all | |
77 | ||
78 | Variable Filling with an `erc-fill-prefix' of four spaces: | |
79 | ||
80 | <shortnick> this is a very very very long message with no | |
81 | meaning at all | |
82 | ||
83 | Static Filling with `erc-fill-static-center' of 27: | |
84 | ||
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 | |
88 | meaning at all | |
89 | ||
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 | |
93 | function is called." | |
94 | :group 'erc-fill | |
95 | :type '(choice (const :tag "Variable Filling" erc-fill-variable) | |
96 | (const :tag "Static Filling" erc-fill-static) | |
97 | function)) | |
98 | ||
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." | |
104 | :group 'erc-fill | |
105 | :type 'integer) | |
106 | ||
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." | |
110 | :group 'erc-fill | |
111 | :type 'integer) | |
112 | ||
113 | (defcustom erc-fill-column 78 | |
114 | "The column at which a filled paragraph is broken." | |
115 | :group 'erc-fill | |
116 | :type 'integer) | |
117 | ||
118 | ;;;###autoload | |
119 | (defun erc-fill () | |
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 | |
83dc6995 MB |
124 | ;; skip initial empty lines |
125 | (goto-char (point-min)) | |
126 | (save-match-data | |
127 | (while (and (looking-at "[ \t\n]*$") | |
128 | (= (forward-line 1) 0)))) | |
129 | (unless (eobp) | |
130 | (save-restriction | |
131 | (narrow-to-region (point) (point-max)) | |
132 | (funcall erc-fill-function)))))) | |
597993cf MB |
133 | |
134 | (defun erc-fill-static () | |
135 | "Fills a text such that messages start at column `erc-fill-static-center'." | |
136 | (save-match-data | |
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 | |
143 | (length nick) 1)) | |
144 | 32)) | |
145 | (erc-fill-regarding-timestamp)) | |
146 | (erc-restore-text-properties)))) | |
147 | ||
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)) | |
153 | (if fill-prefix | |
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))) | |
159 | (save-match-data | |
160 | (let* ((nickp (looking-at "^\\(\\S-+\\)")) | |
161 | (nick (if nickp | |
162 | (match-string 1) | |
163 | "")) | |
164 | (fill-column (- erc-fill-column (erc-timestamp-offset))) | |
165 | (fill-prefix (make-string (min (+ 1 (length nick)) | |
166 | (- fill-column 1) | |
167 | (or erc-fill-variable-maximum-indentation | |
168 | fill-column)) | |
169 | 32))) | |
170 | (erc-fill-regarding-timestamp)))) | |
171 | (erc-restore-text-properties))) | |
172 | ||
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)) | |
177 | (forward-line) | |
178 | (indent-rigidly (point) (point-max) (erc-timestamp-offset))) | |
179 | ||
180 | (defun erc-timestamp-offset () | |
181 | "Get length of timestamp if inserted left." | |
182 | (if (and (boundp 'erc-timestamp-format) | |
183 | 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)) | |
187 | 0)) | |
188 | ||
597993cf MB |
189 | (provide 'erc-fill) |
190 | ||
191 | ;;; erc-fill.el ends here | |
192 | ;; Local Variables: | |
193 | ;; indent-tabs-mode: nil | |
194 | ;; End: | |
195 | ||
196 | ;; arch-tag: 89224581-c2c2-4e26-92e5-e3a390dc516a |