Home | History | Annotate | Line # | Download | only in dist
mark.c revision 1.3
      1 /*	$NetBSD: mark.c,v 1.3 2013/09/04 19:44:21 tron Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1984-2012  Mark Nudelman
      5  *
      6  * You may distribute under the terms of either the GNU General Public
      7  * License or the Less License, as specified in the README file.
      8  *
      9  * For more information, see the README file.
     10  */
     11 
     12 
     13 #include "less.h"
     14 
     15 extern IFILE curr_ifile;
     16 extern int sc_height;
     17 extern int jump_sline;
     18 
     19 /*
     20  * A mark is an ifile (input file) plus a position within the file.
     21  */
     22 struct mark {
     23 	IFILE m_ifile;
     24 	struct scrpos m_scrpos;
     25 };
     26 
     27 /*
     28  * The table of marks.
     29  * Each mark is identified by a lowercase or uppercase letter.
     30  * The final one is lmark, for the "last mark"; addressed by the apostrophe.
     31  */
     32 #define	NMARKS		((2*26)+1)	/* a-z, A-Z, lastmark */
     33 #define	LASTMARK	(NMARKS-1)
     34 static struct mark marks[NMARKS];
     35 
     36 /*
     37  * Initialize the mark table to show no marks are set.
     38  */
     39 	public void
     40 init_mark()
     41 {
     42 	int i;
     43 
     44 	for (i = 0;  i < NMARKS;  i++)
     45 		marks[i].m_scrpos.pos = NULL_POSITION;
     46 }
     47 
     48 /*
     49  * See if a mark letter is valid (between a and z).
     50  */
     51 	static struct mark *
     52 getumark(c)
     53 	int c;
     54 {
     55 	if (c >= 'a' && c <= 'z')
     56 		return (&marks[c-'a']);
     57 
     58 	if (c >= 'A' && c <= 'Z')
     59 		return (&marks[c-'A'+26]);
     60 
     61 	error("Invalid mark letter", NULL_PARG);
     62 	return (NULL);
     63 }
     64 
     65 /*
     66  * Get the mark structure identified by a character.
     67  * The mark struct may come either from the mark table
     68  * or may be constructed on the fly for certain characters like ^, $.
     69  */
     70 	static struct mark *
     71 getmark(c)
     72 	int c;
     73 {
     74 	register struct mark *m;
     75 	static struct mark sm;
     76 
     77 	switch (c)
     78 	{
     79 	case '^':
     80 		/*
     81 		 * Beginning of the current file.
     82 		 */
     83 		m = &sm;
     84 		m->m_scrpos.pos = ch_zero();
     85 		m->m_scrpos.ln = 0;
     86 		m->m_ifile = curr_ifile;
     87 		break;
     88 	case '$':
     89 		/*
     90 		 * End of the current file.
     91 		 */
     92 		if (ch_end_seek())
     93 		{
     94 			error("Cannot seek to end of file", NULL_PARG);
     95 			return (NULL);
     96 		}
     97 		m = &sm;
     98 		m->m_scrpos.pos = ch_tell();
     99 		m->m_scrpos.ln = sc_height-1;
    100 		m->m_ifile = curr_ifile;
    101 		break;
    102 	case '.':
    103 		/*
    104 		 * Current position in the current file.
    105 		 */
    106 		m = &sm;
    107 		get_scrpos(&m->m_scrpos);
    108 		m->m_ifile = curr_ifile;
    109 		break;
    110 	case '\'':
    111 		/*
    112 		 * The "last mark".
    113 		 */
    114 		m = &marks[LASTMARK];
    115 		break;
    116 	default:
    117 		/*
    118 		 * Must be a user-defined mark.
    119 		 */
    120 		m = getumark(c);
    121 		if (m == NULL)
    122 			break;
    123 		if (m->m_scrpos.pos == NULL_POSITION)
    124 		{
    125 			error("Mark not set", NULL_PARG);
    126 			return (NULL);
    127 		}
    128 		break;
    129 	}
    130 	return (m);
    131 }
    132 
    133 /*
    134  * Is a mark letter is invalid?
    135  */
    136 	public int
    137 badmark(c)
    138 	int c;
    139 {
    140 	return (getmark(c) == NULL);
    141 }
    142 
    143 /*
    144  * Set a user-defined mark.
    145  */
    146 	public void
    147 setmark(c)
    148 	int c;
    149 {
    150 	register struct mark *m;
    151 	struct scrpos scrpos;
    152 
    153 	m = getumark(c);
    154 	if (m == NULL)
    155 		return;
    156 	get_scrpos(&scrpos);
    157 	m->m_scrpos = scrpos;
    158 	m->m_ifile = curr_ifile;
    159 }
    160 
    161 /*
    162  * Set lmark (the mark named by the apostrophe).
    163  */
    164 	public void
    165 lastmark()
    166 {
    167 	struct scrpos scrpos;
    168 
    169 	if (ch_getflags() & CH_HELPFILE)
    170 		return;
    171 	get_scrpos(&scrpos);
    172 	if (scrpos.pos == NULL_POSITION)
    173 		return;
    174 	marks[LASTMARK].m_scrpos = scrpos;
    175 	marks[LASTMARK].m_ifile = curr_ifile;
    176 }
    177 
    178 /*
    179  * Go to a mark.
    180  */
    181 	public void
    182 gomark(c)
    183 	int c;
    184 {
    185 	register struct mark *m;
    186 	struct scrpos scrpos;
    187 
    188 	m = getmark(c);
    189 	if (m == NULL)
    190 		return;
    191 
    192 	/*
    193 	 * If we're trying to go to the lastmark and
    194 	 * it has not been set to anything yet,
    195 	 * set it to the beginning of the current file.
    196 	 */
    197 	if (m == &marks[LASTMARK] && m->m_scrpos.pos == NULL_POSITION)
    198 	{
    199 		m->m_ifile = curr_ifile;
    200 		m->m_scrpos.pos = ch_zero();
    201 		m->m_scrpos.ln = jump_sline;
    202 	}
    203 
    204 	/*
    205 	 * If we're using lmark, we must save the screen position now,
    206 	 * because if we call edit_ifile() below, lmark will change.
    207 	 * (We save the screen position even if we're not using lmark.)
    208 	 */
    209 	scrpos = m->m_scrpos;
    210 	if (m->m_ifile != curr_ifile)
    211 	{
    212 		/*
    213 		 * Not in the current file; edit the correct file.
    214 		 */
    215 		if (edit_ifile(m->m_ifile))
    216 			return;
    217 	}
    218 
    219 	jump_loc(scrpos.pos, scrpos.ln);
    220 }
    221 
    222 /*
    223  * Return the position associated with a given mark letter.
    224  *
    225  * We don't return which screen line the position
    226  * is associated with, but this doesn't matter much,
    227  * because it's always the first non-blank line on the screen.
    228  */
    229 	public POSITION
    230 markpos(c)
    231 	int c;
    232 {
    233 	register struct mark *m;
    234 
    235 	m = getmark(c);
    236 	if (m == NULL)
    237 		return (NULL_POSITION);
    238 
    239 	if (m->m_ifile != curr_ifile)
    240 	{
    241 		error("Mark not in current file", NULL_PARG);
    242 		return (NULL_POSITION);
    243 	}
    244 	return (m->m_scrpos.pos);
    245 }
    246 
    247 /*
    248  * Clear the marks associated with a specified ifile.
    249  */
    250 	public void
    251 unmark(ifile)
    252 	IFILE ifile;
    253 {
    254 	int i;
    255 
    256 	for (i = 0;  i < NMARKS;  i++)
    257 		if (marks[i].m_ifile == ifile)
    258 			marks[i].m_scrpos.pos = NULL_POSITION;
    259 }
    260