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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define tab_width option_tab_spacing
27 int line_is_blank (WEdit
* edit
, long line
);
29 #define NO_FORMAT_CHARS_START "-+*\\,.;:&>"
31 static long line_start (WEdit
* edit
, long line
)
33 static long p
= -1, l
= 0;
35 if (p
== -1 || abs (l
- line
) > abs (edit
->curs_line
- line
)) {
40 p
= edit_move_backward (edit
, p
, l
- line
);
42 p
= edit_move_forward (edit
, p
, line
- l
, 0);
44 p
= edit_bol (edit
, p
);
45 while (strchr ("\t ", c
= edit_get_byte (edit
, p
)))
50 static int bad_line_start (WEdit
* edit
, long p
)
53 c
= edit_get_byte (edit
, p
);
54 if (c
== '.') { /* `...' is acceptable */
55 if (edit_get_byte (edit
, p
+ 1) == '.')
56 if (edit_get_byte (edit
, p
+ 2) == '.')
61 if (edit_get_byte (edit
, p
+ 1) == '-')
62 if (edit_get_byte (edit
, p
+ 2) == '-')
63 return 0; /* `---' is acceptable */
66 if (strchr (NO_FORMAT_CHARS_START
, c
))
71 static long begin_paragraph (WEdit
* edit
, long p
, int force
)
74 for (i
= edit
->curs_line
- 1; i
> 0; i
--) {
75 if (line_is_blank (edit
, i
)) {
80 if (bad_line_start (edit
, line_start (edit
, i
))) {
86 return edit_move_backward (edit
, edit_bol (edit
, edit
->curs1
), edit
->curs_line
- i
);
89 static long end_paragraph (WEdit
* edit
, long p
, int force
)
92 for (i
= edit
->curs_line
+ 1; i
< edit
->total_lines
; i
++) {
93 if (line_is_blank (edit
, i
)) {
98 if (bad_line_start (edit
, line_start (edit
, i
))) {
103 return edit_eol (edit
, edit_move_forward (edit
, edit_bol (edit
, edit
->curs1
), i
- edit
->curs_line
, 0));
106 static char *get_paragraph (WEdit
* edit
, long p
, long q
, int indent
, int *size
)
109 t
= malloc ((q
- p
) + 2 * (q
- p
) / option_word_wrap_line_length
+ 10);
112 for (s
= t
; p
< q
; p
++, s
++) {
114 if (edit_get_byte (edit
, p
- 1) == '\n')
115 while (strchr ("\t ", edit_get_byte (edit
, p
)))
117 *s
= edit_get_byte (edit
, p
);
119 *size
= (unsigned long) s
- (unsigned long) t
;
124 static void strip_newlines (char *t
, int size
)
128 *p
= *p
== '\n' ? ' ' : *p
;
134 int edit_width_of_long_printable (int c
);
137 This is a copy of the function
138 int calc_text_pos (WEdit * edit, long b, long *q, int l)
140 It calculates the number of chars in a line specified to length l in pixels
142 extern int tab_width
;
143 static inline int next_tab_pos (int x
)
145 return x
+= tab_width
- x
% tab_width
;
147 static int line_pixel_length (char *t
, long b
, int l
)
149 int x
= 0, c
, xn
= 0;
156 xn
= next_tab_pos (x
);
162 xn
= x
+ edit_width_of_long_printable (c
);
174 /* find the start of a word */
175 static int next_word_start (char *t
, int q
, int size
)
187 if (t
[i
] != ' ' && t
[i
] != '\t')
195 /* find the start of a word */
196 static int word_start (char *t
, int q
, int size
)
199 if (t
[q
] == ' ' || t
[q
] == '\t')
200 return next_word_start (t
, q
, size
);
208 if (c
== ' ' || c
== '\t')
214 /* replaces ' ' with '\n' to properly format a paragraph */
215 static void format_this (char *t
, int size
, int indent
)
218 strip_newlines (t
, size
);
219 ww
= option_word_wrap_line_length
* FONT_MEAN_WIDTH
- indent
;
220 if (ww
< FONT_MEAN_WIDTH
* 2)
221 ww
= FONT_MEAN_WIDTH
* 2;
224 q
= line_pixel_length (t
, q
, ww
);
229 p
= word_start (t
, q
, size
);
231 q
= next_word_start (t
, q
, size
); /* Return the end of the word if the beginning
232 of the word is at the beginning of a line
233 (i.e. a very long word) */
236 if (q
== -1) /* end of paragraph */
243 static void replace_at (WEdit
* edit
, long q
, int c
)
245 edit_cursor_move (edit
, q
- edit
->curs1
);
247 edit_insert_ahead (edit
, c
);
250 void edit_insert_indent (WEdit
* edit
, int indent
);
252 /* replaces a block of text */
253 static void put_paragraph (WEdit
* edit
, char *t
, long p
, long q
, int indent
, int size
)
257 cursor
= edit
->curs1
;
259 while (strchr ("\t ", edit_get_byte (edit
, p
)))
261 for (i
= 0; i
< size
; i
++, p
++) {
263 if (t
[i
- 1] == '\n' && c
== '\n') {
264 while (strchr ("\t ", edit_get_byte (edit
, p
)))
266 } else if (t
[i
- 1] == '\n') {
268 edit_cursor_move (edit
, p
- edit
->curs1
);
270 edit_insert_indent (edit
, indent
);
272 cursor
+= edit
->curs1
- p
;
274 } else if (c
== '\n') {
275 edit_cursor_move (edit
, p
- edit
->curs1
);
276 while (strchr ("\t ", edit_get_byte (edit
, p
))) {
278 if (cursor
> edit
->curs1
)
284 c
= edit_get_byte (edit
, p
);
286 replace_at (edit
, p
, t
[i
]);
288 edit_cursor_move (edit
, cursor
- edit
->curs1
); /* restore cursor position */
291 int edit_indent_width (WEdit
* edit
, long p
);
293 static int test_indent (WEdit
* edit
, long p
, long q
)
296 indent
= edit_indent_width (edit
, p
++);
300 if (edit_get_byte (edit
, p
- 1) == '\n')
301 if (indent
!= edit_indent_width (edit
, p
))
306 void format_paragraph (WEdit
* edit
, int force
)
312 if (option_word_wrap_line_length
< 2)
314 if (line_is_blank (edit
, edit
->curs_line
))
316 p
= begin_paragraph (edit
, edit
->curs1
, force
);
317 q
= end_paragraph (edit
, edit
->curs1
, force
);
318 indent
= test_indent (edit
, p
, q
);
319 t
= get_paragraph (edit
, p
, q
, indent
, &size
);
324 if (strchr (NO_FORMAT_CHARS_START
, *t
)) {
328 for (i
= 0; i
< size
- 1; i
++) {
330 if (strchr (NO_FORMAT_CHARS_START
"\t ", t
[i
+ 1])) {
337 format_this (t
, q
- p
, indent
);
338 put_paragraph (edit
, t
, p
, q
, indent
, size
);