Skip to content

Commit

Permalink
Change algorithm to find lesskey and history files:
Browse files Browse the repository at this point in the history
Previously, less would look for lesskey in $HOME/.lesskey.
Now it looks in $XDG_CONFIG_DIR/lesskey first, and if not found then $HOME/.lesskey.

Previously, less would look for history in $HOME/.lesshst.
Now it looks in $XDG_CONFIG_DIR/lesshst first, and if not found then $HOME/.lesshst.

When writing the history file, it uses $XDG_CONFIG_DIR/lesshst if it exists,
otherwise $HOME/.lesshst if it exists, otherwise $XDG_CONFIG_DIR/lesshst.
  • Loading branch information
gwsw committed Apr 21, 2021
1 parent df6e714 commit 9855d50
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 43 deletions.
31 changes: 19 additions & 12 deletions cmdbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1394,11 +1394,12 @@ mlist_size(ml)
* Get the name of the history file.
*/
static char *
histfile_name(VOID_PARAM)
histfile_name(must_exist)
int must_exist;
{
char *home;
char *xdg;
char *name;
int len;

/* See if filename is explicitly specified by $LESSHISTFILE. */
name = lgetenv("LESSHISTFILE");
Expand All @@ -1414,19 +1415,25 @@ histfile_name(VOID_PARAM)
if (strcmp(LESSHISTFILE, "") == 0 || strcmp(LESSHISTFILE, "-") == 0)
return (NULL);

/* Otherwise, file is in $HOME. */
/* Try in $XDG_DATA_HOME first, then in $HOME. */
xdg = lgetenv("XDG_DATA_HOME");
home = lgetenv("HOME");
if (isnullenv(home))
{
#if OS2
if (isnullenv(home))
home = lgetenv("INIT");
if (isnullenv(home))
#endif
return (NULL);
name = NULL;
if (!must_exist)
{
/* If we're writing the file and the file already exists, use it. */
name = dirfile(xdg, LESSHISTFILE+1, 1);
if (name == NULL)
name = dirfile(home, LESSHISTFILE, 1);
}
len = (int) (strlen(home) + strlen(LESSHISTFILE) + 2);
name = (char *) ecalloc(len, sizeof(char));
SNPRINTF2(name, len, "%s/%s", home, LESSHISTFILE);
if (name == NULL)
name = dirfile(xdg, LESSHISTFILE+1, must_exist);
if (name == NULL)
name = dirfile(home, LESSHISTFILE, must_exist);
return (name);
}

Expand All @@ -1447,7 +1454,7 @@ read_cmdhist2(action, uparam, skip_search, skip_shell)
char *p;
int *skip = NULL;

filename = histfile_name();
filename = histfile_name(1);
if (filename == NULL)
return;
f = fopen(filename, "r");
Expand Down Expand Up @@ -1693,7 +1700,7 @@ save_cmdhist(VOID_PARAM)

if (!histfile_modified())
return;
histname = histfile_name();
histname = histfile_name(0);
if (histname == NULL)
return;
tempname = make_tempname(histname);
Expand Down
12 changes: 9 additions & 3 deletions decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -932,10 +932,16 @@ add_hometable(call_lesskey, envname, def_filename, sysvar)

if (envname != NULL && (filename = lgetenv(envname)) != NULL)
filename = save(filename);
else if (sysvar)
else if (sysvar) /* def_filename is full path */
filename = save(def_filename);
else
filename = homefile(def_filename);
else /* def_filename is just basename */
{
char *xdg = lgetenv("XDG_CONFIG_HOME");
if (!isnullenv(xdg))
filename = dirfile(xdg, def_filename+1, 1);
if (filename == NULL)
filename = homefile(def_filename);
}
if (filename == NULL)
return -1;
r = (*call_lesskey)(filename, sysvar);
Expand Down
45 changes: 21 additions & 24 deletions filename.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,11 @@ shell_quote(s)
* Return a pathname that points to a specified file in a specified directory.
* Return NULL if the file does not exist in the directory.
*/
static char *
dirfile(dirname, filename)
public char *
dirfile(dirname, filename, must_exist)
char *dirname;
char *filename;
int must_exist;
{
char *pathname;
int len;
Expand All @@ -228,17 +229,20 @@ dirfile(dirname, filename)
if (pathname == NULL)
return (NULL);
SNPRINTF3(pathname, len, "%s%s%s", dirname, PATHNAME_SEP, filename);
/*
* Make sure the file exists.
*/
f = open(pathname, OPEN_READ);
if (f < 0)
{
free(pathname);
pathname = NULL;
} else
if (must_exist)
{
close(f);
/*
* Make sure the file exists.
*/
f = open(pathname, OPEN_READ);
if (f < 0)
{
free(pathname);
pathname = NULL;
} else
{
close(f);
}
}
return (pathname);
}
Expand All @@ -252,24 +256,18 @@ homefile(filename)
{
char *pathname;

/*
* Try $HOME/filename.
*/
pathname = dirfile(lgetenv("HOME"), filename);
/* Try $HOME/filename. */
pathname = dirfile(lgetenv("HOME"), filename, 1);
if (pathname != NULL)
return (pathname);
#if OS2
/*
* Try $INIT/filename.
*/
pathname = dirfile(lgetenv("INIT"), filename);
/* Try $INIT/filename. */
pathname = dirfile(lgetenv("INIT"), filename, 1);
if (pathname != NULL)
return (pathname);
#endif
#if MSDOS_COMPILER || OS2
/*
* Look for the file anywhere on search path.
*/
/* Look for the file anywhere on search path. */
pathname = (char *) ecalloc(_MAX_PATH, sizeof(char));
#if MSDOS_COMPILER==DJGPPC
{
Expand Down Expand Up @@ -1118,4 +1116,3 @@ last_component(name)
}
return (name);
}

7 changes: 4 additions & 3 deletions less.nro.VER
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,7 @@ Otherwise,
looks in a standard place for the lesskey source file:
On Unix systems,
.I less
looks for a lesskey file called "$HOME/.lesskey".
looks for a lesskey file called "$XDG_CONFIG_HOME/lesskey" or "$HOME/.lesskey".
On MS-DOS and Windows systems,
.I less
looks for a lesskey file called "$HOME/_lesskey", and if it is not found there,
Expand Down Expand Up @@ -1919,8 +1919,9 @@ Name of the history file used to remember search commands and
shell commands between invocations of
.IR less .
If set to "\-" or "/dev/null", a history file is not used.
The default is "$HOME/.lesshst" on Unix systems, "$HOME/_lesshst" on
DOS and Windows systems, or "$HOME/lesshst.ini" or "$INIT/lesshst.ini"
The default is "$XDG_DATA_HOME/lesshst" or "$HOME/.lesshst" on Unix systems,
"$HOME/_lesshst" on DOS and Windows systems,
or "$HOME/lesshst.ini" or "$INIT/lesshst.ini"
on OS/2 systems.
.IP LESSHISTSIZE
The maximum number of commands to save in the history file.
Expand Down
3 changes: 2 additions & 1 deletion lesskey.nro.VER
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ source file into a format understood by
This compilation step is no longer required and the
.I lesskey
program is therefore deprecated although the file format remains supported by
.IR less .
.I less
itself.
.PP
.SH FILE FORMAT
The input file consists of one or more
Expand Down

0 comments on commit 9855d50

Please sign in to comment.