* Copyright (c) 1992, 1993, 1994
* The Regents of the University of California. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)v_text.c 9.6 (Berkeley) 11/16/94";
* Repeated input in the historic vi is mostly wrong and this isn't very
* backward compatible. For example, if the user entered "3Aab\ncd" in
* the historic vi, the "ab" was repeated 3 times, and the "\ncd" was then
* appended to the result. There was also a hack which I don't remember
* right now, where "3o" would open 3 lines and then let the user fill them
* in, to make screen movements on 300 baud modems more tolerable. I don't
* think it's going to be missed.
* There's a problem with the way that we do logging for change commands with
* implied motions (e.g. A, I, O, cc, etc.). Since the main vi loop logs the
* starting cursor position before the change command "moves" the cursor, the
* cursor position to which we return on undo will be where the user entered
* the change command, not the start of the change. Several of the following
* routines re-log the cursor to make this work correctly. Historic vi tried
* to do the same thing, and mostly got it right. (The only spectacular way
* it fails is if the user entered 'o' from anywhere but the last character of
* the line, the undo returned the cursor to the start of the line. If the
* user was on the last character of the line, the cursor returned to that
* position.) We also check for mapped keys waiting, i.e. if we're in the
* middle of a map, don't bother logging the cursor.
if (!MAPPED_KEYS_WAITING(sp)) \
#define LOG_CORRECT_FIRST { \
static u_int set_txt_std
__P((SCR
*, VICMDARG
*, u_int
));
* Append text to the end of the line.
if (file_gline(sp
, vp
->m_start
.lno
, &len
) != NULL
)
sp
->cno
= len
== 0 ? 0 : len
- 1;
* Append text to the cursor position.
flags
= set_txt_std(sp
, vp
, 0);
for (lno
= vp
->m_start
.lno
,
cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1; cnt
--;) {
* Move the cursor one column to the right and
if ((p
= file_gline(sp
, lno
, &len
)) == NULL
) {
if (file_lline(sp
, &lno
))
if (len
== sp
->cno
+ 1) {
sp
->tiqp
, NULL
, p
, len
, &vp
->m_final
, 0, OOBLNO
, flags
))
sp
->lno
= lno
= vp
->m_final
.lno
;
sp
->cno
= vp
->m_final
.cno
;
* Insert text at the first nonblank.
return (nonblank(sp
, vp
->m_start
.lno
, &sp
->cno
) ? 1 : v_ii(sp
, vp
));
* Insert text at the cursor position.
flags
= set_txt_std(sp
, vp
, 0);
for (lno
= vp
->m_start
.lno
,
cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1; cnt
--;) {
if ((p
= file_gline(sp
, lno
, &len
)) == NULL
) {
if (file_lline(sp
, &lno
))
GETLINE_ERR(sp
, vp
->m_start
.lno
);
/* If len == sp->cno, it's a replay caused by a count. */
if (len
== 0 || len
== sp
->cno
)
sp
->tiqp
, NULL
, p
, len
, &vp
->m_final
, 0, OOBLNO
, flags
))
sp
->lno
= lno
= vp
->m_final
.lno
;
if ((sp
->cno
= vp
->m_final
.cno
) != 0)
* Insert text above this line.
flags
= set_txt_std(sp
, vp
, TXT_APPENDEOL
);
for (first
= 1, cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1; cnt
--;) {
if (file_lline(sp
, &lno
))
/* Correct logging for implied cursor motion. */
if (file_iline(sp
, sp
->lno
, p
, 0))
if ((p
= file_gline(sp
, sp
->lno
, &len
)) == NULL
) {
GETLINE_ERR(sp
, sp
->lno
);
sp
->tiqp
, NULL
, p
, len
, &vp
->m_final
, 0, ai_line
, flags
))
sp
->lno
= lno
= vp
->m_final
.lno
;
sp
->cno
= vp
->m_final
.cno
;
* Insert text after this line.
flags
= set_txt_std(sp
, vp
, TXT_APPENDEOL
);
cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1; cnt
--;) {
if (file_lline(sp
, &lno
))
/* Correct logging for implied cursor motion. */
if (file_aline(sp
, 1, sp
->lno
, p
, len
))
if ((p
= file_gline(sp
, ++sp
->lno
, &len
)) == NULL
) {
GETLINE_ERR(sp
, sp
->lno
);
sp
->tiqp
, NULL
, p
, len
, &vp
->m_final
, 0, ai_line
, flags
))
sp
->lno
= lno
= vp
->m_final
.lno
;
sp
->cno
= vp
->m_final
.cno
;
* v_change -- [buffer][count]c[count]motion
* Find out if the file is empty, it's easier to handle it as a
if (vp
->m_start
.lno
== vp
->m_stop
.lno
&&
(p
= file_gline(sp
, vp
->m_start
.lno
, &len
)) == NULL
) {
if (file_lline(sp
, &lno
))
GETLINE_ERR(sp
, vp
->m_start
.lno
);
flags
= set_txt_std(sp
, vp
, 0);
* Move the cursor to the start of the change. Note, if autoindent
* is turned on, the cc command in line mode changes from the first
* *non-blank* character of the line, not the first character. And,
* to make it just a bit more exciting, the initial space is handled
* as auto-indent characters.
lmode
= F_ISSET(vp
, VM_LMODE
) ? CUT_LINEMODE
: 0;
if (O_ISSET(sp
, O_AUTOINDENT
)) {
if (nonblank(sp
, vp
->m_start
.lno
, &vp
->m_start
.cno
))
sp
->lno
= vp
->m_start
.lno
;
sp
->cno
= vp
->m_start
.cno
;
/* Correct logging for implied cursor motion. */
* 'c' can be combined with motion commands that set the resulting
* cursor position, i.e. "cG". Clear the VM_RCM flags and make the
* resulting cursor position stick, inserting text has its own rules
* for cursor positioning.
* If not in line mode and changing within a single line, copy the
if (!lmode
&& vp
->m_start
.lno
== vp
->m_stop
.lno
) {
* Historic practice, c did not cut into the numeric buffers,
F_ISSET(vp
, VC_BUFFER
) ? &vp
->buffer
: NULL
,
&vp
->m_start
, &vp
->m_stop
, lmode
))
LF_SET(TXT_EMARK
| TXT_OVERWRITE
);
return (v_ntext(sp
, sp
->tiqp
,
&vp
->m_stop
, p
, len
, &vp
->m_final
, 0, OOBLNO
, flags
));
* It's trickier if in line mode or changing over multiple lines. If
* we're in line mode delete all of the lines and insert a replacement
* line which the user edits. If there was leading whitespace in the
* first line being changed, we copy it and use it as the replacement.
* If we're not in line mode, we delete the text and start inserting.
* Copy the text. Historic practice, c did not cut into the numeric
* buffers, only the unnamed one.
F_ISSET(vp
, VC_BUFFER
) ? &vp
->buffer
: NULL
,
&vp
->m_start
, &vp
->m_stop
, lmode
))
/* If replacing entire lines and there's leading text. */
if (lmode
&& vp
->m_start
.cno
) {
/* Get a copy of the first line changed. */
if ((p
= file_gline(sp
, vp
->m_start
.lno
, &len
)) == NULL
) {
GETLINE_ERR(sp
, vp
->m_start
.lno
);
/* Copy the leading text elsewhere. */
GET_SPACE_RET(sp
, bp
, blen
, vp
->m_start
.cno
);
memmove(bp
, p
, vp
->m_start
.cno
);
if (delete(sp
, &vp
->m_start
, &vp
->m_stop
, lmode
))
/* If replacing entire lines, insert a replacement line. */
if (file_iline(sp
, vp
->m_start
.lno
, bp
, vp
->m_start
.cno
))
sp
->lno
= vp
->m_start
.lno
;
len
= sp
->cno
= vp
->m_start
.cno
;
/* Get the line we're editing. */
if ((p
= file_gline(sp
, vp
->m_start
.lno
, &len
)) == NULL
) {
if (file_lline(sp
, &lno
))
GETLINE_ERR(sp
, vp
->m_start
.lno
);
/* Check to see if we're appending to the line. */
if (vp
->m_start
.cno
>= len
)
sp
->tiqp
, NULL
, p
, len
, &vp
->m_final
, 0, OOBLNO
, flags
);
FREE_SPACE(sp
, bp
, blen
);
* Overwrite multiple characters.
sp
->showmode
= "Replace";
flags
= set_txt_std(sp
, vp
, 0);
cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
if ((p
= file_gline(sp
, vp
->m_start
.lno
, &len
)) == NULL
) {
if (file_lline(sp
, &lno
))
GETLINE_ERR(sp
, vp
->m_start
.lno
);
LF_SET(TXT_OVERWRITE
| TXT_REPLACE
);
vp
->m_stop
.lno
= vp
->m_start
.lno
;
vp
->m_stop
.cno
= len
? len
- 1 : 0;
if (v_ntext(sp
, sp
->tiqp
,
&vp
->m_stop
, p
, len
, &vp
->m_final
, 0, OOBLNO
, flags
))
LF_CLR(TXT_APPENDEOL
| TXT_OVERWRITE
| TXT_REPLACE
);
* Special case. The historic vi handled [count]R badly, in that R
* would replace some number of characters, and then the count would
* append count-1 copies of the replacing chars to the replaced space.
* This seems wrong, so this version counts R commands. There is some
* trickiness in moving back to where the user stopped replacing after
* each R command. Basically, if the user ended with a newline, we
* want to use vp->m_final.cno (which will be 0). Otherwise, use the
* column after the returned cursor, unless it would be past the end of
* the line, in which case we append to the line.
if ((p
= file_gline(sp
, vp
->m_final
.lno
, &len
)) == NULL
)
GETLINE_ERR(sp
, vp
->m_final
.lno
);
sp
->lno
= vp
->m_final
.lno
;
if (len
== 0 || vp
->m_final
.cno
== len
- 1) {
sp
->cno
= vp
->m_final
.cno
;
if (vp
->m_final
.cno
!= 0)
LF_SET(TXT_OVERWRITE
| TXT_REPLACE
);
vp
->m_stop
.lno
= sp
->lno
;
vp
->m_stop
.cno
= sp
->cno
;
if (v_ntext(sp
, sp
->tiqp
,
&vp
->m_stop
, p
, len
, &vp
->m_final
, 0, OOBLNO
, flags
))
LF_CLR(TXT_APPENDEOL
| TXT_OVERWRITE
| TXT_REPLACE
);
* v_subst -- [buffer][count]s
flags
= set_txt_std(sp
, vp
, 0);
if ((p
= file_gline(sp
, vp
->m_start
.lno
, &len
)) == NULL
) {
if (file_lline(sp
, &lno
))
GETLINE_ERR(sp
, vp
->m_start
.lno
);
LF_SET(TXT_EMARK
| TXT_OVERWRITE
);
vp
->m_stop
.lno
= vp
->m_start
.lno
;
vp
->m_start
.cno
+ (F_ISSET(vp
, VC_C1SET
) ? vp
->count
- 1 : 0);
if (vp
->m_stop
.cno
> len
- 1)
vp
->m_stop
.cno
= len
- 1;
F_ISSET(vp
, VC_BUFFER
) ? &vp
->buffer
: NULL
,
&vp
->m_start
, &vp
->m_stop
, 0))
return (v_ntext(sp
, sp
->tiqp
,
&vp
->m_stop
, p
, len
, &vp
->m_final
, 0, OOBLNO
, flags
));
* Initialize text processing flags.
set_txt_std(sp
, vp
, init
)
TXT_ESCAPE
| TXT_MAPINPUT
| TXT_RECORD
| TXT_RESOLVE
);
if (O_ISSET(sp
, O_ALTWERASE
))
if (O_ISSET(sp
, O_AUTOINDENT
))
if (O_ISSET(sp
, O_BEAUTIFY
))
if (O_ISSET(sp
, O_SHOWMATCH
))
if (F_ISSET(sp
, S_SCRIPT
))
if (O_ISSET(sp
, O_TTYWERASE
))
if (F_ISSET(vp
, VC_ISDOT
))
* Mapped keys were sometimes unaffected by the wrapmargin option
* in the historic 4BSD vi. Consider the following commands, where
* each is executed on an empty line, in an 80 column screen, with
* the wrapmargin value set to 60.
* :map K aABC DEF ^V<ESC><CR>KKKKK
* :map K 5aABC DEF ^V<ESC><CR>K
* The first and second commands are affected by wrapmargin. The
* third is not. (If the inserted text is itself longer than the
* wrapmargin value, i.e. if the "ABC DEF " string is replaced by
* something that's longer than 60 columns from the beginning of
* the line, the first two commands behave as before, but the third
* command gets fairly strange.) The problem is that people wrote
* macros that depended on the third command NOT being affected by
* wrapmargin, as in this gem which centers lines:
* map #c $mq81a ^V^[81^V^V|D`qld0:s/ / /g^V^M$p
* For compatibility reasons, we try and make it all work here. I
* offer no hope that this is right, but it's probably pretty close.
if ((O_ISSET(sp
, O_WRAPLEN
) || O_ISSET(sp
, O_WRAPMARGIN
)) &&
(!MAPPED_KEYS_WAITING(sp
) || !F_ISSET(vp
, VC_C1SET
)))