Ticket #3262: rename variables.
[midnight-commander.git] / src / editor / format.c
blobb198533b72203f19a9393b97feee558c8aacc0b9
1 /*
2 Dynamic paragraph formatting.
4 Copyright (C) 2011-2015
5 Free Software Foundation, Inc.
7 Copyright (C) 1996 Paul Sheer
9 Writen by:
10 Paul Sheer, 1996
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/>.
29 /** \file
30 * \brief Source: Dynamic paragraph formatting
31 * \author Paul Sheer
32 * \date 1996
33 * \author Andrew Borodin
34 * \date 2013, 2014
37 #include <config.h>
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 <errno.h>
46 #include <sys/stat.h>
48 #include <stdlib.h>
50 #include "lib/global.h"
52 #include "src/setup.h" /* option_tab_spacing */
54 #include "edit-impl.h"
55 #include "editwidget.h"
57 /*** global variables ****************************************************************************/
59 char *option_stop_format_chars = NULL;
61 /*** file scope macro definitions ****************************************************************/
63 #define tab_width option_tab_spacing
65 #define FONT_MEAN_WIDTH 1
67 /*** file scope type declarations ****************************************************************/
69 /*** file scope variables ************************************************************************/
71 /*** file scope functions ************************************************************************/
72 /* --------------------------------------------------------------------------------------------- */
74 static off_t
75 line_start (const edit_buffer_t * buf, long line)
77 off_t p;
78 long l;
80 l = buf->curs_line;
81 p = buf->curs1;
83 if (line < l)
84 p = edit_buffer_move_backward (buf, p, l - line);
85 else if (line > l)
86 p = edit_buffer_move_forward (buf, p, line - l, 0);
88 p = edit_buffer_get_bol (buf, p);
89 while (strchr ("\t ", edit_buffer_get_byte (buf, p)) != NULL)
90 p++;
91 return p;
94 /* --------------------------------------------------------------------------------------------- */
96 static gboolean
97 bad_line_start (const edit_buffer_t * buf, off_t p)
99 int c;
101 c = edit_buffer_get_byte (buf, p);
102 if (c == '.')
104 /* `...' is acceptable */
105 return !(edit_buffer_get_byte (buf, p + 1) == '.'
106 && edit_buffer_get_byte (buf, p + 2) == '.');
108 if (c == '-')
110 /* `---' is acceptable */
111 return !(edit_buffer_get_byte (buf, p + 1) == '-'
112 && edit_buffer_get_byte (buf, p + 2) == '-');
115 return (option_stop_format_chars != NULL && strchr (option_stop_format_chars, c) != NULL);
118 /* --------------------------------------------------------------------------------------------- */
120 * Find the start of the current paragraph for the purpose of formatting.
121 * Return position in the file.
124 static off_t
125 begin_paragraph (WEdit * edit, gboolean force, long *lines)
127 long i;
129 for (i = edit->buffer.curs_line - 1; i >= 0; i--)
130 if (edit_line_is_blank (edit, i) ||
131 (force && bad_line_start (&edit->buffer, line_start (&edit->buffer, i))))
133 i++;
134 break;
137 *lines = edit->buffer.curs_line - i;
139 return edit_buffer_move_backward (&edit->buffer, edit_buffer_get_current_bol (&edit->buffer),
140 *lines);
143 /* --------------------------------------------------------------------------------------------- */
145 * Find the end of the current paragraph for the purpose of formatting.
146 * Return position in the file.
149 static off_t
150 end_paragraph (WEdit * edit, gboolean force)
152 long i;
154 for (i = edit->buffer.curs_line + 1; i <= edit->buffer.lines; i++)
155 if (edit_line_is_blank (edit, i) ||
156 (force && bad_line_start (&edit->buffer, line_start (&edit->buffer, i))))
158 i--;
159 break;
162 return edit_buffer_get_eol (&edit->buffer,
163 edit_buffer_move_forward (&edit->buffer,
164 edit_buffer_get_current_bol
165 (&edit->buffer),
166 i - edit->buffer.curs_line, 0));
169 /* --------------------------------------------------------------------------------------------- */
171 static GString *
172 get_paragraph (const edit_buffer_t * buf, off_t p, off_t q, gboolean indent)
174 GString *t;
176 t = g_string_sized_new (128);
178 for (; p < q; p++)
180 if (indent && edit_buffer_get_byte (buf, p - 1) == '\n')
181 while (strchr ("\t ", edit_buffer_get_byte (buf, p)) != NULL)
182 p++;
184 g_string_append_c (t, edit_buffer_get_byte (buf, p));
187 g_string_append_c (t, '\n');
189 return t;
192 /* --------------------------------------------------------------------------------------------- */
194 static inline void
195 strip_newlines (unsigned char *t, off_t size)
197 unsigned char *p;
199 for (p = t; size-- != 0; p++)
200 if (*p == '\n')
201 *p = ' ';
204 /* --------------------------------------------------------------------------------------------- */
206 This function calculates the number of chars in a line specified to length l in pixels
209 static inline off_t
210 next_tab_pos (off_t x)
212 x += tab_width - x % tab_width;
213 return x;
216 /* --------------------------------------------------------------------------------------------- */
218 static inline off_t
219 line_pixel_length (unsigned char *t, off_t b, off_t l, gboolean utf8)
221 off_t xn, x; /* position conters */
222 off_t char_length; /* character length in bytes */
224 #ifndef HAVE_CHARSET
225 (void) utf8;
226 #endif
228 for (xn = 0, x = 0; xn <= l; x = xn, b += char_length)
230 char *tb;
232 tb = (char *) t + b;
233 char_length = 1;
235 switch (tb[0])
237 case '\n':
238 return b;
239 case '\t':
240 xn = next_tab_pos (x);
241 break;
242 default:
243 #ifdef HAVE_CHARSET
244 if (utf8)
246 gunichar ch;
248 ch = g_utf8_get_char_validated (tb, -1);
249 if (ch != (gunichar) (-2) && ch != (gunichar) (-1))
251 char *next_ch;
253 /* Calculate UTF-8 char length */
254 next_ch = g_utf8_next_char (tb);
255 if (next_ch != NULL)
256 char_length = next_ch - tb;
258 if (g_unichar_iswide (ch))
259 x++;
262 #endif
264 xn = x + 1;
265 break;
269 return b;
272 /* --------------------------------------------------------------------------------------------- */
274 static off_t
275 next_word_start (unsigned char *t, off_t q, off_t size)
277 off_t i;
278 gboolean saw_ws = FALSE;
280 for (i = q; i < size; i++)
282 switch (t[i])
284 case '\n':
285 return -1;
286 case '\t':
287 case ' ':
288 saw_ws = TRUE;
289 break;
290 default:
291 if (saw_ws)
292 return i;
293 break;
296 return (-1);
299 /* --------------------------------------------------------------------------------------------- */
300 /** find the start of a word */
302 static inline int
303 word_start (unsigned char *t, off_t q, off_t size)
305 off_t i;
307 if (t[q] == ' ' || t[q] == '\t')
308 return next_word_start (t, q, size);
310 for (i = q;; i--)
312 unsigned char c;
314 if (i == 0)
315 return (-1);
316 c = t[i - 1];
317 if (c == '\n')
318 return (-1);
319 if (c == ' ' || c == '\t')
320 return i;
324 /* --------------------------------------------------------------------------------------------- */
325 /** replaces ' ' with '\n' to properly format a paragraph */
327 static inline void
328 format_this (unsigned char *t, off_t size, long indent, gboolean utf8)
330 off_t q = 0, ww;
332 strip_newlines (t, size);
333 ww = option_word_wrap_line_length * FONT_MEAN_WIDTH - indent;
334 if (ww < FONT_MEAN_WIDTH * 2)
335 ww = FONT_MEAN_WIDTH * 2;
337 while (TRUE)
339 off_t p;
341 q = line_pixel_length (t, q, ww, utf8);
342 if (q > size)
343 break;
344 if (t[q] == '\n')
345 break;
346 p = word_start (t, q, size);
347 if (p == -1)
348 q = next_word_start (t, q, size); /* Return the end of the word if the beginning
349 of the word is at the beginning of a line
350 (i.e. a very long word) */
351 else
352 q = p;
353 if (q == -1) /* end of paragraph */
354 break;
355 if (q != 0)
356 t[q - 1] = '\n';
360 /* --------------------------------------------------------------------------------------------- */
362 static inline void
363 replace_at (WEdit * edit, off_t q, int c)
365 edit_cursor_move (edit, q - edit->buffer.curs1);
366 edit_delete (edit, TRUE);
367 edit_insert_ahead (edit, c);
370 /* --------------------------------------------------------------------------------------------- */
372 static long
373 edit_indent_width (const WEdit * edit, off_t p)
375 off_t q = p;
377 /* move to the end of the leading whitespace of the line */
378 while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, q)) != NULL
379 && q < edit->buffer.size - 1)
380 q++;
381 /* count the number of columns of indentation */
382 return (long) edit_move_forward3 (edit, p, 0, q);
385 /* --------------------------------------------------------------------------------------------- */
387 static void
388 edit_insert_indent (WEdit * edit, long indent)
390 if (!option_fill_tabs_with_spaces)
391 while (indent >= TAB_SIZE)
393 edit_insert (edit, '\t');
394 indent -= TAB_SIZE;
397 while (indent-- > 0)
398 edit_insert (edit, ' ');
401 /* --------------------------------------------------------------------------------------------- */
402 /** replaces a block of text */
404 static inline void
405 put_paragraph (WEdit * edit, unsigned char *t, off_t p, long indent, off_t size)
407 off_t cursor;
408 off_t i;
409 int c = '\0';
411 cursor = edit->buffer.curs1;
412 if (indent != 0)
413 while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p)) != NULL)
414 p++;
415 for (i = 0; i < size; i++, p++)
417 if (i != 0 && indent != 0)
419 if (t[i - 1] == '\n' && c == '\n')
421 while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p)) != NULL)
422 p++;
424 else if (t[i - 1] == '\n')
426 off_t curs;
428 edit_cursor_move (edit, p - edit->buffer.curs1);
429 curs = edit->buffer.curs1;
430 edit_insert_indent (edit, indent);
431 if (cursor >= curs)
432 cursor += edit->buffer.curs1 - p;
433 p = edit->buffer.curs1;
435 else if (c == '\n')
437 edit_cursor_move (edit, p - edit->buffer.curs1);
438 while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p)) != NULL)
440 edit_delete (edit, TRUE);
441 if (cursor > edit->buffer.curs1)
442 cursor--;
444 p = edit->buffer.curs1;
448 c = edit_buffer_get_byte (&edit->buffer, p);
449 if (c != t[i])
450 replace_at (edit, p, t[i]);
454 /* --------------------------------------------------------------------------------------------- */
456 static inline long
457 test_indent (const WEdit * edit, off_t p, off_t q)
459 long indent;
461 indent = edit_indent_width (edit, p++);
462 if (indent == 0)
463 return 0;
465 for (; p < q; p++)
466 if (edit_buffer_get_byte (&edit->buffer, p - 1) == '\n'
467 && indent != edit_indent_width (edit, p))
468 return 0;
469 return indent;
472 /* --------------------------------------------------------------------------------------------- */
473 /*** public functions ****************************************************************************/
474 /* --------------------------------------------------------------------------------------------- */
476 void
477 format_paragraph (WEdit * edit, gboolean force)
479 off_t p, q;
480 long lines;
481 off_t size;
482 GString *t;
483 long indent;
484 unsigned char *t2;
485 gboolean utf8 = FALSE;
487 if (option_word_wrap_line_length < 2)
488 return;
489 if (edit_line_is_blank (edit, edit->buffer.curs_line))
490 return;
492 p = begin_paragraph (edit, force, &lines);
493 q = end_paragraph (edit, force);
494 indent = test_indent (edit, p, q);
496 t = get_paragraph (&edit->buffer, p, q, indent != 0);
497 size = t->len - 1;
499 if (!force)
501 off_t i;
502 char *stop_format_chars;
504 if (option_stop_format_chars != NULL
505 && strchr (option_stop_format_chars, t->str[0]) != NULL)
507 g_string_free (t, TRUE);
508 return;
511 if (option_stop_format_chars == NULL || *option_stop_format_chars == '\0')
512 stop_format_chars = g_strdup ("\t");
513 else
514 stop_format_chars = g_strconcat (option_stop_format_chars, "\t", (char *) NULL);
516 for (i = 0; i < size - 1; i++)
517 if (t->str[i] == '\n' && strchr (stop_format_chars, t->str[i + 1]) != NULL)
519 g_free (stop_format_chars);
520 g_string_free (t, TRUE);
521 return;
524 g_free (stop_format_chars);
527 t2 = (unsigned char *) g_string_free (t, FALSE);
528 #ifdef HAVE_CHARSET
529 utf8 = edit->utf8;
530 #endif
531 /* scroll up to show 1st line of paragraph */
532 edit_move_up (edit, lines, TRUE);
533 /* scroll left as much as possible to show the formatted paragraph */
534 edit_scroll_left (edit, -edit->start_col);
536 format_this (t2, q - p, indent, utf8);
537 put_paragraph (edit, t2, p, indent, size);
538 g_free ((char *) t2);
540 /* move to the end of paragraph */
541 q = end_paragraph (edit, force);
542 edit_cursor_move (edit, q - edit->buffer.curs1);
544 /* try move to the start of next paragraph */
545 if (edit->buffer.curs_line < edit->buffer.lines)
547 edit_execute_cmd (edit, CK_Home, -1);
551 edit_execute_cmd (edit, CK_Down, -1);
553 while (edit->buffer.curs_line < edit->buffer.lines
554 && edit_line_is_blank (edit, edit->buffer.curs_line));
558 /* --------------------------------------------------------------------------------------------- */