joplin-snippy/snippy

140 lines
4.7 KiB
Plaintext
Raw Normal View History

2016-11-01 16:27:13 -04:00
#!/usr/bin/env bash
# video demo at: http://www.youtube.com/watch?v=90xoathBYfk
2016-11-05 03:41:50 -04:00
# augmented by barbuk: https://github.com/BarbUk/dotfiles/blob/master/bin/snippy
# . restore current clipboard
# . {clipboard} placeholder to use current clipboard in snippet
# . {cursor} placeholder to place the cursor
# . ##noparse header in snippet to not parse
# . execute command begining by $
#
2016-11-01 16:27:13 -04:00
# augmented by "opennomad": https://gist.github.com/opennomad/15c4d624dce99066a82d
# originally written by "mhwombat": https://bbs.archlinux.org/viewtopic.php?id=71938&p=2
# Based on "snippy" by "sessy"
# (https://bbs.archlinux.org/viewtopic.php?id=71938)
#
# You will also need "dmenu", "zenity", "xsel" and "xdotool". Get them from your linux
# distro in the usual way.
#
# To use:
# 1. Create the directory ~/.snippy
#
# 2. Create a file in that directory for each snippet that you want.
# The filename will be used as a menu item, so you might want to
# omit the file extension when you name the file.
#
# TIP: If you have a lot of snippets, you can organise them into
# subdirectories under ~/.snippy.
#
# TIP: The contents of the file will be pasted asis, so if you
# don't want a newline at the end when the text is pasted, don't
# put one in the file.
#
# 3. Bind a convenient key combination to this script.
#
# TIP: If you're using XMonad, add something like this to xmonad.hs
# ((mod4Mask, xK_s), spawn "/path/to/snippy")
#
# 4. Set variables in ~/.snippy.conf. for example
# MENU_ENGINE="dmenu"
# DMENU_ARGS=' -p snippy -fn arial -sf green'
# ZENITY_ARGS='--list --hide-header --column=Snippet --text=snippy'
# GUIPASTE="xdotool key ctrl+v"
# CLIPASTE="xdotool key ctrl+shift+v"
DIR=${HOME}/.snippy
2017-11-07 14:41:07 -05:00
MENU_ENGINE="rofi"
2017-11-07 14:57:18 -05:00
DMENU_ARGS='-dmenu -i -sort -lines 25'
2016-11-01 16:27:13 -04:00
# if nothing happens, try "xdotool click 2", "xdotool key ctrl+v" or "xdotool key ctrl+shift+v"
GUIPASTE="xdotool key ctrl+v"
CLIPASTE="xdotool key ctrl+shift+v"
TMPFILE="/tmp/.snippy.tmp"; :>$TMPFILE
MENU_ARGS=${DMENU_ARGS}
# smarty like template engine which executes inline bash in (bashdown) strings (replaces variables with values e.g.)
# @link http://github.com/coderofsalvation/bashdown
# @dependancies: sed cat
# @example: echo 'hi $NAME it is $(date)' | bashdown
# fetches a document and interprets bashsyntax in string (bashdown) templates
# @param string - string with bash(down) syntax (usually surrounded by ' quotes instead of ")
bashdown(){
while IFS= read -r line; do
2016-11-01 16:27:13 -04:00
line="$(eval "printf -- \"$( printf "%s" "$line" | sed 's/"/\\"/g')\"")";
echo -e "$line"
2018-04-22 01:50:52 -04:00
done
2016-11-01 16:27:13 -04:00
}
2018-02-01 14:49:44 -05:00
is_gui(){
name="$(xprop -id "$(xdotool getwindowfocus)" WM_CLASS | cut -d'"' -f2 | tr '[:upper:]' '[:lower:]')"
[[ "$name" =~ term|tilda ]] && return 1
return 0
}
2016-11-01 16:27:13 -04:00
run(){
cd "${DIR}" || exit
# Use the filenames in the snippy directory as menu entries.
# Get the menu selection from the user.
2017-06-10 17:41:31 -04:00
# shellcheck disable=SC2086
2017-11-07 14:41:07 -05:00
FILE=$(find -L . -type f | grep -v '^\.$' | sed 's!\.\/!!' | grep -vE '\.git/|\.gitconfig|\.gitkeep' | ${MENU_ENGINE} ${MENU_ARGS} -p ' ')
2016-11-01 16:27:13 -04:00
# just return if nothing was selected
[[ -z "${FILE}" ]] && return 1
if [ -f "${DIR}/${FILE}" ]; then
# Put the contents of the selected file into the paste buffer.
# don't parse file with the ##noparse header
if grep -qE "^##noparse" "${FILE}"; then
content="$(tail -n +2 "${DIR}/${FILE}")"
2016-12-09 04:11:52 -05:00
else
content="$(bashdown < "${DIR}/${FILE}")"
fi
printf "%s" "$content" > $TMPFILE
else [[ ${FILE} =~ ^$ ]]
${FILE##*$} 2>/dev/null > $TMPFILE # execute as bashcommand
2016-11-01 16:27:13 -04:00
fi
2016-11-02 12:58:26 -04:00
local current_clip cursor
2016-11-02 12:58:26 -04:00
current_clip=$(xsel -b)
2017-07-28 02:43:26 -04:00
# define cursor at 0, we don't need to go up if there is no {cursor}
cursor=0
2016-11-05 03:35:50 -04:00
2016-11-02 12:58:26 -04:00
# replace {clipboard} by the cliboard comtent
2017-07-28 02:43:26 -04:00
sed -i -e "s~{clipboard}~${current_clip}~g" "$TMPFILE"
2016-11-02 12:58:26 -04:00
2016-11-05 03:35:50 -04:00
# Check if there is a {cursor} placeholder
if grep -qF '{cursor}' "$TMPFILE";then
local file_lines
2016-11-05 03:35:50 -04:00
# retrieve the line
cursor=$(grep -n '{cursor}' "$TMPFILE" | cut -d: -f1)
2016-11-05 03:35:50 -04:00
# calculate snippet total lines
file_lines=$(wc -l < "$TMPFILE")
2016-11-05 03:35:50 -04:00
# determine the number of line to go
cursor=$((file_lines - cursor))
2016-11-05 03:35:50 -04:00
# remove the placeholder
sed -i -e "s/{cursor}//g" "$TMPFILE"
fi
2016-11-02 12:58:26 -04:00
xsel -b --input < "$TMPFILE"
2016-11-01 16:27:13 -04:00
# Paste into the current application.
2018-02-01 14:49:44 -05:00
if is_gui; then
2016-11-01 16:27:13 -04:00
${GUIPASTE}
2016-11-05 03:35:50 -04:00
if [[ $cursor -gt 0 ]]; then
local keyup
until [ $cursor -eq 0 ]; do
keyup+="Up "
2018-02-01 14:49:44 -05:00
((cursor-=1))
2017-06-10 17:41:31 -04:00
# shellcheck disable=SC2086
2016-12-12 11:25:41 -05:00
xdotool key --delay -0.1 $keyup
done
2016-11-05 03:35:50 -04:00
fi
2016-11-01 16:27:13 -04:00
else
${CLIPASTE}
fi
2016-11-02 12:58:26 -04:00
echo -ne "$current_clip" | xsel -b --input
2016-11-01 16:27:13 -04:00
}
run