home *** CD-ROM | disk | FTP | other *** search
- /* SMiLE -- Simple MIni Line Editor
- Copyright (C) 1989 By Alejandro Liu
- You are given permission to Freely Copy and distribute this program
- in its unmodifed form provided that this copyright notice is included.
- */
-
- #include "smile.h"
- #include "bsd_tty.h"
- #include <ctype.h>
- #include <stdio.h>
- #include <strings.h>
-
- #define TRUE 1
- #define FALSE 0
- #define TSTR 100
-
- makebar(width)
- char width;
- {
- int i;
- for (i = 0;i<width;i++) {
- if (i%8)
- prtchr('-');
- else
- prtchr('!');
- };
- if ((width-1)%8)
- prtchr('!');
- prtchr(NEWLIN);
- }
-
- smile_intro(width)
- int width;
- {
- prtstr("\n");
- prtstr("SMiLE -- Simple MIni Line Editor (C) By Alejandro Liu Aug 1989\n");
- prtstr("Press ESC (CTRL+[) for menu. CTRL+X to exit SMiLE\n");
- prtstr(" ");
- makebar(width);
- }
-
- int smile(head,wwrap,width)
- ed_line *head;
- char wwrap,width;
- {
- ed_line *current, *temp;
- char c_buf[TSTR], c_lin, c_col, key, i, j;
- extern char smile_menu();
-
- c_col = 0;
- c_lin = 1;
- current = head;
- smile_intro(width);
- pull(&c_col, c_buf, current);
- showlin(c_lin,c_buf,c_col);
-
- for (;;) {
- key = 127 & getkey();
- switch (key) {
- case PREV_L:
- if (c_lin-1) {
- c_lin--;
- push(c_buf,current);
- temp = current->previous;
- current = temp;
- if (current == NULL) current = head;
- pull(&c_col, c_buf, current);
- prtchr(NEWLIN);
- showlin(c_lin,c_buf, c_col);
- }
- else
- beep();
- break;
- case NEXT_L:
- if (current->next == NULL)
- beep();
- else {
- c_lin++;
- push(c_buf, current);
- temp = current->next;
- current = temp;
- pull(&c_col, c_buf, current);
- prtchr(NEWLIN);
- showlin(c_lin,c_buf,c_col);
- }
- break;
- case INSER_L:
- case INSERT_L:
- insertl(current);
- push(c_buf,current);
- temp = current->next;
- current = temp;
- current->len = 0;
- c_col = 0;
- c_lin++;
- c_buf[0] = 0;
- prtchr(NEWLIN);
- showlin(c_lin,c_buf,c_col);
- break;
- case LEFT_C:
- if (c_col) {
- c_col--;
- backspace(1);
- }
- else
- beep();
- break;
- case RIGHT_C:
- if (c_col == current->len)
- beep();
- else {
- putchar(c_buf[c_col]);
- c_col++;
- }
- break;
- case DEL_00:
- case DEL_01:
- case DEL_02:
- if (c_col) {
- if (c_col == current->len) {
- delete(1);
- c_buf[c_col--] = 0;
- (current->len)--;
- }
- else {
- delete(1);
- for (i = c_col; i<=current->len;i++) {
- c_buf[i-1] = c_buf[i];
- if (c_buf[i])
- prtchr(c_buf[i]);
- else
- prtchr(' ');
- }
- backspace((current->len) - c_col + 1);
- c_col--;
- current->len--;
- }
- }
- else
- beep();
- break;
- case KILL_L:
- if (c_col == current->len) {
- if (current->next) {
- char *mirage;
- temp = current->next;
- delete(c_col + 3);
- c_col = strlen(c_buf) + strlen(temp->data);
- mirage = (char *)malloc(c_col + 1);
- strcpy(mirage,c_buf);
- strcat(mirage,temp->data);
- current->next = temp->next;
- free(temp->data);
- free(temp);
- if (temp = current->next)
- temp->previous = current;
- if (c_col >= width) {
- c_col = width -1;
- mirage[c_col] = 0;
- }
- current->len = c_col;
- strcpy(c_buf,mirage);
- free(mirage);
- showlin(c_lin, c_buf, c_col);
- }
- else
- beep();
- }
- else {
- for (i=c_col;i<current->len;i++)
- prtchr(' ');
- backspace(current->len - c_col);
- c_buf[current->len = c_col] = 0;
- }
- break;
- case MSG_LST:
- push(c_buf,current);
- listmsg(width, head);
- showlin(c_lin, c_buf, c_col);
- break;
- case PREVIEW:
- push(c_buf,current);
- preview(width, head);
- showlin(c_lin, c_buf, c_col);
- break;
- case EXIT:
- push(c_buf, current);
- return(NORMAL_SAVE);
- case MENU:
- push(c_buf,current);
- if (i = smile_menu(head,FULL_MENU, &wwrap, &width, MENU))
- if ((i == NORMAL_SAVE) || (i == QUICK_SAVE) || (i == ABORT)) {
- push(c_buf,current);
- return(i);
- }
- showlin(c_lin,c_buf,c_col);
- break;
- case DOTKEY0:
- case DOTKEY1:
- if (c_col == 0) {
- push(c_buf,current);
- if (i = smile_menu(head,DOT_MENU, &wwrap, &width, key)) {
- switch (i) {
- case NORMAL_SAVE:
- case QUICK_SAVE:
- case ABORT:
- push(c_buf,current);
- return(i);
- case DOTKEY0:
- case DOTKEY1:
- insertc(c_buf, &c_col, &(current->len), i);
- delete(1);
- break;
- }
- }
- showlin(c_lin,c_buf,c_col);
- break;
- }
- default:
- if ((31<key)&&(key<127)) {
- if (c_col == width) {
- if (wwrap) {
- i = c_col;
- while (i&&(c_buf[i--]!=32));
- if (i) {
- if ((c_col-i-1)>0)
- delete(c_col-i-1);
- c_buf[++i] = 0;
- insertl(current);
- push(c_buf,current);
- current = current->next;
- c_lin++;
- push(&c_buf[i+1], current);
- pull(&c_col, c_buf, current);
- c_col = current->len;
- prtchr(NEWLIN);
- showlin(c_lin,c_buf,c_col);
- }
- else {
- insertl(current);
- push(c_buf, current);
- current = current->next;
- current->len = 0;
- c_col = 0;
- c_lin++;
- c_buf[0]= 0;
- prtchr(NEWLIN);
- showlin(c_lin,c_buf,c_col);
- }
- insertc(c_buf,&c_col, &(current->len),key);
- }
- else
- beep();
- }
- else
- insertc(c_buf, &c_col, &(current->len),key);
- }
- else
- beep();
- }
- }
- }
-
- insertc(buffer, col, len, key)
- char buffer[], *col, *len, key;
- {
- buffer[*col] = key;
- if ((*col)++ == (*len))
- buffer[++(*len)] = 0;
- prtchr(key);
- }
-
- backspace(rept)
- char rept;
- {
- while (rept--)
- prtchr(8);
- }
-
- delete(rept)
- char rept;
- {
- while (rept--) {
- prtchr(8);
- prtchr(' ');
- prtchr(8);
- }
- }
-
- showlin(line,buffer,column)
- char line, column, *buffer;
- {
- char leng;
- printf("%2d>",line);
- prtstr(buffer);
- leng = strlen(buffer);
- if (leng != column)
- backspace(leng-column);
- }
-
-
- char smile_menu(head,type, wrap, width, nkey)
- ed_line *head;
- char type, *wrap, *width, nkey;
- {
- char key, *jet;
- extern char *getstr();
- if (type == FULL_MENU) {
- prtstr("\nSMiLE -- Simple MIni Line Editor MENU (C) Aug 1989 Alejandro Liu\n\n");
- prtstr("S - Quick Save A - Abort Editor L - List Text V - Preview Text\n");
- prtstr("W - Wrd Wrp Toggle N - Screen Width H - Help Screen C - Check Spelling\n");
- prtstr("Command -->");
- }
- else
- prtstr("Command: ");
- key = getkey();
- if (isupper(key))
- key = tolower(key);
- switch(key) {
- case DOTSAV:
- prtstr("Save Message\n");
- return(QUICK_SAVE);
- case DOTHLP:
- smile_help(*wrap,*width);
- break;
- case DOTABT:
- prtstr("Abort Message\n");
- return(ABORT);
- case DOTLST:
- prtstr("List Message\n");
- listmsg(*width,head);
- break;
- case DOTPVW:
- prtstr("Preview Message\n");
- preview(*width,head);
- break;
- case DOTWRW:
- if (*wrap) {
- *wrap = FALSE;
- prtstr("Word Wrap Is Off\n");
- }
- else {
- *wrap = TRUE;
- prtstr("Word Wrap Is On\n");
- }
- break;
- case DOTFNY:
- prtstr("Funny Command\n");
- prtstr("A Funny thing happened to me on my way to the forum... \n");
- prtstr("I Got there! hahahaha\n");
- break;
- case DOTSPC:
- prtstr("Check Spelling\n");
- prtstr("SMiLE Spell Checker\n");
- prtstr("Sorry, the dictionary has been erased\n");
- break;
- case DOTCWD:
- prtstr("Max Col? --> ");
- jet = getstr();
- *width = atoi(jet);
- prtstr("\nScreen Width Set to = ");
- printf("%d",*width);
- prtchr(NEWLIN);
- break;
- default:
- if ((type != FULL_MENU)&&(nkey == key)) {
- delete(13);
- return(key);
- }
- else {
- beep();
- prtstr("Unrecognized Command\n");
- }
- }
- return(0);
- }
-
- listmsg(width,head)
- char width;
- ed_line *head;
- {
- char cnt = 1;
- prtstr("SMiLE File Lister\n ");
- makebar(width);
- while (head) {
- if (((2+cnt)%24)==0)
- pause();
- printf("%2d>",cnt++);
- prtstr(head->data);
- prtchr(NEWLIN);
- head = head->next;
- }
- prtchr(NEWLIN);
- }
-
- preview(width,head)
- char width;
- ed_line *head;
- {
- char cnt = 3;
- prtstr("SMiLE File Previewer\n");
- makebar(width);
- while (head) {
- if (((cnt++)%24)==0)
- pause();
- prtstr(head->data);
- prtchr(NEWLIN);
- head = head->next;
- }
- prtchr(NEWLIN);
- }
-
- insertl(ptr)
- ed_line *ptr;
- {
- ed_line *tmp, *tmp2, bogus;
- tmp = (ed_line *)malloc(sizeof(bogus));
- tmp->previous = ptr;
- tmp2 = tmp->next = ptr->next;
- ptr->next = tmp;
- if (tmp2)
- tmp2->previous = tmp;
- tmp->data = (char *)malloc(2);
- tmp->len = 0;
- *(tmp->data) = 0;
- }
-
- pull(col,buf,cur)
- char *col, *buf;
- ed_line *cur;
- {
- strcpy(buf,cur->data);
- cur->len = strlen(buf);
- if (*col>(cur->len))
- *col = cur->len;
- }
-
- push(buf,cur)
- char *buf;
- ed_line *cur;
- {
- free(cur->data);
- cur->len = strlen(buf);
- cur->data = (char *)malloc((cur->len) + 1);
- strcpy(cur->data,buf);
- }
-
- pause()
- {
- prtstr("* MORE *");
- beep();
- getkey();
- delete(8);
- }
-