xref: /netbsd-src/external/bsd/nvi/dist/ex/ex_delete.c (revision 2f698edb5c1cb2dcd9e762b0abb50c41dde8b6b7)
1 /*	$NetBSD: ex_delete.c,v 1.3 2014/01/26 21:43:45 christos Exp $	*/
2 /*-
3  * Copyright (c) 1992, 1993, 1994
4  *	The Regents of the University of California.  All rights reserved.
5  * Copyright (c) 1992, 1993, 1994, 1995, 1996
6  *	Keith Bostic.  All rights reserved.
7  *
8  * See the LICENSE file for redistribution information.
9  */
10 
11 #include "config.h"
12 
13 #include <sys/cdefs.h>
14 #if 0
15 #ifndef lint
16 static const char sccsid[] = "Id: ex_delete.c,v 10.11 2001/06/25 15:19:15 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:15 ";
17 #endif /* not lint */
18 #else
19 __RCSID("$NetBSD: ex_delete.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20 #endif
21 
22 #include <sys/types.h>
23 #include <sys/queue.h>
24 
25 #include <bitstring.h>
26 #include <limits.h>
27 #include <stdio.h>
28 
29 #include "../common/common.h"
30 
31 /*
32  * ex_delete: [line [,line]] d[elete] [buffer] [count] [flags]
33  *
34  *	Delete lines from the file.
35  *
36  * PUBLIC: int ex_delete __P((SCR *, EXCMD *));
37  */
38 int
ex_delete(SCR * sp,EXCMD * cmdp)39 ex_delete(SCR *sp, EXCMD *cmdp)
40 {
41 	db_recno_t lno;
42 
43 	NEEDFILE(sp, cmdp);
44 
45 	/*
46 	 * !!!
47 	 * Historically, lines deleted in ex were not placed in the numeric
48 	 * buffers.  We follow historic practice so that we don't overwrite
49 	 * vi buffers accidentally.
50 	 */
51 	if (cut(sp,
52 	    FL_ISSET(cmdp->iflags, E_C_BUFFER) ? &cmdp->buffer : NULL,
53 	    &cmdp->addr1, &cmdp->addr2, CUT_LINEMODE))
54 		return (1);
55 
56 	/* Delete the lines. */
57 	if (del(sp, &cmdp->addr1, &cmdp->addr2, 1))
58 		return (1);
59 
60 	/* Set the cursor to the line after the last line deleted. */
61 	sp->lno = cmdp->addr1.lno;
62 
63 	/* Or the last line in the file if deleted to the end of the file. */
64 	if (db_last(sp, &lno))
65 		return (1);
66 	if (sp->lno > lno)
67 		sp->lno = lno;
68 	return (0);
69 }
70