blob: 9b304f3d3860f293b4e8a4ed690dec7786b30136 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 */
27#define EXTRA_MARKS 10 /* marks 0-9 */
28static xfmark_T namedfm[NMARKS + EXTRA_MARKS]; /* marks with file nr */
29
30static void fname2fnum __ARGS((xfmark_T *fm));
31static void fmarks_check_one __ARGS((xfmark_T *fm, char_u *name, buf_T *buf));
32static char_u *mark_line __ARGS((pos_T *mp, int lead_len));
33static void show_one_mark __ARGS((int, char_u *, pos_T *, char_u *, int current));
34#ifdef FEAT_JUMPLIST
35static void cleanup_jumplist __ARGS((void));
36#endif
37#ifdef FEAT_VIMINFO
38static void write_one_filemark __ARGS((FILE *fp, xfmark_T *fm, int c1, int c2));
39#endif
40
41/*
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000042 * Set named mark "c" at current cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 * Returns OK on success, FAIL if bad name given.
44 */
45 int
46setmark(c)
47 int c;
48{
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000049 return setmark_pos(c, &curwin->w_cursor, curbuf->b_fnum);
50}
51
52/*
53 * Set named mark "c" to position "pos".
54 * When "c" is upper case use file "fnum".
55 * Returns OK on success, FAIL if bad name given.
56 */
57 int
58setmark_pos(c, pos, fnum)
59 int c;
60 pos_T *pos;
61 int fnum;
62{
Bram Moolenaar071d4272004-06-13 20:20:40 +000063 int i;
64
65 /* Check for a special key (may cause islower() to crash). */
66 if (c < 0)
67 return FAIL;
68
69 if (c == '\'' || c == '`')
70 {
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000071 if (pos == &curwin->w_cursor)
72 {
73 setpcmark();
74 /* keep it even when the cursor doesn't move */
75 curwin->w_prev_pcmark = curwin->w_pcmark;
76 }
77 else
78 curwin->w_pcmark = *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 return OK;
80 }
81
Bram Moolenaar08250432008-02-13 11:42:46 +000082 if (c == '"')
83 {
84 curbuf->b_last_cursor = *pos;
85 return OK;
86 }
87
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 /* Allow setting '[ and '] for an autocommand that simulates reading a
89 * file. */
90 if (c == '[')
91 {
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000092 curbuf->b_op_start = *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000093 return OK;
94 }
95 if (c == ']')
96 {
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000097 curbuf->b_op_end = *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 return OK;
99 }
100
Bram Moolenaar0306ac32012-07-06 17:51:28 +0200101#ifdef FEAT_VISUAL
102 if (c == '<')
103 {
104 curbuf->b_visual.vi_start = *pos;
105 return OK;
106 }
107 if (c == '>')
108 {
109 curbuf->b_visual.vi_end = *pos;
110 return OK;
111 }
112#endif
113
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifndef EBCDIC
115 if (c > 'z') /* some islower() and isupper() cannot handle
116 characters above 127 */
117 return FAIL;
118#endif
119 if (islower(c))
120 {
121 i = c - 'a';
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000122 curbuf->b_namedm[i] = *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 return OK;
124 }
125 if (isupper(c))
126 {
127 i = c - 'A';
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000128 namedfm[i].fmark.mark = *pos;
129 namedfm[i].fmark.fnum = fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 vim_free(namedfm[i].fname);
131 namedfm[i].fname = NULL;
132 return OK;
133 }
134 return FAIL;
135}
136
137/*
138 * Set the previous context mark to the current position and add it to the
139 * jump list.
140 */
141 void
142setpcmark()
143{
144#ifdef FEAT_JUMPLIST
145 int i;
146 xfmark_T *fm;
147#endif
148#ifdef JUMPLIST_ROTATE
149 xfmark_T tempmark;
150#endif
151
152 /* for :global the mark is set only once */
153 if (global_busy || listcmd_busy || cmdmod.keepjumps)
154 return;
155
156 curwin->w_prev_pcmark = curwin->w_pcmark;
157 curwin->w_pcmark = curwin->w_cursor;
158
159#ifdef FEAT_JUMPLIST
160# ifdef JUMPLIST_ROTATE
161 /*
162 * If last used entry is not at the top, put it at the top by rotating
163 * the stack until it is (the newer entries will be at the bottom).
164 * Keep one entry (the last used one) at the top.
165 */
166 if (curwin->w_jumplistidx < curwin->w_jumplistlen)
167 ++curwin->w_jumplistidx;
168 while (curwin->w_jumplistidx < curwin->w_jumplistlen)
169 {
170 tempmark = curwin->w_jumplist[curwin->w_jumplistlen - 1];
171 for (i = curwin->w_jumplistlen - 1; i > 0; --i)
172 curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
173 curwin->w_jumplist[0] = tempmark;
174 ++curwin->w_jumplistidx;
175 }
176# endif
177
178 /* If jumplist is full: remove oldest entry */
179 if (++curwin->w_jumplistlen > JUMPLISTSIZE)
180 {
181 curwin->w_jumplistlen = JUMPLISTSIZE;
182 vim_free(curwin->w_jumplist[0].fname);
183 for (i = 1; i < JUMPLISTSIZE; ++i)
184 curwin->w_jumplist[i - 1] = curwin->w_jumplist[i];
185 }
186 curwin->w_jumplistidx = curwin->w_jumplistlen;
187 fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];
188
189 fm->fmark.mark = curwin->w_pcmark;
190 fm->fmark.fnum = curbuf->b_fnum;
191 fm->fname = NULL;
192#endif
193}
194
195/*
196 * To change context, call setpcmark(), then move the current position to
197 * where ever, then call checkpcmark(). This ensures that the previous
198 * context will only be changed if the cursor moved to a different line.
199 * If pcmark was deleted (with "dG") the previous mark is restored.
200 */
201 void
202checkpcmark()
203{
204 if (curwin->w_prev_pcmark.lnum != 0
205 && (equalpos(curwin->w_pcmark, curwin->w_cursor)
206 || curwin->w_pcmark.lnum == 0))
207 {
208 curwin->w_pcmark = curwin->w_prev_pcmark;
209 curwin->w_prev_pcmark.lnum = 0; /* Show it has been checked */
210 }
211}
212
213#if defined(FEAT_JUMPLIST) || defined(PROTO)
214/*
215 * move "count" positions in the jump list (count may be negative)
216 */
217 pos_T *
218movemark(count)
219 int count;
220{
221 pos_T *pos;
222 xfmark_T *jmp;
223
224 cleanup_jumplist();
225
226 if (curwin->w_jumplistlen == 0) /* nothing to jump to */
227 return (pos_T *)NULL;
228
229 for (;;)
230 {
231 if (curwin->w_jumplistidx + count < 0
232 || curwin->w_jumplistidx + count >= curwin->w_jumplistlen)
233 return (pos_T *)NULL;
234
235 /*
236 * if first CTRL-O or CTRL-I command after a jump, add cursor position
Bram Moolenaarf711faf2007-05-10 16:48:19 +0000237 * to list. Careful: If there are duplicates (CTRL-O immediately after
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 * starting Vim on a file), another entry may have been removed.
239 */
240 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
241 {
242 setpcmark();
243 --curwin->w_jumplistidx; /* skip the new entry */
244 if (curwin->w_jumplistidx + count < 0)
245 return (pos_T *)NULL;
246 }
247
248 curwin->w_jumplistidx += count;
249
250 jmp = curwin->w_jumplist + curwin->w_jumplistidx;
251 if (jmp->fmark.fnum == 0)
252 fname2fnum(jmp);
253 if (jmp->fmark.fnum != curbuf->b_fnum)
254 {
255 /* jump to other file */
256 if (buflist_findnr(jmp->fmark.fnum) == NULL)
257 { /* Skip this one .. */
258 count += count < 0 ? -1 : 1;
259 continue;
260 }
261 if (buflist_getfile(jmp->fmark.fnum, jmp->fmark.mark.lnum,
262 0, FALSE) == FAIL)
263 return (pos_T *)NULL;
264 /* Set lnum again, autocommands my have changed it */
265 curwin->w_cursor = jmp->fmark.mark;
266 pos = (pos_T *)-1;
267 }
268 else
269 pos = &(jmp->fmark.mark);
270 return pos;
271 }
272}
273
274/*
275 * Move "count" positions in the changelist (count may be negative).
276 */
277 pos_T *
278movechangelist(count)
279 int count;
280{
281 int n;
282
283 if (curbuf->b_changelistlen == 0) /* nothing to jump to */
284 return (pos_T *)NULL;
285
286 n = curwin->w_changelistidx;
287 if (n + count < 0)
288 {
289 if (n == 0)
290 return (pos_T *)NULL;
291 n = 0;
292 }
293 else if (n + count >= curbuf->b_changelistlen)
294 {
295 if (n == curbuf->b_changelistlen - 1)
296 return (pos_T *)NULL;
297 n = curbuf->b_changelistlen - 1;
298 }
299 else
300 n += count;
301 curwin->w_changelistidx = n;
302 return curbuf->b_changelist + n;
303}
304#endif
305
306/*
307 * Find mark "c".
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000308 * If "changefile" is TRUE it's allowed to edit another file for '0, 'A, etc.
309 * If "fnum" is not NULL store the fnum there for '0, 'A etc., don't edit
310 * another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 * Returns:
312 * - pointer to pos_T if found. lnum is 0 when mark not set, -1 when mark is
313 * in another file which can't be gotten. (caller needs to check lnum!)
314 * - NULL if there is no mark called 'c'.
315 * - -1 if mark is in other file and jumped there (only if changefile is TRUE)
316 */
317 pos_T *
318getmark(c, changefile)
319 int c;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000320 int changefile;
321{
322 return getmark_fnum(c, changefile, NULL);
323}
324
325 pos_T *
326getmark_fnum(c, changefile, fnum)
327 int c;
328 int changefile;
329 int *fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330{
331 pos_T *posp;
332#ifdef FEAT_VISUAL
333 pos_T *startp, *endp;
334#endif
335 static pos_T pos_copy;
336
337 posp = NULL;
338
339 /* Check for special key, can't be a mark name and might cause islower()
340 * to crash. */
341 if (c < 0)
342 return posp;
343#ifndef EBCDIC
344 if (c > '~') /* check for islower()/isupper() */
345 ;
346 else
347#endif
348 if (c == '\'' || c == '`') /* previous context mark */
349 {
350 pos_copy = curwin->w_pcmark; /* need to make a copy because */
351 posp = &pos_copy; /* w_pcmark may be changed soon */
352 }
353 else if (c == '"') /* to pos when leaving buffer */
354 posp = &(curbuf->b_last_cursor);
355 else if (c == '^') /* to where Insert mode stopped */
356 posp = &(curbuf->b_last_insert);
357 else if (c == '.') /* to where last change was made */
358 posp = &(curbuf->b_last_change);
359 else if (c == '[') /* to start of previous operator */
360 posp = &(curbuf->b_op_start);
361 else if (c == ']') /* to end of previous operator */
362 posp = &(curbuf->b_op_end);
363 else if (c == '{' || c == '}') /* to previous/next paragraph */
364 {
365 pos_T pos;
366 oparg_T oa;
367 int slcb = listcmd_busy;
368
369 pos = curwin->w_cursor;
370 listcmd_busy = TRUE; /* avoid that '' is changed */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000371 if (findpar(&oa.inclusive,
372 c == '}' ? FORWARD : BACKWARD, 1L, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 {
374 pos_copy = curwin->w_cursor;
375 posp = &pos_copy;
376 }
377 curwin->w_cursor = pos;
378 listcmd_busy = slcb;
379 }
380 else if (c == '(' || c == ')') /* to previous/next sentence */
381 {
382 pos_T pos;
383 int slcb = listcmd_busy;
384
385 pos = curwin->w_cursor;
386 listcmd_busy = TRUE; /* avoid that '' is changed */
387 if (findsent(c == ')' ? FORWARD : BACKWARD, 1L))
388 {
389 pos_copy = curwin->w_cursor;
390 posp = &pos_copy;
391 }
392 curwin->w_cursor = pos;
393 listcmd_busy = slcb;
394 }
395#ifdef FEAT_VISUAL
396 else if (c == '<' || c == '>') /* start/end of visual area */
397 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000398 startp = &curbuf->b_visual.vi_start;
399 endp = &curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 if ((c == '<') == lt(*startp, *endp))
401 posp = startp;
402 else
403 posp = endp;
404 /*
405 * For Visual line mode, set mark at begin or end of line
406 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000407 if (curbuf->b_visual.vi_mode == 'V')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 {
409 pos_copy = *posp;
410 posp = &pos_copy;
411 if (c == '<')
412 pos_copy.col = 0;
413 else
414 pos_copy.col = MAXCOL;
415#ifdef FEAT_VIRTUALEDIT
416 pos_copy.coladd = 0;
417#endif
418 }
419 }
420#endif
421 else if (ASCII_ISLOWER(c)) /* normal named mark */
422 {
423 posp = &(curbuf->b_namedm[c - 'a']);
424 }
425 else if (ASCII_ISUPPER(c) || VIM_ISDIGIT(c)) /* named file mark */
426 {
427 if (VIM_ISDIGIT(c))
428 c = c - '0' + NMARKS;
429 else
430 c -= 'A';
431 posp = &(namedfm[c].fmark.mark);
432
433 if (namedfm[c].fmark.fnum == 0)
434 fname2fnum(&namedfm[c]);
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000435
436 if (fnum != NULL)
437 *fnum = namedfm[c].fmark.fnum;
438 else if (namedfm[c].fmark.fnum != curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 {
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000440 /* mark is in another file */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 posp = &pos_copy;
442
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 if (namedfm[c].fmark.mark.lnum != 0
444 && changefile && namedfm[c].fmark.fnum)
445 {
446 if (buflist_getfile(namedfm[c].fmark.fnum,
447 (linenr_T)1, GETF_SETMARK, FALSE) == OK)
448 {
449 /* Set the lnum now, autocommands could have changed it */
450 curwin->w_cursor = namedfm[c].fmark.mark;
451 return (pos_T *)-1;
452 }
453 pos_copy.lnum = -1; /* can't get file */
454 }
455 else
456 pos_copy.lnum = 0; /* mark exists, but is not valid in
457 current buffer */
458 }
459 }
460
461 return posp;
462}
463
464/*
465 * Search for the next named mark in the current file.
466 *
467 * Returns pointer to pos_T of the next mark or NULL if no mark is found.
468 */
469 pos_T *
470getnextmark(startpos, dir, begin_line)
471 pos_T *startpos; /* where to start */
472 int dir; /* direction for search */
473 int begin_line;
474{
475 int i;
476 pos_T *result = NULL;
477 pos_T pos;
478
479 pos = *startpos;
480
481 /* When searching backward and leaving the cursor on the first non-blank,
482 * position must be in a previous line.
483 * When searching forward and leaving the cursor on the first non-blank,
484 * position must be in a next line. */
485 if (dir == BACKWARD && begin_line)
486 pos.col = 0;
487 else if (dir == FORWARD && begin_line)
488 pos.col = MAXCOL;
489
490 for (i = 0; i < NMARKS; i++)
491 {
492 if (curbuf->b_namedm[i].lnum > 0)
493 {
494 if (dir == FORWARD)
495 {
496 if ((result == NULL || lt(curbuf->b_namedm[i], *result))
497 && lt(pos, curbuf->b_namedm[i]))
498 result = &curbuf->b_namedm[i];
499 }
500 else
501 {
502 if ((result == NULL || lt(*result, curbuf->b_namedm[i]))
503 && lt(curbuf->b_namedm[i], pos))
504 result = &curbuf->b_namedm[i];
505 }
506 }
507 }
508
509 return result;
510}
511
512/*
513 * For an xtended filemark: set the fnum from the fname.
514 * This is used for marks obtained from the .viminfo file. It's postponed
515 * until the mark is used to avoid a long startup delay.
516 */
517 static void
518fname2fnum(fm)
519 xfmark_T *fm;
520{
521 char_u *p;
522
523 if (fm->fname != NULL)
524 {
525 /*
526 * First expand "~/" in the file name to the home directory.
Bram Moolenaar525ad4d2008-01-03 19:22:13 +0000527 * Don't expand the whole name, it may contain other '~' chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 */
Bram Moolenaar525ad4d2008-01-03 19:22:13 +0000529 if (fm->fname[0] == '~' && (fm->fname[1] == '/'
530#ifdef BACKSLASH_IN_FILENAME
531 || fm->fname[1] == '\\'
532#endif
533 ))
534 {
535 int len;
536
537 expand_env((char_u *)"~/", NameBuff, MAXPATHL);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +0000538 len = (int)STRLEN(NameBuff);
Bram Moolenaar525ad4d2008-01-03 19:22:13 +0000539 vim_strncpy(NameBuff + len, fm->fname + 2, MAXPATHL - len - 1);
540 }
541 else
542 vim_strncpy(NameBuff, fm->fname, MAXPATHL - 1);
543
544 /* Try to shorten the file name. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 mch_dirname(IObuff, IOSIZE);
546 p = shorten_fname(NameBuff, IObuff);
547
548 /* buflist_new() will call fmarks_check_names() */
549 (void)buflist_new(NameBuff, p, (linenr_T)1, 0);
550 }
551}
552
553/*
554 * Check all file marks for a name that matches the file name in buf.
555 * May replace the name with an fnum.
556 * Used for marks that come from the .viminfo file.
557 */
558 void
559fmarks_check_names(buf)
560 buf_T *buf;
561{
562 char_u *name;
563 int i;
564#ifdef FEAT_JUMPLIST
565 win_T *wp;
566#endif
567
568 if (buf->b_ffname == NULL)
569 return;
570
571 name = home_replace_save(buf, buf->b_ffname);
572 if (name == NULL)
573 return;
574
575 for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
576 fmarks_check_one(&namedfm[i], name, buf);
577
578#ifdef FEAT_JUMPLIST
579 FOR_ALL_WINDOWS(wp)
580 {
581 for (i = 0; i < wp->w_jumplistlen; ++i)
582 fmarks_check_one(&wp->w_jumplist[i], name, buf);
583 }
584#endif
585
586 vim_free(name);
587}
588
589 static void
590fmarks_check_one(fm, name, buf)
591 xfmark_T *fm;
592 char_u *name;
593 buf_T *buf;
594{
595 if (fm->fmark.fnum == 0
596 && fm->fname != NULL
597 && fnamecmp(name, fm->fname) == 0)
598 {
599 fm->fmark.fnum = buf->b_fnum;
600 vim_free(fm->fname);
601 fm->fname = NULL;
602 }
603}
604
605/*
606 * Check a if a position from a mark is valid.
607 * Give and error message and return FAIL if not.
608 */
609 int
610check_mark(pos)
611 pos_T *pos;
612{
613 if (pos == NULL)
614 {
615 EMSG(_(e_umark));
616 return FAIL;
617 }
618 if (pos->lnum <= 0)
619 {
620 /* lnum is negative if mark is in another file can can't get that
621 * file, error message already give then. */
622 if (pos->lnum == 0)
623 EMSG(_(e_marknotset));
624 return FAIL;
625 }
626 if (pos->lnum > curbuf->b_ml.ml_line_count)
627 {
628 EMSG(_(e_markinval));
629 return FAIL;
630 }
631 return OK;
632}
633
634/*
635 * clrallmarks() - clear all marks in the buffer 'buf'
636 *
637 * Used mainly when trashing the entire buffer during ":e" type commands
638 */
639 void
640clrallmarks(buf)
641 buf_T *buf;
642{
643 static int i = -1;
644
645 if (i == -1) /* first call ever: initialize */
646 for (i = 0; i < NMARKS + 1; i++)
647 {
648 namedfm[i].fmark.mark.lnum = 0;
649 namedfm[i].fname = NULL;
650 }
651
652 for (i = 0; i < NMARKS; i++)
653 buf->b_namedm[i].lnum = 0;
654 buf->b_op_start.lnum = 0; /* start/end op mark cleared */
655 buf->b_op_end.lnum = 0;
656 buf->b_last_cursor.lnum = 1; /* '" mark cleared */
657 buf->b_last_cursor.col = 0;
658#ifdef FEAT_VIRTUALEDIT
659 buf->b_last_cursor.coladd = 0;
660#endif
661 buf->b_last_insert.lnum = 0; /* '^ mark cleared */
662 buf->b_last_change.lnum = 0; /* '. mark cleared */
663#ifdef FEAT_JUMPLIST
664 buf->b_changelistlen = 0;
665#endif
666}
667
668/*
669 * Get name of file from a filemark.
670 * When it's in the current buffer, return the text at the mark.
671 * Returns an allocated string.
672 */
673 char_u *
674fm_getname(fmark, lead_len)
675 fmark_T *fmark;
676 int lead_len;
677{
678 if (fmark->fnum == curbuf->b_fnum) /* current buffer */
679 return mark_line(&(fmark->mark), lead_len);
680 return buflist_nr2name(fmark->fnum, FALSE, TRUE);
681}
682
683/*
684 * Return the line at mark "mp". Truncate to fit in window.
685 * The returned string has been allocated.
686 */
687 static char_u *
688mark_line(mp, lead_len)
689 pos_T *mp;
690 int lead_len;
691{
692 char_u *s, *p;
693 int len;
694
695 if (mp->lnum == 0 || mp->lnum > curbuf->b_ml.ml_line_count)
696 return vim_strsave((char_u *)"-invalid-");
697 s = vim_strnsave(skipwhite(ml_get(mp->lnum)), (int)Columns);
698 if (s == NULL)
699 return NULL;
700 /* Truncate the line to fit it in the window */
701 len = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000702 for (p = s; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 len += ptr2cells(p);
705 if (len >= Columns - lead_len)
706 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 }
708 *p = NUL;
709 return s;
710}
711
712/*
713 * print the marks
714 */
715 void
716do_marks(eap)
717 exarg_T *eap;
718{
719 char_u *arg = eap->arg;
720 int i;
721 char_u *name;
722
723 if (arg != NULL && *arg == NUL)
724 arg = NULL;
725
726 show_one_mark('\'', arg, &curwin->w_pcmark, NULL, TRUE);
727 for (i = 0; i < NMARKS; ++i)
728 show_one_mark(i + 'a', arg, &curbuf->b_namedm[i], NULL, TRUE);
729 for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
730 {
731 if (namedfm[i].fmark.fnum != 0)
732 name = fm_getname(&namedfm[i].fmark, 15);
733 else
734 name = namedfm[i].fname;
735 if (name != NULL)
736 {
737 show_one_mark(i >= NMARKS ? i - NMARKS + '0' : i + 'A',
738 arg, &namedfm[i].fmark.mark, name,
739 namedfm[i].fmark.fnum == curbuf->b_fnum);
740 if (namedfm[i].fmark.fnum != 0)
741 vim_free(name);
742 }
743 }
744 show_one_mark('"', arg, &curbuf->b_last_cursor, NULL, TRUE);
745 show_one_mark('[', arg, &curbuf->b_op_start, NULL, TRUE);
746 show_one_mark(']', arg, &curbuf->b_op_end, NULL, TRUE);
747 show_one_mark('^', arg, &curbuf->b_last_insert, NULL, TRUE);
748 show_one_mark('.', arg, &curbuf->b_last_change, NULL, TRUE);
749#ifdef FEAT_VISUAL
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000750 show_one_mark('<', arg, &curbuf->b_visual.vi_start, NULL, TRUE);
751 show_one_mark('>', arg, &curbuf->b_visual.vi_end, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752#endif
753 show_one_mark(-1, arg, NULL, NULL, FALSE);
754}
755
756 static void
757show_one_mark(c, arg, p, name, current)
758 int c;
759 char_u *arg;
760 pos_T *p;
761 char_u *name;
762 int current; /* in current file */
763{
764 static int did_title = FALSE;
765 int mustfree = FALSE;
766
767 if (c == -1) /* finish up */
768 {
769 if (did_title)
770 did_title = FALSE;
771 else
772 {
773 if (arg == NULL)
774 MSG(_("No marks set"));
775 else
776 EMSG2(_("E283: No marks matching \"%s\""), arg);
777 }
778 }
779 /* don't output anything if 'q' typed at --more-- prompt */
780 else if (!got_int
781 && (arg == NULL || vim_strchr(arg, c) != NULL)
782 && p->lnum != 0)
783 {
784 if (!did_title)
785 {
786 /* Highlight title */
787 MSG_PUTS_TITLE(_("\nmark line col file/text"));
788 did_title = TRUE;
789 }
790 msg_putchar('\n');
791 if (!got_int)
792 {
793 sprintf((char *)IObuff, " %c %6ld %4d ", c, p->lnum, p->col);
794 msg_outtrans(IObuff);
795 if (name == NULL && current)
796 {
797 name = mark_line(p, 15);
798 mustfree = TRUE;
799 }
800 if (name != NULL)
801 {
802 msg_outtrans_attr(name, current ? hl_attr(HLF_D) : 0);
803 if (mustfree)
804 vim_free(name);
805 }
806 }
807 out_flush(); /* show one line at a time */
808 }
809}
810
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000811/*
812 * ":delmarks[!] [marks]"
813 */
814 void
815ex_delmarks(eap)
816 exarg_T *eap;
817{
818 char_u *p;
819 int from, to;
820 int i;
821 int lower;
822 int digit;
823 int n;
824
825 if (*eap->arg == NUL && eap->forceit)
826 /* clear all marks */
827 clrallmarks(curbuf);
828 else if (eap->forceit)
829 EMSG(_(e_invarg));
830 else if (*eap->arg == NUL)
831 EMSG(_(e_argreq));
832 else
833 {
834 /* clear specified marks only */
835 for (p = eap->arg; *p != NUL; ++p)
836 {
837 lower = ASCII_ISLOWER(*p);
838 digit = VIM_ISDIGIT(*p);
839 if (lower || digit || ASCII_ISUPPER(*p))
840 {
841 if (p[1] == '-')
842 {
843 /* clear range of marks */
844 from = *p;
845 to = p[2];
846 if (!(lower ? ASCII_ISLOWER(p[2])
847 : (digit ? VIM_ISDIGIT(p[2])
848 : ASCII_ISUPPER(p[2])))
849 || to < from)
850 {
851 EMSG2(_(e_invarg2), p);
852 return;
853 }
854 p += 2;
855 }
856 else
857 /* clear one lower case mark */
858 from = to = *p;
859
860 for (i = from; i <= to; ++i)
861 {
862 if (lower)
863 curbuf->b_namedm[i - 'a'].lnum = 0;
864 else
865 {
866 if (digit)
867 n = i - '0' + NMARKS;
868 else
869 n = i - 'A';
870 namedfm[n].fmark.mark.lnum = 0;
871 vim_free(namedfm[n].fname);
872 namedfm[n].fname = NULL;
873 }
874 }
875 }
876 else
877 switch (*p)
878 {
879 case '"': curbuf->b_last_cursor.lnum = 0; break;
880 case '^': curbuf->b_last_insert.lnum = 0; break;
881 case '.': curbuf->b_last_change.lnum = 0; break;
882 case '[': curbuf->b_op_start.lnum = 0; break;
883 case ']': curbuf->b_op_end.lnum = 0; break;
884#ifdef FEAT_VISUAL
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000885 case '<': curbuf->b_visual.vi_start.lnum = 0; break;
886 case '>': curbuf->b_visual.vi_end.lnum = 0; break;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000887#endif
888 case ' ': break;
889 default: EMSG2(_(e_invarg2), p);
890 return;
891 }
892 }
893 }
894}
895
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896#if defined(FEAT_JUMPLIST) || defined(PROTO)
897/*
898 * print the jumplist
899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 void
901ex_jumps(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000902 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903{
904 int i;
905 char_u *name;
906
907 cleanup_jumplist();
908 /* Highlight title */
909 MSG_PUTS_TITLE(_("\n jump line col file/text"));
910 for (i = 0; i < curwin->w_jumplistlen && !got_int; ++i)
911 {
912 if (curwin->w_jumplist[i].fmark.mark.lnum != 0)
913 {
914 if (curwin->w_jumplist[i].fmark.fnum == 0)
915 fname2fnum(&curwin->w_jumplist[i]);
916 name = fm_getname(&curwin->w_jumplist[i].fmark, 16);
917 if (name == NULL) /* file name not available */
918 continue;
919
920 msg_putchar('\n');
921 if (got_int)
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000922 {
923 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 break;
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 sprintf((char *)IObuff, "%c %2d %5ld %4d ",
927 i == curwin->w_jumplistidx ? '>' : ' ',
928 i > curwin->w_jumplistidx ? i - curwin->w_jumplistidx
929 : curwin->w_jumplistidx - i,
930 curwin->w_jumplist[i].fmark.mark.lnum,
931 curwin->w_jumplist[i].fmark.mark.col);
932 msg_outtrans(IObuff);
933 msg_outtrans_attr(name,
934 curwin->w_jumplist[i].fmark.fnum == curbuf->b_fnum
935 ? hl_attr(HLF_D) : 0);
936 vim_free(name);
937 ui_breakcheck();
938 }
939 out_flush();
940 }
941 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
942 MSG_PUTS("\n>");
943}
944
945/*
946 * print the changelist
947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 void
949ex_changes(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000950 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951{
952 int i;
953 char_u *name;
954
955 /* Highlight title */
956 MSG_PUTS_TITLE(_("\nchange line col text"));
957
958 for (i = 0; i < curbuf->b_changelistlen && !got_int; ++i)
959 {
960 if (curbuf->b_changelist[i].lnum != 0)
961 {
962 msg_putchar('\n');
963 if (got_int)
964 break;
965 sprintf((char *)IObuff, "%c %3d %5ld %4d ",
966 i == curwin->w_changelistidx ? '>' : ' ',
967 i > curwin->w_changelistidx ? i - curwin->w_changelistidx
968 : curwin->w_changelistidx - i,
969 (long)curbuf->b_changelist[i].lnum,
970 curbuf->b_changelist[i].col);
971 msg_outtrans(IObuff);
972 name = mark_line(&curbuf->b_changelist[i], 17);
973 if (name == NULL)
974 break;
975 msg_outtrans_attr(name, hl_attr(HLF_D));
976 vim_free(name);
977 ui_breakcheck();
978 }
979 out_flush();
980 }
981 if (curwin->w_changelistidx == curbuf->b_changelistlen)
982 MSG_PUTS("\n>");
983}
984#endif
985
986#define one_adjust(add) \
987 { \
988 lp = add; \
989 if (*lp >= line1 && *lp <= line2) \
990 { \
991 if (amount == MAXLNUM) \
992 *lp = 0; \
993 else \
994 *lp += amount; \
995 } \
996 else if (amount_after && *lp > line2) \
997 *lp += amount_after; \
998 }
999
1000/* don't delete the line, just put at first deleted line */
1001#define one_adjust_nodel(add) \
1002 { \
1003 lp = add; \
1004 if (*lp >= line1 && *lp <= line2) \
1005 { \
1006 if (amount == MAXLNUM) \
1007 *lp = line1; \
1008 else \
1009 *lp += amount; \
1010 } \
1011 else if (amount_after && *lp > line2) \
1012 *lp += amount_after; \
1013 }
1014
1015/*
1016 * Adjust marks between line1 and line2 (inclusive) to move 'amount' lines.
1017 * Must be called before changed_*(), appended_lines() or deleted_lines().
1018 * May be called before or after changing the text.
1019 * When deleting lines line1 to line2, use an 'amount' of MAXLNUM: The marks
1020 * within this range are made invalid.
1021 * If 'amount_after' is non-zero adjust marks after line2.
1022 * Example: Delete lines 34 and 35: mark_adjust(34, 35, MAXLNUM, -2);
1023 * Example: Insert two lines below 55: mark_adjust(56, MAXLNUM, 2, 0);
1024 * or: mark_adjust(56, 55, MAXLNUM, 2);
1025 */
1026 void
1027mark_adjust(line1, line2, amount, amount_after)
1028 linenr_T line1;
1029 linenr_T line2;
1030 long amount;
1031 long amount_after;
1032{
1033 int i;
1034 int fnum = curbuf->b_fnum;
1035 linenr_T *lp;
1036 win_T *win;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001037#ifdef FEAT_WINDOWS
1038 tabpage_T *tab;
1039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
1041 if (line2 < line1 && amount_after == 0L) /* nothing to do */
1042 return;
1043
1044 if (!cmdmod.lockmarks)
1045 {
1046 /* named marks, lower case and upper case */
1047 for (i = 0; i < NMARKS; i++)
1048 {
1049 one_adjust(&(curbuf->b_namedm[i].lnum));
1050 if (namedfm[i].fmark.fnum == fnum)
1051 one_adjust_nodel(&(namedfm[i].fmark.mark.lnum));
1052 }
1053 for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
1054 {
1055 if (namedfm[i].fmark.fnum == fnum)
1056 one_adjust_nodel(&(namedfm[i].fmark.mark.lnum));
1057 }
1058
1059 /* last Insert position */
1060 one_adjust(&(curbuf->b_last_insert.lnum));
1061
1062 /* last change position */
1063 one_adjust(&(curbuf->b_last_change.lnum));
1064
1065#ifdef FEAT_JUMPLIST
1066 /* list of change positions */
1067 for (i = 0; i < curbuf->b_changelistlen; ++i)
1068 one_adjust_nodel(&(curbuf->b_changelist[i].lnum));
1069#endif
1070
1071#ifdef FEAT_VISUAL
1072 /* Visual area */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001073 one_adjust_nodel(&(curbuf->b_visual.vi_start.lnum));
1074 one_adjust_nodel(&(curbuf->b_visual.vi_end.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075#endif
1076
1077#ifdef FEAT_QUICKFIX
1078 /* quickfix marks */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001079 qf_mark_adjust(NULL, line1, line2, amount, amount_after);
1080 /* location lists */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001081 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001082 qf_mark_adjust(win, line1, line2, amount, amount_after);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083#endif
1084
1085#ifdef FEAT_SIGNS
1086 sign_mark_adjust(line1, line2, amount, amount_after);
1087#endif
1088 }
1089
1090 /* previous context mark */
1091 one_adjust(&(curwin->w_pcmark.lnum));
1092
1093 /* previous pcmark */
1094 one_adjust(&(curwin->w_prev_pcmark.lnum));
1095
1096 /* saved cursor for formatting */
1097 if (saved_cursor.lnum != 0)
1098 one_adjust_nodel(&(saved_cursor.lnum));
1099
1100 /*
1101 * Adjust items in all windows related to the current buffer.
1102 */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001103 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 {
1105#ifdef FEAT_JUMPLIST
1106 if (!cmdmod.lockmarks)
1107 /* Marks in the jumplist. When deleting lines, this may create
1108 * duplicate marks in the jumplist, they will be removed later. */
1109 for (i = 0; i < win->w_jumplistlen; ++i)
1110 if (win->w_jumplist[i].fmark.fnum == fnum)
1111 one_adjust_nodel(&(win->w_jumplist[i].fmark.mark.lnum));
1112#endif
1113
1114 if (win->w_buffer == curbuf)
1115 {
1116 if (!cmdmod.lockmarks)
1117 /* marks in the tag stack */
1118 for (i = 0; i < win->w_tagstacklen; i++)
1119 if (win->w_tagstack[i].fmark.fnum == fnum)
1120 one_adjust_nodel(&(win->w_tagstack[i].fmark.mark.lnum));
1121
1122#ifdef FEAT_VISUAL
1123 /* the displayed Visual area */
1124 if (win->w_old_cursor_lnum != 0)
1125 {
1126 one_adjust_nodel(&(win->w_old_cursor_lnum));
1127 one_adjust_nodel(&(win->w_old_visual_lnum));
1128 }
1129#endif
1130
1131 /* topline and cursor position for windows with the same buffer
1132 * other than the current window */
1133 if (win != curwin)
1134 {
1135 if (win->w_topline >= line1 && win->w_topline <= line2)
1136 {
1137 if (amount == MAXLNUM) /* topline is deleted */
1138 {
1139 if (line1 <= 1)
1140 win->w_topline = 1;
1141 else
1142 win->w_topline = line1 - 1;
1143 }
1144 else /* keep topline on the same line */
1145 win->w_topline += amount;
1146#ifdef FEAT_DIFF
1147 win->w_topfill = 0;
1148#endif
1149 }
1150 else if (amount_after && win->w_topline > line2)
1151 {
1152 win->w_topline += amount_after;
1153#ifdef FEAT_DIFF
1154 win->w_topfill = 0;
1155#endif
1156 }
1157 if (win->w_cursor.lnum >= line1 && win->w_cursor.lnum <= line2)
1158 {
1159 if (amount == MAXLNUM) /* line with cursor is deleted */
1160 {
1161 if (line1 <= 1)
1162 win->w_cursor.lnum = 1;
1163 else
1164 win->w_cursor.lnum = line1 - 1;
1165 win->w_cursor.col = 0;
1166 }
1167 else /* keep cursor on the same line */
1168 win->w_cursor.lnum += amount;
1169 }
1170 else if (amount_after && win->w_cursor.lnum > line2)
1171 win->w_cursor.lnum += amount_after;
1172 }
1173
1174#ifdef FEAT_FOLDING
1175 /* adjust folds */
1176 foldMarkAdjust(win, line1, line2, amount, amount_after);
1177#endif
1178 }
1179 }
1180
1181#ifdef FEAT_DIFF
1182 /* adjust diffs */
1183 diff_mark_adjust(line1, line2, amount, amount_after);
1184#endif
1185}
1186
1187/* This code is used often, needs to be fast. */
1188#define col_adjust(pp) \
1189 { \
1190 posp = pp; \
1191 if (posp->lnum == lnum && posp->col >= mincol) \
1192 { \
1193 posp->lnum += lnum_amount; \
1194 if (col_amount < 0 && posp->col <= (colnr_T)-col_amount) \
1195 posp->col = 0; \
1196 else \
1197 posp->col += col_amount; \
1198 } \
1199 }
1200
1201/*
1202 * Adjust marks in line "lnum" at column "mincol" and further: add
1203 * "lnum_amount" to the line number and add "col_amount" to the column
1204 * position.
1205 */
1206 void
1207mark_col_adjust(lnum, mincol, lnum_amount, col_amount)
1208 linenr_T lnum;
1209 colnr_T mincol;
1210 long lnum_amount;
1211 long col_amount;
1212{
1213 int i;
1214 int fnum = curbuf->b_fnum;
1215 win_T *win;
1216 pos_T *posp;
1217
1218 if ((col_amount == 0L && lnum_amount == 0L) || cmdmod.lockmarks)
1219 return; /* nothing to do */
1220
1221 /* named marks, lower case and upper case */
1222 for (i = 0; i < NMARKS; i++)
1223 {
1224 col_adjust(&(curbuf->b_namedm[i]));
1225 if (namedfm[i].fmark.fnum == fnum)
1226 col_adjust(&(namedfm[i].fmark.mark));
1227 }
1228 for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
1229 {
1230 if (namedfm[i].fmark.fnum == fnum)
1231 col_adjust(&(namedfm[i].fmark.mark));
1232 }
1233
1234 /* last Insert position */
1235 col_adjust(&(curbuf->b_last_insert));
1236
1237 /* last change position */
1238 col_adjust(&(curbuf->b_last_change));
1239
1240#ifdef FEAT_JUMPLIST
1241 /* list of change positions */
1242 for (i = 0; i < curbuf->b_changelistlen; ++i)
1243 col_adjust(&(curbuf->b_changelist[i]));
1244#endif
1245
1246#ifdef FEAT_VISUAL
1247 /* Visual area */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001248 col_adjust(&(curbuf->b_visual.vi_start));
1249 col_adjust(&(curbuf->b_visual.vi_end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250#endif
1251
1252 /* previous context mark */
1253 col_adjust(&(curwin->w_pcmark));
1254
1255 /* previous pcmark */
1256 col_adjust(&(curwin->w_prev_pcmark));
1257
1258 /* saved cursor for formatting */
1259 col_adjust(&saved_cursor);
1260
1261 /*
1262 * Adjust items in all windows related to the current buffer.
1263 */
1264 FOR_ALL_WINDOWS(win)
1265 {
1266#ifdef FEAT_JUMPLIST
1267 /* marks in the jumplist */
1268 for (i = 0; i < win->w_jumplistlen; ++i)
1269 if (win->w_jumplist[i].fmark.fnum == fnum)
1270 col_adjust(&(win->w_jumplist[i].fmark.mark));
1271#endif
1272
1273 if (win->w_buffer == curbuf)
1274 {
1275 /* marks in the tag stack */
1276 for (i = 0; i < win->w_tagstacklen; i++)
1277 if (win->w_tagstack[i].fmark.fnum == fnum)
1278 col_adjust(&(win->w_tagstack[i].fmark.mark));
1279
1280 /* cursor position for other windows with the same buffer */
1281 if (win != curwin)
1282 col_adjust(&win->w_cursor);
1283 }
1284 }
1285}
1286
1287#ifdef FEAT_JUMPLIST
1288/*
1289 * When deleting lines, this may create duplicate marks in the
1290 * jumplist. They will be removed here for the current window.
1291 */
1292 static void
1293cleanup_jumplist()
1294{
1295 int i;
1296 int from, to;
1297
1298 to = 0;
1299 for (from = 0; from < curwin->w_jumplistlen; ++from)
1300 {
1301 if (curwin->w_jumplistidx == from)
1302 curwin->w_jumplistidx = to;
1303 for (i = from + 1; i < curwin->w_jumplistlen; ++i)
1304 if (curwin->w_jumplist[i].fmark.fnum
1305 == curwin->w_jumplist[from].fmark.fnum
1306 && curwin->w_jumplist[from].fmark.fnum != 0
1307 && curwin->w_jumplist[i].fmark.mark.lnum
1308 == curwin->w_jumplist[from].fmark.mark.lnum)
1309 break;
1310 if (i >= curwin->w_jumplistlen) /* no duplicate */
1311 curwin->w_jumplist[to++] = curwin->w_jumplist[from];
1312 else
1313 vim_free(curwin->w_jumplist[from].fname);
1314 }
1315 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
1316 curwin->w_jumplistidx = to;
1317 curwin->w_jumplistlen = to;
1318}
1319
1320# if defined(FEAT_WINDOWS) || defined(PROTO)
1321/*
1322 * Copy the jumplist from window "from" to window "to".
1323 */
1324 void
1325copy_jumplist(from, to)
1326 win_T *from;
1327 win_T *to;
1328{
1329 int i;
1330
1331 for (i = 0; i < from->w_jumplistlen; ++i)
1332 {
1333 to->w_jumplist[i] = from->w_jumplist[i];
1334 if (from->w_jumplist[i].fname != NULL)
1335 to->w_jumplist[i].fname = vim_strsave(from->w_jumplist[i].fname);
1336 }
1337 to->w_jumplistlen = from->w_jumplistlen;
1338 to->w_jumplistidx = from->w_jumplistidx;
1339}
1340
1341/*
1342 * Free items in the jumplist of window "wp".
1343 */
1344 void
1345free_jumplist(wp)
1346 win_T *wp;
1347{
1348 int i;
1349
1350 for (i = 0; i < wp->w_jumplistlen; ++i)
1351 vim_free(wp->w_jumplist[i].fname);
1352}
1353# endif
1354#endif /* FEAT_JUMPLIST */
1355
1356 void
1357set_last_cursor(win)
1358 win_T *win;
1359{
1360 win->w_buffer->b_last_cursor = win->w_cursor;
1361}
1362
Bram Moolenaarea408852005-06-25 22:49:46 +00001363#if defined(EXITFREE) || defined(PROTO)
1364 void
1365free_all_marks()
1366{
1367 int i;
1368
1369 for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
1370 if (namedfm[i].fmark.mark.lnum != 0)
1371 vim_free(namedfm[i].fname);
1372}
1373#endif
1374
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375#if defined(FEAT_VIMINFO) || defined(PROTO)
1376 int
1377read_viminfo_filemark(virp, force)
1378 vir_T *virp;
1379 int force;
1380{
1381 char_u *str;
1382 xfmark_T *fm;
1383 int i;
1384
1385 /* We only get here if line[0] == '\'' or '-'.
1386 * Illegal mark names are ignored (for future expansion). */
1387 str = virp->vir_line + 1;
1388 if (
1389#ifndef EBCDIC
1390 *str <= 127 &&
1391#endif
1392 ((*virp->vir_line == '\'' && (VIM_ISDIGIT(*str) || isupper(*str)))
1393 || (*virp->vir_line == '-' && *str == '\'')))
1394 {
1395 if (*str == '\'')
1396 {
1397#ifdef FEAT_JUMPLIST
1398 /* If the jumplist isn't full insert fmark as oldest entry */
1399 if (curwin->w_jumplistlen == JUMPLISTSIZE)
1400 fm = NULL;
1401 else
1402 {
1403 for (i = curwin->w_jumplistlen; i > 0; --i)
1404 curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
1405 ++curwin->w_jumplistidx;
1406 ++curwin->w_jumplistlen;
1407 fm = &curwin->w_jumplist[0];
1408 fm->fmark.mark.lnum = 0;
1409 fm->fname = NULL;
1410 }
1411#else
1412 fm = NULL;
1413#endif
1414 }
1415 else if (VIM_ISDIGIT(*str))
1416 fm = &namedfm[*str - '0' + NMARKS];
1417 else
1418 fm = &namedfm[*str - 'A'];
1419 if (fm != NULL && (fm->fmark.mark.lnum == 0 || force))
1420 {
1421 str = skipwhite(str + 1);
1422 fm->fmark.mark.lnum = getdigits(&str);
1423 str = skipwhite(str);
1424 fm->fmark.mark.col = getdigits(&str);
1425#ifdef FEAT_VIRTUALEDIT
1426 fm->fmark.mark.coladd = 0;
1427#endif
1428 fm->fmark.fnum = 0;
1429 str = skipwhite(str);
1430 vim_free(fm->fname);
1431 fm->fname = viminfo_readstring(virp, (int)(str - virp->vir_line),
1432 FALSE);
1433 }
1434 }
1435 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
1436}
1437
1438 void
1439write_viminfo_filemarks(fp)
1440 FILE *fp;
1441{
1442 int i;
1443 char_u *name;
1444 buf_T *buf;
1445 xfmark_T *fm;
1446
1447 if (get_viminfo_parameter('f') == 0)
1448 return;
1449
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02001450 fputs(_("\n# File marks:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451
1452 /*
1453 * Find a mark that is the same file and position as the cursor.
1454 * That one, or else the last one is deleted.
1455 * Move '0 to '1, '1 to '2, etc. until the matching one or '9
1456 * Set '0 mark to current cursor position.
1457 */
1458 if (curbuf->b_ffname != NULL && !removable(curbuf->b_ffname))
1459 {
1460 name = buflist_nr2name(curbuf->b_fnum, TRUE, FALSE);
1461 for (i = NMARKS; i < NMARKS + EXTRA_MARKS - 1; ++i)
1462 if (namedfm[i].fmark.mark.lnum == curwin->w_cursor.lnum
1463 && (namedfm[i].fname == NULL
1464 ? namedfm[i].fmark.fnum == curbuf->b_fnum
1465 : (name != NULL
1466 && STRCMP(name, namedfm[i].fname) == 0)))
1467 break;
1468 vim_free(name);
1469
1470 vim_free(namedfm[i].fname);
1471 for ( ; i > NMARKS; --i)
1472 namedfm[i] = namedfm[i - 1];
1473 namedfm[NMARKS].fmark.mark = curwin->w_cursor;
1474 namedfm[NMARKS].fmark.fnum = curbuf->b_fnum;
1475 namedfm[NMARKS].fname = NULL;
1476 }
1477
1478 /* Write the filemarks '0 - '9 and 'A - 'Z */
1479 for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
1480 write_one_filemark(fp, &namedfm[i], '\'',
1481 i < NMARKS ? i + 'A' : i - NMARKS + '0');
1482
1483#ifdef FEAT_JUMPLIST
1484 /* Write the jumplist with -' */
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02001485 fputs(_("\n# Jumplist (newest first):\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 setpcmark(); /* add current cursor position */
1487 cleanup_jumplist();
1488 for (fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];
1489 fm >= &curwin->w_jumplist[0]; --fm)
1490 {
1491 if (fm->fmark.fnum == 0
1492 || ((buf = buflist_findnr(fm->fmark.fnum)) != NULL
1493 && !removable(buf->b_ffname)))
1494 write_one_filemark(fp, fm, '-', '\'');
1495 }
1496#endif
1497}
1498
1499 static void
1500write_one_filemark(fp, fm, c1, c2)
1501 FILE *fp;
1502 xfmark_T *fm;
1503 int c1;
1504 int c2;
1505{
1506 char_u *name;
1507
1508 if (fm->fmark.mark.lnum == 0) /* not set */
1509 return;
1510
1511 if (fm->fmark.fnum != 0) /* there is a buffer */
1512 name = buflist_nr2name(fm->fmark.fnum, TRUE, FALSE);
1513 else
1514 name = fm->fname; /* use name from .viminfo */
1515 if (name != NULL && *name != NUL)
1516 {
1517 fprintf(fp, "%c%c %ld %ld ", c1, c2, (long)fm->fmark.mark.lnum,
1518 (long)fm->fmark.mark.col);
1519 viminfo_writestring(fp, name);
1520 }
1521
1522 if (fm->fmark.fnum != 0)
1523 vim_free(name);
1524}
1525
1526/*
1527 * Return TRUE if "name" is on removable media (depending on 'viminfo').
1528 */
1529 int
1530removable(name)
1531 char_u *name;
1532{
1533 char_u *p;
1534 char_u part[51];
1535 int retval = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001536 size_t n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537
1538 name = home_replace_save(NULL, name);
1539 if (name != NULL)
1540 {
1541 for (p = p_viminfo; *p; )
1542 {
1543 copy_option_part(&p, part, 51, ", ");
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001544 if (part[0] == 'r')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001546 n = STRLEN(part + 1);
1547 if (MB_STRNICMP(part + 1, name, n) == 0)
1548 {
1549 retval = TRUE;
1550 break;
1551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 }
1553 }
1554 vim_free(name);
1555 }
1556 return retval;
1557}
1558
1559static void write_one_mark __ARGS((FILE *fp_out, int c, pos_T *pos));
1560
1561/*
1562 * Write all the named marks for all buffers.
1563 * Return the number of buffers for which marks have been written.
1564 */
1565 int
1566write_viminfo_marks(fp_out)
1567 FILE *fp_out;
1568{
1569 int count;
1570 buf_T *buf;
1571 int is_mark_set;
1572 int i;
1573#ifdef FEAT_WINDOWS
1574 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001575 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
1577 /*
1578 * Set b_last_cursor for the all buffers that have a window.
1579 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001580 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 set_last_cursor(win);
1582#else
1583 set_last_cursor(curwin);
1584#endif
1585
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02001586 fputs(_("\n# History of marks within files (newest to oldest):\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 count = 0;
1588 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1589 {
1590 /*
1591 * Only write something if buffer has been loaded and at least one
1592 * mark is set.
1593 */
1594 if (buf->b_marks_read)
1595 {
1596 if (buf->b_last_cursor.lnum != 0)
1597 is_mark_set = TRUE;
1598 else
1599 {
1600 is_mark_set = FALSE;
1601 for (i = 0; i < NMARKS; i++)
1602 if (buf->b_namedm[i].lnum != 0)
1603 {
1604 is_mark_set = TRUE;
1605 break;
1606 }
1607 }
1608 if (is_mark_set && buf->b_ffname != NULL
1609 && buf->b_ffname[0] != NUL && !removable(buf->b_ffname))
1610 {
1611 home_replace(NULL, buf->b_ffname, IObuff, IOSIZE, TRUE);
1612 fprintf(fp_out, "\n> ");
1613 viminfo_writestring(fp_out, IObuff);
1614 write_one_mark(fp_out, '"', &buf->b_last_cursor);
1615 write_one_mark(fp_out, '^', &buf->b_last_insert);
1616 write_one_mark(fp_out, '.', &buf->b_last_change);
1617#ifdef FEAT_JUMPLIST
1618 /* changelist positions are stored oldest first */
1619 for (i = 0; i < buf->b_changelistlen; ++i)
1620 write_one_mark(fp_out, '+', &buf->b_changelist[i]);
1621#endif
1622 for (i = 0; i < NMARKS; i++)
1623 write_one_mark(fp_out, 'a' + i, &buf->b_namedm[i]);
1624 count++;
1625 }
1626 }
1627 }
1628
1629 return count;
1630}
1631
1632 static void
1633write_one_mark(fp_out, c, pos)
1634 FILE *fp_out;
1635 int c;
1636 pos_T *pos;
1637{
1638 if (pos->lnum != 0)
1639 fprintf(fp_out, "\t%c\t%ld\t%d\n", c, (long)pos->lnum, (int)pos->col);
1640}
1641
1642/*
1643 * Handle marks in the viminfo file:
Bram Moolenaard812df62008-11-09 12:46:09 +00001644 * fp_out != NULL: copy marks for buffers not in buffer list
1645 * fp_out == NULL && (flags & VIF_WANT_MARKS): read marks for curbuf only
1646 * fp_out == NULL && (flags & VIF_GET_OLDFILES | VIF_FORCEIT): fill v:oldfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 */
1648 void
Bram Moolenaard812df62008-11-09 12:46:09 +00001649copy_viminfo_marks(virp, fp_out, count, eof, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 vir_T *virp;
1651 FILE *fp_out;
1652 int count;
1653 int eof;
Bram Moolenaard812df62008-11-09 12:46:09 +00001654 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655{
1656 char_u *line = virp->vir_line;
1657 buf_T *buf;
1658 int num_marked_files;
1659 int load_marks;
1660 int copy_marks_out;
1661 char_u *str;
1662 int i;
1663 char_u *p;
1664 char_u *name_buf;
1665 pos_T pos;
Bram Moolenaard812df62008-11-09 12:46:09 +00001666#ifdef FEAT_EVAL
1667 list_T *list = NULL;
1668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
1670 if ((name_buf = alloc(LSIZE)) == NULL)
1671 return;
1672 *name_buf = NUL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001673
1674#ifdef FEAT_EVAL
1675 if (fp_out == NULL && (flags & (VIF_GET_OLDFILES | VIF_FORCEIT)))
1676 {
1677 list = list_alloc();
1678 if (list != NULL)
1679 set_vim_var_list(VV_OLDFILES, list);
1680 }
1681#endif
1682
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 num_marked_files = get_viminfo_parameter('\'');
1684 while (!eof && (count < num_marked_files || fp_out == NULL))
1685 {
1686 if (line[0] != '>')
1687 {
1688 if (line[0] != '\n' && line[0] != '\r' && line[0] != '#')
1689 {
1690 if (viminfo_error("E576: ", _("Missing '>'"), line))
1691 break; /* too many errors, return now */
1692 }
1693 eof = vim_fgets(line, LSIZE, virp->vir_fd);
1694 continue; /* Skip this dud line */
1695 }
1696
1697 /*
1698 * Handle long line and translate escaped characters.
1699 * Find file name, set str to start.
1700 * Ignore leading and trailing white space.
1701 */
1702 str = skipwhite(line + 1);
1703 str = viminfo_readstring(virp, (int)(str - virp->vir_line), FALSE);
1704 if (str == NULL)
1705 continue;
1706 p = str + STRLEN(str);
1707 while (p != str && (*p == NUL || vim_isspace(*p)))
1708 p--;
1709 if (*p)
1710 p++;
1711 *p = NUL;
1712
Bram Moolenaard812df62008-11-09 12:46:09 +00001713#ifdef FEAT_EVAL
1714 if (list != NULL)
1715 list_append_string(list, str, -1);
1716#endif
1717
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 /*
1719 * If fp_out == NULL, load marks for current buffer.
1720 * If fp_out != NULL, copy marks for buffers not in buflist.
1721 */
1722 load_marks = copy_marks_out = FALSE;
1723 if (fp_out == NULL)
1724 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001725 if ((flags & VIF_WANT_MARKS) && curbuf->b_ffname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 {
1727 if (*name_buf == NUL) /* only need to do this once */
1728 home_replace(NULL, curbuf->b_ffname, name_buf, LSIZE, TRUE);
1729 if (fnamecmp(str, name_buf) == 0)
1730 load_marks = TRUE;
1731 }
1732 }
1733 else /* fp_out != NULL */
1734 {
1735 /* This is slow if there are many buffers!! */
1736 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1737 if (buf->b_ffname != NULL)
1738 {
1739 home_replace(NULL, buf->b_ffname, name_buf, LSIZE, TRUE);
1740 if (fnamecmp(str, name_buf) == 0)
1741 break;
1742 }
1743
1744 /*
1745 * copy marks if the buffer has not been loaded
1746 */
1747 if (buf == NULL || !buf->b_marks_read)
1748 {
1749 copy_marks_out = TRUE;
1750 fputs("\n> ", fp_out);
1751 viminfo_writestring(fp_out, str);
1752 count++;
1753 }
1754 }
1755 vim_free(str);
1756
1757#ifdef FEAT_VIRTUALEDIT
1758 pos.coladd = 0;
1759#endif
1760 while (!(eof = viminfo_readline(virp)) && line[0] == TAB)
1761 {
1762 if (load_marks)
1763 {
1764 if (line[1] != NUL)
1765 {
Bram Moolenaare698add2011-02-25 15:11:22 +01001766 unsigned u;
1767
1768 sscanf((char *)line + 2, "%ld %u", &pos.lnum, &u);
1769 pos.col = u;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 switch (line[1])
1771 {
1772 case '"': curbuf->b_last_cursor = pos; break;
1773 case '^': curbuf->b_last_insert = pos; break;
1774 case '.': curbuf->b_last_change = pos; break;
1775 case '+':
1776#ifdef FEAT_JUMPLIST
1777 /* changelist positions are stored oldest
1778 * first */
1779 if (curbuf->b_changelistlen == JUMPLISTSIZE)
1780 /* list is full, remove oldest entry */
1781 mch_memmove(curbuf->b_changelist,
1782 curbuf->b_changelist + 1,
1783 sizeof(pos_T) * (JUMPLISTSIZE - 1));
1784 else
1785 ++curbuf->b_changelistlen;
1786 curbuf->b_changelist[
1787 curbuf->b_changelistlen - 1] = pos;
1788#endif
1789 break;
1790 default: if ((i = line[1] - 'a') >= 0 && i < NMARKS)
1791 curbuf->b_namedm[i] = pos;
1792 }
1793 }
1794 }
1795 else if (copy_marks_out)
1796 fputs((char *)line, fp_out);
1797 }
1798 if (load_marks)
1799 {
1800#ifdef FEAT_JUMPLIST
1801 win_T *wp;
1802
1803 FOR_ALL_WINDOWS(wp)
1804 {
1805 if (wp->w_buffer == curbuf)
1806 wp->w_changelistidx = curbuf->b_changelistlen;
1807 }
1808#endif
1809 break;
1810 }
1811 }
1812 vim_free(name_buf);
1813}
1814#endif /* FEAT_VIMINFO */