blob: dc9bece44f2f7008176697d1207f5ef4b99dfb3a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * mark.c: functions for setting marks and jumping to them
12 */
13
14#include "vim.h"
15
16/*
17 * This file contains routines to maintain and manipulate marks.
18 */
19
20/*
21 * If a named file mark's lnum is non-zero, it is valid.
22 * If a named file mark's fnum is non-zero, it is for an existing buffer,
23 * otherwise it is from .viminfo and namedfm[n].fname is the file name.
24 * There are marks 'A - 'Z (set by user) and '0 to '9 (set when writing
25 * viminfo).
26 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010027static xfmark_T namedfm[NMARKS + EXTRA_MARKS]; // marks with file nr
Bram Moolenaar071d4272004-06-13 20:20:40 +000028
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020029static void fname2fnum(xfmark_T *fm);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010030static void fmarks_check_one(xfmark_T *fm, char_u *name, buf_T *buf);
31static char_u *mark_line(pos_T *mp, int lead_len);
32static void show_one_mark(int, char_u *, pos_T *, char_u *, int current);
Bram Moolenaar88d298a2017-03-14 21:53:58 +010033static void mark_adjust_internal(linenr_T line1, linenr_T line2, long amount,
34 long amount_after, int adjust_folds);
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
36/*
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000037 * Set named mark "c" at current cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +000038 * Returns OK on success, FAIL if bad name given.
39 */
40 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +010041setmark(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000042{
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000043 return setmark_pos(c, &curwin->w_cursor, curbuf->b_fnum);
44}
45
46/*
47 * Set named mark "c" to position "pos".
48 * When "c" is upper case use file "fnum".
49 * Returns OK on success, FAIL if bad name given.
50 */
51 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +010052setmark_pos(int c, pos_T *pos, int fnum)
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000053{
Bram Moolenaar071d4272004-06-13 20:20:40 +000054 int i;
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010055 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010057 // Check for a special key (may cause islower() to crash).
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 if (c < 0)
59 return FAIL;
60
61 if (c == '\'' || c == '`')
62 {
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000063 if (pos == &curwin->w_cursor)
64 {
65 setpcmark();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010066 // keep it even when the cursor doesn't move
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000067 curwin->w_prev_pcmark = curwin->w_pcmark;
68 }
69 else
70 curwin->w_pcmark = *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000071 return OK;
72 }
73
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010074 buf = buflist_findnr(fnum);
75 if (buf == NULL)
76 return FAIL;
77
Bram Moolenaar08250432008-02-13 11:42:46 +000078 if (c == '"')
79 {
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010080 buf->b_last_cursor = *pos;
Bram Moolenaar08250432008-02-13 11:42:46 +000081 return OK;
82 }
83
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010084 // Allow setting '[ and '] for an autocommand that simulates reading a
85 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 if (c == '[')
87 {
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010088 buf->b_op_start = *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 return OK;
90 }
91 if (c == ']')
92 {
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010093 buf->b_op_end = *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 return OK;
95 }
96
Bram Moolenaarbc88a272013-08-02 17:22:23 +020097 if (c == '<' || c == '>')
Bram Moolenaar0306ac32012-07-06 17:51:28 +020098 {
Bram Moolenaarbc88a272013-08-02 17:22:23 +020099 if (c == '<')
Bram Moolenaarf13e00b2017-01-28 18:23:54 +0100100 buf->b_visual.vi_start = *pos;
Bram Moolenaarbc88a272013-08-02 17:22:23 +0200101 else
Bram Moolenaarf13e00b2017-01-28 18:23:54 +0100102 buf->b_visual.vi_end = *pos;
103 if (buf->b_visual.vi_mode == NUL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100104 // Visual_mode has not yet been set, use a sane default.
Bram Moolenaarf13e00b2017-01-28 18:23:54 +0100105 buf->b_visual.vi_mode = 'v';
Bram Moolenaar0306ac32012-07-06 17:51:28 +0200106 return OK;
107 }
Bram Moolenaar0306ac32012-07-06 17:51:28 +0200108
Bram Moolenaar2d358992016-06-12 21:20:54 +0200109 if (ASCII_ISLOWER(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110 {
111 i = c - 'a';
Bram Moolenaarf13e00b2017-01-28 18:23:54 +0100112 buf->b_namedm[i] = *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113 return OK;
114 }
Bram Moolenaar2d358992016-06-12 21:20:54 +0200115 if (ASCII_ISUPPER(c) || VIM_ISDIGIT(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 {
Bram Moolenaar2d358992016-06-12 21:20:54 +0200117 if (VIM_ISDIGIT(c))
118 i = c - '0' + NMARKS;
119 else
120 i = c - 'A';
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000121 namedfm[i].fmark.mark = *pos;
122 namedfm[i].fmark.fnum = fnum;
Bram Moolenaard23a8232018-02-10 18:45:26 +0100123 VIM_CLEAR(namedfm[i].fname);
Bram Moolenaar2d358992016-06-12 21:20:54 +0200124#ifdef FEAT_VIMINFO
125 namedfm[i].time_set = vim_time();
126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127 return OK;
128 }
129 return FAIL;
130}
131
132/*
133 * Set the previous context mark to the current position and add it to the
134 * jump list.
135 */
136 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100137setpcmark(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 int i;
140 xfmark_T *fm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100142 // for :global the mark is set only once
Bram Moolenaare1004402020-10-24 20:49:43 +0200143 if (global_busy || listcmd_busy || (cmdmod.cmod_flags & CMOD_KEEPJUMPS))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 return;
145
146 curwin->w_prev_pcmark = curwin->w_pcmark;
147 curwin->w_pcmark = curwin->w_cursor;
148
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100149 // If jumplist is full: remove oldest entry
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 if (++curwin->w_jumplistlen > JUMPLISTSIZE)
151 {
152 curwin->w_jumplistlen = JUMPLISTSIZE;
153 vim_free(curwin->w_jumplist[0].fname);
154 for (i = 1; i < JUMPLISTSIZE; ++i)
155 curwin->w_jumplist[i - 1] = curwin->w_jumplist[i];
156 }
157 curwin->w_jumplistidx = curwin->w_jumplistlen;
158 fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];
159
160 fm->fmark.mark = curwin->w_pcmark;
161 fm->fmark.fnum = curbuf->b_fnum;
162 fm->fname = NULL;
Bram Moolenaar739f13a2021-12-13 13:12:53 +0000163#ifdef FEAT_VIMINFO
Bram Moolenaar2d358992016-06-12 21:20:54 +0200164 fm->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166}
167
168/*
169 * To change context, call setpcmark(), then move the current position to
170 * where ever, then call checkpcmark(). This ensures that the previous
171 * context will only be changed if the cursor moved to a different line.
172 * If pcmark was deleted (with "dG") the previous mark is restored.
173 */
174 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100175checkpcmark(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176{
177 if (curwin->w_prev_pcmark.lnum != 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100178 && (EQUAL_POS(curwin->w_pcmark, curwin->w_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 || curwin->w_pcmark.lnum == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 curwin->w_pcmark = curwin->w_prev_pcmark;
Bram Moolenaare08aee62021-10-17 21:53:58 +0100181 curwin->w_prev_pcmark.lnum = 0; // it has been checked
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182}
183
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/*
185 * move "count" positions in the jump list (count may be negative)
186 */
187 pos_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100188movemark(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189{
190 pos_T *pos;
191 xfmark_T *jmp;
192
Bram Moolenaar48679742018-02-13 13:33:29 +0100193 cleanup_jumplist(curwin, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100195 if (curwin->w_jumplistlen == 0) // nothing to jump to
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 return (pos_T *)NULL;
197
198 for (;;)
199 {
200 if (curwin->w_jumplistidx + count < 0
201 || curwin->w_jumplistidx + count >= curwin->w_jumplistlen)
202 return (pos_T *)NULL;
203
204 /*
205 * if first CTRL-O or CTRL-I command after a jump, add cursor position
Bram Moolenaarf711faf2007-05-10 16:48:19 +0000206 * to list. Careful: If there are duplicates (CTRL-O immediately after
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 * starting Vim on a file), another entry may have been removed.
208 */
209 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
210 {
211 setpcmark();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100212 --curwin->w_jumplistidx; // skip the new entry
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 if (curwin->w_jumplistidx + count < 0)
214 return (pos_T *)NULL;
215 }
216
217 curwin->w_jumplistidx += count;
218
219 jmp = curwin->w_jumplist + curwin->w_jumplistidx;
220 if (jmp->fmark.fnum == 0)
221 fname2fnum(jmp);
222 if (jmp->fmark.fnum != curbuf->b_fnum)
223 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100224 // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 if (buflist_findnr(jmp->fmark.fnum) == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100226 { // Skip this one ..
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 count += count < 0 ? -1 : 1;
228 continue;
229 }
230 if (buflist_getfile(jmp->fmark.fnum, jmp->fmark.mark.lnum,
231 0, FALSE) == FAIL)
232 return (pos_T *)NULL;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100233 // Set lnum again, autocommands my have changed it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 curwin->w_cursor = jmp->fmark.mark;
235 pos = (pos_T *)-1;
236 }
237 else
238 pos = &(jmp->fmark.mark);
239 return pos;
240 }
241}
242
243/*
244 * Move "count" positions in the changelist (count may be negative).
245 */
246 pos_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100247movechangelist(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
249 int n;
250
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100251 if (curbuf->b_changelistlen == 0) // nothing to jump to
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 return (pos_T *)NULL;
253
254 n = curwin->w_changelistidx;
255 if (n + count < 0)
256 {
257 if (n == 0)
258 return (pos_T *)NULL;
259 n = 0;
260 }
261 else if (n + count >= curbuf->b_changelistlen)
262 {
263 if (n == curbuf->b_changelistlen - 1)
264 return (pos_T *)NULL;
265 n = curbuf->b_changelistlen - 1;
266 }
267 else
268 n += count;
269 curwin->w_changelistidx = n;
270 return curbuf->b_changelist + n;
271}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272
273/*
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100274 * Find mark "c" in buffer pointed to by "buf".
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000275 * If "changefile" is TRUE it's allowed to edit another file for '0, 'A, etc.
276 * If "fnum" is not NULL store the fnum there for '0, 'A etc., don't edit
277 * another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 * Returns:
279 * - pointer to pos_T if found. lnum is 0 when mark not set, -1 when mark is
280 * in another file which can't be gotten. (caller needs to check lnum!)
281 * - NULL if there is no mark called 'c'.
282 * - -1 if mark is in other file and jumped there (only if changefile is TRUE)
283 */
284 pos_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100285getmark_buf(buf_T *buf, int c, int changefile)
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100286{
287 return getmark_buf_fnum(buf, c, changefile, NULL);
288}
289
290 pos_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100291getmark(int c, int changefile)
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000292{
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100293 return getmark_buf_fnum(curbuf, c, changefile, NULL);
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000294}
295
296 pos_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100297getmark_buf_fnum(
298 buf_T *buf,
299 int c,
300 int changefile,
301 int *fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302{
303 pos_T *posp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 pos_T *startp, *endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 static pos_T pos_copy;
306
307 posp = NULL;
308
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100309 // Check for special key, can't be a mark name and might cause islower()
310 // to crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 if (c < 0)
312 return posp;
313#ifndef EBCDIC
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100314 if (c > '~') // check for islower()/isupper()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 ;
316 else
317#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100318 if (c == '\'' || c == '`') // previous context mark
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100320 pos_copy = curwin->w_pcmark; // need to make a copy because
321 posp = &pos_copy; // w_pcmark may be changed soon
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100323 else if (c == '"') // to pos when leaving buffer
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100324 posp = &(buf->b_last_cursor);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100325 else if (c == '^') // to where Insert mode stopped
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100326 posp = &(buf->b_last_insert);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100327 else if (c == '.') // to where last change was made
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100328 posp = &(buf->b_last_change);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100329 else if (c == '[') // to start of previous operator
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100330 posp = &(buf->b_op_start);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100331 else if (c == ']') // to end of previous operator
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100332 posp = &(buf->b_op_end);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100333 else if (c == '{' || c == '}') // to previous/next paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 {
335 pos_T pos;
336 oparg_T oa;
337 int slcb = listcmd_busy;
338
339 pos = curwin->w_cursor;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100340 listcmd_busy = TRUE; // avoid that '' is changed
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000341 if (findpar(&oa.inclusive,
342 c == '}' ? FORWARD : BACKWARD, 1L, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 {
344 pos_copy = curwin->w_cursor;
345 posp = &pos_copy;
346 }
347 curwin->w_cursor = pos;
348 listcmd_busy = slcb;
349 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100350 else if (c == '(' || c == ')') // to previous/next sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 {
352 pos_T pos;
353 int slcb = listcmd_busy;
354
355 pos = curwin->w_cursor;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100356 listcmd_busy = TRUE; // avoid that '' is changed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 if (findsent(c == ')' ? FORWARD : BACKWARD, 1L))
358 {
359 pos_copy = curwin->w_cursor;
360 posp = &pos_copy;
361 }
362 curwin->w_cursor = pos;
363 listcmd_busy = slcb;
364 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100365 else if (c == '<' || c == '>') // start/end of visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100367 startp = &buf->b_visual.vi_start;
368 endp = &buf->b_visual.vi_end;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100369 if (((c == '<') == LT_POS(*startp, *endp) || endp->lnum == 0)
Bram Moolenaarf13e00b2017-01-28 18:23:54 +0100370 && startp->lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 posp = startp;
372 else
373 posp = endp;
374 /*
375 * For Visual line mode, set mark at begin or end of line
376 */
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100377 if (buf->b_visual.vi_mode == 'V')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {
379 pos_copy = *posp;
380 posp = &pos_copy;
381 if (c == '<')
382 pos_copy.col = 0;
383 else
384 pos_copy.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 pos_copy.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 }
387 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100388 else if (ASCII_ISLOWER(c)) // normal named mark
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100390 posp = &(buf->b_namedm[c - 'a']);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100392 else if (ASCII_ISUPPER(c) || VIM_ISDIGIT(c)) // named file mark
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 {
394 if (VIM_ISDIGIT(c))
395 c = c - '0' + NMARKS;
396 else
397 c -= 'A';
398 posp = &(namedfm[c].fmark.mark);
399
400 if (namedfm[c].fmark.fnum == 0)
401 fname2fnum(&namedfm[c]);
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000402
403 if (fnum != NULL)
404 *fnum = namedfm[c].fmark.fnum;
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100405 else if (namedfm[c].fmark.fnum != buf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100407 // mark is in another file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 posp = &pos_copy;
409
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 if (namedfm[c].fmark.mark.lnum != 0
411 && changefile && namedfm[c].fmark.fnum)
412 {
413 if (buflist_getfile(namedfm[c].fmark.fnum,
414 (linenr_T)1, GETF_SETMARK, FALSE) == OK)
415 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100416 // Set the lnum now, autocommands could have changed it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 curwin->w_cursor = namedfm[c].fmark.mark;
418 return (pos_T *)-1;
419 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100420 pos_copy.lnum = -1; // can't get file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 }
422 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100423 pos_copy.lnum = 0; // mark exists, but is not valid in
424 // current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 }
426 }
427
428 return posp;
429}
430
431/*
432 * Search for the next named mark in the current file.
433 *
434 * Returns pointer to pos_T of the next mark or NULL if no mark is found.
435 */
436 pos_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100437getnextmark(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100438 pos_T *startpos, // where to start
439 int dir, // direction for search
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100440 int begin_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441{
442 int i;
443 pos_T *result = NULL;
444 pos_T pos;
445
446 pos = *startpos;
447
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100448 // When searching backward and leaving the cursor on the first non-blank,
449 // position must be in a previous line.
450 // When searching forward and leaving the cursor on the first non-blank,
451 // position must be in a next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 if (dir == BACKWARD && begin_line)
453 pos.col = 0;
454 else if (dir == FORWARD && begin_line)
455 pos.col = MAXCOL;
456
457 for (i = 0; i < NMARKS; i++)
458 {
459 if (curbuf->b_namedm[i].lnum > 0)
460 {
461 if (dir == FORWARD)
462 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100463 if ((result == NULL || LT_POS(curbuf->b_namedm[i], *result))
464 && LT_POS(pos, curbuf->b_namedm[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 result = &curbuf->b_namedm[i];
466 }
467 else
468 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100469 if ((result == NULL || LT_POS(*result, curbuf->b_namedm[i]))
470 && LT_POS(curbuf->b_namedm[i], pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 result = &curbuf->b_namedm[i];
472 }
473 }
474 }
475
476 return result;
477}
478
479/*
480 * For an xtended filemark: set the fnum from the fname.
481 * This is used for marks obtained from the .viminfo file. It's postponed
482 * until the mark is used to avoid a long startup delay.
483 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200484 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100485fname2fnum(xfmark_T *fm)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486{
487 char_u *p;
488
489 if (fm->fname != NULL)
490 {
491 /*
492 * First expand "~/" in the file name to the home directory.
Bram Moolenaar525ad4d2008-01-03 19:22:13 +0000493 * Don't expand the whole name, it may contain other '~' chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 */
Bram Moolenaar525ad4d2008-01-03 19:22:13 +0000495 if (fm->fname[0] == '~' && (fm->fname[1] == '/'
496#ifdef BACKSLASH_IN_FILENAME
497 || fm->fname[1] == '\\'
498#endif
499 ))
500 {
501 int len;
502
503 expand_env((char_u *)"~/", NameBuff, MAXPATHL);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +0000504 len = (int)STRLEN(NameBuff);
Bram Moolenaar525ad4d2008-01-03 19:22:13 +0000505 vim_strncpy(NameBuff + len, fm->fname + 2, MAXPATHL - len - 1);
506 }
507 else
508 vim_strncpy(NameBuff, fm->fname, MAXPATHL - 1);
509
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100510 // Try to shorten the file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 mch_dirname(IObuff, IOSIZE);
512 p = shorten_fname(NameBuff, IObuff);
513
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100514 // buflist_new() will call fmarks_check_names()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 (void)buflist_new(NameBuff, p, (linenr_T)1, 0);
516 }
517}
518
519/*
520 * Check all file marks for a name that matches the file name in buf.
521 * May replace the name with an fnum.
522 * Used for marks that come from the .viminfo file.
523 */
524 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100525fmarks_check_names(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526{
527 char_u *name;
528 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530
531 if (buf->b_ffname == NULL)
532 return;
533
534 name = home_replace_save(buf, buf->b_ffname);
535 if (name == NULL)
536 return;
537
538 for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
539 fmarks_check_one(&namedfm[i], name, buf);
540
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 FOR_ALL_WINDOWS(wp)
542 {
543 for (i = 0; i < wp->w_jumplistlen; ++i)
544 fmarks_check_one(&wp->w_jumplist[i], name, buf);
545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546
547 vim_free(name);
548}
549
550 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100551fmarks_check_one(xfmark_T *fm, char_u *name, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552{
553 if (fm->fmark.fnum == 0
554 && fm->fname != NULL
555 && fnamecmp(name, fm->fname) == 0)
556 {
557 fm->fmark.fnum = buf->b_fnum;
Bram Moolenaard23a8232018-02-10 18:45:26 +0100558 VIM_CLEAR(fm->fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 }
560}
561
562/*
563 * Check a if a position from a mark is valid.
564 * Give and error message and return FAIL if not.
565 */
566 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100567check_mark(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568{
569 if (pos == NULL)
570 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000571 emsg(_(e_unknown_mark));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 return FAIL;
573 }
574 if (pos->lnum <= 0)
575 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100576 // lnum is negative if mark is in another file can can't get that
577 // file, error message already give then.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 if (pos->lnum == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200579 emsg(_(e_mark_not_set));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 return FAIL;
581 }
582 if (pos->lnum > curbuf->b_ml.ml_line_count)
583 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200584 emsg(_(e_mark_has_invalid_line_number));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 return FAIL;
586 }
587 return OK;
588}
589
590/*
591 * clrallmarks() - clear all marks in the buffer 'buf'
592 *
593 * Used mainly when trashing the entire buffer during ":e" type commands
594 */
595 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100596clrallmarks(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597{
598 static int i = -1;
599
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100600 if (i == -1) // first call ever: initialize
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 for (i = 0; i < NMARKS + 1; i++)
602 {
603 namedfm[i].fmark.mark.lnum = 0;
604 namedfm[i].fname = NULL;
Bram Moolenaar2d358992016-06-12 21:20:54 +0200605#ifdef FEAT_VIMINFO
606 namedfm[i].time_set = 0;
607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 }
609
610 for (i = 0; i < NMARKS; i++)
611 buf->b_namedm[i].lnum = 0;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100612 buf->b_op_start.lnum = 0; // start/end op mark cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 buf->b_op_end.lnum = 0;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100614 buf->b_last_cursor.lnum = 1; // '" mark cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 buf->b_last_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 buf->b_last_cursor.coladd = 0;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100617 buf->b_last_insert.lnum = 0; // '^ mark cleared
618 buf->b_last_change.lnum = 0; // '. mark cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 buf->b_changelistlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620}
621
622/*
623 * Get name of file from a filemark.
624 * When it's in the current buffer, return the text at the mark.
625 * Returns an allocated string.
626 */
627 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100628fm_getname(fmark_T *fmark, int lead_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100630 if (fmark->fnum == curbuf->b_fnum) // current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 return mark_line(&(fmark->mark), lead_len);
632 return buflist_nr2name(fmark->fnum, FALSE, TRUE);
633}
634
635/*
636 * Return the line at mark "mp". Truncate to fit in window.
637 * The returned string has been allocated.
638 */
639 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100640mark_line(pos_T *mp, int lead_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 char_u *s, *p;
643 int len;
644
645 if (mp->lnum == 0 || mp->lnum > curbuf->b_ml.ml_line_count)
646 return vim_strsave((char_u *)"-invalid-");
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200647 // Allow for up to 5 bytes per character.
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200648 s = vim_strnsave(skipwhite(ml_get(mp->lnum)), Columns * 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if (s == NULL)
650 return NULL;
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200651 // Truncate the line to fit it in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 len = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100653 for (p = s; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 {
655 len += ptr2cells(p);
656 if (len >= Columns - lead_len)
657 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 }
659 *p = NUL;
660 return s;
661}
662
663/*
664 * print the marks
665 */
666 void
Bram Moolenaar4bd78232019-09-19 23:21:55 +0200667ex_marks(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668{
669 char_u *arg = eap->arg;
670 int i;
671 char_u *name;
Bram Moolenaar54c3fcd2020-07-19 22:09:06 +0200672 pos_T *posp, *startp, *endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673
674 if (arg != NULL && *arg == NUL)
675 arg = NULL;
676
677 show_one_mark('\'', arg, &curwin->w_pcmark, NULL, TRUE);
678 for (i = 0; i < NMARKS; ++i)
679 show_one_mark(i + 'a', arg, &curbuf->b_namedm[i], NULL, TRUE);
680 for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
681 {
682 if (namedfm[i].fmark.fnum != 0)
683 name = fm_getname(&namedfm[i].fmark, 15);
684 else
685 name = namedfm[i].fname;
686 if (name != NULL)
687 {
688 show_one_mark(i >= NMARKS ? i - NMARKS + '0' : i + 'A',
689 arg, &namedfm[i].fmark.mark, name,
690 namedfm[i].fmark.fnum == curbuf->b_fnum);
691 if (namedfm[i].fmark.fnum != 0)
692 vim_free(name);
693 }
694 }
695 show_one_mark('"', arg, &curbuf->b_last_cursor, NULL, TRUE);
696 show_one_mark('[', arg, &curbuf->b_op_start, NULL, TRUE);
697 show_one_mark(']', arg, &curbuf->b_op_end, NULL, TRUE);
698 show_one_mark('^', arg, &curbuf->b_last_insert, NULL, TRUE);
699 show_one_mark('.', arg, &curbuf->b_last_change, NULL, TRUE);
Bram Moolenaar54c3fcd2020-07-19 22:09:06 +0200700
701 // Show the marks as where they will jump to.
702 startp = &curbuf->b_visual.vi_start;
703 endp = &curbuf->b_visual.vi_end;
704 if ((LT_POS(*startp, *endp) || endp->lnum == 0) && startp->lnum != 0)
705 posp = startp;
706 else
707 posp = endp;
708 show_one_mark('<', arg, posp, NULL, TRUE);
709 show_one_mark('>', arg, posp == startp ? endp : startp, NULL, TRUE);
710
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 show_one_mark(-1, arg, NULL, NULL, FALSE);
712}
713
714 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100715show_one_mark(
716 int c,
717 char_u *arg,
718 pos_T *p,
Bram Moolenaarad6dc492019-04-27 22:40:08 +0200719 char_u *name_arg,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100720 int current) // in current file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721{
722 static int did_title = FALSE;
723 int mustfree = FALSE;
Bram Moolenaarad6dc492019-04-27 22:40:08 +0200724 char_u *name = name_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100726 if (c == -1) // finish up
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {
728 if (did_title)
729 did_title = FALSE;
730 else
731 {
732 if (arg == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100733 msg(_("No marks set"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100735 semsg(_("E283: No marks matching \"%s\""), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 }
737 }
Bram Moolenaarad6dc492019-04-27 22:40:08 +0200738 // don't output anything if 'q' typed at --more-- prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 else if (!got_int
740 && (arg == NULL || vim_strchr(arg, c) != NULL)
741 && p->lnum != 0)
742 {
Bram Moolenaarad6dc492019-04-27 22:40:08 +0200743 if (name == NULL && current)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 {
Bram Moolenaarad6dc492019-04-27 22:40:08 +0200745 name = mark_line(p, 15);
746 mustfree = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 }
Bram Moolenaarad6dc492019-04-27 22:40:08 +0200748 if (!message_filtered(name))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 {
Bram Moolenaarad6dc492019-04-27 22:40:08 +0200750 if (!did_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 {
Bram Moolenaarad6dc492019-04-27 22:40:08 +0200752 // Highlight title
753 msg_puts_title(_("\nmark line col file/text"));
754 did_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 }
Bram Moolenaarad6dc492019-04-27 22:40:08 +0200756 msg_putchar('\n');
757 if (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 {
Bram Moolenaarad6dc492019-04-27 22:40:08 +0200759 sprintf((char *)IObuff, " %c %6ld %4d ", c, p->lnum, p->col);
760 msg_outtrans(IObuff);
761 if (name != NULL)
762 {
763 msg_outtrans_attr(name, current ? HL_ATTR(HLF_D) : 0);
764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 }
Bram Moolenaarad6dc492019-04-27 22:40:08 +0200766 out_flush(); // show one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 }
Bram Moolenaarad6dc492019-04-27 22:40:08 +0200768 if (mustfree)
769 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 }
771}
772
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000773/*
774 * ":delmarks[!] [marks]"
775 */
776 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100777ex_delmarks(exarg_T *eap)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000778{
779 char_u *p;
780 int from, to;
781 int i;
782 int lower;
783 int digit;
784 int n;
785
786 if (*eap->arg == NUL && eap->forceit)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100787 // clear all marks
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000788 clrallmarks(curbuf);
789 else if (eap->forceit)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000790 emsg(_(e_invalid_argument));
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000791 else if (*eap->arg == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000792 emsg(_(e_argument_required));
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000793 else
794 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100795 // clear specified marks only
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000796 for (p = eap->arg; *p != NUL; ++p)
797 {
798 lower = ASCII_ISLOWER(*p);
799 digit = VIM_ISDIGIT(*p);
800 if (lower || digit || ASCII_ISUPPER(*p))
801 {
802 if (p[1] == '-')
803 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100804 // clear range of marks
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000805 from = *p;
806 to = p[2];
807 if (!(lower ? ASCII_ISLOWER(p[2])
808 : (digit ? VIM_ISDIGIT(p[2])
809 : ASCII_ISUPPER(p[2])))
810 || to < from)
811 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000812 semsg(_(e_invalid_argument_str), p);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000813 return;
814 }
815 p += 2;
816 }
817 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100818 // clear one lower case mark
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000819 from = to = *p;
820
821 for (i = from; i <= to; ++i)
822 {
823 if (lower)
824 curbuf->b_namedm[i - 'a'].lnum = 0;
825 else
826 {
827 if (digit)
828 n = i - '0' + NMARKS;
829 else
830 n = i - 'A';
831 namedfm[n].fmark.mark.lnum = 0;
Bram Moolenaar8cd6cd82019-12-27 17:33:26 +0100832 namedfm[n].fmark.fnum = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +0100833 VIM_CLEAR(namedfm[n].fname);
Bram Moolenaar2d358992016-06-12 21:20:54 +0200834#ifdef FEAT_VIMINFO
Bram Moolenaar8cd6cd82019-12-27 17:33:26 +0100835 namedfm[n].time_set = digit ? 0 : vim_time();
Bram Moolenaar2d358992016-06-12 21:20:54 +0200836#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000837 }
838 }
839 }
840 else
841 switch (*p)
842 {
843 case '"': curbuf->b_last_cursor.lnum = 0; break;
844 case '^': curbuf->b_last_insert.lnum = 0; break;
845 case '.': curbuf->b_last_change.lnum = 0; break;
846 case '[': curbuf->b_op_start.lnum = 0; break;
847 case ']': curbuf->b_op_end.lnum = 0; break;
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000848 case '<': curbuf->b_visual.vi_start.lnum = 0; break;
849 case '>': curbuf->b_visual.vi_end.lnum = 0; break;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000850 case ' ': break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000851 default: semsg(_(e_invalid_argument_str), p);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000852 return;
853 }
854 }
855 }
856}
857
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858/*
859 * print the jumplist
860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100862ex_jumps(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863{
864 int i;
865 char_u *name;
866
Bram Moolenaar48679742018-02-13 13:33:29 +0100867 cleanup_jumplist(curwin, TRUE);
Bram Moolenaara7e18d22018-02-11 14:29:49 +0100868
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100869 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +0100870 msg_puts_title(_("\n jump line col file/text"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 for (i = 0; i < curwin->w_jumplistlen && !got_int; ++i)
872 {
873 if (curwin->w_jumplist[i].fmark.mark.lnum != 0)
874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 name = fm_getname(&curwin->w_jumplist[i].fmark, 16);
Bram Moolenaarf86db782018-10-25 13:31:37 +0200876
877 // apply :filter /pat/ or file name not available
878 if (name == NULL || message_filtered(name))
Bram Moolenaard93090f2019-01-27 15:07:39 +0100879 {
880 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 continue;
Bram Moolenaard93090f2019-01-27 15:07:39 +0100882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
884 msg_putchar('\n');
885 if (got_int)
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000886 {
887 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 break;
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 sprintf((char *)IObuff, "%c %2d %5ld %4d ",
891 i == curwin->w_jumplistidx ? '>' : ' ',
892 i > curwin->w_jumplistidx ? i - curwin->w_jumplistidx
893 : curwin->w_jumplistidx - i,
894 curwin->w_jumplist[i].fmark.mark.lnum,
895 curwin->w_jumplist[i].fmark.mark.col);
896 msg_outtrans(IObuff);
897 msg_outtrans_attr(name,
898 curwin->w_jumplist[i].fmark.fnum == curbuf->b_fnum
Bram Moolenaar8820b482017-03-16 17:23:31 +0100899 ? HL_ATTR(HLF_D) : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 vim_free(name);
901 ui_breakcheck();
902 }
903 out_flush();
904 }
905 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100906 msg_puts("\n>");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907}
908
Bram Moolenaar2d358992016-06-12 21:20:54 +0200909 void
910ex_clearjumps(exarg_T *eap UNUSED)
911{
912 free_jumplist(curwin);
913 curwin->w_jumplistlen = 0;
914 curwin->w_jumplistidx = 0;
915}
916
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917/*
918 * print the changelist
919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100921ex_changes(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922{
923 int i;
924 char_u *name;
925
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100926 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +0100927 msg_puts_title(_("\nchange line col text"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
929 for (i = 0; i < curbuf->b_changelistlen && !got_int; ++i)
930 {
931 if (curbuf->b_changelist[i].lnum != 0)
932 {
933 msg_putchar('\n');
934 if (got_int)
935 break;
936 sprintf((char *)IObuff, "%c %3d %5ld %4d ",
937 i == curwin->w_changelistidx ? '>' : ' ',
938 i > curwin->w_changelistidx ? i - curwin->w_changelistidx
939 : curwin->w_changelistidx - i,
940 (long)curbuf->b_changelist[i].lnum,
941 curbuf->b_changelist[i].col);
942 msg_outtrans(IObuff);
943 name = mark_line(&curbuf->b_changelist[i], 17);
944 if (name == NULL)
945 break;
Bram Moolenaar8820b482017-03-16 17:23:31 +0100946 msg_outtrans_attr(name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 vim_free(name);
948 ui_breakcheck();
949 }
950 out_flush();
951 }
952 if (curwin->w_changelistidx == curbuf->b_changelistlen)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100953 msg_puts("\n>");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955
956#define one_adjust(add) \
957 { \
958 lp = add; \
959 if (*lp >= line1 && *lp <= line2) \
960 { \
961 if (amount == MAXLNUM) \
962 *lp = 0; \
963 else \
964 *lp += amount; \
965 } \
966 else if (amount_after && *lp > line2) \
967 *lp += amount_after; \
968 }
969
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100970// don't delete the line, just put at first deleted line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971#define one_adjust_nodel(add) \
972 { \
973 lp = add; \
974 if (*lp >= line1 && *lp <= line2) \
975 { \
976 if (amount == MAXLNUM) \
977 *lp = line1; \
978 else \
979 *lp += amount; \
980 } \
981 else if (amount_after && *lp > line2) \
982 *lp += amount_after; \
983 }
984
985/*
986 * Adjust marks between line1 and line2 (inclusive) to move 'amount' lines.
987 * Must be called before changed_*(), appended_lines() or deleted_lines().
988 * May be called before or after changing the text.
989 * When deleting lines line1 to line2, use an 'amount' of MAXLNUM: The marks
990 * within this range are made invalid.
991 * If 'amount_after' is non-zero adjust marks after line2.
992 * Example: Delete lines 34 and 35: mark_adjust(34, 35, MAXLNUM, -2);
993 * Example: Insert two lines below 55: mark_adjust(56, MAXLNUM, 2, 0);
994 * or: mark_adjust(56, 55, MAXLNUM, 2);
995 */
996 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100997mark_adjust(
998 linenr_T line1,
999 linenr_T line2,
1000 long amount,
1001 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
Bram Moolenaar88d298a2017-03-14 21:53:58 +01001003 mark_adjust_internal(line1, line2, amount, amount_after, TRUE);
1004}
1005
1006 void
1007mark_adjust_nofold(
1008 linenr_T line1,
1009 linenr_T line2,
1010 long amount,
1011 long amount_after)
1012{
1013 mark_adjust_internal(line1, line2, amount, amount_after, FALSE);
1014}
1015
1016 static void
1017mark_adjust_internal(
1018 linenr_T line1,
1019 linenr_T line2,
1020 long amount,
1021 long amount_after,
1022 int adjust_folds UNUSED)
1023{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 int i;
1025 int fnum = curbuf->b_fnum;
1026 linenr_T *lp;
1027 win_T *win;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001028 tabpage_T *tab;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001029 static pos_T initpos = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001031 if (line2 < line1 && amount_after == 0L) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 return;
1033
Bram Moolenaare1004402020-10-24 20:49:43 +02001034 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001036 // named marks, lower case and upper case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 for (i = 0; i < NMARKS; i++)
1038 {
1039 one_adjust(&(curbuf->b_namedm[i].lnum));
1040 if (namedfm[i].fmark.fnum == fnum)
1041 one_adjust_nodel(&(namedfm[i].fmark.mark.lnum));
1042 }
1043 for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
1044 {
1045 if (namedfm[i].fmark.fnum == fnum)
1046 one_adjust_nodel(&(namedfm[i].fmark.mark.lnum));
1047 }
1048
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001049 // last Insert position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 one_adjust(&(curbuf->b_last_insert.lnum));
1051
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001052 // last change position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 one_adjust(&(curbuf->b_last_change.lnum));
1054
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001055 // last cursor position, if it was set
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001056 if (!EQUAL_POS(curbuf->b_last_cursor, initpos))
Bram Moolenaarb6a76ff2013-02-06 12:33:21 +01001057 one_adjust(&(curbuf->b_last_cursor.lnum));
1058
1059
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001060 // list of change positions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 for (i = 0; i < curbuf->b_changelistlen; ++i)
1062 one_adjust_nodel(&(curbuf->b_changelist[i].lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001064 // Visual area
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001065 one_adjust_nodel(&(curbuf->b_visual.vi_start.lnum));
1066 one_adjust_nodel(&(curbuf->b_visual.vi_end.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067
1068#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001069 // quickfix marks
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001070 qf_mark_adjust(NULL, line1, line2, amount, amount_after);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001071 // location lists
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001072 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001073 qf_mark_adjust(win, line1, line2, amount, amount_after);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074#endif
1075
1076#ifdef FEAT_SIGNS
1077 sign_mark_adjust(line1, line2, amount, amount_after);
1078#endif
1079 }
1080
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001081 // previous context mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 one_adjust(&(curwin->w_pcmark.lnum));
1083
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001084 // previous pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 one_adjust(&(curwin->w_prev_pcmark.lnum));
1086
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001087 // saved cursor for formatting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (saved_cursor.lnum != 0)
1089 one_adjust_nodel(&(saved_cursor.lnum));
1090
1091 /*
1092 * Adjust items in all windows related to the current buffer.
1093 */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001094 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaare1004402020-10-24 20:49:43 +02001096 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001097 // Marks in the jumplist. When deleting lines, this may create
1098 // duplicate marks in the jumplist, they will be removed later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 for (i = 0; i < win->w_jumplistlen; ++i)
1100 if (win->w_jumplist[i].fmark.fnum == fnum)
1101 one_adjust_nodel(&(win->w_jumplist[i].fmark.mark.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102
1103 if (win->w_buffer == curbuf)
1104 {
Bram Moolenaare1004402020-10-24 20:49:43 +02001105 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001106 // marks in the tag stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 for (i = 0; i < win->w_tagstacklen; i++)
1108 if (win->w_tagstack[i].fmark.fnum == fnum)
1109 one_adjust_nodel(&(win->w_tagstack[i].fmark.mark.lnum));
1110
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001111 // the displayed Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 if (win->w_old_cursor_lnum != 0)
1113 {
1114 one_adjust_nodel(&(win->w_old_cursor_lnum));
1115 one_adjust_nodel(&(win->w_old_visual_lnum));
1116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001118 // topline and cursor position for windows with the same buffer
1119 // other than the current window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 if (win != curwin)
1121 {
1122 if (win->w_topline >= line1 && win->w_topline <= line2)
1123 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001124 if (amount == MAXLNUM) // topline is deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
1126 if (line1 <= 1)
1127 win->w_topline = 1;
1128 else
1129 win->w_topline = line1 - 1;
1130 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001131 else // keep topline on the same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 win->w_topline += amount;
1133#ifdef FEAT_DIFF
1134 win->w_topfill = 0;
1135#endif
1136 }
1137 else if (amount_after && win->w_topline > line2)
1138 {
1139 win->w_topline += amount_after;
1140#ifdef FEAT_DIFF
1141 win->w_topfill = 0;
1142#endif
1143 }
1144 if (win->w_cursor.lnum >= line1 && win->w_cursor.lnum <= line2)
1145 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001146 if (amount == MAXLNUM) // line with cursor is deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 {
1148 if (line1 <= 1)
1149 win->w_cursor.lnum = 1;
1150 else
1151 win->w_cursor.lnum = line1 - 1;
1152 win->w_cursor.col = 0;
1153 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001154 else // keep cursor on the same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 win->w_cursor.lnum += amount;
1156 }
1157 else if (amount_after && win->w_cursor.lnum > line2)
1158 win->w_cursor.lnum += amount_after;
1159 }
1160
1161#ifdef FEAT_FOLDING
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001162 // adjust folds
Bram Moolenaar88d298a2017-03-14 21:53:58 +01001163 if (adjust_folds)
1164 foldMarkAdjust(win, line1, line2, amount, amount_after);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165#endif
1166 }
1167 }
1168
1169#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001170 // adjust diffs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 diff_mark_adjust(line1, line2, amount, amount_after);
1172#endif
1173}
1174
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001175// This code is used often, needs to be fast.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176#define col_adjust(pp) \
1177 { \
1178 posp = pp; \
1179 if (posp->lnum == lnum && posp->col >= mincol) \
1180 { \
1181 posp->lnum += lnum_amount; \
1182 if (col_amount < 0 && posp->col <= (colnr_T)-col_amount) \
1183 posp->col = 0; \
Bram Moolenaare1e714e2018-12-31 23:58:24 +01001184 else if (posp->col < spaces_removed) \
1185 posp->col = col_amount + spaces_removed; \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 else \
1187 posp->col += col_amount; \
1188 } \
1189 }
1190
1191/*
1192 * Adjust marks in line "lnum" at column "mincol" and further: add
1193 * "lnum_amount" to the line number and add "col_amount" to the column
1194 * position.
Bram Moolenaare1e714e2018-12-31 23:58:24 +01001195 * "spaces_removed" is the number of spaces that were removed, matters when the
1196 * cursor is inside them.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 */
1198 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001199mark_col_adjust(
1200 linenr_T lnum,
1201 colnr_T mincol,
1202 long lnum_amount,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01001203 long col_amount,
1204 int spaces_removed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205{
1206 int i;
1207 int fnum = curbuf->b_fnum;
1208 win_T *win;
1209 pos_T *posp;
1210
Bram Moolenaare1004402020-10-24 20:49:43 +02001211 if ((col_amount == 0L && lnum_amount == 0L)
1212 || (cmdmod.cmod_flags & CMOD_LOCKMARKS))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001213 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001215 // named marks, lower case and upper case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 for (i = 0; i < NMARKS; i++)
1217 {
1218 col_adjust(&(curbuf->b_namedm[i]));
1219 if (namedfm[i].fmark.fnum == fnum)
1220 col_adjust(&(namedfm[i].fmark.mark));
1221 }
1222 for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
1223 {
1224 if (namedfm[i].fmark.fnum == fnum)
1225 col_adjust(&(namedfm[i].fmark.mark));
1226 }
1227
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001228 // last Insert position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 col_adjust(&(curbuf->b_last_insert));
1230
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001231 // last change position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 col_adjust(&(curbuf->b_last_change));
1233
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001234 // list of change positions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 for (i = 0; i < curbuf->b_changelistlen; ++i)
1236 col_adjust(&(curbuf->b_changelist[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001238 // Visual area
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001239 col_adjust(&(curbuf->b_visual.vi_start));
1240 col_adjust(&(curbuf->b_visual.vi_end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001242 // previous context mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 col_adjust(&(curwin->w_pcmark));
1244
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001245 // previous pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 col_adjust(&(curwin->w_prev_pcmark));
1247
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001248 // saved cursor for formatting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 col_adjust(&saved_cursor);
1250
1251 /*
1252 * Adjust items in all windows related to the current buffer.
1253 */
1254 FOR_ALL_WINDOWS(win)
1255 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001256 // marks in the jumplist
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 for (i = 0; i < win->w_jumplistlen; ++i)
1258 if (win->w_jumplist[i].fmark.fnum == fnum)
1259 col_adjust(&(win->w_jumplist[i].fmark.mark));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260
1261 if (win->w_buffer == curbuf)
1262 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001263 // marks in the tag stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 for (i = 0; i < win->w_tagstacklen; i++)
1265 if (win->w_tagstack[i].fmark.fnum == fnum)
1266 col_adjust(&(win->w_tagstack[i].fmark.mark));
1267
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001268 // cursor position for other windows with the same buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 if (win != curwin)
1270 col_adjust(&win->w_cursor);
1271 }
1272 }
1273}
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275/*
1276 * When deleting lines, this may create duplicate marks in the
Bram Moolenaara7e18d22018-02-11 14:29:49 +01001277 * jumplist. They will be removed here for the specified window.
Bram Moolenaar48679742018-02-13 13:33:29 +01001278 * When "loadfiles" is TRUE first ensure entries have the "fnum" field set
1279 * (this may be a bit slow).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 */
Bram Moolenaara7e18d22018-02-11 14:29:49 +01001281 void
Bram Moolenaar48679742018-02-13 13:33:29 +01001282cleanup_jumplist(win_T *wp, int loadfiles)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
1284 int i;
1285 int from, to;
1286
Bram Moolenaar48679742018-02-13 13:33:29 +01001287 if (loadfiles)
1288 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001289 // If specified, load all the files from the jump list. This is
1290 // needed to properly clean up duplicate entries, but will take some
1291 // time.
Bram Moolenaar48679742018-02-13 13:33:29 +01001292 for (i = 0; i < wp->w_jumplistlen; ++i)
1293 {
1294 if ((wp->w_jumplist[i].fmark.fnum == 0) &&
1295 (wp->w_jumplist[i].fmark.mark.lnum != 0))
1296 fname2fnum(&wp->w_jumplist[i]);
1297 }
1298 }
1299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 to = 0;
Bram Moolenaara7e18d22018-02-11 14:29:49 +01001301 for (from = 0; from < wp->w_jumplistlen; ++from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01001303 if (wp->w_jumplistidx == from)
1304 wp->w_jumplistidx = to;
1305 for (i = from + 1; i < wp->w_jumplistlen; ++i)
1306 if (wp->w_jumplist[i].fmark.fnum
1307 == wp->w_jumplist[from].fmark.fnum
1308 && wp->w_jumplist[from].fmark.fnum != 0
1309 && wp->w_jumplist[i].fmark.mark.lnum
1310 == wp->w_jumplist[from].fmark.mark.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001312 if (i >= wp->w_jumplistlen) // no duplicate
Bram Moolenaara7e18d22018-02-11 14:29:49 +01001313 wp->w_jumplist[to++] = wp->w_jumplist[from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 else
Bram Moolenaara7e18d22018-02-11 14:29:49 +01001315 vim_free(wp->w_jumplist[from].fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 }
Bram Moolenaara7e18d22018-02-11 14:29:49 +01001317 if (wp->w_jumplistidx == wp->w_jumplistlen)
1318 wp->w_jumplistidx = to;
1319 wp->w_jumplistlen = to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320}
1321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322/*
1323 * Copy the jumplist from window "from" to window "to".
1324 */
1325 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001326copy_jumplist(win_T *from, win_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327{
1328 int i;
1329
1330 for (i = 0; i < from->w_jumplistlen; ++i)
1331 {
1332 to->w_jumplist[i] = from->w_jumplist[i];
1333 if (from->w_jumplist[i].fname != NULL)
1334 to->w_jumplist[i].fname = vim_strsave(from->w_jumplist[i].fname);
1335 }
1336 to->w_jumplistlen = from->w_jumplistlen;
1337 to->w_jumplistidx = from->w_jumplistidx;
1338}
1339
1340/*
1341 * Free items in the jumplist of window "wp".
1342 */
1343 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001344free_jumplist(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345{
1346 int i;
1347
1348 for (i = 0; i < wp->w_jumplistlen; ++i)
1349 vim_free(wp->w_jumplist[i].fname);
1350}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
1352 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001353set_last_cursor(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354{
Bram Moolenaar9db12932013-11-03 00:20:52 +01001355 if (win->w_buffer != NULL)
1356 win->w_buffer->b_last_cursor = win->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357}
1358
Bram Moolenaarea408852005-06-25 22:49:46 +00001359#if defined(EXITFREE) || defined(PROTO)
1360 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001361free_all_marks(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001362{
1363 int i;
1364
1365 for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
1366 if (namedfm[i].fmark.mark.lnum != 0)
1367 vim_free(namedfm[i].fname);
1368}
1369#endif
1370
Bram Moolenaar2d358992016-06-12 21:20:54 +02001371/*
Bram Moolenaar1e78e692019-07-22 20:18:27 +02001372 * Return a pointer to the named file marks.
Bram Moolenaar2d358992016-06-12 21:20:54 +02001373 */
Bram Moolenaar1e78e692019-07-22 20:18:27 +02001374 xfmark_T *
1375get_namedfm(void)
Bram Moolenaar2d358992016-06-12 21:20:54 +02001376{
Bram Moolenaar1e78e692019-07-22 20:18:27 +02001377 return namedfm;
Bram Moolenaar2d358992016-06-12 21:20:54 +02001378}
Bram Moolenaarcfb4b472020-05-31 15:41:57 +02001379
1380#if defined(FEAT_EVAL) || defined(PROTO)
1381/*
1382 * Add information about mark 'mname' to list 'l'
1383 */
1384 static int
1385add_mark(list_T *l, char_u *mname, pos_T *pos, int bufnr, char_u *fname)
1386{
1387 dict_T *d;
1388 list_T *lpos;
1389
1390 if (pos->lnum <= 0)
1391 return OK;
1392
1393 d = dict_alloc();
1394 if (d == NULL)
1395 return FAIL;
1396
1397 if (list_append_dict(l, d) == FAIL)
1398 {
1399 dict_unref(d);
1400 return FAIL;
1401 }
1402
1403 lpos = list_alloc();
1404 if (lpos == NULL)
1405 return FAIL;
1406
1407 list_append_number(lpos, bufnr);
1408 list_append_number(lpos, pos->lnum);
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +02001409 list_append_number(lpos, pos->col + 1);
Bram Moolenaarcfb4b472020-05-31 15:41:57 +02001410 list_append_number(lpos, pos->coladd);
1411
1412 if (dict_add_string(d, "mark", mname) == FAIL
1413 || dict_add_list(d, "pos", lpos) == FAIL
1414 || (fname != NULL && dict_add_string(d, "file", fname) == FAIL))
1415 return FAIL;
1416
1417 return OK;
1418}
1419
1420/*
1421 * Get information about marks local to a buffer.
1422 */
1423 static void
1424get_buf_local_marks(buf_T *buf, list_T *l)
1425{
1426 char_u mname[3] = "' ";
1427 int i;
1428
1429 // Marks 'a' to 'z'
1430 for (i = 0; i < NMARKS; ++i)
1431 {
1432 mname[1] = 'a' + i;
1433 add_mark(l, mname, &buf->b_namedm[i], buf->b_fnum, NULL);
1434 }
1435
1436 // Mark '' is a window local mark and not a buffer local mark
1437 add_mark(l, (char_u *)"''", &curwin->w_pcmark, curbuf->b_fnum, NULL);
1438
1439 add_mark(l, (char_u *)"'\"", &buf->b_last_cursor, buf->b_fnum, NULL);
1440 add_mark(l, (char_u *)"'[", &buf->b_op_start, buf->b_fnum, NULL);
1441 add_mark(l, (char_u *)"']", &buf->b_op_end, buf->b_fnum, NULL);
1442 add_mark(l, (char_u *)"'^", &buf->b_last_insert, buf->b_fnum, NULL);
1443 add_mark(l, (char_u *)"'.", &buf->b_last_change, buf->b_fnum, NULL);
1444 add_mark(l, (char_u *)"'<", &buf->b_visual.vi_start, buf->b_fnum, NULL);
1445 add_mark(l, (char_u *)"'>", &buf->b_visual.vi_end, buf->b_fnum, NULL);
1446}
1447
1448/*
1449 * Get information about global marks ('A' to 'Z' and '0' to '9')
1450 */
1451 static void
1452get_global_marks(list_T *l)
1453{
1454 char_u mname[3] = "' ";
1455 int i;
1456 char_u *name;
1457
1458 // Marks 'A' to 'Z' and '0' to '9'
1459 for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
1460 {
1461 if (namedfm[i].fmark.fnum != 0)
1462 name = buflist_nr2name(namedfm[i].fmark.fnum, TRUE, TRUE);
1463 else
1464 name = namedfm[i].fname;
1465 if (name != NULL)
1466 {
1467 mname[1] = i >= NMARKS ? i - NMARKS + '0' : i + 'A';
1468 add_mark(l, mname, &namedfm[i].fmark.mark,
1469 namedfm[i].fmark.fnum, name);
1470 if (namedfm[i].fmark.fnum != 0)
1471 vim_free(name);
1472 }
1473 }
1474}
1475
1476/*
1477 * getmarklist() function
1478 */
1479 void
1480f_getmarklist(typval_T *argvars, typval_T *rettv)
1481{
1482 buf_T *buf = NULL;
1483
1484 if (rettv_list_alloc(rettv) != OK)
1485 return;
1486
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001487 if (in_vim9script() && check_for_opt_buffer_arg(argvars, 0) == FAIL)
1488 return;
1489
Bram Moolenaarcfb4b472020-05-31 15:41:57 +02001490 if (argvars[0].v_type == VAR_UNKNOWN)
1491 {
1492 get_global_marks(rettv->vval.v_list);
1493 return;
1494 }
1495
1496 buf = tv_get_buf(&argvars[0], FALSE);
1497 if (buf == NULL)
1498 return;
1499
1500 get_buf_local_marks(buf, rettv->vval.v_list);
1501}
1502#endif