IDE for the Agon Light using neovim or vim

Why a Mini-IDE for Agon Light?

In the previous article, I introduced my script agon_automate.py, which allows sending commands to Agon Light and executing automations or remote controls. This makes it possible to perform tasks on Agon without switching keyboards or entering things manually.

However, manually starting scripts can become cumbersome when you are in your editor and want to quickly transfer and start a program on a physical Agon Light (or of course, on its Console 8). This is especially true when you’re in the development phase and need to do this constantly.

As a fan of vi, vim, and especially neovim, I have crafted a small “IDE” for neovim. This allows me to write programs in BBC Basic directly and comfortably on my main computer and transfer and start them on a physical Agon right from neovim. All this with a short command, without leaving the editor and even without having to specify the file name.

Integration of the Agon Automation Script in neovim

I usually work only with neovim. Since some may also work with VIM, I rewrote the script for it as well. I will explain everything first from the perspective of neovim, as I wrote the script natively in Lua (the scripting language of neovim). At the end of this post you will find the script for VIM. It’s less tested, but it should also work.

-- Save file, transfer to Agon und Run it.
vim.cmd [[ command! AgonRun :w ++ff=dos | :!python ~/bin/agon_automate.py script ~/bin/agonbasic.script % ]]

-- Send one short command to Agon
vim.cmd [[command! -nargs=1 AgonCommand execute ':!python ~/bin/agon_automate.py direct <args>' | redraw!]]

-- Add the command AgonPaste to NeoVim
vim.api.nvim_create_user_command('AgonPaste', function()
  -- Determine the path for the temporary file in the temp directory
  local temp_file_path = vim.fn.tempname() .. "_AgonPaste.tmp"

  -- Open the temporary file in write mode
  local file = io.open(temp_file_path, "w")
  if not file then
    print("Error opening file: " .. temp_file_path)
    return
  end

  -- Get the content of the register
  local register_content = vim.fn.getreg('"')

  -- Write the content to the file and close the file
  file:write(register_content)
  file:close()

  -- Execute the external Python command with the script mode
  local command = "python " .. vim.loop.os_homedir() .. "/bin/agon_automate.py script " .. temp_file_path
  vim.fn.system(command)

  -- Remove the temporary file after sending
  os.remove(temp_file_path)
end, {})

Place the entire script where it can be loaded in your neovim environment. The config file is usually called init.lua and is located in .config/nvim/init.lua. If you have a more complex environment or config, it may be located elsewhere for you. For example a directory or file under.config/nvim/lua/.

You just need to be careful if there is a file named init.vim. In that case, neovim is working with VimScript and not natively with Lua. Since init.lua is used first and then perhaps your config is no longer applied, this could be helpful in the init.lua file: vim.cmd('source ~/.config/nvim/init.vim')

Unfortunately, there is no standard for the neovim configs. For more complex neovim configurations, it may also be a different config file.

Please don’t forget to adapt the above script for your environment (python or python3 and directories). Especially these three lines in the script above:

vim.cmd [[ command! AgonRun :w ++ff=dos | :!python ~/bin/agon_automate.py script ~/bin/agonbasic.script % ]] … vim.cmd [[command! -nargs=1 AgonCommand execute ‘:!python ~/bin/agon_automate.py direct <args>’ | redraw!]] … local command = “python ” .. vim.loop.os_homedir() .. “/bin/agon_automate.py script ” .. temp_file_path

Whether the call is made via python or python3 and of course the correct paths to agon_automate.py and the script (in my case agonbasic.script) containing the desired automation. That is, which commands are executed in the automation.

Once you have integrated the script, reload neovim. Then load a Basic code to have something to test and play with.

What can I specifically do with it?

The script itself offers three main commands. Commands in vi are started with a “:” in Normal mode. So instead of pressing i for Insert, you press a colon “:”. Then enter one of the commands below.

AgonCommand

With this new neovim command, you can send short commands directly from the editor to Agon Light.

An example would be:

:AgonCommand LIST

This send the command LIST to your Agon to list a program in BBC Basic. If you’re already familiar with agon_automate.py, it’s no different than entering python agon_automate.py direct “LIST“ in the terminal.

AgonRun

This function saves the current file in DOS mode (since Agon has a problem with line breaks in Unix files) and triggers the automation.

For this, you need to adjust the script to define the path to agon_automate.py and the name of the automation script (in my case ~/bin/agonbasic.script). This is identical to the one in my last post:

*BYE
hexload vdp {FILENAME}
{COMMAND python ~/bin/send.py {FILENAME} {DEVICE}}{WAIT 2}
load /bbcbasic.bin
run
LOAD "{FILENAME}"
{WAIT 2}
RUN

Of course, you can build another script for yourself, not just for Basic. In my case, it’s for my workflow for writing programs in BBC BASIC.

The file name of the current file is passed automatically.

:AgonRun

The Command requires no further parameters. After which the document is saved, transferred, loaded, and executed, or whatever your automation entails. All the same like python agon_automate.py script agonbasic.script filename.

AgonPaste

This is the real magic and time saver. You’ve transferred complete code to Agon with AgonRun and started it there. But now you want to change just a single line in the code.

Before you do it directly on Agon and end up with different versions of the code on Agon Light and your computer, or having to transfer everything again.

Or you copy the line with yy (yank line, like copy a line to clipboard) in neovim and run this command:

:AgonPaste

What you need to know is how vi works. Much of what you do in it goes into the standard buffer. You could also call it a clipboard. Everything you delete with dd or copy with yy ends up in this buffer.

This function does nothing more than send the contents of the current buffer to your Agon. So you can also mark multiple lines with V and then put them into the buffer with y and transfer them.

In the video, I’m modifying a line from the basic program Matrix. Then, I press yy on that line and execute :AgonPaste. The modification is then transferred. Afterward, I can restart the program on my Agon with :AgonCommand RUN.


If you’re familiar with vi, it should be clear by now that you can transfer more than one line with this (5yy for 5 lines or use v for visual mode and then yy). Maybe check out this older post (in german) of mine, where I go over some basics of vi, vim, and neovim.

VIM

Functionally, everything is the same. Only the script is created in VimConfig (VIM doesn’t natively understand LUA). You should have ~/.vimrc or ~/.config/nvim /init.vim. Incorporate the script below there. Everything else is identical to the above instructions.

The whole thing is now explained more specifically for a workflow for BBC Basic and built upon it. But of course, you can rebuild everything based on these fundamentals to suit your needs.

" Save the current file with DOS file format and run a Python script with the current file as an argument
command! AgonRun w ++ff=dos | !python3 ~/bin/agon_automate.py script ~/bin/agonbasic.script %

" Send a single command to the Python script directly
command! -nargs=1 AgonCommand execute '!python3 ~/bin/agon_automate.py direct <args>' | redraw!

" Define a function to paste the content of the default register into a temporary file and execute a Python script with it
function! AgonPaste()
  " Create a temporary file path
  let temp_file_path = tempname() . "_AgonPaste.tmp"

  " Write the content of the default register to the temporary file
  call writefile([getreg('"')], temp_file_path)

  " Execute the Python script with the temporary file
  let command = 'python3 ' . $HOME . '/bin/agon_automate.py script ' . temp_file_path
  call system(command)

  " Remove the temporary file
  call delete(temp_file_path)
endfunction
command! AgonPaste call AgonPaste()

I’ve briefly tested the script. It should all work fine. However, since I usually only work in neovim, I don’t really have much experience with how stable it actually is.

If you have questions, encounter problems, or just want to give me some nice feedback, feel free to leave a comment.

Similar Posts

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *