]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - shell/functions
shell/env: Move shell functions to shell/functions.
[config/dotfiles.git] / shell / functions
1 # Shell functions useful to 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 source_debug() {
9     if [ x$DEBUG != x ]; then
10         echo $@
11     fi
12 }
13
14 # Sources a configuration file if it exists; loads a fallback .local file if
15 # it doesn't. If the .local files doesn't exist nothing is sourced.
16 #
17 # $1: Path to directory where configuration files are located.
18 # $2: Directory name in $1 where the non .local files are stored, can be
19 #     empty. If empty both configuration files (normal and .local) are stored
20 #     in $1.
21 # $3: Base name of file in $2, for example "rc" or "env".
22 # $4: Extension for $3, if this file doesn't exist "$1/$3.local" is sourced.
23 #     Can be empty, then no extension is used.
24 # $5: Additional options, set to nolocal to prevent loading of "$1/$3.local"
25 #                         if "$1/$2/$3.$4" doesn't exist.
26 #
27 # Example with the following directory/file structure, $os is Darwin and
28 # $hostname is marvin.
29 #
30 # ~/.zsh
31 # ~/.zsh/env
32 # ~/.zsh/env.local
33 # ~/.zsh/rc
34 # ~/.zsh/rc.local
35 # ~/.zsh/host/rc.marvin
36 # ~/.zsh/os/rc.Darwin
37 #
38 # To load additional rc files from within ~/.zsh/rc use the following:
39 #
40 # source_config ~/.zsh os   rc $os       # loads os/rc.Darwin
41 # source_config ~/.zsh host rc $hostname # loads host/rc.marvin
42 #
43 # To load additional env files from within ~/.zsh/env use the following (note
44 # nolocal to prevent loading env.local two times if os and host files don't
45 # exist):
46 #
47 # source_config zsh os   env $os       nolocal # loads os/rc.Darwin
48 # source_config zsh host env $hostname         # loads env.local
49 #
50 # By letting $4 empty normal configuration files can be sourced. A .local can
51 # still be used.
52 #
53 # source_config zsh "" file # loads zsh/file if it exists
54 #
55 # Doesn't fit perfectly in this file, but this is the best place to make it
56 # available everywhere.
57 #
58 # If DEBUG is set to a non empty value additional debug output is printed.
59 source_config() {
60     # Path to the file to source and its local counterpart.
61     if [ x$4 != x ]; then
62         source_file=$1/$2/$3.$4
63     # If $4 is empty don't append a trailing dot. This allows source_config()
64     # to load normal configuration files.
65     else
66         source_file=$1/$2/$3
67     fi
68     source_file_local=$1/$3.local
69
70     # Additional debug output.
71     source_debug "source_config(): checking if $source_file exists"
72     source_debug "source_config(): checking if $source_file_local exists"
73
74     # If the file does exist then source it.
75     if [ -f $source_file ]; then
76         source_debug "source_config(): -> sourcing $source_file"
77         . $source_file
78
79     # Otherwise load the .local file if it exists and .local files are
80     # allowed.
81     elif [ -f $source_file_local -a x$5 != xnolocal ]; then
82         source_debug "source_config(): -> sourcing $source_file_local"
83         . $source_file_local
84     else
85         source_debug "source_config(): -> neither exists"
86     fi
87
88     unset source_file source_file_local
89 }
90
91 # vim: ft=sh