xref: /freebsd/contrib/nvi/ex/ex_delete.c (revision 110d525ec6188f3c9dc4f54c4bc1cced2f7184cd)
1b8ba871bSPeter Wemm /*-
2b8ba871bSPeter Wemm  * Copyright (c) 1992, 1993, 1994
3b8ba871bSPeter Wemm  *	The Regents of the University of California.  All rights reserved.
4b8ba871bSPeter Wemm  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5b8ba871bSPeter Wemm  *	Keith Bostic.  All rights reserved.
6b8ba871bSPeter Wemm  *
7b8ba871bSPeter Wemm  * See the LICENSE file for redistribution information.
8b8ba871bSPeter Wemm  */
9b8ba871bSPeter Wemm 
10b8ba871bSPeter Wemm #include "config.h"
11b8ba871bSPeter Wemm 
12b8ba871bSPeter Wemm #include <sys/types.h>
13b8ba871bSPeter Wemm #include <sys/queue.h>
14f0957ccaSPeter Wemm #include <sys/time.h>
15b8ba871bSPeter Wemm 
16b8ba871bSPeter Wemm #include <bitstring.h>
17b8ba871bSPeter Wemm #include <limits.h>
18b8ba871bSPeter Wemm #include <stdio.h>
19b8ba871bSPeter Wemm 
20b8ba871bSPeter Wemm #include "../common/common.h"
21b8ba871bSPeter Wemm 
22b8ba871bSPeter Wemm /*
23b8ba871bSPeter Wemm  * ex_delete: [line [,line]] d[elete] [buffer] [count] [flags]
24b8ba871bSPeter Wemm  *
25b8ba871bSPeter Wemm  *	Delete lines from the file.
26b8ba871bSPeter Wemm  *
27*c271fa92SBaptiste Daroussin  * PUBLIC: int ex_delete(SCR *, EXCMD *);
28b8ba871bSPeter Wemm  */
29b8ba871bSPeter Wemm int
ex_delete(SCR * sp,EXCMD * cmdp)30f0957ccaSPeter Wemm ex_delete(SCR *sp, EXCMD *cmdp)
31b8ba871bSPeter Wemm {
32b8ba871bSPeter Wemm 	recno_t lno;
33b8ba871bSPeter Wemm 
34b8ba871bSPeter Wemm 	NEEDFILE(sp, cmdp);
35b8ba871bSPeter Wemm 
36b8ba871bSPeter Wemm 	/*
37b8ba871bSPeter Wemm 	 * !!!
38b8ba871bSPeter Wemm 	 * Historically, lines deleted in ex were not placed in the numeric
39b8ba871bSPeter Wemm 	 * buffers.  We follow historic practice so that we don't overwrite
40b8ba871bSPeter Wemm 	 * vi buffers accidentally.
41b8ba871bSPeter Wemm 	 */
42b8ba871bSPeter Wemm 	if (cut(sp,
43b8ba871bSPeter Wemm 	    FL_ISSET(cmdp->iflags, E_C_BUFFER) ? &cmdp->buffer : NULL,
44b8ba871bSPeter Wemm 	    &cmdp->addr1, &cmdp->addr2, CUT_LINEMODE))
45b8ba871bSPeter Wemm 		return (1);
46b8ba871bSPeter Wemm 
47b8ba871bSPeter Wemm 	/* Delete the lines. */
48b8ba871bSPeter Wemm 	if (del(sp, &cmdp->addr1, &cmdp->addr2, 1))
49b8ba871bSPeter Wemm 		return (1);
50b8ba871bSPeter Wemm 
51b8ba871bSPeter Wemm 	/* Set the cursor to the line after the last line deleted. */
52b8ba871bSPeter Wemm 	sp->lno = cmdp->addr1.lno;
53b8ba871bSPeter Wemm 
54b8ba871bSPeter Wemm 	/* Or the last line in the file if deleted to the end of the file. */
55b8ba871bSPeter Wemm 	if (db_last(sp, &lno))
56b8ba871bSPeter Wemm 		return (1);
57b8ba871bSPeter Wemm 	if (sp->lno > lno)
58b8ba871bSPeter Wemm 		sp->lno = lno;
59b8ba871bSPeter Wemm 	return (0);
60b8ba871bSPeter Wemm }
61