# -*- shell-script -*-
# Joerg Arndt's  .jjzshrc
# ... online at http://www.jjj.de/
# your feedback is welcome  mailto: arndt (AT) jjj.de
# version: 2023-May-13 (13:46)

### ------- ZSH specific settings -------

# The following options are set if the shell is invoked as sh or ksh:
# NO_BAD_PATTERN, NO_BANG_HIST, NO_BG_NICE, NO_EQUALS, NO_FUNC­
# TION_ARGZERO, GLOB_SUBST, NO_GLOBAL_EXPORT, NO_HUP,
# INTERACTIVE_COMMENTS, KSH_ARRAYS, NO_MULTIOS, NO_NOMATCH, NO_NOTIFY,
# POSIX_BUILTINS, NO_PROMPT_PERCENT, RM_STAR_SILENT, SH_FILE_EXPANSION,
# SH_GLOB, SH_OPTION_LETTERS, SH_WORD_SPLIT.  Additionally the BSD_ECHO
# and IGNORE_BRACES options are set if zsh is invoked as sh.  Also, the
# KSH_OPTION_PRINT, LOCAL_OPTIONS, PROMPT_BANG, PROMPT_SUBST and
# SINGLE_LINE_ZLE options are set if zsh is invoked as ksh.

### from OpenSuse /etc/zshrc (start)
#
# Set/unset  shell options
#setopt   globdots nocorrect pushdtohome autolist nopromptcr
#setopt   nocorrectall autocd recexact longlistjobs
#setopt   histignoredups pushdsilent appendhistory histexpiredupsfirst
#setopt   autopushd pushdminus extendedglob rcquotes
#unsetopt bgnice autoparamslash hup

#autoload -U bashcompinit
autoload -U compinit
compinit

autoload -U zle
#echo $fpath
## /usr/share/zsh/site/scripts/ /usr/share/zsh/4.3.10/functions
#autoload -Uz history-search-end
#zle -N history-beginning-search-backward-end history-search-end
#zle -N history-beginning-search-forward-end history-search-end
#bindkey "^[[5~" history-beginning-search-backward-end
#bindkey "^[[6~" history-beginning-search-forward-end
#
### from OpenSuse /etc/zshrc (end)
#
## jj:
#zle -N history-search-backward
#zle -N history-search-forward
#bindkey "^[[5~" history-search-backward
#bindkey "^[[6~" history-search-forward
#autoload -U up-line-or-beginning-search
#autoload -U down-line-or-beginning-search
#zle -N up-line-or-beginning-search
#zle -N down-line-or-beginning-search
#bindkey "^[[5~" up-line-or-beginning-search
#bindkey "^[[6~" down-line-or-beginning-search


#WORDCHARS='*?_-.[]~=/&;!#$%^(){}<>'
WORDCHARS=''

SAVEHIST=$HISTSIZE
export HISTFILE=${HOME}/.zsh_history

function h { history -20; }
function hh { history -70; }
function hhh { history 0; }
alias help=run-help

## options: man zshoptions;  check with:  set -o
set -o shwordsplit
set -o globsubst
set -o histverify
set -o hist_reduce_blanks
## sharing history between terminals: (disabled)
setopt +o inc_append_history
setopt +o share_history
## end (sharing history between terminals).
set +o nomatch
set -o notify
set +o recexact
#set -o chaselinks
#set -o interactivecomments
#set -o nobadpattern
#set -o extendedhistory
#set -o magicequalsubst
### report if last command had nonzero exit status:
#set -o print_exit_value

setopt autocd
setopt noautoparamslash
setopt autopushd
setopt nobadpattern
setopt nobgnice
setopt chaselinks
setopt extendedglob
setopt extendedhistory
setopt globdots
setopt globsubst
setopt histexpiredupsfirst
setopt histignoredups
setopt histreduceblanks
setopt histverify
setopt nohup
setopt interactive
setopt interactivecomments
setopt login
setopt longlistjobs
setopt magicequalsubst
setopt monitor
setopt nonomatch
setopt printexitvalue
setopt nopromptcr
setopt pushdminus
setopt pushdsilent
setopt pushdtohome
setopt rcquotes
#setopt shinstdin # ??
setopt shwordsplit
setopt zle

##

## Quote the whole line in '$( )':
bindkey -s '^xq' '^e)^a $(^a'

## Quote the whole line in 'ee $( )': (ee == emacs)
bindkey -s '^xe' '^e)^a $(^aee '

## postpone but leave in history:
#bindkey -s '^x-' '^a#^e\n'
bindkey -s '\e#' '^a#^e\n'

## make alt-left and alt-right jump over words:
bindkey '\e[1;3D' backward-word
bindkey '\e[1;3C' forward-word
#bindkey '^[1;5D' backward-word
#bindkey '^[1;5C' forward-word
#bindkey '\e[D' backward-char # OK

## make del-key work for stupid systems:
bindkey '\e[3~' delete-char

#bindkey '\e[5~' history-beginning-search-backward-end
#bindkey '\e[6~' history-beginning-search-forward-end
bindkey '\e[F' end-of-line
bindkey '\e[H' beginning-of-line
bindkey '\e^?' backward-kill-word
bindkey '\ed' kill-word
#bindkey "^[[5C" emacs-forward-word
#bindkey "^[[5D" emacs-backward-word
#bindkey "^[[OC" emacs-forward-word
#bindkey "^[[OD" emacs-backward-word
##bindkey "^[[C" emacs-forward-word
##bindkey "^[[D" emacs-backward-word
#bindkey "^[OC" emacs-forward-word
#bindkey "^[OD" emacs-backward-word
##bindkey "^[[1;5D" emacs-backward-word
##bindkey "^[[1;5C" emacs-forward-word
#bindkey "^B" emacs-backward-word
#bindkey "^W" delete-word



## Meta-p, Meta-n for history search:
#bindkey -s '\ep' '\exhistory-beginning-search-backward-end\n'
#bindkey -s '\en' '\exhistory-beginning-search-forward-end\n'
#
## same with (ctrl-)pageup/down (does NOT work):
#bindkey  -s '\e[05~' '\exhistory-beginning-search-backward-end\n'
#bindkey  -s '\e[06~' '\exhistory-beginning-search-forward-end\n'


#if test "${HOST}" = 'racer'; then
##    echo "Debian...";
bindkey "^[[5~" history-beginning-search-backward
bindkey "^[[6~" history-beginning-search-forward
bindkey '\e[5~' history-beginning-search-backward
bindkey '\e[6~' history-beginning-search-forward
## Meta-p, Meta-n for history search:
bindkey -s '\ep' '\exhistory-beginning-search-backward\n'
bindkey -s '\en' '\exhistory-beginning-search-forward\n'
#else
##    echo "Suse...";
#fi



## logout:
set +o ignoreeof
#set -o ignoreeof
#bindkey -s '^xd' '^aexit;\n'


## quick base-converter:
bindkey -s '^x6' '^e ))^aecho $(( [#16] 10#'
bindkey -s '^x1' '^e ))^aecho $(( [#10] 16#'
bindkey -s '^x2' '^e ))^aecho $(( [#2] 10#'
bindkey -s '^x0' '^e ))^aecho $(( [#10] 10#'

bindkey -s '^xf' '^afind . -name \\*'

## show type of command:
#bindkey -s '^xp' '^atype -a \n\e[A^a\ed\ed'
## list 3 newest files and init cmdline with newest file:
#bindkey -s '^xl' '^a\\ls -tr1 | tail -3 \n\e[A^a\ed\ed $(\\ls -tr1 | tail -1^e)^a'

## Executed whenever a command has a non-zero exit status:
#TRAPZERR() { echo 'Oink!'; }


# ===== stuff for prompts: =====
#
unset PS1
export ME="$USER@$HOSTNAME";
if [ -n "$INSIDE_EMACS" ]; then # running emacs shell
#    echo "Running inside Emacs."
    PP1="%*";
    PP2="%n@%m:";
    PP3="~/";
    JOBSQ="%(1j. [%j].)";
#    unset JOBSQ
else
    if [ -n "$DISPLAY" ]; then # xterm: settings for white background
        PP1="%{${GREEN}%}%*%{${NORMAL}%}"
        PP2="%n@%m:"
        PP3="%{${BLUE}%}%~/%{${NORMAL}%}"
    else
        PP1="%{${BRIGHT}${GREEN}%}%*%{${NORMAL}%}"
        PP2="%n@%m:"
        PP3="%{${BRIGHT}${BLUE}%}%~/%{${NORMAL}%}"
    fi
# echo number of jobs iff it is >0:
#    JOBSQ='%(1j. [%j].)'
    JOBSQ="%(1j. [%{$MAGENTA%}%j%{$NORMAL%}].)"
fi

PS1="${PP1} ${PP2}${PP3}";
if  [ "$EUID" = "0" ]; then  # append '#' to prompt:
    PS1="$PS1$JOBSQ %{${BRIGHT}${RED}%}%%%{${NORMAL}%} "
else
    PS1="$PS1$JOBSQ %% "
fi
unset PP1 PP2 PP3
#export TPS1="$PS1"'\n'"%% " ; # does not work
export PS1;

unset PS2
if [ -n "$INSIDE_EMACS" ]; then # running emacs shell
    PS2=">>"
else
    PS2="%{$MAGENTA%}>>%{$NORMAL%}"
fi
export PS2;
# ===== end (prompts) =====


if [ -n "$INSIDE_EMACS" ]; then # running emacs shell
#    echo "Running inside Emacs."
    unset PROMPT_COMMAND;

else
if [ -n "$DISPLAY" ]; then # set PROMPT_COMMAND:
# chpwd:  Executed whenever the current working directory is changed.
    TAPP=" __ $(uname -mr) __ $(date +'     %A %d-%m-%Y')";
#    PROMPT_COMMAND="echo -ne '\033]0;'$ME:\`dirs +0\`/' '$TAPP'\007'";
#      precmd Executed before each prompt.
#    precmd() { echo -ne '\033]0;'"$ME:${PWD/${HOME}\//~/}"' '$TAPP'\007' };
    precmd()
    {
        if test -z "$XTITLE"; then
#            echo -ne '\033]0;'"$ME:${PWD/#${HOME}/~}"' '$TAPP'\007';
            echo -ne '\033]0;'"${PWD/#${HOME}/~} [$ME]"' '$TAPP'\007';
        else
            echo -ne '\033]0;'"$XTITLE"'\007';
        fi
    };

    function pp
    {
        XTITLE="$@";
    }

    # set xtitle to last part of dirname and full dirname:
    function  p
    {
        export XTITLE="${PWD##*/}  [$PWD]";
    }

    function  xtitle
    {
        if [ $# = 0 ]; then XTITLE=''; else XTITLE="$@"; fi
    }

    function xs  ## cd to dir and set title,  'xs .' just puts dirname into title
    {
        cd $1;  XTITLE="${PWD##*/}  [$PWD]";
    }

#else
#    unset PROMPT_COMMAND;
fi
fi
#export PROMPT_COMMAND;



## Disable bracketed-paste mode.
## Treat copy and pasted material as if typed in:
unset zle_bracketed_paste

## Attempt to use zsh highlighting:
## See (not) https://github.com/zsh-users/zsh-syntax-highlighting
## failed, thanks for not documenting your shit, guys.
#
#plugins=(zsh-syntax-highlighting)
#typeset -A ZSH_HIGHLIGHT_STYLES
#: ${ZSH_HIGHLIGHT_STYLES[unknown-token]:=fg=red,bold}
#SHF=/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
#test -f ${SHF}  &&  source ${SHF}
