]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - shell/env
Improve source_config() debug output.
[config/dotfiles.git] / shell / env
1 # Configuration file for environment related options for all shells.
2
3
4 # Helper function to print debug information if $DEBUG is not empty.
5 #
6 # Doesn't fit perfectly in this file, but this is the best place to make it
7 # available everywhere.
8 function source_debug() {
9     if [ x$DEBUG != x ]; then
10         echo $@
11     fi
12 }
13
14
15 source_debug "sourcing ~/.shell/env"
16
17 # Use UTF-8 encoding in the terminal.
18 LC_ALL=en_US.UTF-8
19 LANG=$LC_ALL
20 export LC_ALL LANG
21
22 # Use Vim as editor.
23 EDITOR=vim
24 export EDITOR
25
26 # Set less as pager, its configuration is done through the .less file.
27 PAGER=less
28 export PAGER
29
30 # Set colors for GNU ls (and zsh completions). This colors files normal (fi),
31 # directories blue bold (di), symbolic links cyan (ln), named pipes normal
32 # (pi), sockets normal (so), block devices normal (bd), character devices
33 # normal (cd), orphaned links with red background (or), setuid (su) and setgid
34 # (sg) files with yellow background, directories writable by others with blue
35 # background (ow) and executables bold red (ex).
36 # See `dircolors --print-database` for possible colors.
37 LS_COLORS="no=00:fi=00:di=34;01:ln=36:pi=00:so=00:bd=00:cd=00:\
38 or=36;41:su=31;01;43:sg=31;01;43:ow=34;01;41:ex=31;01"
39 export LS_COLORS
40 # Set the same colors for non GNU ls, except for orphaned links which aren't
41 # supported.
42 LSCOLORS='ExgxxxxxBxxxxxBdBdExEb'
43 #         ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
44 #         | | | | | | | | | | |
45 #         | | | | | | | | | | *- directory writable to others, without sticky bit
46 #         | | | | | | | | | *--- directory writable to others, with sticky bit
47 #         | | | | | | | | *----- executable with setgid bit set
48 #         | | | | | | | *------- executable with setuid bit set
49 #         | | | | | | *--------- character special
50 #         | | | | | *----------- block special
51 #         | | | | *------------- executable
52 #         | | | *--------------- pipe
53 #         | | *----------------- socket
54 #         | *------------------- symbolic link
55 #         *--------------------- directory
56 export LSCOLORS
57
58
59 # Sources a configuration file if it exists; loads a fallback .local file if
60 # it doesn't. If the .local files doesn't exist nothing is sourced.
61 #
62 # $1: Path to directory where configuration files are located.
63 # $2: Directory name in $1 where the non .local files are stored, can be
64 #     empty. If empty both configuration files (normal and .local) are stored
65 #     in $1.
66 # $3: Base name of file in $2, for example "rc" or "env".
67 # $4: Extension for $3, if this file doesn't exist "$1/$3.local" is sourced.
68 # $5: Additional options, set to nolocal to prevent loading of "$1/$3.local"
69 #                         if "$1/$2/$3.$4" doesn't exist.
70 #
71 # Example with the following directory/file structure, $os is Darwin and
72 # $hostname is marvin.
73 #
74 # ~/.zsh
75 # ~/.zsh/env
76 # ~/.zsh/env.local
77 # ~/.zsh/rc
78 # ~/.zsh/rc.local
79 # ~/.zsh/host/rc.marvin
80 # ~/.zsh/os/rc.Darwin
81 #
82 # To load additional rc files from within ~/.zsh/rc use the following:
83 #
84 # source_config ~/.zsh os   rc $os       # loads os/rc.Darwin
85 # source_config ~/.zsh host rc $hostname # loads host/rc.marvin
86 #
87 # To load additional env files from within ~/.zsh/env use the following (note
88 # nolocal to prevent loading env.local two times if os and host files don't
89 # exist):
90 #
91 # source_config zsh os   env $os       nolocal # loads os/rc.Darwin
92 # source_config zsh host env $hostname         # loads env.local
93 #
94 # Doesn't fit perfectly in this file, but this is the best place to make it
95 # available everywhere.
96 #
97 # If DEBUG is set to a non empty value additional debug output is printed.
98 function source_config() {
99     # Path to the file to source and its local counterpart.
100     local source_file=$1/$2/$3.$4
101     local source_file_local=$1/$3.local
102
103     # Additional debug output.
104     source_debug "source_config(): checking if $source_file exists"
105     source_debug "source_config(): checking if $source_file_local exists"
106
107     # If the file does exist then source it.
108     if [ -f $source_file ]; then
109         source_debug "source_config(): -> sourcing $source_file"
110         source $source_file
111
112     # Otherwise load the .local file if it exists and .local files are
113     # allowed.
114     elif [ -f $source_file_local -a x$5 != xnolocal ]; then
115         source_debug "source_config(): -> sourcing $source_file_local"
116         source $source_file_local
117     else
118         source_debug "source_config(): -> neither exists"
119     fi
120 }
121
122 source_debug "finished sourcing ~/.shell/env"