Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add variable substitution to snippets #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 19 additions & 2 deletions texpander.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down