2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: ex_move.c,v 10.16 2012/02/11 15:52:33 zy Exp $";
16 #include <sys/types.h>
17 #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(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
54 for (cnt
= fm1
.lno
; cnt
<= fm2
.lno
; ++cnt
)
55 if (cut_line(sp
, cnt
, 0, ENTIRE_LINE
, &cb
)) {
61 /* Put the text into place. */
62 tm
.lno
= cmdp
->lineno
;
64 if (put(sp
, &cb
, NULL
, &tm
, &m
, 1))
68 * Copy puts the cursor on the last line copied. The cursor
69 * returned by the put routine is the first line put, not the
70 * last, because that's the historic semantic of vi.
72 cnt
= (fm2
.lno
- fm1
.lno
) + 1;
73 sp
->lno
= m
.lno
+ (cnt
- 1);
76 err
: text_lfree(cb
.textq
);
81 * ex_move -- :[line [,line]] mo[ve] line
82 * Move selected lines.
84 * PUBLIC: int ex_move(SCR *, EXCMD *);
87 ex_move(SCR
*sp
, EXCMD
*cmdp
)
91 recno_t cnt
, diff
, fl
, tl
, mfl
, mtl
;
100 * It's not possible to move things into the area that's being
105 if (cmdp
->lineno
>= fm1
.lno
&& cmdp
->lineno
<= fm2
.lno
) {
106 msgq(sp
, M_ERR
, "139|Destination line is inside move range");
111 * Log the positions of any marks in the to-be-deleted lines. This
112 * has to work with the logging code. What happens is that we log
113 * the old mark positions, make the changes, then log the new mark
114 * positions. Then the marks end up in the right positions no matter
115 * which way the log is traversed.
118 * Reset the MARK_USERSET flag so that the log can undo the mark.
119 * This isn't very clean, and should probably be fixed.
124 /* Log the old positions of the marks. */
126 SLIST_FOREACH(lmp
, sp
->ep
->marks
, q
)
127 if (lmp
->name
!= ABSMARK1
&&
128 lmp
->lno
>= fl
&& lmp
->lno
<= tl
) {
130 F_CLR(lmp
, MARK_USERSET
);
131 (void)log_mark(sp
, lmp
);
134 /* Get memory for the copy. */
135 GET_SPACE_RETW(sp
, bp
, blen
, 256);
137 /* Move the lines. */
138 diff
= (fm2
.lno
- fm1
.lno
) + 1;
139 if (tl
> fl
) { /* Destination > source. */
142 for (cnt
= diff
; cnt
--;) {
143 if (db_get(sp
, fl
, DBG_FATAL
, &p
, &len
))
145 BINC_RETW(sp
, bp
, blen
, len
);
147 if (db_append(sp
, 1, tl
, bp
, len
))
150 SLIST_FOREACH(lmp
, sp
->ep
->marks
, q
)
151 if (lmp
->name
!= ABSMARK1
&&
154 if (db_delete(sp
, fl
))
157 } else { /* Destination < source. */
160 for (cnt
= diff
; cnt
--;) {
161 if (db_get(sp
, fl
, DBG_FATAL
, &p
, &len
))
163 BINC_RETW(sp
, bp
, blen
, len
);
165 if (db_append(sp
, 1, tl
++, bp
, len
))
168 SLIST_FOREACH(lmp
, sp
->ep
->marks
, q
)
169 if (lmp
->name
!= ABSMARK1
&&
173 if (db_delete(sp
, fl
))
177 FREE_SPACEW(sp
, bp
, blen
);
179 sp
->lno
= tl
; /* Last line moved. */
182 /* Log the new positions of the marks. */
184 SLIST_FOREACH(lmp
, sp
->ep
->marks
, q
)
185 if (lmp
->name
!= ABSMARK1
&&
186 lmp
->lno
>= mfl
&& lmp
->lno
<= mtl
)
187 (void)log_mark(sp
, lmp
);
190 sp
->rptlines
[L_MOVED
] += diff
;