zshrc.d/30-functions.zrc
author Mikael Berthe <mikael@lilotux.net>
Sat, 14 Jul 2012 19:20:57 +0200
changeset 0 7215ca490221
child 5 5dbc9ebf690c
permissions -rw-r--r--
Re-import repository after cleanup from personal stuff (see description) I've remove too dirty or specific stuff, so that i could put this configuration on a public repo. Here's a copy of the initial Mercurial changelog, FWIW... * (2012-07-14) Mikael Berthe Reorganize all scripts * (2012-07-14) Mikael Berthe Add a "tools" directory * (2012-07-14) Mikael Berthe Don't use ^X^C as a keybinding * (2012-07-14) Mikael Berthe Disable ... completion for now * (2012-07-14) Mikael Berthe Add rehash key binding * (2012-07-14) Mikael Berthe Add n fix a few key bindings * (2012-07-14) Mikael Berthe More cleanup Remove zshrc_compinstall Move personnal stuff to zshrc_local * (2012-07-14) Mikael Berthe New commands: ...* Previous commands .. and ... have been renamed to ,, and ,,, * (2012-07-14) Mikael Berthe Add WATCHFMT, move REPORTTIME to zshenv * (2012-07-14) Mikael Berthe Add REPORTTIME * (2012-07-13) Mikael Berthe Add a few useful functions Ideas borrowed from https://github.com/whiteinge/dotfiles/blob/master/.zshrc * (2012-07-13) Mikael Berthe Move aliases to a new file * (2012-07-13) Mikael Berthe Check for zshrc_local existence * (2012-07-13) Mikael Berthe Cleanup vcsinfo prompt * (2012-07-12) Mikael Berthe Colorize time in default color prompt as well * (2012-07-12) Mikael Berthe Prompt: colorize time * (2012-07-12) Mikael Berthe Add a copy of git-prompt.sh * (2012-07-12) Mikael Berthe Update prompt themes Since we do not display the mode on the first line, let's use this place for the date/time. * (2012-07-12) Mikael Berthe Update prompts / new showmode * (2012-07-12) Mikael Berthe More showmode cleanup, part2 * (2012-07-12) Mikael Berthe Switch showmode to using RPROMPT, part1 * (2012-07-12) Mikael Berthe Prompt: many improvements and fixes * (2012-07-11) Mikael Berthe More prompt code cleanup * (2012-07-11) Mikael Berthe More readable prompt generation * (2012-07-11) Mikael Berthe Dynamic path color * (2012-07-11) Mikael Berthe New prompt based on Zsh' vcs_info module * (2012-07-10) Mikael Berthe Add git info, add a light dev2 prompt * (2012-07-10) Mikael Berthe Dev prompt: Improve Mercurial repository detection * (2012-07-08) Mikael Berthe hg prompt: Change color of unapplied patches * (2012-07-08) Mikael Berthe Change Mercurial prompt format * (2012-07-08) Mikael Berthe Try to improve showmode support. Enable it for 256 color terms. * (2012-07-08) Mikael Berthe Fix interaction between showmode and the new "dev" prompt When showmode is disabled (e.g. in screen), do not display the mode in PS1. * (2012-07-08) Mikael Berthe Add development prompt (HG and minimal git support) Can be set up from the shell command line with "prompt_set_dev". * (2012-07-08) Mikael Berthe Ignore Vim swap files * (2009-12-22) Mikael Berthe Slight update zlog{in,out} * (2009-12-22) Mikael Berthe Add zlogin * (2009-12-22) Mikael Berthe Update hgignore list * (2009-12-22) Mikael Berthe Initial import into Mercurial


#############
# Functions #
#############

# Commande tres pratique pour formatter un fichier .man
# exemple : mf prog.1
function mf() { tbl $* | nroff -mandoc | less -s }

# ,,(), ,,,() for quickly changing $CWD {{{1
# http://www.shell-fu.org/lister.php?id=769

# Go up n levels:
# ,, 3
function ,, (){
    local arg=${1:-1};
    local dir=""
    while [ $arg -gt 0 ]; do
        dir="../$dir"
        arg=$(($arg - 1));
    done
    cd $dir >&/dev/null
}

# Go up to a named dir
# ,,, usr
function ,,, (){
    if [ -z "$1" ]; then
        return
    fi
    local maxlvl=16
    local dir=$1
    while [ $maxlvl -gt 0 ]; do
        dir="../$dir"
        maxlvl=$(($maxlvl - 1));
        if [ -d "$dir" ]; then
            cd $dir >&/dev/null
        fi
    done
}


# Customized version from Frank Terbeck' scripts
# http://bewatermyfriend.org/p/2011/013/
# "."   will display the current working directory
# ".."  will get you 1 directory up,
# "..." will get you 2 directories up, etc.
function ft_accept_line_cd_up() {
    setopt local_options extended_glob
    local -a cmdline
    cmdline=( ${(z)BUFFER} )
    if (( ${#cmdline} == 1 )) && [[ ${cmdline[1]} == "." ]] ; then
        BUFFER="pwd"
        zle ".$WIDGET"
        return
    fi
    if (( ${#cmdline} == 1 )) && [[ ${cmdline[1]} == ..## ]] ; then
        BUFFER='cd '
        for (( i = 2; i <= ${#${cmdline[1]}}; i++ )); do
            BUFFER="${BUFFER}../"
        done
        BUFFER=${BUFFER%/}
    fi
    zle ".$WIDGET"
}
zle -N accept-line ft_accept_line_cd_up

# 2nd part, completion support...
function ft_complete_with_dots() {
    setopt local_options extended_glob
    local -a cmdline
    cmdline=( ${(z)BUFFER} )
    if (( ${#cmdline} == 1 )) && [[ ${cmdline[1]} == ..## ]] ; then
        BUFFER='cd '
        for (( i = 2; i <= ${#${cmdline[1]}}; i++ )); do
            BUFFER="${BUFFER}../"
        done
        CURSOR=${#BUFFER}
    fi
    zle ".$WIDGET"
}
# Disabling for now as I don't know how to fallback to automatic correction...
#zle -N expand-or-complete ft_complete_with_dots


# genpass()
# Generates a tough password of a given length
# Borrowed (and slightly modifed) from Seth House github repository.

function genpass() {
    if [ ! "$1" ]; then
        echo "Usage: $0 20"
        echo "For a random, 20-character password."
        return 1
    fi
    dd if=/dev/urandom count=1 2>/dev/null | tr -cd 'A-Za-z0-9' | cut -c-$1
}