Monday, January 16, 2017

Re: Set cusror position from inside function

Hi, Alessandro,

Here is Key_k() function (the other functions are analogous, only l:lineToGo and l:columnToGo are set to different values):

function Key_k()
    let l:lineToGo = line(".") - 1
    let l:columnToGo = col(".")
    let b:noteUnderCursor = GetNoteOnLine(l:lineToGo)
    call Key_Movement(l:lineToGo, l:columnToGo)
endfunction 

And this is Key_Movement():


function Key_Movement(line, column)
    if !empty(b:noteUnderCursor)
        if !(b:noteUnderCursor["line"] == a:line)
            let l:indentLen = strlen(b:noteUnderCursor["indent"])
            " accounting for "| " portion just after whispace indent
            let l:indentLen = l:indentLen + 3
            if a:column <= l:indentLen
                call setpos(".", [0, a:line, l:indentLen, 0])
                return
            endif
        endif
    endif
    call setpos(".", [0, a:line, a:column, 0])
endfunction

However, I managed to get it work by mapping the keys without <expr> option, like this:

noremap <silent> k :call Key_k()<CR>

2017-01-16 18:11 GMT+02:00 Alessandro Antonello <antonello.ale@gmail.com>:
Hi, Tihomir.

Looking at your message I can't see from were the 'a:line' and 'a:column'
variables did come from. The way you are calling the set of "Key_*()"
functions, you are not passing any information through parameters. So I'm
assuming you are discovering these values inside the function. Which is a
little bit strange since local variables are prefixed with "l:". "a:" prefix
is used for function arguments.

Could you send a message with the code that doesn't work? I think it would be
more easy to find an issue if we could see the function. Or a bigger snippet.


2017-01-15 10:15 GMT-02:00 Tihomir Mitkov <mitkovtihomir@gmail.com>:
Since vim doesn't support having some lines editable and others not, I figured it out that I can simulate this by keeping meta data about lines and remapping all keys to a function which checks cursor position and decides whether to put the character in question or not. So far so good, now I need to prohibit the cursor from going to certain locations. I remapped h,j,k,l keys like this:

map <expr> h Key_h()
map <expr> j Key_j()
map <expr> k Key_k()
map <expr> l Key_l()

That also works like a charm. Somewhere in the those functions I call setpos() like this:


call setpos(".", [0, a:line, a:column, 0])

Unfortunately the only thing this does is to send cursor the first column at the line it is currently in. Further movement with h,j,k,l does nothing. From inside the mapped functions Key_h(), Key_j() I return nothing, since cursor movement is already performed.

--
--
You received this message from the "vim_use" 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_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
--
You received this message from the "vim_use" 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 a topic in the Google Groups "vim_use" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/fyzeSj9BKNI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
--
You received this message from the "vim_use" 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_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment