Sunday, May 31, 2020

Re: function definition for only one file, not for other simultaneously opened files

On 2020-06-01, Manas wrote:
> On 31/05/20 6:18 am, Gary Johnson wrote:

> How are you executing your function and what is it doing to affect
> files or buffers that you don't want it to?
>
> My function executes a bash command to get the current date and time, whenever
> I hit <ENTER> and prepends it to the line.
>
>
> On 31/05/20 2:13 pm, Tony Mechelynck wrote:
>
> - a function can be assigned to a Funcref which is a variable (global,
> script-local, buffer-local, window-local, etc.) of a special type;
> that Funcref can then be used both as a variable (when not followed by
> () ) or as a function (followed by () ).
>
> I have a kind of hunch that there is something for you (Manas) in the
> above but I'm not sure what.
>
> I found this the most optimum. What I did was:
>
> 1. define the function
>
> 2. create a buffer-local function reference pointing to the function:
>
> > let b:Fn = function("PrependTime")
>
> `PrependTime` is my function name.
>
> 3. And now `:echo b:Fn()` in current buffer will prepend the timestamp and in
> other buffers, it will throw error (E117: Unknown function). Now I wanted to
> somehow suppress the error message but I could not find much.

You wrote that you want to have <Enter> insert the date at the start
of the next line, but only in one buffer. Wouldn't making that
mapping buffer-local solve your problem? Something like this?

inoremap <buffer> <CR> <CR><C-R>=PrependTime()<CR>

function! PrependTime()
return strftime("%c")
endfunction

See

:help map-<buffer>

You would just need to execute that mapping only in the one buffer
where you wanted that behavior.

If other buffers are throwing error E117, then you have <Enter>
(a.k.a. <CR>) mapped in buffers where it shouldn't be mapped.

Regards,
Gary

--
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200601044307.GA10889%40phoenix.

No comments: