add DB's tweaks to do_wrap(), plus a few minor bits of mine (convert

some ints to size_t's and convert the int wrapping to use TRUE and FALSE
since it's used as a boolean)


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1793 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
master
David Lawrence Ramsey 2004-05-31 14:58:59 +00:00
parent 684b19380e
commit 9a527f5a17
2 changed files with 41 additions and 37 deletions

View File

@ -156,6 +156,8 @@ CVS code -
- Switch the last test (current != NULL or not) around to match - Switch the last test (current != NULL or not) around to match
the order of the same test in do_next_word() (current == the order of the same test in do_next_word() (current ==
NULL). The results are the same either way. (DLR) NULL). The results are the same either way. (DLR)
do_wrap()
- Tweak for efficiency. (David Benbennick)
do_spell() do_spell()
- Tweak for efficiency. (David Benbennick) - Tweak for efficiency. (David Benbennick)
- Change the statusbar entries used in cases of failure so that - Change the statusbar entries used in cases of failure so that

View File

@ -1258,28 +1258,31 @@ void wrap_reset(void)
#endif #endif
#ifndef DISABLE_WRAPPING #ifndef DISABLE_WRAPPING
/* We wrap the given line. Precondition: we assume the cursor has been /* We wrap the given line. Precondition: we assume the cursor has been
* moved forward since the last typed character. Return value: * moved forward since the last typed character. Return value: whether
* whether we wrapped. */ * we wrapped. */
int do_wrap(filestruct *inptr) int do_wrap(filestruct *inptr)
{ {
size_t len = strlen(inptr->data); /* length of the line we wrap */ size_t len = strlen(inptr->data);
int i = 0; /* generic loop variable */ /* Length of the line we wrap. */
int wrap_loc = -1; /* index of inptr->data where we wrap */ size_t i = 0;
/* Generic loop variable. */
int wrap_loc = -1;
/* Index of inptr->data where we wrap. */
int word_back = -1; int word_back = -1;
#ifndef NANO_SMALL #ifndef NANO_SMALL
const char *indentation = NULL; const char *indentation = NULL;
/* indentation to prepend to the new line */ /* Indentation to prepend to the new line. */
int indent_len = 0; /* strlen(indentation) */ size_t indent_len = 0; /* strlen(indentation) */
#endif #endif
const char *after_break; /* text after the wrap point */ const char *after_break; /* Text after the wrap point. */
int after_break_len; /* strlen(after_break) */ size_t after_break_len; /* strlen(after_break) */
int wrapping = 0; /* do we prepend to the next line? */ int wrapping = FALSE; /* Do we prepend to the next line? */
const char *wrap_line = NULL; const char *wrap_line = NULL;
/* the next line, minus indentation */ /* The next line, minus indentation */
int wrap_line_len = 0; /* strlen(wrap_line) */ size_t wrap_line_len = 0; /* strlen(wrap_line) */
char *newline = NULL; /* the line we create */ char *newline = NULL; /* The line we create. */
int new_line_len = 0; /* eventual length of newline */ size_t new_line_len = 0; /* Eventual length of newline. */
/* There are three steps. First, we decide where to wrap. Then, we /* There are three steps. First, we decide where to wrap. Then, we
* create the new wrap line. Finally, we clean up. */ * create the new wrap line. Finally, we clean up. */
@ -1310,23 +1313,23 @@ int do_wrap(filestruct *inptr)
#endif #endif
wrap_line = inptr->data + i; wrap_line = inptr->data + i;
for (; i < len; i++, wrap_line++) { for (; i < len; i++, wrap_line++) {
/* record where the last word ended */ /* Record where the last word ended. */
if (!isblank(*wrap_line)) if (!isblank(*wrap_line))
word_back = i; word_back = i;
/* if we have found a "legal wrap point" and the current word /* If we have found a "legal wrap point" and the current word
* extends too far, then we stop */ * extends too far, then we stop. */
if (wrap_loc != -1 && strnlenpt(inptr->data, word_back + 1) > fill) if (wrap_loc != -1 && strnlenpt(inptr->data, word_back + 1) > fill)
break; break;
/* we record the latest "legal wrap point" */ /* We record the latest "legal wrap point". */
if (word_back != i && !isblank(wrap_line[1])) if (word_back != i && !isblank(wrap_line[1]))
wrap_loc = i; wrap_loc = i;
} }
if (wrap_loc < 0 || i == len) if (i == len)
return 0; return FALSE;
/* Step 2, making the new wrap line. It will consist of indentation + /* Step 2, making the new wrap line. It will consist of indentation
* after_break + " " + wrap_line (although indentation and wrap_line are * + after_break + " " + wrap_line (although indentation and
* conditional on flags and #defines). */ * wrap_line are conditional on flags and #defines). */
/* after_break is the text that will be moved to the next line. */ /* after_break is the text that will be moved to the next line. */
after_break = inptr->data + wrap_loc + 1; after_break = inptr->data + wrap_loc + 1;
@ -1344,9 +1347,9 @@ int do_wrap(filestruct *inptr)
wrap_line = inptr->next->data; wrap_line = inptr->next->data;
wrap_line_len = strlen(wrap_line); wrap_line_len = strlen(wrap_line);
/* +1 for the space between after_break and wrap_line */ /* +1 for the space between after_break and wrap_line. */
if ((new_line_len + 1 + wrap_line_len) <= fill) { if ((new_line_len + 1 + wrap_line_len) <= fill) {
wrapping = 1; wrapping = TRUE;
new_line_len += (1 + wrap_line_len); new_line_len += (1 + wrap_line_len);
} }
} }
@ -1374,11 +1377,13 @@ int do_wrap(filestruct *inptr)
if (ISSET(AUTOINDENT)) { if (ISSET(AUTOINDENT)) {
strncpy(newline, indentation, indent_len); strncpy(newline, indentation, indent_len);
newline[indent_len] = '\0'; newline[indent_len] = '\0';
new_line_len = indent_len;
} }
#endif #endif
strcat(newline, after_break); strcat(newline, after_break);
/* We end the old line after wrap_loc. Note this does not eat the new_line_len += after_break_len;
* space. */ /* We end the old line after wrap_loc. Note that this does not eat
* the space. */
null_at(&inptr->data, wrap_loc + 1); null_at(&inptr->data, wrap_loc + 1);
totsize++; totsize++;
if (wrapping) { if (wrapping) {
@ -1386,7 +1391,7 @@ int do_wrap(filestruct *inptr)
* between after_break and wrap_line. If the line already ends * between after_break and wrap_line. If the line already ends
* in a tab or a space, we don't add a space and decrement * in a tab or a space, we don't add a space and decrement
* totsize to account for that. */ * totsize to account for that. */
if (!isblank(newline[strlen(newline) - 1])) if (!isblank(newline[new_line_len - 1]))
strcat(newline, " "); strcat(newline, " ");
else else
totsize--; totsize--;
@ -1396,8 +1401,8 @@ int do_wrap(filestruct *inptr)
} else { } else {
filestruct *temp = (filestruct *)nmalloc(sizeof(filestruct)); filestruct *temp = (filestruct *)nmalloc(sizeof(filestruct));
/* In this case, the file size changes by +1 for the new line, and /* In this case, the file size changes by +1 for the new line,
* +indent_len for the new indentation. */ * and +indent_len for the new indentation. */
#ifndef NANO_SMALL #ifndef NANO_SMALL
totsize += indent_len; totsize += indent_len;
#endif #endif
@ -1414,8 +1419,8 @@ int do_wrap(filestruct *inptr)
filebot = temp; filebot = temp;
} }
/* Step 3, clean up. Here we reposition the cursor and mark, and do some /* Step 3, clean up. Here we reposition the cursor and mark, and do
* other sundry things. */ * some other sundry things. */
/* later wraps of this line will be prepended to the next line. */ /* later wraps of this line will be prepended to the next line. */
same_line_wrap = 1; same_line_wrap = 1;
@ -1448,10 +1453,7 @@ int do_wrap(filestruct *inptr)
mark_beginx += after_break_len; mark_beginx += after_break_len;
#endif /* !NANO_SMALL */ #endif /* !NANO_SMALL */
/* Place the cursor. */ return TRUE;
reset_cursor();
return 1;
} }
#endif /* !DISABLE_WRAPPING */ #endif /* !DISABLE_WRAPPING */