/*RX
 * AREXX	Name:TeXedit.rexx	version:1.5JS		date:15-Nov-91
 *

ATTENTION!! There were necessary changes out of the editors specific
            part to include TTX. It will be possible to get them into
            the specific part, just try it out. Until now, please
            insert other editors at the end of this file, behind the
            TTX-part. They still should work.

            If there is no line-number given the number will be fetched out
            of the logfile.

 This ARexx script is called from virtex (or initex) in case of an
error or if the 'e' command is used, and it's given the current file
and line number as argument. It may be called from the TeX-server too.
We will successively call TurboText (TTX) to load the file and the
logfile. Other editors welcome!
 *
INPUTS:
 1: filename to edit (please no spaces in filename)
 2: line where error occured
 *
BUGS:
 GETCLIP("TEXFILE") gets precedence over any other information for
retrieving the logfile, so if you alternately start virtex directly
from CLI while the server is active, the wrong files may be loaded.
 *
 This file tries to cope with the following version (PasTeX 1.2a)
> This is a PD-Version of Pas-TeX (made Jan 30 1991 [br]/[hes])
> This is TeX, C Version 3.1
(The logfile is sometimes put at the wrong place.)
 *
 Does not handle names relative to the local root correctly (like ":foo/bar")
 *
 See each editor relative bugs below.
 *
FILES:
 Rexx:namestruc
 LIBS:rexxsupport.library
AUTHORS:
 *	J\"org H\"ohle, since March 91
 *	Georg Hessmann, previous version
 *  Jan Schroeder (not Schr\"oder!!), update for TurboText
 */

/*
 * IF ~SHOW('L','rexxsupport.library') THEN
 * 	IF ~ADDLIB('rexxsupport.library',0,-30,0) THEN DO
 * 		say "Can't open 'rexxsupport.library'!"
 * 		EXIT 20
 * 		END
 */

/* The TeX server may want to know that we have been called */

CALL SETCLIP("TEXTIME",TIME('s'))
PARSE ARG filename number .

/* if the TeX 'e' command was used to call the editor, don't ask if
	files should be loaded. */

IF address() = 'TeX-Rexx-Port' & "EDIT" ~= UPPER(mygetenv("TEXREXX"))
THEN askload = 0
ELSE askload = 1

PARSE VALUE namestruc(filename) WITH ivol idirs ibase .

/* The idea behind the following statements is to get an absolute path
for <filename>. The result will be stored in <errnfile>. For example, "CD
TeX:bar" "virtex foo" should give <filename>=foo, and
<errnfile>=TeX:bar/foo. This is necessary because the editor doesn't
know where file foo is located, but will be able to load file
TeX:bar/foo. */

mainname = GETCLIP("TEXFILE")
if "" == mainname THEN DO
   /* TeX has not been started through some Start_TeX.#? script. */
   /* We have a hard time finding the right directories */

   texdir = pragma('d')

   /* Amiga OS dirnames should end with either : or /
	thus you need just append the filename */
   IF RIGHT(texdir,1)~=':' & RIGHT(texdir,1)~='/' THEN texdir = texdir||'/'

   IF 0 = ivol THEN DO
	errnfile = texdir||SUBSTR(filename, 1+ivol)
	/* The logfile is in the current dir */
	logfile = texdir||SUBSTR(filename, 1+ivol+idirs, ibase)||".log"
	END
   ELSE DO
	errnfile=filename
	IF 0 = idirs THEN

    /* This is a bug in virtex from PasTeX12a (made Jan 30 1991 [br]/[hes])
	The logfile is *not* in virtex's directory, but rather in the
    source's directory if a device name, but no subsequent directory was
    given. I.e, virtex (CD in RAM) TeX:sample would put the errorfile in
    TeX: and not in RAM: as it should be. */

    logfile = LEFT(filename, ivol+idirs+ibase)||".log"
	ELSE logfile = texdir||SUBSTR(filename, 1+ivol+idirs, ibase)||".log"
	END
   DROP texdir
   END

ELSE DO	/* TeX was started through Start_TeX.#? scripts */
	/* "TEXFILE" tells us CD and the main file */

   PARSE VALUE namestruc(mainname) WITH mvol mdirs mbase .
   IF 0 ~= ivol THEN errnfile = filename
   ELSE	errnfile = LEFT(mainname, mvol+mdirs)||SUBSTR(filename, 1+ivol)
   logfile = LEFT(mainname, mvol+mdirs+mbase)||".log"
   efile = RIGHT(mainname,mbase+4)
   lfile = SUBSTR(filename, 1+ivol+idirs, ibase)||".log"
   DROP mvol mdirs mbase
END

/* 0 = ibase would mean that the call was incorrect, for example when
TEXREXXEDIT says "TeXedit.rexx" and not "TeXedit.rexx %s %d" */

IF 0 = ibase | ~EXISTS(errnfile) THEN DO
 say "TeXedit.rexx: Can't find erroneous file."
 EXIT 10
END
ELSE IF ~EXISTS(logfile) THEN DO
 say "TeXedit.rexx: Can't find log file."
 logfile = ""
 /* but we continue */
END

DROP ivol idirs ibase


/* turbotext part */
IF SHOW('P', 'TURBOTEXT') THEN DO

    DO i=0 TO 20
	h='TURBOTEXT'i
	IF SHOW('P', h) THEN LEAVE
    END

    ADDRESS VALUE h
    OPTIONS RESULTS

    Screen2Front

    /* testen ob die Files nicht schon geladen sind. (hes) */

    IF askload THEN DO
	RequestBool PROMPT errnfile TITLE '"TeX found an error! Load files?."'
	IF "YES" ~= RESULT THEN EXIT
    END

     /* TODO: We should really delete old logfiles automatically, without letting
	CED open a requester to ask if an old modified file may be
	overwritten or not */

     IF "" ~= lfile THEN DO

	getdocuments
	docs = RESULT
	i = 0
	host = ""
	DO WHILE docs ~= ''
	      PARSE VAR docs '"' a '"' port docs
	      IF a = lfile THEN DO
	        host = port
		LEAVE
	      END
	      i = i+1
	END

	IF "" = host THEN DO /* always assume the logfile currently
		loaded is old, because TeX generated a new one. */
		/*expand view*/
		opendoc
		host = RESULT
	END

	ADDRESS VALUE host
	openfile logfile
	setreadonly	/* now it's non-editable */
	MoveSOF
	/* if we don't have the line number: search the number in the logfile (hes) */
	/* bringt das wirklich etwas ? Der erste Fehler steht
		sowieso gegen Anfang der Logdatei */
	IF 0 ~= number THEN find "l."||number
	ELSE DO
	  find "l."
	  GetCursorPos
	  PARSE VAR RESULT l c f
	  GetLine c
	  PARSE VAR RESULT n dum
	  number = SUBSTR(n,3)
        END
   END
   ELSE  RequestBool PROMPT '"Couldn''t find logfile"' TITLE '"Where is the Log-File?"'

  /*
   * CleanUp
   */

  GetDocuments
  docs       = RESULT
  numWindows = Words(docs)/2

  GetScreenInfo
  screenWidth  = Word(RESULT,3)
  screenHeight = Word(RESULT,4)

  wHeight = (screenHeight-2) % numWindows
  yPos    = 2

  DO i=1 TO numWindows
	IF (i = numWindows) THEN DO
	  wHeight = wHeight+2
	END

	INTERPRET ADDRESS Word(docs,(i*2))
	GetWindowInfo
	PARSE VAR RESULT dum wX wY sX sY .

	/* If sizing first would put it off the screen, size it last */
	IF ((wY+wHeight > screenHeight) | (wX>0)) THEN DO
	  MoveWindow (-wX) (yPos-wY)
	  SizeWindow screenWidth-sX (wHeight-2)-sY

	END; ELSE DO
	  SizeWindow screenWidth-sX (wHeight-2)-sY
	  MoveWindow (-wX) (yPos-wY)

	END

	yPos = yPos+wHeight
  END

  GetPort efile
  host = RESULT
  ADDRESS VALUE host

  ActivateWindow
  IF 0 ~= number THEN move number
  MoveSOL

  EXIT
END	/* End of TTX part */



/*
 * Insert other editors here
 */



SAY "TeXedit.rexx: No supported editor active."
EXIT 10

mygetenv: procedure	/* when will ARexx supply GetEnv/SetEnv ? */
   PARSE ARG name

   IF open(TEMPFILE,"ENV:"||name,'r') THEN DO
	gives = readln(TEMPFILE)
	CALL close TEMPFILE
	END
   ELSE gives = ""

   RETURN gives
