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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
22 #include "../common/common.h"
25 * ex_copy -- :[line [,line]] co[py] line [flags]
26 * Copy selected lines.
28 * PUBLIC: int ex_copy(SCR *, EXCMD *);
31 ex_copy(SCR
*sp
, EXCMD
*cmdp
)
43 * It's possible to copy things into the area that's being
44 * copied, e.g. "2,5copy3" is legitimate. Save the text to
50 for (cnt
= fm1
.lno
; cnt
<= fm2
.lno
; ++cnt
)
51 if (cut_line(sp
, cnt
, 0, ENTIRE_LINE
, &cb
)) {
57 /* Put the text into place. */
58 tm
.lno
= cmdp
->lineno
;
60 if (put(sp
, &cb
, NULL
, &tm
, &m
, 1))
64 * Copy puts the cursor on the last line copied. The cursor
65 * returned by the put routine is the first line put, not the
66 * last, because that's the historic semantic of vi.
68 cnt
= (fm2
.lno
- fm1
.lno
) + 1;
69 sp
->lno
= m
.lno
+ (cnt
- 1);
72 err
: text_lfree(cb
.textq
);
77 * ex_move -- :[line [,line]] mo[ve] line
78 * Move selected lines.
80 * PUBLIC: int ex_move(SCR *, EXCMD *);
83 ex_move(SCR
*sp
, EXCMD
*cmdp
)
87 recno_t cnt
, diff
, fl
, tl
, mfl
, mtl
;
96 * It's not possible to move things into the area that's being
101 if (cmdp
->lineno
>= fm1
.lno
&& cmdp
->lineno
<= fm2
.lno
) {
102 msgq(sp
, M_ERR
, "139|Destination line is inside move range");
107 * Log the positions of any marks in the to-be-deleted lines. This
108 * has to work with the logging code. What happens is that we log
109 * the old mark positions, make the changes, then log the new mark
110 * positions. Then the marks end up in the right positions no matter
111 * which way the log is traversed.
114 * Reset the MARK_USERSET flag so that the log can undo the mark.
115 * This isn't very clean, and should probably be fixed.
120 /* Log the old positions of the marks. */
122 SLIST_FOREACH(lmp
, sp
->ep
->marks
, q
)
123 if (lmp
->name
!= ABSMARK1
&&
124 lmp
->lno
>= fl
&& lmp
->lno
<= tl
) {
126 F_CLR(lmp
, MARK_USERSET
);
127 (void)log_mark(sp
, lmp
);
130 /* Get memory for the copy. */
131 GET_SPACE_RETW(sp
, bp
, blen
, 256);
133 /* Move the lines. */
134 diff
= (fm2
.lno
- fm1
.lno
) + 1;
135 if (tl
> fl
) { /* Destination > source. */
138 for (cnt
= diff
; cnt
--;) {
139 if (db_get(sp
, fl
, DBG_FATAL
, &p
, &len
))
141 BINC_RETW(sp
, bp
, blen
, len
);
143 if (db_append(sp
, 1, tl
, bp
, len
))
146 SLIST_FOREACH(lmp
, sp
->ep
->marks
, q
)
147 if (lmp
->name
!= ABSMARK1
&&
150 if (db_delete(sp
, fl
))
153 } else { /* Destination < source. */
156 for (cnt
= diff
; cnt
--;) {
157 if (db_get(sp
, fl
, DBG_FATAL
, &p
, &len
))
159 BINC_RETW(sp
, bp
, blen
, len
);
161 if (db_append(sp
, 1, tl
++, bp
, len
))
164 SLIST_FOREACH(lmp
, sp
->ep
->marks
, q
)
165 if (lmp
->name
!= ABSMARK1
&&
169 if (db_delete(sp
, fl
))
173 FREE_SPACEW(sp
, bp
, blen
);
175 sp
->lno
= tl
; /* Last line moved. */
178 /* Log the new positions of the marks. */
180 SLIST_FOREACH(lmp
, sp
->ep
->marks
, q
)
181 if (lmp
->name
!= ABSMARK1
&&
182 lmp
->lno
>= mfl
&& lmp
->lno
<= mtl
)
183 (void)log_mark(sp
, lmp
);
186 sp
->rptlines
[L_MOVED
] += diff
;