From 4b741b9c7b8416e02145bc761a2d289ddb896ea2 Mon Sep 17 00:00:00 2001 From: David Lawrence Ramsey Date: Fri, 30 Apr 2004 19:40:03 +0000 Subject: [PATCH] add DB's overhaul of regexp_init() and a few other regex-related bits, and convert REGEXP_COMPILED to a static int in search.c, since it's only used there git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1724 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- ChangeLog | 17 +++++++++++++++++ src/nano.h | 35 +++++++++++++++++----------------- src/search.c | 54 ++++++++++++++++++++++++++++++---------------------- src/winio.c | 1 + 4 files changed, 66 insertions(+), 41 deletions(-) diff --git a/ChangeLog b/ChangeLog index 38c66060..5bd40080 100644 --- a/ChangeLog +++ b/ChangeLog @@ -52,6 +52,9 @@ CVS code - - Use size_t's instead of ints for the get_verbatim_kbinput() call and the loop that ungetch()es its returned int*, respectively. (DLR) +- nano.h: + - Since REGEXP_COMPILED is only used in search.c, convert it + from a flag to a static int there. (DLR) - proto.h: - Remove unused xpt() and add_marked_sameline() prototypes. (DLR) @@ -59,6 +62,15 @@ CVS code - - Move "rebinddelete" up in the list of options so that the list is in alphabetical order. (DLR) - search.c: + regexp_init() + - Overhaul for efficiency. Also check if regcomp() failed, and + if so, display "Bad regex" message on the statusbar, so that + we don't have to display it separately after every call to + this function. (David Benbennick) + search_init() + - Only check whether USE_REGEXP is set, and hence whether or not + to display "[Regexp]" on the search prompt, if HAVE_REGEX_H is + defined. (DLR) not_found_msg() - Convert to properly handle strings generated by display_string() that have been used in the search prompt @@ -96,6 +108,11 @@ CVS code - searched_later_lines. (DLR) edit_refresh() - Remove apparently unneeded leaveok() calls. (David Benbennick) + statusbar() + - Call reset_cursor() just before refreshing the edit window, so + that slang and other non-ncurses versions of curses will + properly place the cursor back in the edit window instead of + leaving it at the end of the statusbar. (DLR) do_credits() - Use napms() instead of nanosleep(), as it does the same thing (aside from taking an argument in milliseconds instead of diff --git a/src/nano.h b/src/nano.h index 6ae094c6..bd2ca43c 100644 --- a/src/nano.h +++ b/src/nano.h @@ -251,24 +251,23 @@ typedef struct historyheadtype { #define VIEW_MODE (1<<10) #define USE_MOUSE (1<<11) #define USE_REGEXP (1<<12) -#define REGEXP_COMPILED (1<<13) -#define TEMP_OPT (1<<14) -#define CUT_TO_END (1<<15) -#define REVERSE_SEARCH (1<<16) -#define MULTIBUFFER (1<<17) -#define DOS_FILE (1<<18) -#define MAC_FILE (1<<19) -#define SMOOTHSCROLL (1<<20) -#define DISABLE_CURPOS (1<<21) /* Damn, we still need it. */ -#define REBIND_DELETE (1<<22) -#define NO_CONVERT (1<<23) -#define BACKUP_FILE (1<<24) -#define NO_RCFILE (1<<25) -#define COLOR_SYNTAX (1<<26) -#define PRESERVE (1<<27) -#define HISTORY_CHANGED (1<<28) -#define HISTORYLOG (1<<29) -#define RESTRICTED (1<<30) +#define TEMP_OPT (1<<13) +#define CUT_TO_END (1<<14) +#define REVERSE_SEARCH (1<<15) +#define MULTIBUFFER (1<<16) +#define DOS_FILE (1<<17) +#define MAC_FILE (1<<18) +#define SMOOTHSCROLL (1<<19) +#define DISABLE_CURPOS (1<<20) /* Damn, we still need it. */ +#define REBIND_DELETE (1<<21) +#define NO_CONVERT (1<<22) +#define BACKUP_FILE (1<<23) +#define NO_RCFILE (1<<24) +#define COLOR_SYNTAX (1<<25) +#define PRESERVE (1<<26) +#define HISTORY_CHANGED (1<<27) +#define HISTORYLOG (1<<28) +#define RESTRICTED (1<<29) /* Control key sequences, changing these would be very very bad. */ #define NANO_CONTROL_SPACE 0 diff --git a/src/search.c b/src/search.c index 03d30e06..eed733c8 100644 --- a/src/search.c +++ b/src/search.c @@ -31,24 +31,39 @@ #include "proto.h" #include "nano.h" -/* Regular expression helper functions */ #ifdef HAVE_REGEX_H +static int regexp_compiled = FALSE; + +/* Regular expression helper functions. */ + +/* Compile the given regular expression. Return value 0 means the + * expression was invalid, and we wrote an error message on the status + * bar. Return value 1 means success. */ int regexp_init(const char *regexp) { - /* Hmm, perhaps we should check for whether regcomp returns successfully */ - if (regcomp(&search_regexp, regexp, (ISSET(CASE_SENSITIVE) ? 0 : REG_ICASE) - | REG_EXTENDED) != 0) + int rc = regcomp(&search_regexp, regexp, REG_EXTENDED | + (ISSET(CASE_SENSITIVE) ? 0 : REG_ICASE)); + + assert(!regexp_compiled); + if (rc != 0) { + size_t len = regerror(rc, &search_regexp, NULL, 0); + char *str = charalloc(len); + + regerror(rc, &search_regexp, str, len); + statusbar(_("Bad regex \"%s\": %s"), regexp, str); + free(str); return 0; + } - SET(REGEXP_COMPILED); + regexp_compiled = TRUE; return 1; } void regexp_cleanup(void) { - if (ISSET(REGEXP_COMPILED)) { - UNSET(REGEXP_COMPILED); + if (regexp_compiled) { + regexp_compiled = FALSE; regfree(&search_regexp); } } @@ -146,9 +161,12 @@ int search_init(int replacing) #endif "", +#ifdef HAVE_REGEX_H /* This string is just a modifier for the search prompt; no * grammar is implied. */ - ISSET(USE_REGEXP) ? _(" [Regexp]") : "", + ISSET(USE_REGEXP) ? _(" [Regexp]") : +#endif + "", #ifndef NANO_SMALL /* This string is just a modifier for the search prompt; no @@ -170,7 +188,6 @@ int search_init(int replacing) if (i == -1 || (i < 0 && last_search[0] == '\0') || (!replacing && i == 0 && answer[0] == '\0')) { statusbar(_("Search Cancelled")); - reset_cursor(); #ifndef NANO_SMALL search_history.current = search_history.next; #endif @@ -180,21 +197,15 @@ int search_init(int replacing) case -2: /* It's the same string. */ #ifdef HAVE_REGEX_H /* Since answer is "", use last_search! */ - if (ISSET(USE_REGEXP) && regexp_init(last_search) == 0) { - statusbar(_("Invalid regex \"%s\""), last_search); - reset_cursor(); + if (ISSET(USE_REGEXP) && regexp_init(last_search) == 0) return -1; - } #endif break; case 0: /* They entered something new. */ last_replace[0] = '\0'; #ifdef HAVE_REGEX_H - if (ISSET(USE_REGEXP) && regexp_init(answer) == 0) { - statusbar(_("Invalid regex \"%s\""), answer); - reset_cursor(); + if (ISSET(USE_REGEXP) && regexp_init(answer) == 0) return -1; - } #endif break; #ifndef NANO_SMALL @@ -424,11 +435,8 @@ int do_research(void) #ifdef HAVE_REGEX_H /* Since answer is "", use last_search! */ - if (ISSET(USE_REGEXP) && regexp_init(last_search) == 0) { - statusbar(_("Invalid regex \"%s\""), last_search); - reset_cursor(); - return -1; - } + if (ISSET(USE_REGEXP) && regexp_init(last_search) == 0) + return -1; #endif search_last_line = 0; @@ -914,7 +922,7 @@ int do_find_bracket(void) regexp_init(regexp_pat); /* We constructed regexp_pat to be a valid expression. */ - assert(ISSET(REGEXP_COMPILED)); + assert(regexp_compiled); search_last_line = 0; while (TRUE) { diff --git a/src/winio.c b/src/winio.c index 7abaca60..2f05b35c 100644 --- a/src/winio.c +++ b/src/winio.c @@ -2380,6 +2380,7 @@ void statusbar(const char *msg, ...) waddstr(bottomwin, " ]"); wattroff(bottomwin, A_REVERSE); wnoutrefresh(bottomwin); + reset_cursor(); wrefresh(edit); /* Leave the cursor at its position in the edit window, not * in the statusbar. */ -- 2.39.5