Home | History | Annotate | Line # | Download | only in vi
v_ch.c revision 1.2
      1  1.2  christos /*	$NetBSD: v_ch.c,v 1.2 2013/11/22 15:52:06 christos Exp $ */
      2  1.1  christos /*-
      3  1.1  christos  * Copyright (c) 1992, 1993, 1994
      4  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      5  1.1  christos  * Copyright (c) 1992, 1993, 1994, 1995, 1996
      6  1.1  christos  *	Keith Bostic.  All rights reserved.
      7  1.1  christos  *
      8  1.1  christos  * See the LICENSE file for redistribution information.
      9  1.1  christos  */
     10  1.1  christos 
     11  1.1  christos #include "config.h"
     12  1.1  christos 
     13  1.1  christos #ifndef lint
     14  1.1  christos static const char sccsid[] = "Id: v_ch.c,v 10.10 2001/06/25 15:19:30 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:30 ";
     15  1.1  christos #endif /* not lint */
     16  1.1  christos 
     17  1.1  christos #include <sys/types.h>
     18  1.1  christos #include <sys/queue.h>
     19  1.1  christos #include <sys/time.h>
     20  1.1  christos 
     21  1.1  christos #include <bitstring.h>
     22  1.1  christos #include <limits.h>
     23  1.1  christos #include <stdio.h>
     24  1.1  christos #include <stdlib.h>
     25  1.1  christos 
     26  1.1  christos #include "../common/common.h"
     27  1.1  christos #include "vi.h"
     28  1.1  christos 
     29  1.1  christos static void notfound __P((SCR *, ARG_CHAR_T));
     30  1.1  christos static void noprev __P((SCR *));
     31  1.1  christos 
     32  1.1  christos /*
     33  1.1  christos  * v_chrepeat -- [count];
     34  1.1  christos  *	Repeat the last F, f, T or t search.
     35  1.1  christos  *
     36  1.1  christos  * PUBLIC: int v_chrepeat __P((SCR *, VICMD *));
     37  1.1  christos  */
     38  1.1  christos int
     39  1.1  christos v_chrepeat(SCR *sp, VICMD *vp)
     40  1.1  christos {
     41  1.1  christos 	vp->character = VIP(sp)->lastckey;
     42  1.1  christos 
     43  1.1  christos 	switch (VIP(sp)->csearchdir) {
     44  1.1  christos 	case CNOTSET:
     45  1.1  christos 		noprev(sp);
     46  1.1  christos 		return (1);
     47  1.1  christos 	case FSEARCH:
     48  1.1  christos 		return (v_chF(sp, vp));
     49  1.1  christos 	case fSEARCH:
     50  1.1  christos 		return (v_chf(sp, vp));
     51  1.1  christos 	case TSEARCH:
     52  1.1  christos 		return (v_chT(sp, vp));
     53  1.1  christos 	case tSEARCH:
     54  1.1  christos 		return (v_cht(sp, vp));
     55  1.1  christos 	default:
     56  1.1  christos 		abort();
     57  1.1  christos 	}
     58  1.1  christos 	/* NOTREACHED */
     59  1.1  christos }
     60  1.1  christos 
     61  1.1  christos /*
     62  1.1  christos  * v_chrrepeat -- [count],
     63  1.1  christos  *	Repeat the last F, f, T or t search in the reverse direction.
     64  1.1  christos  *
     65  1.1  christos  * PUBLIC: int v_chrrepeat __P((SCR *, VICMD *));
     66  1.1  christos  */
     67  1.1  christos int
     68  1.1  christos v_chrrepeat(SCR *sp, VICMD *vp)
     69  1.1  christos {
     70  1.1  christos 	cdir_t savedir;
     71  1.1  christos 	int rval;
     72  1.1  christos 
     73  1.1  christos 	vp->character = VIP(sp)->lastckey;
     74  1.1  christos 	savedir = VIP(sp)->csearchdir;
     75  1.1  christos 
     76  1.1  christos 	switch (VIP(sp)->csearchdir) {
     77  1.1  christos 	case CNOTSET:
     78  1.1  christos 		noprev(sp);
     79  1.1  christos 		return (1);
     80  1.1  christos 	case FSEARCH:
     81  1.1  christos 		rval = v_chf(sp, vp);
     82  1.1  christos 		break;
     83  1.1  christos 	case fSEARCH:
     84  1.1  christos 		rval = v_chF(sp, vp);
     85  1.1  christos 		break;
     86  1.1  christos 	case TSEARCH:
     87  1.1  christos 		rval = v_cht(sp, vp);
     88  1.1  christos 		break;
     89  1.1  christos 	case tSEARCH:
     90  1.1  christos 		rval = v_chT(sp, vp);
     91  1.1  christos 		break;
     92  1.1  christos 	default:
     93  1.1  christos 		abort();
     94  1.1  christos 	}
     95  1.1  christos 	VIP(sp)->csearchdir = savedir;
     96  1.1  christos 	return (rval);
     97  1.1  christos }
     98  1.1  christos 
     99  1.1  christos /*
    100  1.1  christos  * v_cht -- [count]tc
    101  1.1  christos  *	Search forward in the line for the character before the next
    102  1.1  christos  *	occurrence of the specified character.
    103  1.1  christos  *
    104  1.1  christos  * PUBLIC: int v_cht __P((SCR *, VICMD *));
    105  1.1  christos  */
    106  1.1  christos int
    107  1.1  christos v_cht(SCR *sp, VICMD *vp)
    108  1.1  christos {
    109  1.1  christos 	if (v_chf(sp, vp))
    110  1.1  christos 		return (1);
    111  1.1  christos 
    112  1.1  christos 	/*
    113  1.1  christos 	 * v_chf places the cursor on the character, where the 't'
    114  1.1  christos 	 * command wants it to its left.  We know this is safe since
    115  1.1  christos 	 * we had to move right for v_chf() to have succeeded.
    116  1.1  christos 	 */
    117  1.1  christos 	--vp->m_stop.cno;
    118  1.1  christos 
    119  1.1  christos 	/*
    120  1.1  christos 	 * Make any necessary correction to the motion decision made
    121  1.1  christos 	 * by the v_chf routine.
    122  1.1  christos 	 */
    123  1.1  christos 	if (!ISMOTION(vp))
    124  1.1  christos 		vp->m_final = vp->m_stop;
    125  1.1  christos 
    126  1.1  christos 	VIP(sp)->csearchdir = tSEARCH;
    127  1.1  christos 	return (0);
    128  1.1  christos }
    129  1.1  christos 
    130  1.1  christos /*
    131  1.1  christos  * v_chf -- [count]fc
    132  1.1  christos  *	Search forward in the line for the next occurrence of the
    133  1.1  christos  *	specified character.
    134  1.1  christos  *
    135  1.1  christos  * PUBLIC: int v_chf __P((SCR *, VICMD *));
    136  1.1  christos  */
    137  1.1  christos int
    138  1.1  christos v_chf(SCR *sp, VICMD *vp)
    139  1.1  christos {
    140  1.1  christos 	size_t len;
    141  1.1  christos 	u_long cnt;
    142  1.2  christos 	int isempty;
    143  1.2  christos 	ARG_CHAR_T key;
    144  1.1  christos 	CHAR_T *endp, *p, *startp;
    145  1.1  christos 
    146  1.1  christos 	/*
    147  1.1  christos 	 * !!!
    148  1.1  christos 	 * If it's a dot command, it doesn't reset the key for which we're
    149  1.1  christos 	 * searching, e.g. in "df1|f2|.|;", the ';' searches for a '2'.
    150  1.1  christos 	 */
    151  1.1  christos 	key = vp->character;
    152  1.1  christos 	if (!F_ISSET(vp, VC_ISDOT))
    153  1.1  christos 		VIP(sp)->lastckey = key;
    154  1.1  christos 	VIP(sp)->csearchdir = fSEARCH;
    155  1.1  christos 
    156  1.1  christos 	if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
    157  1.1  christos 		if (isempty)
    158  1.1  christos 			goto empty;
    159  1.1  christos 		return (1);
    160  1.1  christos 	}
    161  1.1  christos 
    162  1.1  christos 	if (len == 0) {
    163  1.1  christos empty:		notfound(sp, key);
    164  1.1  christos 		return (1);
    165  1.1  christos 	}
    166  1.1  christos 
    167  1.1  christos 	endp = (startp = p) + len;
    168  1.1  christos 	p += vp->m_start.cno;
    169  1.1  christos 	for (cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt--;) {
    170  1.1  christos 		while (++p < endp && *p != key);
    171  1.1  christos 		if (p == endp) {
    172  1.1  christos 			notfound(sp, key);
    173  1.1  christos 			return (1);
    174  1.1  christos 		}
    175  1.1  christos 	}
    176  1.1  christos 
    177  1.1  christos 	vp->m_stop.cno = p - startp;
    178  1.1  christos 
    179  1.1  christos 	/*
    180  1.1  christos 	 * Non-motion commands move to the end of the range.
    181  1.1  christos 	 * Delete and yank stay at the start, ignore others.
    182  1.1  christos 	 */
    183  1.1  christos 	vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
    184  1.1  christos 	return (0);
    185  1.1  christos }
    186  1.1  christos 
    187  1.1  christos /*
    188  1.1  christos  * v_chT -- [count]Tc
    189  1.1  christos  *	Search backward in the line for the character after the next
    190  1.1  christos  *	occurrence of the specified character.
    191  1.1  christos  *
    192  1.1  christos  * PUBLIC: int v_chT __P((SCR *, VICMD *));
    193  1.1  christos  */
    194  1.1  christos int
    195  1.1  christos v_chT(SCR *sp, VICMD *vp)
    196  1.1  christos {
    197  1.1  christos 	if (v_chF(sp, vp))
    198  1.1  christos 		return (1);
    199  1.1  christos 
    200  1.1  christos 	/*
    201  1.1  christos 	 * v_chF places the cursor on the character, where the 'T'
    202  1.1  christos 	 * command wants it to its right.  We know this is safe since
    203  1.1  christos 	 * we had to move left for v_chF() to have succeeded.
    204  1.1  christos 	 */
    205  1.1  christos 	++vp->m_stop.cno;
    206  1.1  christos 	vp->m_final = vp->m_stop;
    207  1.1  christos 
    208  1.1  christos 	VIP(sp)->csearchdir = TSEARCH;
    209  1.1  christos 	return (0);
    210  1.1  christos }
    211  1.1  christos 
    212  1.1  christos /*
    213  1.1  christos  * v_chF -- [count]Fc
    214  1.1  christos  *	Search backward in the line for the next occurrence of the
    215  1.1  christos  *	specified character.
    216  1.1  christos  *
    217  1.1  christos  * PUBLIC: int v_chF __P((SCR *, VICMD *));
    218  1.1  christos  */
    219  1.1  christos int
    220  1.1  christos v_chF(SCR *sp, VICMD *vp)
    221  1.1  christos {
    222  1.1  christos 	size_t len;
    223  1.1  christos 	u_long cnt;
    224  1.2  christos 	int isempty;
    225  1.2  christos 	ARG_CHAR_T key;
    226  1.1  christos 	CHAR_T *endp, *p;
    227  1.1  christos 
    228  1.1  christos 	/*
    229  1.1  christos 	 * !!!
    230  1.1  christos 	 * If it's a dot command, it doesn't reset the key for which
    231  1.1  christos 	 * we're searching, e.g. in "df1|f2|.|;", the ';' searches
    232  1.1  christos 	 * for a '2'.
    233  1.1  christos 	 */
    234  1.1  christos 	key = vp->character;
    235  1.1  christos 	if (!F_ISSET(vp, VC_ISDOT))
    236  1.1  christos 		VIP(sp)->lastckey = key;
    237  1.1  christos 	VIP(sp)->csearchdir = FSEARCH;
    238  1.1  christos 
    239  1.1  christos 	if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
    240  1.1  christos 		if (isempty)
    241  1.1  christos 			goto empty;
    242  1.1  christos 		return (1);
    243  1.1  christos 	}
    244  1.1  christos 
    245  1.1  christos 	if (len == 0) {
    246  1.1  christos empty:		notfound(sp, key);
    247  1.1  christos 		return (1);
    248  1.1  christos 	}
    249  1.1  christos 
    250  1.1  christos 	endp = p - 1;
    251  1.1  christos 	p += vp->m_start.cno;
    252  1.1  christos 	for (cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt--;) {
    253  1.1  christos 		while (--p > endp && *p != key);
    254  1.1  christos 		if (p == endp) {
    255  1.1  christos 			notfound(sp, key);
    256  1.1  christos 			return (1);
    257  1.1  christos 		}
    258  1.1  christos 	}
    259  1.1  christos 
    260  1.1  christos 	vp->m_stop.cno = (p - endp) - 1;
    261  1.1  christos 
    262  1.1  christos 	/*
    263  1.1  christos 	 * All commands move to the end of the range.  Motion commands
    264  1.1  christos 	 * adjust the starting point to the character before the current
    265  1.1  christos 	 * one.
    266  1.1  christos 	 */
    267  1.1  christos 	vp->m_final = vp->m_stop;
    268  1.1  christos 	if (ISMOTION(vp))
    269  1.1  christos 		--vp->m_start.cno;
    270  1.1  christos 	return (0);
    271  1.1  christos }
    272  1.1  christos 
    273  1.1  christos static void
    274  1.1  christos noprev(SCR *sp)
    275  1.1  christos {
    276  1.1  christos 	msgq(sp, M_BERR, "178|No previous F, f, T or t search");
    277  1.1  christos }
    278  1.1  christos 
    279  1.1  christos static void
    280  1.1  christos notfound(SCR *sp, ARG_CHAR_T ch)
    281  1.1  christos {
    282  1.1  christos 	msgq(sp, M_BERR, "179|%s not found", KEY_NAME(sp, ch));
    283  1.1  christos }
    284