about summary refs log tree commit diff
path: root/Functions/Misc/sticky-note
diff options
context:
space:
mode:
authorBart Schaefer <barts@users.sourceforge.net>2008-01-13 17:37:46 +0000
committerBart Schaefer <barts@users.sourceforge.net>2008-01-13 17:37:46 +0000
commitcdbe7f188d6708785d0f215a3a5d5143f495ec85 (patch)
treebe9c1d85943c6a48d405248dc300fcc9aebe5c75 /Functions/Misc/sticky-note
parent7a678d803ce043c4837230cf8ff40ac8c9af4de1 (diff)
downloadzsh-cdbe7f188d6708785d0f215a3a5d5143f495ec85.tar.gz
zsh-cdbe7f188d6708785d0f215a3a5d5143f495ec85.tar.xz
zsh-cdbe7f188d6708785d0f215a3a5d5143f495ec85.zip
users/12426: post-it notes for zsh
Diffstat (limited to 'Functions/Misc/sticky-note')
-rw-r--r--Functions/Misc/sticky-note87
1 files changed, 87 insertions, 0 deletions
diff --git a/Functions/Misc/sticky-note b/Functions/Misc/sticky-note
new file mode 100644
index 000000000..840ae1037
--- /dev/null
+++ b/Functions/Misc/sticky-note
@@ -0,0 +1,87 @@
+#!/bin/zsh -f
+
+# A little zsh sticky-note ("post-it") application.  Sticky notes are
+# stored in the file named by the STICKYFILE variable (defaults to
+# $HOME/.zsticky).  The number of notes stored is STICKYSIZE (1000).
+#
+# May be bound as a widget:
+#    zle -N sticky-note
+# And/or run as a command:
+#    sticky-note
+#    sticky-note -b
+#    sticky-note -l ...
+# With the -l option, lists previous sticky notes.  Most options of the
+# "fc -l" command are supported, for selecting which notes to display.
+# The -b option is like "zed -b": installs keymaps/bindings only.
+#
+# Otherwise invokes the line editor with the previous notes available
+# as an editor history.  Two quick taps on the return/enter key finish
+# the note, or you can use use ^X^W as usual (ZZ in vicmd mode).
+
+# I encourage all you creative people to contribute enhancements ...
+# no-prize for the first person to put the most recent sticky note in
+# his PS1 string.
+
+emulate -LR zsh
+
+local STICKYFILE=${STICKYFILE:-$HOME/.zsticky}
+local STICKYSIZE=${STICKYSIZE:-1000}
+
+# If invoked as a widget, behave a bit like run-help
+if zle
+then
+  if [[ $CONTEXT = (cont|select|vared) ]]
+  then
+    zle -M "No stickies during ${${(z)PREBUFFER}[1]:-$CONTEXT}, sorry"
+    zle .beep
+  else
+    zle .push-line
+    BUFFER=sticky-note
+    zle .accept-line
+  fi
+  return 0
+fi
+
+(($+bg && $+fg)) || { autoload -U colors; colors }
+
+# Invoked as a command, behave like zed, but write a history file
+setopt nobanghist extendedhistory histignoredups
+fc -ap $STICKYFILE $STICKYSIZE $STICKYSIZE
+
+# With a -l option, list the existing sticky notes
+if [[ "$1" == -*l* ]]
+then
+  print -nr "$bg[yellow]$fg[black]"
+  fc -f "$@"
+  print -nr "$reset_color"
+  return 0
+fi
+
+# Set up keybindings (adapted from "zed")
+if ! bindkey -M sticky >& /dev/null
+then
+  bindkey -N sticky main
+  bindkey -M sticky ^X^W accept-line
+  bindkey -M sticky ^M^M accept-line	# Two quick RETs ends note
+  bindkey -M sticky ^M self-insert-unmeta
+fi
+if ! bindkey -M sticky-vicmd >& /dev/null 
+then
+  bindkey -N sticky-vicmd vicmd
+  bindkey -M sticky-vicmd ZZ accept-line
+fi
+
+[[ "$1" == -b ]] && return 0
+
+# Edit a new sticky note and add it to the STICKYFILE
+local sticky
+while vared -h -p "%{$bg[yellow]$fg[black]%}" -M sticky -m sticky-vicmd sticky
+do
+  {
+    print -s -- "$sticky"
+  } always {
+    (( TRY_BLOCK_ERROR = 0 ))
+  } && break
+  echo -n -e '\a'
+done
+return 0