home *** CD-ROM | disk | FTP | other *** search
- /*
- * This software is Copyright (c) 1991 by Andy Knight
- *
- * Permission is hereby granted to copy, distribute or otherwise
- * use any part of this package as long as you do not try to make
- * money from it or pretend that you wrote it. This copyright
- * notice must be maintained in any copy made.
- *
- * Use of this software constitutes acceptance for use in an AS IS
- * condition. There are NO warranties with regard to this software.
- * In no event shall the author be liable for any damages whatsoever
- * arising out of or in connection with the use or performance of this
- * software. Any use of this software is at the user's own risk.
- *
- * If you make modifications to this software that you feel
- * increases it usefulness for the rest of the community, please
- * email the changes, enhancements, bug fixes as well as any and
- * all ideas to me. This software is going to be maintained and
- * enhanced as deemed necessary by the community.
- *
- * " ... Freely you have recieved, freely give" <Matthew 10:8>
- *
- * Andy Knight
- * aknight@ourgang.prime.com
- */
-
- #include "config.h"
-
- extern char *hist[], cstr[];
- extern int xbeg, cur_cmd, last_hline, pwolfe_getch();
- extern int edit_mode, x_pos, savex_pos, xend;
-
- extern void my_wmove(), add_hline(), cmd_to_win(), win_to_cmd();
- extern void my_waddstr(), my_winsch(), case_upper();
- extern void my_wdelch(), case_lower(), eat_white();
- extern SIGTYPE die_curses(), die_normal();
- extern WINDOW *win;
-
- #ifdef SYSVcurses
- extern struct termio tio, tin;
- #else
- extern struct tchars tco, tcn;
- #endif
-
-
- edit_cmd()
- {
- int edch, tch, i, tmp_cmd;
- fprintf(stdout,"\n");
- if(initscr() == ERR)
- {
- fprintf(stderr, "Curses won't initialize - help!\n");
- die_normal();
- }
- signal(SIGINT,die_curses); /* die cleanly */
- CBREAKF();
- noecho();
- #ifdef SYSVcurses
- win = newwin(2,COLS,0,0);
- #else
- clearok(curscr, FALSE); /* SYSV curses clears it anyway ;-( */
- win = newwin(2,COLS,LINES-2,0);
- #endif
-
- cmd_to_win();
- wrefresh(win);
-
- #ifdef SYSVcurses /* disable STOP/START (CTRL-S) */
- if(ioctl(0, TCGETA, &tio) != 0)
- perror("ioctl");
- tin = tio;
- tin.c_iflag &= ~IXON;
- if(ioctl(0, TCSETA, &tin) != 0)
- perror("ioctl");
- #else
- if(ioctl(0, TIOCGETC, &tco) != 0)
- perror("ioctl");
- tcn = tco;
- tcn.t_stopc = -1;
- if(ioctl(0, TIOCSETC, &tcn) != 0)
- perror("ioctl");
- #endif
- edit_mode = EMACS_MODE;
- for (; (edch = pwolfe_getch(win)) != '\n';)
- {
- switch (edch)
- {
- case ControlU:
- case Sun_R3:
- wclear(win);
- wrefresh(win);
- die_curses();
- break;
- case ControlW: /*Delete word backwards*/
- case EscapeDEL:
- case Sun_R1:
- if(x_pos > xbeg)
- {
- my_wmove(--x_pos);
- eat_white(-1,YES);
- for(;!(isspace(winch(win))) && (x_pos >= xbeg);)
- {
- my_wdelch();
- if(x_pos == xbeg)
- break;
- else
- my_wmove(--x_pos);
- }
- if(x_pos > xbeg)
- my_wmove(++x_pos);
- }
- else
- beep();
- break;
- case EscapeB: /*move back to beginning of previous word*/
- case Sun_R4:
- if(x_pos > xbeg)
- {
- my_wmove(--x_pos);
- eat_white(-1,NO);
- for(;!(isspace(winch(win))) && (x_pos > xbeg);)
- {
- my_wmove(--x_pos);
- }
- if(x_pos > xbeg)
- my_wmove(++x_pos);
- }
- else
- beep();
- break;
- case EscapeD: /*delete forward to beginning of next word*/
- if(x_pos < xend)
- {
- eat_white(1,YES);
- for(;!(isspace(winch(win))) && (x_pos < xend);)
- {
- my_wdelch();
- }
- }
- else
- add_hline();
- break;
- case EscapeF: /*move forward to beginning of next word*/
- case Sun_R6:
- if(x_pos < xend)
- {
- eat_white(1,NO);
- for(;!(isspace(winch(win))) && (x_pos < xend);)
- {
- my_wmove(++x_pos);
- }
- }
- else
- beep();
- break;
- case ControlL: /* redraw win */
- savex_pos = x_pos;
- win_to_cmd();
- wclear(win);
- wrefresh(win);
- cmd_to_win();
- x_pos = savex_pos;
- my_wmove(x_pos);
- break;
- case BackSpace:
- case KEY_LEFT:
- case ControlB:
- case KEY_BACKSPACE:
- case Sun_R10:
- if(x_pos > xbeg)
- my_wmove(--x_pos);
- else
- beep();
- break;
- case EscapeM: /* move cursor to middle */
- case Sun_R11:
- x_pos = xbeg + (xend-xbeg)/2;
- my_wmove(x_pos);
- break;
- case ControlF:
- case KEY_RIGHT:
- case Sun_R12:
- if(x_pos < xend)
- my_wmove(++x_pos);
- else
- beep();
- break;
- case KEY_UP: /* move upward in history list */
- case ControlP:
- case Sun_R8:
- if(cur_cmd > 0)
- {
- --cur_cmd;
- strcpy(cstr, hist[cur_cmd]);
- wclear(win);
- cmd_to_win();
- my_wmove(x_pos);
- }
- else
- beep();
- break;
- case KEY_DOWN: /* move downward in history list */
- case ControlN:
- case Sun_R14:
- if(cur_cmd < last_hline)
- {
- ++cur_cmd;
- strcpy(cstr, hist[cur_cmd]);
- wclear(win);
- cmd_to_win();
- my_wmove(x_pos);
- }
- else
- beep();
- break;
- case ControlR: /*search history list backwards*/
- if((tmp_cmd = index_cmd(cur_cmd - 1,-1)) != cur_cmd)
- {
- cur_cmd = tmp_cmd;
- strcpy(cstr, hist[cur_cmd]);
- wclear(win);
- cmd_to_win();
- my_wmove(x_pos);
- }
- else
- beep();
- break;
- case ControlS: /*search history list forwards*/
- if((tmp_cmd = index_cmd(cur_cmd + 1,1)) != cur_cmd)
- {
- cur_cmd = tmp_cmd;
- strcpy(cstr, hist[cur_cmd]);
- wclear(win);
- cmd_to_win();
- my_wmove(x_pos);
- }
- else
- beep();
- break;
- case ControlA:
- case Sun_R7:
- my_wmove(xbeg);
- x_pos = xbeg;
- break;
- case ControlE:
- case Sun_R13:
- case Sun_R9:
- my_wmove(xend);
- x_pos = xend;
- break;
- case ControlK: /*delete to end of line*/
- if(x_pos < xend)
- {
- for(i = xend; i > x_pos; i--)
- {
- my_wdelch();
- }
- }
- else
- add_hline();
- break;
- case Delete: /*delete character before cursor*/
- if(x_pos > xbeg)
- {
- my_wmove(--x_pos);
- my_wdelch();
- }
- else
- beep();
- break;
- case ControlD: /*delete current character*/
- if(x_pos < xend)
- {
- my_wdelch();
- }
- else
- add_hline();
- break;
- case EscapeL: /*lower case whole command*/
- case Sun_R2:
- savex_pos = x_pos;
- win_to_cmd();
- case_lower();
- cmd_to_win();
- x_pos = savex_pos;
- my_wmove(x_pos);
- break;
- case EscapeU: /*upper case whole command*/
- savex_pos = x_pos;
- win_to_cmd();
- case_upper();
- cmd_to_win();
- x_pos = savex_pos;
- my_wmove(x_pos);
- break;
- case EscapeC: /* toggle case of current character */
- case Sun_R5:
- tch = winch(win);
- if(isupper(tch))
- {
- wdelch(win);
- winsch(win,tolower(tch));
- }
- else if(islower(tch))
- {
- wdelch(win);
- winsch(win,toupper(tch));
- }
- if(x_pos < xend)
- my_wmove(++x_pos);
- break;
- case ControlT: /* transpose characters */
- if(x_pos > xbeg && x_pos != COLS)
- {
- if(x_pos == xend)
- my_wmove(--x_pos);
- my_wmove(--x_pos);
- tch = winch(win);
- wdelch(win);
- my_wmove(++x_pos);
- winsch(win,tch);
- my_wmove(++x_pos);
- }
- else
- beep();
- break;
- default: /* insert character */
- if(isprint(edch))
- {
- my_winsch(edch);
- }
- else
- beep();
- break;
- }
- wrefresh(win);
- if (xend == xbeg)
- die_curses();
- }
- win_to_cmd();
- #ifdef SYSVcurses /* reset tty */
- if(ioctl(0, TCSETA, &tio) != 0)
- perror("ioctl");
- #else
- if(ioctl(0, TIOCSETC, &tco) != 0)
- perror("ioctl");
- #endif
- NOCBREAKF();
- echo();
- endwin();
- return; /* finished, execute command */
- }
-