home *** CD-ROM | disk | FTP | other *** search
/ Dream 45 / Amiga_Dream_45.iso / Amiga / Magazine / Dossier-LaTeX / AmiWeb2C.lha / web2c61 / rexx / TeXEdit.rexx < prev   
OS/2 REXX Batch file  |  1994-12-10  |  10KB  |  410 lines

  1. /*
  2. ** AREXX $VER:TeXedit.rexxáV1.44 (31.1.1994)
  3. **
  4. ** This ARexx script is called from virtex (or initex) in case of an
  5. ** error or if the 'e' command is used, and it's given the current file
  6. ** and line number as arguments. It may be called from the TeX-server too.
  7. ** We will successively call CygnusEd (CED) or Micro(GNU)Emacs (MG) to
  8. ** load the file and the logfile. Other editors welcome!
  9. **
  10. ** INPUTS:
  11. **    1: filename to edit (please no spaces in filename)
  12. **    2: line where error occured
  13. **
  14. ** BUGS:
  15. **    GETCLIP("TEXFILE") gets precedence over any other information for
  16. **    retrieving the logfile, so if you alternately start virtex directly
  17. **    from CLI while the server is active, the wrong files may be loaded.
  18. **
  19. **    This file tries to cope with the following version (PasTeX 1.2a)
  20. **    > This is a PD-Version of Pas-TeX (made Jan 30 1991 [br]/[hes])
  21. **    > This is TeX, C Version 3.1
  22. **    (The logfile is sometimes put at the wrong place.)
  23. **
  24. **    Does not handle names relative to the local root correctly (like ":foo/bar")
  25. **
  26. **    See each editor relative bugs below.
  27. **
  28. ** FILES:
  29. **    Rexx:NameStruc
  30. **    LIBS:rexxsupport.library
  31. **
  32. ** AUTHORS:
  33. **    J\"org H\"ohle, since March 91
  34. **    Georg Hessmann, previous version
  35. **
  36. ** CHANGES:
  37. **    Modifications for personal use by Andreas Scherer, 1993, 1994.
  38. */
  39.  
  40. If ~Show('Libraries','rexxsupport.library') Then
  41.   If ~AddLib('rexxsupport.library',0,-30,0) Then Do
  42.     Say "Konnte 'rexxsupport.library' nicht ÷ffnen!"
  43.     Exit 20
  44.   End
  45.  
  46. /*
  47. ** The TeX server may want to know that we have been called.
  48. */
  49. Call SetClip("TEXTIME",TIME('s'))
  50.  
  51. Parse Arg FILENAME NUMBER .
  52.  
  53. /*
  54. ** If the TeX 'e' command was used to call the editor,
  55. ** don't ask if files should be loaded.
  56. */
  57. If "EDIT" ~= Upper(MyGetEnv("TEXREXX")) Then
  58.   ASKLOAD = 0
  59. Else
  60.   ASKLOAD = 1
  61.  
  62. Parse Value NameStruc(FILENAME) With IVOL IDIRS IBASE .
  63.  
  64. /*
  65. ** The idea in the following statements is to get an absolute path
  66. ** for <filename>. The result will be stored in <errnfile>. For example,
  67. ** "CD TeX:bar" "virtex foo" should give <filename>=foo, and
  68. ** <errnfile>=TeX:bar/foo. This is necessary because the editor doesn't
  69. ** know where file foo is located, but will be able to load file
  70. ** TeX:bar/foo.
  71. */
  72.  
  73. MAINNAME = GetClip("TEXFILE")
  74. If "" = MAINNAME Then Do
  75. /*
  76. ** TeX has not been started through some Start_TeX.#? script.
  77. ** We have a hard time finding the right directories.
  78. */
  79.  
  80.   TEXDIR = Pragma('d')
  81.  
  82. /*
  83. ** Amiga OS dirnames should end with either `:' or `/'.
  84. ** Thus you need just append the filename.
  85. */
  86.   If Right(TEXDIR,1) ~= ':' & Right(TEXDIR,1) ~= '/' Then
  87.     TEXDIR = TEXDIR||'/'
  88.  
  89.   If 0 = IVOL Then Do
  90.     ERRNFILE = TEXDIR||SubStr(FILENAME,1+IVOL)
  91. /*
  92. ** The logfile is in the current dir.
  93. */
  94.     LOGFILE = TEXDIR||SubStr(FILENAME,1+IVOL+IDIRS,IBASE)||".log"
  95.   End; Else Do
  96.     ERRNFILE = FILENAME
  97.     If 0 = IDIRS Then
  98. /*
  99. ** This is a bug in virtex from PasTeX12a (made Jan 30 1991 [br]/[hes])
  100. ** The logfile is *not* in virtex's directory, but rather in the
  101. ** source's directory if a device name, but no subsequent directory was
  102. ** given. I.e, virtex (CD in RAM) TeX:sample would put the errorfile in
  103. ** TeX: and not in RAM: as it should be.
  104. */
  105.       LOGFILE = Left(FILENAME,IVOL+IDIRS+IBASE)||".log"
  106.     Else
  107.       LOGFILE = TEXDIR||SubStr(FILENAME,1+IVOL+IDIRS,IBASE)||".log"
  108.     End
  109.   Drop TEXDIR
  110. End; Else Do
  111. /*
  112. ** TeX was started through Start_TeX.#? scripts.
  113. ** "TEXFILE" tells us CD and the main file.
  114. */
  115.   Parse Value NameStruc(MAINNAME) With MVOL MDIRS MBASE .
  116.  
  117.   If 0 ~= IVOL Then
  118.     ERRNFILE = FILENAME
  119.   Else
  120.     ERRNFILE = Left(MAINNAME,MVOL+MDIRS)||SubStr(FILENAME,1+IVOL)
  121.  
  122.   LOGFILE = Left(MAINNAME,MVOL+MDIRS+MBASE)||".log"
  123.   Drop MVOL MDIRS MBASE
  124. End
  125.  
  126. /*
  127. ** 0 = ibase would mean that the call was incorrect, for example when
  128. ** TEXREXXEDIT says "TeXedit.rexx" and not "TeXedit.rexx %s %d"
  129. */
  130. If 0 = IBASE | ~Exists(ERRNFILE) Then Do
  131.   Say "TeXedit.rexx: Konnte fehlerhafte Datei nicht finden."
  132.   Exit 10
  133. End; Else If ~Exists(LOGFILE) Then Do
  134.   Say "TeXedit.rexx: Konnte Logfile nicht finden."
  135.   LOGFILE = ""
  136. /*
  137. ** But we continue.
  138. */
  139. End
  140.  
  141. Drop IVOL IDIRS IBASE
  142.  
  143. /*
  144. ** Here starts each editor's specific part.
  145. */
  146.  
  147. /*
  148. ** Cygnus Ed Professional Version 3.5+
  149. */
  150. If Show('Port','rexx_ced') Then Do
  151.   Address 'rexx_ced'
  152.  
  153.   Options Results
  154.  
  155.   CEDtoFront
  156.  
  157. /*
  158. ** The following should not be commented out, as this macro is called
  159. ** asynchronously, and it would be very bad for the user to have it's
  160. ** input mixed with the newly loaded files. Popping up a requester
  161. ** ensures that the user does not type in something and waits for the
  162. ** files to be loaded. This is the method I use for synchronization. (JCH)
  163. */
  164.   If ASKLOAD Then Do
  165.     Okay2 "TeX fand einen Fehler in der Datei"'0A'X"'"ERRNFILE"'. Datei laden?"
  166.     If 1 ~= RESULT Then
  167.       Exit 0
  168.   End
  169.  
  170. /*
  171. ** Make sure the TEX-file is loaded.  This works with CED 3.5+ only.
  172. ** There was no mechanism to make sure that the errorneous file was
  173. ** actually loaded in one of the views.  If it wasn't, the next command
  174. ** would open a second view for the log file and a *third* for the
  175. ** TeX-file, but half of the CED screen was left empty with `Unnamed.'
  176. ** You could replace this by `jump to file' for CED 2.12.
  177. */
  178.   OW ERRNFILE
  179.  
  180. /*
  181. ** TODO: We should really delete old logfiles automatically, without letting
  182. **       CED open a requester to ask if an old modified file may be
  183. **       overwritten or not.
  184. */
  185.  
  186.   If "" ~= LOGFILE Then Do
  187.     'jump to file "'LOGFILE'"'
  188.  
  189. /*
  190. ** Always assume the logfile currently loaded is old,
  191. ** because TeX generated a new one.
  192. */
  193.     If 0 ~= RESULT Then
  194.       Quit
  195.  
  196.     Open new
  197.     Open '"'LOGFILE'"'
  198.  
  199. /*
  200. ** Now it's non-editable.
  201. */
  202.     Editable file
  203.     Beg of file
  204.  
  205. /*
  206. ** If we don't have the line number: search the number in the logfile. (hes)
  207. */
  208.     If 0 ~= NUMBER Then
  209.       'Search for...' "l."||NUMBER
  210.     Else Do
  211. /*
  212. ** ^M bringt nichts, und dann Steuercodes hier ...
  213. */
  214.       'Search for...' "l."
  215.       'Right'
  216.  
  217. /*
  218. ** Take the current line from CED.
  219. */
  220.       'Status 55'
  221.       Parse Var RESULT "l."NUMBER .
  222.     End
  223. /*
  224. ** Changed from Search "! " (hes)
  225. */
  226.   End; Else
  227.     Okay1 "Konnte kein richtiges Logfile finden!"
  228.  
  229. /*
  230. ** The following line should really read "jump to file errnfile" and not
  231. ** filename, because I have done extra work to get the right directory. But
  232. ** this information is only in errnfile, which contains an absolute pathname,
  233. ** and not in filename, which is the parameter supplied by virtex or initex,
  234. ** usually a relative pathname. CED's current directory is not necessarily
  235. ** virtex's one. It also seems that CED is not smart enough as to make the
  236. ** difference between foo:tgmoae/myfile and bar:tgmoab/myfile. If I say
  237. ** jump to file foo:tgmoae/myfile it may as well jump to bar:tgmoab/myfile,
  238. ** whichever window comes first.
  239. */
  240.   'jump to file "'ERRNFILE'"'
  241.   If 0 = RESULT Then Do
  242. /*
  243. ** Not reached with `OW ERRNFILE' above.
  244. */
  245.     Open new
  246.     Open '"'errnfile'"'
  247.   End
  248.  
  249. /*
  250. ** TODO: another editor may have modified the disk file, or the user
  251. ** the currently loaded file, while virtex compiled an old version. How
  252. ** can I get rid of that stupid filerequester in that case? I said "open
  253. ** errnfile", so why does CED pop up a filerequester (and may put the
  254. ** user in the wrong directory too)?
  255. */
  256.  
  257.   If 0 ~= NUMBER Then
  258.     JumpTo NUMBER
  259.  
  260.   Beg of line
  261.  
  262.   Exit 0
  263. End
  264. /*
  265. ** End of CED part.
  266. */
  267.  
  268. /*
  269. ** Micro(Gnu)Emacs (MG), to be found on AmigaLibDisk352.
  270. */
  271. If Show('Port','mg') Then Do
  272.   Address 'mg'
  273.  
  274.   Options Results
  275.  
  276. /*
  277. ** The following waits until MG is deiconified.
  278. */
  279.   'amiga-window-to-top'
  280.  
  281. /*
  282. ** We need to prevent the user from continuing to type and thus
  283. ** modifying the newly created buffers. I choose the rexx-lock/unlock
  284. ** method to synchronise with the user because MG3b4 has some cursor
  285. ** position/display bugs: the cursor may erroneously appear in some
  286. ** buffer and overwrite it (on the screen only), and not appear in the
  287. ** bottom line where it belongs.
  288. */
  289.  
  290.   If ASKLOAD Then Do
  291. /*
  292. ** This is dangerous if this script aborts.
  293. */
  294.     'rexx-lock'
  295.  
  296.     Address Value RESULT
  297.  
  298.     'rexx-request "TeX fand einen Fehler. Datei laden? "'
  299.     RETC = RC
  300.     RETS = RESULT
  301.  
  302.     'rexx-unlock'
  303.  
  304.     Address Value RESULT
  305.  
  306.     If 1 < RETC | Left(Upper(RETS),1) = 'N' Then Do
  307.       'rexx-display ""'
  308.       Exit 0
  309.     End
  310.  
  311.     Drop RETC RETS
  312.   End
  313.  
  314.   'find-file "'ERRNFILE'"'
  315. /*
  316. ** MG doesn't seem to set the RC value on a find-file.
  317. */
  318.   'rexx-buffer' BUF
  319.  
  320. /*
  321. ** buf.1 is the buffer name, buf.3 the number of lines.
  322. */
  323.   If NUMBER < BUF.3 Then Do
  324.     'delete-other-windows'
  325.  
  326.     If "" ~= LOGFILE Then Do
  327.       'split-window-vertically'
  328.  
  329. /*
  330. ** Now get rid of old logfiles. Here I make sure that I get rid of
  331. ** every suspicious logfile, because multiple pathes may lead to the same
  332. ** file, as for example SYS:TeX/sample.log & TeX:sample.log.
  333. */
  334.       'rexx-buffer-list' BUFFERS
  335.  
  336.       Parse Value NameStruc(LOGFILE) With IVOL IDIRS IBASE
  337.  
  338.       LOGNAME = Upper(SubStr(LOGFILE,1+IVOL+IDIRS))
  339.  
  340.       Do I=1 To BUFFERS.0
  341.         If 0 < Index(Upper(BUFFERS.I.FILE),LOGNAME) Then Do
  342.           'switch-to-buffer "'BUFFERS.I.NAME'"'
  343.           'not-modified'
  344.           'kill-buffer "'BUFFERS.I.NAME'"'
  345.         End
  346.       End
  347.  
  348.       'find-file "'LOGFILE'"'
  349. /*
  350. ** MG doesn't seem to set the RC value.
  351. */
  352.       'beginning-of-buffer'
  353.  
  354.       If 0 ~= NUMBER Then
  355.         're-search-forward "^l."NUMBER'
  356.       Else Do
  357. /*
  358. ** Try to use normal search?
  359. */
  360.         're-search-forward "^l."' /* ^ means begin of line */
  361.  
  362. /*
  363. ** If search successfull get current line contents.
  364. */
  365.         If 0 = RC Then Do
  366.           'rexx-line'
  367.           Parse Var RESULT "l."NUMBER .
  368.         End
  369.       End
  370.  
  371.       'other-window'
  372.       'rexx-display "Now what''s that error?"'
  373.     End; Else
  374.       'rexx-display "Konnte kein richtiges Logfile finden!"'
  375.  
  376.     If 0 ~= NUMBER Then
  377.       'goto-line 'NUMBER
  378.   End; Else Do
  379.     'not-modified'
  380.     'kill-buffer "'BUF.NAME'"'
  381.     'rexx-display "Konnte fehlerhafte Datei nicht finden!"'
  382.     Exit 5
  383.   End
  384.  
  385.   Exit 0
  386. End
  387. /*
  388. ** End of MG part.
  389. */
  390.  
  391. /*
  392. ** Write macros for your favorite editor here.
  393. */
  394.  
  395. Say "TeXedit.rexx: Kein unterstⁿtzter Editor aktiv."
  396. Exit 10
  397.  
  398. /*
  399. ** When will ARexx supply GetEnv/SetEnv?
  400. */
  401. MyGetEnv: Procedure
  402. Parse Arg NAME
  403.  
  404. If Open(TEMPFILE,"ENV:"||NAME,'r') Then Do
  405.   GIVES = Readln(TEMPFILE)
  406.   Call Close TEMPFILE
  407. End; Else GIVES = ""
  408.  
  409. Return GIVES
  410.