From fda6ad05f6b6ab4644e95d19a3f298a525cc2f3d Mon Sep 17 00:00:00 2001 From: Carl Cravens Date: Fri, 29 Jun 2018 14:29:47 -0400 Subject: [PATCH] add variable substitution to snippets --- README.md | 8 ++++++++ texpander.sh | 21 +++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2ff0a6d..09e7a2b 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,14 @@ The text expansion files reside in your `~/.texpander` directory and can be orga I have `crtl+space` assigned to run `~/bin/texpander.sh`. So, if I'm typing an email, it doesn't matter if I'm in gmail (using Firefox, Chrome, Opera, or Vivaldi), Thunderbird, Vim, or Nylas, the workflow is the same. I have a couple different email signatures that I use. If I am writing to somebody about Cart66, the [WordPress Shopping Cart plugin](https://cart66.com), I'll use my Cart66 signature. I have a file `~/.texpander/sig66.txt` that has all my contact info and so forth for Cart66. +You can perform variable substitution on expansion files, with optional default values. Repeated variables will only prompt once, but if you use a default, be sure it is on the first occurrence or it will be dropped. (The format is the same as TextExpander, so you can more easily import your Mac-using coworkers snippets.) + +``` +mkdir %filltext:name=DIRECTORY:default=root_tmpdir% +chmod 755 %filltext:name=DIRECTORY% +chown root. %filltext:name=DIRECTORY% +``` + ![Texpander - text snippets for Ubuntu](https://lee.blue/share/texpander-demo.gif) ### Setting Up Custom Keyboard Shortcuts diff --git a/texpander.sh b/texpander.sh index bdb98bc..4d80b77 100755 --- a/texpander.sh +++ b/texpander.sh @@ -31,15 +31,32 @@ if [ -f "${base_dir}/${name}" ] then if [ -e "$path" ] then + # Determine if the text requires variable substitution + if grep '%filltext' $path + then + # variable pattern is either + # "%filltext:name=VAR%" + # "%filltext:name=VAR:default=DEFAULT%" + subpat='' + while read var def; do + val=$(zenity --entry --title=Texpander --text="$var" --entry-text="$def") + # build a sed pattern to replace the filltext + subpat+="s/%filltext:name=$var\(:default=[^%]\+\)\?%/$val/;" + done < <(grep -E -o '%filltext:name=[[:alnum:]]+(:default=[[:alnum:][:space:]]+)?%' $path |sed 's/%//g; s/\(name=\|default=\|filltext:\)//g; y/:/ /;' |sort -u -k1,1) + # grep returns just the matched text + # sed cleans up and returns lines of "name default text" + # sort removes duplicate names + fi + # Preserve the current value of the clipboard clipboard=$(xsel -b -o) # Put text in primary buffer for Shift+Insert pasting - echo -n "$(tac "$path")" | tac | xsel -p -i + echo -n "$(cat "$path")" | sed "$subpat" | xsel -p -i # Put text in clipboard selection for apps like Firefox that # insist on using the clipboard for all pasting - echo -n "$(tac "$path")" | tac | xsel -b -i + echo -n "$(cat "$path")" | sed "$subpat" | xsel -b -i # Paste text into current active window sleep 0.3