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 the computer and transfer and start them on Agon right from neovim. All this with a short command, without leaving the editor and even without having to specify the file name.

You probably know that often an error message appears immediately, indicating a mistake in the code, and you have to transfer everything again. Therefore, I added a function to also transfer anything that is in the buffer (clipboard) of neovim. Like a diff. This saves me from having to transfer the entire program again and again.

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

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')

I use NvChad as the basis of my native neovim config. If you’re not familiar with something like this, I recommend checking it out. It gives you a very comprehensive configuration and many additional functions for neovim. In NvChad, the place for your own scripts or configs is ~/.config/nvim/lua/custom/init.lua.

Please don’t forget to adapt the script for your environment. 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 command, you can send short commands directly from the editor to Agon Light.

An example would be:

:AgonCommand RUN

This lets you start a program in BBC Basic on Agon. If you’re already familiar with agon_automate.py, it’s no different than entering python agon_automate.py direct “RUN“ 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).

The file name of the current file is passed automatically.

:AgonRun

The process is started with and requires no further parameters, after which the document is saved, transferred, loaded, and executed, or whatever your automation entails. Essentially, it’s the example script from my last post turned into a command in neovim with automatic passing of the file name of the current document from neovim.

AgonPaste

This is the real magic. You’ve transferred extensive 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, it’s better to use:

: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. In this case, not via agon_automate.py direct, but with the function script and a temporary file. This ensures that even more complex and longer texts, including multiple lines, are transmitted without error.

So, if you’re in the BBC Basic prompt (as if you were entering it yourself with the keyboard) and, for example, change line 50 in the code under vi, just go to line 50 and press yy (put the entire line in the buffer) and enter :AgonPaste. The exact line will then appear on Agon and be confirmed. And of course, you could also now enter :AgonCommand "RUN" in neovim to start the code.


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 *