home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / jove / part06 / misc.c next >
Encoding:
C/C++ Source or Header  |  1987-02-02  |  7.4 KB  |  502 lines

  1. /************************************************************************
  2.  * This program is Copyright (C) 1986 by Jonathan Payne.  JOVE is       *
  3.  * provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is *
  5.  * included in all the files.                                           *
  6.  ************************************************************************/
  7.  
  8. #include "jove.h"
  9. #include "ctype.h"
  10. #include <signal.h>
  11. #ifdef ANSICODES
  12. #include "termcap.h"
  13. #endif
  14.  
  15. Digit()
  16. {
  17.     GetExp(LastKeyStruck);
  18. }
  19.  
  20. Digit0()
  21. {
  22.     GetExp('0');
  23. }
  24.  
  25. Digit1()
  26. {
  27.     GetExp('1');
  28. }
  29.  
  30. Digit2()
  31. {
  32.     GetExp('2');
  33. }
  34.  
  35. Digit3()
  36. {
  37.     GetExp('3');
  38. }
  39.  
  40. Digit4()
  41. {
  42.     GetExp('4');
  43. }
  44.  
  45. Digit5()
  46. {
  47.     GetExp('5');
  48. }
  49.  
  50. Digit6()
  51. {
  52.     GetExp('6');
  53. }
  54.  
  55. Digit7()
  56. {
  57.     GetExp('7');
  58. }
  59.  
  60. Digit8()
  61. {
  62.     GetExp('8');
  63. }
  64.  
  65. Digit9()
  66. {
  67.     GetExp('9');
  68. }
  69.  
  70. prCTIME()
  71. {
  72.     s_mess(": %f %s", get_time((time_t *) 0, (char *) 0, 0, -1));
  73. }
  74.  
  75. extern int    alarmed;
  76.  
  77. FourTime()
  78. {
  79.     int    oldc = LastKeyStruck,
  80.         newc;
  81.     int    nexp;
  82.  
  83.     alarmed = 0;
  84.     exp_p = YES;
  85.     this_cmd = ARG_CMD;
  86.     do {
  87.         if ((nexp = exp * 4) != 0)
  88.             exp = nexp;
  89.         if (!alarmed)
  90.             newc = waitchar();
  91.         else
  92.             newc = getch();
  93.         if (alarmed)
  94.             message(key_strokes);
  95.     } while (newc == oldc);
  96.     Ungetc(newc);
  97. }
  98.  
  99. int    exp_p,
  100.     exp;
  101.  
  102. GetExp(c)
  103. {
  104.     int    sign = 0;
  105.     static int    digited;
  106.  
  107.     if (!isdigit(c) && c != '-')
  108.         complain((char *) 0);
  109.     if (exp_p == NO) {    /* if we just got here */
  110.         exp = 0;    /* start over */
  111.         digited = NO;
  112.     } else if (exp_p == YES_NODIGIT) {
  113.         sign = (exp < 0) ? -1 : 1;
  114.         exp = 0;
  115.     }
  116.  
  117.     if (!sign)
  118.         sign = (exp < 0) ? -1 : 1;
  119.     if (sign == -1)
  120.         exp = -exp;
  121.     if (c == '-') {
  122.         sign = -sign;
  123.         goto goread;
  124.     }
  125.     for (;;) {
  126.         if (alarmed)
  127.             message(key_strokes);
  128.         if (isdigit(c)) {
  129.             exp = (exp * 10) + (c - '0');
  130.             digited++;
  131.         } else {
  132.             if (digited)
  133.                 exp_p = YES;
  134.             else {
  135.                 exp = 1;
  136.                 if (exp_p == NO)
  137.                     exp_p = YES_NODIGIT;
  138.             }
  139.             exp *= sign;
  140.             this_cmd = ARG_CMD;
  141.             Ungetc(c);
  142.             return;
  143.         }
  144. goread:        if (!alarmed)
  145.             c = waitchar();
  146.         else {
  147.             add_mess(NullStr);
  148.             c = getch();
  149.         }
  150.     }
  151. }
  152.  
  153. ChrToOct()
  154. {
  155.     int    c;
  156.  
  157.     c = waitchar();
  158.     if (alarmed)
  159.         message(key_strokes);
  160.     ins_str(sprint("\\%03o", c), NO);
  161. }
  162.  
  163. StrLength()
  164. {
  165.     static char    inquotes[] = "Where are the quotes?";
  166.     char    *first = StrIndex(-1, linebuf, curchar, '"'),
  167.         *last = StrIndex(1, linebuf, curchar + 1, '"'),
  168.         c;
  169.     int    numchars = 0;
  170.  
  171.     if (first == 0 || last == 0)
  172.         complain(inquotes);
  173.     first++;
  174.     while (first < last) {
  175.         c = *first++;
  176.         if (c == '\\') {
  177.             int    num;
  178.  
  179.             if (!isdigit(*first))
  180.                 ++first;
  181.             else {
  182.                 num = 3;
  183.                 while (num-- && isdigit(*first++) && first < last)
  184.                     ;
  185.             }
  186.         }
  187.         numchars++;
  188.     }
  189.     s_mess("%d characters", numchars);
  190. }
  191.  
  192. /* Transpos cur_char with cur_char - 1 */
  193.  
  194. TransChar()
  195. {
  196.     char    before;
  197.  
  198.     if (curchar == 0 || (eolp() && curchar == 1))
  199.         complain((char *) 0);    /* BEEP */
  200.     exp = 1;
  201.     if (eolp())
  202.         BackChar();
  203.     before = linebuf[curchar - 1];
  204.     DelPChar();
  205.     ForChar();
  206.     Insert(before);
  207. }
  208.  
  209. /* Switch current line with previous one */
  210.  
  211. TransLines()
  212. {
  213.     disk_line    old_prev;
  214.  
  215.     if (firstp(curline))
  216.         return;
  217.     exp = 1;
  218.     lsave();
  219.     old_prev = curline->l_prev->l_dline;
  220.     curline->l_prev->l_dline = curline->l_dline;
  221.     curline->l_dline = old_prev;
  222.     getDOT();
  223.     if (!lastp(curline))
  224.         line_move(FORWARD, NO);
  225.     modify();
  226. }
  227.  
  228. Leave()
  229. {
  230.     longjmp(mainjmp, QUIT);
  231. }
  232.  
  233. /* If argument is specified, kill that many lines down.  Otherwise,
  234.    if we "appear" to be at the end of a line, i.e. everything to the
  235.    right of the cursor is white space, we delete the line separator
  236.    as if we were at the end of the line. */
  237.  
  238. KillEOL()
  239. {
  240.     Line    *line2;
  241.     int    char2;
  242.  
  243.     if (exp_p) {
  244.         if (exp == 0) {    /* Kill to beginning of line */
  245.             line2 = curline;
  246.             char2 = 0;
  247.         } else {
  248.             line2 = next_line(curline, exp);
  249.             if ((LineDist(curline, line2) < exp) || (line2 == curline))
  250.                 char2 = length(line2);
  251.             else
  252.                 char2 = 0;
  253.         }
  254.     } else if (blnkp(&linebuf[curchar])) {
  255.         line2 = next_line(curline, 1);
  256.         if (line2 == curline)
  257.             char2 = length(curline);
  258.         else
  259.             char2 = 0;
  260.     } else {
  261.         line2 = curline;
  262.         char2 = length(curline);
  263.     }
  264.     reg_kill(line2, char2, 0);
  265. }
  266.  
  267. /* Kill to beginning of sentence */
  268.  
  269. KillBos()
  270. {
  271.     exp = -exp;
  272.     KillEos();
  273. }
  274.  
  275. /* Kill to end of sentence */
  276.  
  277. KillEos()
  278. {
  279.     Line    *line1;
  280.     int    char1;
  281.  
  282.     line1 = curline;
  283.     char1 = curchar;
  284.     Eos();
  285.     reg_kill(line1, char1, 1);
  286. }
  287.  
  288. KillExpr()
  289. {
  290.     Line    *line1;
  291.     int    char1;
  292.  
  293.     line1 = curline;
  294.     char1 = curchar;
  295.     FSexpr();
  296.     reg_kill(line1, char1, 1);
  297. }
  298.  
  299. EscPrefix()
  300. {
  301.     HandlePref(pref1map);
  302. }
  303.  
  304. CtlxPrefix()
  305. {
  306.     HandlePref(pref2map);
  307. }
  308.  
  309. MiscPrefix()
  310. {
  311.     HandlePref(miscmap);
  312. }
  313.  
  314. HandlePref(map)
  315. data_obj    **map;
  316. {
  317.     register data_obj    *cp;
  318.     register int    c;
  319.  
  320.     c = waitchar();
  321.     if (c == CTL(G)) {
  322.         message("[Aborted]");
  323.         rbell();
  324.         return;
  325.     }
  326.  
  327.     if (alarmed)
  328.         message(key_strokes);
  329.  
  330.     cp = map[c];
  331.     if (cp == 0) {
  332.         s_mess("[%sunbound]", key_strokes);
  333.         rbell();
  334.     } else
  335.         ExecCmd(cp);
  336. }
  337.  
  338. Yank()
  339. {
  340.     Line    *line,
  341.         *lp;
  342.     Bufpos    *dot;
  343.  
  344.     if (killbuf[killptr] == 0)
  345.         complain("[Nothing to yank!]");
  346.     lsave();
  347.     this_cmd = YANKCMD;
  348.     line = killbuf[killptr];
  349.     lp = lastline(line);
  350.     dot = DoYank(line, 0, lp, length(lp), curline, curchar, curbuf);
  351.     SetMark();
  352.     SetDot(dot);
  353. }
  354.  
  355. WtModBuf()
  356. {
  357.     if (!ModBufs(NO))
  358.         message("[No buffers need saving]");
  359.     else
  360.         put_bufs(exp_p);
  361. }
  362.  
  363. put_bufs(askp)
  364. {
  365.     register Buffer    *oldb = curbuf,    
  366.             *b;        
  367.  
  368.     for (b = world; b != 0; b = b->b_next) {
  369.         if (!IsModified(b) || b->b_type != B_FILE)
  370.             continue;
  371.         SetBuf(b);    /* Make this current Buffer */
  372.         if (curbuf->b_fname == 0) {
  373.             char    *newname;
  374.  
  375.             newname = ask(NullStr, "Buffer \"%s\" needs a file name; type Return to skip: ", b->b_name);
  376.             if (*newname == 0)
  377.                 continue;
  378.             setfname(b, newname);
  379.         }
  380.         if (askp && (yes_or_no_p("Write %s? ", curbuf->b_fname) == NO))
  381.             continue;
  382.         filemunge(curbuf->b_fname);
  383.         chk_mtime(curbuf, curbuf->b_fname, "save");
  384.         file_write(curbuf->b_fname, 0);
  385.         unmodify();
  386.     }
  387.     SetBuf(oldb);
  388. }
  389.  
  390. ToIndent()
  391. {
  392.     register char    *cp,
  393.             c;
  394.  
  395.     for (cp = linebuf; c = *cp; cp++)
  396.         if (c != ' ' && c != '\t')
  397.             break;
  398.     curchar = cp - linebuf;
  399. }
  400.  
  401. GoLine()
  402. {
  403.     Line    *newline;
  404.  
  405. #ifndef ANSICODES
  406.     if (exp_p == NO)
  407.         return;
  408. #else
  409.     if (exp_p == NO || exp <= 0) {
  410.         if (SP)
  411.             putpad(SP, 1);    /* Ask for cursor position */
  412.         return;
  413.     }
  414. #endif
  415.     newline = next_line(curbuf->b_first, exp - 1);
  416.     PushPntp(newline);
  417.     SetLine(newline);
  418. }
  419.  
  420. #ifdef ANSICODES
  421. MoveToCursor(line, col)
  422. {
  423.     register struct scrimage *sp = &PhysScreen[line];
  424.  
  425.     while (sp->s_id == NULL)
  426.         sp = &PhysScreen[--line];
  427.     if (sp->s_flags & MODELINE)
  428.         complain((char *) 0);
  429.     if (curwind != sp->s_window)
  430.         SetWind(sp->s_window);
  431.     SetLine(sp->s_lp);
  432.     curchar = how_far(sp->s_lp, col);
  433. }
  434.  
  435. AnsiCodes()
  436. {
  437.     int    c;
  438.     int    num1 = 0;
  439.     int    num2;
  440.     static char *unsupported = "[Unsupported ANSI code received]";
  441.  
  442.     while (isdigit(c = getch()))
  443.         num1 = (num1*10) + (c - '0');
  444.  
  445.     switch (c) {
  446.     case ';':
  447.         num2 = 0;
  448.         while (isdigit(c = getch()))
  449.             num2 = (num2*10) + (c - '0');
  450.         switch (c) {
  451.         case 'R':
  452.             MoveToCursor(--num1, --num2);
  453.             break;
  454.         case 'H':
  455.             Eow(); Bol();
  456.             break;
  457.         default:
  458.             complain(unsupported);
  459.         }
  460.         break;
  461.     case 'A':
  462.         line_move(BACKWARD, YES);
  463.         break;
  464.     case 'B':
  465.         line_move(FORWARD, YES);
  466.         break;
  467.     case 'C':
  468.         ForChar();
  469.         break;
  470.     case 'D':
  471.         BackChar();
  472.         break;
  473.     case 'H':
  474.         Bow();
  475.         break;
  476.     case 'J':
  477.         if (num1 == 2) {
  478.             ClAndRedraw();
  479.             break;
  480.         }
  481.         /* FALL THROUGH */
  482.     default:
  483.         complain(unsupported);
  484.     }
  485. }
  486. #endif ANSICODES
  487.  
  488. NotModified()
  489. {
  490.     unmodify();
  491. }
  492.  
  493. SetLMargin()
  494. {
  495.     LMargin = calc_pos(linebuf, curchar);
  496. }
  497.  
  498. SetRMargin()
  499. {
  500.     RMargin = calc_pos(linebuf, curchar);
  501. }
  502.