diff --git a/modules/programs/zsh/history.nix b/modules/programs/zsh/history.nix index 3b766d62b..acdc6531f 100644 --- a/modules/programs/zsh/history.nix +++ b/modules/programs/zsh/history.nix @@ -199,16 +199,46 @@ in mkdir -p "$(dirname "$HISTFILE")" setopt HIST_FCNTL_LOCK - ${if cfg.history.append then "setopt" else "unsetopt"} APPEND_HISTORY - ${if cfg.history.ignoreDups then "setopt" else "unsetopt"} HIST_IGNORE_DUPS - ${if cfg.history.ignoreAllDups then "setopt" else "unsetopt"} HIST_IGNORE_ALL_DUPS - ${if cfg.history.saveNoDups then "setopt" else "unsetopt"} HIST_SAVE_NO_DUPS - ${if cfg.history.findNoDups then "setopt" else "unsetopt"} HIST_FIND_NO_DUPS - ${if cfg.history.ignoreSpace then "setopt" else "unsetopt"} HIST_IGNORE_SPACE - ${if cfg.history.expireDuplicatesFirst then "setopt" else "unsetopt"} HIST_EXPIRE_DUPS_FIRST - ${if cfg.history.share then "setopt" else "unsetopt"} SHARE_HISTORY - ${if cfg.history.extended then "setopt" else "unsetopt"} EXTENDED_HISTORY - ${if cfg.autocd != null then "${if cfg.autocd then "setopt" else "unsetopt"} autocd" else ""} + + ${ + let + historyOptions = { + APPEND_HISTORY = cfg.history.append; + HIST_IGNORE_DUPS = cfg.history.ignoreDups; + HIST_IGNORE_ALL_DUPS = cfg.history.ignoreAllDups; + HIST_SAVE_NO_DUPS = cfg.history.saveNoDups; + HIST_FIND_NO_DUPS = cfg.history.findNoDups; + HIST_IGNORE_SPACE = cfg.history.ignoreSpace; + HIST_EXPIRE_DUPS_FIRST = cfg.history.expireDuplicatesFirst; + SHARE_HISTORY = cfg.history.share; + EXTENDED_HISTORY = cfg.history.extended; + } + // lib.optionalAttrs (cfg.autocd != null) { + inherit (cfg) autocd; + }; + + enabledOpts = lib.filterAttrs (_: enabled: enabled) historyOptions; + disabledOpts = lib.filterAttrs (_: enabled: !enabled) historyOptions; + in + lib.concatStringsSep "\n\n" ( + lib.filter (s: s != "") [ + (lib.optionalString (enabledOpts != { }) '' + # Enabled history options + ${lib.hm.zsh.define "enabled_opts" (lib.mapAttrsToList (name: _: name) enabledOpts)} + for opt in "''${enabled_opts[@]}"; do + setopt "$opt" + done + unset opt enabled_opts'') + (lib.optionalString (disabledOpts != { }) '' + # Disabled history options + ${lib.hm.zsh.define "disabled_opts" (lib.mapAttrsToList (name: _: name) disabledOpts)} + for opt in "''${disabled_opts[@]}"; do + unsetopt "$opt" + done + unset opt disabled_opts'') + ] + ) + } '') (lib.mkIf (cfg.historySubstringSearch.enable or false) ( @@ -217,12 +247,28 @@ in # https://github.com/zsh-users/zsh-history-substring-search#usage '' source ${pkgs.zsh-history-substring-search}/share/zsh-history-substring-search/zsh-history-substring-search.zsh - ${lib.concatMapStringsSep "\n" (upKey: ''bindkey "${upKey}" history-substring-search-up'') ( - lib.toList cfg.historySubstringSearch.searchUpKey - )} - ${lib.concatMapStringsSep "\n" (downKey: ''bindkey "${downKey}" history-substring-search-down'') ( - lib.toList cfg.historySubstringSearch.searchDownKey - )} + + ${ + let + upKeys = lib.toList cfg.historySubstringSearch.searchUpKey; + downKeys = lib.toList cfg.historySubstringSearch.searchDownKey; + in + '' + # Bind search up keys + ${lib.hm.zsh.define "search_up_keys" upKeys} + for key in "''${search_up_keys[@]}"; do + bindkey "$key" history-substring-search-up + done + unset key search_up_keys + + # Bind search down keys + ${lib.hm.zsh.define "search_down_keys" downKeys} + for key in "''${search_down_keys[@]}"; do + bindkey "$key" history-substring-search-down + done + unset key search_down_keys + '' + } '' )) ]; diff --git a/tests/modules/programs/fabric-ai/zshrc b/tests/modules/programs/fabric-ai/zshrc index 0817c5028..82c9888a7 100644 --- a/tests/modules/programs/fabric-ai/zshrc +++ b/tests/modules/programs/fabric-ai/zshrc @@ -15,16 +15,25 @@ HISTFILE="/home/hm-user/.zsh_history" mkdir -p "$(dirname "$HISTFILE")" setopt HIST_FCNTL_LOCK -unsetopt APPEND_HISTORY -setopt HIST_IGNORE_DUPS -unsetopt HIST_IGNORE_ALL_DUPS -unsetopt HIST_SAVE_NO_DUPS -unsetopt HIST_FIND_NO_DUPS -setopt HIST_IGNORE_SPACE -unsetopt HIST_EXPIRE_DUPS_FIRST -setopt SHARE_HISTORY -unsetopt EXTENDED_HISTORY +# Enabled history options +enabled_opts=( + HIST_IGNORE_DUPS HIST_IGNORE_SPACE SHARE_HISTORY +) +for opt in "${enabled_opts[@]}"; do + setopt "$opt" +done +unset opt enabled_opts + +# Disabled history options +disabled_opts=( + APPEND_HISTORY EXTENDED_HISTORY HIST_EXPIRE_DUPS_FIRST HIST_FIND_NO_DUPS + HIST_IGNORE_ALL_DUPS HIST_SAVE_NO_DUPS +) +for opt in "${disabled_opts[@]}"; do + unsetopt "$opt" +done +unset opt disabled_opts diff --git a/tests/modules/programs/zsh/aliases.nix b/tests/modules/programs/zsh/aliases.nix index 906bb62d0..ee91806a2 100644 --- a/tests/modules/programs/zsh/aliases.nix +++ b/tests/modules/programs/zsh/aliases.nix @@ -32,16 +32,25 @@ mkdir -p "$(dirname "$HISTFILE")" setopt HIST_FCNTL_LOCK - unsetopt APPEND_HISTORY - setopt HIST_IGNORE_DUPS - unsetopt HIST_IGNORE_ALL_DUPS - unsetopt HIST_SAVE_NO_DUPS - unsetopt HIST_FIND_NO_DUPS - setopt HIST_IGNORE_SPACE - unsetopt HIST_EXPIRE_DUPS_FIRST - setopt SHARE_HISTORY - unsetopt EXTENDED_HISTORY + # Enabled history options + enabled_opts=( + HIST_IGNORE_DUPS HIST_IGNORE_SPACE SHARE_HISTORY + ) + for opt in "''${enabled_opts[@]}"; do + setopt "$opt" + done + unset opt enabled_opts + + # Disabled history options + disabled_opts=( + APPEND_HISTORY EXTENDED_HISTORY HIST_EXPIRE_DUPS_FIRST HIST_FIND_NO_DUPS + HIST_IGNORE_ALL_DUPS HIST_SAVE_NO_DUPS + ) + for opt in "''${disabled_opts[@]}"; do + unsetopt "$opt" + done + unset opt disabled_opts alias -- test1=alias alias -- test2=alias2 diff --git a/tests/modules/programs/zsh/zshrc-content-priorities.nix b/tests/modules/programs/zsh/zshrc-content-priorities.nix index 1d80db940..fa149291e 100644 --- a/tests/modules/programs/zsh/zshrc-content-priorities.nix +++ b/tests/modules/programs/zsh/zshrc-content-priorities.nix @@ -50,16 +50,25 @@ mkdir -p "$(dirname "$HISTFILE")" setopt HIST_FCNTL_LOCK - unsetopt APPEND_HISTORY - setopt HIST_IGNORE_DUPS - unsetopt HIST_IGNORE_ALL_DUPS - unsetopt HIST_SAVE_NO_DUPS - unsetopt HIST_FIND_NO_DUPS - setopt HIST_IGNORE_SPACE - unsetopt HIST_EXPIRE_DUPS_FIRST - setopt SHARE_HISTORY - unsetopt EXTENDED_HISTORY + # Enabled history options + enabled_opts=( + HIST_IGNORE_DUPS HIST_IGNORE_SPACE SHARE_HISTORY + ) + for opt in "''${enabled_opts[@]}"; do + setopt "$opt" + done + unset opt enabled_opts + + # Disabled history options + disabled_opts=( + APPEND_HISTORY EXTENDED_HISTORY HIST_EXPIRE_DUPS_FIRST HIST_FIND_NO_DUPS + HIST_IGNORE_ALL_DUPS HIST_SAVE_NO_DUPS + ) + for opt in "''${disabled_opts[@]}"; do + unsetopt "$opt" + done + unset opt disabled_opts # Default priority echo "Default priority content"