summaryrefslogtreecommitdiff
path: root/.zsh/zshrc/90_profiles
blob: 556db62cd7ca9b739933373ae889c2a1d172cf4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# zshrc/90_profiles
#
# set up directory specific variables and options
#
# http://michael-prokop.at/blog/2009/05/30/directory-specific-shell-configuration-with-zsh
#
# chpwd_profiles taken from grml zshrc
#
# Copyright © 2010-2017 Tobias Klauser <tklauser@distanz.ch>
# Released under the terms of the Artistic Licence 2.0
#
# Source repository: git://git.distanz.ch/dotfiles/zsh.git

CHPWD_PROFILE='default'

zstyle ':chpwd:profiles:/home/tklauser/go(|/|/*)'   profile distanz
zstyle ':chpwd:profiles:/home/tobiask/go(|/|/*)'    profile distanz
zstyle ':chpwd:profiles:/home/tklauser/src(|/|/*)'  profile distanz
zstyle ':chpwd:profiles:/home/tobiask/src(|/|/*)'   profile distanz
zstyle ':chpwd:profiles:/scratch/tobiask/go(|/|/*)' profile distanz
zstyle ':chpwd:profiles:/home/tobiask/zi(|/|/*)'    profile zhinst
zstyle ':chpwd:profiles:/scratch/tobiask/zi(|/|/*)' profile zhinst
zstyle ':chpwd:profiles:/scratch/yoctozi(|/|/*)'    profile zhinst

chpwd_profile_distanz()
{
	[[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
	print "chpwd(): Switching to profile: $profile"

	export GIT_AUTHOR_EMAIL="tklauser@distanz.ch"
	export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
}

chpwd_profile_zhinst()
{
	[[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
	print "chpwd(): Switching to profile: $profile"

	export GIT_AUTHOR_EMAIL="tobias.klauser@zhinst.com"
	export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
}

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
    #      good enough for you, you can put the following snippet into your
    #      .zshrc.local:
    #
    #   (( ${+functions[chpwd_profiles]} )) && print "directory profiles active"
    #
    #   b) If that is not good enough, and you would prefer to be notified
    #      whenever a profile changes, you can solve that by making sure you
    #      start *every* profile function you create like this:
    #
    #   function chpwd_profile_myprofilename() {
    #       [[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
    #       print "chpwd(): Switching to profile: $profile"
    #     ...
    #   }
    #
    #      That makes sure you only get notified if a profile is *changed*,
    #      not everytime you change directory, which would probably piss
    #      you off fairly quickly. :-)
    #
    # There you go. Now have fun with that.
    local -x profile

    zstyle -s ":chpwd:profiles:${PWD}" profile profile || profile='default'
    if (( ${+functions[chpwd_profile_$profile]} )) ; then
        chpwd_profile_${profile}
    fi

    CHPWD_PROFILE="${profile}"
    return 0
}
chpwd_functions=( ${chpwd_functions} chpwd_profiles )


# vim:ft=zsh