1

In §9 of :h channel is mentioned 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 argument?

1
  • 2
    out_cb/exit_cb take 2 arguments not 1 list
    – Mass
    Feb 27, 2022 at 14:36

1 Answer 1

1

This is official answer of Bram Moolenaar, hope this can help someone. 😊

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 example 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.