home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / the25.zip / the25O_w32.zip / spell.the < prev    next >
Text File  |  1997-09-10  |  9KB  |  269 lines

  1. /*
  2. $Id: spell.the 2.1 1995/06/24 16:28:29 MH Rel MH $
  3. */
  4. /***********************************************************************/
  5. /* Description: REXX macro to check spelling of words in a target.     */
  6. /* Syntax:      spell target                                           */
  7. /* Notes:       This macro spells words in the lines specified in the  */
  8. /*              target.                                                */
  9. /*              Full XEDIT/KEDIT/THE targets are supported include WORD*/
  10. /*              (WORD option not yet supported)                        */
  11. /*              This macro will only work with International Ispell    */
  12. /*              v 3.1 or above.                                        */
  13. /***********************************************************************/
  14. Trace o
  15. If batch() Then
  16.    Do
  17.      'emsg This macro cannot be run in batch'
  18.      Exit 1
  19.    End
  20. arg1 = Arg(1)
  21. noargs = Arg()
  22. 'preserve'
  23. time_to_quit = 'NO'
  24. spell_command = 'ispell -a'
  25. no_match_line = "'A' to add, 'E' to edit, 'I' to ignore or 'Q' to quit."
  26. If noargs = 0 Then arg1 = '1'               /* no args - assume 1 line */
  27. forward = 1                  /* assume direction is forward by default */
  28. 'EXTRACT /LINE/SIZE/STAY/FTYPE/FNAME/CURLINE/RESERVED */BLOCK/ZONE/'    /* get various stuff */
  29. reserved_start = curline.2
  30. current_line = line.1                   /* save current line for later */
  31. left_col = zone.1
  32. right_col = zone.2
  33. If arg1 = 'word' | arg1 = 'WORD' Then Call spell_word
  34. Else Call spell_target
  35.  
  36. /* put things back the way they were */
  37. 'reserved * off'
  38. Do i = 1 To reserved.0      /* restore any reserved lines as they were */
  39.   'reserved' reserved.i
  40. End
  41. 'reset block'
  42. 'zone' zone.1 zone.2
  43. 'restore'
  44. Return
  45.  
  46. /*---------------------------------------------------------------------*/
  47. spell_target:
  48. /*---------------------------------------------------------------------*/
  49. reply = valid_target(arg1)
  50. If reply = 'ERROR' Then
  51.    Do
  52.      'EMSG Error: 17 Invalid target' arg1
  53.      'restore'
  54.      Exit
  55.    End
  56. If reply = 'NOTFOUND' Then
  57.    Do
  58.      'EMSG Error: 17 Target not found' arg1
  59.      'restore'
  60.      Exit
  61.    End
  62. If Translate(arg1) = 'BLOCK' Then
  63.    Do
  64.      'EXTRACT /BLOCK/'
  65.      If block.1 \= 'LINE' Then
  66.         Do
  67.           left_col = block.3
  68.           right_col = block.5
  69.         End
  70.    End
  71. start_line = Word(reply,1)                        /* get starting line */
  72. nolines = Word(reply,2)                         /* get number of lines */
  73. If nolines < 0 Then Do                /* if target before current line */
  74.    forward = 0                    /* indicate direction to be backward */
  75.    nolines = nolines * -1                     /* make nolines positive */
  76. End
  77. 'cursor cmdline'
  78. ':'||start_line                                    /* go to first line */
  79. totlines = 0                             /* reset changed line counter */
  80. Do Forever                              /* for each line to target ... */
  81.    If nolines = 0 Then Leave
  82.    respell_current_line = 'NO'
  83.    'EXTRACT /CURLINE/TOF/EOF/LINE/'       /* get current line contents, etc.*/
  84.    If tof.1 = 'ON',                    /* ignore line if on TOF or EOF */
  85.    |  eof.1 = 'ON' Then Nop
  86.    Else
  87.      Do
  88.        line = Translate(curline.3,,'`"1234567890    -=\~!@#$%^&*()_+|[]{};:,.<>/?',' ')
  89.        wordidx = 0
  90.        Do i = 1 To Words(line)
  91.           word_start_col = Wordindex(line,i)
  92.           If word_start_col >= left_col & word_start_col <= right_col Then
  93.              Do
  94.                wordidx = wordidx + 1
  95.                word_in_line.wordidx = Word(line,i)
  96.                word_start.wordidx = Wordindex(line,i)
  97.              End
  98.        End
  99.        word_in_line.0 = wordidx
  100.        word_start.0 = wordidx
  101.        Call RunSpellCommand "word_in_line." "word_out_line." "stderr."
  102.        Call TidyOutput
  103.        Do i = 1 To word_out_line.0
  104.           Parse Var word_out_line.i cmd . num . ':' wordlist
  105.           Select
  106.             When cmd = '*' | cmd = '+' | cmd = '-' Then Iterate
  107.             When cmd = '#' Then Call no_match
  108.             When cmd = '&' & num > 0 Then
  109.                  Do
  110.                    word_out_line.i = Translate(wordlist,' ',',')
  111.                    Call matches
  112.                  End
  113.             Otherwise Nop
  114.           End
  115.           If time_to_quit = 'YES' Then Leave
  116.           If respell_current_line = 'YES' Then Leave
  117.        End
  118.        /* display prompts etc here */
  119.        drop word_in_line.
  120.        drop word_out_line.
  121.      End
  122.    If time_to_quit = 'YES' Then Leave
  123.    'cursor cmdline'
  124.    If respell_current_line \= 'YES' Then
  125.      Do
  126.        If forward = 1 Then 'N'      /* if going forward, get next line */
  127.        Else 'U'               /* if going backwards, get previous line */
  128.        If rc \= 0 Then Leave                     /* shouldn't get here */
  129.        nolines = nolines - 1
  130.      End
  131. End
  132. 'cursor cmdline'
  133. If stay.1 = 'ON' Then ':'||current_line 
  134. 'msg Spelling complete'
  135. 'restore'
  136. Return                                               /* go back to THE */
  137.  
  138. /*---------------------------------------------------------------------*/
  139. spell_word:
  140. /*---------------------------------------------------------------------*/
  141. Return
  142.  
  143. /*---------------------------------------------------------------------*/
  144. no_match:
  145. /*---------------------------------------------------------------------*/
  146. 'reserved * off'
  147. Call HighlightWord                               /* highlight the word */
  148. 'reserved' reserved_start+1 'No suggestions for:' word_in_line.i
  149. 'reserved' reserved_start+2 no_match_line
  150. Call ProcessResponse 'NOMATCH'
  151. Return
  152.  
  153. /*---------------------------------------------------------------------*/
  154. matches:
  155. /*---------------------------------------------------------------------*/
  156. 'reserved * off'
  157. Call HighlightWord                               /* highlight the word */
  158. 'reserved' reserved_start+1 'Suggestions for:' word_in_line.i
  159. Do j = 1 To Min(Words(word_out_line.i),9)
  160.    'reserved' j+1+reserved_start '  ('||j||')' Word(word_out_line.i,j)
  161. End
  162. 'reserved' j+1+reserved_start 'Press a number,' no_match_line
  163. Call ProcessResponse j-1
  164. Return
  165.  
  166. /*---------------------------------------------------------------------*/
  167. ProcessResponse:
  168. /*---------------------------------------------------------------------*/
  169. Parse Arg itemno .
  170. Do Forever
  171.    'readv KEY'
  172.    readv.1 = Translate(readv.1)
  173.    Select
  174.      When readv.1 = 'A' Then
  175.        Do
  176.          insert.0 = 2
  177.          insert.1 = '*' || word_in_line.i    /* add word to dictionary */
  178.          insert.2 = '#'                             /* save dictionary */
  179.          Call RunSpellCommand "insert." "result." "stderr."
  180.          drop insert.
  181.          drop result.
  182.          Leave
  183.        End
  184.      When readv.1 = 'Q' Then
  185.        Do
  186.          time_to_quit = 'YES'
  187.          Leave
  188.        End
  189.      When readv.1 = 'E' Then
  190.        Do
  191.          Call EditWord
  192.          Leave
  193.        End
  194.      When readv.1 = 'I' Then Leave
  195.      When Datatype(itemno) = 'NUM' & readv.1 >= 1 & readv.1 <= itemno Then
  196.        Do
  197.          itemno = itemno
  198.          off = Length(Word(word_out_line.i,itemno))-Length(word_in_line.i)
  199.          Do wordidx = i+1 To word_in_line.0
  200.             word_start.wordidx = word_start.wordidx + off
  201.          End
  202.          'zone' word_start.i '*'
  203.          'nomsg c/'||word_in_line.i||'/'||Word(word_out_line.i,readv.1)||'/'
  204.          Leave
  205.        End
  206.      Otherwise 
  207.        Do
  208.          'emsg Invalid response'
  209.        End
  210.    End
  211. End
  212. 'reserved * off'
  213. 'reset block'
  214. 'refresh'
  215. Return
  216.  
  217. /*---------------------------------------------------------------------*/
  218. HighlightWord:
  219. /*---------------------------------------------------------------------*/
  220. 'reset block'
  221. 'cursor file' line.1 word_start.i + Length(word_in_line.i) -1
  222. 'mark box'
  223. 'cursor file' line.1 word_start.i
  224. 'mark box'
  225. Return
  226.  
  227. /*---------------------------------------------------------------------*/
  228. EditWord:
  229. /*---------------------------------------------------------------------*/
  230. orig_len = Length(word_in_line.i)
  231. 'readv cmdline' word_in_line.i
  232. 'zone' word_start.i '*'
  233. 'nomsg c/'||word_in_line.i||'/'||readv.1||'/'
  234. word_in_line.i = Word(readv.1,1)
  235. respell_current_line = 'YES'
  236. 'zone 1 *'
  237. Return
  238.  
  239. /*---------------------------------------------------------------------*/
  240. RunSpellCommand:
  241. /*---------------------------------------------------------------------*/
  242. Parse Arg instem outstem errstem .
  243. rc = run_os(spell_command,instem,outstem,errstem)
  244. If rc \= 0 Then
  245.    'emsg Return code from "'||spell_command||'"' rc
  246. If stderr.0 \= 0 Then
  247.    Do
  248.      Do i = 1 To stderr.0
  249.         'emsg' stderr.i
  250.      End
  251.      'restore'
  252.      Exit 1
  253.    End
  254. If rc \= 0 Then Exit 1
  255. Return
  256.  
  257. /*---------------------------------------------------------------------*/
  258. TidyOutput:
  259. /*---------------------------------------------------------------------*/
  260. Do m = 2 To word_out_line.0 By 2
  261.    Queue word_out_line.m
  262. End
  263. numqueued = Queued()
  264. Do m = 1 To numqueued
  265.    Parse Pull word_out_line.m
  266. End
  267. word_out_line.0 = numqueued
  268. Return
  269.