diff --git a/cmdbuf.c b/cmdbuf.c index fc7667ef..0dda15f7 100644 --- a/cmdbuf.c +++ b/cmdbuf.c @@ -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"); @@ -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); } @@ -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"); @@ -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); diff --git a/decode.c b/decode.c index 914d1034..9fa89196 100644 --- a/decode.c +++ b/decode.c @@ -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); diff --git a/filename.c b/filename.c index c7e3bf08..1e51412d 100644 --- a/filename.c +++ b/filename.c @@ -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; @@ -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); } @@ -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 { @@ -1118,4 +1116,3 @@ last_component(name) } return (name); } - diff --git a/less.nro.VER b/less.nro.VER index fb579e22..24ea98f7 100644 --- a/less.nro.VER +++ b/less.nro.VER @@ -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, @@ -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. diff --git a/lesskey.nro.VER b/lesskey.nro.VER index 07362c6b..ae8d58e7 100644 --- a/lesskey.nro.VER +++ b/lesskey.nro.VER @@ -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