lib/Makefile.am: added lib/skin.h into distribution
[midnight-commander.git] / src / editor / wordproc.c
blobed4db5a3f539f2ab702eb401f2bb8a25eea6bd92
1 /* wordproc.c - word-processor mode for the editor: does dynamic
2 paragraph formatting.
3 Copyright (C) 1996 Paul Sheer
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA.
21 /** \file
22 * \brief Source: word-processor mode for the editor: does dynamic paragraph formatting
23 * \author Paul Sheer
24 * \date 1996
27 #include <config.h>
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <sys/stat.h>
38 #include <stdlib.h>
40 #include "lib/global.h"
42 #include "edit-impl.h"
43 #include "edit-widget.h"
44 #include "src/main.h" /* option_tab_spacing */
46 #define tab_width option_tab_spacing
48 #define NO_FORMAT_CHARS_START "-+*\\,.;:&>"
49 #define FONT_MEAN_WIDTH 1
51 static long
52 line_start (WEdit *edit, long line)
54 long p, l;
56 l = edit->curs_line;
57 p = edit->curs1;
59 if (line < l)
60 p = edit_move_backward (edit, p, l - line);
61 else if (line > l)
62 p = edit_move_forward (edit, p, line - l, 0);
64 p = edit_bol (edit, p);
65 while (strchr ("\t ", edit_get_byte (edit, p)))
66 p++;
67 return p;
70 static int bad_line_start (WEdit * edit, long p)
72 int c;
73 c = edit_get_byte (edit, p);
74 if (c == '.') { /* `...' is acceptable */
75 if (edit_get_byte (edit, p + 1) == '.')
76 if (edit_get_byte (edit, p + 2) == '.')
77 return 0;
78 return 1;
80 if (c == '-') {
81 if (edit_get_byte (edit, p + 1) == '-')
82 if (edit_get_byte (edit, p + 2) == '-')
83 return 0; /* `---' is acceptable */
84 return 1;
86 if (strchr (NO_FORMAT_CHARS_START, c))
87 return 1;
88 return 0;
92 * Find the start of the current paragraph for the purpose of formatting.
93 * Return position in the file.
95 static long
96 begin_paragraph (WEdit *edit, int force)
98 long i;
99 for (i = edit->curs_line - 1; i >= 0; i--) {
100 if (line_is_blank (edit, i)) {
101 i++;
102 break;
104 if (force) {
105 if (bad_line_start (edit, line_start (edit, i))) {
106 i++;
107 break;
111 return edit_move_backward (edit, edit_bol (edit, edit->curs1),
112 edit->curs_line - i);
116 * Find the end of the current paragraph for the purpose of formatting.
117 * Return position in the file.
119 static long
120 end_paragraph (WEdit *edit, int force)
122 int i;
123 for (i = edit->curs_line + 1; i <= edit->total_lines; i++) {
124 if (line_is_blank (edit, i)) {
125 i--;
126 break;
128 if (force)
129 if (bad_line_start (edit, line_start (edit, i))) {
130 i--;
131 break;
134 return edit_eol (edit,
135 edit_move_forward (edit, edit_bol (edit, edit->curs1),
136 i - edit->curs_line, 0));
139 static unsigned char *
140 get_paragraph (WEdit *edit, long p, long q, int indent, int *size)
142 unsigned char *s, *t;
143 #if 0
144 t = g_try_malloc ((q - p) + 2 * (q - p) / option_word_wrap_line_length +
145 10);
146 #else
147 t = g_try_malloc (2 * (q - p) + 100);
148 #endif
149 if (t == NULL)
150 return NULL;
151 for (s = t; p < q; p++, s++) {
152 if (indent)
153 if (edit_get_byte (edit, p - 1) == '\n')
154 while (strchr ("\t ", edit_get_byte (edit, p)))
155 p++;
156 *s = edit_get_byte (edit, p);
158 *size = (unsigned long) s - (unsigned long) t;
159 t[*size] = '\n';
160 return t;
163 static inline void
164 strip_newlines (unsigned char *t, int size)
166 unsigned char *p = t;
167 while (size-- != 0) {
168 if (*p == '\n')
169 *p = ' ';
170 p++;
175 This function calculates the number of chars in a line specified to length l in pixels
177 static inline int
178 next_tab_pos (int x)
180 return x += tab_width - x % tab_width;
183 static inline int
184 line_pixel_length (unsigned char *t, long b, int l)
186 int x = 0, c, xn = 0;
187 for (;;) {
188 c = t[b];
189 switch (c) {
190 case '\n':
191 return b;
192 case '\t':
193 xn = next_tab_pos (x);
194 break;
195 default:
196 xn = x + 1;
197 break;
199 if (xn > l)
200 break;
201 x = xn;
202 b++;
204 return b;
207 static int
208 next_word_start (unsigned char *t, int q, int size)
210 int i;
211 int saw_ws = 0;
213 for (i = q; i < size; i++) {
214 switch (t[i]) {
215 case '\n':
216 return -1;
217 case '\t':
218 case ' ':
219 saw_ws = 1;
220 break;
221 default:
222 if (saw_ws != 0)
223 return i;
224 break;
227 return -1;
230 /* find the start of a word */
231 static inline int
232 word_start (unsigned char *t, int q, int size)
234 int i = q;
235 if (t[q] == ' ' || t[q] == '\t')
236 return next_word_start (t, q, size);
237 for (;;) {
238 int c;
239 if (!i)
240 return -1;
241 c = t[i - 1];
242 if (c == '\n')
243 return -1;
244 if (c == ' ' || c == '\t')
245 return i;
246 i--;
250 /* replaces ' ' with '\n' to properly format a paragraph */
251 static inline void
252 format_this (unsigned char *t, int size, int indent)
254 int q = 0, ww;
255 strip_newlines (t, size);
256 ww = option_word_wrap_line_length * FONT_MEAN_WIDTH - indent;
257 if (ww < FONT_MEAN_WIDTH * 2)
258 ww = FONT_MEAN_WIDTH * 2;
259 for (;;) {
260 int p;
261 q = line_pixel_length (t, q, ww);
262 if (q > size)
263 break;
264 if (t[q] == '\n')
265 break;
266 p = word_start (t, q, size);
267 if (p == -1)
268 q = next_word_start (t, q, size); /* Return the end of the word if the beginning
269 of the word is at the beginning of a line
270 (i.e. a very long word) */
271 else
272 q = p;
273 if (q == -1) /* end of paragraph */
274 break;
275 if (q)
276 t[q - 1] = '\n';
280 static inline void
281 replace_at (WEdit * edit, long q, int c)
283 edit_cursor_move (edit, q - edit->curs1);
284 edit_delete (edit, 1);
285 edit_insert_ahead (edit, c);
288 /* replaces a block of text */
289 static inline void
290 put_paragraph (WEdit * edit, unsigned char *t, long p, int indent, int size)
292 long cursor;
293 int i, c = 0;
294 cursor = edit->curs1;
295 if (indent)
296 while (strchr ("\t ", edit_get_byte (edit, p)))
297 p++;
298 for (i = 0; i < size; i++, p++) {
299 if (i && indent) {
300 if (t[i - 1] == '\n' && c == '\n') {
301 while (strchr ("\t ", edit_get_byte (edit, p)))
302 p++;
303 } else if (t[i - 1] == '\n') {
304 long curs;
305 edit_cursor_move (edit, p - edit->curs1);
306 curs = edit->curs1;
307 edit_insert_indent (edit, indent);
308 if (cursor >= curs)
309 cursor += edit->curs1 - p;
310 p = edit->curs1;
311 } else if (c == '\n') {
312 edit_cursor_move (edit, p - edit->curs1);
313 while (strchr ("\t ", edit_get_byte (edit, p))) {
314 edit_delete (edit, 1);
315 if (cursor > edit->curs1)
316 cursor--;
318 p = edit->curs1;
321 c = edit_get_byte (edit, p);
322 if (c != t[i])
323 replace_at (edit, p, t[i]);
325 edit_cursor_move (edit, cursor - edit->curs1); /* restore cursor position */
328 static inline int
329 test_indent (WEdit * edit, long p, long q)
331 int indent;
332 indent = edit_indent_width (edit, p++);
333 if (!indent)
334 return 0;
335 for (; p < q; p++)
336 if (edit_get_byte (edit, p - 1) == '\n')
337 if (indent != edit_indent_width (edit, p))
338 return 0;
339 return indent;
342 void
343 format_paragraph (WEdit *edit, int force)
345 long p, q;
346 int size;
347 unsigned char *t;
348 int indent = 0;
349 if (option_word_wrap_line_length < 2)
350 return;
351 if (line_is_blank (edit, edit->curs_line))
352 return;
353 p = begin_paragraph (edit, force);
354 q = end_paragraph (edit, force);
355 indent = test_indent (edit, p, q);
356 t = get_paragraph (edit, p, q, indent, &size);
357 if (!t)
358 return;
359 if (!force) {
360 int i;
361 if (strchr (NO_FORMAT_CHARS_START, *t)) {
362 g_free (t);
363 return;
365 for (i = 0; i < size - 1; i++) {
366 if (t[i] == '\n') {
367 if (strchr (NO_FORMAT_CHARS_START "\t ", t[i + 1])) {
368 g_free (t);
369 return;
374 format_this (t, q - p, indent);
375 put_paragraph (edit, t, p, indent, size);
376 g_free (t);
378 /* Scroll left as much as possible to show the formatted paragraph */
379 edit_scroll_left (edit, -edit->start_col);