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>
35 #include <mhl/memory.h>
37 #include "../src/global.h"
40 #include "edit-widget.h"
42 #define tab_width option_tab_spacing
44 #define NO_FORMAT_CHARS_START "-+*\\,.;:&>"
45 #define FONT_MEAN_WIDTH 1
48 line_start (WEdit
*edit
, long line
)
56 p
= edit_move_backward (edit
, p
, l
- line
);
58 p
= edit_move_forward (edit
, p
, line
- l
, 0);
60 p
= edit_bol (edit
, p
);
61 while (strchr ("\t ", edit_get_byte (edit
, p
)))
66 static int bad_line_start (WEdit
* edit
, long p
)
69 c
= edit_get_byte (edit
, p
);
70 if (c
== '.') { /* `...' is acceptable */
71 if (edit_get_byte (edit
, p
+ 1) == '.')
72 if (edit_get_byte (edit
, p
+ 2) == '.')
77 if (edit_get_byte (edit
, p
+ 1) == '-')
78 if (edit_get_byte (edit
, p
+ 2) == '-')
79 return 0; /* `---' is acceptable */
82 if (strchr (NO_FORMAT_CHARS_START
, c
))
88 * Find the start of the current paragraph for the purpose of formatting.
89 * Return position in the file.
92 begin_paragraph (WEdit
*edit
, int force
)
95 for (i
= edit
->curs_line
- 1; i
>= 0; i
--) {
96 if (line_is_blank (edit
, i
)) {
101 if (bad_line_start (edit
, line_start (edit
, i
))) {
107 return edit_move_backward (edit
, edit_bol (edit
, edit
->curs1
),
108 edit
->curs_line
- i
);
112 * Find the end of the current paragraph for the purpose of formatting.
113 * Return position in the file.
116 end_paragraph (WEdit
*edit
, int force
)
119 for (i
= edit
->curs_line
+ 1; i
<= edit
->total_lines
; i
++) {
120 if (line_is_blank (edit
, i
)) {
125 if (bad_line_start (edit
, line_start (edit
, i
))) {
130 return edit_eol (edit
,
131 edit_move_forward (edit
, edit_bol (edit
, edit
->curs1
),
132 i
- edit
->curs_line
, 0));
135 static unsigned char *
136 get_paragraph (WEdit
*edit
, long p
, long q
, int indent
, int *size
)
138 unsigned char *s
, *t
;
140 t
= g_malloc ((q
- p
) + 2 * (q
- p
) / option_word_wrap_line_length
+
143 t
= g_malloc (2 * (q
- p
) + 100);
147 for (s
= t
; p
< q
; p
++, s
++) {
149 if (edit_get_byte (edit
, p
- 1) == '\n')
150 while (strchr ("\t ", edit_get_byte (edit
, p
)))
152 *s
= edit_get_byte (edit
, p
);
154 *size
= (unsigned long) s
- (unsigned long) t
;
159 static void strip_newlines (unsigned char *t
, int size
)
161 unsigned char *p
= t
;
163 *p
= *p
== '\n' ? ' ' : *p
;
169 This is a copy of the function
170 int calc_text_pos (WEdit * edit, long b, long *q, int l)
172 It calculates the number of chars in a line specified to length l in pixels
174 static inline int next_tab_pos (int x
)
176 return x
+= tab_width
- x
% tab_width
;
178 static int line_pixel_length (unsigned char *t
, long b
, int l
)
180 int x
= 0, c
, xn
= 0;
187 xn
= next_tab_pos (x
);
202 next_word_start (unsigned char *t
, int q
, int size
)
207 for (i
= q
; i
< size
; i
++) {
224 /* find the start of a word */
226 word_start (unsigned char *t
, int q
, int size
)
229 if (t
[q
] == ' ' || t
[q
] == '\t')
230 return next_word_start (t
, q
, size
);
238 if (c
== ' ' || c
== '\t')
244 /* replaces ' ' with '\n' to properly format a paragraph */
245 static void format_this (unsigned char *t
, int size
, int indent
)
248 strip_newlines (t
, size
);
249 ww
= option_word_wrap_line_length
* FONT_MEAN_WIDTH
- indent
;
250 if (ww
< FONT_MEAN_WIDTH
* 2)
251 ww
= FONT_MEAN_WIDTH
* 2;
254 q
= line_pixel_length (t
, q
, ww
);
259 p
= word_start (t
, q
, size
);
261 q
= next_word_start (t
, q
, size
); /* Return the end of the word if the beginning
262 of the word is at the beginning of a line
263 (i.e. a very long word) */
266 if (q
== -1) /* end of paragraph */
273 static void replace_at (WEdit
* edit
, long q
, int c
)
275 edit_cursor_move (edit
, q
- edit
->curs1
);
277 edit_insert_ahead (edit
, c
);
280 /* replaces a block of text */
282 put_paragraph (WEdit
* edit
, unsigned char *t
, long p
, int indent
, int size
)
286 cursor
= edit
->curs1
;
288 while (strchr ("\t ", edit_get_byte (edit
, p
)))
290 for (i
= 0; i
< size
; i
++, p
++) {
292 if (t
[i
- 1] == '\n' && c
== '\n') {
293 while (strchr ("\t ", edit_get_byte (edit
, p
)))
295 } else if (t
[i
- 1] == '\n') {
297 edit_cursor_move (edit
, p
- edit
->curs1
);
299 edit_insert_indent (edit
, indent
);
301 cursor
+= edit
->curs1
- p
;
303 } else if (c
== '\n') {
304 edit_cursor_move (edit
, p
- edit
->curs1
);
305 while (strchr ("\t ", edit_get_byte (edit
, p
))) {
307 if (cursor
> edit
->curs1
)
313 c
= edit_get_byte (edit
, p
);
315 replace_at (edit
, p
, t
[i
]);
317 edit_cursor_move (edit
, cursor
- edit
->curs1
); /* restore cursor position */
320 static int test_indent (WEdit
* edit
, long p
, long q
)
323 indent
= edit_indent_width (edit
, p
++);
327 if (edit_get_byte (edit
, p
- 1) == '\n')
328 if (indent
!= edit_indent_width (edit
, p
))
334 format_paragraph (WEdit
*edit
, int force
)
340 if (option_word_wrap_line_length
< 2)
342 if (line_is_blank (edit
, edit
->curs_line
))
344 p
= begin_paragraph (edit
, force
);
345 q
= end_paragraph (edit
, force
);
346 indent
= test_indent (edit
, p
, q
);
347 t
= get_paragraph (edit
, p
, q
, indent
, &size
);
352 if (strchr (NO_FORMAT_CHARS_START
, *t
)) {
356 for (i
= 0; i
< size
- 1; i
++) {
358 if (strchr (NO_FORMAT_CHARS_START
"\t ", t
[i
+ 1])) {
365 format_this (t
, q
- p
, indent
);
366 put_paragraph (edit
, t
, p
, indent
, size
);
369 /* Scroll left as much as possible to show the formatted paragraph */
370 edit_scroll_left (edit
, -edit
->start_col
);