@@ -106,14 +106,8 @@ fn get_delta_less_hist_file() -> std::io::Result<PathBuf> {
106106 dir. place_data_file ( "lesshst" )
107107}
108108
109- // LESSHISTFILE
110- // Name of the history file used to remember search commands
111- // and shell commands between invocations of less. If set to
112- // "-" or "/dev/null", a history file is not used. The
113- // default is "$HOME/.lesshst" on Unix systems,
114- // "$HOME/_lesshst" on DOS and Windows systems, or
115- // "$HOME/lesshst.ini" or "$INIT/lesshst.ini" on OS/2
116- // systems.
109+ // Get path of the less history file. See `man less` for more details.
110+ // On Unix, check all possible locations and pick the newest file.
117111fn get_less_hist_file ( ) -> Option < PathBuf > {
118112 if let Some ( home_dir) = dirs:: home_dir ( ) {
119113 match std:: env:: var ( "LESSHISTFILE" ) . as_deref ( ) {
@@ -122,18 +116,34 @@ fn get_less_hist_file() -> Option<PathBuf> {
122116 None
123117 }
124118 Ok ( path) => {
125- // The user has specified a custom histfile
119+ // The user has specified a custom histfile.
126120 Some ( PathBuf :: from ( path) )
127121 }
128122 Err ( _) => {
129123 // The user is using the default less histfile location.
130- let mut hist_file = home_dir;
131- hist_file. push ( if cfg ! ( windows) {
132- "_lesshst"
124+ if cfg ! ( windows) {
125+ let mut hist_file = home_dir;
126+ hist_file. push ( "_lesshst" ) ;
127+ Some ( hist_file)
133128 } else {
134- ".lesshst"
135- } ) ;
136- Some ( hist_file)
129+ // According to the less 643 manual:
130+ // "$XDG_STATE_HOME/lesshst" or "$HOME/.local/state/lesshst" or
131+ // "$XDG_DATA_HOME/lesshst" or "$HOME/.lesshst".
132+ let xdg_dirs = xdg:: BaseDirectories :: new ( ) . ok ( ) ?;
133+ [
134+ xdg_dirs. get_state_home ( ) . join ( "lesshst" ) ,
135+ xdg_dirs. get_data_home ( ) . join ( "lesshst" ) ,
136+ home_dir. join ( ".lesshst" ) ,
137+ ]
138+ . iter ( )
139+ . filter ( |path| path. exists ( ) )
140+ . max_by_key ( |path| {
141+ std:: fs:: metadata ( path)
142+ . and_then ( |m| m. modified ( ) )
143+ . unwrap_or ( std:: time:: UNIX_EPOCH )
144+ } )
145+ . cloned ( )
146+ }
137147 }
138148 }
139149 } else {
@@ -147,6 +157,14 @@ mod tests {
147157
148158 use crate :: tests:: integration_test_utils;
149159
160+ #[ test]
161+ #[ ignore]
162+ // manually verify: cargo test -- test_get_less_hist_file --ignored --nocapture
163+ fn test_get_less_hist_file ( ) {
164+ let hist_file = super :: get_less_hist_file ( ) ;
165+ dbg ! ( hist_file) ;
166+ }
167+
150168 #[ test]
151169 fn test_navigate_with_overridden_key_in_main_section ( ) {
152170 let git_config_contents = b"
0 commit comments