forked from leehblue/texpander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexpander.sh
executable file
·92 lines (72 loc) · 2.65 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/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" )
# save launch dir
launch_dir=$(pwd)
# move to base_dir
cd ${base_dir}
# search files and folders in base_dir
choices=$(ls -p --group-directories-first)
# let user select a file or a folder
selection=$(zenity --list --title=Texpander --width=275 --height=400 --column=Abbreviations $choices)
rel_path=""
# while the selection is a folder
while [[ "$selection" == */ ]]
do
rel_path="$rel_path$selection"
cd $selection
# search files and folders in selected dir
choices=$(ls -p --group-directories-first)
# let user select a file or a folder
selection=$(zenity --list --title=Texpander --width=275 --height=400 --column=Abbreviations $choices)
done
# return to launch dir
cd $launch_dir
rel_path="$rel_path$selection"
path="${base_dir}/${rel_path}"
if [ -f "$path" ]
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${rel_path}"
fi
fi