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