-
Notifications
You must be signed in to change notification settings - Fork 45
/
texpander.sh
executable file
·69 lines (53 loc) · 2.15 KB
/
texpander.sh
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
#!/bin/bash
# Version: 2.0
# Release: November 24, 2017
# Get window id, pass to getwindow pid to output the pid of current window
pid=$(xdotool getwindowfocus getwindowpid)
# Store text name of process based on pid of current window
proc_name=$(cat /proc/$pid/comm)
# If ~/.texpander directory does not exist, create it
if [ ! -d ${HOME}/.texpander ]; then
mkdir ${HOME}/.texpander
fi
# Store base directory path, expand complete path using HOME environemtn variable
base_dir=$(realpath "${HOME}/.texpander")
# Set globstar shell option (turn on) ** for filename matching glob patterns on subdirectories of ~/.texpander
shopt -s globstar
# Find regular files in base_dir, pipe output to sed
abbrvs=$(find "${base_dir}" -type f | sort | sed "s?^${base_dir}/??g")
# 'Echo'ing the options instead of passing them directly
# to zenity allows names like '+1' or '-1'
name=$(echo ${abbrvs} | tr ' ' '\n' | zenity --list --title=Texpander --width=275 --height=400 --column=Abbreviations)
path="${base_dir}/${name}"
if [ -f "${base_dir}/${name}" ]
then
if [ -e "$path" ]
then
# Preserve the current value of the clipboard
clipboard=$(xsel -b -o)
# Put text in primary buffer for Shift+Insert pasting
echo -n "$(cat "$path")" | xsel -p -i
# Put text in clipboard selection for apps like Firefox that
# insist on using the clipboard for all pasting
echo -n "$(cat "$path")" | xsel -b -i
# Paste text into current active window
sleep 0.3
xdotool key shift+Insert
# If you're having trouble pasting into apps, use xdotool
# to type into the app instead. This is a little bit slower
# but may work better with some applications.
#
# Make xdotool type RETURN instead of LINEFEED characters
# otherwise some apps like Gmail in Firefox won't recognize
# newline characters.
#
# To use this, comment out line #32 (xdotool key shift+Insert)
# and uncomment the line below.
#xdotool type -- "$(xsel -bo | tr \\n \\r | sed s/\\r*\$//)"
# Restore the original value of the clipboard
sleep 0.5
echo $clipboard | xsel -b -i
else
zenity --error --text="Abbreviation not found:\n${name}"
fi
fi