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