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
)
213 if (t
[i
] != ' ' && t
[i
] != '\t')
221 /* find the start of a word */
223 word_start (unsigned char *t
, int q
)
226 if (t
[q
] == ' ' || t
[q
] == '\t')
227 return next_word_start (t
, q
);
235 if (c
== ' ' || c
== '\t')
241 /* replaces ' ' with '\n' to properly format a paragraph */
242 static void format_this (unsigned char *t
, int size
, int indent
)
245 strip_newlines (t
, size
);
246 ww
= option_word_wrap_line_length
* FONT_MEAN_WIDTH
- indent
;
247 if (ww
< FONT_MEAN_WIDTH
* 2)
248 ww
= FONT_MEAN_WIDTH
* 2;
251 q
= line_pixel_length (t
, q
, ww
);
256 p
= word_start (t
, q
);
258 q
= next_word_start (t
, q
); /* Return the end of the word if the beginning
259 of the word is at the beginning of a line
260 (i.e. a very long word) */
263 if (q
== -1) /* end of paragraph */
270 static void replace_at (WEdit
* edit
, long q
, int c
)
272 edit_cursor_move (edit
, q
- edit
->curs1
);
274 edit_insert_ahead (edit
, c
);
277 /* replaces a block of text */
279 put_paragraph (WEdit
* edit
, unsigned char *t
, long p
, int indent
, int size
)
283 cursor
= edit
->curs1
;
285 while (strchr ("\t ", edit_get_byte (edit
, p
)))
287 for (i
= 0; i
< size
; i
++, p
++) {
289 if (t
[i
- 1] == '\n' && c
== '\n') {
290 while (strchr ("\t ", edit_get_byte (edit
, p
)))
292 } else if (t
[i
- 1] == '\n') {
294 edit_cursor_move (edit
, p
- edit
->curs1
);
296 edit_insert_indent (edit
, indent
);
298 cursor
+= edit
->curs1
- p
;
300 } else if (c
== '\n') {
301 edit_cursor_move (edit
, p
- edit
->curs1
);
302 while (strchr ("\t ", edit_get_byte (edit
, p
))) {
304 if (cursor
> edit
->curs1
)
310 c
= edit_get_byte (edit
, p
);
312 replace_at (edit
, p
, t
[i
]);
314 edit_cursor_move (edit
, cursor
- edit
->curs1
); /* restore cursor position */
317 static int test_indent (WEdit
* edit
, long p
, long q
)
320 indent
= edit_indent_width (edit
, p
++);
324 if (edit_get_byte (edit
, p
- 1) == '\n')
325 if (indent
!= edit_indent_width (edit
, p
))
331 format_paragraph (WEdit
*edit
, int force
)
337 if (option_word_wrap_line_length
< 2)
339 if (line_is_blank (edit
, edit
->curs_line
))
341 p
= begin_paragraph (edit
, force
);
342 q
= end_paragraph (edit
, force
);
343 indent
= test_indent (edit
, p
, q
);
344 t
= get_paragraph (edit
, p
, q
, indent
, &size
);
349 if (strchr (NO_FORMAT_CHARS_START
, *t
)) {
353 for (i
= 0; i
< size
- 1; i
++) {
355 if (strchr (NO_FORMAT_CHARS_START
"\t ", t
[i
+ 1])) {
362 format_this (t
, q
- p
, indent
);
363 put_paragraph (edit
, t
, p
, indent
, size
);
366 /* Scroll left as much as possible to show the formatted paragraph */
367 edit_scroll_left (edit
, -edit
->start_col
);