This source file includes following definitions.
- line_start
- bad_line_start
- begin_paragraph
- end_paragraph
- get_paragraph
- strip_newlines
- next_tab_pos
- line_pixel_length
- next_word_start
- word_start
- format_this
- replace_at
- edit_indent_width
- edit_insert_indent
- put_paragraph
- test_indent
- format_paragraph
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 #include <config.h>
38
39 #include <stdio.h>
40 #include <stdarg.h>
41 #include <sys/types.h>
42 #include <unistd.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <sys/stat.h>
46
47 #include <stdlib.h>
48
49 #include "lib/global.h"
50 #include "lib/util.h"
51
52 #include "edit-impl.h"
53 #include "editwidget.h"
54
55
56
57
58
59 #define FONT_MEAN_WIDTH 1
60
61
62
63
64
65
66
67
68
69
70
71 static off_t
72 line_start (const edit_buffer_t *buf, long line)
73 {
74 off_t p;
75 long l;
76
77 l = buf->curs_line;
78 p = buf->curs1;
79
80 if (line < l)
81 p = edit_buffer_get_backward_offset (buf, p, l - line);
82 else if (line > l)
83 p = edit_buffer_get_forward_offset (buf, p, line - l, 0);
84
85 p = edit_buffer_get_bol (buf, p);
86 while (strchr ("\t ", edit_buffer_get_byte (buf, p)) != NULL)
87 p++;
88 return p;
89 }
90
91
92
93 static gboolean
94 bad_line_start (const edit_buffer_t *buf, off_t p)
95 {
96 int c;
97
98 c = edit_buffer_get_byte (buf, p);
99 if (c == '.')
100 {
101
102 return !(edit_buffer_get_byte (buf, p + 1) == '.'
103 && edit_buffer_get_byte (buf, p + 2) == '.');
104 }
105 if (c == '-')
106 {
107
108 return !(edit_buffer_get_byte (buf, p + 1) == '-'
109 && edit_buffer_get_byte (buf, p + 2) == '-');
110 }
111
112 return (edit_options.stop_format_chars != NULL
113 && strchr (edit_options.stop_format_chars, c) != NULL);
114 }
115
116
117
118
119
120
121
122 static off_t
123 begin_paragraph (WEdit *edit, gboolean force, long *lines)
124 {
125 long i;
126
127 for (i = edit->buffer.curs_line - 1; i >= 0; i--)
128 if (edit_line_is_blank (edit, i) ||
129 (force && bad_line_start (&edit->buffer, line_start (&edit->buffer, i))))
130 {
131 i++;
132 break;
133 }
134
135 *lines = edit->buffer.curs_line - i;
136
137 return edit_buffer_get_backward_offset (&edit->buffer,
138 edit_buffer_get_current_bol (&edit->buffer), *lines);
139 }
140
141
142
143
144
145
146
147 static off_t
148 end_paragraph (WEdit *edit, gboolean force)
149 {
150 long i;
151
152 for (i = edit->buffer.curs_line + 1; i <= edit->buffer.lines; i++)
153 if (edit_line_is_blank (edit, i) ||
154 (force && bad_line_start (&edit->buffer, line_start (&edit->buffer, i))))
155 {
156 i--;
157 break;
158 }
159
160 return edit_buffer_get_eol (&edit->buffer,
161 edit_buffer_get_forward_offset (&edit->buffer,
162 edit_buffer_get_current_bol
163 (&edit->buffer),
164 i - edit->buffer.curs_line, 0));
165 }
166
167
168
169 static GString *
170 get_paragraph (const edit_buffer_t *buf, off_t p, off_t q, gboolean indent)
171 {
172 GString *t;
173
174 t = g_string_sized_new (128);
175
176 for (; p < q; p++)
177 {
178 if (indent && edit_buffer_get_byte (buf, p - 1) == '\n')
179 while (strchr ("\t ", edit_buffer_get_byte (buf, p)) != NULL)
180 p++;
181
182 g_string_append_c (t, edit_buffer_get_byte (buf, p));
183 }
184
185 g_string_append_c (t, '\n');
186
187 return t;
188 }
189
190
191
192 static inline void
193 strip_newlines (unsigned char *t, off_t size)
194 {
195 unsigned char *p;
196
197 for (p = t; size-- != 0; p++)
198 if (*p == '\n')
199 *p = ' ';
200 }
201
202
203
204
205
206
207 static inline off_t
208 next_tab_pos (off_t x)
209 {
210 x += TAB_SIZE - x % TAB_SIZE;
211 return x;
212 }
213
214
215
216 static inline off_t
217 line_pixel_length (unsigned char *t, off_t b, off_t l, gboolean utf8)
218 {
219 off_t xn, x;
220 off_t char_length = 0;
221
222 #ifndef HAVE_CHARSET
223 (void) utf8;
224 #endif
225
226 for (xn = 0, x = 0; xn <= l; x = xn)
227 {
228 char *tb;
229
230 b += char_length;
231 tb = (char *) t + b;
232 char_length = 1;
233
234 switch (tb[0])
235 {
236 case '\n':
237 return b;
238 case '\t':
239 xn = next_tab_pos (x);
240 break;
241 default:
242 #ifdef HAVE_CHARSET
243 if (utf8)
244 {
245 gunichar ch;
246
247 ch = g_utf8_get_char_validated (tb, -1);
248 if (ch != (gunichar) (-2) && ch != (gunichar) (-1))
249 {
250 char *next_ch;
251
252
253 next_ch = g_utf8_next_char (tb);
254 char_length = next_ch - tb;
255
256 if (g_unichar_iswide (ch))
257 x++;
258 }
259 }
260 #endif
261
262 xn = x + 1;
263 break;
264 }
265 }
266
267 return b;
268 }
269
270
271
272 static off_t
273 next_word_start (unsigned char *t, off_t q, off_t size)
274 {
275 off_t i;
276 gboolean saw_ws = FALSE;
277
278 for (i = q; i < size; i++)
279 {
280 switch (t[i])
281 {
282 case '\n':
283 return -1;
284 case '\t':
285 case ' ':
286 saw_ws = TRUE;
287 break;
288 default:
289 if (saw_ws)
290 return i;
291 break;
292 }
293 }
294 return (-1);
295 }
296
297
298
299
300 static inline int
301 word_start (unsigned char *t, off_t q, off_t size)
302 {
303 off_t i;
304
305 if (whitespace (t[q]))
306 return next_word_start (t, q, size);
307
308 for (i = q;; i--)
309 {
310 unsigned char c;
311
312 if (i == 0)
313 return (-1);
314 c = t[i - 1];
315 if (c == '\n')
316 return (-1);
317 if (whitespace (c))
318 return i;
319 }
320 }
321
322
323
324
325 static inline void
326 format_this (unsigned char *t, off_t size, long indent, gboolean utf8)
327 {
328 off_t q = 0, ww;
329
330 strip_newlines (t, size);
331 ww = edit_options.word_wrap_line_length * FONT_MEAN_WIDTH - indent;
332 if (ww < FONT_MEAN_WIDTH * 2)
333 ww = FONT_MEAN_WIDTH * 2;
334
335 while (TRUE)
336 {
337 off_t p;
338
339 q = line_pixel_length (t, q, ww, utf8);
340 if (q > size)
341 break;
342 if (t[q] == '\n')
343 break;
344 p = word_start (t, q, size);
345 if (p == -1)
346 q = next_word_start (t, q, size);
347
348
349 else
350 q = p;
351 if (q == -1)
352 break;
353 if (q != 0)
354 t[q - 1] = '\n';
355 }
356 }
357
358
359
360 static inline void
361 replace_at (WEdit *edit, off_t q, int c)
362 {
363 edit_cursor_move (edit, q - edit->buffer.curs1);
364 edit_delete (edit, TRUE);
365 edit_insert_ahead (edit, c);
366 }
367
368
369
370 static long
371 edit_indent_width (const WEdit *edit, off_t p)
372 {
373 off_t q = p;
374
375
376 while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, q)) != NULL
377 && q < edit->buffer.size - 1)
378 q++;
379
380 return (long) edit_move_forward3 (edit, p, 0, q);
381 }
382
383
384
385 static void
386 edit_insert_indent (WEdit *edit, long indent)
387 {
388 if (!edit_options.fill_tabs_with_spaces)
389 while (indent >= TAB_SIZE)
390 {
391 edit_insert (edit, '\t');
392 indent -= TAB_SIZE;
393 }
394
395 while (indent-- > 0)
396 edit_insert (edit, ' ');
397 }
398
399
400
401
402 static inline void
403 put_paragraph (WEdit *edit, unsigned char *t, off_t p, long indent, off_t size)
404 {
405 off_t cursor;
406 off_t i;
407 int c = '\0';
408
409 cursor = edit->buffer.curs1;
410 if (indent != 0)
411 while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p)) != NULL)
412 p++;
413 for (i = 0; i < size; i++, p++)
414 {
415 if (i != 0 && indent != 0)
416 {
417 if (t[i - 1] == '\n' && c == '\n')
418 {
419 while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p)) != NULL)
420 p++;
421 }
422 else if (t[i - 1] == '\n')
423 {
424 off_t curs;
425
426 edit_cursor_move (edit, p - edit->buffer.curs1);
427 curs = edit->buffer.curs1;
428 edit_insert_indent (edit, indent);
429 if (cursor >= curs)
430 cursor += edit->buffer.curs1 - p;
431 p = edit->buffer.curs1;
432 }
433 else if (c == '\n')
434 {
435 edit_cursor_move (edit, p - edit->buffer.curs1);
436 while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p)) != NULL)
437 {
438 edit_delete (edit, TRUE);
439 if (cursor > edit->buffer.curs1)
440 cursor--;
441 }
442 p = edit->buffer.curs1;
443 }
444 }
445
446 c = edit_buffer_get_byte (&edit->buffer, p);
447 if (c != t[i])
448 replace_at (edit, p, t[i]);
449 }
450 edit_cursor_move (edit, cursor - edit->buffer.curs1);
451 }
452
453
454
455 static inline long
456 test_indent (const WEdit *edit, off_t p, off_t q)
457 {
458 long indent;
459
460 indent = edit_indent_width (edit, p++);
461 if (indent == 0)
462 return 0;
463
464 for (; p < q; p++)
465 if (edit_buffer_get_byte (&edit->buffer, p - 1) == '\n'
466 && indent != edit_indent_width (edit, p))
467 return 0;
468 return indent;
469 }
470
471
472
473
474
475 void
476 format_paragraph (WEdit *edit, gboolean force)
477 {
478 off_t p, q;
479 long lines;
480 off_t size;
481 GString *t;
482 long indent;
483 unsigned char *t2;
484 gboolean utf8 = FALSE;
485
486 if (edit_options.word_wrap_line_length < 2)
487 return;
488 if (edit_line_is_blank (edit, edit->buffer.curs_line))
489 return;
490
491 p = begin_paragraph (edit, force, &lines);
492 q = end_paragraph (edit, force);
493 indent = test_indent (edit, p, q);
494
495 t = get_paragraph (&edit->buffer, p, q, indent != 0);
496 size = t->len - 1;
497
498 if (!force)
499 {
500 off_t i;
501 char *stop_format_chars;
502
503 if (edit_options.stop_format_chars != NULL
504 && strchr (edit_options.stop_format_chars, t->str[0]) != NULL)
505 {
506 g_string_free (t, TRUE);
507 return;
508 }
509
510 if (edit_options.stop_format_chars == NULL || *edit_options.stop_format_chars == '\0')
511 stop_format_chars = g_strdup ("\t");
512 else
513 stop_format_chars = g_strconcat (edit_options.stop_format_chars, "\t", (char *) NULL);
514
515 for (i = 0; i < size - 1; i++)
516 if (t->str[i] == '\n' && strchr (stop_format_chars, t->str[i + 1]) != NULL)
517 {
518 g_free (stop_format_chars);
519 g_string_free (t, TRUE);
520 return;
521 }
522
523 g_free (stop_format_chars);
524 }
525
526 t2 = (unsigned char *) g_string_free (t, FALSE);
527 #ifdef HAVE_CHARSET
528 utf8 = edit->utf8;
529 #endif
530 format_this (t2, q - p, indent, utf8);
531 put_paragraph (edit, t2, p, indent, size);
532 g_free ((char *) t2);
533
534
535 edit_scroll_left (edit, -edit->start_col);
536 }
537
538