#!/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 while you edit you switch text buffers in # the original application it may replace the text in that text buffer! # # INSTALLATION: # # See the dependacy check below for what needs to be installed. # # 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 # # # # # edit_textbuf # # # #### PROGNAME=`type $0 | awk '{print $3}'` # search for executable on path PROGNAME=`basename $PROGNAME` # base name of program # -------------------------------------------------------------------------- # User Configuration Section #editor=xeditor # The prefered X Window Editor to use... editor="xterm -g 80x24 -name $PROGNAME -n $PROGNAME -T $PROGNAME \ -e ${VISUAL:=vi}" # 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. Error() { # Just output an error condition and exit (no usage) error_message "$PROGNAME" "$@" exit 2 } for i in xdotool xsel xterm; do type $i >/dev/null 2>&1 || Error "Required program dependency \"$i\" missing" done # -------------------------------------------------------------------------- # 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 may need to 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. macro_key_up() { # ensure the keys that may have initiated the macro are no longer pressed xdotool keyup Super_L keyup Control_L keyup Alt_L keyup e } selectall() { case "$wintitle" in Navagator) xdotool windowfocus -sync window=$1 key alt+a ;; *) xdotool windowfocus -sync window=$1 key ctrl+a ;; esac } copy() { xdotool windowfocus -sync window=$1 key ctrl+c } paste() { xdotool windowfocus -sync window=$1 key ctrl+v } home() { xdotool windowfocus -sync window=$1 key 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 macro_key_up # precaution selectall $winid # select all the text in the current focus copy $winid # copy it to clipboard #home $winid # place curcour at home position (why?) # pause a moment sleep .1 # Get the current clipboard xsel -b > "$tmp" # xclip -o -sel c > "$tmp" # xselection CLIPBOARD > "$tmp" # ---------------------------------------- # invoke the editor, wait for it to finish $editor "$tmp" # ---------------------------------------- # Copy edited text into clipboard # applications generally use the clipboard for paste! xsel -b -i < "$tmp" # xclip -i -sel c < "$tmp" # xselection CLIPBOARD - < "$tmp" # some applications use the primary selection # xsel -i -p < "$tmp" # xclip -sel p "$tmp" # xselection PRIMARY - < "$tmp" # paste text using application keyboard short-cuts selectall $winid # select all the text paste $winid # paste text from clipboard #home $winid # place cursor at start (Why?) # finished - auto clean up # -------------------------------------------------------------------