Skip to content

Commit

Permalink
Add += syntax to variable section in lesskey files.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed Oct 8, 2021
1 parent 695c2e2 commit f49c787
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 33 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

* Add #version conditional to lesskey.

* Add += syntax to variable section in lesskey files.

* Allow option name in -- command to end with '=' in addition to '\n'.

* Don't read or write history file in secure mode.
Expand Down
35 changes: 19 additions & 16 deletions lesskey.nro.VER
Original file line number Diff line number Diff line change
Expand Up @@ -337,23 +337,15 @@ Variables assigned in this way are visible only to
.IR less .
If a variable is specified in the system environment and also in a
lesskey file, the value in the lesskey file takes precedence.
Although the lesskey file can be used to override variables set in the
environment, the main purpose of assigning variables in the lesskey file
is simply to have all
.I less
configuration information stored in one file.
.
.SH EXAMPLE
The following input file sets the \-i option whenever
.I less
is run, and specifies the character set to be "latin1":
.sp
.nf
#env
LESS = \-i
LESSCHARSET = latin1
.fi
.sp
If the variable name is followed by += rather than =,
the string is appended to the variable's existing value.
This currently works only if any += lines immediately follow
the same variable's original definition (with an = line),
without any intervening definitions of other variables.
It can append only to a variable defined earlier in the file;
it cannot append to a variable in the system environment.
.
.SH CONDITIONAL CONFIGURATION
If a line begins with #version followed by a relational operator and a version number,
Expand Down Expand Up @@ -393,9 +385,20 @@ The #version feature is not supported in
.I less
and
.I lesskey
version 594 and earlier.
before version 594.
In those older versions, all #version lines are ignored.
.
.SH EXAMPLE
The following input file sets the \-i and \-S options when
.less
is run and, on version 595 and higher, adds a \-\-color option.
.sp
.nf
#env
LESS = \-i\ \-S
#version\ >=\ 595\ \ LESS\ +=\ \-\-color=Hkc
.fi
.
.SH "SEE ALSO"
.BR less (1)
.
Expand Down
50 changes: 34 additions & 16 deletions lesskey_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,13 @@ add_cmd_char(c, tables)
xbuf_add(&tables->currtable->buf, c);
}

static void
erase_cmd_char(tables)
struct lesskey_tables *tables;
{
xbuf_pop(&tables->currtable->buf);
}

/*
* Add a string to the output command table.
*/
Expand Down Expand Up @@ -567,26 +574,37 @@ parse_varline(line, tables)
{
char *s;
char *p = line;
char *eq;

do
eq = strchr(line, '=');
if (eq != NULL && eq > line && eq[-1] == '+')
{
s = tstr(&p, 0);
add_cmd_str(s, tables);
} while (*p != '\0' && !issp(*p) && *p != '=');
/*
* Terminate the variable name with a null byte.
*/
add_cmd_char('\0', tables);

p = skipsp(p);
if (*p++ != '=')
/*
* Rather ugly way of handling a += line.
* {{ Note that we ignore the variable name and
* just append to the previously defined variable. }}
*/
erase_cmd_char(tables); /* backspace over the final null */
p = eq+1;
} else
{
parse_error("missing = in variable definition", "");
return;
do
{
s = tstr(&p, 0);
add_cmd_str(s, tables);
} while (*p != '\0' && !issp(*p) && *p != '=');
/*
* Terminate the variable name with a null byte.
*/
add_cmd_char('\0', tables);
p = skipsp(p);
if (*p++ != '=')
{
parse_error("missing = in variable definition", "");
return;
}
add_cmd_char(EV_OK|A_EXTRA, tables);
}

add_cmd_char(EV_OK|A_EXTRA, tables);

p = skipsp(p);
while (*p != '\0')
{
Expand Down
2 changes: 1 addition & 1 deletion version.c
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ v592 8/24/21 Add --status-line option; limit use of /proc kludge; add --heade
v593 8/30/21 Add header columns, --no-number-headers.
v594 Let regex library handle caseless; add --redraw-on-quit option;
add #version to lesskey.
v595
v595 Add H color type; add += to lesskey var section.
*/

char version[] = "595x";
9 changes: 9 additions & 0 deletions xbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,12 @@ xbuf_add(xbuf, ch)
}
xbuf->data[xbuf->end++] = ch;
}

public int
xbuf_pop(buf)
struct xbuffer *buf;
{
if (buf->end == 0)
return -1;
return buf->data[--(buf->end)];
}
1 change: 1 addition & 0 deletions xbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ struct xbuffer
void xbuf_init(struct xbuffer *xbuf);
void xbuf_reset(struct xbuffer *xbuf);
void xbuf_add(struct xbuffer *xbuf, int ch);
int xbuf_pop(struct xbuffer *xbuf);

#endif

0 comments on commit f49c787

Please sign in to comment.