Oh My Zshの処理を見てみる(2) : oh-my-zsh.sh
oh-my-zsh.sh
Oh My Zshのメイン処理であるoh-my-zsh.shの処理を見てみる。
# Check for updates on initial load... if [ "$DISABLE_AUTO_UPDATE" != "true" ]; then env ZSH=$ZSH DISABLE_UPDATE_PROMPT=$DISABLE_UPDATE_PROMPT zsh -f $ZSH/tools/check_for_upgrade.sh fi
$ZSH/tools/check_for_upgrade.shにアップデートをするスクリプトがあり、起動時に自動アップデートチェックを行う。デフォルトでは2週間毎にチェックを行う設定になっており、~/.zsh-updateに前回のアップデート実行日(UNIX timeを60 / 60 / 24したもの)が記述されている。自動アップデートチェックを無効にしたい場合は~/.zshrcのDISABLE_AUTO_UPDATE="true"を有効化すればよい。
$ cat ~/.zsh-update LAST_EPOCH=17056
# Initializes Oh My Zsh
ここからOh My Zshの初期化を開始。
# add a function path fpath=($ZSH/functions $ZSH/completions $fpath)
$fpathに$ZSH/functionsと$ZSH/completionsを追加する。$fpathとは、
この後のautoloadコマンドで関数を読み込むが、そのときに関数を探索するパス。ただ、自分の環境では$ZSH/functions、$ZSH/completions共に存在しなかった。
ちなみに、$fpathは半角スペース区切りで、$FPATHはコロン区切りになる。
$ echo $fpath /home/vagrant/.oh-my-zsh/plugins/git /home/vagrant/.oh-my-zsh/functions /home/vagrant/.oh-my-zsh/completions /usr/local/share/zsh/site-functions /usr/local/share/zsh/5.2/functions $ echo $FPATH /home/vagrant/.oh-my-zsh/plugins/git:/home/vagrant/.oh-my-zsh/functions:/home/vagrant/.oh-my-zsh/completions:/usr/local/share/zsh/site-functions:/usr/local/share/zsh/5.2/functions
# Load all stock functions (from $fpath files) called below. autoload -U compaudit compinit
この後の処理で使用するcompauditとcompinit関数をロードする。このときの関数の探索先が先程設定した$fpathになる。実際に調べてみるとこれらの関数は/usr/local/share/zsh/5.2/functions(環境により違うはず)で定義されている。autoloadの-Uはエイリアスの展開をしないオプションで、これを設定しないと意図しない動作となってしまう場合がある。
: ${ZSH_DISABLE_COMPFIX:=true}
この書き方は下記ページが参考になる。結果として、$ZSH_DISABLE_COMPFIXがtrueにセットされるようだ。
# Set ZSH_CUSTOM to the path where your custom config files # and plugins exists, or else we will use the default custom/ if [[ -z "$ZSH_CUSTOM" ]]; then ZSH_CUSTOM="$ZSH/custom" fi
ユーザのカスタム設定ファルを置くディレクトリを設定する。~/.zshrcで$ZSH_CUSTOMが設定されていない場合はデフォルトの$ZSH/customが設定される。
# Set ZSH_CACHE_DIR to the path where cache files should be created # or else we will use the default cache/ if [[ -z "$ZSH_CACHE_DIR" ]]; then ZSH_CACHE_DIR="$ZSH/cache" fi
キャッシュファイルを作成するディレクトリを設定する。~/.zshrcで$ZSH_CACHE_DIRが設定されていない場合はデフォルトの$ZSH/cacheが設定される。キャッシュを利用して高速に処理するようだがよくわからないので後回し。
# Load all of the config files in ~/oh-my-zsh that end in .zsh # TIP: Add files you don't want in git to .gitignore for config_file ($ZSH/lib/*.zsh); do custom_config_file="${ZSH_CUSTOM}/lib/${config_file:t}" [ -f "${custom_config_file}" ] && config_file=${custom_config_file} source $config_file done
$ZSH/libにあるスクリプト群をsourceするが、$ZSH_CUSTOM/libに同名のスクリプトがあればそちらが優先される。:tは編集子と呼ばれるものでファイル名だけを取得する。$ZSH/lib以下には色々なスクリプトがあるので後回し。
is_plugin() {
local base_dir=$1
local name=$2
test -f $base_dir/plugins/$name/$name.plugin.zsh \
|| test -f $base_dir/plugins/$name/_$name
}
# Add all defined plugins to fpath. This must be done
# before running compinit.
for plugin ($plugins); do
if is_plugin $ZSH_CUSTOM $plugin; then
fpath=($ZSH_CUSTOM/plugins/$plugin $fpath)
elif is_plugin $ZSH $plugin; then
fpath=($ZSH/plugins/$plugin $fpath)
fi
done
この後の処理のcompinit実行前に~/.zshrcで設定したプラグインをfpathに追加する。$ZSH_CUSTOM/pluginsに同名のプラグインがあればそちらが優先される。
# Figure out the SHORT hostname if [[ "$OSTYPE" = darwin* ]]; then # OS X's $HOST changes with dhcp, etc. Use ComputerName if possible. SHORT_HOST=$(scutil --get ComputerName 2>/dev/null) || SHORT_HOST=${HOST/.*/} else SHORT_HOST=${HOST/.*/} fi
この後の処理のダンプファイル名で使用するホスト名を設定する。${HOST/.*/}は.*(ドット以下の文字全て)に一致する文字列を空文字に置換するということで、AAA.BBB.CCCの場合はAAAになる。なお、Macの場合はscutil --get ComputerNameによりコンピュータ名を取得する。
# Save the location of the current completion dump file. if [ -z "$ZSH_COMPDUMP" ]; then ZSH_COMPDUMP="${ZDOTDIR:-${HOME}}/.zcompdump-${SHORT_HOST}-${ZSH_VERSION}" fi
補完を高速で行うために使用するダンプファイルのパスとファイル名を指定する。$ZDOTDIRが定義されていない場合は$HOMEが使用されるので、$HOME/.zcompdump-hostname-5.2のようになる。
if [[ $ZSH_DISABLE_COMPFIX != true ]]; then # If completion insecurities exist, warn the user without enabling completions. if ! compaudit &>/dev/null; then # This function resides in the "lib/compfix.zsh" script sourced above. handle_completion_insecurities # Else, enable and cache completions to the desired file. else compinit -d "${ZSH_COMPDUMP}" fi else compinit -i -d "${ZSH_COMPDUMP}" fi
スクリプトの前半で${ZSH_DISABLE_COMPFIX:=true}が設定されているのでcompinit -i -d "${ZSH_COMPDUMP}"が実行される。-iはinsecureなファイルとディレクトリ(所有者がrootかカレントユーザではない)を無視するオプションで、-d "${ZSH_COMPDUMP}"オプションでダンプファイルのパスとファイル名を指定する。補完についてはややこしそうなので後回し。
# Load all of the plugins that were defined in ~/.zshrc for plugin ($plugins); do if [ -f $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh ]; then source $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh elif [ -f $ZSH/plugins/$plugin/$plugin.plugin.zsh ]; then source $ZSH/plugins/$plugin/$plugin.plugin.zsh fi done
~/.zshrcで指定したプラグインをロードする。探索場所は$ZSH_CUSTOM/plugins/と$ZSH/plugins/で、同一のファイル名がある場合は$ZSH_CUSTOM/plugins/の方が優先される。
# Load all of your custom configurations from custom/ for config_file ($ZSH_CUSTOM/*.zsh(N)); do source $config_file done unset config_file
$ZSH_CUSTOMにある拡張子が.zshのカスタム設定ファイルをsourceで読み込む。ここで*.zsh(N)の(N)はNULL_GLOBオプションを指定するという意味で、マッチするファイルが存在しない場合にzsh: no matches found: *.zshメッセージが表示されずに適切に処理できる。
# Load the theme if [ "$ZSH_THEME" = "random" ]; then themes=($ZSH/themes/*zsh-theme) N=${#themes[@]} ((N=(RANDOM%N)+1)) RANDOM_THEME=${themes[$N]} source "$RANDOM_THEME" echo "[oh-my-zsh] Random theme '$RANDOM_THEME' loaded..." else if [ ! "$ZSH_THEME" = "" ]; then if [ -f "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme" ]; then source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme" elif [ -f "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme" ]; then source "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme" else source "$ZSH/themes/$ZSH_THEME.zsh-theme" fi fi fi
~/.zshrcで設定したテーマをロードする。テーマについては下記記事を参照。
wonderwall.hatenablog.com