1994-08-16 23:36:45 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1992, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1996-11-01 06:45:43 +00:00
|
|
|
* Copyright (c) 1992, 1993, 1994, 1995, 1996
|
|
|
|
* Keith Bostic. All rights reserved.
|
1994-08-16 23:36:45 +00:00
|
|
|
*
|
1996-11-01 06:45:43 +00:00
|
|
|
* See the LICENSE file for redistribution information.
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
1994-08-16 23:36:45 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/queue.h>
|
2013-08-11 09:44:58 +00:00
|
|
|
#include <sys/time.h>
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
#include <bitstring.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
#include "common.h"
|
1994-08-16 23:36:45 +00:00
|
|
|
|
2015-04-10 13:26:05 +00:00
|
|
|
static LMARK *mark_find(SCR *, ARG_CHAR_T);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/*
|
2013-08-11 09:44:58 +00:00
|
|
|
* Marks are maintained in a key sorted singly linked list. We can't
|
1994-08-16 23:36:45 +00:00
|
|
|
* use arrays because we have no idea how big an index key could be.
|
|
|
|
* The underlying assumption is that users don't have more than, say,
|
|
|
|
* 10 marks at any one time, so this will be is fast enough.
|
|
|
|
*
|
|
|
|
* Marks are fixed, and modifications to the line don't update the mark's
|
|
|
|
* position in the line. This can be hard. If you add text to the line,
|
|
|
|
* place a mark in that text, undo the addition and use ` to move to the
|
|
|
|
* mark, the location will have disappeared. It's tempting to try to adjust
|
|
|
|
* the mark with the changes in the line, but this is hard to do, especially
|
|
|
|
* if we've given the line to v_ntext.c:v_ntext() for editing. Historic vi
|
|
|
|
* would move to the first non-blank on the line when the mark location was
|
|
|
|
* past the end of the line. This can be complicated by deleting to a mark
|
1996-11-01 06:45:43 +00:00
|
|
|
* that has disappeared using the ` command. Historic vi treated this as
|
1994-08-16 23:36:45 +00:00
|
|
|
* a line-mode motion and deleted the line. This implementation complains to
|
|
|
|
* the user.
|
|
|
|
*
|
|
|
|
* In historic vi, marks returned if the operation was undone, unless the
|
|
|
|
* mark had been subsequently reset. Tricky. This is hard to start with,
|
|
|
|
* but in the presence of repeated undo it gets nasty. When a line is
|
|
|
|
* deleted, we delete (and log) any marks on that line. An undo will create
|
|
|
|
* the mark. Any mark creations are noted as to whether the user created
|
|
|
|
* it or if it was created by an undo. The former cannot be reset by another
|
|
|
|
* undo, but the latter may.
|
|
|
|
*
|
|
|
|
* All of these routines translate ABSMARK2 to ABSMARK1. Setting either of
|
|
|
|
* the absolute mark locations sets both, so that "m'" and "m`" work like
|
|
|
|
* they, ah, for lack of a better word, "should".
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mark_init --
|
|
|
|
* Set up the marks.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int mark_init(SCR *, EXF *);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2020-09-09 07:57:59 +00:00
|
|
|
mark_init(SCR *sp, EXF *ep)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
/*
|
1996-11-01 06:45:43 +00:00
|
|
|
* !!!
|
|
|
|
* ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
|
|
|
|
*
|
|
|
|
* Set up the marks.
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
2013-08-11 09:44:58 +00:00
|
|
|
SLIST_INIT(ep->marks);
|
1994-08-16 23:36:45 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mark_end --
|
|
|
|
* Free up the marks.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int mark_end(SCR *, EXF *);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2020-09-09 07:57:59 +00:00
|
|
|
mark_end(SCR *sp, EXF *ep)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
LMARK *lmp;
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
/*
|
|
|
|
* !!!
|
|
|
|
* ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
|
|
|
|
*/
|
2013-08-11 09:44:58 +00:00
|
|
|
while ((lmp = SLIST_FIRST(ep->marks)) != NULL) {
|
|
|
|
SLIST_REMOVE_HEAD(ep->marks, q);
|
1996-11-01 06:45:43 +00:00
|
|
|
free(lmp);
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mark_get --
|
|
|
|
* Get the location referenced by a mark.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int mark_get(SCR *, ARG_CHAR_T, MARK *, mtype_t);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2020-09-09 07:57:59 +00:00
|
|
|
mark_get(SCR *sp, ARG_CHAR_T key, MARK *mp, mtype_t mtype)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
LMARK *lmp;
|
|
|
|
|
|
|
|
if (key == ABSMARK2)
|
|
|
|
key = ABSMARK1;
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
lmp = mark_find(sp, key);
|
1994-08-16 23:36:45 +00:00
|
|
|
if (lmp == NULL || lmp->name != key) {
|
1996-11-01 06:45:43 +00:00
|
|
|
msgq(sp, mtype, "017|Mark %s: not set", KEY_NAME(sp, key));
|
2015-04-10 13:26:05 +00:00
|
|
|
return (1);
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|
|
|
|
if (F_ISSET(lmp, MARK_DELETED)) {
|
1996-11-01 06:45:43 +00:00
|
|
|
msgq(sp, mtype,
|
|
|
|
"018|Mark %s: the line was deleted", KEY_NAME(sp, key));
|
2015-04-10 13:26:05 +00:00
|
|
|
return (1);
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|
1996-11-01 06:45:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* !!!
|
|
|
|
* The absolute mark is initialized to lno 1/cno 0, and historically
|
|
|
|
* you could use it in an empty file. Make such a mark always work.
|
|
|
|
*/
|
|
|
|
if ((lmp->lno != 1 || lmp->cno != 0) && !db_exist(sp, lmp->lno)) {
|
|
|
|
msgq(sp, mtype,
|
|
|
|
"019|Mark %s: cursor position no longer exists",
|
1994-08-16 23:36:45 +00:00
|
|
|
KEY_NAME(sp, key));
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
mp->lno = lmp->lno;
|
|
|
|
mp->cno = lmp->cno;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mark_set --
|
|
|
|
* Set the location referenced by a mark.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int mark_set(SCR *, ARG_CHAR_T, MARK *, int);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2020-09-09 07:57:59 +00:00
|
|
|
mark_set(SCR *sp, ARG_CHAR_T key, MARK *value, int userset)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
LMARK *lmp, *lmt;
|
|
|
|
|
|
|
|
if (key == ABSMARK2)
|
|
|
|
key = ABSMARK1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The rules are simple. If the user is setting a mark (if it's a
|
|
|
|
* new mark this is always true), it always happens. If not, it's
|
|
|
|
* an undo, and we set it if it's not already set or if it was set
|
|
|
|
* by a previous undo.
|
|
|
|
*/
|
1996-11-01 06:45:43 +00:00
|
|
|
lmp = mark_find(sp, key);
|
1994-08-16 23:36:45 +00:00
|
|
|
if (lmp == NULL || lmp->name != key) {
|
2020-09-09 07:57:59 +00:00
|
|
|
MALLOC_RET(sp, lmt, sizeof(LMARK));
|
1994-08-16 23:36:45 +00:00
|
|
|
if (lmp == NULL) {
|
2013-08-11 09:44:58 +00:00
|
|
|
SLIST_INSERT_HEAD(sp->ep->marks, lmt, q);
|
1994-08-16 23:36:45 +00:00
|
|
|
} else
|
2013-08-11 09:44:58 +00:00
|
|
|
SLIST_INSERT_AFTER(lmp, lmt, q);
|
1994-08-16 23:36:45 +00:00
|
|
|
lmp = lmt;
|
|
|
|
} else if (!userset &&
|
|
|
|
!F_ISSET(lmp, MARK_DELETED) && F_ISSET(lmp, MARK_USERSET))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
lmp->lno = value->lno;
|
|
|
|
lmp->cno = value->cno;
|
|
|
|
lmp->name = key;
|
|
|
|
lmp->flags = userset ? MARK_USERSET : 0;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mark_find --
|
|
|
|
* Find the requested mark, or, the slot immediately before
|
|
|
|
* where it would go.
|
|
|
|
*/
|
|
|
|
static LMARK *
|
2020-09-09 07:57:59 +00:00
|
|
|
mark_find(SCR *sp, ARG_CHAR_T key)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
2013-08-11 09:44:58 +00:00
|
|
|
LMARK *lmp, *lastlmp = NULL;
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the requested mark or the slot immediately before
|
|
|
|
* where it should go.
|
|
|
|
*/
|
2013-08-11 09:44:58 +00:00
|
|
|
SLIST_FOREACH(lmp, sp->ep->marks, q) {
|
1994-08-16 23:36:45 +00:00
|
|
|
if (lmp->name >= key)
|
|
|
|
return (lmp->name == key ? lmp : lastlmp);
|
2013-08-11 09:44:58 +00:00
|
|
|
lastlmp = lmp;
|
|
|
|
}
|
1994-08-16 23:36:45 +00:00
|
|
|
return (lastlmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mark_insdel --
|
|
|
|
* Update the marks based on an insertion or deletion.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int mark_insdel(SCR *, lnop_t, recno_t);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
1996-11-01 06:45:43 +00:00
|
|
|
int
|
2020-09-09 07:57:59 +00:00
|
|
|
mark_insdel(SCR *sp, lnop_t op, recno_t lno)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
LMARK *lmp;
|
1996-11-01 06:45:43 +00:00
|
|
|
recno_t lline;
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case LINE_APPEND:
|
1996-11-01 06:45:43 +00:00
|
|
|
/* All insert/append operations are done as inserts. */
|
|
|
|
abort();
|
1994-08-16 23:36:45 +00:00
|
|
|
case LINE_DELETE:
|
2013-08-11 09:44:58 +00:00
|
|
|
SLIST_FOREACH(lmp, sp->ep->marks, q)
|
2020-10-01 04:25:54 +00:00
|
|
|
if (lmp->lno >= lno) {
|
1994-08-16 23:36:45 +00:00
|
|
|
if (lmp->lno == lno) {
|
|
|
|
F_SET(lmp, MARK_DELETED);
|
1996-11-01 06:45:43 +00:00
|
|
|
(void)log_mark(sp, lmp);
|
1994-08-16 23:36:45 +00:00
|
|
|
} else
|
|
|
|
--lmp->lno;
|
2020-10-01 04:25:54 +00:00
|
|
|
}
|
1996-11-01 06:45:43 +00:00
|
|
|
break;
|
1994-08-16 23:36:45 +00:00
|
|
|
case LINE_INSERT:
|
1996-11-01 06:45:43 +00:00
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* Very nasty special case. If the file was empty, then we're
|
|
|
|
* adding the first line, which is a replacement. So, we don't
|
|
|
|
* modify the marks. This is a hack to make:
|
|
|
|
*
|
|
|
|
* mz:r!echo foo<carriage-return>'z
|
|
|
|
*
|
|
|
|
* work, i.e. historically you could mark the "line" in an empty
|
|
|
|
* file and replace it, and continue to use the mark. Insane,
|
|
|
|
* well, yes, I know, but someone complained.
|
|
|
|
*
|
|
|
|
* Check for line #2 before going to the end of the file.
|
|
|
|
*/
|
|
|
|
if (!db_exist(sp, 2)) {
|
|
|
|
if (db_last(sp, &lline))
|
|
|
|
return (1);
|
|
|
|
if (lline == 1)
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
SLIST_FOREACH(lmp, sp->ep->marks, q)
|
1994-08-16 23:36:45 +00:00
|
|
|
if (lmp->lno >= lno)
|
|
|
|
++lmp->lno;
|
1996-11-01 06:45:43 +00:00
|
|
|
break;
|
1994-08-16 23:36:45 +00:00
|
|
|
case LINE_RESET:
|
1996-11-01 06:45:43 +00:00
|
|
|
break;
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|
1996-11-01 06:45:43 +00:00
|
|
|
return (0);
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|