2 Dynamic paragraph formatting.
4 Copyright (C) 2011-2024
5 Free Software Foundation, Inc.
7 Copyright (C) 1996 Paul Sheer
11 Andrew Borodin <aborodin@vmail.ru>, 2013, 2014
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 * \brief Source: Dynamic paragraph formatting
33 * \author Andrew Borodin
41 #include <sys/types.h>
49 #include "lib/global.h"
50 #include "lib/util.h" /* whitespace() */
52 #include "edit-impl.h"
53 #include "editwidget.h"
55 /*** global variables ****************************************************************************/
57 /*** file scope macro definitions ****************************************************************/
59 #define FONT_MEAN_WIDTH 1
61 /*** file scope type declarations ****************************************************************/
63 /*** forward declarations (file scope functions) *************************************************/
65 /*** file scope variables ************************************************************************/
67 /* --------------------------------------------------------------------------------------------- */
68 /*** file scope functions ************************************************************************/
69 /* --------------------------------------------------------------------------------------------- */
72 line_start (const edit_buffer_t
*buf
, long line
)
81 p
= edit_buffer_get_backward_offset (buf
, p
, l
- line
);
83 p
= edit_buffer_get_forward_offset (buf
, p
, line
- l
, 0);
85 p
= edit_buffer_get_bol (buf
, p
);
86 while (strchr ("\t ", edit_buffer_get_byte (buf
, p
)) != NULL
)
91 /* --------------------------------------------------------------------------------------------- */
94 bad_line_start (const edit_buffer_t
*buf
, off_t p
)
98 c
= edit_buffer_get_byte (buf
, p
);
101 /* `...' is acceptable */
102 return !(edit_buffer_get_byte (buf
, p
+ 1) == '.'
103 && edit_buffer_get_byte (buf
, p
+ 2) == '.');
107 /* `---' is acceptable */
108 return !(edit_buffer_get_byte (buf
, p
+ 1) == '-'
109 && edit_buffer_get_byte (buf
, p
+ 2) == '-');
112 return (edit_options
.stop_format_chars
!= NULL
113 && strchr (edit_options
.stop_format_chars
, c
) != NULL
);
116 /* --------------------------------------------------------------------------------------------- */
118 * Find the start of the current paragraph for the purpose of formatting.
119 * Return position in the file.
123 begin_paragraph (WEdit
*edit
, gboolean force
, long *lines
)
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
))))
135 *lines
= edit
->buffer
.curs_line
- i
;
137 return edit_buffer_get_backward_offset (&edit
->buffer
,
138 edit_buffer_get_current_bol (&edit
->buffer
), *lines
);
141 /* --------------------------------------------------------------------------------------------- */
143 * Find the end of the current paragraph for the purpose of formatting.
144 * Return position in the file.
148 end_paragraph (WEdit
*edit
, gboolean force
)
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
))))
160 return edit_buffer_get_eol (&edit
->buffer
,
161 edit_buffer_get_forward_offset (&edit
->buffer
,
162 edit_buffer_get_current_bol
164 i
- edit
->buffer
.curs_line
, 0));
167 /* --------------------------------------------------------------------------------------------- */
170 get_paragraph (const edit_buffer_t
*buf
, off_t p
, off_t q
, gboolean indent
)
174 t
= g_string_sized_new (128);
178 if (indent
&& edit_buffer_get_byte (buf
, p
- 1) == '\n')
179 while (strchr ("\t ", edit_buffer_get_byte (buf
, p
)) != NULL
)
182 g_string_append_c (t
, edit_buffer_get_byte (buf
, p
));
185 g_string_append_c (t
, '\n');
190 /* --------------------------------------------------------------------------------------------- */
193 strip_newlines (unsigned char *t
, off_t size
)
197 for (p
= t
; size
-- != 0; p
++)
202 /* --------------------------------------------------------------------------------------------- */
204 This function calculates the number of chars in a line specified to length l in pixels
208 next_tab_pos (off_t x
)
210 x
+= TAB_SIZE
- x
% TAB_SIZE
;
214 /* --------------------------------------------------------------------------------------------- */
217 line_pixel_length (unsigned char *t
, off_t b
, off_t l
, gboolean utf8
)
219 off_t xn
, x
; /* position counters */
220 off_t char_length
= 0; /* character length in bytes */
226 for (xn
= 0, x
= 0; xn
<= l
; x
= xn
)
239 xn
= next_tab_pos (x
);
247 ch
= g_utf8_get_char_validated (tb
, -1);
248 if (ch
!= (gunichar
) (-2) && ch
!= (gunichar
) (-1))
252 /* Calculate UTF-8 char length */
253 next_ch
= g_utf8_next_char (tb
);
254 char_length
= next_ch
- tb
;
256 if (g_unichar_iswide (ch
))
270 /* --------------------------------------------------------------------------------------------- */
273 next_word_start (unsigned char *t
, off_t q
, off_t size
)
276 gboolean saw_ws
= FALSE
;
278 for (i
= q
; i
< size
; i
++)
297 /* --------------------------------------------------------------------------------------------- */
298 /** find the start of a word */
301 word_start (unsigned char *t
, off_t q
, off_t size
)
305 if (whitespace (t
[q
]))
306 return next_word_start (t
, q
, size
);
322 /* --------------------------------------------------------------------------------------------- */
323 /** replaces ' ' with '\n' to properly format a paragraph */
326 format_this (unsigned char *t
, off_t size
, long indent
, gboolean utf8
)
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;
339 q
= line_pixel_length (t
, q
, ww
, utf8
);
344 p
= word_start (t
, q
, size
);
346 q
= next_word_start (t
, q
, size
); /* Return the end of the word if the beginning
347 of the word is at the beginning of a line
348 (i.e. a very long word) */
351 if (q
== -1) /* end of paragraph */
358 /* --------------------------------------------------------------------------------------------- */
361 replace_at (WEdit
*edit
, off_t q
, int c
)
363 edit_cursor_move (edit
, q
- edit
->buffer
.curs1
);
364 edit_delete (edit
, TRUE
);
365 edit_insert_ahead (edit
, c
);
368 /* --------------------------------------------------------------------------------------------- */
371 edit_indent_width (const WEdit
*edit
, off_t p
)
375 /* move to the end of the leading whitespace of the line */
376 while (strchr ("\t ", edit_buffer_get_byte (&edit
->buffer
, q
)) != NULL
377 && q
< edit
->buffer
.size
- 1)
379 /* count the number of columns of indentation */
380 return (long) edit_move_forward3 (edit
, p
, 0, q
);
383 /* --------------------------------------------------------------------------------------------- */
386 edit_insert_indent (WEdit
*edit
, long indent
)
388 if (!edit_options
.fill_tabs_with_spaces
)
389 while (indent
>= TAB_SIZE
)
391 edit_insert (edit
, '\t');
396 edit_insert (edit
, ' ');
399 /* --------------------------------------------------------------------------------------------- */
400 /** replaces a block of text */
403 put_paragraph (WEdit
*edit
, unsigned char *t
, off_t p
, long indent
, off_t size
)
409 cursor
= edit
->buffer
.curs1
;
411 while (strchr ("\t ", edit_buffer_get_byte (&edit
->buffer
, p
)) != NULL
)
413 for (i
= 0; i
< size
; i
++, p
++)
415 if (i
!= 0 && indent
!= 0)
417 if (t
[i
- 1] == '\n' && c
== '\n')
419 while (strchr ("\t ", edit_buffer_get_byte (&edit
->buffer
, p
)) != NULL
)
422 else if (t
[i
- 1] == '\n')
426 edit_cursor_move (edit
, p
- edit
->buffer
.curs1
);
427 curs
= edit
->buffer
.curs1
;
428 edit_insert_indent (edit
, indent
);
430 cursor
+= edit
->buffer
.curs1
- p
;
431 p
= edit
->buffer
.curs1
;
435 edit_cursor_move (edit
, p
- edit
->buffer
.curs1
);
436 while (strchr ("\t ", edit_buffer_get_byte (&edit
->buffer
, p
)) != NULL
)
438 edit_delete (edit
, TRUE
);
439 if (cursor
> edit
->buffer
.curs1
)
442 p
= edit
->buffer
.curs1
;
446 c
= edit_buffer_get_byte (&edit
->buffer
, p
);
448 replace_at (edit
, p
, t
[i
]);
450 edit_cursor_move (edit
, cursor
- edit
->buffer
.curs1
); /* restore cursor position */
453 /* --------------------------------------------------------------------------------------------- */
456 test_indent (const WEdit
*edit
, off_t p
, off_t q
)
460 indent
= edit_indent_width (edit
, p
++);
465 if (edit_buffer_get_byte (&edit
->buffer
, p
- 1) == '\n'
466 && indent
!= edit_indent_width (edit
, p
))
471 /* --------------------------------------------------------------------------------------------- */
472 /*** public functions ****************************************************************************/
473 /* --------------------------------------------------------------------------------------------- */
476 format_paragraph (WEdit
*edit
, gboolean force
)
484 gboolean utf8
= FALSE
;
486 if (edit_options
.word_wrap_line_length
< 2)
488 if (edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
491 p
= begin_paragraph (edit
, force
, &lines
);
492 q
= end_paragraph (edit
, force
);
493 indent
= test_indent (edit
, p
, q
);
495 t
= get_paragraph (&edit
->buffer
, p
, q
, indent
!= 0);
501 char *stop_format_chars
;
503 if (edit_options
.stop_format_chars
!= NULL
504 && strchr (edit_options
.stop_format_chars
, t
->str
[0]) != NULL
)
506 g_string_free (t
, TRUE
);
510 if (edit_options
.stop_format_chars
== NULL
|| *edit_options
.stop_format_chars
== '\0')
511 stop_format_chars
= g_strdup ("\t");
513 stop_format_chars
= g_strconcat (edit_options
.stop_format_chars
, "\t", (char *) NULL
);
515 for (i
= 0; i
< size
- 1; i
++)
516 if (t
->str
[i
] == '\n' && strchr (stop_format_chars
, t
->str
[i
+ 1]) != NULL
)
518 g_free (stop_format_chars
);
519 g_string_free (t
, TRUE
);
523 g_free (stop_format_chars
);
526 t2
= (unsigned char *) g_string_free (t
, FALSE
);
530 format_this (t2
, q
- p
, indent
, utf8
);
531 put_paragraph (edit
, t2
, p
, indent
, size
);
532 g_free ((char *) t2
);
534 /* Scroll left as much as possible to show the formatted paragraph */
535 edit_scroll_left (edit
, -edit
->start_col
);
538 /* --------------------------------------------------------------------------------------------- */