1 /* $NetBSD: ex_move.c,v 1.3 2013/11/25 22:43:46 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.
14 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 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <bitstring.h>
26 #include "../common/common.h"
29 * ex_copy -- :[line [,line]] co[py] line [flags]
30 * Copy selected lines.
32 * PUBLIC: int ex_copy __P((SCR *, EXCMD *));
35 ex_copy(SCR
*sp
, EXCMD
*cmdp
)
47 * It's possible to copy things into the area that's being
48 * copied, e.g. "2,5copy3" is legitimate. Save the text to
53 memset(&cb
, 0, sizeof(cb
));
54 TAILQ_INIT(&cb
.textq
);
55 for (cnt
= fm1
.lno
; cnt
<= fm2
.lno
; ++cnt
)
56 if (cut_line(sp
, cnt
, 0, ENTIRE_LINE
, &cb
)) {
62 /* Put the text into place. */
63 tm
.lno
= cmdp
->lineno
;
65 if (put(sp
, &cb
, NULL
, &tm
, &m
, 1))
69 * Copy puts the cursor on the last line copied. The cursor
70 * returned by the put routine is the first line put, not the
71 * last, because that's the historic semantic of vi.
73 cnt
= (fm2
.lno
- fm1
.lno
) + 1;
74 sp
->lno
= m
.lno
+ (cnt
- 1);
77 err
: text_lfree(&cb
.textq
);
82 * ex_move -- :[line [,line]] mo[ve] line
83 * Move selected lines.
85 * PUBLIC: int ex_move __P((SCR *, EXCMD *));
88 ex_move(SCR
*sp
, EXCMD
*cmdp
)
92 db_recno_t cnt
, diff
, fl
, tl
, mfl
, mtl
;
101 * It's not possible to move things into the area that's being
106 if (cmdp
->lineno
>= fm1
.lno
&& cmdp
->lineno
<= fm2
.lno
) {
107 msgq(sp
, M_ERR
, "139|Destination line is inside move range");
112 * Log the positions of any marks in the to-be-deleted lines. This
113 * has to work with the logging code. What happens is that we log
114 * the old mark positions, make the changes, then log the new mark
115 * positions. Then the marks end up in the right positions no matter
116 * which way the log is traversed.
119 * Reset the MARK_USERSET flag so that the log can undo the mark.
120 * This isn't very clean, and should probably be fixed.
125 /* Log the old positions of the marks. */
127 LIST_FOREACH(lmp
, &sp
->ep
->marks
, q
)
128 if (lmp
->name
!= ABSMARK1
&&
129 lmp
->lno
>= fl
&& lmp
->lno
<= tl
) {
131 F_CLR(lmp
, MARK_USERSET
);
132 (void)log_mark(sp
, lmp
);
135 /* Get memory for the copy. */
136 GET_SPACE_RETW(sp
, bp
, blen
, 256);
138 /* Move the lines. */
139 diff
= (fm2
.lno
- fm1
.lno
) + 1;
140 if (tl
> fl
) { /* Destination > source. */
143 for (cnt
= diff
; cnt
--;) {
144 if (db_get(sp
, fl
, DBG_FATAL
, &p
, &len
))
146 BINC_RETW(sp
, bp
, blen
, len
);
148 if (db_append(sp
, 1, tl
, bp
, len
))
151 LIST_FOREACH(lmp
, &sp
->ep
->marks
, q
)
152 if (lmp
->name
!= ABSMARK1
&&
155 if (db_delete(sp
, fl
))
158 } else { /* Destination < source. */
161 for (cnt
= diff
; cnt
--;) {
162 if (db_get(sp
, fl
, DBG_FATAL
, &p
, &len
))
164 BINC_RETW(sp
, bp
, blen
, len
);
166 if (db_append(sp
, 1, tl
++, bp
, len
))
169 LIST_FOREACH(lmp
, &sp
->ep
->marks
, q
)
170 if (lmp
->name
!= ABSMARK1
&&
174 if (db_delete(sp
, fl
))
178 FREE_SPACEW(sp
, bp
, blen
);
180 sp
->lno
= tl
; /* Last line moved. */
183 /* Log the new positions of the marks. */
185 LIST_FOREACH(lmp
, &sp
->ep
->marks
, q
)
186 if (lmp
->name
!= ABSMARK1
&&
187 lmp
->lno
>= mfl
&& lmp
->lno
<= mtl
)
188 (void)log_mark(sp
, lmp
);
191 sp
->rptlines
[L_MOVED
] += diff
;