xref: /freebsd/contrib/nvi/ex/ex_delete.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
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[] = "@(#)ex_delete.c	10.9 (Berkeley) 10/23/96";
14 #endif /* not lint */
15 
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 
19 #include <bitstring.h>
20 #include <limits.h>
21 #include <stdio.h>
22 
23 #include "../common/common.h"
24 
25 /*
26  * ex_delete: [line [,line]] d[elete] [buffer] [count] [flags]
27  *
28  *	Delete lines from the file.
29  *
30  * PUBLIC: int ex_delete __P((SCR *, EXCMD *));
31  */
32 int
33 ex_delete(sp, cmdp)
34 	SCR *sp;
35 	EXCMD *cmdp;
36 {
37 	recno_t lno;
38 
39 	NEEDFILE(sp, cmdp);
40 
41 	/*
42 	 * !!!
43 	 * Historically, lines deleted in ex were not placed in the numeric
44 	 * buffers.  We follow historic practice so that we don't overwrite
45 	 * vi buffers accidentally.
46 	 */
47 	if (cut(sp,
48 	    FL_ISSET(cmdp->iflags, E_C_BUFFER) ? &cmdp->buffer : NULL,
49 	    &cmdp->addr1, &cmdp->addr2, CUT_LINEMODE))
50 		return (1);
51 
52 	/* Delete the lines. */
53 	if (del(sp, &cmdp->addr1, &cmdp->addr2, 1))
54 		return (1);
55 
56 	/* Set the cursor to the line after the last line deleted. */
57 	sp->lno = cmdp->addr1.lno;
58 
59 	/* Or the last line in the file if deleted to the end of the file. */
60 	if (db_last(sp, &lno))
61 		return (1);
62 	if (sp->lno > lno)
63 		sp->lno = lno;
64 	return (0);
65 }
66