2 * Copyright (C) 1984-2021 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
14 extern IFILE curr_ifile;
16 extern int jump_sline;
17 extern int perma_marks;
20 * A mark is an ifile (input file) plus a position within the file.
25 * Normally m_ifile != IFILE_NULL and m_filename == NULL.
26 * For restored marks we set m_filename instead of m_ifile
27 * because we don't want to create an ifile until the
28 * user explicitly requests the file (by name or mark).
30 char m_letter; /* Associated character */
31 IFILE m_ifile; /* Input file being marked */
32 char *m_filename; /* Name of the input file */
33 struct scrpos m_scrpos; /* Position of the mark */
38 * Each mark is identified by a lowercase or uppercase letter.
39 * The final one is lmark, for the "last mark"; addressed by the apostrophe.
41 #define NMARKS ((2*26)+2) /* a-z, A-Z, mousemark, lastmark */
42 #define NUMARKS ((2*26)+1) /* user marks (not lastmark) */
43 #define MOUSEMARK (NMARKS-2)
44 #define LASTMARK (NMARKS-1)
45 static struct mark marks[NMARKS];
46 public int marks_modified = 0;
50 * Initialize a mark struct.
53 cmark(m, ifile, pos, ln)
60 m->m_scrpos.pos = pos;
66 * Initialize the mark table to show no marks are set.
73 for (i = 0; i < NMARKS; i++)
77 case MOUSEMARK: letter = '#'; break;
78 case LASTMARK: letter = '\''; break;
79 default: letter = (i < 26) ? 'a'+i : 'A'+i-26; break;
81 marks[i].m_letter = letter;
82 cmark(&marks[i], NULL_IFILE, NULL_POSITION, -1);
87 * Set m_ifile and clear m_filename.
90 mark_set_ifile(m, ifile)
95 /* With m_ifile set, m_filename is no longer needed. */
101 * Populate the m_ifile member of a mark struct from m_filename.
107 if (m->m_ifile != NULL_IFILE)
108 return; /* m_ifile is already set */
109 mark_set_ifile(m, get_ifile(m->m_filename, prev_ifile(NULL_IFILE)));
113 * Return the user mark struct identified by a character.
120 if (c >= 'a' && c <= 'z')
121 return (&marks[c-'a']);
122 if (c >= 'A' && c <= 'Z')
123 return (&marks[c-'A'+26]);
125 return (&marks[LASTMARK]);
127 return (&marks[MOUSEMARK]);
128 parg.p_char = (char) c;
129 error("Invalid mark letter %c", &parg);
134 * Get the mark structure identified by a character.
135 * The mark struct may either be in the mark table (user mark)
136 * or may be constructed on the fly for certain characters like ^, $.
143 static struct mark sm;
149 * Beginning of the current file.
152 cmark(m, curr_ifile, ch_zero(), 0);
156 * End of the current file.
160 error("Cannot seek to end of file", NULL_PARG);
164 cmark(m, curr_ifile, ch_tell(), sc_height);
168 * Current position in the current file.
171 get_scrpos(&m->m_scrpos, TOP);
172 cmark(m, curr_ifile, m->m_scrpos.pos, m->m_scrpos.ln);
178 m = &marks[LASTMARK];
182 * Must be a user-defined mark.
187 if (m->m_scrpos.pos == NULL_POSITION)
189 error("Mark not set", NULL_PARG);
198 * Is a mark letter invalid?
204 return (getmark(c) == NULL);
208 * Set a user-defined mark.
216 struct scrpos scrpos;
221 get_scrpos(&scrpos, where);
222 if (scrpos.pos == NULL_POSITION)
227 cmark(m, curr_ifile, scrpos.pos, scrpos.ln);
232 * Clear a user-defined mark.
243 if (m->m_scrpos.pos == NULL_POSITION)
248 m->m_scrpos.pos = NULL_POSITION;
253 * Set lmark (the mark named by the apostrophe).
258 struct scrpos scrpos;
260 if (ch_getflags() & CH_HELPFILE)
262 get_scrpos(&scrpos, TOP);
263 if (scrpos.pos == NULL_POSITION)
265 cmark(&marks[LASTMARK], curr_ifile, scrpos.pos, scrpos.ln);
277 struct scrpos scrpos;
284 * If we're trying to go to the lastmark and
285 * it has not been set to anything yet,
286 * set it to the beginning of the current file.
287 * {{ Couldn't we instead set marks[LASTMARK] in edit()? }}
289 if (m == &marks[LASTMARK] && m->m_scrpos.pos == NULL_POSITION)
290 cmark(m, curr_ifile, ch_zero(), jump_sline);
294 /* Save scrpos; if it's LASTMARK it could change in edit_ifile. */
295 scrpos = m->m_scrpos;
296 if (m->m_ifile != curr_ifile)
299 * Not in the current file; edit the correct file.
301 if (edit_ifile(m->m_ifile))
305 jump_loc(scrpos.pos, scrpos.ln);
309 * Return the position associated with a given mark letter.
311 * We don't return which screen line the position
312 * is associated with, but this doesn't matter much,
313 * because it's always the first non-blank line on the screen.
323 return (NULL_POSITION);
325 if (m->m_ifile != curr_ifile)
327 error("Mark not in current file", NULL_PARG);
328 return (NULL_POSITION);
330 return (m->m_scrpos.pos);
334 * Return the mark associated with a given position, if any.
342 /* Only user marks */
343 for (i = 0; i < NUMARKS; i++)
345 if (marks[i].m_ifile == curr_ifile && marks[i].m_scrpos.pos == pos)
347 if (i < 26) return 'a' + i;
348 if (i < 26*2) return 'A' + (i - 26);
356 * Clear the marks associated with a specified ifile.
364 for (i = 0; i < NMARKS; i++)
365 if (marks[i].m_ifile == ifile)
366 marks[i].m_scrpos.pos = NULL_POSITION;
370 * Check if any marks refer to a specified ifile vi m_filename
371 * rather than m_ifile.
374 mark_check_ifile(ifile)
378 char *filename = get_real_filename(ifile);
380 for (i = 0; i < NMARKS; i++)
382 struct mark *m = &marks[i];
383 char *mark_filename = m->m_filename;
384 if (mark_filename != NULL)
386 mark_filename = lrealpath(mark_filename);
387 if (strcmp(filename, mark_filename) == 0)
388 mark_set_ifile(m, ifile);
397 * Save marks to history file.
400 save_marks(fout, hdr)
409 fprintf(fout, "%s\n", hdr);
410 for (i = 0; i < NMARKS; i++)
413 struct mark *m = &marks[i];
414 char pos_str[INT_STRLEN_BOUND(m->m_scrpos.pos) + 2];
415 if (m->m_scrpos.pos == NULL_POSITION)
417 postoa(m->m_scrpos.pos, pos_str);
418 filename = m->m_filename;
419 if (filename == NULL)
420 filename = get_real_filename(m->m_ifile);
421 if (strcmp(filename, "-") != 0)
422 fprintf(fout, "m %c %d %s %s\n",
423 m->m_letter, m->m_scrpos.ln, pos_str, filename);
428 * Restore one mark from the history file.
438 #define skip_whitespace while (*line == ' ') line++
442 m = getumark(*line++);
446 ln = lstrtoi(line, &line);
452 pos = lstrtopos(line, &line);
454 cmark(m, NULL_IFILE, pos, ln);
455 m->m_filename = save(line);
458 #endif /* CMD_HISTORY */