Skip to content

Instantly share code, notes, and snippets.

Created January 22, 2014 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8554590 to your computer and use it in GitHub Desktop.
Save anonymous/8554590 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# edit_textbuf
#
# Grab the current text buffer, and open it in a editor window, when
# finshed replace the contents of the original text buffer.
#
###
#
# Orig Author: Ben Collerson {benc[at]bur[dot]st} 24 Oct 2004
# Fixes by: Shreevatsa R 28 Dec 2006
# Modified by: Anthony Thyssen 10 Nov 2010
#
# CAVAT:
#
# The script will fail if a specific text buffer is not actually active,
# essentually you may get and edit the last text buffer, but it may not
# paste back into place, or if during edit you switch text buffers in the
# same application it may replace the test in a different text buffer!
#
# INSTALLATION:
#
# Software Requirements
# "xprop" find the actualy current window
# "xte" from xautomation for XEvent injection ALT: "xdotool"
# "xwit" controlling client focus ALT: "wmctrl" "xdotool"
# "xsel" X selection handling ALT: "xselection"
#
# Other than that you need to set the "editor" below to something that
# launches a "X Window Editor", see below for examples.
#
# Then bind this script with some specific key sequence, typically via
# the window manger being used.
#
# For fluxbox/bbkeys use something like this
#
# KeyToGrab(e), WithModifier(Mod1), WithAction(ExecCommand),
# DoThis(edit_textbuffer)
#
# For openbox somethng like this
#
# <keybind key="W-e">
# <action name="Execute">
# <!-- personal script to edit a text buffer -->
# <command>edit_textbuf</command>
# </action>
# </keybind>
#
####
PROGNAME=`type $0 | awk '{print $3}'` # search for executable on path
PROGNAME=`basename $PROGNAME` # base name of program
# --------------------------------------------------------------------------
# User Configuration Section
# The prefered X Window Editor to use...
# editor="xterm -g 80x24 -name $PROGNAME -n $PROGNAME -T $PROGNAME \
# -e ${VISUAL:=vi}"
editor="konsole --nofork -e vim"
# Alternative suggestions.
# editor="/bin/gvim -f"
# editor="/bin/emacs"
# editor="/usr/bin/emacsclient -a emacs"
# Note the editor must not fork (which is why you need: gvim -f).
# When the editor finished the saved text is returned to the textbuffer.
#
# --------------------------------------------------------------------------
# Get the current windows information
winid=`xprop -root _NET_ACTIVE_WINDOW | sed 's/.* //'`
wintitle=`xprop -id $winid WM_CLASS | sed 's/.*= "//; s/", .*//'`
# This script works by actually simulating the keystrokes that programs
# use for Select-All, Copy, and Paste.
#
# Different programs use different ones; so we use $wintitle to decide
# what to do. The common browsers (Firefox, Mozilla, Konqueror, Opera)
# seem to only differ in what they use for Select-All; so the rest of
# the functions are constant functions here. Write your own functions
# below if the ones here don't apply to your programs.
xte_selectall()
{
case "$wintitle" in
Navagator)
echo '
| usleep 50000
| keydown Alt_L
| key a
| keyup Alt_L
' | sed -n '/^ *|/!d; s/^ *|//; s/^ //; p' | xte
# xdotool windowfocus -sync window=$1 alt+a
;;
*)
echo '
| usleep 50000
| keydown Control_L
| key a
| keyup Control_L
' | sed -n '/^ *|/!d; s/^ *|//; s/^ //; p' | xte
# xdotool windowfocus -sync window=$1 ctrl+l
;;
esac
}
xte_copy()
{
echo '
| usleep 50000
| keydown Control_L
| key c
| keyup Control_L
' | sed -n '/^ *|/!d; s/^ *|//; s/^ //; p' | xte
# xdotool windowfocus -sync window=$1 ctrl+l
}
xte_paste()
{
echo '
| usleep 50000
| keydown Control_L
| key v
| keyup Control_L
' | sed -n '/^ *|/!d; s/^ *|//; s/^ //; p' | xte
# xdotool windowfocus -sync window=$1 ctrl+v
}
xte_home()
{
echo '
| usleep 50000
| keydown Control_L
| key Home
| keyup Control_L
' | sed -n '/^ *|/!d; s/^ *|//; s/^ //; p' | xte
# xdotool windowfocus -sync window=$1 ctrl+Home
}
# ---------------------------------------------------------------
# Main Routine
# Setup temporary file
umask 77
tmp=`mktemp "${TMPDIR:-/tmp}/$PROGNAME.XXXXXXXXXX"` ||
{ echo >&2 "$PROGNAME: Unable to create temporary file"; exit 10;}
trap 'rm -f "$tmp"' 0
trap 'exit 2' 1 2 3 15
# copy text using application keyboard short-cuts
xte_selectall $winid
xte_copy $winid
xte_home $winid
xsel -b -o > "$tmp"
# xselection CLIPBOARD > "$tmp"
# xclip -selection clipboard -o > "$tmp"
# invoke the editor, wait for it to finish
$editor "$tmp"
# applications generally use the clipboard for paste!
xsel -b -i < "$tmp"
# xselection CLIPBOARD - < "$tmp"
# some applications use the primary selection
# xselection PRIMARY - < "$tmp"
# xsel -p -i < "$tmp"
# activates the original window
# Not needed if xdotool is used above
xwit -focus -id $winid
# wmctrl -ia $winid
# paste text using application keyboard short-cuts
xte_selectall $winid
xte_paste $winid
xte_home $winid
# finished - auto clean up
# -------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment