home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume21 / mced / part01 / emacs_edit.c < prev    next >
C/C++ Source or Header  |  1991-08-09  |  7KB  |  351 lines

  1. /*
  2.  * This software is Copyright (c) 1991 by Andy Knight
  3.  *
  4.  * Permission is hereby granted to copy, distribute or otherwise
  5.  * use any part of this package as long as you do not try to make
  6.  * money from it or pretend that you wrote it.  This copyright
  7.  * notice must be maintained in any copy made.
  8.  *
  9.  * Use of this software constitutes acceptance for use in an AS IS
  10.  * condition. There are NO warranties with regard to this software.
  11.  * In no event shall the author be liable for any damages whatsoever
  12.  * arising out of or in connection with the use or performance of this
  13.  * software.  Any use of this software is at the user's own risk.
  14.  *
  15.  * If you make modifications to this software that you feel
  16.  * increases it usefulness for the rest of the community, please
  17.  * email the changes, enhancements, bug fixes as well as any and
  18.  * all ideas to me. This software is going to be maintained and
  19.  * enhanced as deemed necessary by the community.
  20.  *
  21.  *        Andy Knight
  22.  *        aknight@ourgang.prime.com
  23.  */
  24.  
  25. #include "config.h"
  26.  
  27. extern char *hist[], cstr[];
  28. extern int xbeg, cur_cmd, last_hline, pwolfe_getch();
  29. extern int edit_mode, x_pos, savex_pos, xend;
  30.  
  31. extern void my_wmove(), add_hline(), cmd_to_win(), win_to_cmd();
  32. extern void my_waddstr(), my_winsch(), case_upper();
  33. extern void my_wdelch(), case_lower(), eat_white();
  34. extern SIGTYPE die_curses(), die_normal();
  35. extern WINDOW *win;
  36.  
  37. #ifdef SYSVcurses
  38.     extern struct termio tio, tin;
  39. #else
  40.     extern struct tchars tco, tcn;
  41. #endif
  42.  
  43.  
  44. edit_cmd()
  45. {
  46.     int edch, tch, i, tmp_cmd;
  47.     fprintf(stdout,"\n");
  48.     if(initscr() == ERR)
  49.     {
  50.     fprintf(stderr, "Curses won't initialize - help!\n");
  51.     die_normal();
  52.     }
  53.     signal(SIGINT,die_curses);      /* die cleanly */
  54.     cbreak();
  55.     noecho();
  56. #ifdef SYSVcurses
  57.     win = newwin(2,COLS,0,0);
  58. #else
  59.     clearok(curscr, FALSE);    /* SYSV curses clears it anyway ;-( */
  60.     win = newwin(2,COLS,LINES-2,0);
  61. #endif
  62.  
  63.     cmd_to_win();
  64.     wrefresh(win);
  65.  
  66. #ifdef SYSVcurses        /* disable STOP/START (CTRL-S) */
  67.     if(ioctl(0, TCGETA, &tio) != 0)
  68.     perror("ioctl");
  69.     tin = tio;
  70.     tin.c_iflag &= ~IXON;
  71.     if(ioctl(0, TCSETA, &tin) != 0)
  72.     perror("ioctl");
  73. #else
  74.     if(ioctl(0, TIOCGETC, &tco) != 0)
  75.     perror("ioctl");
  76.     tcn = tco;
  77.     tcn.t_stopc = -1;
  78.     if(ioctl(0, TIOCSETC, &tcn) != 0)
  79.     perror("ioctl");
  80. #endif
  81.     edit_mode = EMACS_MODE;
  82.     for (; (edch = pwolfe_getch(win)) != '\n';)
  83.     {
  84.     switch (edch)
  85.     {
  86.         case ControlU:
  87.         case Sun_R3:
  88.         wclear(win);
  89.         wrefresh(win);
  90.         die_curses();
  91.         break;
  92.         case ControlW:    /*Delete word backwards*/
  93.         case EscapeDEL:
  94.         case Sun_R1:
  95.         if(x_pos > xbeg)
  96.         {
  97.             my_wmove(--x_pos);
  98.             eat_white(-1,YES);
  99.             for(;!(isspace(winch(win))) && (x_pos >= xbeg);)
  100.             {
  101.             my_wdelch();
  102.             if(x_pos == xbeg)
  103.                 break;
  104.             else
  105.                 my_wmove(--x_pos);
  106.             }
  107.             if(x_pos > xbeg)
  108.                 my_wmove(++x_pos);
  109.         }
  110.         else
  111.             beep();
  112.         break;
  113.         case EscapeB:    /*move back to beginning of previous word*/
  114.         case Sun_R4:
  115.         if(x_pos > xbeg)
  116.         {
  117.             my_wmove(--x_pos);
  118.             eat_white(-1,NO);
  119.             for(;!(isspace(winch(win))) && (x_pos > xbeg);)
  120.             {
  121.                 my_wmove(--x_pos);
  122.             }
  123.             if(x_pos > xbeg)
  124.                 my_wmove(++x_pos);
  125.         }
  126.         else
  127.             beep();
  128.         break;
  129.         case EscapeD:    /*delete forward to beginning of next word*/
  130.         if(x_pos < xend)
  131.         {
  132.             eat_white(1,YES);
  133.             for(;!(isspace(winch(win))) && (x_pos < xend);)
  134.             {
  135.             my_wdelch();
  136.             }
  137.         }
  138.         else
  139.             add_hline();
  140.         break;
  141.         case EscapeF:    /*move forward to beginning of next word*/
  142.         case Sun_R6:
  143.         if(x_pos < xend)
  144.         {
  145.             eat_white(1,NO);
  146.             for(;!(isspace(winch(win))) && (x_pos < xend);)
  147.             {
  148.                 my_wmove(++x_pos);
  149.             }
  150.         }
  151.         else
  152.             beep();
  153.         break;
  154.         case ControlL:    /* redraw win */
  155.         savex_pos = x_pos;
  156.         win_to_cmd();
  157.         wclear(win);
  158.         wrefresh(win);
  159.         cmd_to_win();
  160.         x_pos = savex_pos;
  161.         my_wmove(x_pos);
  162.         break;
  163.         case BackSpace:
  164.         case KEY_LEFT:
  165.         case ControlB:
  166.         case KEY_BACKSPACE:
  167.         case Sun_R10:
  168.         if(x_pos > xbeg)
  169.             my_wmove(--x_pos);
  170.         else
  171.             beep();
  172.         break;
  173.         case EscapeM:    /* move cursor to middle */
  174.         case Sun_R11:
  175.         x_pos = xbeg + (xend-xbeg)/2;
  176.         my_wmove(x_pos);
  177.         break;
  178.         case ControlF:
  179.         case KEY_RIGHT:
  180.         case Sun_R12:
  181.         if(x_pos < xend)
  182.             my_wmove(++x_pos);
  183.         else
  184.             beep();
  185.         break;
  186.         case KEY_UP:    /* move upward in history list */
  187.         case ControlP:
  188.         case Sun_R8:
  189.         if(cur_cmd > 0)
  190.         {
  191.             --cur_cmd;
  192.             strcpy(cstr, hist[cur_cmd]);
  193.             wclear(win);
  194.             cmd_to_win();
  195.             my_wmove(x_pos);
  196.         }
  197.         else
  198.             beep();
  199.         break;
  200.         case KEY_DOWN:    /* move downward in history list */
  201.         case ControlN:
  202.         case Sun_R14:
  203.         if(cur_cmd < last_hline)
  204.         {
  205.             ++cur_cmd;
  206.             strcpy(cstr, hist[cur_cmd]);
  207.             wclear(win);
  208.             cmd_to_win();
  209.             my_wmove(x_pos);
  210.         }
  211.         else
  212.             beep();
  213.         break;
  214.         case ControlR:    /*search history list backwards*/
  215.         if((tmp_cmd = index_cmd(cur_cmd - 1,-1)) != cur_cmd)
  216.         {
  217.             cur_cmd = tmp_cmd;
  218.             strcpy(cstr, hist[cur_cmd]);
  219.             wclear(win);
  220.             cmd_to_win();
  221.             my_wmove(x_pos);
  222.         }
  223.         else
  224.             beep();
  225.         break;
  226.         case ControlS:    /*search history list forwards*/
  227.         if((tmp_cmd = index_cmd(cur_cmd + 1,1)) != cur_cmd)
  228.         {
  229.             cur_cmd = tmp_cmd;
  230.             strcpy(cstr, hist[cur_cmd]);
  231.             wclear(win);
  232.             cmd_to_win();
  233.             my_wmove(x_pos);
  234.         }
  235.         else
  236.             beep();
  237.         break;
  238.         case ControlA:
  239.         case Sun_R7:
  240.         my_wmove(xbeg);
  241.         x_pos = xbeg;
  242.         break;
  243.         case ControlE:
  244.         case Sun_R13:
  245.         case Sun_R9:
  246.         my_wmove(xend);
  247.         x_pos = xend;
  248.         break;
  249.         case ControlK:    /*delete to end of line*/
  250.         if(x_pos < xend)
  251.         {
  252.             for(i = xend; i > x_pos; i--)
  253.             {
  254.             my_wdelch();
  255.             }
  256.         }
  257.         else
  258.                     add_hline();
  259.         break;
  260.         case Delete:    /*delete character before cursor*/
  261.         if(x_pos > xbeg)
  262.         {
  263.             my_wmove(--x_pos);
  264.             my_wdelch();
  265.         }
  266.         else
  267.             beep();
  268.         break;
  269.         case ControlD:    /*delete current character*/
  270.         if(x_pos < xend)
  271.         {
  272.             my_wdelch();
  273.         }
  274.         else
  275.             add_hline();
  276.         break;
  277.         case EscapeL:    /*lower case whole command*/
  278.         case Sun_R2:
  279.         savex_pos = x_pos;
  280.         win_to_cmd();
  281.         case_lower();
  282.         cmd_to_win();
  283.         x_pos = savex_pos;
  284.         my_wmove(x_pos);
  285.         break;
  286.         case EscapeU:    /*upper case whole command*/
  287.         savex_pos = x_pos;
  288.         win_to_cmd();
  289.         case_upper();
  290.         cmd_to_win();
  291.         x_pos = savex_pos;
  292.         my_wmove(x_pos);
  293.         break;
  294.         case EscapeC:    /* toggle case of current character */
  295.         case Sun_R5:
  296.         tch = winch(win);
  297.         if(isupper(tch))
  298.         {
  299.             wdelch(win);
  300.             winsch(win,tolower(tch));
  301.         }
  302.         else if(islower(tch))
  303.         {
  304.             wdelch(win);
  305.             winsch(win,toupper(tch));
  306.         }
  307.         if(x_pos < xend)
  308.             my_wmove(++x_pos);
  309.         break;
  310.         case ControlT:    /* transpose characters */
  311.         if(x_pos > xbeg && x_pos != COLS)
  312.         {
  313.             if(x_pos == xend)
  314.                 my_wmove(--x_pos);
  315.             my_wmove(--x_pos);
  316.             tch = winch(win);
  317.             wdelch(win);
  318.             my_wmove(++x_pos);
  319.             winsch(win,tch);
  320.             my_wmove(++x_pos);
  321.         }
  322.         else
  323.             beep();
  324.         break;
  325.         default:        /* insert character */
  326.         if(isprint(edch))
  327.         {
  328.             my_winsch(edch);
  329.         }
  330.         else
  331.             beep();
  332.         break;
  333.     }
  334.     wrefresh(win);
  335.     if (xend == xbeg)
  336.         die_curses();
  337.     }
  338.     win_to_cmd();
  339. #ifdef SYSVcurses    /* reset tty */
  340.     if(ioctl(0, TCSETA, &tio) != 0)
  341.     perror("ioctl");
  342. #else
  343.     if(ioctl(0, TIOCSETC, &tco) != 0)
  344.     perror("ioctl");
  345. #endif
  346.     nocbreak();
  347.     echo();
  348.     endwin();
  349.     return;    /* finished, execute command */
  350. }
  351.