Home | History | Annotate | Line # | Download | only in vi
v_paragraph.c revision 1.2
      1  1.2  christos /*	$NetBSD: v_paragraph.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_paragraph.c,v 10.10 2001/06/25 15:19:32 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:32 ";
     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 <errno.h>
     23  1.1  christos #include <limits.h>
     24  1.1  christos #include <stdio.h>
     25  1.1  christos #include <stdlib.h>
     26  1.1  christos #include <string.h>
     27  1.1  christos 
     28  1.1  christos #include "../common/common.h"
     29  1.1  christos #include "vi.h"
     30  1.1  christos 
     31  1.1  christos #define	INTEXT_CHECK {							\
     32  1.1  christos 	if (len == 0 || v_isempty(p, len)) {				\
     33  1.1  christos 		if (!--cnt)						\
     34  1.1  christos 			goto found;					\
     35  1.1  christos 		pstate = P_INBLANK;					\
     36  1.1  christos 	}								\
     37  1.1  christos 	/*								\
     38  1.1  christos 	 * !!!								\
     39  1.1  christos 	 * Historic documentation (USD:15-11, 4.2) said that formfeed	\
     40  1.1  christos 	 * characters (^L) in the first column delimited paragraphs.	\
     41  1.1  christos 	 * The historic vi code mentions formfeed characters, but never	\
     42  1.1  christos 	 * implements them.  It seems reasonable, do it.		\
     43  1.1  christos 	 */								\
     44  1.1  christos 	if (p[0] == '\014') {						\
     45  1.1  christos 		if (!--cnt)						\
     46  1.1  christos 			goto found;					\
     47  1.1  christos 		continue;						\
     48  1.1  christos 	}								\
     49  1.1  christos 	if (p[0] != '.' || len < 2)					\
     50  1.1  christos 		continue;						\
     51  1.1  christos 	for (lp = VIP(sp)->ps; *lp != '\0'; lp += 2)			\
     52  1.1  christos 		if (lp[0] == p[1] &&					\
     53  1.2  christos 		    ((lp[1] == ' ' && len == 2) || lp[1] == p[2]) &&	\
     54  1.1  christos 		    !--cnt)						\
     55  1.1  christos 			goto found;					\
     56  1.1  christos }
     57  1.1  christos 
     58  1.1  christos /*
     59  1.1  christos  * v_paragraphf -- [count]}
     60  1.1  christos  *	Move forward count paragraphs.
     61  1.1  christos  *
     62  1.1  christos  * Paragraphs are empty lines after text, formfeed characters, or values
     63  1.1  christos  * from the paragraph or section options.
     64  1.1  christos  *
     65  1.1  christos  * PUBLIC: int v_paragraphf __P((SCR *, VICMD *));
     66  1.1  christos  */
     67  1.1  christos int
     68  1.1  christos v_paragraphf(SCR *sp, VICMD *vp)
     69  1.1  christos {
     70  1.1  christos 	enum { P_INTEXT, P_INBLANK } pstate;
     71  1.1  christos 	size_t lastlen, len;
     72  1.1  christos 	db_recno_t cnt, lastlno, lno;
     73  1.1  christos 	int isempty;
     74  1.1  christos 	CHAR_T *p;
     75  1.1  christos 	char *lp;
     76  1.1  christos 
     77  1.1  christos 	/*
     78  1.1  christos 	 * !!!
     79  1.1  christos 	 * If the starting cursor position is at or before any non-blank
     80  1.1  christos 	 * characters in the line, i.e. the movement is cutting all of the
     81  1.1  christos 	 * line's text, the buffer is in line mode.  It's a lot easier to
     82  1.1  christos 	 * check here, because we know that the end is going to be the start
     83  1.1  christos 	 * or end of a line.
     84  1.1  christos 	 *
     85  1.1  christos 	 * This was historical practice in vi, with a single exception.  If
     86  1.1  christos 	 * the paragraph movement was from the start of the last line to EOF,
     87  1.1  christos 	 * then all the characters were deleted from the last line, but the
     88  1.1  christos 	 * line itself remained.  If somebody complains, don't pause, don't
     89  1.1  christos 	 * hesitate, just hit them.
     90  1.1  christos 	 */
     91  1.2  christos 	if (ISMOTION(vp)) {
     92  1.1  christos 		if (vp->m_start.cno == 0)
     93  1.1  christos 			F_SET(vp, VM_LMODE);
     94  1.1  christos 		else {
     95  1.1  christos 			vp->m_stop = vp->m_start;
     96  1.1  christos 			vp->m_stop.cno = 0;
     97  1.1  christos 			if (nonblank(sp, vp->m_stop.lno, &vp->m_stop.cno))
     98  1.1  christos 				return (1);
     99  1.1  christos 			if (vp->m_start.cno <= vp->m_stop.cno)
    100  1.1  christos 				F_SET(vp, VM_LMODE);
    101  1.1  christos 		}
    102  1.2  christos 	}
    103  1.1  christos 
    104  1.1  christos 	/* Figure out what state we're currently in. */
    105  1.1  christos 	lno = vp->m_start.lno;
    106  1.1  christos 	if (db_get(sp, lno, 0, &p, &len))
    107  1.1  christos 		goto eof;
    108  1.1  christos 
    109  1.1  christos 	/*
    110  1.1  christos 	 * If we start in text, we want to switch states
    111  1.1  christos 	 * (2 * N - 1) times, in non-text, (2 * N) times.
    112  1.1  christos 	 */
    113  1.1  christos 	cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
    114  1.1  christos 	cnt *= 2;
    115  1.1  christos 	if (len == 0 || v_isempty(p, len))
    116  1.1  christos 		pstate = P_INBLANK;
    117  1.1  christos 	else {
    118  1.1  christos 		--cnt;
    119  1.1  christos 		pstate = P_INTEXT;
    120  1.1  christos 	}
    121  1.1  christos 
    122  1.1  christos 	for (;;) {
    123  1.1  christos 		lastlno = lno;
    124  1.1  christos 		lastlen = len;
    125  1.1  christos 		if (db_get(sp, ++lno, 0, &p, &len))
    126  1.1  christos 			goto eof;
    127  1.1  christos 		switch (pstate) {
    128  1.1  christos 		case P_INTEXT:
    129  1.1  christos 			INTEXT_CHECK;
    130  1.1  christos 			break;
    131  1.1  christos 		case P_INBLANK:
    132  1.1  christos 			if (len == 0 || v_isempty(p, len))
    133  1.1  christos 				break;
    134  1.1  christos 			if (--cnt) {
    135  1.1  christos 				pstate = P_INTEXT;
    136  1.1  christos 				break;
    137  1.1  christos 			}
    138  1.1  christos 			/*
    139  1.1  christos 			 * !!!
    140  1.1  christos 			 * Non-motion commands move to the end of the range,
    141  1.1  christos 			 * delete and yank stay at the start.  Ignore others.
    142  1.1  christos 			 * Adjust the end of the range for motion commands;
    143  1.1  christos 			 * historically, a motion component was to the end of
    144  1.1  christos 			 * the previous line, whereas the movement command was
    145  1.1  christos 			 * to the start of the new "paragraph".
    146  1.1  christos 			 */
    147  1.1  christos found:			if (ISMOTION(vp)) {
    148  1.1  christos 				vp->m_stop.lno = lastlno;
    149  1.1  christos 				vp->m_stop.cno = lastlen ? lastlen - 1 : 0;
    150  1.1  christos 				vp->m_final = vp->m_start;
    151  1.1  christos 			} else {
    152  1.1  christos 				vp->m_stop.lno = lno;
    153  1.1  christos 				vp->m_stop.cno = 0;
    154  1.1  christos 				vp->m_final = vp->m_stop;
    155  1.1  christos 			}
    156  1.1  christos 			return (0);
    157  1.1  christos 		default:
    158  1.1  christos 			abort();
    159  1.1  christos 		}
    160  1.1  christos 	}
    161  1.1  christos 
    162  1.1  christos 	/*
    163  1.1  christos 	 * !!!
    164  1.1  christos 	 * Adjust end of the range for motion commands; EOF is a movement
    165  1.1  christos 	 * sink.  The } command historically moved to the end of the last
    166  1.1  christos 	 * line, not the beginning, from any position before the end of the
    167  1.1  christos 	 * last line.  It also historically worked on empty files, so we
    168  1.1  christos 	 * have to make it okay.
    169  1.1  christos 	 */
    170  1.1  christos eof:	if (vp->m_start.lno == lno || vp->m_start.lno == lno - 1) {
    171  1.1  christos 		if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
    172  1.1  christos 			if (!isempty)
    173  1.1  christos 				return (1);
    174  1.1  christos 			vp->m_start.cno = 0;
    175  1.1  christos 			return (0);
    176  1.1  christos 		}
    177  1.1  christos 		if (vp->m_start.cno == (len ? len - 1 : 0)) {
    178  1.1  christos 			v_eof(sp, NULL);
    179  1.1  christos 			return (1);
    180  1.1  christos 		}
    181  1.1  christos 	}
    182  1.1  christos 	/*
    183  1.1  christos 	 * !!!
    184  1.1  christos 	 * Non-motion commands move to the end of the range, delete
    185  1.1  christos 	 * and yank stay at the start.  Ignore others.
    186  1.1  christos 	 *
    187  1.1  christos 	 * If deleting the line (which happens if deleting to EOF), then
    188  1.1  christos 	 * cursor movement is to the first nonblank.
    189  1.1  christos 	 */
    190  1.1  christos 	if (ISMOTION(vp) && ISCMD(vp->rkp, 'd')) {
    191  1.1  christos 		F_CLR(vp, VM_RCM_MASK);
    192  1.1  christos 		F_SET(vp, VM_RCM_SETFNB);
    193  1.1  christos 	}
    194  1.1  christos 	vp->m_stop.lno = lno - 1;
    195  1.1  christos 	vp->m_stop.cno = len ? len - 1 : 0;
    196  1.1  christos 	vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
    197  1.1  christos 	return (0);
    198  1.1  christos }
    199  1.1  christos 
    200  1.1  christos /*
    201  1.1  christos  * v_paragraphb -- [count]{
    202  1.1  christos  *	Move backward count paragraphs.
    203  1.1  christos  *
    204  1.1  christos  * PUBLIC: int v_paragraphb __P((SCR *, VICMD *));
    205  1.1  christos  */
    206  1.1  christos int
    207  1.1  christos v_paragraphb(SCR *sp, VICMD *vp)
    208  1.1  christos {
    209  1.1  christos 	enum { P_INTEXT, P_INBLANK } pstate;
    210  1.1  christos 	size_t len;
    211  1.1  christos 	db_recno_t cnt, lno;
    212  1.1  christos 	CHAR_T *p;
    213  1.1  christos 	char *lp;
    214  1.1  christos 
    215  1.1  christos 	/*
    216  1.1  christos 	 * !!!
    217  1.1  christos 	 * Check for SOF.  The historic vi didn't complain if users hit SOF
    218  1.1  christos 	 * repeatedly, unless it was part of a motion command.  There is no
    219  1.1  christos 	 * question but that Emerson's editor of choice was vi.
    220  1.1  christos 	 *
    221  1.1  christos 	 * The { command historically moved to the beginning of the first
    222  1.1  christos 	 * line if invoked on the first line.
    223  1.1  christos 	 *
    224  1.1  christos 	 * !!!
    225  1.1  christos 	 * If the starting cursor position is in the first column (backward
    226  1.1  christos 	 * paragraph movements did NOT historically pay attention to non-blank
    227  1.1  christos 	 * characters) i.e. the movement is cutting the entire line, the buffer
    228  1.1  christos 	 * is in line mode.  Cuts from the beginning of the line also did not
    229  1.1  christos 	 * cut the current line, but started at the previous EOL.
    230  1.1  christos 	 *
    231  1.1  christos 	 * Correct for a left motion component while we're thinking about it.
    232  1.1  christos 	 */
    233  1.1  christos 	lno = vp->m_start.lno;
    234  1.1  christos 
    235  1.2  christos 	if (ISMOTION(vp)) {
    236  1.1  christos 		if (vp->m_start.cno == 0) {
    237  1.1  christos 			if (vp->m_start.lno == 1) {
    238  1.1  christos 				v_sof(sp, &vp->m_start);
    239  1.1  christos 				return (1);
    240  1.1  christos 			} else
    241  1.1  christos 				--vp->m_start.lno;
    242  1.1  christos 			F_SET(vp, VM_LMODE);
    243  1.1  christos 		} else
    244  1.1  christos 			--vp->m_start.cno;
    245  1.2  christos 	}
    246  1.1  christos 
    247  1.1  christos 	if (vp->m_start.lno <= 1)
    248  1.1  christos 		goto sof;
    249  1.1  christos 
    250  1.1  christos 	/* Figure out what state we're currently in. */
    251  1.1  christos 	if (db_get(sp, lno, 0, &p, &len))
    252  1.1  christos 		goto sof;
    253  1.1  christos 
    254  1.1  christos 	/*
    255  1.1  christos 	 * If we start in text, we want to switch states
    256  1.1  christos 	 * (2 * N - 1) times, in non-text, (2 * N) times.
    257  1.1  christos 	 */
    258  1.1  christos 	cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
    259  1.1  christos 	cnt *= 2;
    260  1.1  christos 	if (len == 0 || v_isempty(p, len))
    261  1.1  christos 		pstate = P_INBLANK;
    262  1.1  christos 	else {
    263  1.1  christos 		--cnt;
    264  1.1  christos 		pstate = P_INTEXT;
    265  1.1  christos 
    266  1.1  christos 		/*
    267  1.1  christos 		 * !!!
    268  1.1  christos 		 * If the starting cursor is past the first column,
    269  1.1  christos 		 * the current line is checked for a paragraph.
    270  1.1  christos 		 */
    271  1.1  christos 		if (vp->m_start.cno > 0)
    272  1.1  christos 			++lno;
    273  1.1  christos 	}
    274  1.1  christos 
    275  1.1  christos 	for (;;) {
    276  1.1  christos 		if (db_get(sp, --lno, 0, &p, &len))
    277  1.1  christos 			goto sof;
    278  1.1  christos 		switch (pstate) {
    279  1.1  christos 		case P_INTEXT:
    280  1.1  christos 			INTEXT_CHECK;
    281  1.1  christos 			break;
    282  1.1  christos 		case P_INBLANK:
    283  1.1  christos 			if (len != 0 && !v_isempty(p, len)) {
    284  1.1  christos 				if (!--cnt)
    285  1.1  christos 					goto found;
    286  1.1  christos 				pstate = P_INTEXT;
    287  1.1  christos 			}
    288  1.1  christos 			break;
    289  1.1  christos 		default:
    290  1.1  christos 			abort();
    291  1.1  christos 		}
    292  1.1  christos 	}
    293  1.1  christos 
    294  1.1  christos 	/* SOF is a movement sink. */
    295  1.1  christos sof:	lno = 1;
    296  1.1  christos 
    297  1.1  christos found:	vp->m_stop.lno = lno;
    298  1.1  christos 	vp->m_stop.cno = 0;
    299  1.1  christos 
    300  1.1  christos 	/*
    301  1.1  christos 	 * All commands move to the end of the range.  (We already
    302  1.1  christos 	 * adjusted the start of the range for motion commands).
    303  1.1  christos 	 */
    304  1.1  christos 	vp->m_final = vp->m_stop;
    305  1.1  christos 	return (0);
    306  1.1  christos }
    307  1.1  christos 
    308  1.1  christos /*
    309  1.1  christos  * v_buildps --
    310  1.1  christos  *	Build the paragraph command search pattern.
    311  1.1  christos  *
    312  1.2  christos  * PUBLIC: int v_buildps __P((SCR *, const char *, const char *));
    313  1.1  christos  */
    314  1.1  christos int
    315  1.2  christos v_buildps(SCR *sp, const char *p_p, const char *s_p)
    316  1.1  christos {
    317  1.1  christos 	VI_PRIVATE *vip;
    318  1.1  christos 	size_t p_len, s_len;
    319  1.1  christos 	char *p;
    320  1.1  christos 
    321  1.1  christos 	/*
    322  1.1  christos 	 * The vi paragraph command searches for either a paragraph or
    323  1.1  christos 	 * section option macro.
    324  1.1  christos 	 */
    325  1.1  christos 	p_len = p_p == NULL ? 0 : strlen(p_p);
    326  1.1  christos 	s_len = s_p == NULL ? 0 : strlen(s_p);
    327  1.1  christos 
    328  1.1  christos 	if (p_len == 0 && s_len == 0)
    329  1.1  christos 		return (0);
    330  1.1  christos 
    331  1.1  christos 	MALLOC_RET(sp, p, char *, p_len + s_len + 1);
    332  1.1  christos 
    333  1.1  christos 	vip = VIP(sp);
    334  1.1  christos 	if (vip->ps != NULL)
    335  1.1  christos 		free(vip->ps);
    336  1.1  christos 
    337  1.1  christos 	if (p_p != NULL)
    338  1.1  christos 		memmove(p, p_p, p_len + 1);
    339  1.1  christos 	if (s_p != NULL)
    340  1.1  christos 		memmove(p + p_len, s_p, s_len + 1);
    341  1.1  christos 	vip->ps = p;
    342  1.1  christos 	return (0);
    343  1.1  christos }
    344