Skip to content

Commit

Permalink
Add the --search-options option.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed Oct 12, 2021
1 parent 440f178 commit e8edfb8
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 9 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

* Add the --redraw-on-quit option.

* Add the --search-options option.

* Add 'H' color type to set color of header lines.

* Add #version conditional to lesskey.
Expand All @@ -40,6 +42,8 @@

* Fix bug when parsing a malformed lesskey file.

* Fix bug scrolling history when --incsearch is set.

* Fix buffer overflow when invoking lessecho with more than 63 -m/-n options.

======================================================================
Expand Down
21 changes: 17 additions & 4 deletions command.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ extern void *ml_search;
extern void *ml_examine;
extern int wheel_lines;
extern int header_lines;
extern int def_search_type;
extern int updown_match;
#if SHELL_ESCAPE || PIPEC
extern void *ml_shell;
Expand Down Expand Up @@ -516,6 +517,19 @@ mca_opt_char(c)
return (MCA_MORE);
}

/*
* Normalize search type.
*/
public int
norm_search_type(st)
int st;
{
/* WRAP and PAST_EOF are mutually exclusive. */
if ((st & (SRCH_PAST_EOF|SRCH_WRAP)) == (SRCH_PAST_EOF|SRCH_WRAP))
st ^= SRCH_PAST_EOF;
return st;
}

/*
* Handle a char of a search command.
*/
Expand Down Expand Up @@ -566,8 +580,7 @@ mca_search_char(c)

if (flag != 0)
{
/* Toggle flag, but keep PAST_EOF and WRAP mutually exclusive. */
search_type ^= flag | (search_type & (SRCH_PAST_EOF|SRCH_WRAP));
search_type = norm_search_type(search_type ^ flag);
mca_search();
return (MCA_MORE);
}
Expand Down Expand Up @@ -1618,7 +1631,7 @@ commands(VOID_PARAM)
* Search forward for a pattern.
* Get the first char of the pattern.
*/
search_type = SRCH_FORW;
search_type = SRCH_FORW | def_search_type;
if (number <= 0)
number = 1;
mca_search();
Expand All @@ -1630,7 +1643,7 @@ commands(VOID_PARAM)
* Search backward for a pattern.
* Get the first char of the pattern.
*/
search_type = SRCH_BACK;
search_type = SRCH_BACK | def_search_type;
if (number <= 0)
number = 1;
mca_search();
Expand Down
2 changes: 2 additions & 0 deletions less.hlp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@
Set the character used to mark truncated lines.
--save-marks
Retain marks across invocations of less.
--search-options=[EFKNRW-]
Set default options for every search.
--status-col-width=N
Set the width of the -J status column to N characters.
--status-line
Expand Down
10 changes: 10 additions & 0 deletions less.nro.VER
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ WRAP around the current file.
That is, if the search reaches the end of the current file
without finding a match, the search continues from the first line of the
current file up to the line where it started.
If the ^W modifier is set, the ^E modifier is ignored.
.RE
.IP ?pattern
Search backward in the file for the N-th line containing the pattern.
Expand Down Expand Up @@ -1173,6 +1174,15 @@ has quit.
Save marks in the history file, so marks are retained
across different invocations of
.IR less .
.IP "\-\-search-options"
Sets default search modifiers.
The value is a string of one or more of the characters
E, F, K, N, R or W.
Setting any of these has the same effect as typing that
control character at the beginning of every search pattern.
For example, setting \-\-search-options=W is the same as
typing ^W at the beginning of every pattern.
The value "-" disables all default search modifiers.
.IP "\-\-status-col-width"
Sets the width of the status column when the \-J option is in effect.
The default is 2 characters.
Expand Down
57 changes: 57 additions & 0 deletions optfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ extern int use_color;
extern int want_filesize;
extern int header_lines;
extern int header_cols;
extern int def_search_type;
#if LOGFILE
extern char *namelogfile;
extern int force_logfile;
Expand Down Expand Up @@ -1030,6 +1031,62 @@ opt_header(type, s)
}
}

/*
* Handler for the --search-options option.
*/
/*ARGSUSED*/
public void
opt_search_type(type, s)
int type;
char *s;
{
int st;
PARG parg;
char buf[16];
char *bp;

switch (type)
{
case INIT:
case TOGGLE:
st = 0;
for (; *s != '\0'; s++)
{
switch (*s)
{
case 'E': case 'e': case CONTROL('E'): st |= SRCH_PAST_EOF; break;
case 'F': case 'f': case CONTROL('F'): st |= SRCH_FIRST_FILE; break;
case 'K': case 'k': case CONTROL('K'): st |= SRCH_NO_MOVE; break;
case 'N': case 'n': case CONTROL('N'): st |= SRCH_NO_MATCH; break;
case 'R': case 'r': case CONTROL('R'): st |= SRCH_NO_REGEX; break;
case 'W': case 'w': case CONTROL('W'): st |= SRCH_WRAP; break;
case '-': st = 0; break;
case '^': break;
default:
parg.p_char = *s;
error("invalid search option '%c'", &parg);
return;
}
}
def_search_type = norm_search_type(st);
break;
case QUERY:
bp = buf;
if (def_search_type & SRCH_PAST_EOF) *bp++ = 'E';
if (def_search_type & SRCH_FIRST_FILE) *bp++ = 'F';
if (def_search_type & SRCH_NO_MOVE) *bp++ = 'K';
if (def_search_type & SRCH_NO_MATCH) *bp++ = 'N';
if (def_search_type & SRCH_NO_REGEX) *bp++ = 'R';
if (def_search_type & SRCH_WRAP) *bp++ = 'W';
if (bp == buf)
*bp++ = '-';
*bp = '\0';
parg.p_string = buf;
error("search options: %s", &parg);
break;
}
}

#if LESSTEST
/*
* Handler for the --tty option.
Expand Down
12 changes: 11 additions & 1 deletion opttbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public int header_lines; /* Freeze header lines at top of screen */
public int header_cols; /* Freeze header columns at left of screen */
public int nonum_headers; /* Don't give headers line numbers */
public int redraw_on_quit; /* Redraw last screen after term deinit */
public int def_search_type; /* */
#if HILITE_SEARCH
public int hilite_search; /* Highlight matched search patterns? */
#endif
Expand Down Expand Up @@ -148,6 +149,7 @@ static struct optname status_line_optname = { "status-line", NULL };
static struct optname header_optname = { "header", NULL };
static struct optname nonum_headers_optname = { "no-number-headers", NULL };
static struct optname redraw_on_quit_optname = { "redraw-on-quit", NULL };
static struct optname search_type_optname = { "search-options", NULL };
#if LESSTEST
static struct optname ttyin_name_optname = { "tty", NULL };
static struct optname rstat_optname = { "rstat", NULL };
Expand Down Expand Up @@ -583,7 +585,7 @@ static struct loption option[] =
STRING|REPAINT, 0, NULL, opt_header,
{
"Header lines: ",
"%d header lines",
NULL,
NULL
}
},
Expand All @@ -603,6 +605,14 @@ static struct loption option[] =
NULL
}
},
{ OLETTER_NONE, &search_type_optname,
STRING, 0, NULL, opt_search_type,
{
"Search options: ",
NULL,
NULL
}
},
#if LESSTEST
{ OLETTER_NONE, &ttyin_name_optname,
STRING|NO_TOGGLE, 0, NULL, opt_ttyin_name,
Expand Down
8 changes: 6 additions & 2 deletions output.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,13 @@ less_printf(fmt, parg)
parg++;
break;
case 'c':
putchr(parg->p_char);
col++;
s = prchar(parg->p_char);
parg++;
while (*s != '\0')
{
putchr(*s++);
col++;
}
break;
case '%':
putchr('%');
Expand Down
6 changes: 4 additions & 2 deletions version.c
Original file line number Diff line number Diff line change
Expand Up @@ -935,9 +935,11 @@ v591 8/8/21 Use \kB for backspace key in lesskey; add more \k codes;
handle multibyte chars in prompt.
v592 8/24/21 Add --status-line option; limit use of /proc kludge; add --header.
v593 8/30/21 Add header columns, --no-number-headers.
v594 Let regex library handle caseless; add --redraw-on-quit option;
v594 10/1/21 Let regex library handle caseless; add --redraw-on-quit option;
add #version to lesskey.
v595 Add H color type; add += to lesskey var section.
v595 Add H color type; add += to lesskey var section;
add --search-options.
*/

char version[] = "595x";

0 comments on commit e8edfb8

Please sign in to comment.