xref: /openbsd/usr.bin/vi/ex/ex_delete.c (revision 17df1aa7)
1 /*	$OpenBSD: ex_delete.c,v 1.6 2009/10/27 23:59:47 deraadt 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(sp, cmdp)
32 	SCR *sp;
33 	EXCMD *cmdp;
34 {
35 	recno_t lno;
36 
37 	NEEDFILE(sp, cmdp);
38 
39 	/*
40 	 * !!!
41 	 * Historically, lines deleted in ex were not placed in the numeric
42 	 * buffers.  We follow historic practice so that we don't overwrite
43 	 * vi buffers accidentally.
44 	 */
45 	if (cut(sp,
46 	    FL_ISSET(cmdp->iflags, E_C_BUFFER) ? &cmdp->buffer : NULL,
47 	    &cmdp->addr1, &cmdp->addr2, CUT_LINEMODE))
48 		return (1);
49 
50 	/* Delete the lines. */
51 	if (del(sp, &cmdp->addr1, &cmdp->addr2, 1))
52 		return (1);
53 
54 	/* Set the cursor to the line after the last line deleted. */
55 	sp->lno = cmdp->addr1.lno;
56 
57 	/* Or the last line in the file if deleted to the end of the file. */
58 	if (db_last(sp, &lno))
59 		return (1);
60 	if (sp->lno > lno)
61 		sp->lno = lno;
62 	return (0);
63 }
64