summary refs log tree commit diff
path: root/Functions/Misc/sticky-note
blob: dee26cadebef4a97d0721327ee70ae279a4c2ef3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/zsh -fi

# 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).
#
# Load this file as a function:
#    autoload -U sticky-note
#
# It may then 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