summaryrefslogtreecommitdiff
path: root/plugins/magic-enter
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/magic-enter')
-rw-r--r--plugins/magic-enter/README.md17
-rw-r--r--plugins/magic-enter/Readme.md14
-rw-r--r--plugins/magic-enter/magic-enter.plugin.zsh52
3 files changed, 50 insertions, 33 deletions
diff --git a/plugins/magic-enter/README.md b/plugins/magic-enter/README.md
new file mode 100644
index 000000000..78514c67d
--- /dev/null
+++ b/plugins/magic-enter/README.md
@@ -0,0 +1,17 @@
+## Magic Enter plugin
+
+This plugin makes your enter key magical, by binding commonly used commands to it.
+
+To use it, add `magic-enter` to the plugins array in your zshrc file. You can set the
+commands to be run in your .zshrc, before the line containing plugins. If no command
+is specified in a git directory, `git status` is executed; in other directories, `ls`.
+
+```zsh
+# defaults
+MAGIC_ENTER_GIT_COMMAND='git status -u .'
+MAGIC_ENTER_OTHER_COMMAND='ls -lh .'
+
+plugins=(... magic-enter)
+```
+
+**Maintainer:** [@dufferzafar](https://github.com/dufferzafar)
diff --git a/plugins/magic-enter/Readme.md b/plugins/magic-enter/Readme.md
deleted file mode 100644
index b401ab415..000000000
--- a/plugins/magic-enter/Readme.md
+++ /dev/null
@@ -1,14 +0,0 @@
-## Magic Enter
-
-**Maintainer:** [@dufferzafar](https://github.com/dufferzafar)
-
-Makes your enter key magical, by binding commonly used commands to it.
-
-You can set the commands to be run in your .zshrc, before the line containing plugins!
-
-```bash
-MAGIC_ENTER_GIT_COMMAND='git status -u .'
-MAGIC_ENTER_OTHER_COMMAND='ls -lh .'
-
-plugins=(magic-enter)
-```
diff --git a/plugins/magic-enter/magic-enter.plugin.zsh b/plugins/magic-enter/magic-enter.plugin.zsh
index 8e1859678..55b893535 100644
--- a/plugins/magic-enter/magic-enter.plugin.zsh
+++ b/plugins/magic-enter/magic-enter.plugin.zsh
@@ -1,24 +1,38 @@
-# Bind quick stuff to enter!
-#
-# Pressing enter in a git directory runs `git status`
-# in other directories `ls`
-magic-enter () {
+# Default commands
+: ${MAGIC_ENTER_GIT_COMMAND:="git status -u ."} # run when in a git repository
+: ${MAGIC_ENTER_OTHER_COMMAND:="ls -lh ."} # run anywhere else
- # If commands are not already set, use the defaults
- [ -z "$MAGIC_ENTER_GIT_COMMAND" ] && MAGIC_ENTER_GIT_COMMAND="git status -u ."
- [ -z "$MAGIC_ENTER_OTHER_COMMAND" ] && MAGIC_ENTER_OTHER_COMMAND="ls -lh ."
+magic-enter() {
+ # Only run MAGIC_ENTER commands when in PS1 and command line is empty
+ # http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#User_002dDefined-Widgets
+ if [[ -n "$BUFFER" || "$CONTEXT" != start ]]; then
+ return
+ fi
- if [[ -z $BUFFER ]]; then
- echo ""
- if git rev-parse --is-inside-work-tree &>/dev/null; then
- eval "$MAGIC_ENTER_GIT_COMMAND"
- else
- eval "$MAGIC_ENTER_OTHER_COMMAND"
- fi
- zle redisplay
+ if command git rev-parse --is-inside-work-tree &>/dev/null; then
+ BUFFER="$MAGIC_ENTER_GIT_COMMAND"
else
- zle accept-line
+ BUFFER="$MAGIC_ENTER_OTHER_COMMAND"
fi
}
-zle -N magic-enter
-bindkey "^M" magic-enter
+
+# Wrapper for the accept-line zle widget (run when pressing Enter)
+
+# If the wrapper already exists don't redefine it
+(( ! ${+functions[_magic-enter_accept-line]} )) || return 0
+
+case "$widgets[accept-line]" in
+ # Override the current accept-line widget, calling the old one
+ user:*) zle -N _magic-enter_orig_accept-line "${widgets[accept-line]#user:}"
+ function _magic-enter_accept-line() {
+ magic-enter
+ zle _magic-enter_orig_accept-line -- "$@"
+ } ;;
+ # If no user widget defined, call the original accept-line widget
+ builtin) function _magic-enter_accept-line() {
+ magic-enter
+ zle .accept-line
+ } ;;
+esac
+
+zle -N accept-line _magic-enter_accept-line