-
Notifications
You must be signed in to change notification settings - Fork 1
/
fzf-marker.plugin.zsh
73 lines (68 loc) · 2.57 KB
/
fzf-marker.plugin.zsh
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
# vim: fdm=marker ts=2 sw=2 sts=2 expandtab
#===================================================================================================
# fzf-marker: The terminal command
# Tweak https://github.com/pindexis/marker.git
#
# Author: hanson <[email protected]>
# Github: https://github.com/liangguohaun
# Last Modified: 2017-05-26 10:10
#
# How to install?
# 1.treat it as oh-my-zsh plugin
# $ mkdir .oh-my-zsh/custom/plugins/fzf-marker
# $ mv fzf-marker.plugin.zsh .oh-my-zsh/custom/plugins/fzf-marker
# config .zshrc file in line plugins=(... fzf-marker)
# 2.manual install
# [[ -s "fzf-marker.plugin.zsh" ]] && source "fzf-marker.plugin.zsh"
#
# Keybind:
# 1.ctrl+space:
# show markers from fzf tty
# OR
# replace maker into real value if '{{' exists in cmdline
# 2.ctrl+v: move to next placeholder and set default val in {{}}
#
# Environment:
# # must before: export ZSH=$HOME/.oh-my-zsh if treat it as plugin
# export FZF_MARKER_CONF_DIR=~/.config/marker
# export FZF_MARKER_COMMAND_COLOR='\x1b[38;5;255m'
# export FZF_MARKER_COMMENT_COLOR='\x1b[38;5;8m'
# export FZF_MARKER_MAIN_KEY='\C-@'
# export FZF_MARKER_PLACEHOLDER_KEY='\C-v'
#
#===================================================================================================
# marker templete select
_fzf_marker_main_widget() {
if echo "$BUFFER" | grep -q -P "{{"; then
_fzf_marker_placeholder
else
local selected
if selected=$(cat ${FZF_MARKER_CONF_DIR:-~/.config/marker}/*.txt |
sed -e "s/\(^[a-zA-Z0-9_-]\+\)\s/${FZF_MARKER_COMMAND_COLOR:-\x1b[38;5;255m}\1\x1b[0m /" \
-e "s/\s*\(#\+\)\(.*\)/${FZF_MARKER_COMMENT_COLOR:-\x1b[38;5;8m} \1\2\x1b[0m/" |
fzf --bind 'tab:down,btab:up' --height=80% --ansi -q "$LBUFFER"); then
LBUFFER=$(echo $selected | sed 's/\s*#.*//')
fi
zle redisplay
fi
}
_fzf_marker_placeholder() {
local strp pos placeholder
strp=$(echo $BUFFER | grep -Z -P -b -o "\{\{[^\{\}]+\}\}")
strp=$(echo "$strp" | head -1)
pos=$(echo $strp | cut -d ":" -f1)
placeholder=${strp#*:}
if [[ -n "$1" ]]; then
BUFFER=$(echo -E $BUFFER | sed -e "s/{{//" -e "s/}}//")
CURSOR=$(($pos + ${#placeholder} - 4))
else
placeholder=$(echo ${placeholder//\//\\/})
BUFFER=$(echo -E $BUFFER | sed "s/$placeholder//")
CURSOR=pos
fi
}
_fzf_marker_placeholder_widget() { _fzf_marker_placeholder "defval" }
zle -N _fzf_marker_main_widget
zle -N _fzf_marker_placeholder_widget
bindkey "${FZF_MARKER_MAIN_KEY:-\C-@}" _fzf_marker_main_widget
bindkey "${FZF_MARKER_PLACEHOLDER_KEY:-\C-v}" _fzf_marker_placeholder_widget