zshrc: replace ogg2mp3_192 with improved 2mp3_192
[grml-etc-core.git] / etc / zsh / zshrc
index decda2b..42b5671 100644 (file)
@@ -256,13 +256,6 @@ 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
-(( EUID != 0 )) && umask 002 || umask 022
-
 setopt append_history       # append history list to the history file (important for multiple parallel zsh sessions!)
 is4 && 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
@@ -938,7 +931,10 @@ bindkey '^z' grml-zsh-fg
 # run command line as user root via sudo:
 sudo-command-line() {
     [[ -z $BUFFER ]] && zle up-history
-    [[ $BUFFER != sudo\ * ]] && BUFFER="sudo $BUFFER"
+    if [[ $BUFFER != sudo\ * ]]; then
+        BUFFER="sudo $BUFFER"
+        CURSOR=$(( CURSOR+5 ))
+    fi
 }
 zle -N sudo-command-line
 
@@ -2052,9 +2048,7 @@ function info_print () {
     esc_end="$2"
     shift 2
     printf '%s' ${esc_begin}
-    for item in "$@" ; do
-        printf '%s ' "$item"
-    done
+    printf '%s' "$*"
     printf '%s' "${esc_end}"
 }
 
@@ -2286,7 +2280,7 @@ if ! check_com asc &>/dev/null ; then
 fi
 
 # get top 10 shell commands:
-alias top10='print -l ${(o)history%% *} | uniq -c | sort -nr | head -n 10'
+alias top10='print -l ${(o)history%% *} | uniq -c | sort -nr | head -n 10'
 
 # truecrypt; use e.g. via 'truec /dev/ice /mnt/ice' or 'truec -i'
 if check_com -c truecrypt ; then
@@ -2795,7 +2789,7 @@ compdef _aliases edalias
 
 #f1# Edit a function via zle
 edfunc() {
-    [[ -z "$1" ]] && { echo "Usage: edfun <function_to_edit>" ; return 1 } || zed -f "$1" ;
+    [[ -z "$1" ]] && { echo "Usage: edfunc <function_to_edit>" ; return 1 } || zed -f "$1" ;
 }
 compdef _functions edfunc
 
@@ -2881,18 +2875,16 @@ check_com -c qma && alias ?='qma zshall'
 # grep for running process, like: 'any vim'
 any() {
     emulate -L zsh
+    unsetopt KSH_ARRAYS
     if [[ -z "$1" ]] ; then
         echo "any - grep for process(es) by keyword" >&2
         echo "Usage: any <keyword>" >&2 ; return 1
     else
-        local STRING=$1
-        local LENGTH=$(expr length $STRING)
-        local FIRSCHAR=$(echo $(expr substr $STRING 1 1))
-        local REST=$(echo $(expr substr $STRING 2 $LENGTH))
-        ps xauwww| grep "[$FIRSCHAR]$REST"
+        ps xauwww | grep --color=auto "[${1[1]}]${1[2,-1]}"
     fi
 }
 
+
 # After resuming from suspend, system is paging heavily, leading to very bad interactivity.
 # taken from $LINUX-KERNELSOURCE/Documentation/power/swsusp.txt
 [[ -r /proc/1/maps ]] && \
@@ -3079,14 +3071,14 @@ exit 0;
 ssl_hashes=( sha512 sha256 sha1 md5 )
 
 for sh in ${ssl_hashes}; do
-    ssl-cert-${sh}() {
+    eval 'ssl-cert-'${sh}'() {
         emulate -L zsh
         if [[ -z $1 ]] ; then
-            printf 'usage: %s <file>\n' "ssh-cert-${sh}"
+            printf '\''usage: %s <file>\n'\'' "ssh-cert-'${sh}'"
             return 1
         fi
-        openssl x509 -noout -fingerprint -${sh} -in $1
-    }
+        openssl x509 -noout -fingerprint -'${sh}' -in $1
+    }'
 done; unset sh
 
 ssl-cert-fingerprints() {
@@ -3209,9 +3201,9 @@ alias lsd='ls -d *(/)'                 # only show directories
 #a2# Only show empty directories
 alias lse='ls -d *(/^F)'               # only show empty directories
 #a2# Display the ten newest files
-alias lsnew="ls -rl *(D.om[1,10])"     # display the newest files
+alias lsnew="ls -rtlh *(D.om[1,10])"   # display the newest files
 #a2# Display the ten oldest files
-alias lsold="ls -rtlh *(D.om[1,10])"   # display the oldest files
+alias lsold="ls -rtlh *(D.Om[1,10])"   # display the oldest files
 #a2# Display the ten smallest files
 alias lssmall="ls -Srl *(.oL[1,10])"   # display the smallest files
 
@@ -3579,6 +3571,20 @@ purge() {
     fi
 }
 
+#f5# show labels and uuids of disk devices
+if is439 && [[ -d /dev/disk/by-id/ ]]; then
+    lsdisk() {
+        emulate -L zsh
+        setopt extendedglob
+        local -a -U disks
+        disks=( /dev/disk/by-id/*(@:A) )
+        for dev in "$disks[@]"; do
+            print ${fg_bold[red]}${dev}${reset_color} /dev/disk/by-label/*(@e/'[[ ${REPLY:A} == $dev ]] && REPLY=${fg[blue]}LABEL=${REPLY:t}${reset_color}'/N) /dev/disk/by-uuid/*(@e/'[[ ${REPLY:A} == $dev ]] && REPLY=${fg[green]}UUID=${REPLY:t}${reset_color}'/N)
+            print -f "  %s\n" /dev/disk/by-id/*(@e/'[[ ${REPLY:A} == $dev ]]'/N:t)
+        done
+    }
+fi
+
 # Translate DE<=>EN
 # 'translate' looks up fot a word in a file with language-to-language
 # translations (field separator should be " : "). A typical wordlist looks
@@ -3710,7 +3716,11 @@ show-archive() {
 #f5# Use \kbd{vim} as your manpage reader
 vman() {
     emulate -L zsh
-    man $* | col -b | view -c 'set ft=man nomod nolist' -
+    if (( ${#argv} == 0 )); then
+        printf 'usage: vman <topic>\n'
+        return 1
+    fi
+    man "$@" | col -b | view -c 'set ft=man nomod nolist' -
 }
 
 # function readme() { $PAGER -- (#ia3)readme* }
@@ -3891,11 +3901,52 @@ allulimit() {
     ulimit -t unlimited
 }
 
-# ogg2mp3 with bitrate of 192
-ogg2mp3_192() {
-    emulate -L zsh
-    oggdec -o - $1 | lame -b 192 - ${1:r}.mp3
-}
+# 2mp3 transcodes flac and ogg to mp3 with bitrate of 192 while preserving basic tags
+if check_com lame; then
+    2mp3_192() {
+        emulate -L zsh
+        setopt extendedglob
+        unsetopt ksharrays
+
+        local -a DECODE id3tags
+        local -A tagmap
+        local tagdata
+        local RC=0
+        tagmap=("(#l)title" --tt "(#l)artist" --ta "(#l)tracknumber" --tn "(#l)genre" --tg "(#l)album" --tl)
+
+        if [[ ${@[(i)*.ogg]} -le ${#@} ]] && ! check_com oggdec; then
+            echo "ERROR: please install oggdec" >&2
+            return 1
+        fi
+        if [[ ${@[(i)*.flac]} -le ${#@} ]] && ! check_com flac; then
+            echo "ERROR: please install flac" >&2
+            return 1
+        fi
+
+        for af in "$@"; do
+            id3tags=()
+            case "$af" in
+                (*.flac)
+                    DECODE=(flac -d -c "$af")
+                    tagdata="$(metaflac --export-tags-to=- "$af")"
+                    ;;
+                (*.ogg)
+                    DECODE=(oggdec -Q -o - "$af")
+                    tagdata="$(ogginfo "$af")"
+                    ;;
+                (*) continue ;;
+            esac
+            for line (${(f)tagdata}) \
+                [[ "$line" == (#s)[[:space:]]#(#b)([^=]##)=(*)(#e) && -n $tagmap[(k)$match[1]] ]] && \
+                id3tags+=($tagmap[(k)$match[1]] "$match[2]")
+            [[ ${#id3tags} -gt 0 ]] && id3tags=(--add-id3v2 --ignore-tag-errors $id3tags)
+            $DECODE[*] | lame -b 192 -v -h --replaygain-fast  "${id3tags[@]}" - "${af:r}.mp3" || {RC=$?; print "Error transcoding" "${af}"; }
+        done
+        # return 0 if no error or exit code if at least one error happened
+        return $RC
+    }
+    alias ogg2mp3_192 2mp3_192
+fi
 
 #f5# RFC 2396 URL encoding in Z-Shell
 urlencode() {