home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 2 / amigaformatcd02.iso / pd / text / alphaspell_suite / aspellgui / rexx / annotate / aspell.ann
Encoding:
Text File  |  1996-05-20  |  17.5 KB  |  549 lines

  1. /**************************************************************************/
  2. /* $VER: ASpell.ann 1.6 (13 Mar 1996)                                     */
  3. /* The AlphaSpell GUI © Copyright Fergus Duniho 1995-6                    */
  4. /**************************************************************************/
  5.  
  6. OPTIONS RESULTS
  7. OPTIONS FAILAT 20
  8. SIGNAL ON SYNTAX
  9. SIGNAL ON FAILURE
  10.  
  11. CALL OpenLib("rexxsupport.library")
  12. CALL OpenLib("rexxtricks.library")
  13. CALL OpenLib("rexxreqtools.library")
  14.  
  15. EditPort = GetEditPort()
  16. IF EditPort ~= "" THEN ADDRESS VALUE EditPort
  17. PSC = GetScreen()
  18.  
  19. tempfile = "T:Temp" || TIME(s)
  20. GUI = GetENV("ENV:ASpellGUI")
  21. rttags = "rt_pubscrname =" PSC
  22. win.title = "Select a Word:"
  23. win.gadgettext = "_Accept|_Cancel"
  24. win.pubscreen = PSC
  25. win.width = 40
  26. win.sort = "FALSE"
  27. win.multiselect = "FALSE"
  28.  
  29. CALL Spellcheck()
  30. CALL Cleanup()
  31. EXIT
  32.  
  33. /**************************************************************************/
  34. /* Spellcheck() -- The MAIN routine                                       */
  35. /**************************************************************************/
  36.  
  37. Spellcheck:
  38.  
  39. /**************************************************************************/
  40. /* Launch Varexx                                                          */
  41. /**************************************************************************/
  42.  
  43. /* Check Varexx is loaded if not load it */
  44.  
  45. IF SHOW("P","VAREXX") ~= 1 THEN DO
  46.     ADDRESS COMMAND "run >NIL: varexx"
  47.     ADDRESS COMMAND "WaitForPort VAREXX"
  48. END
  49. ADDRESS VAREXX
  50.  
  51. IF OPENPORT("HOLLY") = 0 THEN DO
  52.     CALL rtezrequest "Could not open a port.", "_Abort", "Varexx Error:", rttags
  53.     RETURN
  54. END
  55. version
  56. IF RESULT < 1.6 THEN DO
  57.     CALL rtezrequest "You need version 1.6+ of Varexx", "_Okay", "Varexx Error:", rttags
  58.     RETURN
  59. END
  60. "load" GUI "HOLLY PS" PSC
  61. vhost = RESULT
  62. ADDRESS
  63. ADDRESS VALUE vhost
  64.  
  65. /**************************************************************************/
  66. /* Localize gadget text for chosen language                               */
  67. /**************************************************************************/
  68.  
  69. lang = GetENV("language")
  70. IF lang ~= "" & lang ~= "english" THEN DO
  71.     catalog = "SYS:Catalogs/ASpell." || lang
  72.     CALL READFILE catalog, lines
  73.     DO x = 1 to lines.0
  74.         INTERPRET "setlabel" "'"lines.x"'"
  75.     END
  76. END
  77.  
  78. /**************************************************************************/
  79. /* Show About screen while AlphaSpell checks document.                    */
  80. /**************************************************************************/
  81.  
  82. show about
  83. CALL ReadPrefs()
  84. CALL SaveTemp()
  85.  
  86. /**************************************************************************/
  87. /* Spell check tempfile with AlphaSpell                                   */
  88. /**************************************************************************/
  89.  
  90. ADDRESS COMMAND "AlphaSpell -Ss" tempfile "-o" tempfile "-d" dict_path dict_list
  91.  
  92. /**************************************************************************/
  93. /* Set Lists                                                              */
  94. /**************************************************************************/
  95.  
  96. CALL ReadList "UNFOUND"
  97. IF UNFOUND.0 = 0 THEN DO
  98.     CALL rtezrequest "No misspellings found.", "_Exit", "Spell Checked Finished:", rttags
  99.     RETURN
  100. END
  101. current = 1
  102. LWORDS.0 = 0
  103. MWORDS.0 = 0
  104.  
  105. hide
  106. show main
  107. window front activate
  108. CALL SetTarget UNFOUND.1
  109.  
  110. /**************************************************************************/
  111. /* MAIN LOOP -- Check for GUI events                                      */
  112. /**************************************************************************/
  113.  
  114. DO FOREVER
  115.     CALL WAITPKT("HOLLY")
  116.     packet = GETPKT("HOLLY")
  117.     IF packet ~= '00000000'x THEN DO
  118.         class = GETARG(packet)
  119.         SELECT
  120.             WHEN class = "CLOSEWINDOW" THEN LEAVE
  121.             WHEN class = "LEARN" THEN CALL Learn()
  122.             WHEN class = "FIND" THEN flag = FindWord(flag)
  123.             WHEN class = "REPLACE" THEN CALL ReplaceWord()
  124.             WHEN class = "ANAGRAMS" THEN CALL ASearch("A")
  125.             WHEN class = "GUESS" THEN CALL ASearch("G")
  126.             WHEN class = "NEXT" THEN CALL SetTarget("+1")
  127.             WHEN class = "PREV" THEN CALL SetTarget("-1")
  128.             WHEN class = "FIRST" THEN CALL SetTarget(1)
  129.             WHEN class = "LAST" THEN CALL SetTarget(UNFOUND.0)
  130.             WHEN class = "SELECT" THEN CALL ChooseWord()
  131.             WHEN class = "PREFS" THEN DO
  132.                 CALL Preferences()
  133.                 CALL SetTarget(current)
  134.             END
  135.             OTHERWISE NOP
  136.         END
  137.         window front activate
  138.     END
  139. END
  140. IF LWORDS.0 + MWORDS.0 > 0 THEN DO
  141.     hide
  142.     show learn
  143.     CALL QSORT "LWORDS"
  144.     LWORDS.count = LWORDS.0
  145.     setlist lwords clear stem LWORDS select LWORDS.1
  146.     CALL QSORT "MWORDS"
  147.     MWORDS.count = MWORDS.0
  148.     setlist mwords clear stem MWORDS select MWORDS.1
  149.     DO FOREVER
  150.         CALL WAITPKT("HOLLY")
  151.         packet = GETPKT("HOLLY")
  152.         IF packet ~= '00000000'x THEN DO
  153.             class = GETARG(packet)
  154.             SELECT
  155.                 WHEN class = "CLOSEWINDOW" THEN LEAVE
  156.                 WHEN class = "SAVEWORDS" THEN DO
  157.                     CALL SaveList()
  158.                     LEAVE
  159.                 END
  160.                 WHEN class = "MOVE" THEN CALL Move()
  161.                 WHEN class = "RML" THEN CALL Lose("lwords")
  162.                 WHEN class = "RMM" THEN CALL Lose("mwords")
  163.                 OTHERWISE NOP
  164.             END
  165.             window front activate
  166.         END
  167.     END
  168. END
  169. ADDRESS
  170. RETURN
  171.  
  172. /**************************************************************************/
  173. /* VARIOUS SUBROUTINES                                                    */
  174. /**************************************************************************/
  175.  
  176. /**************************************************************************/
  177. /* SetTarget(word) -- Sets the word in the target string gadget           */
  178. /**************************************************************************/
  179.  
  180. SetTarget:
  181. IF DATATYPE(arg(1)) = "NUM" THEN DO
  182.     IF VERIFY(arg(1), "+-", "M") = 1 THEN current = current + arg(1)
  183.     ELSE current = arg(1)
  184.     IF current < 1 THEN current = UNFOUND.0
  185.     IF current > UNFOUND.0 THEN current = 1
  186.     settext target UNFOUND.current
  187.     settext replacement UNFOUND.current
  188. END
  189. ELSE DO
  190.     settext target arg(1)
  191.     settext replacement arg(1)
  192. END
  193. flag = 0 /* Word hasn't been searched for since selection */
  194. RETURN
  195.  
  196. /**************************************************************************/
  197. /* SetReplace() - Sets replacement string gadget                          */
  198. /**************************************************************************/
  199.  
  200. SetReplace:
  201. settext replacement arg(1)
  202. settext target UNFOUND.current
  203. RETURN
  204.  
  205. /**************************************************************************/
  206. /* Learn() -- Adds a word to LEARN, the words to learn list               */
  207. /**************************************************************************/
  208.  
  209. Learn:
  210. read target
  211. wrd = RESULT
  212. /* Tests whether wrd is lowercase */
  213. IF BITOR(wrd,," ") == wrd THEN DO
  214.     IF LSEARCH(wrd, "LWORDS") == -1 THEN DO
  215.         cnt = LWORDS.0 + 1
  216.         LWORDS.0 = cnt
  217.         LWORDS.cnt = wrd
  218.     END
  219. END
  220. ELSE DO
  221.     cur = LSEARCH(wrd, "MWORDS")
  222.     DO WHILE (MWORDS.cur = wrd) & (MWORDS.cur ~== wrd)
  223.         cur = cur + 1
  224.     END
  225.     IF cur == -1 THEN DO
  226.         cnt = MWORDS.0 + 1
  227.         MWORDS.0 = cnt
  228.         MWORDS.cnt = wrd
  229.     END
  230. END
  231. CALL SetTarget("+1")
  232. RETURN
  233.  
  234. /**************************************************************************/
  235. /* ASearch(mode) has AlphaSpell search for anagrams, matches, or guesses  */
  236. /**************************************************************************/
  237.  
  238. ASearch:
  239. ARG mode /* G = Guess, A = Anagrams, P = Pattern Match" */
  240. busy set
  241. read replacement
  242. targ = RESULT
  243. IF VERIFY(target, "[]!^*?\", "M") > 0 THEN mode = "P"
  244. com = "AlphaSpell -" || mode "-d" dict_path "-w" targ "-n" edit_dist "-o" tempfile "-k" keyfile dict_list
  245. ADDRESS COMMAND com
  246. CALL ReadList "GUESS"
  247. busy
  248. IF GUESS.0 > 0 THEN DO
  249.     IF VIEWLIST("GUESS", "win", "dest") = 1 THEN CALL SetReplace(dest.1)
  250. END
  251. ELSE CALL rtezrequest "No match found.", "_Continue", "Search Complete:", rttags
  252. RETURN
  253.  
  254. /**************************************************************************/
  255. /* ChooseWord() -- Select word from listview of unfound words             */
  256. /**************************************************************************/
  257.  
  258. ChooseWord:
  259. IF VIEWLIST("UNFOUND", "win", "dest") = 1 THEN CALL SetTarget(dest.1)
  260. IF dest.1 ~= "" THEN DO
  261.     current = LSEARCH(dest.1, "UNFOUND")
  262.     uwrd = UPPER(dest.1)
  263.     DO WHILE (UPPER(UNFOUND.current) = uwrd) & (UNFOUND.current ~= dest.1)
  264.         current = current + 1
  265.     END
  266. END
  267. RETURN
  268.  
  269. /**************************************************************************/
  270. /* SaveList() -- Saves words in the "LEARN" list to user dictionary       */
  271. /**************************************************************************/
  272.  
  273. SaveList:
  274. udict.low = MAKEPATH(dict_path, user_dict || ".low")
  275. udict.mix = MAKEPATH(dict_path, user_dict || ".mix")
  276. read lwords LEARN
  277. LEARN.0 = LEARN.count
  278. IF LEARN.0 > 0 THEN DO
  279.     CALL WRITEFILE tempfile, "LEARN"
  280.     com = "AlphaSpell -Lco" udict.low tempfile
  281.     IF Exists(udict.low) THEN com = com udict.low
  282.     ADDRESS COMMAND com
  283. END
  284. read mwords LEARN
  285. LEARN.0 = LEARN.count
  286. IF LEARN.0 > 0 THEN DO
  287.     CALL WRITEFILE tempfile, "LEARN"
  288.     com = "AlphaSpell -Lco" udict.mix tempfile
  289.     IF Exists(udict.mix) THEN com = com udict.mix
  290.     ADDRESS COMMAND com
  291. END
  292. CALL DELETE tempfile
  293. RETURN
  294.  
  295. /**************************************************************************/
  296. /* Move() -- Moves word from mixed case listview to lowercase listview    */
  297. /**************************************************************************/
  298.  
  299. Move:
  300. setlist lwords BITOR(Lose("mwords"),," ")
  301. RETURN
  302.  
  303. /**************************************************************************/
  304. /* ReadList -- Reads words from tempfile to a list and sorts the list     */
  305. /**************************************************************************/
  306.  
  307. ReadList:
  308. CALL READFILE tempfile, arg(1)
  309. INTERPRET arg(1) || ".count =" arg(1) || ".0"
  310. CALL QSORT arg(1)
  311. RETURN
  312.  
  313. /**************************************************************************/
  314. /* Lose() -- Deletes a word from a listview                               */
  315. /**************************************************************************/
  316.  
  317. Lose:
  318. INTERPRET "read" arg(1) boo
  319. wrd = RESULT
  320. INTERPRET "setlist" arg(1) "wrd del"
  321. item = boo.select
  322. IF item = boo.count THEN item = item - 1
  323. INTERPRET "setlist" arg(1) "select s update" item
  324. RETURN wrd
  325.  
  326. /**************************************************************************/
  327. /* Preferences() -- Preferences GUI                                       */
  328. /**************************************************************************/
  329.  
  330. Preferences:
  331. hide
  332. show prefs
  333. settext dir dict_path
  334. settext dict dict_list
  335. settext udict user_dict
  336. setnum ed edit_dist
  337. settext key keyfile
  338. IF ~EXISTS(keyfile) THEN setbar ed max 2
  339. DO FOREVER
  340.     CALL WAITPKT("HOLLY")
  341.     packet = GETPKT("HOLLY")
  342.     IF packet ~= '00000000'x THEN DO
  343.         class = GETARG(packet)
  344.         SELECT
  345.             WHEN class = "CLOSEWINDOW" | class = "CANCEL" THEN LEAVE
  346.             WHEN class = "SAVE" | class = "USE" THEN DO
  347.                 read dir
  348.                 dict_path = RESULT
  349.                 read dict
  350.                 dict_list = RESULT
  351.                 read udict
  352.                 user_dict = RESULT
  353.                 read ed
  354.                 edit_dist = RESULT
  355.                 read key
  356.                 keyfile = RESULT
  357.                 CALL WritePrefs "ENV:ASpell.prefs"
  358.                 IF class = "SAVE" THEN CALL WritePrefs "ENVARC:ASpell.prefs"
  359.                 LEAVE
  360.             END
  361.             OTHERWISE NOP
  362.         END
  363.     END
  364. END
  365. hide
  366. show main
  367. RETURN
  368.  
  369. /**************************************************************************/
  370. /* WritePrefs() -- Writes Preferences to a file                           */
  371. /**************************************************************************/
  372.  
  373. WritePrefs:
  374. IF OPEN(output, arg(1), "W") = 1 THEN DO
  375.     CALL WRITELN output, dict_path
  376.     CALL WRITELN output, dict_list
  377.     CALL WRITELN output, user_dict
  378.     CALL WRITELN output, edit_dist
  379.     CALL WRITELN output, keyfile
  380.     CALL CLOSE output
  381. END
  382. RETURN
  383.  
  384. /**************************************************************************/
  385. /* ReadPrefs() -- Read Preferences from ENV:ASpell.prefs or use defaults  */
  386. /**************************************************************************/
  387.  
  388. ReadPrefs:
  389. CALL READFILE "ENV:ASpell.prefs", "PREFS"
  390. fields = 5
  391. IF PREFS.0 >= 1 & PREFS.0 <= fields THEN dict_path = PREFS.1
  392. ELSE dict_path = "Work:AlphaSpell/English/"
  393. IF PREFS.0 >= 2 & PREFS.0 <= fields THEN dict_list = PREFS.2
  394. ELSE dict_list = "*.low *.mix"
  395. IF PREFS.0 >= 3 & PREFS.0 <= fields THEN user_dict = PREFS.3
  396. ELSE user_dict = "User"
  397. IF PREFS.0 >= 4 & PREFS.0 <= fields THEN edit_dist = PREFS.4
  398. ELSE edit_dist = 0
  399. IF PREFS.0 >= 5 & PREFS.0 <= fields THEN keyfile = PREFS.5
  400. ELSE keyfile = "S:Alpha-Key"
  401. IF PREFS.0 ~= fields THEN DO
  402.     CALL Preferences()
  403.     show about
  404. END
  405. RETURN
  406.  
  407. /**************************************************************************/
  408. /* Cleanup() -- Closes down the GUI                                       */
  409. /**************************************************************************/
  410.  
  411. Cleanup:
  412. IF SHOWLIST("P", "HOLLY") = 1 THEN CALL CLOSEPORT ("HOLLY")
  413. IF SHOWLIST("P", "VAREXX") = 1 THEN ADDRESS "VAREXX" hide unload
  414. RETURN
  415.  
  416. /**************************************************************************/
  417. /* OpenLib(library) -- Checks that library exists and opens it if it does */
  418. /**************************************************************************/
  419.  
  420. OpenLib: PROCEDURE
  421.  
  422. IF EXISTS("libs:" || arg(1)) THEN DO
  423.     IF ~SHOW("L", arg(1)) THEN
  424.         IF ~ADDLIB(arg(1),0,-30,0) THEN EXIT
  425. END
  426. ELSE EXIT
  427. RETURN
  428.  
  429. /**************************************************************************/
  430. /* ERROR MESSAGES                                                         */
  431. /**************************************************************************/
  432.  
  433. failure:
  434. syntax:
  435. SAY "Error" rc  "-- Line" SIGL
  436. SAY errortext(rc)
  437. SAY sourceline(SIGL)
  438. CALL Cleanup()
  439. EXIT
  440.  
  441. /**************************************************************************/
  442. /* Functions to get around the limits of some text editors. You might or  */
  443. /* might not need some of these.                                          */
  444. /**************************************************************************/
  445.  
  446. /**************************************************************************/
  447. /* Replace(word, target, replacement)                                     */
  448. /**************************************************************************/
  449.  
  450. Replace: PROCEDURE
  451. PARSE ARG word, target, repl
  452. start = 1
  453. size = Length(target)
  454. DO WHILE start < Length(word)
  455.     x = Pos(target, word, start)
  456.     IF x == 0 THEN LEAVE
  457.     word = Delstr(word, x, size)
  458.     word = Insert(repl, word, x-1)
  459.     start = x + size + 1
  460. END
  461. RETURN word
  462.  
  463. /**************************************************************************/
  464. /* WordComp(string, word) -- Checks whether a target word can be parsed   */
  465. /* from a given string. This is useful if your text editor lacks a whole  */
  466. /* word search mode. You can search for a word, read the full text of the */
  467. /* found string, and compare them.                                        */
  468. /**************************************************************************/
  469.  
  470. WordComp: PROCEDURE
  471. Parse Arg str, wrd, x
  472. s = Index(str, wrd, x)
  473. IF s = 0 THEN RETURN 0
  474. IF s>1 THEN DO
  475.     c = Substr(str, s-1, 1)
  476.     IF Datatype(c, "A") = 1 | c = "'" THEN RETURN 0
  477. END
  478. s = s + Length(wrd)
  479. IF s > Length(str) THEN RETURN 1
  480. c = Substr(str, s, 1)
  481. IF Datatype(c, "M") = 1 THEN RETURN 0
  482. RETURN 1
  483. END
  484.  
  485. /**************************************************************************/
  486. /* EDITOR SPECIFIC SUBROUTINES                                            */
  487. /**************************************************************************/
  488.  
  489. /**************************************************************************/
  490. /* FindWord(flag) -- Finds selected word in document                      */
  491. /**************************************************************************/
  492.  
  493. FindWord: PROCEDURE
  494. read target
  495. wrd = RESULT /* Reads selected word */
  496. ADDRESS
  497. IF arg(1) = 0 THEN MOVE_CURSOR ABS 0 0
  498. SET FIND_TEXT wrd
  499. DOMENU FIND_NEXT
  500. ADDRESS
  501. RETURN 1
  502.  
  503. /**************************************************************************/
  504. /* ReplaceWord() -- Replaces selected word with word in string gadget     */
  505. /**************************************************************************/
  506.  
  507. ReplaceWord:
  508. read target
  509. oldword = RESULT
  510. read replacement
  511. newword = RESULT
  512. ADDRESS
  513. DEL Length(oldword)
  514. INSERT STRING newword
  515. ADDRESS
  516. RETURN
  517.  
  518. /**************************************************************************/
  519. /* SaveTemp() -- Saves the current file as a temporary file               */
  520. /**************************************************************************/
  521.  
  522. SaveTemp:
  523. ADDRESS
  524. DOMENU SELECT_ALL
  525. DOMENU COPY 0
  526. ADDRESS
  527. ADDRESS COMMAND "ClipSave" tempfile
  528. RETURN
  529.  
  530. /**************************************************************************/
  531. /* GetEditPort() -- Makes sure the right text editor port is open.        */
  532. /**************************************************************************/
  533.  
  534. GetEditPort:
  535. IF Abbrev(Address(), "Annotate_Rexx") = 1 THEN RETURN Address()
  536. IF ~SHOWLIST("P", "Annotate_Rexx") THEN DO
  537.     CALL rtezrequest "Annotate_Rexx unavailable", "_Abort", "Missing Port:"
  538.     EXIT
  539. END
  540. RETURN "Annotate_Rexx"
  541.  
  542. /**************************************************************************/
  543. /* GetScreen() -- Returns the screen name                                 */
  544. /**************************************************************************/
  545.  
  546. GetScreen: PROCEDURE
  547. RETURN GETDEFAULTPUBSCREEN()
  548.  
  549.