1 /* wordproc.c - word-processor mode for the editor: does dynamic
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., 59 Temple Place, Suite 330, Boston, MA
24 #define tab_width option_tab_spacing
26 int line_is_blank (WEdit
* edit
, long line
);
28 #define NO_FORMAT_CHARS_START "-+*\\,.;:&>"
31 line_start (WEdit
*edit
, long line
)
39 p
= edit_move_backward (edit
, p
, l
- line
);
41 p
= edit_move_forward (edit
, p
, line
- l
, 0);
43 p
= edit_bol (edit
, p
);
44 while (strchr ("\t ", edit_get_byte (edit
, p
)))
49 static int bad_line_start (WEdit
* edit
, long p
)
52 c
= edit_get_byte (edit
, p
);
53 if (c
== '.') { /* `...' is acceptable */
54 if (edit_get_byte (edit
, p
+ 1) == '.')
55 if (edit_get_byte (edit
, p
+ 2) == '.')
60 if (edit_get_byte (edit
, p
+ 1) == '-')
61 if (edit_get_byte (edit
, p
+ 2) == '-')
62 return 0; /* `---' is acceptable */
65 if (strchr (NO_FORMAT_CHARS_START
, c
))
71 * Find the start of the current paragraph for the purpose of formatting.
72 * Return position in the file.
75 begin_paragraph (WEdit
*edit
, int force
)
78 for (i
= edit
->curs_line
- 1; i
>= 0; i
--) {
79 if (line_is_blank (edit
, i
)) {
84 if (bad_line_start (edit
, line_start (edit
, i
))) {
90 return edit_move_backward (edit
, edit_bol (edit
, edit
->curs1
),
95 * Find the end of the current paragraph for the purpose of formatting.
96 * Return position in the file.
99 end_paragraph (WEdit
*edit
, int force
)
102 for (i
= edit
->curs_line
+ 1; i
<= edit
->total_lines
; i
++) {
103 if (line_is_blank (edit
, i
)) {
108 if (bad_line_start (edit
, line_start (edit
, i
))) {
113 return edit_eol (edit
,
114 edit_move_forward (edit
, edit_bol (edit
, edit
->curs1
),
115 i
- edit
->curs_line
, 0));
118 static unsigned char *get_paragraph (WEdit
* edit
, long p
, long q
, int indent
, int *size
)
120 unsigned char *s
, *t
;
122 t
= malloc ((q
- p
) + 2 * (q
- p
) / option_word_wrap_line_length
+ 10);
124 t
= malloc (2 * (q
- p
) + 100);
128 for (s
= t
; p
< q
; p
++, s
++) {
130 if (edit_get_byte (edit
, p
- 1) == '\n')
131 while (strchr ("\t ", edit_get_byte (edit
, p
)))
133 *s
= edit_get_byte (edit
, p
);
135 *size
= (unsigned long) s
- (unsigned long) t
;
140 static void strip_newlines (unsigned char *t
, int size
)
142 unsigned char *p
= t
;
144 *p
= *p
== '\n' ? ' ' : *p
;
150 This is a copy of the function
151 int calc_text_pos (WEdit * edit, long b, long *q, int l)
153 It calculates the number of chars in a line specified to length l in pixels
155 static inline int next_tab_pos (int x
)
157 return x
+= tab_width
- x
% tab_width
;
159 static int line_pixel_length (unsigned char *t
, long b
, int l
)
161 int x
= 0, c
, xn
= 0;
168 xn
= next_tab_pos (x
);
182 /* find the start of a word */
183 static int next_word_start (unsigned char *t
, int q
, int size
)
195 if (t
[i
] != ' ' && t
[i
] != '\t')
203 /* find the start of a word */
204 static int word_start (unsigned char *t
, int q
, int size
)
207 if (t
[q
] == ' ' || t
[q
] == '\t')
208 return next_word_start (t
, q
, size
);
216 if (c
== ' ' || c
== '\t')
222 /* replaces ' ' with '\n' to properly format a paragraph */
223 static void format_this (unsigned char *t
, int size
, int indent
)
226 strip_newlines (t
, size
);
227 ww
= option_word_wrap_line_length
* FONT_MEAN_WIDTH
- indent
;
228 if (ww
< FONT_MEAN_WIDTH
* 2)
229 ww
= FONT_MEAN_WIDTH
* 2;
232 q
= line_pixel_length (t
, q
, ww
);
237 p
= word_start (t
, q
, size
);
239 q
= next_word_start (t
, q
, size
); /* Return the end of the word if the beginning
240 of the word is at the beginning of a line
241 (i.e. a very long word) */
244 if (q
== -1) /* end of paragraph */
251 static void replace_at (WEdit
* edit
, long q
, int c
)
253 edit_cursor_move (edit
, q
- edit
->curs1
);
255 edit_insert_ahead (edit
, c
);
258 void edit_insert_indent (WEdit
* edit
, int indent
);
260 /* replaces a block of text */
261 static void put_paragraph (WEdit
* edit
, unsigned char *t
, long p
, long q
, int indent
, int size
)
265 cursor
= edit
->curs1
;
267 while (strchr ("\t ", edit_get_byte (edit
, p
)))
269 for (i
= 0; i
< size
; i
++, p
++) {
271 if (t
[i
- 1] == '\n' && c
== '\n') {
272 while (strchr ("\t ", edit_get_byte (edit
, p
)))
274 } else if (t
[i
- 1] == '\n') {
276 edit_cursor_move (edit
, p
- edit
->curs1
);
278 edit_insert_indent (edit
, indent
);
280 cursor
+= edit
->curs1
- p
;
282 } else if (c
== '\n') {
283 edit_cursor_move (edit
, p
- edit
->curs1
);
284 while (strchr ("\t ", edit_get_byte (edit
, p
))) {
286 if (cursor
> edit
->curs1
)
292 c
= edit_get_byte (edit
, p
);
294 replace_at (edit
, p
, t
[i
]);
296 edit_cursor_move (edit
, cursor
- edit
->curs1
); /* restore cursor position */
299 int edit_indent_width (WEdit
* edit
, long p
);
301 static int test_indent (WEdit
* edit
, long p
, long q
)
304 indent
= edit_indent_width (edit
, p
++);
308 if (edit_get_byte (edit
, p
- 1) == '\n')
309 if (indent
!= edit_indent_width (edit
, p
))
314 void format_paragraph (WEdit
* edit
, int force
)
320 if (option_word_wrap_line_length
< 2)
322 if (line_is_blank (edit
, edit
->curs_line
))
324 p
= begin_paragraph (edit
, force
);
325 q
= end_paragraph (edit
, force
);
326 indent
= test_indent (edit
, p
, q
);
327 t
= get_paragraph (edit
, p
, q
, indent
, &size
);
332 if (strchr (NO_FORMAT_CHARS_START
, *t
)) {
336 for (i
= 0; i
< size
- 1; i
++) {
338 if (strchr (NO_FORMAT_CHARS_START
"\t ", t
[i
+ 1])) {
345 format_this (t
, q
- p
, indent
);
346 put_paragraph (edit
, t
, p
, q
, indent
, size
);
349 /* Scroll left as much as possible to show the formatted paragraph */
350 edit_scroll_left(edit
, - edit
->start_col
);