← Back to team overview

zim-wiki team mailing list archive

Another custom tool

 

I'm using 8th (the Forth dialect) again, this time to give me the ability to embed code in a Zim page and then evaluate it in situ.

I can type in $[10 100 ^]$ , select that piece of text and then click on "Evalu8th" on the tool bar and end up with
$[10 100 ^]$
->10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000<-

The selection logic in Zim means I can double-click on the answer and not have the "->" and "<-" selected.

The EvaluateSelection.8th script looks like this:

   "\\$\\[([\\s\\S]*)\\]\\$" r:new var, pattern

   : main
        argc 0 = not if
            0 args \ -- s
            dup \ s -- s s'
            pattern @ \ s s' -- s s' r
            r:/ \ s s' r -- s s' a
            array? if
                0 a:@ \ s s' a -- s s' s
                eval \ s s' s -- s s' x
                nip \ s s' x -- s x
                swap \ s x -- x s
                . \ x s -- x
                "\n->" .
                . \ x --
                "<-" .
            then
        then
   ;

   main bye

and the config file for Zim is

   [Desktop Entry]
   Type=X-Zim-CustomTool
   Version=1.0
   GenericName=
   Name=Evalu8th
   Comment=Evaluate an inline 8th script
   Exec=
   TryExec=
   Icon=
   MimeType=
   Terminal=false
   NoDisplay=false
   X-Zim-ExecTool=c:\8th\bin\win64\8th.exe c:\bin\EvaluateSelection.8th
   "%t"
   X-Zim-ReadOnly=false
   X-Zim-ShowInToolBar=true
   X-Zim-ShowInContextMenu=Text
   X-Zim-ReplaceSelection=true

It doesn't have to be 8th. I can be Python, Lua, whatever. In another implementation (in JScript) I have the markup starting with ${js: and supporting a few different scripting languages.

Enjoy,
Bruce.