Skip to content

Commit

Permalink
Added prezto
Browse files Browse the repository at this point in the history
- Loading and creating symlink for prezto
- using preztos builtin module load function
- allow setting of prezto options, and *:*
- Load a repository as prezto module
  • Loading branch information
m42e committed Sep 8, 2015
1 parent 8e1c8de commit b7e0660
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 2 deletions.
39 changes: 39 additions & 0 deletions README.md
Expand Up @@ -11,6 +11,22 @@ A lightweight plugin manager for ZSH inspired by Antigen. The goal is to have a
zgen oh-my-zsh <location>
This is a shortcut for `zgen load`.

#### Or if you prefer prezto
zgen prezto
This will also create a symlink in the ZSHDOT or HOME directory. This is needed by prezto

#### Load prezto plugins
zgen prezto <modulename>
This is uses the prezto method for loading modules

#### Load a repo as prezto plugins
zgen pmodule <reponame> <branch>
This is uses the prezto method for loading the module, by creating a symlink and calling pmodule

#### Set prezto options
zgen prezto <modulename> <option> <value(s)>
This must be used before the module is loaded. Or if the default modules should be loaded (default) these settings must be done before the `zgen prezto` command. `module` is prepended if the name does not start with `module`, `prezto` or a `*`, `prezto` is prepended if it does not start with `prezto`.

#### Load plugins and completions
zgen load <repo> [location] [branch]
Similar to `antigen bundle`. It tries to source any scripts from `location`. If none found, it adds `location` to `$fpath`.
Expand Down Expand Up @@ -82,6 +98,29 @@ EOPLUGINS
fi
```

### Example .zshrc for prezto use
Here is a partial example how to work with prezto

```zsh
...
echo "Creating a zgen save"

# prezto options
zgen prezto editor key-bindings 'emacs'
zgen prezto prompt theme 'sorin'

# prezto and modules
zgen prezto
zgen prezto git
zgen prezto command-not-found
zgen prezto syntax-highlighting

# plugins
zgen load /path/to/super-secret-private-plugin
....

```

## Other resources

The [awesome-zsh-plugins](https://github.com/unixorn/awesome-zsh-plugins) list contains many zgen-compatible zsh plugins & themes that you may find useful.
152 changes: 150 additions & 2 deletions zgen.zsh
Expand Up @@ -16,10 +16,26 @@ if [[ -z "${ZGEN_LOADED}" ]]; then
ZGEN_LOADED=()
fi

if [[ -z "${ZGEN_PREZTO_OPTIONS}" ]]; then
ZGEN_PREZTO_OPTIONS=()
fi

if [[ -z "${ZGEN_PREZTO_LOAD}" ]]; then
ZGEN_PREZTO_LOAD=()
fi

if [[ -z "${ZGEN_COMPLETIONS}" ]]; then
ZGEN_COMPLETIONS=()
fi

if [[ -z "${ZGEN_USE_PREZTO}" ]]; then
ZGEN_USE_PREZTO=0
fi

if [[ -z "${ZGEN_PREZTO_LOAD_DEFAULT}" ]]; then
ZGEN_PREZTO_LOAD_DEFAULT=1
fi

if [[ -z "${ZGEN_OH_MY_ZSH_REPO}" ]]; then
ZGEN_OH_MY_ZSH_REPO=robbyrussell
fi
Expand All @@ -32,6 +48,18 @@ if [[ -z "${ZGEN_OH_MY_ZSH_BRANCH}" ]]; then
ZGEN_OH_MY_ZSH_BRANCH=master
fi

if [[ -z "${ZGEN_PREZTO_REPO}" ]]; then
ZGEN_PREZTO_REPO=sorin-ionescu
fi

if [[ "${ZGEN_PREZTO_REPO}" != */* ]]; then
ZGEN_PREZTO_REPO="${ZGEN_PREZTO_REPO}/prezto"
fi

if [[ -z "${ZGEN_PREZTO_BRANCH}" ]]; then
ZGEN_PREZTO_BRANCH=master
fi

-zgen-encode-url () {
# Remove characters from a url that don't work well in a filename.
# Inspired by -anti-get-clone-dir() method from antigen.
Expand Down Expand Up @@ -116,6 +144,43 @@ zgen-clone() {
-zgen-add-to-fpath "${completion_path}"
}

-zgen-prezto-option(){
local module=${1}
shift
local option=${1}
shift
local params=$@
if [[ ${module} =~ "^:" ]]; then
module=${module[1,]}
fi
if [[ ! $module =~ "^(\*|module|prezto:module):" ]]; then
module="module:$module"
fi
if [[ ! $module =~ "^(prezto):" ]]; then
module="prezto:$module"
fi
local cmd="zstyle ':${module}' $option ${(qq)params}"

# execute in place
eval $cmd

if [[ ! "${ZGEN_PREZTO_OPTIONS[@]}" =~ "${cmd}" ]]; then
ZGEN_PREZTO_OPTIONS+=("${cmd}")
fi
}

-zgen-prezto-load(){
local params="$*"
local cmd="pmodload ${params[@]}"

# execute in place
eval $cmd

if [[ ! "${ZGEN_PREZTO[@]}" =~ "${cmd}" ]]; then
ZGEN_PREZTO_LOAD+=("${params[@]}")
fi
}

zgen-init() {
if [[ -f "${ZGEN_INIT}" ]]; then
source "${ZGEN_INIT}"
Expand Down Expand Up @@ -145,6 +210,17 @@ zgen-save() {
echo "#" >! "${ZGEN_INIT}"
echo "# Generated by zgen." >> "${ZGEN_INIT}"
echo "# This file will be overwritten the next time you run zgen save" >> "${ZGEN_INIT}"
echo >> "${ZGEN_INIT}"
echo "ZSH=$(-zgen-get-zsh)" >> "${ZGEN_INIT}"
if [[ ${ZGEN_USE_PREZTO} == 1 ]]; then
echo >> "${ZGEN_INIT}"
echo "# init prezto" >> "${ZGEN_INIT}"
for option in "${ZGEN_PREZTO_OPTIONS[@]}"; do
echo "${option}" >> "${ZGEN_INIT}"
done
fi

echo >> "${ZGEN_INIT}"
echo "#" >> "${ZGEN_INIT}"
for file in "${ZGEN_LOADED[@]}"; do
echo "source \"${(q)file}\"" >> "${ZGEN_INIT}"
Expand Down Expand Up @@ -173,6 +249,17 @@ zgen-save() {
echo "fi" >> "${ZGEN_INIT}"
fi

# load prezto modules
if [[ ${ZGEN_USE_PREZTO} == 1 ]]; then
echo >> "${ZGEN_INIT}"
echo "# init prezto" >> "${ZGEN_INIT}"
echo -n "pmodload " >> "${ZGEN_INIT}"
for module in "${ZGEN_PREZTO_LOAD[@]}"; do
echo -n "${module} " >> "${ZGEN_INIT}"
done
echo >> "${ZGEN_INIT}"
fi

zgen-apply --verbose
}

Expand All @@ -193,6 +280,14 @@ zgen-completions() {
[ -e "$1"/*"$2"(.,@[1]) ]
}

-zgen-get-zsh(){
if [[ ${ZGEN_USE_PREZTO} == 1 ]]; then
echo "$(-zgen-get-clone-dir "$ZGEN_PREZTO_REPO" "$ZGEN_PREZTO_BRANCH")"
else
echo "$(-zgen-get-clone-dir "$ZGEN_OH_MY_ZSH_REPO" "$ZGEN_OH_MY_ZSH_BRANCH")"
fi
}

zgen-load() {
if [[ "$#" == 1 && ("${1[1]}" == '/' || "${1[1]}" == '.' ) ]]; then
local location="${1}"
Expand Down Expand Up @@ -294,10 +389,64 @@ zgen-oh-my-zsh() {
zgen-load "${repo}" "${file}"
}

zgen-prezto() {
local repo="$ZGEN_PREZTO_REPO"
local file="${1:-init.zsh}"

# load prezto itself
if [[ $# == 0 ]]; then
ZGEN_USE_PREZTO=1
zgen-load "${repo}" "${file}"
if [[ ! -h ${ZDOTDIR:-$HOME}/.zprezto ]]; then
local dir="$(-zgen-get-clone-dir ${repo} ${ZGEN_PREZTO_BRANCH})"
ln -s "${dir}" "${ZDOTDIR:-$HOME}/.zprezto"
fi
if [[ ${ZGEN_PREZTO_LOAD_DEFAULT} != 0 ]]; then
-zgen-prezto-load "'environment' 'terminal' 'editor' 'history' 'directory' 'spectrum' 'utility' 'completion' 'prompt'"
fi

# this is a prezto module
elif [[ $# == 1 ]]; then
local module=${file}
if [[ -z ${file} ]]; then
echo "Please specify which module to load using 'zgen prezto <name of module>'"
return 1
fi
-zgen-prezto-load "'$module'"

# this is a prezto option
else
shift
-zgen-prezto-option ${file} ${(q)@}
fi

}

zgen-pmodule() {
local repo="${1}"
local branch="${2:-master}"

local dir="$(-zgen-get-clone-dir ${repo} ${branch})"

# clone repo if not present
if [[ ! -d "${dir}" ]]; then
zgen-clone "${repo}" "${branch}"
fi

local module=$(basename ${repo})

local preztodir="${ZDOTDIR:-$HOME}/.zprezto/modules/${module}"
if [[ ! -h ${preztodir} ]]; then
ln -s $dir ${preztodir}
fi

-zgen-prezto-load "'${module}'"
}

zgen() {
local cmd="${1}"
if [[ -z "${cmd}" ]]; then
echo "usage: zgen [clone|completions|list|load|oh-my-zsh|reset|save|selfupdate|update]"
echo "usage: zgen [clone|completions|list|load|oh-my-zsh|pmodule|prezto|reset|save|selfupdate|update]"
return 1
fi

Expand All @@ -310,7 +459,6 @@ zgen() {
fi
}

ZSH="$(-zgen-get-clone-dir "$ZGEN_OH_MY_ZSH_REPO" "$ZGEN_OH_MY_ZSH_BRANCH")"
zgen-init
fpath=($ZGEN_SOURCE $fpath)

Expand Down

0 comments on commit b7e0660

Please sign in to comment.