Commit 3d71eab5 authored by Pierre-Elliott Bécue's avatar Pierre-Elliott Bécue

Ajout du zshrc, même s'il manque encore de commentaires etc.

parent 1f8b0889
Put your completion files here.
#!/bin/zsh
# zsh profiling {{{
# just execute 'ZSH_PROFILE_RC=1 zsh' and run 'zprof' to get the details
if [[ $ZSH_PROFILE_RC -gt 0 ]] ; then
zmodload zsh/zprof
fi
# }}}
# Zshrc function definitions
# {{{ check for version/system
# check for versions (compatibility reasons)
is439(){
[[ $ZSH_VERSION == 4.3.<9->* || $ZSH_VERSION == 4.<4->* || $ZSH_VERSION == <5->* ]] && return 0
return 1
}
#f1# are we running within an utf environment?
isutfenv() {
case "$LANG $CHARSET $LANGUAGE" in
*utf*) return 0 ;;
*UTF*) return 0 ;;
*) return 1 ;;
esac
}
# check for user, if not running as root set $SUDO to sudo
(( EUID != 0 )) && SUDO='sudo' || SUDO=''
# autoload wrapper - use this one instead of autoload directly
# We need to define this function as early as this, because autoloading
# 'is-at-least()' needs it.
function zrcautoload() {
emulate -L zsh
setopt extended_glob
local fdir ffile
local -i ffound
ffile=$1
(( found = 0 ))
for fdir in ${fpath} ; do
[[ -e ${fdir}/${ffile} ]] && (( ffound = 1 ))
done
(( ffound == 0 )) && return 1
autoload -U ${ffile} || return 1
return 0
}
# Load is-at-least() for more precise version checks
# Note that this test will *always* fail, if the is-at-least
# function could not be marked for autoloading.
zrcautoload is-at-least || is-at-least() { return 1 }
# }}}
# {{{ set some important options (as early as possible)
# Please update these tags, if you change the umask settings below.
#o# r_umask 002
#o# r_umaskstr rwxrwxr-x
#o# umask 022
#o# umaskstr rwxr-xr-x
umask 022
setopt append_history # append history list to the history file (important for multiple parallel zsh sessions!)
setopt SHARE_HISTORY # import new commands from the history file also in other zsh-session
setopt extended_history # save each command's beginning timestamp and the duration to the history file
#setopt histignorealldups # If a new command line being added to the history
# list duplicates an older one, the older command is removed from the list
setopt histignorespace # remove command lines from the history list when
# the first character on the line is a space
setopt auto_cd # if a command is issued that can't be executed as a normal command,
# and the command is the name of a directory, perform the cd command to that directory
setopt extended_glob # in order to use #, ~ and ^ for filename generation
# grep word *~(*.gz|*.bz|*.bz2|*.zip|*.Z) ->
# -> searches for word not in compressed files
# don't forget to quote '^', '~' and '#'!
setopt longlistjobs # display PID when suspending processes as well
setopt notify # report the status of backgrounds jobs immediately
setopt hash_list_all # Whenever a command completion is attempted, make sure \
# the entire command path is hashed first.
setopt completeinword # not just at the end
setopt nohup # and don't kill them, either
setopt auto_pushd # make cd push the old directory onto the directory stack.
setopt nonomatch # try to avoid the 'zsh: no matches found...'
setopt nobeep # avoid "beep"ing
setopt pushd_ignore_dups # don't push the same dir twice.
setopt noglobdots # * shouldn't match dotfiles. ever.
setopt noshwordsplit # use zsh style word splitting
setopt unset # don't error out when unset parameters are used
# }}}
#!/bin/zsh
# utility functions {{{
# this function checks if a command exists and returns either true
# or false. This avoids using 'which' and 'whence', which will
# avoid problems with aliases for which on certain weird systems. :-)
# Usage: check_com [-c|-g] word
# -c only checks for external commands
# -g does the usual tests and also checks for global aliases
check_com() {
emulate -L zsh
local -i comonly gatoo
if [[ $1 == '-c' ]] ; then
(( comonly = 1 ))
shift
elif [[ $1 == '-g' ]] ; then
(( gatoo = 1 ))
else
(( comonly = 0 ))
(( gatoo = 0 ))
fi
if (( ${#argv} != 1 )) ; then
printf 'usage: check_com [-cg] <command>\n' >&2
return 1
fi
if (( comonly > 0 )) ; then
[[ -n ${commands[$1]} ]] && return 0
return 1
fi
if [[ -n ${commands[$1]} ]] \
|| [[ -n ${functions[$1]} ]] \
|| [[ -n ${aliases[$1]} ]] \
|| [[ -n ${reswords[(r)$1]} ]] ; then
return 0
fi
if (( gatoo > 0 )) && [[ -n ${galiases[$1]} ]] ; then
return 0
fi
return 1
}
# creates an alias and precedes the command with
# sudo if $EUID is not zero.
salias() {
emulate -L zsh
local only=0 ; local multi=0
while [[ $1 == -* ]] ; do
case $1 in
(-o) only=1 ;;
(-a) multi=1 ;;
(--) shift ; break ;;
(-h)
printf 'usage: salias [-h|-o|-a] <alias-expression>\n'
printf ' -h shows this help text.\n'
printf ' -a replace '\'' ; '\'' sequences with '\'' ; sudo '\''.\n'
printf ' be careful using this option.\n'
printf ' -o only sets an alias if a preceding sudo would be needed.\n'
return 0
;;
(*) printf "unkown option: '%s'\n" "$1" ; return 1 ;;
esac
shift
done
if (( ${#argv} > 1 )) ; then
printf 'Too many arguments %s\n' "${#argv}"
return 1
fi
key="${1%%\=*}" ; val="${1#*\=}"
if (( EUID == 0 )) && (( only == 0 )); then
alias -- "${key}=${val}"
elif (( EUID > 0 )) ; then
(( multi > 0 )) && val="${val// ; / ; sudo }"
alias -- "${key}=sudo ${val}"
fi
return 0
}
# Check if we can read given files and source those we can.
xsource() {
emulate -L zsh
if (( ${#argv} < 1 )) ; then
printf 'usage: xsource FILE(s)...\n' >&2
return 1
fi
while (( ${#argv} > 0 )) ; do
[[ -r $1 ]] && source $1
shift
done
return 0
}
# Check if we can read a given file and 'cat(1)' it.
xcat() {
emulate -L zsh
if (( ${#argv} != 1 )) ; then
printf 'usage: xcat FILE\n' >&2
return 1
fi
[[ -r $1 ]] && cat $1
return 0
}
# Remove these functions again, they are of use only in these
# setup files. This should be called at the end of .zshrc.
xunfunction() {
emulate -L zsh
local -a funcs
funcs=(salias xcat xsource xunfunction zrcautoload)
for func in $funcs ; do
[[ -n ${functions[$func]} ]] \
&& unfunction $func
done
return 0
}
#}}}
# Handles command not fount error
function command_not_found_handler() {
emulate -L zsh
if [[ -x /usr/share/command-not-found/command-not-found ]] ; then
/usr/share/command-not-found/command-not-found $1
fi
return 1
}
# Gets battery percentage.
battery() {
PERCENT="${${"$(acpi 2>/dev/null)"}/(#b)[[:space:]]#Battery <->: [^0-9]##, (<->)%*/${match[1]}}"
}
# Sets color in BATTCOLOR, designed to be used with RPROMPT.
batcolor() {
battery
if [ ! -z $PERCENT ]; then
if (acpi | head -n 1 | grep "Charging") 2> /dev/null 1>&2; then
BATTCOLOR="%F{cyan}"
else
if [ $PERCENT -gt 65 ]; then
BATTCOLOR="%F{green}"
else
if [ $PERCENT -gt 32 ]; then
BATTCOLOR="%F{yellow}"
else
BATTCOLOR="%F{red}"
fi
fi
fi
BATTERY="${BATTCOLOR} ${PERCENT} %%"
else
BATTERY=""
fi
}
#x compatibility
xsource "/etc/default/locale"
for var in LANG LC_ALL LC_MESSAGES ; do
[[ -n ${(P)var} ]] && export $var
done
xsource "/etc/sysconfig/keyboard"
#Sets timezone
TZ=$(xcat /etc/timezone)
# color setup for ls:
check_com -c dircolors && eval $(dircolors -b)
# Pager
export PAGER=less
# While in doubt.
export SHELL='/bin/zsh'
# Colors in less...
export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking
export LESS_TERMCAP_md=$'\E[01;38;5;74m' # begin bold
export LESS_TERMCAP_me=$'\E[0m' # end mode
export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
export LESS_TERMCAP_so=$'\E[38;5;246m' # begin standout-mode - info box
export LESS_TERMCAP_ue=$'\E[0m' # end underline
export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline
export LESS="-SR"
# does not need export
MAILCHECK=30 # mailchecks
REPORTTIME=5 # report about cpu-/system-/user-time of command if running longer than 5 seconds
watch=(notme root) # watch for everyone but me and root
# automatically remove duplicates from these arrays
typeset -U path cdpath fpath manpath
# adding personnal completion files in fpath
fpath=(~/.zsh/completion $fpath)
if [ $TERM = "rxvt-unicode-256color" ]; then
export TERM=rxvt-unicode
fi
#+---------------------------------------+
#| HISTORY |
#+---------------------------------------+
h=$(hostname --fqdn)
#v#
HISTFILE=$ZSHDIR/history-$h
HISTSIZE=100000
SAVEHIST=100000
export HISTFILE HISTSIZE SAVEHIST
############ MISC ##################
# set terminal property (used e.g. by msgid-chooser)
export COLORTERM="yes"
# set default browser
if [[ -z "$BROWSER" ]] ; then
if [[ -n "$DISPLAY" ]] ; then
#v# If X11 is running
check_com -c firefox && export BROWSER=firefox
else
#v# If no X11 is running
check_com -c w3m && export BROWSER=w3m
fi
fi
#m# v QTDIR \kbd{/usr/share/qt[34]}\quad [for non-root only]
[[ -d /usr/share/qt3 ]] && export QTDIR=/usr/share/qt3
[[ -d /usr/share/qt4 ]] && export QTDIR=/usr/share/qt4
# support running 'jikes *.java && jamvm HelloWorld' OOTB:
#v# [for non-root only]
[[ -f /usr/share/classpath/glibj.zip ]] && export JIKESPATH=/usr/share/classpath/glibj.zip
# }}}
# umask personnal config file
# used to set specific umask
UMASKFILE=~/.umask
# a generic accept-line wrapper {{{
# This widget can prevent unwanted autocorrections from command-name
# to _command-name, rehash automatically on enter and call any number
# of builtin and user-defined widgets in different contexts.
#
# For a broader description, see:
# <http://bewatermyfriend.org/posts/2007/12-26.11-50-38-tooltime.html>
#
# The code is imported from the file 'zsh/functions/accept-line' from
# <http://ft.bewatermyfriend.org/comp/zsh/zsh-dotfiles.tar.bz2>, which
# distributed under the same terms as zsh itself.
# A newly added command will may not be found or will cause false
# correction attempts, if you got auto-correction set. By setting the
# following style, we force accept-line() to rehash, if it cannot
# find the first word on the command line in the $command[] hash.
zstyle ':acceptline:*' rehash true
function Accept-Line() {
emulate -L zsh
local -a subs
local -xi aldone
local sub
zstyle -a ":acceptline:${alcontext}" actions subs
(( ${#subs} < 1 )) && return 0
(( aldone = 0 ))
for sub in ${subs} ; do
[[ ${sub} == 'accept-line' ]] && sub='.accept-line'
zle ${sub}
(( aldone > 0 )) && break
done
}
function Accept-Line-getdefault() {
emulate -L zsh
local default_action
zstyle -s ":acceptline:${alcontext}" default_action default_action
case ${default_action} in
((accept-line|))
printf ".accept-line"
;;
(*)
printf ${default_action}
;;
esac
}
function accept-line() {
emulate -L zsh
local -a cmdline
local -x alcontext
local buf com fname format msg default_action
alcontext='default'
buf="${BUFFER}"
cmdline=(${(z)BUFFER})
com="${cmdline[1]}"
fname="_${com}"
zstyle -t ":acceptline:${alcontext}" rehash \
&& [[ -z ${commands[$com]} ]] \
&& rehash
if [[ -n ${reswords[(r)$com]} ]] \
|| [[ -n ${aliases[$com]} ]] \
|| [[ -n ${functions[$com]} ]] \
|| [[ -n ${builtins[$com]} ]] \
|| [[ -n ${commands[$com]} ]] ; then
# there is something sensible to execute, just do it.
alcontext='normal'
zle Accept-Line
default_action=$(Accept-Line-getdefault)
zstyle -T ":acceptline:${alcontext}" call_default \
&& zle ${default_action}
return
fi
if [[ -o correct ]] \
|| [[ -o correctall ]] \
&& [[ -n ${functions[$fname]} ]] ; then
# nothing there to execute but there is a function called
# _command_name; a completion widget. Makes no sense to
# call it on the commandline, but the correct{,all} options
# will ask for it nevertheless, so warn the user.
if [[ ${LASTWIDGET} == 'accept-line' ]] ; then
# Okay, we warned the user before, he called us again,
# so have it his way.
alcontext='force'
zle Accept-Line
default_action=$(Accept-Line-getdefault)
zstyle -T ":acceptline:${alcontext}" call_default \
&& zle ${default_action}
return
fi
# prepare warning message for the user, configurable via zstyle.
zstyle -s ":acceptline:${alcontext}" compwarnfmt msg
if [[ -z ${msg} ]] ; then
msg="%c will not execute and completion %f exists."
fi
zformat -f msg "${msg}" "c:${com}" "f:${fname}"
zle -M -- "${msg}"
return
elif [[ -n ${buf//[$' \t\n']##/} ]] ; then
# If we are here, the commandline contains something that is not
# executable, which is neither subject to _command_name correction
# and is not empty. might be a variable assignment
alcontext='misc'
zle Accept-Line
default_action=$(Accept-Line-getdefault)
zstyle -T ":acceptline:${alcontext}" call_default \
&& zle ${default_action}
return
fi
# If we got this far, the commandline only contains whitespace, or is empty.
alcontext='empty'
zle Accept-Line
default_action=$(Accept-Line-getdefault)
zstyle -T ":acceptline:${alcontext}" call_default \
&& zle ${default_action}
}
zle -N accept-line
zle -N Accept-Line
# }}}
# power completion - abbreviation expansion {{{
# power completion / abbreviation expansion / buffer expansion
# see http://zshwiki.org/home/examples/zleiab for details
# less risky than the global aliases but powerful as well
# just type the abbreviation key and afterwards ',.' to expand it
declare -A abk
setopt extendedglob
setopt interactivecomments
abk=(
# key # value (#d additional doc string)
#A# start
'...' '../..'
'....' '../../..'
'BG' '& exit'
'C' '| wc -l'
'G' '|& grep --color=auto '
'H' '| head'
'Hl' ' --help |& less -r' #d (Display help in pager)
'L' '| less'
'LL' '|& less -r'
'M' '| most'
'N' '&>/dev/null' #d (No Output)
'R' '| tr A-z N-za-m' #d (ROT13)
'SL' '| sort | less'
'S' '| sort -u'
'T' '| tail'
'V' '|& vim -'
#A# end
'co' './configure && make && sudo make install'
)
globalias() {
emulate -L zsh
setopt extendedglob
local MATCH
if (( NOABBREVIATION > 0 )) ; then
LBUFFER="${LBUFFER},."
return 0
fi
matched_chars='[.-|_a-zA-Z0-9]#'
LBUFFER=${LBUFFER%%(#m)[.-|_a-zA-Z0-9]#}
LBUFFER+=${abk[$MATCH]:-$MATCH}
}
zle -N globalias
bindkey ",." globalias
# }}}
This diff is collapsed.
#!/bin/zsh
# Fichier contenant des fonctions "pratiques" de manipulation de
# VCS comme git ou cvs ou autre
#f5# Cvs add
cvsa() {
emulate -L zsh
cvs add $* && cvs com -m 'initial checkin' $*
}
#f5# Cvs diff
cvsd() {
emulate -L zsh
cvs diff -N $* |& $PAGER
}
#f5# Cvs log
cvsl() {
emulate -L zsh
cvs log $* |& $PAGER
}
#f5# Cvs update
cvsq() {
emulate -L zsh
cvs -nq update
}
#f5# Rcs2log
cvsr() {
emulate -L zsh
rcs2log $* | $PAGER
}
#f5# Cvs status
cvss() {
emulate -L zsh
cvs status -v $*
}
#f5# Find all files in \$PATH with setuid bit set
suidfind() { ls -latg $path | grep '^...s' }
#f5# Get specific git commitdiff
git-get-diff() {
emulate -L zsh
if [[ -z $GITTREE ]] ; then
GITTREE='linux/kernel/git/torvalds/linux-2.6.git'
fi
if ! [[ -z $1 ]] ; then
${=BROWSER} "http://kernel.org/git/?p=$GITTREE;a=commitdiff;h=$1"
else
echo "Usage: git-get-diff <commit>"
fi
}
#f5# Get specific git commit
git-get-commit() {
emulate -L zsh
if [[ -z $GITTREE ]] ; then
GITTREE='linux/kernel/git/torvalds/linux-2.6.git'
fi
if ! [[ -z $1 ]] ; then
${=BROWSER} "http://kernel.org/git/?p=$GITTREE;a=commit;h=$1"
else
echo "Usage: git-get-commit <commit>"
fi
}
#f5# Get specific git diff
git-get-plaindiff () {
emulate -L zsh
if [[ -z $GITTREE ]] ; then
GITTREE='linux/kernel/git/torvalds/linux-2.6.git'
fi
if [[ -z $1 ]] ; then
echo 'Usage: git-get-plaindiff '
else
echo -n "Downloading $1.diff ... "
# avoid "generating ..." stuff from kernel.org server:
wget --quiet "http://kernel.org/git/?p=$GITTREE;a=commitdiff_plain;h=$1" -O /dev/null
wget --quiet "http://kernel.org/git/?p=$GITTREE;a=commitdiff_plain;h=$1" -O $1.diff \
&& echo done || echo failed
fi
}
#!/bin/zsh
# mercurial related stuff {{{
if check_com -c hg ; then
# gnu like diff for mercurial
# http://www.selenic.com/mercurial/wiki/index.cgi/TipsAndTricks
#f5# GNU like diff for mercurial
hgdi() {
emulate -L zsh
for i in $(hg status -marn "$@") ; diff -ubwd <(hg cat "$i") "$i"
}
# build debian package
#a2# Alias for \kbd{hg-buildpackage}
alias hbp='hg-buildpackage'
# execute commands on the versioned patch-queue from the current repos
alias mq='hg -R $(readlink -f $(hg root)/.hg/patches)'
# diffstat for specific version of a mercurial repository
# hgstat => display diffstat between last revision and tip
# hgstat 1234 => display diffstat between revision 1234 and tip
#f5# Diffstat for specific version of a mercurial repos
hgstat() {
emulate -L zsh
[[ -n "$1" ]] && hg diff -r $1 -r tip | diffstat || hg export tip | diffstat
}
#f5# Get current mercurial tip via hg itself
gethgclone() {
emulate -L zsh
setopt errreturn
if [[ -f mercurial-tree/.hg ]] ; then
cd mercurial-tree
echo "Running hg pull for retreiving latest version..."
hg pull
echo "Finished update. Building mercurial"
make local
echo "Setting \$PATH to $PWD:\$PATH..."
export PATH="$PWD:$PATH"
else
echo "Downloading mercurial via hg"
hg clone http://selenic.com/repo/hg mercurial-tree
cd mercurial-tree
echo "Building mercurial"
make local
echo "Setting \$PATH to $PWD:\$PATH..."
export PATH="$PWD:$PATH"
echo "make sure you set it permanent via ~/.zshrc if you plan to use it permanently."
# echo "Setting \$PYTHONPATH to PYTHONPATH=\${HOME}/lib/python,"
# export PYTHONPATH=${HOME}/lib/python
fi
}
fi # end of check whether we have the 'hg'-executable
# get current mercurial snapshot
#f5# Get current mercurial snapshot
gethgsnap() {
emulate -L zsh
setopt errreturn
if [[ -f mercurial-snapshot.tar.gz ]] ; then
echo "mercurial-snapshot.tar.gz exists already, skipping download."
else
echo "Downloading mercurial snapshot"
wget http://www.selenic.com/mercurial/mercurial-snapshot.tar.gz
fi
echo "Unpacking mercurial-snapshot.tar.gz"
tar zxf mercurial-snapshot.tar.gz
cd mercurial-snapshot/
echo "Installing required build-dependencies"
$SUDO apt-get update
$SUDO apt-get install python2.4-dev
echo "Building mercurial"
make local
echo "Setting \$PATH to $PWD:\$PATH..."
export PATH="$PWD:$PATH"
echo "make sure you set it permanent via ~/.zshrc if you plan to use it permanently."
}
#!/bin/zsh
# directory based profiles {{{
CHPWD_PROFILE='default'
function chpwd_profiles() {
# Say you want certain settings to be active in certain directories.
# This is what you want.
#
# zstyle ':chpwd:profiles:/usr/src/grml(|/|/*)' profile grml
# zstyle ':chpwd:profiles:/usr/src/debian(|/|/*)' profile debian
#
# When that's done and you enter a directory that matches the pattern
# in the third part of the context, a function called chpwd_profile_grml,
# for example, is called (if it exists).
#
# If no pattern matches (read: no profile is detected) the profile is
# set to 'default', which means chpwd_profile_default is attempted to
# be called.
#
# A word about the context (the ':chpwd:profiles:*' stuff in the zstyle
# command) which is used: The third part in the context is matched against
# ${PWD}. That's why using a pattern such as /foo/bar(|/|/*) makes sense.
# Because that way the profile is detected for all these values of ${PWD}:
# /foo/bar
# /foo/bar/
# /foo/bar/baz
# So, if you want to make double damn sure a profile works in /foo/bar
# and everywhere deeper in that tree, just use (|/|/*) and be happy.
#
# The name of the detected profile will be available in a variable called
# 'profile' in your functions. You don't need to do anything, it'll just
# be there.
#
# Then there is the parameter $CHPWD_PROFILE is set to the profile, that
# was is currently active. That way you can avoid running code for a
# profile that is already active, by running code such as the following
# at the start of your function:
#
# function chpwd_profile_grml() {
# [[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
# ...
# }
#
# The initial value for $CHPWD_PROFILE is 'default'.
#
# Version requirement:
# This feature requires zsh 4.3.3 or newer.
# If you use this feature and need to know whether it is active in your
# current shell, there are several ways to do that. Here are two simple
# ways:
#
# a) If knowing if the profiles feature is active when zsh starts is