On 24/01/17 21:02, Bram Moolenaar wrote:

Matthew Malcomson wrote:

I think it would be nice if setpos() uses the bufnum part of the
position it's given when asked to set a lowercase mark.

Also, when it's given an invalid buffer number exist for uppercase and
numbered marks I think it should fail and return -1.

I have a suggested patch and test below.
Interesting idea.

Why not also do this for other marks in "curbuf"?

The documentation also needs to be updated.


I didn't think about extending it to the other marks (I assume you mean the ", [, ], <, and > marks) it seems like a good idea to me. I've included a modified patch below.

I don't think the documentation needs to be updated, I only thought of making the change because that's how the documentation reads. The only mention of the "bufnum" part is

"
        "bufnum" is the buffer number.  Zero can be used for the
        current buffer.  Setting the cursor is only possible for
        the current buffer.  To set a mark in another buffer you can
        use the |bufnr()| function to turn a file name into a buffer
        number.
"
i.e. there's nothing about there being exceptions for anything other than the cursor position.

The thought to return -1 if given an invalid buffer number came from the sentence "Returns 0 when the position could be set, -1 otherwise."



diff --git a/src/mark.c b/src/mark.c
index 9c84bc40d..f4c2322f7 100644
--- a/src/mark.c
+++ b/src/mark.c
@@ -75,9 +75,15 @@ setmark_pos(int c, pos_T *pos, int fnum)
     return OK;
     }

+    buf_T *buf = buflist_findnr(fnum);
+    // Can't set a mark in a non-existant buffer.
+    if (buf == NULL) {
+    return FAIL;
+    }
+
     if (c == '"')
     {
-    curbuf->b_last_cursor = *pos;
+    buf->b_last_cursor = *pos;
     return OK;
     }

@@ -85,31 +91,31 @@ setmark_pos(int c, pos_T *pos, int fnum)
      * file. */
     if (c == '[')
     {
-    curbuf->b_op_start = *pos;
+    buf->b_op_start = *pos;
     return OK;
     }
     if (c == ']')
     {
-    curbuf->b_op_end = *pos;
+    buf->b_op_end = *pos;
     return OK;
     }

     if (c == '<' || c == '>')
     {
     if (c == '<')
-        curbuf->b_visual.vi_start = *pos;
+        buf->b_visual.vi_start = *pos;
     else
-        curbuf->b_visual.vi_end = *pos;
-    if (curbuf->b_visual.vi_mode == NUL)
+        buf->b_visual.vi_end = *pos;
+    if (buf->b_visual.vi_mode == NUL)
         /* Visual_mode has not yet been set, use a sane default. */
-        curbuf->b_visual.vi_mode = 'v';
+        buf->b_visual.vi_mode = 'v';
     return OK;
     }

     if (ASCII_ISLOWER(c))
     {
     i = c - 'a';
-    curbuf->b_namedm[i] = *pos;
+    buf->b_namedm[i] = *pos;
     return OK;
     }
     if (ASCII_ISUPPER(c) || VIM_ISDIGIT(c))
diff --git a/src/testdir/test_marks.vim b/src/testdir/test_marks.vim
index d00b1ddc8..edefff082 100644
--- a/src/testdir/test_marks.vim
+++ b/src/testdir/test_marks.vim
@@ -13,6 +13,28 @@ function! Test_Restore_DelMark()
   enew!
 endfunction

+" Test that setpos() can set a mark in another buffer.
+function! Test_Setpos_Mark()
+  enew!
+  let l:first_bufnr = bufnr('%')
+  call append(0, ["First line", "Second line", "Third line"])
+  new
+  let l:second_bufnr = bufnr('%')
+  call append(0, ["Line 1", "Line 2", "Line 3"])
+  call setpos("'d", [l:first_bufnr, 2, 1, 0])
+  for element in getpos("'d")
+    call assert_equal(0, element)
+  endfor
+  wincmd w
+  let pos = getpos("'d")
+  call assert_equal(2, pos[1])
+  call assert_equal(1, pos[2])
+  let retval = setpos("'d", [l:second_bufnr + 1, 2, 1, 0])
+  call assert_equal(-1 , retval)
+  quit!
+  enew!
+endfunction
+
 " Test that CTRL-A and CTRL-X updates last changed mark '[, '].
 function! Test_Incr_Marks()
   enew!

--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- You received this message because you are subscribed to the Google Groups "vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Raspunde prin e-mail lui