home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d9xx / d945 / emacsstarter.lha / EmacsStarter / rexx / emacs.rexx next >
OS/2 REXX Batch file  |  1993-12-20  |  9KB  |  287 lines

  1. /*
  2. FILE         Emacs.rexx
  3. VERSION      $VER: V1.06 Emacs.rexx
  4. DESCRIPTION  Load a file into a (running) emacs.
  5.              emacs [flags and files]
  6.  
  7.          The following options are supported:
  8.          -[not]tofront    (Don't) move the Emacs window and screen
  9.                      to the front.
  10.          -[not]sticky    Do not terminate until the files on the
  11.                 command line are killed.
  12.          -[no]screen    Open a public screen (Requiers ScreenManager)
  13.  
  14.              where the third and fourth argument corresponds to the
  15.              the screensize.
  16. AUTHOR       Anders Lindgren, d91ali@csd.uu.se
  17.              Bo Liljegren, bo-lilje@dsv.su.se
  18. STATUS       This file is in the public domain
  19. HISTORY
  20.   11-Aug-92 ALi        Created this file
  21.   13-Aug-92 ALi        Replaced "runwsh" with "run".
  22.                        "else nop" added.
  23.   14-Aug-92 ALi  1.00  V1.0 released.
  24.   20-Aug-92 Bo L 1.01  Added ScreenManager stuff.
  25.   03-Jan-93 ALi  1.02  MODE edited. COLOURS removed.
  26.                        amiga-set-geometry moved to .emacs
  27.   13-Jan-93 ALi  1.03  TOBACK added. SHANGHAI and DEFAULT removed,
  28.                        emacs can now open itself correctly.
  29.                        -nottofront flag added.
  30.   20-Jan-93 Bo L 1.04  Replaced the static portname with the dynamic
  31.                        in the 'WaitForPort'-line
  32.   16 aug 93 Bo L 1.05  Changed screenresolution argument to DISPID.
  33.                        To avoid nameconflict with different languages.
  34.                        Changed ScrenManager parse part for those
  35.                        (unblessed) who don't have WShell.
  36.   22-Sep-93 ALi     1.06  Real option and filename parser added.
  37.                  (amiga-window-to-front) and
  38.                (amiga-activate-window) called instead of
  39.                ScreenManager.
  40.                (cd ) removed. (Why should emacs move directry?)
  41.                Sticky option added.
  42.                Files loaded by (load-file ), even when Emacs
  43.                is started by the script.
  44.                Temporary file which sets the window size and
  45.                screen name added. (No use for anything in .emacs)
  46.                POPPUB option removed.
  47.                Now gets screen font from WB.
  48. */
  49.  
  50. OPTIONS RESULTS
  51. OPTIONS FAILAT 21
  52.  
  53. /* Make sure required libraries are present */
  54. IF ~SHOW('l',"rexxsupport.library") THEN DO
  55.     IF ~ADDLIB("rexxsupport.library",0,-30,0) THEN DO
  56.     SAY "Can't find rexxsupport.library"
  57.     EXIT
  58.     END
  59.     END
  60.  
  61.  
  62. /* The name for the public screen */
  63. screenname = 'Emacs'
  64.  
  65. portname = 'EMACS1'
  66. args = ARG(1)  
  67.  
  68. /* The commandline to send to a new emacs */
  69. cmd = ""
  70.  
  71. /* A stem containing the options, the defaults are set here */
  72. flags.batch   = 0        /* Batch mode, always start a new Emacs */
  73. flags.kill    = 0        /* Kill a running emacs. */
  74. flags.screen  = 1        /* Use a public screen (Requiers ScreenManager) */
  75. flags.sticky  = 0        /* Wait until the buffers are terminated */
  76. flags.tofront = 1        /* Send the screen to the front */
  77.  
  78. /* Argument parser */
  79.  
  80. filesidx = 0
  81. DO WHILE LENGTH(args) > 1
  82.     args = strip(args, 'L')    /* Strip leading blanks */
  83.     IF LEFT(args,1) = '"' THEN DO
  84.     filesidx = filesidx + 1
  85.     PARSE VAR args  '"' files.filesidx '"' args
  86.     END
  87.     ELSE DO
  88.     PARSE VAR args  onearg args
  89.     IF LEFT(onearg,1) = '-' THEN DO
  90.         SELECT
  91.         WHEN onearg = "-tofront"    THEN flags.tofront = 1
  92.         WHEN onearg = "-nottofront" THEN flags.tofront = 0
  93.         WHEN onearg = "-sticky"     THEN flags.sticky = 1
  94.         WHEN onearg = "-notsticky"  THEN flags.sticky = 0
  95.         WHEN onearg = "-screen"     THEN flags.screen = 1
  96.         WHEN onearg = "-noscreen"   THEN flags.screen = 0
  97.         WHEN onearg = "-batch"        THEN flags.batch = 1
  98.         WHEN onearg = "-kill"        THEN DO
  99.             flags.kill = 1
  100.             cmd = cmd onearg
  101.             END
  102.         /* Emacs options with one argument */
  103.         WHEN (onearg = "-l" | onearg = "-load" | onearg = "-f",
  104.               | onearg = "-funcall" | onearg = "-i" | onearg = "-insert",
  105.               | onearg = "-t" | onearg = "-u" | onearg = "-user") THEN DO
  106.             PARSE VAR args  optarg args
  107.             cmd = cmd onearg optarg
  108.             END
  109.         /* Emacs options with two arguments */
  110.         WHEN (onearg = "-dev" | onearg = "-fn") THEN DO
  111.             PARSE VAR args  optarg1 optarg2 args
  112.             cmd = cmd onearg optarg1 optarg2
  113.             END
  114.         OTHERWISE cmd = cmd onearg
  115.         END
  116.         END
  117.     ELSE IF LEFT(onearg,1) = '+' THEN DO
  118.         PARSE VAR onearg  '+' flags.linenumber
  119.         END
  120.     ELSE DO
  121.         filesidx = filesidx + 1
  122.         files.filesidx = onearg
  123.         END
  124.     END
  125.     END
  126.  
  127. /* If in batch-mode, start a new Emacs synchronous using the
  128.  * original options */
  129. IF flags.batch THEN DO
  130.     ADDRESS COMMAND "temacs" arg(1)
  131.     EXIT rc
  132.     END
  133.  
  134.  
  135. /* If an Emacs Screen doesn't exists, create one */
  136. IF flags.screen & ~SHOWLIST('W',screenname||'-Demon') THEN DO 
  137.     filnamn = 'T:SM'||d2x(random(256,1024,time('s')))
  138.     ADDRESS COMMAND 'ScreenManager INFO Workbench EXPERT >'||filnamn
  139.     IF ~OPEN(fil,filnamn,'R') THEN EXIT 20
  140.     cnt = 4
  141.     rad = READLN(fil)
  142.     IF EOF(fil) THEN cnt = 0
  143.     DO WHILE cnt ~= 0
  144.     ord = WORD(rad,1)
  145.     SELECT
  146.         WHEN ord = 'Position:' THEN DO
  147.         PARSE VAR rad . '=' xpos ' ' . '=' ypos ' ' .
  148.         cnt = cnt - 1
  149.         END
  150.         WHEN ord = 'Size:' THEN DO
  151.         PARSE VAR rad . '=' xsize ' ' . '=' ysize ' ' .
  152.         cnt = cnt - 1
  153.         END
  154.         WHEN ord = 'DisplayID:' THEN DO
  155.         resmode = WORD(rad,2)
  156.         cnt = cnt - 1
  157.         END
  158.         WHEN ord = 'Font:' THEN DO
  159.         PARSE VAR rad  'Font:'  font '.font ' fontsize
  160.         font = strip(font)
  161.         cnt = cnt - 1
  162.             END
  163.         OTHERWISE
  164.         NOP
  165.         END
  166.     rad = READLN(fil)
  167.     IF EOF(fil) THEN cnt = 0
  168.     END
  169.     CALL CLOSE(fil)
  170.     CALL DELETE(filnamn)
  171.     menubar = fontsize + 2
  172.     ypos = ypos + menubar
  173.     ysize = ysize - menubar
  174.     ScrMng = ' OPEN '||screenname||' TITLE="'||screenname||' Screen"'
  175.     ScrMng = ScrMng||' DISPID='||resmode
  176.     ScrMng = ScrMng||' PLANES=2'
  177.     ScrMng = ScrMng||' PENS=011213103'
  178.     ScrMng = ScrMng||' FONT='||font||'.'||fontsize
  179.     ScrMng = ScrMng||' AUTOCLOSE' /* close screen when Emacs exists */
  180.     ScrMng = ScrMng||' TOBACK'      /* It's moved to the front below  */
  181.     ScrMng = ScrMng||' CX_TOFRONT="LCOMMAND e"'
  182.     ScrMng = ScrMng||' CX_DEFAULT="LCOMMAND SHIFT e"'
  183.     ADDRESS COMMAND 'ScreenManager' ScrMng
  184.  
  185.     /* Create a small elisp file which sets the screen and size of Emacs */
  186.     sizefilename = 't:size' || d2x(random(256,1024,time('s'))) || '.el'
  187.     IF ~OPEN(fil, sizefilename, 'W') THEN EXIT 20
  188.     CALL WRITELN(fil, ';; Automagically created by Emacs.rexx')
  189.     CALL WRITELN(fil, '(amiga-set-geometry 0' fontsize+3 xsize ysize '"' || screenname || '")')
  190.     CALL CLOSE(fil)
  191.     cmd = cmd '-load' sizefilename
  192.     END
  193.     
  194. /* Start a new Emacs, if no one exists. */
  195. IF SHOW('P', portname) = 0 THEN DO
  196.     
  197.     CALL PRAGMA('stack', 40000)
  198.  
  199.     ADDRESS COMMAND 'run <nil: >nil: temacs' cmd
  200.     ADDRESS COMMAND 'WaitForPort' portname
  201.     flags.kill = 0
  202.  
  203.     END
  204.  
  205.  
  206. ADDRESS VALUE portname
  207.  
  208. /* This no-op call doesn't return until after the 
  209.  * t:sizeXX file has been read */
  210. "()"
  211.  
  212. /* Delete the temporary file, if it was created. */
  213. IF SYMBOL('sizefilename') = 'VAR' THEN CALL DELETE(sizefilename)
  214.  
  215. /* If in sticky mode, load sticky module into Emacs and
  216.  * open a communication port */
  217. IF flags.sticky THEN DO
  218.     "(require 'sticky)"
  219.  
  220.     count = 1
  221.     DO UNTIL ~SHOW('P', stickyportname)
  222.     stickyportname = "EMACS_CLIENT" || count
  223.     count = count + 1
  224.     END
  225.  
  226.     CALL OPENPORT(stickyportname)
  227.     IF ~SHOW('P', stickyportname) THEN DO
  228.     SAY "Emacs client: Can't open sticky port."
  229.     EXIT 20
  230.     END
  231.     END
  232.  
  233. /* Get the current diretory. (There is no point in building a correct
  234.  * filename, since Emacs does that for us.) */
  235. dir = PRAGMA('directory')
  236. IF RIGHT(dir,1) ~= ':' THEN dir = dir || '/'
  237.  
  238. stickycount = 0            /* The number of files to wait for */
  239.  
  240. /* Load the files into emacs */
  241. DO i = 1 TO filesidx
  242.     '(find-file "' || dir || files.i || '")'
  243.  
  244.     IF flags.sticky THEN DO
  245.     '(setq sticky-rexx-port "' || stickyportname || '")'
  246.     stickycount = stickycount + 1
  247.     END
  248.     END
  249.  
  250. /* If the +linenumber option was given, go to the correct line */
  251. IF SYMBOL('flags.linenumber') = 'VAR' THEN '(goto-line ' || flags.linenumber || ')'
  252.  
  253. /* Bring an activated Emacs window and screen to the front */
  254. IF flags.tofront THEN DO
  255.     '(amiga-window-to-front)'
  256.     '(amiga-activate-window)'
  257.     END
  258.  
  259. /* If in sticky mode, wait for the buffers to be deleted */
  260. IF flags.sticky THEN DO
  261.     DO WHILE stickycount > 0
  262.  
  263.     CALL WAITPKT(stickyportname)
  264.     pkt = GETPKT(stickyportname)
  265.     IF pkt = NULL() then iterate
  266.  
  267.     IF GETARG(pkt) = "TERMINATING" THEN
  268.         stickycount = stickycount - 1
  269.  
  270.     CALL REPLY(pkt, 0)
  271.  
  272.     END
  273.  
  274.     CALL CLOSEPORT(stickyportname)
  275.     END
  276.  
  277.  
  278. IF flags.kill THEN DO
  279.     '(save-buffers-kill-emacs)'
  280.     /* Screen normally closed automatically, but this won't hurt */
  281.     IF flags.screen THEN DO
  282.         ADDRESS COMMAND 'ScreenManager close' screenname
  283.         END
  284.     END
  285.  
  286. EXIT 0
  287.