Skip to content

Commit f4a1d1c

Browse files
committed
patch 8.1.2302: :lockmarks does not work for '[ and ']
Problem: :lockmarks does not work for '[ and ']. Solution: save and restore '[ and '] marks. (James McCoy, closes #5222)
1 parent ab85ca4 commit f4a1d1c

10 files changed

Lines changed: 208 additions & 69 deletions

File tree

src/bufwrite.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,8 @@ buf_write(
683683
context_sha256_T sha_ctx;
684684
#endif
685685
unsigned int bkc = get_bkc_value(buf);
686+
pos_T orig_start = buf->b_op_start;
687+
pos_T orig_end = buf->b_op_end;
686688

687689
if (fname == NULL || *fname == NUL) // safety check
688690
return FAIL;
@@ -875,6 +877,13 @@ buf_write(
875877
#endif
876878
)
877879
{
880+
if (buf != NULL && cmdmod.lockmarks)
881+
{
882+
// restore the original '[ and '] positions
883+
buf->b_op_start = orig_start;
884+
buf->b_op_end = orig_end;
885+
}
886+
878887
--no_wait_return;
879888
msg_scroll = msg_save;
880889
if (nofile_err)
@@ -952,6 +961,13 @@ buf_write(
952961
fname = buf->b_sfname;
953962
}
954963

964+
if (cmdmod.lockmarks)
965+
{
966+
// restore the original '[ and '] positions
967+
buf->b_op_start = orig_start;
968+
buf->b_op_end = orig_end;
969+
}
970+
955971
#ifdef FEAT_NETBEANS_INTG
956972
if (netbeans_active() && isNetbeansBuffer(buf))
957973
{

src/diff.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,16 +772,22 @@ diff_write(buf_T *buf, diffin_T *din)
772772
{
773773
int r;
774774
char_u *save_ff;
775+
int save_lockmarks;
775776

776777
if (din->din_fname == NULL)
777778
return diff_write_buffer(buf, din);
778779

779780
// Always use 'fileformat' set to "unix".
780781
save_ff = buf->b_p_ff;
781782
buf->b_p_ff = vim_strsave((char_u *)FF_UNIX);
783+
save_lockmarks = cmdmod.lockmarks;
784+
// Writing the buffer is an implementation detail of performing the diff,
785+
// so it shouldn't update the '[ and '] marks.
786+
cmdmod.lockmarks = TRUE;
782787
r = buf_write(buf, din->din_fname, NULL,
783788
(linenr_T)1, buf->b_ml.ml_line_count,
784789
NULL, FALSE, FALSE, FALSE, TRUE);
790+
cmdmod.lockmarks = save_lockmarks;
785791
free_string_option(buf->b_p_ff);
786792
buf->b_p_ff = save_ff;
787793
return r;

src/ex_cmds.c

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,11 @@ do_move(linenr_T line1, linenr_T line2, linenr_T dest)
749749
foldMoveRange(&win->w_folds, line1, line2, dest);
750750
}
751751
#endif
752-
curbuf->b_op_start.lnum = dest - num_lines + 1;
753-
curbuf->b_op_end.lnum = dest;
752+
if (!cmdmod.lockmarks)
753+
{
754+
curbuf->b_op_start.lnum = dest - num_lines + 1;
755+
curbuf->b_op_end.lnum = dest;
756+
}
754757
}
755758
else
756759
{
@@ -761,10 +764,14 @@ do_move(linenr_T line1, linenr_T line2, linenr_T dest)
761764
foldMoveRange(&win->w_folds, dest + 1, line1 - 1, line2);
762765
}
763766
#endif
764-
curbuf->b_op_start.lnum = dest + 1;
765-
curbuf->b_op_end.lnum = dest + num_lines;
767+
if (!cmdmod.lockmarks)
768+
{
769+
curbuf->b_op_start.lnum = dest + 1;
770+
curbuf->b_op_end.lnum = dest + num_lines;
771+
}
766772
}
767-
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
773+
if (!cmdmod.lockmarks)
774+
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
768775
mark_adjust_nofold(last_line - num_lines + 1, last_line,
769776
-(last_line - dest - extra), 0L);
770777

@@ -813,9 +820,12 @@ ex_copy(linenr_T line1, linenr_T line2, linenr_T n)
813820
char_u *p;
814821

815822
count = line2 - line1 + 1;
816-
curbuf->b_op_start.lnum = n + 1;
817-
curbuf->b_op_end.lnum = n + count;
818-
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
823+
if (!cmdmod.lockmarks)
824+
{
825+
curbuf->b_op_start.lnum = n + 1;
826+
curbuf->b_op_end.lnum = n + count;
827+
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
828+
}
819829

820830
/*
821831
* there are three situations:
@@ -1055,10 +1065,17 @@ do_filter(
10551065
char_u *cmd_buf;
10561066
buf_T *old_curbuf = curbuf;
10571067
int shell_flags = 0;
1068+
pos_T orig_start = curbuf->b_op_start;
1069+
pos_T orig_end = curbuf->b_op_end;
1070+
int save_lockmarks = cmdmod.lockmarks;
10581071

10591072
if (*cmd == NUL) /* no filter command */
10601073
return;
10611074

1075+
// Temporarily disable lockmarks since that's needed to propagate changed
1076+
// regions of the buffer for foldUpdate(), linecount, etc.
1077+
cmdmod.lockmarks = 0;
1078+
10621079
cursor_save = curwin->w_cursor;
10631080
linecount = line2 - line1 + 1;
10641081
curwin->w_cursor.lnum = line1;
@@ -1287,11 +1304,18 @@ do_filter(
12871304

12881305
filterend:
12891306

1307+
cmdmod.lockmarks = save_lockmarks;
12901308
if (curbuf != old_curbuf)
12911309
{
12921310
--no_wait_return;
12931311
emsg(_("E135: *Filter* Autocommands must not change current buffer"));
12941312
}
1313+
else if (cmdmod.lockmarks)
1314+
{
1315+
curbuf->b_op_start = orig_start;
1316+
curbuf->b_op_end = orig_end;
1317+
}
1318+
12951319
if (itmp != NULL)
12961320
mch_remove(itmp);
12971321
if (otmp != NULL)
@@ -3276,13 +3300,16 @@ ex_append(exarg_T *eap)
32763300
* eap->line2 pointed to the end of the buffer and nothing was appended)
32773301
* "end" is set to lnum when something has been appended, otherwise
32783302
* it is the same than "start" -- Acevedo */
3279-
curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
3280-
eap->line2 + 1 : curbuf->b_ml.ml_line_count;
3281-
if (eap->cmdidx != CMD_append)
3282-
--curbuf->b_op_start.lnum;
3283-
curbuf->b_op_end.lnum = (eap->line2 < lnum)
3284-
? lnum : curbuf->b_op_start.lnum;
3285-
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
3303+
if (!cmdmod.lockmarks)
3304+
{
3305+
curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ?
3306+
eap->line2 + 1 : curbuf->b_ml.ml_line_count;
3307+
if (eap->cmdidx != CMD_append)
3308+
--curbuf->b_op_start.lnum;
3309+
curbuf->b_op_end.lnum = (eap->line2 < lnum)
3310+
? lnum : curbuf->b_op_start.lnum;
3311+
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
3312+
}
32863313
curwin->w_cursor.lnum = lnum;
32873314
check_cursor_lnum();
32883315
beginline(BL_SOL | BL_FIX);
@@ -4592,10 +4619,13 @@ do_sub(exarg_T *eap)
45924619

45934620
if (sub_nsubs > start_nsubs)
45944621
{
4595-
/* Set the '[ and '] marks. */
4596-
curbuf->b_op_start.lnum = eap->line1;
4597-
curbuf->b_op_end.lnum = line2;
4598-
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4622+
if (!cmdmod.lockmarks)
4623+
{
4624+
// Set the '[ and '] marks.
4625+
curbuf->b_op_start.lnum = eap->line1;
4626+
curbuf->b_op_end.lnum = line2;
4627+
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
4628+
}
45994629

46004630
if (!global_busy)
46014631
{

src/fileio.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ readfile(
188188
wasn't possible */
189189
char_u conv_rest[CONV_RESTLEN];
190190
int conv_restlen = 0; /* nr of bytes in conv_rest[] */
191+
pos_T orig_start;
191192
buf_T *old_curbuf;
192193
char_u *old_b_ffname;
193194
char_u *old_b_fname;
@@ -250,9 +251,7 @@ readfile(
250251
*/
251252
if (!filtering && !read_stdin && !read_buffer)
252253
{
253-
pos_T pos;
254-
255-
pos = curbuf->b_op_start;
254+
orig_start = curbuf->b_op_start;
256255

257256
/* Set '[ mark to the line above where the lines go (line 1 if zero). */
258257
curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
@@ -276,7 +275,7 @@ readfile(
276275
return OK;
277276
#endif
278277

279-
curbuf->b_op_start = pos;
278+
curbuf->b_op_start = orig_start;
280279
}
281280

282281
if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
@@ -617,6 +616,7 @@ readfile(
617616
/*
618617
* Set '[ mark to the line above where the lines go (line 1 if zero).
619618
*/
619+
orig_start = curbuf->b_op_start;
620620
curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
621621
curbuf->b_op_start.col = 0;
622622

@@ -658,6 +658,7 @@ readfile(
658658
try_mac = (vim_strchr(p_ffs, 'm') != NULL);
659659
try_dos = (vim_strchr(p_ffs, 'd') != NULL);
660660
try_unix = (vim_strchr(p_ffs, 'x') != NULL);
661+
curbuf->b_op_start = orig_start;
661662

662663
if (msg_scrolled == n)
663664
msg_scroll = m;
@@ -2471,13 +2472,14 @@ readfile(
24712472
check_cursor_lnum();
24722473
beginline(BL_WHITE | BL_FIX); /* on first non-blank */
24732474

2474-
/*
2475-
* Set '[ and '] marks to the newly read lines.
2476-
*/
2477-
curbuf->b_op_start.lnum = from + 1;
2478-
curbuf->b_op_start.col = 0;
2479-
curbuf->b_op_end.lnum = from + linecnt;
2480-
curbuf->b_op_end.col = 0;
2475+
if (!cmdmod.lockmarks)
2476+
{
2477+
// Set '[ and '] marks to the newly read lines.
2478+
curbuf->b_op_start.lnum = from + 1;
2479+
curbuf->b_op_start.col = 0;
2480+
curbuf->b_op_end.lnum = from + linecnt;
2481+
curbuf->b_op_end.col = 0;
2482+
}
24812483

24822484
#ifdef MSWIN
24832485
/*

src/indent.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,9 +1001,12 @@ op_reindent(oparg_T *oap, int (*how)(void))
10011001
smsg(NGETTEXT("%ld line indented ",
10021002
"%ld lines indented ", i), i);
10031003
}
1004-
// set '[ and '] marks
1005-
curbuf->b_op_start = oap->start;
1006-
curbuf->b_op_end = oap->end;
1004+
if (!cmdmod.lockmarks)
1005+
{
1006+
// set '[ and '] marks
1007+
curbuf->b_op_start = oap->start;
1008+
curbuf->b_op_end = oap->end;
1009+
}
10071010
}
10081011
#endif // defined(FEAT_LISP) || defined(FEAT_CINDENT)
10091012

0 commit comments

Comments
 (0)