home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Shells / zsh-3.0.5-MIHS / src / Src / zle_word.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-28  |  7.6 KB  |  440 lines

  1. /*
  2.  * $Id: zle_word.c,v 2.6 1996/10/15 20:16:35 hzoli Exp $
  3.  *
  4.  * zle_word.c - word-related editor functions
  5.  *
  6.  * This file is part of zsh, the Z shell.
  7.  *
  8.  * Copyright (c) 1992-1996 Paul Falstad
  9.  * All rights reserved.
  10.  *
  11.  * Permission is hereby granted, without written agreement and without
  12.  * license or royalty fees, to use, copy, modify, and distribute this
  13.  * software and to distribute modified versions of this software for any
  14.  * purpose, provided that the above copyright notice and the following
  15.  * two paragraphs appear in all copies of this software.
  16.  *
  17.  * In no event shall Paul Falstad or the Zsh Development Group be liable
  18.  * to any party for direct, indirect, special, incidental, or consequential
  19.  * damages arising out of the use of this software and its documentation,
  20.  * even if Paul Falstad and the Zsh Development Group have been advised of
  21.  * the possibility of such damage.
  22.  *
  23.  * Paul Falstad and the Zsh Development Group specifically disclaim any
  24.  * warranties, including, but not limited to, the implied warranties of
  25.  * merchantability and fitness for a particular purpose.  The software
  26.  * provided hereunder is on an "as is" basis, and Paul Falstad and the
  27.  * Zsh Development Group have no obligation to provide maintenance,
  28.  * support, updates, enhancements, or modifications.
  29.  *
  30.  */
  31.  
  32. #define ZLE
  33. #include "zsh.h"
  34.  
  35. /**/
  36. void
  37. forwardword(void)
  38. {
  39.     if (zmult < 0) {
  40.     zmult = -zmult;
  41.     backwardword();
  42.     return;
  43.     }
  44.     while (zmult--) {
  45.     while (cs != ll && iword(line[cs]))
  46.         cs++;
  47.     if (wordflag && !zmult)
  48.         return;
  49.     while (cs != ll && !iword(line[cs]))
  50.         cs++;
  51.     }
  52. }
  53.  
  54. /**/
  55. void
  56. viforwardword(void)
  57. {
  58.     if (zmult < 0) {
  59.     zmult = -zmult;
  60.     backwardword();
  61.     return;
  62.     }
  63.     while (zmult--) {
  64.     if (iident(line[cs]))
  65.         while (cs != ll && iident(line[cs]))
  66.         cs++;
  67.     else
  68.         while (cs != ll && !iident(line[cs]) && !iblank(line[cs]))
  69.         cs++;
  70.     if (wordflag && !zmult)
  71.         return;
  72.     while (cs != ll && iblank(line[cs]))
  73.         cs++;
  74.     }
  75. }
  76.  
  77. /**/
  78. void
  79. viforwardblankword(void)
  80. {
  81.     if (zmult < 0) {
  82.     zmult = -zmult;
  83.     vibackwardblankword();
  84.     return;
  85.     }
  86.     while (zmult--) {
  87.     while (cs != ll && !iblank(line[cs]))
  88.         cs++;
  89.     if (wordflag && !zmult)
  90.         return;
  91.     while (cs != ll && iblank(line[cs]))
  92.         cs++;
  93.     }
  94. }
  95.  
  96. /**/
  97. void
  98. emacsforwardword(void)
  99. {
  100.     if (zmult < 0) {
  101.     zmult = -zmult;
  102.     emacsbackwardword();
  103.     return;
  104.     }
  105.     while (zmult--) {
  106.     while (cs != ll && !iword(line[cs]))
  107.         cs++;
  108.     if (wordflag && !zmult)
  109.         return;
  110.     while (cs != ll && iword(line[cs]))
  111.         cs++;
  112.     }
  113. }
  114.  
  115. /**/
  116. void
  117. viforwardblankwordend(void)
  118. {
  119.     if (zmult < 0)
  120.     return;
  121.     while (zmult--) {
  122.     while (cs != ll && iblank(line[cs + 1]))
  123.         cs++;
  124.     while (cs != ll && !iblank(line[cs + 1]))
  125.         cs++;
  126.     }
  127.     if (cs != ll && virangeflag)
  128.     cs++;
  129. }
  130.  
  131. /**/
  132. void
  133. viforwardwordend(void)
  134. {
  135.     if (zmult < 0) {
  136.     zmult = -zmult;
  137.     backwardword();
  138.     return;
  139.     }
  140.     while (zmult--) {
  141.     if (iblank(line[cs + 1]))
  142.         while (cs != ll && iblank(line[cs + 1]))
  143.         cs++;
  144.     if (iident(line[cs + 1]))
  145.         while (cs != ll && iident(line[cs + 1]))
  146.         cs++;
  147.     else
  148.         while (cs != ll && !iident(line[cs + 1]) && !iblank(line[cs + 1]))
  149.         cs++;
  150.     }
  151.     if (cs != ll && virangeflag)
  152.     cs++;
  153. }
  154.  
  155. /**/
  156. void
  157. backwardword(void)
  158. {
  159.     if (zmult < 0) {
  160.     zmult = -zmult;
  161.     forwardword();
  162.     return;
  163.     }
  164.     while (zmult--) {
  165.     while (cs && !iword(line[cs - 1]))
  166.         cs--;
  167.     while (cs && iword(line[cs - 1]))
  168.         cs--;
  169.     }
  170. }
  171.  
  172. /**/
  173. void
  174. vibackwardword(void)
  175. {
  176.     if (zmult < 0) {
  177.     zmult = -zmult;
  178.     backwardword();
  179.     return;
  180.     }
  181.     while (zmult--) {
  182.     while (cs && iblank(line[cs - 1]))
  183.         cs--;
  184.     if (iident(line[cs - 1]))
  185.         while (cs && iident(line[cs - 1]))
  186.         cs--;
  187.     else
  188.         while (cs && !iident(line[cs - 1]) && !iblank(line[cs - 1]))
  189.         cs--;
  190.     }
  191. }
  192.  
  193. /**/
  194. void
  195. vibackwardblankword(void)
  196. {
  197.     if (zmult < 0) {
  198.     zmult = -zmult;
  199.     viforwardblankword();
  200.     return;
  201.     }
  202.     while (zmult--) {
  203.     while (cs && iblank(line[cs - 1]))
  204.         cs--;
  205.     while (cs && !iblank(line[cs - 1]))
  206.         cs--;
  207.     }
  208. }
  209.  
  210. /**/
  211. void
  212. emacsbackwardword(void)
  213. {
  214.     if (zmult < 0) {
  215.     zmult = -zmult;
  216.     emacsforwardword();
  217.     return;
  218.     }
  219.     while (zmult--) {
  220.     while (cs && !iword(line[cs - 1]))
  221.         cs--;
  222.     while (cs && iword(line[cs - 1]))
  223.         cs--;
  224.     }
  225. }
  226.  
  227. /**/
  228. void
  229. backwarddeleteword(void)
  230. {
  231.     int x = cs;
  232.  
  233.     if (zmult < 0) {
  234.     zmult = -zmult;
  235.     deleteword();
  236.     return;
  237.     }
  238.     while (zmult--) {
  239.     while (x && !iword(line[x - 1]))
  240.         x--;
  241.     while (x && iword(line[x - 1]))
  242.         x--;
  243.     }
  244.     backdel(cs - x);
  245. }
  246.  
  247. /**/
  248. void
  249. vibackwardkillword(void)
  250. {
  251.     int x = cs, lim = (viinsbegin > findbol()) ? viinsbegin : findbol();
  252.  
  253.     if (zmult < 0) {
  254.     feep();
  255.     return;
  256.     }
  257. /* this taken from "vibackwardword" */
  258.     while (zmult--) {
  259.     while ((x > lim) && iblank(line[x - 1]))
  260.         x--;
  261.     if (iident(line[x - 1]))
  262.         while ((x > lim) && iident(line[x - 1]))
  263.         x--;
  264.     else
  265.         while ((x > lim) && !iident(line[x - 1]) && !iblank(line[x - 1]))
  266.         x--;
  267.     }
  268.     backkill(cs - x, 1);
  269. }
  270.  
  271. /**/
  272. void
  273. backwardkillword(void)
  274. {
  275.     int x = cs;
  276.  
  277.     if (zmult < 0) {
  278.     zmult = -zmult;
  279.     killword();
  280.     return;
  281.     }
  282.     while (zmult--) {
  283.     while (x && !iword(line[x - 1]))
  284.         x--;
  285.     while (x && iword(line[x - 1]))
  286.         x--;
  287.     }
  288.     backkill(cs - x, 1);
  289. }
  290.  
  291. /**/
  292. void
  293. upcaseword(void)
  294. {
  295.     int neg = zmult < 0, ocs = cs;
  296.  
  297.     if (neg)
  298.     zmult = -zmult;
  299.     while (zmult--) {
  300.     while (cs != ll && !iword(line[cs]))
  301.         cs++;
  302.     while (cs != ll && iword(line[cs])) {
  303.         line[cs] = tuupper(line[cs]);
  304.         cs++;
  305.     }
  306.     }
  307.     if (neg)
  308.     cs = ocs;
  309. }
  310.  
  311. /**/
  312. void
  313. downcaseword(void)
  314. {
  315.     int neg = zmult < 0, ocs = cs;
  316.  
  317.     if (neg)
  318.     zmult = -zmult;
  319.     while (zmult--) {
  320.     while (cs != ll && !iword(line[cs]))
  321.         cs++;
  322.     while (cs != ll && iword(line[cs])) {
  323.         line[cs] = tulower(line[cs]);
  324.         cs++;
  325.     }
  326.     }
  327.     if (neg)
  328.     cs = ocs;
  329. }
  330.  
  331. /**/
  332. void
  333. capitalizeword(void)
  334. {
  335.     int first;
  336.     int neg = zmult < 0, ocs = cs;
  337.  
  338.     if (neg)
  339.     zmult = -zmult;
  340.     while (zmult--) {
  341.     first = 1;
  342.     while (cs != ll && !iword(line[cs]))
  343.         cs++;
  344.     while (cs != ll && iword(line[cs]) && !isalpha(line[cs]))
  345.         cs++;
  346.     while (cs != ll && iword(line[cs])) {
  347.         line[cs] = (first) ? tuupper(line[cs]) : tulower(line[cs]);
  348.         first = 0;
  349.         cs++;
  350.     }
  351.     }
  352.     if (neg)
  353.     cs = ocs;
  354. }
  355.  
  356. /**/
  357. void
  358. deleteword(void)
  359. {
  360.     int x = cs;
  361.  
  362.     if (zmult < 0) {
  363.     zmult = -zmult;
  364.     backwarddeleteword();
  365.     return;
  366.     }
  367.     while (zmult--) {
  368.     while (x != ll && !iword(line[x]))
  369.         x++;
  370.     while (x != ll && iword(line[x]))
  371.         x++;
  372.     }
  373.     foredel(x - cs);
  374. }
  375.  
  376. /**/
  377. void
  378. killword(void)
  379. {
  380.     int x = cs;
  381.  
  382.     if (zmult < 0) {
  383.     zmult = -zmult;
  384.     backwardkillword();
  385.     return;
  386.     }
  387.     while (zmult--) {
  388.     while (x != ll && !iword(line[x]))
  389.         x++;
  390.     while (x != ll && iword(line[x]))
  391.         x++;
  392.     }
  393.     forekill(x - cs, 0);
  394. }
  395.  
  396. /**/
  397. void
  398. transposewords(void)
  399. {
  400.     int p1, p2, p3, p4, x = cs;
  401.     char *temp, *pp;
  402.     int neg = zmult < 0, ocs = cs;
  403.  
  404.     if (neg)
  405.     zmult = -zmult;
  406.     while (zmult--) {
  407.     while (x != ll && line[x] != '\n' && !iword(line[x]))
  408.         x++;
  409.     if (x == ll || line[x] == '\n') {
  410.         x = cs;
  411.         while (x && line[x - 1] != '\n' && !iword(line[x]))
  412.         x--;
  413.         if (!x || line[x - 1] == '\n') {
  414.         feep();
  415.         return;
  416.         }
  417.     }
  418.     for (p4 = x; p4 != ll && iword(line[p4]); p4++);
  419.     for (p3 = p4; p3 && iword(line[p3 - 1]); p3--);
  420.     if (!p3) {
  421.         feep();
  422.         return;
  423.     }
  424.     for (p2 = p3; p2 && !iword(line[p2 - 1]); p2--);
  425.     if (!p2) {
  426.         feep();
  427.         return;
  428.     }
  429.     for (p1 = p2; p1 && iword(line[p1 - 1]); p1--);
  430.     pp = temp = (char *)halloc(p4 - p1 + 1);
  431.     struncpy(&pp, (char *) line + p3, p4 - p3);
  432.     struncpy(&pp, (char *) line + p2, p3 - p2);
  433.     struncpy(&pp, (char *) line + p1, p2 - p1);
  434.     strncpy((char *)line + p1, temp, p4 - p1);
  435.     cs = p4;
  436.     }
  437.     if (neg)
  438.     cs = ocs;
  439. }
  440.