home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / window / ttzapple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-18  |  6.7 KB  |  366 lines

  1. /*
  2.  * Copyright (c) 1989 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Edward Wang at The University of California, Berkeley.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@(#)ttzapple.c    3.11 (Berkeley) 6/6/90";
  39. #endif /* not lint */
  40.  
  41. #include "ww.h"
  42. #include "tt.h"
  43. #include "char.h"
  44.  
  45. /*
  46. zz|zapple|perfect apple:\
  47.     :am:pt:co#80:li#24:le=^H:nd=^F:up=^K:do=^J:\
  48.     :ho=\E0:ll=\E1:cm=\E=%+ %+ :ch=\E<%+ :cv=\E>%+ :\
  49.     :cl=\E4:ce=\E2:cd=\E3:rp=\E@%.%+ :\
  50.     :so=\E+:se=\E-:\
  51.     :dc=\Ec:DC=\EC%+ :ic=\Ei:IC=\EI%+ :\
  52.     :al=\Ea:AL=\EA%+ :dl=\Ed:DL=\ED%+ :\
  53.     :sf=\Ef:SF=\EF%+ :sr=\Er:SR=\ER%+ :cs=\E?%+ %+ :\
  54.     :is=\E-\ET :
  55. */
  56.  
  57. #define NCOL        80
  58. #define NROW        24
  59. #define TOKEN_MAX    32
  60.  
  61. extern short gen_frame[];
  62.  
  63. zz_setmodes(new)
  64. {
  65.     if (new & WWM_REV) {
  66.         if ((tt.tt_modes & WWM_REV) == 0)
  67.             ttesc('+');
  68.     } else
  69.         if (tt.tt_modes & WWM_REV)
  70.             ttesc('-');
  71.     tt.tt_modes = new;
  72. }
  73.  
  74. zz_insline(n)
  75. {
  76.     if (n == 1)
  77.         ttesc('a');
  78.     else {
  79.         ttesc('A');
  80.         ttputc(n + ' ');
  81.     }
  82. }
  83.  
  84. zz_delline(n)
  85. {
  86.     if (n == 1)
  87.         ttesc('d');
  88.     else {
  89.         ttesc('D');
  90.         ttputc(n + ' ');
  91.     }
  92. }
  93.  
  94. zz_putc(c)
  95.     char c;
  96. {
  97.     if (tt.tt_nmodes != tt.tt_modes)
  98.         zz_setmodes(tt.tt_nmodes);
  99.     ttputc(c);
  100.     if (++tt.tt_col == NCOL)
  101.         tt.tt_col = 0, tt.tt_row++;
  102. }
  103.  
  104. zz_write(p, n)
  105.     register char *p;
  106.     register n;
  107. {
  108.     if (tt.tt_nmodes != tt.tt_modes)
  109.         zz_setmodes(tt.tt_nmodes);
  110.     ttwrite(p, n);
  111.     tt.tt_col += n;
  112.     if (tt.tt_col == NCOL)
  113.         tt.tt_col = 0, tt.tt_row++;
  114. }
  115.  
  116. zz_move(row, col)
  117.     register row, col;
  118. {
  119.     register x;
  120.  
  121.     if (tt.tt_row == row) {
  122. same_row:
  123.         if ((x = col - tt.tt_col) == 0)
  124.             return;
  125.         if (col == 0) {
  126.             ttctrl('m');
  127.             goto out;
  128.         }
  129.         switch (x) {
  130.         case 2:
  131.             ttctrl('f');
  132.         case 1:
  133.             ttctrl('f');
  134.             goto out;
  135.         case -2:
  136.             ttctrl('h');
  137.         case -1:
  138.             ttctrl('h');
  139.             goto out;
  140.         }
  141.         if ((col & 7) == 0 && x > 0 && x <= 16) {
  142.             ttctrl('i');
  143.             if (x > 8)
  144.                 ttctrl('i');
  145.             goto out;
  146.         }
  147.         ttesc('<');
  148.         ttputc(col + ' ');
  149.         goto out;
  150.     }
  151.     if (tt.tt_col == col) {
  152.         switch (row - tt.tt_row) {
  153.         case 2:
  154.             ttctrl('j');
  155.         case 1:
  156.             ttctrl('j');
  157.             goto out;
  158.         case -2:
  159.             ttctrl('k');
  160.         case -1:
  161.             ttctrl('k');
  162.             goto out;
  163.         }
  164.         if (col == 0) {
  165.             if (row == 0)
  166.                 goto home;
  167.             if (row == NROW - 1)
  168.                 goto ll;
  169.         }
  170.         ttesc('>');
  171.         ttputc(row + ' ');
  172.         goto out;
  173.     }
  174.     if (col == 0) {
  175.         if (row == 0) {
  176. home:
  177.             ttesc('0');
  178.             goto out;
  179.         }
  180.         if (row == tt.tt_row + 1) {
  181.             /*
  182.              * Do newline first to match the sequence
  183.              * for scroll down and return
  184.              */
  185.             ttctrl('j');
  186.             ttctrl('m');
  187.             goto out;
  188.         }
  189.         if (row == NROW - 1) {
  190. ll:
  191.             ttesc('1');
  192.             goto out;
  193.         }
  194.     }
  195.     /* favor local motion for better compression */
  196.     if (row == tt.tt_row + 1) {
  197.         ttctrl('j');
  198.         goto same_row;
  199.     }
  200.     if (row == tt.tt_row - 1) {
  201.         ttctrl('k');
  202.         goto same_row;
  203.     }
  204.     ttesc('=');
  205.     ttputc(' ' + row);
  206.     ttputc(' ' + col);
  207. out:
  208.     tt.tt_col = col;
  209.     tt.tt_row = row;
  210. }
  211.  
  212. zz_start()
  213. {
  214.     zz_setmodes(0);
  215.     zz_setscroll(0, NROW - 1);
  216.     zz_clear();
  217.     ttesc('T');
  218.     ttputc(TOKEN_MAX + ' ');
  219. }
  220.  
  221. zz_end()
  222. {
  223.     ttesc('T');
  224.     ttputc(' ');
  225. }
  226.  
  227. zz_clreol()
  228. {
  229.     ttesc('2');
  230. }
  231.  
  232. zz_clreos()
  233. {
  234.     ttesc('3');
  235. }
  236.  
  237. zz_clear()
  238. {
  239.     ttesc('4');
  240.     tt.tt_col = tt.tt_row = 0;
  241. }
  242.  
  243. zz_insspace(n)
  244. {
  245.     if (n == 1)
  246.         ttesc('i');
  247.     else {
  248.         ttesc('I');
  249.         ttputc(n + ' ');
  250.     }
  251. }
  252.  
  253. zz_delchar(n)
  254. {
  255.     if (n == 1)
  256.         ttesc('c');
  257.     else {
  258.         ttesc('C');
  259.         ttputc(n + ' ');
  260.     }
  261. }
  262.  
  263. zz_scroll_down(n)
  264. {
  265.     if (n == 1)
  266.         if (tt.tt_row == NROW - 1)
  267.             ttctrl('j');
  268.         else
  269.             ttesc('f');
  270.     else {
  271.         ttesc('F');
  272.         ttputc(n + ' ');
  273.     }
  274. }
  275.  
  276. zz_scroll_up(n)
  277. {
  278.     if (n == 1)
  279.         ttesc('r');
  280.     else {
  281.         ttesc('R');
  282.         ttputc(n + ' ');
  283.     }
  284. }
  285.  
  286. zz_setscroll(top, bot)
  287. {
  288.     ttesc('?');
  289.     ttputc(top + ' ');
  290.     ttputc(bot + ' ');
  291.     tt.tt_scroll_top = top;
  292.     tt.tt_scroll_bot = bot;
  293. }
  294.  
  295. int zz_debug = 0;
  296.  
  297. zz_set_token(t, s, n)
  298.     char *s;
  299. {
  300.     if (tt.tt_nmodes != tt.tt_modes)
  301.         zz_setmodes(tt.tt_nmodes);
  302.     if (zz_debug) {
  303.         char buf[100];
  304.         zz_setmodes(WWM_REV);
  305.         (void) sprintf(buf, "%02x=", t);
  306.         ttputs(buf);
  307.         tt.tt_col += 3;
  308.     }
  309.     ttputc(0x80);
  310.     ttputc(t + 1);
  311.     s[n - 1] |= 0x80;
  312.     ttwrite(s, n);
  313.     s[n - 1] &= ~0x80;
  314. }
  315.  
  316. /*ARGSUSED*/
  317. zz_put_token(t, s, n)
  318.     char *s;
  319. {
  320.     if (tt.tt_nmodes != tt.tt_modes)
  321.         zz_setmodes(tt.tt_nmodes);
  322.     if (zz_debug) {
  323.         char buf[100];
  324.         zz_setmodes(WWM_REV);
  325.         (void) sprintf(buf, "%02x>", t);
  326.         ttputs(buf);
  327.         tt.tt_col += 3;
  328.     }
  329.     ttputc(t + 0x81);
  330. }
  331.  
  332. tt_zapple()
  333. {
  334.     tt.tt_insspace = zz_insspace;
  335.     tt.tt_delchar = zz_delchar;
  336.     tt.tt_insline = zz_insline;
  337.     tt.tt_delline = zz_delline;
  338.     tt.tt_clreol = zz_clreol;
  339.     tt.tt_clreos = zz_clreos;
  340.     tt.tt_scroll_down = zz_scroll_down;
  341.     tt.tt_scroll_up = zz_scroll_up;
  342.     tt.tt_setscroll = zz_setscroll;
  343.     tt.tt_availmodes = WWM_REV;
  344.     tt.tt_wrap = 1;
  345.     tt.tt_retain = 0;
  346.     tt.tt_ncol = NCOL;
  347.     tt.tt_nrow = NROW;
  348.     tt.tt_start = zz_start;
  349.     tt.tt_end = zz_end;
  350.     tt.tt_write = zz_write;
  351.     tt.tt_putc = zz_putc;
  352.     tt.tt_move = zz_move;
  353.     tt.tt_clear = zz_clear;
  354.     tt.tt_setmodes = zz_setmodes;
  355.     tt.tt_frame = gen_frame;
  356.     tt.tt_padc = TT_PADC_NONE;
  357.     tt.tt_ntoken = 127;
  358.     tt.tt_set_token = zz_set_token;
  359.     tt.tt_put_token = zz_put_token;
  360.     tt.tt_token_min = 1;
  361.     tt.tt_token_max = TOKEN_MAX;
  362.     tt.tt_set_token_cost = 2;
  363.     tt.tt_put_token_cost = 1;
  364.     return 0;
  365. }
  366.