Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ##########
- # Tag Insert — Version 0.1, 20090517
- # By Roberto Bechtlufft
- # http://www.bechtranslations.com.br
- # Modified by Kos Ivantsov
- # http://libretraduko.wordpress.com
- ##########
- # Requires:
- # cat, sed, grep, tr, xte, xclip
- # zenity (to locate OT installation dir),
- # notify-send (to show selected tag)
- #
- # xte can be substituted with xmacro or xdotool
- # xclip can be substituted with xsel or xclipboard
- ##########
- ##########
- # create a file that contains OT installation directory
- # it's used later on to find OT icon for notifications
- # if there's no need for icons, this part can be deleted
- if [ ! -f $HOME/.omegat/ompath ]; then
- dirname `zenity --file-selection --title="Navigate to OT directory" \
- --text="Select OmegaT.jar from the folder where OmegaT is installed"` \
- > $HOME/.omegat/ompath
- fi
- OM_PATH=`cat $HOME/.omegat/ompath`
- ##########
- ##########
- # shows currently selected tag with libnotify popup
- function notify {
- notify-send --icon="$OM_PATH/images/OmegaT.png" `cat $currenttag`
- }
- ##########
- ##########
- # checks if OT is running, used later on during invocation of the
- # script either to execute, or to exit
- function test_omegat () {
- export OTPID=`ps ax|grep OmegaT.jar|grep java|cut -d " " -f 2`
- if [ -z $OTPID ]; then
- echo "NORUN"
- else
- echo "RUN"
- fi
- }
- ##########
- ##########
- # set up some variables and files to store tags at different stages
- #
- source=$HOME/.omegat/script/source.txt
- tagsdir=/tmp/ottags
- tags=$tagsdir/tags
- tagnumber=$tagsdir/tagnumber
- currenttag=$tagsdir/currenttag
- pidfile=$tagsdir/pidfile
- ##########
- ##########
- # Create temp dir for temp files with tags
- if [ ! -d $tagsdir ]; then
- mkdir -p $tagsdir
- fi
- ##########
- ##########
- # If the script is run without parameters, it checks if another instance
- # is already running, and if so, exits, otherwise proceeds
- # and then loops in the background
- if [ -z $1 ]; then
- if [ -e $pidfile ]; then
- rm $pidfile
- fi
- echo $$ > $pidfile
- export PID=`cat $pidfile`
- test `pgrep taginsert|grep -v $PID|wc -l` -ge 2 && rm $pidfile && exit 0
- #
- # then it watches (every 2 seconds) if OT's exported source segment
- # has changed
- # if changed, gather all the tags
- # (one on each line with preceding number) into $tags
- # set $tagnumber to "1" and set $currenttag to string #1 from $tags
- # (without preceding digit(s))
- #
- old=`stat $source | grep Modify`
- while [ i != 0 ]; do
- sleep 2
- [ "$(test_omegat)" = "NORUN" ] && rm -r $tagsdir && exit 0
- new=`stat $source | grep Modify`
- if [ "$old" != "$new" ]; then
- rm -r $tagsdir/*
- old="$new"
- grep -o "<.[^>]*>" $source |grep -n "<" > $tags
- echo "1" > $tagnumber
- grep "^1:" $tags | cut -d ":" -f 2 > $currenttag
- fi
- done
- fi
- ##########
- ##########
- # if invoked with parameters, then select prev./next tag
- # selection goes in loop, ie next to the last is the first,
- # and previous to the first is the last
- # Upon changing selection popup notification is brought up,
- # showing OT's icon and currently selected tag
- # Before inserting current tag, clipboard is stored into a variable,
- # and upon inserting the tag, it gets restored.
- case $1 in
- previous)
- [ "$(test_omegat)" = "NORUN" ] && rm -r $tagsdir && exit 0
- export numberoftags=`cat $tags|wc -l`
- if [ `cat $tagnumber` -ge 2 ]; then
- expr `cat $tagnumber` - 1 > $tagnumber
- else
- echo $numberoftags > $tagnumber
- fi
- tagnumber=`cat $tagnumber`
- grep "^$tagnumber:" $tags | cut -d ":" -f 2 > $currenttag
- notify
- ;;
- next)
- [ "$(test_omegat)" = "NORUN" ] && rm -r $tagsdir && exit 0
- export numberoftags=`cat $tags|wc -l`
- if [ `cat $tagnumber` -lt ${numberoftags} ]; then
- expr `cat $tagnumber` + 1 > $tagnumber
- else
- echo "1" > $tagnumber
- fi
- tagnumber=`cat $tagnumber`
- grep "^$tagnumber:" $tags | cut -d ":" -f 2 > $currenttag
- notify
- ;;
- insert)
- [ "$(test_omegat)" = "NORUN" ] && rm -r $tagsdir && exit 0
- export PREVCLIP=`xclip -o -selection clipboard`
- cat $currenttag | tr -d '\n' |xclip -i -selection clipboard
- xte "keydown Control_L" "key v" "keyup Control_L"
- sleep 2
- echo "$PREVCLIP"| xclip -i -selection clipboard
- ;;
- *)
- exit 0
- ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement