home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume8 / smile / smile.c < prev    next >
C/C++ Source or Header  |  1989-10-01  |  10KB  |  455 lines

  1. /* SMiLE -- Simple MIni Line Editor
  2.    Copyright (C) 1989 By Alejandro Liu
  3.    You are given permission to Freely Copy and distribute this program
  4.    in its unmodifed form provided that this copyright notice is included.
  5.  */
  6.  
  7. #include "smile.h"
  8. #include "bsd_tty.h"
  9. #include <ctype.h>
  10. #include <stdio.h>
  11. #include <strings.h>
  12.  
  13. #define TRUE       1
  14. #define FALSE      0
  15. #define TSTR     100
  16.  
  17. makebar(width)
  18. char width;
  19. {
  20.   int i;
  21.   for (i = 0;i<width;i++) {
  22.     if (i%8)
  23.       prtchr('-');
  24.     else
  25.       prtchr('!');
  26.   };
  27.   if ((width-1)%8)
  28.     prtchr('!');
  29.   prtchr(NEWLIN);
  30. }
  31.  
  32. smile_intro(width)
  33. int width;
  34. {
  35.   prtstr("\n");
  36.   prtstr("SMiLE -- Simple MIni Line Editor        (C) By Alejandro Liu Aug 1989\n");
  37.   prtstr("Press ESC (CTRL+[) for menu.            CTRL+X to exit SMiLE\n");
  38.   prtstr("   ");
  39.   makebar(width);
  40. }
  41.  
  42. int smile(head,wwrap,width)
  43. ed_line *head;
  44. char wwrap,width;
  45. {
  46.   ed_line *current, *temp;
  47.   char c_buf[TSTR], c_lin, c_col, key, i, j;
  48.   extern char smile_menu();
  49.  
  50.   c_col = 0;
  51.   c_lin = 1;
  52.   current = head;
  53.   smile_intro(width);
  54.   pull(&c_col, c_buf, current);
  55.   showlin(c_lin,c_buf,c_col);
  56.  
  57.   for (;;) {
  58.     key = 127 & getkey();
  59.     switch (key) {
  60.       case PREV_L:
  61.         if (c_lin-1) {
  62.           c_lin--;
  63.           push(c_buf,current);
  64.           temp = current->previous;
  65.       current = temp;
  66.           if (current == NULL) current = head;
  67.           pull(&c_col, c_buf, current);
  68.           prtchr(NEWLIN);
  69.           showlin(c_lin,c_buf, c_col);
  70.         }
  71.         else
  72.           beep();
  73.         break;
  74.       case NEXT_L:
  75.         if (current->next == NULL)
  76.           beep();
  77.         else {
  78.           c_lin++;
  79.           push(c_buf, current);
  80.           temp = current->next;
  81.       current = temp;
  82.           pull(&c_col, c_buf, current);
  83.           prtchr(NEWLIN);
  84.           showlin(c_lin,c_buf,c_col);
  85.         }
  86.         break;
  87.       case INSER_L:
  88.       case INSERT_L:
  89.         insertl(current);
  90.         push(c_buf,current);
  91.         temp = current->next;
  92.     current = temp;
  93.         current->len = 0;
  94.         c_col = 0;
  95.         c_lin++;
  96.         c_buf[0] = 0;
  97.         prtchr(NEWLIN);
  98.         showlin(c_lin,c_buf,c_col);
  99.         break;
  100.       case LEFT_C:
  101.         if (c_col) {
  102.           c_col--;
  103.           backspace(1);
  104.         }
  105.         else
  106.           beep();
  107.         break;
  108.       case RIGHT_C:
  109.         if (c_col == current->len)
  110.           beep();
  111.         else {
  112.           putchar(c_buf[c_col]);
  113.           c_col++;
  114.         }
  115.         break;
  116.       case DEL_00:
  117.       case DEL_01:
  118.       case DEL_02:
  119.         if (c_col) {
  120.           if (c_col == current->len) {
  121.             delete(1);
  122.             c_buf[c_col--] = 0;
  123.             (current->len)--;
  124.           }
  125.           else {
  126.             delete(1);
  127.             for (i = c_col; i<=current->len;i++) {
  128.               c_buf[i-1] = c_buf[i];
  129.               if (c_buf[i])
  130.                 prtchr(c_buf[i]);
  131.               else
  132.                 prtchr(' ');
  133.             }
  134.         backspace((current->len) - c_col + 1);
  135.             c_col--;
  136.             current->len--;
  137.           }
  138.         }
  139.         else
  140.           beep();
  141.         break;
  142.       case KILL_L:
  143.         if (c_col == current->len) {
  144.           if (current->next) {
  145.         char *mirage;
  146.         temp = current->next;
  147.         delete(c_col + 3);
  148.         c_col = strlen(c_buf) + strlen(temp->data);
  149.         mirage = (char *)malloc(c_col + 1);
  150.         strcpy(mirage,c_buf);
  151.         strcat(mirage,temp->data);
  152.             current->next = temp->next;
  153.             free(temp->data);
  154.             free(temp);
  155.             if (temp = current->next)
  156.           temp->previous = current;
  157.         if (c_col >= width) {
  158.           c_col = width -1;
  159.           mirage[c_col] = 0;
  160.         }
  161.         current->len = c_col;
  162.         strcpy(c_buf,mirage);
  163.         free(mirage);
  164.             showlin(c_lin, c_buf, c_col);
  165.       }
  166.           else
  167.         beep();
  168.         }
  169.         else {
  170.           for (i=c_col;i<current->len;i++)
  171.             prtchr(' ');
  172.           backspace(current->len - c_col);
  173.           c_buf[current->len = c_col] = 0;
  174.         }
  175.         break;
  176.       case MSG_LST:
  177.         push(c_buf,current);
  178.         listmsg(width, head);
  179.         showlin(c_lin, c_buf, c_col);
  180.         break;
  181.       case PREVIEW:
  182.         push(c_buf,current);
  183.         preview(width, head);
  184.         showlin(c_lin, c_buf, c_col);
  185.         break;
  186.       case EXIT:
  187.         push(c_buf, current);
  188.         return(NORMAL_SAVE);
  189.       case MENU:
  190.         push(c_buf,current);
  191.         if (i = smile_menu(head,FULL_MENU, &wwrap, &width, MENU))
  192.           if ((i == NORMAL_SAVE) || (i == QUICK_SAVE) || (i == ABORT)) {
  193.             push(c_buf,current);
  194.             return(i);
  195.           }
  196.         showlin(c_lin,c_buf,c_col);
  197.         break;
  198.       case DOTKEY0:
  199.       case DOTKEY1:
  200.         if (c_col == 0) {
  201.           push(c_buf,current);
  202.           if (i = smile_menu(head,DOT_MENU, &wwrap, &width, key)) {
  203.             switch (i) {
  204.               case NORMAL_SAVE:
  205.               case QUICK_SAVE:
  206.               case ABORT:
  207.                 push(c_buf,current);
  208.                 return(i);
  209.               case DOTKEY0:
  210.               case DOTKEY1:
  211.                 insertc(c_buf, &c_col, &(current->len), i);
  212.         delete(1);
  213.                 break;
  214.             }
  215.           }
  216.           showlin(c_lin,c_buf,c_col);
  217.       break;
  218.         }
  219.       default:
  220.         if ((31<key)&&(key<127)) {
  221.           if (c_col == width) {
  222.             if (wwrap) {
  223.               i = c_col;
  224.               while (i&&(c_buf[i--]!=32));
  225.               if (i) {
  226.         if ((c_col-i-1)>0)
  227.           delete(c_col-i-1);
  228.                 c_buf[++i] = 0;
  229.                 insertl(current);
  230.                 push(c_buf,current);
  231.                 current = current->next;
  232.                 c_lin++;
  233.                 push(&c_buf[i+1], current);
  234.                 pull(&c_col, c_buf, current);
  235.                 c_col = current->len;
  236.                 prtchr(NEWLIN);
  237.                 showlin(c_lin,c_buf,c_col);
  238.               }
  239.               else {
  240.                 insertl(current);
  241.                 push(c_buf, current);
  242.                 current = current->next;
  243.                 current->len = 0;
  244.                 c_col = 0;
  245.                 c_lin++;
  246.                 c_buf[0]= 0;
  247.                 prtchr(NEWLIN);
  248.                 showlin(c_lin,c_buf,c_col);
  249.               }
  250.               insertc(c_buf,&c_col, &(current->len),key);
  251.             }
  252.             else
  253.               beep();
  254.           }
  255.           else
  256.             insertc(c_buf, &c_col, &(current->len),key);
  257.         }
  258.         else
  259.           beep();
  260.     }
  261.   }
  262. }
  263.  
  264. insertc(buffer, col, len, key)
  265. char buffer[], *col, *len, key;
  266. {
  267.   buffer[*col] = key;
  268.   if ((*col)++ == (*len))
  269.     buffer[++(*len)] = 0;
  270.   prtchr(key);
  271. }
  272.  
  273. backspace(rept)
  274. char rept;
  275. {
  276.   while (rept--)
  277.     prtchr(8);
  278. }
  279.  
  280. delete(rept)
  281. char rept;
  282. {
  283.   while (rept--) {
  284.     prtchr(8);
  285.     prtchr(' ');
  286.     prtchr(8);
  287.   }
  288. }
  289.  
  290. showlin(line,buffer,column)
  291. char line, column, *buffer;
  292. {
  293.   char leng;
  294.   printf("%2d>",line);
  295.   prtstr(buffer);
  296.   leng = strlen(buffer);
  297.   if (leng != column)
  298.     backspace(leng-column);
  299. }
  300.  
  301.  
  302. char smile_menu(head,type, wrap, width, nkey)
  303. ed_line *head;
  304. char type, *wrap, *width, nkey;
  305. {
  306.   char key, *jet;
  307.   extern char *getstr();
  308.   if (type == FULL_MENU) {
  309.     prtstr("\nSMiLE -- Simple MIni Line Editor MENU   (C) Aug 1989 Alejandro Liu\n\n");
  310.     prtstr("S - Quick Save      A - Abort Editor    L - List Text       V - Preview Text\n");
  311.     prtstr("W - Wrd Wrp Toggle  N - Screen Width    H - Help Screen     C - Check Spelling\n");
  312.     prtstr("Command -->");
  313.   }
  314.   else
  315.     prtstr("Command: ");
  316.   key = getkey();
  317.   if (isupper(key))
  318.     key = tolower(key);
  319.   switch(key) {
  320.     case DOTSAV:
  321.       prtstr("Save Message\n");
  322.       return(QUICK_SAVE);
  323.     case DOTHLP:
  324.       smile_help(*wrap,*width);
  325.       break;
  326.     case DOTABT:
  327.       prtstr("Abort Message\n");
  328.       return(ABORT);
  329.     case DOTLST:
  330.       prtstr("List Message\n");
  331.       listmsg(*width,head);
  332.       break;
  333.     case DOTPVW:
  334.       prtstr("Preview Message\n");
  335.       preview(*width,head);
  336.       break;
  337.     case DOTWRW:
  338.       if (*wrap) {
  339.         *wrap = FALSE;
  340.         prtstr("Word Wrap Is Off\n");
  341.       }
  342.       else {
  343.         *wrap = TRUE;
  344.         prtstr("Word Wrap Is On\n");
  345.       }
  346.       break;
  347.     case DOTFNY:
  348.       prtstr("Funny Command\n");
  349.       prtstr("A Funny thing happened to me on my way  to the forum... \n");
  350.       prtstr("I Got there! hahahaha\n");
  351.       break;
  352.     case DOTSPC:
  353.       prtstr("Check Spelling\n");
  354.       prtstr("SMiLE Spell Checker\n");
  355.       prtstr("Sorry, the dictionary has been erased\n");
  356.       break;
  357.     case DOTCWD:
  358.       prtstr("Max Col? --> ");
  359.       jet = getstr();
  360.       *width = atoi(jet);
  361.       prtstr("\nScreen Width Set to = ");
  362.       printf("%d",*width);
  363.       prtchr(NEWLIN);
  364.       break;
  365.     default:
  366.       if ((type != FULL_MENU)&&(nkey == key)) {
  367.         delete(13);
  368.         return(key);
  369.       }
  370.       else {
  371.         beep();
  372.         prtstr("Unrecognized Command\n");
  373.       }
  374.   }
  375.   return(0);
  376. }
  377.  
  378. listmsg(width,head)
  379. char width;
  380. ed_line *head;
  381. {
  382.   char cnt = 1;
  383.   prtstr("SMiLE File Lister\n   ");
  384.   makebar(width);
  385.   while (head) {
  386.     if (((2+cnt)%24)==0)
  387.       pause();
  388.     printf("%2d>",cnt++);
  389.     prtstr(head->data);
  390.     prtchr(NEWLIN);
  391.     head = head->next;
  392.   }
  393.   prtchr(NEWLIN);
  394. }
  395.  
  396. preview(width,head)
  397. char width;
  398. ed_line *head;
  399. {
  400.   char cnt = 3;
  401.   prtstr("SMiLE File Previewer\n");
  402.   makebar(width);
  403.   while (head) {
  404.     if (((cnt++)%24)==0)
  405.       pause();
  406.     prtstr(head->data);
  407.     prtchr(NEWLIN);
  408.     head = head->next;
  409.   }
  410.   prtchr(NEWLIN);
  411. }
  412.  
  413. insertl(ptr)
  414. ed_line *ptr;
  415. {
  416.   ed_line *tmp, *tmp2, bogus;
  417.   tmp = (ed_line *)malloc(sizeof(bogus));
  418.   tmp->previous = ptr;
  419.   tmp2 = tmp->next = ptr->next;
  420.   ptr->next = tmp;
  421.   if (tmp2)
  422.     tmp2->previous = tmp;
  423.   tmp->data = (char *)malloc(2);
  424.   tmp->len = 0;
  425.   *(tmp->data) = 0;
  426. }
  427.  
  428. pull(col,buf,cur)
  429. char *col, *buf;
  430. ed_line *cur;
  431. {
  432.   strcpy(buf,cur->data);
  433.   cur->len = strlen(buf);
  434.   if (*col>(cur->len))
  435.     *col = cur->len;
  436. }
  437.  
  438. push(buf,cur)
  439. char *buf;
  440. ed_line *cur;
  441. {
  442.   free(cur->data);
  443.   cur->len = strlen(buf);
  444.   cur->data = (char *)malloc((cur->len) + 1);
  445.   strcpy(cur->data,buf);
  446. }
  447.  
  448. pause()
  449. {
  450.   prtstr("* MORE *");
  451.   beep();
  452.   getkey();
  453.   delete(8);
  454. }
  455.