1 /* $NetBSD: ex_move.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
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.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 static const char sccsid
[] = "Id: ex_move.c,v 10.15 2001/06/25 15:19:17 skimo Exp (Berkeley) Date: 2001/06/25 15:19:17 ";
19 __RCSID("$NetBSD: ex_move.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
25 #include <bitstring.h>
31 #include "../common/common.h"
34 * ex_copy -- :[line [,line]] co[py] line [flags]
35 * Copy selected lines.
37 * PUBLIC: int ex_copy __P((SCR *, EXCMD *));
40 ex_copy(SCR
*sp
, EXCMD
*cmdp
)
52 * It's possible to copy things into the area that's being
53 * copied, e.g. "2,5copy3" is legitimate. Save the text to
58 memset(&cb
, 0, sizeof(cb
));
59 TAILQ_INIT(&cb
.textq
);
60 for (cnt
= fm1
.lno
; cnt
<= fm2
.lno
; ++cnt
)
61 if (cut_line(sp
, cnt
, 0, ENTIRE_LINE
, &cb
)) {
67 /* Put the text into place. */
68 tm
.lno
= cmdp
->lineno
;
70 if (put(sp
, &cb
, NULL
, &tm
, &m
, 1))
74 * Copy puts the cursor on the last line copied. The cursor
75 * returned by the put routine is the first line put, not the
76 * last, because that's the historic semantic of vi.
78 cnt
= (fm2
.lno
- fm1
.lno
) + 1;
79 sp
->lno
= m
.lno
+ (cnt
- 1);
82 err
: text_lfree(&cb
.textq
);
87 * ex_move -- :[line [,line]] mo[ve] line
88 * Move selected lines.
90 * PUBLIC: int ex_move __P((SCR *, EXCMD *));
93 ex_move(SCR
*sp
, EXCMD
*cmdp
)
97 db_recno_t cnt
, diff
, fl
, tl
, mfl
, mtl
;
106 * It's not possible to move things into the area that's being
111 if (cmdp
->lineno
>= fm1
.lno
&& cmdp
->lineno
<= fm2
.lno
) {
112 msgq(sp
, M_ERR
, "139|Destination line is inside move range");
117 * Log the positions of any marks in the to-be-deleted lines. This
118 * has to work with the logging code. What happens is that we log
119 * the old mark positions, make the changes, then log the new mark
120 * positions. Then the marks end up in the right positions no matter
121 * which way the log is traversed.
124 * Reset the MARK_USERSET flag so that the log can undo the mark.
125 * This isn't very clean, and should probably be fixed.
130 /* Log the old positions of the marks. */
132 LIST_FOREACH(lmp
, &sp
->ep
->marks
, q
)
133 if (lmp
->name
!= ABSMARK1
&&
134 lmp
->lno
>= fl
&& lmp
->lno
<= tl
) {
136 F_CLR(lmp
, MARK_USERSET
);
137 (void)log_mark(sp
, lmp
);
140 /* Get memory for the copy. */
141 GET_SPACE_RETW(sp
, bp
, blen
, 256);
143 /* Move the lines. */
144 diff
= (fm2
.lno
- fm1
.lno
) + 1;
145 if (tl
> fl
) { /* Destination > source. */
148 for (cnt
= diff
; cnt
--;) {
149 if (db_get(sp
, fl
, DBG_FATAL
, &p
, &len
))
151 BINC_RETW(sp
, bp
, blen
, len
);
153 if (db_append(sp
, 1, tl
, bp
, len
))
156 LIST_FOREACH(lmp
, &sp
->ep
->marks
, q
)
157 if (lmp
->name
!= ABSMARK1
&&
160 if (db_delete(sp
, fl
))
163 } else { /* Destination < source. */
166 for (cnt
= diff
; cnt
--;) {
167 if (db_get(sp
, fl
, DBG_FATAL
, &p
, &len
))
169 BINC_RETW(sp
, bp
, blen
, len
);
171 if (db_append(sp
, 1, tl
++, bp
, len
))
174 LIST_FOREACH(lmp
, &sp
->ep
->marks
, q
)
175 if (lmp
->name
!= ABSMARK1
&&
179 if (db_delete(sp
, fl
))
183 FREE_SPACEW(sp
, bp
, blen
);
185 sp
->lno
= tl
; /* Last line moved. */
188 /* Log the new positions of the marks. */
190 LIST_FOREACH(lmp
, &sp
->ep
->marks
, q
)
191 if (lmp
->name
!= ABSMARK1
&&
192 lmp
->lno
>= mfl
&& lmp
->lno
<= mtl
)
193 (void)log_mark(sp
, lmp
);
196 sp
->rptlines
[L_MOVED
] += diff
;