init.zsh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #
  2. # Initializes Prezto.
  3. #
  4. # Authors:
  5. # Sorin Ionescu <sorin.ionescu@gmail.com>
  6. #
  7. #
  8. # Version Check
  9. #
  10. # Check for the minimum supported version.
  11. min_zsh_version='4.3.11'
  12. if ! autoload -Uz is-at-least || ! is-at-least "$min_zsh_version"; then
  13. printf "prezto: old shell detected, minimum required: %s\n" "$min_zsh_version" >&2
  14. return 1
  15. fi
  16. unset min_zsh_version
  17. # zprezto convenience updater
  18. # The function is surrounded by ( ) instead of { } so it starts in a subshell
  19. # and won't affect the environment of the calling shell
  20. function zprezto-update {
  21. (
  22. function cannot-fast-forward {
  23. local STATUS="$1"
  24. [[ -n "${STATUS}" ]] && printf "%s\n" "${STATUS}"
  25. printf "Unable to fast-forward the changes. You can fix this by "
  26. printf "running\ncd '%s' and then\n'git pull' " "${ZPREZTODIR}"
  27. printf "to manually pull and possibly merge in changes\n"
  28. }
  29. cd -q -- "${ZPREZTODIR}" || return 7
  30. local orig_branch="$(git symbolic-ref HEAD 2> /dev/null | cut -d '/' -f 3)"
  31. if [[ "$orig_branch" == "master" ]]; then
  32. git fetch || return "$?"
  33. local UPSTREAM=$(git rev-parse '@{u}')
  34. local LOCAL=$(git rev-parse HEAD)
  35. local REMOTE=$(git rev-parse "$UPSTREAM")
  36. local BASE=$(git merge-base HEAD "$UPSTREAM")
  37. if [[ $LOCAL == $REMOTE ]]; then
  38. printf "There are no updates.\n"
  39. return 0
  40. elif [[ $LOCAL == $BASE ]]; then
  41. printf "There is an update available. Trying to pull.\n\n"
  42. if git pull --ff-only; then
  43. printf "Syncing submodules\n"
  44. git submodule sync --recursive
  45. git submodule update --init --recursive
  46. return $?
  47. else
  48. cannot-fast-forward
  49. return 1
  50. fi
  51. elif [[ $REMOTE == $BASE ]]; then
  52. cannot-fast-forward "Commits in master that aren't in upstream."
  53. return 1
  54. else
  55. cannot-fast-forward "Upstream and local have diverged."
  56. return 1
  57. fi
  58. else
  59. printf "zprezto install at '%s' is not on the master branch " "${ZPREZTODIR}"
  60. printf "(you're on '%s')\nUnable to automatically update.\n" "${orig_branch}"
  61. return 1
  62. fi
  63. return 1
  64. )
  65. }
  66. #
  67. # Module Loader
  68. #
  69. # Loads Prezto modules.
  70. function pmodload {
  71. local -a pmodules
  72. local -a pmodule_dirs
  73. local -a locations
  74. local pmodule
  75. local pmodule_location
  76. local pfunction_glob='^([_.]*|prompt_*_setup|README*|*~)(-.N:t)'
  77. # Load in any additional directories and warn if they don't exist
  78. zstyle -a ':prezto:load' pmodule-dirs 'user_pmodule_dirs'
  79. for user_dir in "$user_pmodule_dirs[@]"; do
  80. if [[ ! -d "$user_dir" ]]; then
  81. echo "$0: Missing user module dir: $user_dir"
  82. fi
  83. done
  84. pmodule_dirs=("$ZPREZTODIR/modules" "$ZPREZTODIR/contrib" "$user_pmodule_dirs[@]")
  85. # $argv is overridden in the anonymous function.
  86. pmodules=("$argv[@]")
  87. # Load Prezto modules.
  88. for pmodule in "$pmodules[@]"; do
  89. if zstyle -t ":prezto:module:$pmodule" loaded 'yes' 'no'; then
  90. continue
  91. else
  92. locations=(${pmodule_dirs:+${^pmodule_dirs}/$pmodule(-/FN)})
  93. if (( ${#locations} > 1 )); then
  94. if ! zstyle -t ':prezto:load' pmodule-allow-overrides 'yes'; then
  95. print "$0: conflicting module locations: $locations"
  96. continue
  97. fi
  98. elif (( ${#locations} < 1 )); then
  99. print "$0: no such module: $pmodule"
  100. continue
  101. fi
  102. # Grab the full path to this module
  103. pmodule_location=${locations[-1]}
  104. # Add functions to $fpath.
  105. fpath=(${pmodule_location}/functions(-/FN) $fpath)
  106. function {
  107. local pfunction
  108. # Extended globbing is needed for listing autoloadable function directories.
  109. setopt LOCAL_OPTIONS EXTENDED_GLOB
  110. # Load Prezto functions.
  111. for pfunction in ${pmodule_location}/functions/$~pfunction_glob; do
  112. autoload -Uz "$pfunction"
  113. done
  114. }
  115. if [[ -s "${pmodule_location}/init.zsh" ]]; then
  116. source "${pmodule_location}/init.zsh"
  117. elif [[ -s "${pmodule_location}/${pmodule}.plugin.zsh" ]]; then
  118. source "${pmodule_location}/${pmodule}.plugin.zsh"
  119. fi
  120. if (( $? == 0 )); then
  121. zstyle ":prezto:module:$pmodule" loaded 'yes'
  122. else
  123. # Remove the $fpath entry.
  124. fpath[(r)${pmodule_location}/functions]=()
  125. function {
  126. local pfunction
  127. # Extended globbing is needed for listing autoloadable function
  128. # directories.
  129. setopt LOCAL_OPTIONS EXTENDED_GLOB
  130. # Unload Prezto functions.
  131. for pfunction in ${pmodule_location}/functions/$~pfunction_glob; do
  132. unfunction "$pfunction"
  133. done
  134. }
  135. zstyle ":prezto:module:$pmodule" loaded 'no'
  136. fi
  137. fi
  138. done
  139. }
  140. #
  141. # Prezto Initialization
  142. #
  143. # This finds the directory prezto is installed to so plugin managers don't need
  144. # to rely on dirty hacks to force prezto into a directory. Additionally, it
  145. # needs to be done here because inside the pmodload function ${0:h} evaluates to
  146. # the current directory of the shell rather than the prezto dir.
  147. ZPREZTODIR=${0:h}
  148. # Source the Prezto configuration file.
  149. if [[ -s "${ZDOTDIR:-$HOME}/.zpreztorc" ]]; then
  150. source "${ZDOTDIR:-$HOME}/.zpreztorc"
  151. fi
  152. # Disable color and theme in dumb terminals.
  153. if [[ "$TERM" == 'dumb' ]]; then
  154. zstyle ':prezto:*:*' color 'no'
  155. zstyle ':prezto:module:prompt' theme 'off'
  156. fi
  157. # Load Zsh modules.
  158. zstyle -a ':prezto:load' zmodule 'zmodules'
  159. for zmodule ("$zmodules[@]") zmodload "zsh/${(z)zmodule}"
  160. unset zmodule{s,}
  161. # Load more specific 'run-help' function from $fpath.
  162. (( $+aliases[run-help] )) && unalias run-help && autoload -Uz run-help
  163. # Autoload Zsh functions.
  164. zstyle -a ':prezto:load' zfunction 'zfunctions'
  165. for zfunction ("$zfunctions[@]") autoload -Uz "$zfunction"
  166. unset zfunction{s,}
  167. # Load Prezto modules.
  168. zstyle -a ':prezto:load' pmodule 'pmodules'
  169. pmodload "$pmodules[@]"
  170. unset pmodules