# Source manjaro-zsh-configuration
if [[ -e /usr/share/zsh/manjaro-zsh-config ]]; then
  source /usr/share/zsh/manjaro-zsh-config
fi

# 通用顏色文字輸出（要在最前面執行，不然會失效）
[[ -s "/etc/grc.zsh" ]] && source /etc/grc.zsh

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi


### Added by Zinit's installer
if [[ ! -f $HOME/.local/share/zinit/zinit.git/zinit.zsh ]]; then
    print -P "%F{33} %F{220}Installing %F{33}ZDHARMA-CONTINUUM%F{220} Initiative Plugin Manager (%F{33}zdharma-continuum/zinit%F{220})…%f"
    command mkdir -p "$HOME/.local/share/zinit" && command chmod g-rwX "$HOME/.local/share/zinit"
    command git clone https://github.com/zdharma-continuum/zinit "$HOME/.local/share/zinit/zinit.git" && \
        print -P "%F{33} %F{34}Installation successful.%f%b" || \
        print -P "%F{160} The clone has failed.%f%b"
fi

source "$HOME/.local/share/zinit/zinit.git/zinit.zsh"
autoload -Uz _zinit
(( ${+_comps} )) && _comps[zinit]=_zinit

# Load a few important annexes, without Turbo
# (this is currently required for annexes)
zinit light-mode for \
    zdharma-continuum/zinit-annex-as-monitor \
    zdharma-continuum/zinit-annex-bin-gem-node \
    zdharma-continuum/zinit-annex-patch-dl \
    zdharma-continuum/zinit-annex-rust

### End of Zinit's installer chunk

# zsh 套件四天王
zinit light zsh-users/zsh-completions
zinit light zsh-users/zsh-autosuggestions
zinit light zsh-users/zsh-history-substring-search
zinit light zdharma-continuum/fast-syntax-highlighting

# Oh My Zsh 功能
zinit snippet OMZ::lib/clipboard.zsh
zinit snippet OMZ::lib/history.zsh
zinit snippet OMZP::vscode
zinit snippet OMZ::lib/key-bindings.zsh
zinit snippet OMZ::lib/completion.zsh
zinit snippet OMZP::sudo
zinit snippet OMZ::lib/theme-and-appearance.zsh
zinit snippet OMZP::laravel

#zinit wait="1" lucid for \
#    OMZL::clipboard.zsh \
#    OMZL::history.zsh \
#    OMZL::key-bindings.zsh \
#    OMZL::completion.zsh \
#    OMZP::sudo/sudo.plugin.zsh
#    # OMZL::git.zsh \
#    # OMZP::systemd/systemd.plugin.zsh \
#    # OMZP::git/git.plugin.zsh \
#    # OMZP::mvn/mvn.plugin.zsh \
#    # OMZP::colored-man-pages/colored-man-pages.plugin.zsh

# Powerlevel10k
zinit ice depth=1; zinit light romkatv/powerlevel10k

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

# 開啟自動補全
setopt AUTO_LIST
setopt AUTO_MENU
setopt MENU_COMPLETE

autoload -U compinit
compinit

# Completion caching
#zstyle ':completion::complete:*' use-cache on
#zstyle ':completion::complete:*' cache-path .zcache
#zstyle ':completion:*:cd:*' ignore-parents parent pwd
#
##Completion Options
#zstyle ':completion:*:match:*' original only
#zstyle ':completion::prefix-1:*' completer _complete
#zstyle ':completion:predict:*' completer _complete
#zstyle ':completion:incremental:*' completer _complete _correct
#zstyle ':completion:*' completer _complete _prefix _correct _prefix _match _approximate
#
## Path Expansion
#zstyle ':completion:*' expand 'yes'
#zstyle ':completion:*' squeeze-shlashes 'yes'
#zstyle ':completion::complete:*' '\\'
#
#zstyle ':completion:*:*:*:default' menu yes select
#zstyle ':completion:*:*:default' force-list always

# Binary release in archive, from GitHub-releases page.
# After automatic unpacking it provides program "fzf".
zi ice from"gh-r" as"program"
zi light junegunn/fzf
# zsh-fzf-history-search
zinit ice lucid wait'0'
zinit light joshskidmore/zsh-fzf-history-search
#bindkey '^T' fzf-completion
#zinit light Aloxaf/fzf-tab
export FZF_DEFAULT_OPTS="--height 40% --layout=reverse --border --preview 'bat --color=always {}' --preview-window '~3'"

# 給自動補齊上色
# Standard style used by default for 'list-colors'
LS_COLORS=${LS_COLORS:-'di=34:ln=35:so=32:pi=33:ex=31:bd=36;01:cd=33;01:su=31;40;07:sg=36;40;07:tw=32;40;07:ow=33;40;07:'}

# apply ls colors
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*:default' list-prompt '%S%M matches%s'

# pretty cd [tab] stuffs
zstyle ':completion:*:directory-stack' list-colors '=(#b) #([0-9]#)*( *)==95=38;5;12'

# highlight case in select
zstyle ':completion:*' menu select

## Alias section
alias cp="cp -i"                          # Confirm before overwriting something
alias df='df -h'                          # Human-readable sizes
alias free='free -m'                      # Show sizes in MB

# Zsh Shift Select Mode
zinit light jirutka/zsh-shift-select
# Ctrl+Y 複製：複製到 zsh 與系統剪貼簿
function my_copy_region() {
  zle copy-region-as-kill
  print -rn -- "$CUTBUFFER" | clipcopy
}
zle -N my_copy_region
bindkey '^Y' my_copy_region
# Ctrl+P 貼上（zsh yank）
# function my_paste() {
#   zle yank
# }
# zle -N my_paste
# bindkey '^P' my_paste
# 貼上（clipboard-paste）
function my_paste_from_clipboard() {
  LBUFFER+=$(clippaste)
}
zle -N my_paste_from_clipboard
# bindkey '^O' my_paste_from_clipboard  # 例如 Ctrl+O
bindkey '^P' my_paste_from_clipboard  # 例如 Ctrl+P

# SSH 自動補齊
zinit light sunlei/zsh-ssh

# Ollama 自動補齊
zinit light ocodo/ollama_zsh_completion
# zinit snippet https://gist.github.com/obeone/9313811fd61a7cbb843e0001a4434c58/raw/5a6a44efc6a07b6f937dbc596d9d7385b297dda8/_ollama.zsh

# Kollzsh zsh命令行直接輸入模糊語意用途，按下ctrl+o以後，會連線到API伺服器取出正確指令下法
zinit light chyuaner/kollzsh
KOLLZSH_MODEL="qwen2.5-coder:7b"
KOLLZSH_HOTKEY="^o"
KOLLZSH_COMMAND_COUNT=5
KOLLZSH_URL={{ .ollamaUrl | quote }}
KOLLZSH_APIKEY={{ .ollamaKey | quote }}
KOLLZSH_KEEP_ALIVE="1h"
KOLLZSH_PYTHON3="python3"

# overwrite ls
if  command -v colorls &> /dev/null; then
    alias ls='colorls'
    compdef colorls='ls'
    source $(dirname $(gem which colorls))/tab_complete.sh
elif  command -v eza &> /dev/null; then
    zinit ice as"completion"
    zinit snippet https://github.com/eza-community/eza/blob/main/completions/zsh/_eza
    alias ls='eza --color=always --icons=always'
    #zinit light 'eza-community/eza'
elif  command -v exa &> /dev/null; then
    compdef exa='ls'
    alias ls='exa --color=always --icons'
fi

# Floorp 支援 Firefox參數補齊
compdef floorp='firefox'

# 開啟檔案總管快速鍵 alt+e
if  command -v xdg-open &> /dev/null; then
    bindkey -s '^[e' 'xdg-open "$PWD"  2> /dev/null\n'
elif [ "$(uname -s)" = 'Darwin' ]; then
    bindkey -s '^[e' 'open "$PWD"  2> /dev/null\n'
fi
# bindkey -s '^E' 'gnome-terminal --working-directory="$PWD" -- zsh\n'

{{ if eq .chezmoi.os "darwin" -}}
# iterm2
test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh"
{{ end -}}

#先加入環境變數，因為內含chezmoi，不然載入到zsh-chezmoi時會找不到bin
export PATH="$PATH:$HOME/bin"
zinit light mass8326/zsh-chezmoi

{{ if .signingkey }}
export GPG_TTY=$TTY{{ end }}
gpgconf --launch gpg-agent

# 自動補上 noglob 並禁用 Zsh 的 globbing 解析
function git_pushmulti() {
    # 手動處理方括號或括號內容，並進行推送
    local remotes="$1"
    local branches="$2"
    remotes="${remotes//\[/\"[\"}"
    remotes="${remotes//\]/\"]\"}"
    branches="${branches//\[/\"[\"}"
    branches="${branches//\]/\"]\"}"

    # 執行 git pushmulti
    noglob git pushmulti "$remotes" "$branches"
}

{{ template "common.sh.tmpl" . }}

