2012-07-23 21:31:53 +00:00
|
|
|
/*
|
2021-04-19 01:15:19 +00:00
|
|
|
* Copyright (C) 1984-2021 Mark Nudelman
|
2012-07-23 21:31:53 +00:00
|
|
|
*
|
|
|
|
* You may distribute under the terms of either the GNU General Public
|
|
|
|
* License or the Less License, as specified in the README file.
|
|
|
|
*
|
|
|
|
* For more information, see the README file.
|
|
|
|
*/
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "less.h"
|
2017-11-20 06:53:49 +00:00
|
|
|
#include "position.h"
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
extern IFILE curr_ifile;
|
|
|
|
extern int sc_height;
|
|
|
|
extern int jump_sline;
|
2019-06-29 04:39:01 +00:00
|
|
|
extern int perma_marks;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A mark is an ifile (input file) plus a position within the file.
|
|
|
|
*/
|
|
|
|
struct mark
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Normally m_ifile != IFILE_NULL and m_filename == NULL.
|
|
|
|
* For restored marks we set m_filename instead of m_ifile
|
|
|
|
* because we don't want to create an ifile until the
|
|
|
|
* user explicitly requests the file (by name or mark).
|
|
|
|
*/
|
|
|
|
char m_letter; /* Associated character */
|
|
|
|
IFILE m_ifile; /* Input file being marked */
|
|
|
|
char *m_filename; /* Name of the input file */
|
|
|
|
struct scrpos m_scrpos; /* Position of the mark */
|
|
|
|
};
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The table of marks.
|
|
|
|
* Each mark is identified by a lowercase or uppercase letter.
|
|
|
|
* The final one is lmark, for the "last mark"; addressed by the apostrophe.
|
|
|
|
*/
|
2021-04-19 01:15:19 +00:00
|
|
|
#define NMARKS ((2*26)+2) /* a-z, A-Z, mousemark, lastmark */
|
|
|
|
#define NUMARKS ((2*26)+1) /* user marks (not lastmark) */
|
|
|
|
#define MOUSEMARK (NMARKS-2)
|
|
|
|
#define LASTMARK (NMARKS-1)
|
2000-05-22 09:53:22 +00:00
|
|
|
static struct mark marks[NMARKS];
|
2019-06-29 04:39:01 +00:00
|
|
|
public int marks_modified = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize a mark struct.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
cmark(m, ifile, pos, ln)
|
|
|
|
struct mark *m;
|
|
|
|
IFILE ifile;
|
|
|
|
POSITION pos;
|
|
|
|
int ln;
|
|
|
|
{
|
|
|
|
m->m_ifile = ifile;
|
|
|
|
m->m_scrpos.pos = pos;
|
|
|
|
m->m_scrpos.ln = ln;
|
|
|
|
m->m_filename = NULL;
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the mark table to show no marks are set.
|
|
|
|
*/
|
|
|
|
public void
|
2019-06-29 04:39:01 +00:00
|
|
|
init_mark(VOID_PARAM)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NMARKS; i++)
|
2019-06-29 04:39:01 +00:00
|
|
|
{
|
|
|
|
char letter;
|
|
|
|
switch (i) {
|
|
|
|
case MOUSEMARK: letter = '#'; break;
|
|
|
|
case LASTMARK: letter = '\''; break;
|
|
|
|
default: letter = (i < 26) ? 'a'+i : 'A'+i-26; break;
|
|
|
|
}
|
|
|
|
marks[i].m_letter = letter;
|
|
|
|
cmark(&marks[i], NULL_IFILE, NULL_POSITION, -1);
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-06-29 04:39:01 +00:00
|
|
|
* Set m_ifile and clear m_filename.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
mark_set_ifile(m, ifile)
|
|
|
|
struct mark *m;
|
|
|
|
IFILE ifile;
|
|
|
|
{
|
|
|
|
m->m_ifile = ifile;
|
|
|
|
/* With m_ifile set, m_filename is no longer needed. */
|
|
|
|
free(m->m_filename);
|
|
|
|
m->m_filename = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Populate the m_ifile member of a mark struct from m_filename.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
mark_get_ifile(m)
|
|
|
|
struct mark *m;
|
|
|
|
{
|
|
|
|
if (m->m_ifile != NULL_IFILE)
|
|
|
|
return; /* m_ifile is already set */
|
|
|
|
mark_set_ifile(m, get_ifile(m->m_filename, prev_ifile(NULL_IFILE)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the user mark struct identified by a character.
|
2000-05-22 09:53:22 +00:00
|
|
|
*/
|
|
|
|
static struct mark *
|
2017-04-25 03:42:16 +00:00
|
|
|
getumark(c)
|
|
|
|
int c;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2021-04-19 01:15:19 +00:00
|
|
|
PARG parg;
|
2000-05-22 09:53:22 +00:00
|
|
|
if (c >= 'a' && c <= 'z')
|
|
|
|
return (&marks[c-'a']);
|
|
|
|
if (c >= 'A' && c <= 'Z')
|
|
|
|
return (&marks[c-'A'+26]);
|
2021-04-19 01:15:19 +00:00
|
|
|
if (c == '\'')
|
|
|
|
return (&marks[LASTMARK]);
|
2019-06-29 04:39:01 +00:00
|
|
|
if (c == '#')
|
|
|
|
return (&marks[MOUSEMARK]);
|
2021-04-19 01:15:19 +00:00
|
|
|
parg.p_char = (char) c;
|
|
|
|
error("Invalid mark letter %c", &parg);
|
2000-05-22 09:53:22 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the mark structure identified by a character.
|
2019-06-29 04:39:01 +00:00
|
|
|
* The mark struct may either be in the mark table (user mark)
|
2000-05-22 09:53:22 +00:00
|
|
|
* or may be constructed on the fly for certain characters like ^, $.
|
|
|
|
*/
|
|
|
|
static struct mark *
|
2017-04-25 03:42:16 +00:00
|
|
|
getmark(c)
|
|
|
|
int c;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2017-04-29 06:30:21 +00:00
|
|
|
struct mark *m;
|
2000-05-22 09:53:22 +00:00
|
|
|
static struct mark sm;
|
|
|
|
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '^':
|
|
|
|
/*
|
|
|
|
* Beginning of the current file.
|
|
|
|
*/
|
|
|
|
m = &sm;
|
2019-06-29 04:39:01 +00:00
|
|
|
cmark(m, curr_ifile, ch_zero(), 0);
|
2000-05-22 09:53:22 +00:00
|
|
|
break;
|
|
|
|
case '$':
|
|
|
|
/*
|
|
|
|
* End of the current file.
|
|
|
|
*/
|
|
|
|
if (ch_end_seek())
|
|
|
|
{
|
|
|
|
error("Cannot seek to end of file", NULL_PARG);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
m = &sm;
|
2019-06-29 04:39:01 +00:00
|
|
|
cmark(m, curr_ifile, ch_tell(), sc_height);
|
2000-05-22 09:53:22 +00:00
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
/*
|
|
|
|
* Current position in the current file.
|
|
|
|
*/
|
|
|
|
m = &sm;
|
2017-11-20 06:53:49 +00:00
|
|
|
get_scrpos(&m->m_scrpos, TOP);
|
2019-06-29 04:39:01 +00:00
|
|
|
cmark(m, curr_ifile, m->m_scrpos.pos, m->m_scrpos.ln);
|
2000-05-22 09:53:22 +00:00
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
/*
|
|
|
|
* The "last mark".
|
|
|
|
*/
|
|
|
|
m = &marks[LASTMARK];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* Must be a user-defined mark.
|
|
|
|
*/
|
|
|
|
m = getumark(c);
|
|
|
|
if (m == NULL)
|
|
|
|
break;
|
|
|
|
if (m->m_scrpos.pos == NULL_POSITION)
|
|
|
|
{
|
|
|
|
error("Mark not set", NULL_PARG);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (m);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-06-29 04:39:01 +00:00
|
|
|
* Is a mark letter invalid?
|
2000-05-22 09:53:22 +00:00
|
|
|
*/
|
|
|
|
public int
|
2017-04-25 03:42:16 +00:00
|
|
|
badmark(c)
|
|
|
|
int c;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
return (getmark(c) == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set a user-defined mark.
|
|
|
|
*/
|
|
|
|
public void
|
2017-11-20 06:53:49 +00:00
|
|
|
setmark(c, where)
|
2017-04-25 03:42:16 +00:00
|
|
|
int c;
|
2017-11-20 06:53:49 +00:00
|
|
|
int where;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2017-04-29 06:30:21 +00:00
|
|
|
struct mark *m;
|
2000-05-22 09:53:22 +00:00
|
|
|
struct scrpos scrpos;
|
|
|
|
|
|
|
|
m = getumark(c);
|
|
|
|
if (m == NULL)
|
|
|
|
return;
|
2017-11-20 06:53:49 +00:00
|
|
|
get_scrpos(&scrpos, where);
|
2019-06-29 04:39:01 +00:00
|
|
|
if (scrpos.pos == NULL_POSITION)
|
|
|
|
{
|
|
|
|
bell();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cmark(m, curr_ifile, scrpos.pos, scrpos.ln);
|
|
|
|
marks_modified = 1;
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 06:53:49 +00:00
|
|
|
/*
|
|
|
|
* Clear a user-defined mark.
|
|
|
|
*/
|
|
|
|
public void
|
|
|
|
clrmark(c)
|
|
|
|
int c;
|
|
|
|
{
|
|
|
|
struct mark *m;
|
|
|
|
|
|
|
|
m = getumark(c);
|
|
|
|
if (m == NULL)
|
|
|
|
return;
|
2019-06-29 04:39:01 +00:00
|
|
|
if (m->m_scrpos.pos == NULL_POSITION)
|
|
|
|
{
|
|
|
|
bell();
|
|
|
|
return;
|
|
|
|
}
|
2017-11-20 06:53:49 +00:00
|
|
|
m->m_scrpos.pos = NULL_POSITION;
|
2019-06-29 04:39:01 +00:00
|
|
|
marks_modified = 1;
|
2017-11-20 06:53:49 +00:00
|
|
|
}
|
|
|
|
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* Set lmark (the mark named by the apostrophe).
|
|
|
|
*/
|
|
|
|
public void
|
2019-06-29 04:39:01 +00:00
|
|
|
lastmark(VOID_PARAM)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
struct scrpos scrpos;
|
|
|
|
|
|
|
|
if (ch_getflags() & CH_HELPFILE)
|
|
|
|
return;
|
2017-11-20 06:53:49 +00:00
|
|
|
get_scrpos(&scrpos, TOP);
|
2000-05-22 09:53:22 +00:00
|
|
|
if (scrpos.pos == NULL_POSITION)
|
|
|
|
return;
|
2019-06-29 04:39:01 +00:00
|
|
|
cmark(&marks[LASTMARK], curr_ifile, scrpos.pos, scrpos.ln);
|
2021-04-19 01:15:19 +00:00
|
|
|
marks_modified = 1;
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Go to a mark.
|
|
|
|
*/
|
|
|
|
public void
|
2017-04-25 03:42:16 +00:00
|
|
|
gomark(c)
|
|
|
|
int c;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2017-04-29 06:30:21 +00:00
|
|
|
struct mark *m;
|
2000-05-22 09:53:22 +00:00
|
|
|
struct scrpos scrpos;
|
|
|
|
|
|
|
|
m = getmark(c);
|
|
|
|
if (m == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're trying to go to the lastmark and
|
|
|
|
* it has not been set to anything yet,
|
|
|
|
* set it to the beginning of the current file.
|
2019-06-29 04:39:01 +00:00
|
|
|
* {{ Couldn't we instead set marks[LASTMARK] in edit()? }}
|
2000-05-22 09:53:22 +00:00
|
|
|
*/
|
|
|
|
if (m == &marks[LASTMARK] && m->m_scrpos.pos == NULL_POSITION)
|
2019-06-29 04:39:01 +00:00
|
|
|
cmark(m, curr_ifile, ch_zero(), jump_sline);
|
2000-05-22 09:53:22 +00:00
|
|
|
|
2019-06-29 04:39:01 +00:00
|
|
|
mark_get_ifile(m);
|
|
|
|
|
|
|
|
/* Save scrpos; if it's LASTMARK it could change in edit_ifile. */
|
2000-05-22 09:53:22 +00:00
|
|
|
scrpos = m->m_scrpos;
|
|
|
|
if (m->m_ifile != curr_ifile)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Not in the current file; edit the correct file.
|
|
|
|
*/
|
|
|
|
if (edit_ifile(m->m_ifile))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
jump_loc(scrpos.pos, scrpos.ln);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the position associated with a given mark letter.
|
|
|
|
*
|
|
|
|
* We don't return which screen line the position
|
|
|
|
* is associated with, but this doesn't matter much,
|
|
|
|
* because it's always the first non-blank line on the screen.
|
|
|
|
*/
|
|
|
|
public POSITION
|
2017-04-25 03:42:16 +00:00
|
|
|
markpos(c)
|
|
|
|
int c;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2017-04-29 06:30:21 +00:00
|
|
|
struct mark *m;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
m = getmark(c);
|
|
|
|
if (m == NULL)
|
|
|
|
return (NULL_POSITION);
|
|
|
|
|
|
|
|
if (m->m_ifile != curr_ifile)
|
|
|
|
{
|
|
|
|
error("Mark not in current file", NULL_PARG);
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
return (m->m_scrpos.pos);
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:53:49 +00:00
|
|
|
/*
|
|
|
|
* Return the mark associated with a given position, if any.
|
|
|
|
*/
|
|
|
|
public char
|
|
|
|
posmark(pos)
|
|
|
|
POSITION pos;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2019-06-29 04:39:01 +00:00
|
|
|
/* Only user marks */
|
|
|
|
for (i = 0; i < NUMARKS; i++)
|
2017-11-20 06:53:49 +00:00
|
|
|
{
|
|
|
|
if (marks[i].m_ifile == curr_ifile && marks[i].m_scrpos.pos == pos)
|
|
|
|
{
|
|
|
|
if (i < 26) return 'a' + i;
|
2019-06-29 04:39:01 +00:00
|
|
|
if (i < 26*2) return 'A' + (i - 26);
|
|
|
|
return '#';
|
2017-11-20 06:53:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* Clear the marks associated with a specified ifile.
|
|
|
|
*/
|
|
|
|
public void
|
2017-04-25 03:42:16 +00:00
|
|
|
unmark(ifile)
|
|
|
|
IFILE ifile;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NMARKS; i++)
|
|
|
|
if (marks[i].m_ifile == ifile)
|
|
|
|
marks[i].m_scrpos.pos = NULL_POSITION;
|
|
|
|
}
|
2019-06-29 04:39:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if any marks refer to a specified ifile vi m_filename
|
|
|
|
* rather than m_ifile.
|
|
|
|
*/
|
|
|
|
public void
|
|
|
|
mark_check_ifile(ifile)
|
|
|
|
IFILE ifile;
|
|
|
|
{
|
|
|
|
int i;
|
2021-04-19 01:15:19 +00:00
|
|
|
char *filename = get_real_filename(ifile);
|
2019-06-29 04:39:01 +00:00
|
|
|
|
|
|
|
for (i = 0; i < NMARKS; i++)
|
|
|
|
{
|
|
|
|
struct mark *m = &marks[i];
|
|
|
|
char *mark_filename = m->m_filename;
|
|
|
|
if (mark_filename != NULL)
|
|
|
|
{
|
|
|
|
mark_filename = lrealpath(mark_filename);
|
|
|
|
if (strcmp(filename, mark_filename) == 0)
|
|
|
|
mark_set_ifile(m, ifile);
|
|
|
|
free(mark_filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CMD_HISTORY
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save marks to history file.
|
|
|
|
*/
|
|
|
|
public void
|
|
|
|
save_marks(fout, hdr)
|
|
|
|
FILE *fout;
|
|
|
|
char *hdr;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!perma_marks)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fprintf(fout, "%s\n", hdr);
|
2021-04-19 01:15:19 +00:00
|
|
|
for (i = 0; i < NMARKS; i++)
|
2019-06-29 04:39:01 +00:00
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
struct mark *m = &marks[i];
|
|
|
|
char pos_str[INT_STRLEN_BOUND(m->m_scrpos.pos) + 2];
|
|
|
|
if (m->m_scrpos.pos == NULL_POSITION)
|
|
|
|
continue;
|
|
|
|
postoa(m->m_scrpos.pos, pos_str);
|
|
|
|
filename = m->m_filename;
|
|
|
|
if (filename == NULL)
|
2021-04-19 01:15:19 +00:00
|
|
|
filename = get_real_filename(m->m_ifile);
|
2019-06-29 04:39:01 +00:00
|
|
|
if (strcmp(filename, "-") != 0)
|
|
|
|
fprintf(fout, "m %c %d %s %s\n",
|
|
|
|
m->m_letter, m->m_scrpos.ln, pos_str, filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Restore one mark from the history file.
|
|
|
|
*/
|
|
|
|
public void
|
|
|
|
restore_mark(line)
|
|
|
|
char *line;
|
|
|
|
{
|
|
|
|
struct mark *m;
|
|
|
|
int ln;
|
|
|
|
POSITION pos;
|
|
|
|
|
|
|
|
#define skip_whitespace while (*line == ' ') line++
|
|
|
|
if (*line++ != 'm')
|
|
|
|
return;
|
|
|
|
skip_whitespace;
|
|
|
|
m = getumark(*line++);
|
|
|
|
if (m == NULL)
|
|
|
|
return;
|
|
|
|
skip_whitespace;
|
|
|
|
ln = lstrtoi(line, &line);
|
|
|
|
if (ln < 1)
|
|
|
|
ln = 1;
|
|
|
|
if (ln > sc_height)
|
|
|
|
ln = sc_height;
|
|
|
|
skip_whitespace;
|
|
|
|
pos = lstrtopos(line, &line);
|
|
|
|
skip_whitespace;
|
|
|
|
cmark(m, NULL_IFILE, pos, ln);
|
|
|
|
m->m_filename = save(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CMD_HISTORY */
|