Advertisement
KosIvantsov

OmegaT tag insert

Mar 20th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.29 KB | None | 0 0
  1. #!/bin/bash
  2. ##########
  3. # Tag Insert — Version 0.1, 20090517
  4. # By Roberto Bechtlufft
  5. # http://www.bechtranslations.com.br
  6. # Modified by Kos Ivantsov
  7. # http://libretraduko.wordpress.com
  8. ##########
  9. # Requires:
  10. # cat, sed, grep, tr, xte, xclip
  11. # zenity (to locate OT installation dir),
  12. # notify-send (to show selected tag)
  13. #
  14. # xte can be substituted with xmacro or xdotool
  15. # xclip can be substituted with xsel or xclipboard
  16. ##########
  17.  
  18. ##########
  19. # create a file that contains OT installation directory
  20. # it's used later on to find OT icon for notifications
  21. # if there's no need for icons, this part can be deleted
  22. if [ ! -f $HOME/.omegat/ompath ]; then
  23.     dirname `zenity --file-selection --title="Navigate to OT directory" \
  24.     --text="Select OmegaT.jar from the folder where OmegaT is installed"` \
  25.     > $HOME/.omegat/ompath
  26. fi
  27. OM_PATH=`cat $HOME/.omegat/ompath`
  28. ##########
  29.  
  30. ##########
  31. # shows currently selected tag with libnotify popup
  32. function notify {
  33.     notify-send --icon="$OM_PATH/images/OmegaT.png" `cat $currenttag`
  34. }
  35. ##########
  36.  
  37. ##########
  38. # checks if OT is running, used later on during invocation of the
  39. # script either to execute, or to exit
  40. function test_omegat () {
  41.     export OTPID=`ps ax|grep OmegaT.jar|grep java|cut -d " " -f 2`
  42.     if [ -z $OTPID ]; then
  43.     echo "NORUN"
  44.     else
  45.     echo "RUN"
  46.     fi
  47. }
  48. ##########
  49.  
  50. ##########
  51. # set up some variables and files to store tags at different stages
  52. #
  53. source=$HOME/.omegat/script/source.txt
  54. tagsdir=/tmp/ottags
  55. tags=$tagsdir/tags
  56. tagnumber=$tagsdir/tagnumber
  57. currenttag=$tagsdir/currenttag
  58. pidfile=$tagsdir/pidfile
  59. ##########
  60.  
  61. ##########
  62. # Create temp dir for temp files with tags
  63. if [ ! -d $tagsdir ]; then
  64. mkdir -p $tagsdir
  65. fi
  66. ##########
  67.  
  68. ##########
  69. # If the script is run without parameters, it checks if another instance
  70. # is already running, and if so, exits, otherwise proceeds
  71. # and then loops in the background
  72. if [ -z $1 ]; then
  73.     if [ -e $pidfile ]; then
  74.     rm $pidfile
  75.     fi
  76. echo $$ > $pidfile
  77. export PID=`cat $pidfile`
  78. test `pgrep taginsert|grep -v $PID|wc -l` -ge 2 && rm $pidfile && exit 0
  79. #
  80. # then it watches (every 2 seconds) if OT's exported source segment
  81. # has changed
  82. # if changed, gather all the tags
  83. # (one on each line with preceding number) into $tags
  84. # set $tagnumber to "1" and set $currenttag to string #1 from $tags
  85. # (without preceding digit(s))
  86. #
  87. old=`stat $source | grep Modify`
  88. while [ i != 0 ]; do
  89.     sleep 2
  90.     [ "$(test_omegat)" = "NORUN" ] && rm -r $tagsdir && exit 0
  91.     new=`stat $source | grep Modify`
  92.     if [ "$old" != "$new" ]; then
  93.         rm -r $tagsdir/*
  94.         old="$new"
  95.         grep -o "<.[^>]*>" $source |grep -n "<" > $tags
  96.         echo "1" > $tagnumber
  97.         grep "^1:" $tags | cut -d ":" -f 2 > $currenttag
  98.     fi
  99. done
  100. fi
  101. ##########
  102.  
  103. ##########
  104. # if invoked with parameters, then select prev./next tag
  105. # selection goes in loop, ie next to the last is the first,
  106. # and previous to the first is the last
  107. # Upon changing selection popup notification is brought up,
  108. # showing OT's icon and currently selected tag
  109. # Before inserting current tag, clipboard is stored into a variable,
  110. # and upon inserting the tag, it gets restored.
  111. case $1 in
  112.     previous)
  113.     [ "$(test_omegat)" = "NORUN" ] && rm -r $tagsdir && exit 0
  114.     export numberoftags=`cat $tags|wc -l`
  115.     if [ `cat $tagnumber` -ge 2 ]; then
  116.     expr `cat $tagnumber` - 1 > $tagnumber
  117.     else
  118.     echo $numberoftags > $tagnumber
  119.     fi
  120.     tagnumber=`cat $tagnumber`
  121.     grep "^$tagnumber:" $tags | cut -d ":" -f 2 > $currenttag
  122.     notify
  123.     ;;
  124.     next)
  125.     [ "$(test_omegat)" = "NORUN" ] && rm -r $tagsdir && exit 0
  126.     export numberoftags=`cat $tags|wc -l`
  127.     if [ `cat $tagnumber` -lt ${numberoftags} ]; then
  128.     expr `cat $tagnumber` + 1 > $tagnumber
  129.     else
  130.     echo "1" > $tagnumber
  131.     fi
  132.     tagnumber=`cat $tagnumber`
  133.     grep "^$tagnumber:" $tags | cut -d ":" -f 2 > $currenttag
  134.     notify
  135.     ;;
  136.     insert)
  137.     [ "$(test_omegat)" = "NORUN" ] && rm -r $tagsdir && exit 0
  138.     export PREVCLIP=`xclip -o -selection clipboard`
  139.     cat $currenttag | tr -d '\n' |xclip -i -selection clipboard
  140.     xte "keydown Control_L" "key v" "keyup Control_L"
  141.     sleep 2
  142.     echo "$PREVCLIP"| xclip -i -selection clipboard
  143.     ;;
  144.     *)
  145.     exit 0
  146.     ;;
  147. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement