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., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <sys/types.h>
36 #include "../src/global.h"
39 #include "edit-widget.h"
41 #define tab_width option_tab_spacing
43 #define NO_FORMAT_CHARS_START "-+*\\,.;:&>"
44 #define FONT_MEAN_WIDTH 1
47 line_start (WEdit
*edit
, long line
)
55 p
= edit_move_backward (edit
, p
, l
- line
);
57 p
= edit_move_forward (edit
, p
, line
- l
, 0);
59 p
= edit_bol (edit
, p
);
60 while (strchr ("\t ", edit_get_byte (edit
, p
)))
65 static int bad_line_start (WEdit
* edit
, long p
)
68 c
= edit_get_byte (edit
, p
);
69 if (c
== '.') { /* `...' is acceptable */
70 if (edit_get_byte (edit
, p
+ 1) == '.')
71 if (edit_get_byte (edit
, p
+ 2) == '.')
76 if (edit_get_byte (edit
, p
+ 1) == '-')
77 if (edit_get_byte (edit
, p
+ 2) == '-')
78 return 0; /* `---' is acceptable */
81 if (strchr (NO_FORMAT_CHARS_START
, c
))
87 * Find the start of the current paragraph for the purpose of formatting.
88 * Return position in the file.
91 begin_paragraph (WEdit
*edit
, int force
)
94 for (i
= edit
->curs_line
- 1; i
>= 0; i
--) {
95 if (line_is_blank (edit
, i
)) {
100 if (bad_line_start (edit
, line_start (edit
, i
))) {
106 return edit_move_backward (edit
, edit_bol (edit
, edit
->curs1
),
107 edit
->curs_line
- i
);
111 * Find the end of the current paragraph for the purpose of formatting.
112 * Return position in the file.
115 end_paragraph (WEdit
*edit
, int force
)
118 for (i
= edit
->curs_line
+ 1; i
<= edit
->total_lines
; i
++) {
119 if (line_is_blank (edit
, i
)) {
124 if (bad_line_start (edit
, line_start (edit
, i
))) {
129 return edit_eol (edit
,
130 edit_move_forward (edit
, edit_bol (edit
, edit
->curs1
),
131 i
- edit
->curs_line
, 0));
134 static unsigned char *
135 get_paragraph (WEdit
*edit
, long p
, long q
, int indent
, int *size
)
137 unsigned char *s
, *t
;
139 t
= g_malloc ((q
- p
) + 2 * (q
- p
) / option_word_wrap_line_length
+
142 t
= g_malloc (2 * (q
- p
) + 100);
146 for (s
= t
; p
< q
; p
++, s
++) {
148 if (edit_get_byte (edit
, p
- 1) == '\n')
149 while (strchr ("\t ", edit_get_byte (edit
, p
)))
151 *s
= edit_get_byte (edit
, p
);
153 *size
= (unsigned long) s
- (unsigned long) t
;
158 static void strip_newlines (unsigned char *t
, int size
)
160 unsigned char *p
= t
;
162 *p
= *p
== '\n' ? ' ' : *p
;
168 This is a copy of the function
169 int calc_text_pos (WEdit * edit, long b, long *q, int l)
171 It calculates the number of chars in a line specified to length l in pixels
173 static inline int next_tab_pos (int x
)
175 return x
+= tab_width
- x
% tab_width
;
177 static int line_pixel_length (unsigned char *t
, long b
, int l
)
179 int x
= 0, c
, xn
= 0;
186 xn
= next_tab_pos (x
);
201 next_word_start (unsigned char *t
, int q
, int size
)
206 for (i
= q
; i
< size
; i
++) {
223 /* find the start of a word */
225 word_start (unsigned char *t
, int q
, int size
)
228 if (t
[q
] == ' ' || t
[q
] == '\t')
229 return next_word_start (t
, q
, size
);
237 if (c
== ' ' || c
== '\t')
243 /* replaces ' ' with '\n' to properly format a paragraph */
244 static void format_this (unsigned char *t
, int size
, int indent
)
247 strip_newlines (t
, size
);
248 ww
= option_word_wrap_line_length
* FONT_MEAN_WIDTH
- indent
;
249 if (ww
< FONT_MEAN_WIDTH
* 2)
250 ww
= FONT_MEAN_WIDTH
* 2;
253 q
= line_pixel_length (t
, q
, ww
);
258 p
= word_start (t
, q
, size
);
260 q
= next_word_start (t
, q
, size
); /* Return the end of the word if the beginning
261 of the word is at the beginning of a line
262 (i.e. a very long word) */
265 if (q
== -1) /* end of paragraph */
272 static void replace_at (WEdit
* edit
, long q
, int c
)
274 edit_cursor_move (edit
, q
- edit
->curs1
);
275 edit_delete (edit
, 1);
276 edit_insert_ahead (edit
, c
);
279 /* replaces a block of text */
281 put_paragraph (WEdit
* edit
, unsigned char *t
, long p
, int indent
, int size
)
285 cursor
= edit
->curs1
;
287 while (strchr ("\t ", edit_get_byte (edit
, p
)))
289 for (i
= 0; i
< size
; i
++, p
++) {
291 if (t
[i
- 1] == '\n' && c
== '\n') {
292 while (strchr ("\t ", edit_get_byte (edit
, p
)))
294 } else if (t
[i
- 1] == '\n') {
296 edit_cursor_move (edit
, p
- edit
->curs1
);
298 edit_insert_indent (edit
, indent
);
300 cursor
+= edit
->curs1
- p
;
302 } else if (c
== '\n') {
303 edit_cursor_move (edit
, p
- edit
->curs1
);
304 while (strchr ("\t ", edit_get_byte (edit
, p
))) {
305 edit_delete (edit
, 1);
306 if (cursor
> edit
->curs1
)
312 c
= edit_get_byte (edit
, p
);
314 replace_at (edit
, p
, t
[i
]);
316 edit_cursor_move (edit
, cursor
- edit
->curs1
); /* restore cursor position */
319 static int test_indent (WEdit
* edit
, long p
, long q
)
322 indent
= edit_indent_width (edit
, p
++);
326 if (edit_get_byte (edit
, p
- 1) == '\n')
327 if (indent
!= edit_indent_width (edit
, p
))
333 format_paragraph (WEdit
*edit
, int force
)
339 if (option_word_wrap_line_length
< 2)
341 if (line_is_blank (edit
, edit
->curs_line
))
343 p
= begin_paragraph (edit
, force
);
344 q
= end_paragraph (edit
, force
);
345 indent
= test_indent (edit
, p
, q
);
346 t
= get_paragraph (edit
, p
, q
, indent
, &size
);
351 if (strchr (NO_FORMAT_CHARS_START
, *t
)) {
355 for (i
= 0; i
< size
- 1; i
++) {
357 if (strchr (NO_FORMAT_CHARS_START
"\t ", t
[i
+ 1])) {
364 format_this (t
, q
- p
, indent
);
365 put_paragraph (edit
, t
, p
, indent
, size
);
368 /* Scroll left as much as possible to show the formatted paragraph */
369 edit_scroll_left (edit
, -edit
->start_col
);