How to Add History to the REPL Config File

Those who do not remember the past are condemned to repeat it. - George Santayana

·

3 min read

How to Add History to the REPL Config File

If you played with pil21 a little bit, you might start to miss a function: If you close the REPL, all your commands are lost. Which means when you restart it, you need to type again. This can be very annoying for complicated commands, right?

It is not that the "history" function was forgotten in pil21, but rather that it's so customizable that there is no standard solution included. In this post I will explain how you can define it according to your own needs.


The rc.sample file

In the doc/ folder of your PicoLisp installation you can find a file called rc.sample. And in your home directory, you probably have a directory called .pil/. So as first step, move the rc.sample file to ~/.pil/ (~ is the home directory) and rename it to rc:

$ mv rc.sample ~/.pil/rc

This is the standard file content:

(history
   (make
      (skip "#")
      (while (line T) (link @))  # Global history
      (while (read) (eval @))  # Initial commands
      (when (info ".pilrc")  # Local history and commands
         (in @@
            (skip "#")
            (while (line T) (link @))
            (while (read) (eval @)) ) ) ) )

# Initial history
(stack)
(dbCheck)
(vi (car (url> *ID)))
(show *ID)

# Initial commands
(scl 2)

(de x ()
   (load "x.l") )

Now open a REPL somewhere on your system and try "arrow-up" or Ctrl-k. If everything worked well, you should see (show *ID) as first command. What happened?

When the REPL is opened, the interpreter reads in the commands defined in "Initial history" and "Initial commands". Then it is reading in the local history file .pilrc if it exits.


Setting the global *Bye variable

Now in order to keep our history after we closed the REPL, we need to modify our *Bye variable which contains all commands to be executed when the process ends.

Add the following line to the initial commands:

(push1 '*Bye '(out ".pilrc" (mapc prinl (history))))

With this, your history will be preserved in .pilrc when you are closing the session. Since the file is created for each directory, you can store a different history for each one of them.


Advantages of a local history

A local history makes sense if you are working in several different projects and expect to run different commands on each one of them. If you want to access some commands from every folder, then you can just store them to the "inital commands" section.

If you return to your project after some time, you don't need to be annoyed that your old history is overwritten, because it is still perfectly maintained as it was when you touched it the last time.


How to set a global history

But to be honest, I think the local history config is not for everyone (for example, not for me). Since I don't have all commands in my head yet, I'm often searching even very basic stuff from the REPL and then I find it very frustrating to not find it because I'm in the wrong folder. Also, the things I'm typing don't differ so much from project to project.

So here is a more beginner and memory friendly global history rc file. It is much shorter:

(history
   (when (info "~/.pil/pilrc")  # Local history and commands
      (make
         (in @@
            (while (line T) (link @)) ) ) ) )

# Initial commands
(push1 '*Bye '(out "~/.pil/pilrc" (mapc prinl (history))))

You can find both rc files here.


Sources

Photo by Cookie the Pom on Unsplash