* Copyright (c) 1992, 1993
* 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
[] = "@(#)line.c 8.20 (Berkeley) 12/29/93";
static inline int scr_update
__P((SCR
*, EXF
*, recno_t
, enum operation
, int));
* Look in the text buffers for a line; if it's not there
* call file_rline to retrieve it from the database.
file_gline(sp
, ep
, lno
, lenp
)
recno_t lno
; /* Line number. */
size_t *lenp
; /* Length store. */
* The underlying recno stuff handles zero by returning NULL, but
* have to have an oob condition for the look-aside into the input
* Look-aside into the TEXT buffers and see if the line we want
if (F_ISSET(sp
, S_INPUT
)) {
l1
= ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
l2
= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
;
if (l1
<= lno
&& l2
>= lno
) {
for (tp
= sp
->tiq
.cqh_first
;
tp
->lno
!= lno
; tp
= tp
->q
.cqe_next
);
* Adjust the line number for the number of lines used
* by the text input buffers.
return (file_rline(sp
, ep
, lno
, lenp
));
* Look in the cache for a line; if it's not there retrieve
file_rline(sp
, ep
, lno
, lenp
)
recno_t lno
; /* Line number. */
size_t *lenp
; /* Length store. */
/* Get the line from the underlying database. */
switch (ep
->db
->get(ep
->db
, &key
, &data
, 0)) {
"Error: %s/%d: unable to get line %u: %s.",
tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
* Delete a line from the file.
TRACE(sp
, "delete line %lu\n", lno
);
* Marks and global commands have to know when lines are
mark_insdel(sp
, ep
, LINE_DELETE
, lno
);
global_insdel(sp
, ep
, LINE_DELETE
, lno
);
log_line(sp
, ep
, lno
, LOG_LINE_DELETE
);
if (ep
->db
->del(ep
->db
, &key
, 0) == 1) {
"Error: %s/%d: unable to delete line %u: %s.",
tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
/* Flush the cache, update line count, before screen update. */
if (ep
->c_nlines
!= OOBLNO
)
if (F_ISSET(ep
, F_FIRSTMODIFY
))
return (scr_update(sp
, ep
, lno
, LINE_DELETE
, 1));
* Append a line into the file.
file_aline(sp
, ep
, update
, lno
, p
, len
)
TRACE(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
* Very nasty special case. The historic vi code displays a single
* space (or a '$' if the list option is set) for the first line in
* an "empty" file. If we "insert" a line, that line gets scrolled
* down, not repainted, so it's incorrect when we refresh the the
* screen. This is really hard to find and fix in the vi code -- the
* text input functions detect it explicitly and don't insert a new
* line. The hack here is to repaint the screen if we're appending
if (file_lline(sp
, ep
, &lline
))
if (ep
->db
->put(ep
->db
, &key
, &data
, R_IAFTER
) == -1) {
"Error: %s/%d: unable to append to line %u: %s.",
tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
/* Flush the cache, update line count, before screen update. */
if (ep
->c_nlines
!= OOBLNO
)
if (F_ISSET(ep
, F_FIRSTMODIFY
))
log_line(sp
, ep
, lno
+ 1, LOG_LINE_APPEND
);
* Marks and global commands have to know when lines are
mark_insdel(sp
, ep
, LINE_INSERT
, lno
+ 1);
global_insdel(sp
, ep
, LINE_INSERT
, lno
+ 1);
* Nasty hack. If multiple lines are input by the user, they aren't
* committed until an <ESC> is entered. The problem is the screen was
* updated/scrolled as each line was entered. So, when this routine
* is called to copy the new lines from the cut buffer into the file,
* it has to know not to update the screen again.
return (scr_update(sp
, ep
, lno
, LINE_APPEND
, update
));
* Insert a line into the file.
file_iline(sp
, ep
, lno
, p
, len
)
"insert before %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
/* Very nasty special case. See comment in file_aline(). */
if (file_lline(sp
, ep
, &lline
))
if (ep
->db
->put(ep
->db
, &key
, &data
, R_IBEFORE
) == -1) {
"Error: %s/%d: unable to insert at line %u: %s.",
tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
/* Flush the cache, update line count, before screen update. */
if (ep
->c_nlines
!= OOBLNO
)
if (F_ISSET(ep
, F_FIRSTMODIFY
))
log_line(sp
, ep
, lno
, LOG_LINE_INSERT
);
* Marks and global commands have to know when lines are
mark_insdel(sp
, ep
, LINE_INSERT
, lno
);
global_insdel(sp
, ep
, LINE_INSERT
, lno
);
return (scr_update(sp
, ep
, lno
, LINE_INSERT
, 1));
* Store a line in the file.
file_sline(sp
, ep
, lno
, p
, len
)
"replace line %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
log_line(sp
, ep
, lno
, LOG_LINE_RESET_B
);
if (ep
->db
->put(ep
->db
, &key
, &data
, 0) == -1) {
"Error: %s/%d: unable to store line %u: %s.",
tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
/* Flush the cache, before logging or screen update. */
if (F_ISSET(ep
, F_FIRSTMODIFY
))
log_line(sp
, ep
, lno
, LOG_LINE_RESET_F
);
return (scr_update(sp
, ep
, lno
, LINE_RESET
, 1));
* Return the number of lines in the file.
if (ep
->c_nlines
!= OOBLNO
) {
*lnop
= (F_ISSET(sp
, S_INPUT
) &&
((TEXT
*)sp
->tiq
.cqh_last
)->lno
> ep
->c_nlines
?
((TEXT
*)sp
->tiq
.cqh_last
)->lno
: ep
->c_nlines
);
switch (ep
->db
->seq(ep
->db
, &key
, &data
, R_LAST
)) {
"Error: %s/%d: unable to get last line: %s.",
tail(__FILE__
), __LINE__
, strerror(errno
));
memmove(&lno
, key
.data
, sizeof(lno
));
ep
->c_nlines
= ep
->c_lno
= lno
;
*lnop
= (F_ISSET(sp
, S_INPUT
) &&
((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
* Update all of the screens that are backed by the file that
scr_update(sp
, ep
, lno
, op
, current
)
for (tsp
= sp
->gp
->dq
.cqh_first
;
tsp
!= (void *)&sp
->gp
->dq
; tsp
= tsp
->q
.cqe_next
)
if (sp
!= tsp
&& tsp
->ep
== ep
)
(void)sp
->s_change(tsp
, ep
, lno
, op
);
return (current
&& sp
->s_change(sp
, ep
, lno
, op
));