overhaul the paragraph-searching code to make it simpler too

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2360 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
master
David Lawrence Ramsey 2005-03-13 21:12:25 +00:00
parent 9ec5db8b42
commit ff4a487efa
4 changed files with 86 additions and 110 deletions

View File

@ -171,10 +171,15 @@ CVS code -
parse_syntax(), parse_colors(), parse_rcfile(), do_rcfile(), parse_syntax(), parse_colors(), parse_rcfile(), do_rcfile(),
etc. (David Benbennick) DLR: Rename colortoint() to etc. (David Benbennick) DLR: Rename colortoint() to
color_to_int(), and add a few miscellaneous tweaks. color_to_int(), and add a few miscellaneous tweaks.
- Overhaul the justify code to make it leave the right number of - Overhaul the paragraph-searching code to make it use the
spaces at the ends of the lines of a paragraph, and also to paragraph-searching utility functions when possible instead of
make it simpler. Changes to justify_format() and duplicating code. Also overhaul the justify code to make it
do_justify(); removal of breakable(). (DLR) leave the right number of spaces at the ends of the lines of a
paragraph, and also to make it simpler. New functions
do_para_begin_void() and do_para_end_void(); changes to
justify_format(), do_para_begin(), inpar(), do_para_end(),
do_para_search() (renamed find_paragraph()), and do_justify();
removal of breakable(). (DLR)
- Still more steps toward full wide/multibyte character support. - Still more steps toward full wide/multibyte character support.
Make whitespace display mode work with multibyte characters, Make whitespace display mode work with multibyte characters,
and add a few related documentation updates. New function and add a few related documentation updates. New function

View File

@ -554,12 +554,12 @@ void shortcut_init(bool unjustify)
/* Translators: try to keep this string under 10 characters long */ /* Translators: try to keep this string under 10 characters long */
sc_init_one(&main_list, NANO_NO_KEY, beg_of_par_msg, sc_init_one(&main_list, NANO_NO_KEY, beg_of_par_msg,
IFHELP(nano_parabegin_msg, NANO_PARABEGIN_ALTKEY1), NANO_NO_KEY, IFHELP(nano_parabegin_msg, NANO_PARABEGIN_ALTKEY1), NANO_NO_KEY,
NANO_PARABEGIN_ALTKEY2, VIEW, do_para_begin); NANO_PARABEGIN_ALTKEY2, VIEW, do_para_begin_void);
/* Translators: try to keep this string under 10 characters long */ /* Translators: try to keep this string under 10 characters long */
sc_init_one(&main_list, NANO_NO_KEY, end_of_par_msg, sc_init_one(&main_list, NANO_NO_KEY, end_of_par_msg,
IFHELP(nano_paraend_msg, NANO_PARAEND_ALTKEY1), NANO_NO_KEY, IFHELP(nano_paraend_msg, NANO_PARAEND_ALTKEY1), NANO_NO_KEY,
NANO_PARAEND_ALTKEY2, VIEW, do_para_end); NANO_PARAEND_ALTKEY2, VIEW, do_para_end_void);
#endif #endif
#ifdef ENABLE_MULTIBUFFER #ifdef ENABLE_MULTIBUFFER
@ -637,12 +637,12 @@ void shortcut_init(bool unjustify)
/* Translators: try to keep this string under 10 characters long */ /* Translators: try to keep this string under 10 characters long */
sc_init_one(&whereis_list, NANO_PARABEGIN_KEY, beg_of_par_msg, sc_init_one(&whereis_list, NANO_PARABEGIN_KEY, beg_of_par_msg,
IFHELP(nano_parabegin_msg, NANO_PARABEGIN_ALTKEY1), NANO_NO_KEY, IFHELP(nano_parabegin_msg, NANO_PARABEGIN_ALTKEY1), NANO_NO_KEY,
NANO_PARABEGIN_ALTKEY2, VIEW, do_para_begin); NANO_PARABEGIN_ALTKEY2, VIEW, do_para_begin_void);
/* Translators: try to keep this string under 10 characters long */ /* Translators: try to keep this string under 10 characters long */
sc_init_one(&whereis_list, NANO_PARAEND_KEY, end_of_par_msg, sc_init_one(&whereis_list, NANO_PARAEND_KEY, end_of_par_msg,
IFHELP(nano_paraend_msg, NANO_PARAEND_ALTKEY1), NANO_NO_KEY, IFHELP(nano_paraend_msg, NANO_PARAEND_ALTKEY1), NANO_NO_KEY,
NANO_PARAEND_ALTKEY2, VIEW, do_para_end); NANO_PARAEND_ALTKEY2, VIEW, do_para_end_void);
#endif #endif
#ifndef NANO_SMALL #ifndef NANO_SMALL

View File

@ -2618,7 +2618,7 @@ bool begpar(const filestruct *const foo)
/* We find the last beginning-of-paragraph line before the current /* We find the last beginning-of-paragraph line before the current
* line. */ * line. */
void do_para_begin(void) void do_para_begin(bool allow_update)
{ {
const filestruct *current_save = current; const filestruct *current_save = current;
const size_t pww_save = placewewant; const size_t pww_save = placewewant;
@ -2633,20 +2633,33 @@ void do_para_begin(void)
} while (!begpar(current)); } while (!begpar(current));
} }
if (allow_update)
edit_redraw(current_save, pww_save); edit_redraw(current_save, pww_save);
} }
bool inpar(const char *str) void do_para_begin_void(void)
{ {
size_t quote_len = quote_length(str); do_para_begin(TRUE);
}
return str[quote_len + indent_length(str + quote_len)] != '\0'; /* Is foo inside a paragraph? */
bool inpar(const filestruct *const foo)
{
size_t quote_len;
if (foo == NULL)
return FALSE;
quote_len = quote_length(foo->data);
return foo->data[quote_len + indent_length(foo->data +
quote_len)] != '\0';
} }
/* A line is the last line of a paragraph if it is in a paragraph, and /* A line is the last line of a paragraph if it is in a paragraph, and
* the next line isn't, or is the beginning of a paragraph. We move * the next line isn't, or is the beginning of a paragraph. We move
* down to the end of a paragraph, then one line farther. */ * down to the end of a paragraph, then one line farther. */
void do_para_end(void) void do_para_end(bool allow_update)
{ {
const filestruct *const current_save = current; const filestruct *const current_save = current;
const size_t pww_save = placewewant; const size_t pww_save = placewewant;
@ -2654,10 +2667,10 @@ void do_para_end(void)
current_x = 0; current_x = 0;
placewewant = 0; placewewant = 0;
while (current->next != NULL && !inpar(current->data)) while (current->next != NULL && !inpar(current))
current = current->next; current = current->next;
while (current->next != NULL && inpar(current->next->data) && while (current->next != NULL && inpar(current->next) &&
!begpar(current->next)) { !begpar(current->next)) {
current = current->next; current = current->next;
current_y++; current_y++;
@ -2666,9 +2679,15 @@ void do_para_end(void)
if (current->next != NULL) if (current->next != NULL)
current = current->next; current = current->next;
if (allow_update)
edit_redraw(current_save, pww_save); edit_redraw(current_save, pww_save);
} }
void do_para_end_void(void)
{
do_para_end(TRUE);
}
/* Put the next par_len lines, starting with first_line, into the /* Put the next par_len lines, starting with first_line, into the
* justify buffer, leaving copies of those lines in place. Assume there * justify buffer, leaving copies of those lines in place. Assume there
* are enough lines after first_line. Return the new copy of * are enough lines after first_line. Return the new copy of
@ -2798,112 +2817,62 @@ ssize_t break_line(const char *line, ssize_t goal, bool force)
/* Find the beginning of the current paragraph if we're in one, or the /* Find the beginning of the current paragraph if we're in one, or the
* beginning of the next paragraph if we're not. Afterwards, save the * beginning of the next paragraph if we're not. Afterwards, save the
* quote length and paragraph length in *quote and *par. Return FALSE * quote length and paragraph length in *quote and *par. Return TRUE if
* if we found a paragraph, or TRUE if there was an error or we didn't * we found a paragraph, or FALSE if there was an error or we didn't
* find a paragraph. * find a paragraph.
* *
* See the comment at begpar() for more about when a line is the * See the comment at begpar() for more about when a line is the
* beginning of a paragraph. */ * beginning of a paragraph. */
bool do_para_search(size_t *const quote, size_t *const par) bool find_paragraph(size_t *const quote, size_t *const par)
{ {
size_t quote_len; size_t quote_len;
/* Length of the initial quotation of the paragraph we /* Length of the initial quotation of the paragraph we search
* search. */ * for. */
size_t par_len; size_t par_len;
/* Number of lines in that paragraph. */ /* Number of lines in the paragraph we search for. */
size_t indent_len; filestruct *current_save;
/* Generic indentation length. */ /* The line at the beginning of the paragraph we search for. */
filestruct *line; size_t current_y_save;
/* Generic line of text. */ /* The y-coordinate at the beginning of the paragraph we search
* for. */
#ifdef HAVE_REGEX_H #ifdef HAVE_REGEX_H
if (quoterc != 0) { if (quoterc != 0) {
statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr); statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr);
return TRUE; return FALSE;
} }
#endif #endif
/* Here is an assumption that is always true anyway. */
assert(current != NULL); assert(current != NULL);
/* Move back to the beginning of the current line. */
current_x = 0; current_x = 0;
/* Find the first line of the current or next paragraph. First, if
* the current line isn't in a paragraph, move forward to the line
* after the end of the next paragraph. If the line before that
* isn't in a paragraph, it means there aren't any paragraphs left,
* so get out. Otherwise, if the current line is in a paragraph and
* it isn't the first line of that paragraph, move back to the first
* line. */
if (!inpar(current)) {
do_para_end(FALSE);
if (!inpar(current->prev))
return FALSE;
}
if (!begpar(current))
do_para_begin(FALSE);
/* Now current is the first line of the paragraph. Set quote_len to
* the quotation length of that line, and set par_len to the number
* of lines in this paragraph. */
quote_len = quote_length(current->data); quote_len = quote_length(current->data);
indent_len = indent_length(current->data + quote_len); current_save = current;
current_y_save = current_y;
/* Here we find the first line of the paragraph to search. If the do_para_end(FALSE);
* current line is in a paragraph, then we move back to the first par_len = current->lineno - current_save->lineno;
* line of the paragraph. Otherwise, we move to the first line that current = current_save;
* is in a paragraph. */ current_y = current_y_save;
if (current->data[quote_len + indent_len] != '\0') {
/* This line is part of a paragraph. So we must search back to
* the first line of this paragraph. First we check items 1)
* and 3) above. */
while (current->prev != NULL && quotes_match(current->data,
quote_len, current->prev->data)) {
size_t temp_id_len =
indent_length(current->prev->data + quote_len);
/* The indentation length of the previous line. */
/* Is this line the beginning of a paragraph, according to
* items 2), 5), or 4) above? If so, stop. */
if (current->prev->data[quote_len + temp_id_len] == '\0' ||
(quote_len == 0 && indent_len > 0
#ifndef NANO_SMALL
&& !ISSET(AUTOINDENT)
#endif
) || !indents_match(current->prev->data + quote_len,
temp_id_len, current->data + quote_len, indent_len))
break;
indent_len = temp_id_len;
current = current->prev;
current_y--;
}
} else {
/* This line is not part of a paragraph. Move down until we get
* to a non "blank" line. */
do {
/* There is no next paragraph, so nothing to move to. */
if (current->next == NULL) {
placewewant = 0;
return TRUE;
}
current = current->next;
current_y++;
quote_len = quote_length(current->data);
indent_len = indent_length(current->data + quote_len);
} while (current->data[quote_len + indent_len] == '\0');
}
/* Now current is the first line of the paragraph, and quote_len is
* the quotation length of that line. */
/* Next step, compute par_len, the number of lines in this
* paragraph. */
line = current;
par_len = 1;
indent_len = indent_length(line->data + quote_len);
while (line->next != NULL &&
quotes_match(current->data, quote_len, line->next->data)) {
size_t temp_id_len = indent_length(line->next->data + quote_len);
if (!indents_match(line->data + quote_len, indent_len,
line->next->data + quote_len, temp_id_len) ||
line->next->data[quote_len + temp_id_len] == '\0' ||
(quote_len == 0 && temp_id_len > 0
#ifndef NANO_SMALL
&& !ISSET(AUTOINDENT)
#endif
))
break;
indent_len = temp_id_len;
line = line->next;
par_len++;
}
/* Now par_len is the number of lines in this paragraph. We should
* never call quotes_match() or quote_length() again. */
/* Save the values of quote_len and par_len. */ /* Save the values of quote_len and par_len. */
assert(quote != NULL && par != NULL); assert(quote != NULL && par != NULL);
@ -2911,7 +2880,7 @@ bool do_para_search(size_t *const quote, size_t *const par)
*quote = quote_len; *quote = quote_len;
*par = par_len; *par = par_len;
return FALSE; return TRUE;
} }
/* If full_justify is TRUE, justify the entire file. Otherwise, justify /* If full_justify is TRUE, justify the entire file. Otherwise, justify
@ -2978,7 +2947,7 @@ void do_justify(bool full_justify)
* line of the text to be justified to the last line of the file * line of the text to be justified to the last line of the file
* and break out of the loop. Otherwise, refresh the screen and * and break out of the loop. Otherwise, refresh the screen and
* get out. */ * get out. */
if (do_para_search(&quote_len, &par_len)) { if (!find_paragraph(&quote_len, &par_len)) {
if (full_justify && first_par_line != filebot) { if (full_justify && first_par_line != filebot) {
last_par_line = filebot; last_par_line = filebot;
break; break;

View File

@ -421,13 +421,15 @@ bool quotes_match(const char *a_line, size_t a_quote, const char
bool indents_match(const char *a_line, size_t a_indent, const char bool indents_match(const char *a_line, size_t a_indent, const char
*b_line, size_t b_indent); *b_line, size_t b_indent);
bool begpar(const filestruct *const foo); bool begpar(const filestruct *const foo);
void do_para_begin(void); void do_para_begin(bool allow_update);
bool inpar(const char *str); void do_para_begin_void(void);
void do_para_end(void); bool inpar(const filestruct *const foo);
void do_para_end(bool allow_update);
void do_para_end_void(void);
filestruct *backup_lines(filestruct *first_line, size_t par_len, size_t filestruct *backup_lines(filestruct *first_line, size_t par_len, size_t
quote_len); quote_len);
ssize_t break_line(const char *line, ssize_t goal, bool force); ssize_t break_line(const char *line, ssize_t goal, bool force);
bool do_para_search(size_t *const quote, size_t *const par); bool find_paragraph(size_t *const quote, size_t *const par);
void do_justify(bool full_justify); void do_justify(bool full_justify);
void do_justify_void(void); void do_justify_void(void);
void do_full_justify(void); void do_full_justify(void);