Re: @ Dotan: As requested (back in March, sorry): Hotkey launcher scripts

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Duncan posted on Sat, 27 Oct 2012 11:50:27 +0000 as excerpted:

> So four posts should follow, one each for the hkm menu script, the
> hotkeylookup.lst keymap file, and two example user menu scripts.

Here's hkm, the menu script.  Again:

1) There's some settings near the top you can change as desired.
2) If you change the name from hkm, change the invocation in hkl as well.
3) I put it in /usr/local/bin/, but anywhere on the path should work.
(Or you can change the invocation in hkl to an absolute path and won't
have to have it on your normal executable path, then.)
4) Don't forget to set the file executable.

I'm posting this as text, unwrapped.  Be sure your client isn't
rewrapping the lines when you copy the below to the file.  Also
be sure not to copy my sig to the file too, only the content between
the --- begin hkm --- and --- end hkm --- lines. =:^)

---------------- begin hkm --------------------
#!/bin/bash

# hkm (hotkey-menu)
# Two options possible:
# * debug as first will redirect the command's output to ~/hotkey.debug
# * Anything else as first parameter (or second if the first is debug)
#   will be interpreted as the menu file to use (there's a default, see settings).
#   If the menu file path isn't absolute, it's interpreted as relative
#   to $hotkeylistdir, see settings, below.
unset hotkeydebug
unset hotkeylistfile
if [[ $1 = debug ]] ; then
	hotkeydebug=1
	[[ $2 ]] && hotkeylistfile="$2"
elif [[ $1 ]]; then
	hotkeylistfile="$1"
fi


################################################################################
# Settings
########################################
# Prompt settings:
# Hotkey menu timeout delay, seconds (default=30)
querytimeout=30
# Post-launch timeout delay, seconds (default=3)
postlaunchtimeout=3

# Default key entered if prompt times out (default=<escape>)
defaultkey=$(echo -e \\e)

# Hotkey prompt
hotkeyprompt="Enter key from first column.\nAuto-cancel in $querytimeout sec.: "

# Comment the /second/ line below if you don't want $defaultkey to abort
unset abortondefaultkey
abortondefaultkey=yes
########################################
# Location of the (default) hotkey listfiles dir.
# * Listfile format is key<tabs>description<tabs>command<newline>,
#   thus allowing commands with options and arguments.
# * A line beginning with # is a comment.
# * The first two lines should be comments,
#   and are intended to be displayed as the
#   column headers when the list is displayed (below).
# * Blank lines are allowed for display formatting.
# * Settings defaulted here can be set per-file:
#   * as comments for backward compatibility
#   * keyed on the leading #%%
#   * post-setting comment optional):
#   #%%querytimeout=nnn		#comment
#   #%%postlaunchtimeout=nn	#comment
#   #%%defaultkey=xxxxxx	#comment
hotkeylistdir="${XDG_CONFIG_HOME:-~/.config}/hotkey.lst"

# The default hotkey list file.
# If the path isn't absolute,
# it is parsed as relative to $hotkeylistdir, above.
defaulthotkeylistfile=default

# Location of the hotkey-lookup list, for control code lookups.
# This matches typed codes such as bs (backspace), as found in the hotkey list,
# to the control codes the script gets from the machine.
# Format is textcode:machinecode<newline>
# Again, a line beginning with a # is a comment, blank lines allowed.
# No special processing of the path is done here, so best make it absolute.
hotkeylookupfile=/usr/local/etc/hotkeylookup.lst
################################################################################
################################################################################
# First, do some setup
# If defaulthotkeylistfile is relative, prepend hotkeylistdir
[[ ${defaulthotkeylistfile} = ${defaulthotkeylistfile#/} ]] && \
	defaulthotkeylistfile="${hotkeylistdir}/${defaulthotkeylistfile}"

# Do the same for hotkeylistfile if it was passed, default it if not.
if [[ $hotkeylistfile ]] ; then
	[[ ${hotkeylistfile} = ${hotkeylistfile#/} ]] && \
		hotkeylistfile="${hotkeylistdir}/${hotkeylistfile}"
else
	hotkeylistfile=$defaulthotkeylistfile
fi
unset defaulthotkeylistfile

# sed hotkeylistfile for per-file settings and evaluate them.
IFS="
"
perfilesettings=$(sed -n 's/^#%%//p' "$hotkeylistfile") #'
[[ $perfilesettings ]] && set $perfilesettings
for eachperfilesetting; do
	eval $eachperfilesetting 2>&-
done
unset IFS eachperfilesetting perfilesettings
################################################################################
# OK, now the real functionality!

# Display the first two lines of hotkeylistfile as column headers
head -2 $hotkeylistfile

# Grab the active lines out of hotkeylistfile, display them.
IFS=
hotkeylist=$(sed /^#/d $hotkeylistfile)
echo $hotkeylist
echo
unset IFS hotkeylistfile

# Display the prompt (takes one (perhaps modified) key, reply caught in $REPLY)
echo -ne "$hotkeyprompt"
read -r -N1 -d$defaultkey -t$querytimeout
[[ $? -gt 128 ]] && REPLY=$defaultkey
echo
unset querytimeout hotkeyprompt

# Was it $defaultkey, and does that mean abort?
[ "$abortondefaultkey" ] && [ "$REPLY" = "$defaultkey" ] && exit
unset defaultkey abortondefaultkey

# Grab the hotkeylookup file entries here.
lookuptable=$(sed /^#/d $hotkeylookupfile|sed /^$/d)
unset hotkeylookupfile

# Loop over lookuptable for a machine-key match to REPLY.
# If we get a match, reset REPLY to the corresponding text-key,
# so it'll match in the hotkey list matching loop, below.
for entry in $lookuptable; do
	# Extract the text and machine portions.
	text=$(echo $entry | cut -d: -f1)
	machine=$(echo $entry | cut -d: -f2)
	# Does the machine portion match our REPLY?
	# If so, remap REPLY to text portion, and break out of the loop
	[ "$machine" = "$REPLY" ] && REPLY=$text && break
done
echo Post lookup-loop REPLY=$REPLY.

################################################################################

# try to find a matching hotkey
IFS="
"
set -- $(echo "$hotkeylist")
unset IFS keymatch
for record; do
	# Extract the key and cmd, desc is for humans; we don't care.
	key=$(echo $record | cut -d" " -f1)
	cmd=$(echo $record | cut -d" " -f3-)
	# The simple case, single character match.
	# With the above lookuptable match loop and REPLY fudgery,
	# this should match (single) covered control chars as well.
	# Modified keys fudgery isn't yet implemented.
	[ "$key" = "$REPLY" ] && keymatch=yes && break
done

# If no match, abort
[ "$keymatch" ] || exit 0
unset hotkeylist record key keymatch REPLY

# OK, it was a match, and we should have our command in $cmd, so run it
# Do the actual launch in the background using nohup, so it won't die when
# we do.  Sleep a second to give it time to launch properly.
echo -e "Launching:\n$cmd"
trap '' SIGHUP
if [[ $hotkeydebug ]] ; then
		nohup $cmd & >| ~/hotkey.debug 2>&1
	else
		setsid $cmd &
fi
sleep $postlaunchtimeout

---------------------- end hkm ----------------------

-- 
Duncan - List replies preferred.   No HTML msgs.
"Every nonfree program has a lord, a master --
and if you use the program, he is your master."  Richard Stallman


___________________________________________________
This message is from the kde mailing list.
Account management:  https://mail.kde.org/mailman/listinfo/kde.
Archives: http://lists.kde.org/.
More info: http://www.kde.org/faq.html.


[Index of Archives]     [Trinity (TDE) Desktop Users]     [Fedora KDE]     [Fedora Desktop]     [Linux Kernel]     [Gimp]     [GIMP for Windows]     [Gnome]     [Yosemite Hiking]
  Powered by Linux