new feature: allow specifying a search string to "jump to" at startup

The string to "jump to" is specified with +/ for a forward search
(from the top of the file), or with +? for a backward search (from
the bottom of the file).

This fulfills https://savannah.gnu.org/bugs/?54535.
Requested-by: Derek Wolfe <dwwolfe1@gmail.com>

With-help-from: Brand Huntsman <alpha@qzx.com>
This commit is contained in:
Benno Schulenberg 2019-08-09 14:55:06 +02:00
parent 073bd3ad6e
commit a9dd73fb16
2 changed files with 26 additions and 0 deletions

View File

@ -2581,9 +2581,19 @@ int main(int argc, char **argv)
/* Read the files mentioned on the command line into new buffers. */
while (optind < argc && (!openfile || read_them_all)) {
ssize_t givenline = 0, givencol = 0;
char *searchstring = NULL;
/* If there's a +LINE[,COLUMN] argument here, eat it up. */
if (optind < argc - 1 && argv[optind][0] == '+') {
if (argv[optind][1] == '/' || argv[optind][1] == '?') {
if (argv[optind][2]) {
searchstring = mallocstrcpy(NULL, &argv[optind][2]);
if (argv[optind][1] == '?')
SET(BACKWARDS_SEARCH);
} else
statusline(ALERT, _("Empty search string"));
optind++;
} else
if (!parse_line_column(&argv[optind++][1], &givenline, &givencol))
statusline(ALERT, _("Invalid line or column number"));
}
@ -2600,6 +2610,20 @@ int main(int argc, char **argv)
/* If a position was given on the command line, go there. */
if (givenline != 0 || givencol != 0)
do_gotolinecolumn(givenline, givencol, FALSE, FALSE);
else if (searchstring != NULL) {
if (ISSET(USE_REGEXP))
regexp_init(searchstring);
if (!findnextstr(searchstring, FALSE, JUSTFIND, NULL, TRUE,
openfile->filetop, 0))
not_found_msg(searchstring);
else
wipe_statusbar();
if (ISSET(USE_REGEXP))
tidy_up_after_search();
free(last_search);
last_search = searchstring;
searchstring = NULL;
}
#ifdef ENABLE_HISTORIES
else if (ISSET(POSITIONLOG) && openfile->filename[0] != '\0') {
ssize_t savedline, savedcol;

View File

@ -479,6 +479,8 @@ void do_rcfiles(void);
#endif /* ENABLE_NANORC */
/* Most functions in search.c. */
bool regexp_init(const char *regexp);
void tidy_up_after_search(void);
int findnextstr(const char *needle, bool whole_word_only, int modus,
size_t *match_len, bool skipone, const linestruct *begin, size_t begin_x);
void do_search(void);