Out_cb callback handler in vim9

45 views
Skip to first unread message

Ni Va

unread,
Feb 27, 2022, 7:27:14 AM2/27/22
to vim_use
Hi All, 

In §9 of channel help is mentionned the legacy vimscript out_cb handler.


" let job = job_start(command, {"out_cb": "MyHandler"})
The function will be called with the channel and a message. You would define
it like this: >
    func MyHandler(channel, msg)  
    "


I tried this def OutCb function but got the following  message error :

" def OutCb(l: list<any>)
    # echomsg 'OutCb' .. string(l)
    # eval [][0]
  enddef

  def ExitCb(l: list<any>)
    # echomsg 'ExitCb' .. string(l)
    # sleep 1m
    # source += l
    # echomsg 'Exiting ' .. string(l) .. ' ' .. string(@z)
  enddef

  jobid = job_start(zip_cmd, { out_cb: OutCb, exit_cb:  ExitCb, mode: 'raw', timeout: 1200000 } ) "

Error message:
" This is a valid directory .
  Jobid: process 14328 run
  Press ENTER or type command to continue
  E118: Too many arguments for function: <lambda>4 "


How to port and deal with this in vim9 script that seems to have only one
argue ?

Thank you 
Nv

Bram Moolenaar

unread,
Feb 27, 2022, 8:25:43 AM2/27/22
to vim...@googlegroups.com, Ni Va
In Vim9 script the type of the arguments is checked. That helps you
writing correct functions, and once written it is easier to read back.
It does require a bit of extra text.

Here is an exmple that will help you:

vim9script

# Create a channel log so we can see what happens.
ch_logfile('logfile', 'w')

var shell_job: job

# Function handling a line of text that has been typed.
def TextEntered(text: string)
# Send the text to a shell with Enter appended.
ch_sendraw(shell_job, text .. "\n")
enddef

# Function handling output from the shell: Add it above the prompt.
def GotOutput(channel: channel, msg: string)
append(line("$") - 1, "- " .. msg)
enddef

# Function handling the shell exits: close the window.
def JobExit(job: job, status: number)
quit!
enddef

# Start a shell in the background.
shell_job = job_start(["/bin/sh"], {
out_cb: GotOutput,
err_cb: GotOutput,
exit_cb: JobExit,
})

new
set buftype=prompt
var buf = bufnr('')
prompt_setcallback(buf, TextEntered)
prompt_setprompt(buf, "shell command: ")

# start accepting shell commands
startinsert


--
hundred-and-one symptoms of being an internet addict:
115. You are late picking up your kid from school and try to explain
to the teacher you were stuck in Web traffic.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Yegappan Lakshmanan

unread,
Feb 27, 2022, 9:08:20 AM2/27/22
to vim_use
Hi,

On Sun, Feb 27, 2022 at 4:27 AM Ni Va <niva...@gmail.com> wrote:
>
> Hi All,
>
> In §9 of channel help is mentionned the legacy vimscript out_cb handler.
>
>
> " let job = job_start(command, {"out_cb": "MyHandler"})
> The function will be called with the channel and a message. You would define
> it like this: >
> func MyHandler(channel, msg)
> "
>
>
> I tried this def OutCb function but got the following message error :
>
> " def OutCb(l: list<any>)

As described in the help, this function should accept two arguments. The first
argument is the channel and the second argument is the message string.

> # echomsg 'OutCb' .. string(l)
> # eval [][0]
> enddef
>
> def ExitCb(l: list<any>)

This function also should accept two arguments. The first argument is the job
and the second argument is the exit status number.

- Yegappan

Ni Va

unread,
Feb 27, 2022, 12:52:23 PM2/27/22
to vim...@googlegroups.com
Thank you Yegappan ! 


Nota:probably missunderstood this with porting the legacy vimscript function that has argslist.

Fu foo(...) 

a0[i]

Endfu


--
--
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/M65deWEN3O0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vim_use+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAAW7x7mPGcK6GkO7hzVUq-F6XZDqBMXy%2BnibTMXCRhhUFhcAAw%40mail.gmail.com.

Nicolas

unread,
Dec 9, 2023, 7:40:18 AM12/9/23
to vim_use
Hi all,

According to Bram example in the job's help now, 
Is it possible to pass additional parameters to job's handlers callback in vim9script ?
Thank you
Nicolas

This minimalist example WORKING as Bram said:
def Compress_OnExit(job_id: job, exit_status: number): void
    echom 'Job OnExit ' .. job_id->string() .. ' exited with status ' .. exit_status
enddef
var job = job_start(cmd, { 'exit_cb': Compress_OnExit}) 

This minimal example with additional params: 
def Compress_OnExit(job_id: job, exit_status: number, foo: string): void
    echom 'Job OnExit ' .. job_id->string() .. ' exited with status ' .. exit_status
    # Compress_CopyToDrive('PATRIOT')
enddef
var job = job_start(cmd, { 'exit_cb': function('Compress_OnExit', ['foo']) } )

Lifepillar

unread,
Dec 10, 2023, 2:55:57 PM12/10/23
to vim...@googlegroups.com
On 2023-12-09, Nicolas <niva...@gmail.com> wrote:
> Hi all,
>
> According to Bram example in the job's help now,
> Is it possible to pass additional parameters to job's handlers callback in
> vim9script ?

Yes. See, for instance, $VIMRUNTIME/autoload/typeset.vim, in particular
the Callbacks section and the Typeset() function.

> This minimal example with additional params:
> def Compress_OnExit(job_id: job, exit_status: number, foo: string): void
> echom 'Job OnExit ' .. job_id->string() .. ' exited with status ' ..
> exit_status
> # Compress_CopyToDrive('PATRIOT')
> enddef
> var job = job_start(cmd, { 'exit_cb': function('Compress_OnExit',
> ['foo']) } )

That should be:

var job = job_start(['ls'], {
exit_cb: (j, e) => Compress_OnExit(j, e, 'foo')
})

Hope this helps,
Life.

Nicolas

unread,
Dec 14, 2023, 1:41:07 PM12/14/23
to vim_use
Thank you Life it works perfectly ;)
Nicolas

Reply all
Reply to author
Forward
0 new messages