Description of the page content

An S*BASIC preprocessor.


SBim stands for S*BASIC improved. It's an S*BASIC preprocessor written in the programming language of the Vim editor.

Main features:

At the moment, more details are available only in Spansih.

Source code

The main file of SBim in the converter:

" sbim.converter.vim
" (~/.vim/sbim.vim)

" This file is part of SBim

" Last modified 201709291146
" See change log at the end of the file

" ==============================================================
" Description

" This VimL program converts S*BASIC source code written in the
" SBim format to an ordinary S*BASIC file ready to be loaded by
" a S*BASIC interpreter.

" ==============================================================
" Author and license

" Author: Marcos Cruz (, 2011, 2012, 2015,
" 2016, 2017

" You may do whatever you want with this work, so long as you
" retain the copyright/authorship/acknowledgment/credit
" notice(s) and this license in all redistributed copies and
" derived works.  There is no warranty.

" ==============================================================

if exists("*SBim")
  " Function `SBim` is already defined.

" ==============================================================
" Generic functions {{{1

function! Trim(input_string)
  " Remove trailing spaces from a string.
  " Reference:
  return substitute(a:input_string, '^\s*\(.\{-}\)\s*$', '\1', '')

" ==============================================================
" Cleaning {{{1

function! SBimClean()

  silent %s,\t\+, ,ge " Remove tabs
  echo 'Tabs removed.'

  silent %s,\(^\s*\|\s\+\)'\(\s.*\)\?$,,e " Remove line comments
  echo 'Comments removed.'

  silent %s,^\s*\n,,ge " Remove the empty lines
  echo 'Empty lines removed.'

  silent %s,^\s*\(.\+\)\s*$,\1,e " Remove blanks
  echo 'Indentation and blanks removed.'

  silent %s,\\\s*\n,,e " Join the splitted lines 
  echo 'Splitted lines joined.'


" ==============================================================
" Renum

function! SBimRenum()

  " Put line numbers;
  " remove spaces from blank lines;
  " save the file.

  " Call the the nl program (part of the Debian coreutils package):
  execute ":silent %!nl --body-numbering=t --number-format=rn --number-width=5 --number-separator=' ' --starting-line-number=".s:firstLine." --line-increment=1"

  " In older versions of coreutils,
  " -v sets the first line number, and -i sets the line increment.
  " (the long option for -v doesn't work, though the manual mentions it).
  " Modern versions of nl uses the clearer options
  " --first-line and --line-increment, see:

  " Remove all spaces from lines that only contain spaces:

  " Remove spaces before line numbers
  "(nl has no option to remove them):
  silent %substitute/^\s*//e

  echo 'Line numbers added.'


function! SBimGetFirstLine()

  " Store into s:firstLine the first line number
  " to be used by the final S*BASIC program
  " The command #firstline can be used to set
  " the desired line number. Only the first occurence
  " of #firstline will be used; it can be anywhere
  " in the source but always at the start of a line
  " (with optional indentation).

  let s:firstLine=1 " default value

  " Go to the top of the file:
  normal gg
  if search('^\s*#firstline\s\+[0-9]\+\>','Wc')
    " Store the number into register 'l':
    normal ww"lyw
    " And then into the variable:
    let s:firstLine=getreg('l',1)
  echo 'First line number: '.s:firstLine


" ==============================================================
" #include {{{1

function! SBimInclude()

  " Execute all '#include' directives.

  " Syntax:
  " #include filename

  " Warning: nested including is possible, but no recursion check is made!

  call cursor(1,1) " Go to the top of the file.
  let l:includedFiles=0 " Counter
  while search('^\s*#include\s\+','Wc')
    let l:includedFiles += 1
    let l:filename=matchstr(getline('.'),'\S\+.*',8)
    call setline('.',"' <<< start of included file ".l:filename)
    call append('.',"' >>> end of included file ".l:filename)
    let l:filecontent=readfile(s:sourceFileDir.'/'.l:filename)
    call append('.',l:filecontent)

  if l:includedFiles==0
    echo 'No file included'
  elseif l:includedFiles==1
    echo 'One file included'
    echo l:includedFiles 'files included'


" ==============================================================


You can find the latest, under development version 2.0.0 of SBim in GitHub.

Related pages

Cross-development toolkit for MasterBASIC and SimCoupe.

External related links