Home | History | Annotate | Line # | Download | only in vi
v_match.c revision 1.1.1.1
      1 /*-
      2  * Copyright (c) 1992, 1993, 1994
      3  *	The Regents of the University of California.  All rights reserved.
      4  * Copyright (c) 1992, 1993, 1994, 1995, 1996
      5  *	Keith Bostic.  All rights reserved.
      6  *
      7  * See the LICENSE file for redistribution information.
      8  */
      9 
     10 #include "config.h"
     11 
     12 #ifndef lint
     13 static const char sccsid[] = "Id: v_match.c,v 10.10 2001/06/25 15:19:32 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:32 ";
     14 #endif /* not lint */
     15 
     16 #include <sys/types.h>
     17 #include <sys/queue.h>
     18 #include <sys/time.h>
     19 
     20 #include <bitstring.h>
     21 #include <limits.h>
     22 #include <stdio.h>
     23 #include <string.h>
     24 
     25 #include "../common/common.h"
     26 #include "vi.h"
     27 
     28 /*
     29  * v_match -- %
     30  *	Search to matching character.
     31  *
     32  * PUBLIC: int v_match __P((SCR *, VICMD *));
     33  */
     34 int
     35 v_match(SCR *sp, VICMD *vp)
     36 {
     37 	VCS cs;
     38 	MARK *mp;
     39 	size_t cno, len, off;
     40 	int cnt, isempty, matchc, startc, (*gc)__P((SCR *, VCS *));
     41 	CHAR_T *p;
     42 
     43 	/*
     44 	 * !!!
     45 	 * Historic practice; ignore the count.
     46 	 *
     47 	 * !!!
     48 	 * Historical practice was to search for the initial character in the
     49 	 * forward direction only.
     50 	 */
     51 	if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
     52 		if (isempty)
     53 			goto nomatch;
     54 		return (1);
     55 	}
     56 	for (off = vp->m_start.cno;; ++off) {
     57 		if (off >= len) {
     58 nomatch:		msgq(sp, M_BERR, "184|No match character on this line");
     59 			return (1);
     60 		}
     61 		switch (startc = p[off]) {
     62 		case '(':
     63 			matchc = ')';
     64 			gc = cs_next;
     65 			break;
     66 		case ')':
     67 			matchc = '(';
     68 			gc = cs_prev;
     69 			break;
     70 		case '[':
     71 			matchc = ']';
     72 			gc = cs_next;
     73 			break;
     74 		case ']':
     75 			matchc = '[';
     76 			gc = cs_prev;
     77 			break;
     78 		case '{':
     79 			matchc = '}';
     80 			gc = cs_next;
     81 			break;
     82 		case '}':
     83 			matchc = '{';
     84 			gc = cs_prev;
     85 			break;
     86 		case '<':
     87 			matchc = '>';
     88 			gc = cs_next;
     89 			break;
     90 		case '>':
     91 			matchc = '<';
     92 			gc = cs_prev;
     93 			break;
     94 		default:
     95 			continue;
     96 		}
     97 		break;
     98 	}
     99 
    100 	cs.cs_lno = vp->m_start.lno;
    101 	cs.cs_cno = off;
    102 	if (cs_init(sp, &cs))
    103 		return (1);
    104 	for (cnt = 1;;) {
    105 		if (gc(sp, &cs))
    106 			return (1);
    107 		if (cs.cs_flags != 0) {
    108 			if (cs.cs_flags == CS_EOF || cs.cs_flags == CS_SOF)
    109 				break;
    110 			continue;
    111 		}
    112 		if (cs.cs_ch == startc)
    113 			++cnt;
    114 		else if (cs.cs_ch == matchc && --cnt == 0)
    115 			break;
    116 	}
    117 	if (cnt) {
    118 		msgq(sp, M_BERR, "185|Matching character not found");
    119 		return (1);
    120 	}
    121 
    122 	vp->m_stop.lno = cs.cs_lno;
    123 	vp->m_stop.cno = cs.cs_cno;
    124 
    125 	/*
    126 	 * If moving right, non-motion commands move to the end of the range.
    127 	 * Delete and yank stay at the start.
    128 	 *
    129 	 * If moving left, all commands move to the end of the range.
    130 	 *
    131 	 * !!!
    132 	 * Don't correct for leftward movement -- historic vi deleted the
    133 	 * starting cursor position when deleting to a match.
    134 	 */
    135 	if (vp->m_start.lno < vp->m_stop.lno ||
    136 	    vp->m_start.lno == vp->m_stop.lno &&
    137 	    vp->m_start.cno < vp->m_stop.cno)
    138 		vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
    139 	else
    140 		vp->m_final = vp->m_stop;
    141 
    142 	/*
    143 	 * !!!
    144 	 * If the motion is across lines, and the earliest cursor position
    145 	 * is at or before any non-blank characters in the line, i.e. the
    146 	 * movement is cutting all of the line's text, and the later cursor
    147 	 * position has nothing other than whitespace characters between it
    148 	 * and the end of its line, the buffer is in line mode.
    149 	 */
    150 	if (!ISMOTION(vp) || vp->m_start.lno == vp->m_stop.lno)
    151 		return (0);
    152 	mp = vp->m_start.lno < vp->m_stop.lno ? &vp->m_start : &vp->m_stop;
    153 	if (mp->cno != 0) {
    154 		cno = 0;
    155 		if (nonblank(sp, mp->lno, &cno))
    156 			return (1);
    157 		if (cno < mp->cno)
    158 			return (0);
    159 	}
    160 	mp = vp->m_start.lno < vp->m_stop.lno ? &vp->m_stop : &vp->m_start;
    161 	if (db_get(sp, mp->lno, DBG_FATAL, &p, &len))
    162 		return (1);
    163 	for (p += mp->cno + 1, len -= mp->cno; --len; ++p)
    164 		if (!ISBLANK(*p))
    165 			return (0);
    166 	F_SET(vp, VM_LMODE);
    167 	return (0);
    168 }
    169