2 * Copyright (C) 1984-2020 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.
119 if (c >= 'a' && c <= 'z')
120 return (&marks[c-'a']);
121 if (c >= 'A' && c <= 'Z')
122 return (&marks[c-'A'+26]);
124 return (&marks[MOUSEMARK]);
125 error("Invalid mark letter", NULL_PARG);
130 * Get the mark structure identified by a character.
131 * The mark struct may either be in the mark table (user mark)
132 * or may be constructed on the fly for certain characters like ^, $.
139 static struct mark sm;
145 * Beginning of the current file.
148 cmark(m, curr_ifile, ch_zero(), 0);
152 * End of the current file.
156 error("Cannot seek to end of file", NULL_PARG);
160 cmark(m, curr_ifile, ch_tell(), sc_height);
164 * Current position in the current file.
167 get_scrpos(&m->m_scrpos, TOP);
168 cmark(m, curr_ifile, m->m_scrpos.pos, m->m_scrpos.ln);
174 m = &marks[LASTMARK];
178 * Must be a user-defined mark.
183 if (m->m_scrpos.pos == NULL_POSITION)
185 error("Mark not set", NULL_PARG);
194 * Is a mark letter invalid?
200 return (getmark(c) == NULL);
204 * Set a user-defined mark.
212 struct scrpos scrpos;
217 get_scrpos(&scrpos, where);
218 if (scrpos.pos == NULL_POSITION)
223 cmark(m, curr_ifile, scrpos.pos, scrpos.ln);
228 * Clear a user-defined mark.
239 if (m->m_scrpos.pos == NULL_POSITION)
244 m->m_scrpos.pos = NULL_POSITION;
249 * Set lmark (the mark named by the apostrophe).
254 struct scrpos scrpos;
256 if (ch_getflags() & CH_HELPFILE)
258 get_scrpos(&scrpos, TOP);
259 if (scrpos.pos == NULL_POSITION)
261 cmark(&marks[LASTMARK], curr_ifile, scrpos.pos, scrpos.ln);
272 struct scrpos scrpos;
279 * If we're trying to go to the lastmark and
280 * it has not been set to anything yet,
281 * set it to the beginning of the current file.
282 * {{ Couldn't we instead set marks[LASTMARK] in edit()? }}
284 if (m == &marks[LASTMARK] && m->m_scrpos.pos == NULL_POSITION)
285 cmark(m, curr_ifile, ch_zero(), jump_sline);
289 /* Save scrpos; if it's LASTMARK it could change in edit_ifile. */
290 scrpos = m->m_scrpos;
291 if (m->m_ifile != curr_ifile)
294 * Not in the current file; edit the correct file.
296 if (edit_ifile(m->m_ifile))
300 jump_loc(scrpos.pos, scrpos.ln);
304 * Return the position associated with a given mark letter.
306 * We don't return which screen line the position
307 * is associated with, but this doesn't matter much,
308 * because it's always the first non-blank line on the screen.
318 return (NULL_POSITION);
320 if (m->m_ifile != curr_ifile)
322 error("Mark not in current file", NULL_PARG);
323 return (NULL_POSITION);
325 return (m->m_scrpos.pos);
329 * Return the mark associated with a given position, if any.
337 /* Only user marks */
338 for (i = 0; i < NUMARKS; i++)
340 if (marks[i].m_ifile == curr_ifile && marks[i].m_scrpos.pos == pos)
342 if (i < 26) return 'a' + i;
343 if (i < 26*2) return 'A' + (i - 26);
351 * Clear the marks associated with a specified ifile.
359 for (i = 0; i < NMARKS; i++)
360 if (marks[i].m_ifile == ifile)
361 marks[i].m_scrpos.pos = NULL_POSITION;
365 * Check if any marks refer to a specified ifile vi m_filename
366 * rather than m_ifile.
369 mark_check_ifile(ifile)
373 char *filename = lrealpath(get_filename(ifile));
375 for (i = 0; i < NMARKS; i++)
377 struct mark *m = &marks[i];
378 char *mark_filename = m->m_filename;
379 if (mark_filename != NULL)
381 mark_filename = lrealpath(mark_filename);
382 if (strcmp(filename, mark_filename) == 0)
383 mark_set_ifile(m, ifile);
393 * Save marks to history file.
396 save_marks(fout, hdr)
405 fprintf(fout, "%s\n", hdr);
406 for (i = 0; i < NUMARKS; i++)
409 struct mark *m = &marks[i];
410 char pos_str[INT_STRLEN_BOUND(m->m_scrpos.pos) + 2];
411 if (m->m_scrpos.pos == NULL_POSITION)
413 postoa(m->m_scrpos.pos, pos_str);
414 filename = m->m_filename;
415 if (filename == NULL)
416 filename = get_filename(m->m_ifile);
417 filename = lrealpath(filename);
418 if (strcmp(filename, "-") != 0)
419 fprintf(fout, "m %c %d %s %s\n",
420 m->m_letter, m->m_scrpos.ln, pos_str, filename);
426 * Restore one mark from the history file.
436 #define skip_whitespace while (*line == ' ') line++
440 m = getumark(*line++);
444 ln = lstrtoi(line, &line);
450 pos = lstrtopos(line, &line);
452 cmark(m, NULL_IFILE, pos, ln);
453 m->m_filename = save(line);
456 #endif /* CMD_HISTORY */