Notes for Lisp in Vim Post
==========================


General Setup
-------------

### Create User in Remote Servers

    hostname=<HOSTNAME>

    [ -z "$hostname" ] && echo "error: hostname not set" >&2 && read -r hostname
    adduser susam --gecos '' --disabled-password
    adduser susam sudo
    echo "susam ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers.d/nopasswd
    echo "$hostname" > /etc/hostname
    echo 127.0.0.1 "$hostname" >> /etc/hosts
    reboot


### Create Helper Functions Locally

Edit `~/my/bin/env` and add these functions:


    sv() {
        ssh susam@172.105.52.133
    }

    vl() {
        ssh susam@172.105.49.220
    }

    deb9() {
        ssh susam@172.105.50.172
    }


### Upload SSH Keys from Local System

    ssh root@172.105.52.133 "umask 077; mkdir -p /home/susam/.ssh && echo $(cat ~/.ssh/id_rsa.pub) > /home/susam/.ssh/authorized_keys; chown -R susam:susam /home/susam/.ssh"

    ssh root@172.105.49.220 "umask 077; mkdir -p /home/susam/.ssh && echo $(cat ~/.ssh/id_rsa.pub) > /home/susam/.ssh/authorized_keys; chown -R susam:susam /home/susam/.ssh"

    ssh root@172.105.50.172 "umask 077; mkdir -p /home/susam/.ssh && echo $(cat ~/.ssh/id_rsa.pub) > /home/susam/.ssh/authorized_keys; chown -R susam:susam /home/susam/.ssh"


### Install Tools on Remote Servers

    sudo apt-get update
    sudo apt-get install tree


Slimv Setup
-----------

### Configuration Files

```
cat > ~/.tmux.conf <<eof
set -g prefix2 C-j
bind-key C-j send-prefix -2
bind '"' split-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
bind c new-window -c "#{pane_current_path}"
set -g escape-time 0
set -g default-terminal screen-256color
set -g mode-keys vi
eof

cat > ~/.vimrc <<eof
syntax on
colorscheme murphy
eof
```


### Install Slimv

    rm -rf ~/.vim/pack/plugins/start/slimv

    sudo apt-get -y install vim-nox sbcl tmux git

    git clone https://github.com/kovisoft/slimv.git ~/.vim/pack/plugins/start/slimv
    git -C ~/.vim/pack/plugins/start/slimv checkout -b pinned 47a0070
    vim +'helptags ~/.vim/pack/plugins/start/slimv/doc' +q


### Possible Errors

When `,c` is pressed in Vim to connect to Swank, the following error
appears:

    Vim is compiled without the Python feature or Python is not
    installed. Unable to run SWANK client. Press ENTER to continue.

To resolve this, enter:

    sudo apt-get install vim-nox

The next issue is:

    Enter Lisp path (or fill g:slimv_lisp in your vimrc):

To resolve this install a Common Lisp implementation:

    sudo apt-get install sbcl

The next issue is:

    No X server. Run Vim from screen/tmux or start SWANK server manually.
    Press ENTER to continue.

To resolve this, install tmux or GNU screen:

    sudo apt-get install tmux

or start Swank manually:

    sbcl --load ~/.vim/pack/plugins/start/slimv/slime/start-swank.lisp


Vlime Setup
-----------

### Configuration

```
cat > ~/.vimrc <<eof
syntax on
colorscheme murphy
eof
```


### Install Quicklisp Vlime

    sudo apt-get -y install vim sbcl git curl

    rm -rf ~/quicklisp ~/.vim/bundle/vlime ~/.vim/pack/plugins/start/paredit
    curl -O https://beta.quicklisp.org/quicklisp.lisp
    sbcl --load quicklisp.lisp --eval '(quicklisp-quickstart:install)' --eval '(exit)'
    sbcl --load ~/quicklisp/setup.lisp --eval '(ql:add-to-init-file)' --eval '(exit)'


### Install Vlime

    git clone https://github.com/l04m33/vlime.git ~/.vim/bundle/vlime
    git clone https://github.com/kovisoft/paredit ~/.vim/pack/plugins/start/paredit
    echo 'set runtimepath^=~/.vim/bundle/vlime/vim' >> ~/.vimrc
    git -C ~/.vim/bundle/vlime checkout -b pinned 065b95f
    git -C ~/.vim/pack/plugins/start/paredit checkout -b pinned d99905a
    vim +'helptags ~/.vim/bundle/vlime/vim/doc' +'helptags ~/.vim/pack/plugins/start/paredit/doc' +q


### Start Vlime Server Manually

    sbcl --load ~/.vim/bundle/vlime/lisp/start-vlime.lisp

Attempting to run Swank without Quicklisp leads to this error:

    debugger invoked on a VLIME::QUICKLISP-NOT-FOUND-ERROR in thread
    #<THREAD "main thread" RUNNING {10005D05B3}>:
      Quicklisp not found. Please set up Quicklisp or install the
      dependencies for VLIME manually.

### Tracing

When the `\TD` command is entered in Vlime, the following error occurs:

    SWANK-TRACE-DIALOG is not available.
    Error detected while processing function vlime#plugin#OpenTraceDialog:
    line   10:
    E716: Key not present in Dictionary: ReportSpecs
    Press ENTER or type command to continue

Resolving this requires adding the following configuration in
`~/.vimrc`:

    let g:vlime_contribs = ['SWANK-ASDF', 'SWANK-PACKAGE-FU',
                          \ 'SWANK-PRESENTATIONS', 'SWANK-FANCY-INSPECTOR',
                          \ 'SWANK-C-P-C', 'SWANK-ARGLISTS', 'SWANK-REPL',
                          \ 'SWANK-FUZZY', 'SWANK-TRACE-DIALOG']


Run Vlime with CLISP
--------------------

### Install Quicklisp

    sudo apt-get -y install clisp

    rm -rf ~/.clisprc.lisp ~/quicklisp quicklisp.lisp
    curl -O https://beta.quicklisp.org/quicklisp.lisp
    clisp -i quicklisp.lisp -x '(quicklisp-quickstart:install)'
    clisp -i ~/quicklisp/setup.lisp -x '(ql:add-to-init-file)'


### Start Vlime Server Manually

    clisp -i ~/.vim/bundle/vlime/lisp/start-vlime.lisp


### Start Vlime Server Automatically

Add the following code to `~/.vimrc`:

    let g:vlime_cl_impl = 'clisp'
    function! VlimeBuildServerCommandFor_clisp(vlime_loader, vlime_eval)
        return ['clisp', '-i', a:vlime_loader,
                       \ '-x', a:vlime_eval,
                       \ '-repl']
    endfunction


Run Vlime with ECL
------------------

### Install Quicklisp

    sudo apt-get -y install ecl

    rm -rf ~/.eclrc ~/quicklisp quicklisp.lisp
    curl -O https://beta.quicklisp.org/quicklisp.lisp
    ecl --load quicklisp.lisp --eval '(quicklisp-quickstart:install)' --eval '(quit)'
    ecl --load ~/quicklisp/setup.lisp --eval '(ql:add-to-init-file)' --eval '(quit)'


### Start Vlime Server Manually

    ecl --load ~/.vim/bundle/vlime/lisp/start-vlime.lisp


### Start Vlime Server Automatically

    let g:vlime_cl_impl = 'ecl'
    function! VlimeBuildServerCommandFor_ecl(vlime_loader, vlime_eval)
        return ['ecl', '--load', a:vlime_loader,
                     \ '--eval', a:vlime_eval]
    endfunction


Run Slimv with GCL
------------------

### Install GCL

    sudo apt-get -y install gcl

    gcl -load ~/.vim/pack/plugins/start/slimv/slime/start-swank.lisp


### Start Swank Server Manually (Failure)

    $ gcl -load ~/.vim/pack/plugins/start/slimv/slime/start-swank.lisp

    Error: Too few arguments are supplied to defmacro-lambda-list.
    Fast links are on: do (si::use-fast-links nil) for debugging
    Signalled by FUNCALL.
    Too few arguments are supplied to defmacro-lambda-list.

    Broken at FUNCALL.  Type :H for Help.
        1  Return to top level.
    SWANK-LOADER>>


### Start Swank Server Automatically

Slimv uses incorrect option to start Swank server. In slimv.vim:97, the
`cmd` variable is set to the following:

    "gcl" -i "/home/susam/.vim/pack/plugins/start/slimv/slime/start-swank.lisp"

As a result, GCL starts without running Swank server:

    GCL (GNU Common Lisp)  2.6.12 CLtL1    Fri Apr 22 15:51:11 UTC 2016
    Source License: LGPL(gcl,gmp), GPL(unexec,bfd,xgcl)
    Binary License:  GPL due to GPL'ed components: (XGCL READLINE UNEXEC)
    Modifications of this banner must retain notice of a compatible license
    Dedicated to the memory of W. Schelter

    Use (help) to get some basic information on how to use GCL.
    Temporary directory for compiler files:
    /tmp/

    >

Slimv times out with this error:

    SWANK server is not running. Press ENTER to continue.

Here is the fix for the above issue:

    --- a/ftplugin/lisp/slimv-lisp.vim
    +++ b/ftplugin/lisp/slimv-lisp.vim
    @@ -38,7 +38,7 @@ let g:slimv_lisp_loaded = 1
     let s:lisp_desc = [
     \ [ 'sbcl',         'sbcl',      '',    '' ],
     \ [ 'clisp',        'clisp',     '',    '' ],
    -\ [ 'gcl',          'clisp',     '',    '' ],
    +\ [ 'gcl',          'cmu',       '',    '' ],
     \ [ 'cmucl',        'cmu',       '',    '' ],
     \ [ 'ecl',          'ecl',       '',    '' ],
     \ [ 'acl',          'allegro',   '',    '' ],

With this, starting Swank server automatically leads to the same error
we get when we attempt to start Swank server manually:

    Error: Too few arguments are supplied to defmacro-lambda-list.
    Fast links are on: do (si::use-fast-links nil) for debugging
    Signalled by FUNCALL.
    Too few arguments are supplied to defmacro-lambda-list.

    Broken at FUNCALL.  Type :H for Help.
        1  Return to top level.
    SWANK-LOADER>>


Run Vlime with GCL
------------------

### Install Quicklisp (Failure)

    sudo apt-get -y install gcl

    rm -rf ~/quicklisp quicklisp.lisp
    curl -O https://beta.quicklisp.org/quicklisp.lisp
    gcl -load quicklisp.lisp -eval '(quicklisp-quickstart:install)' -eval '(quit)'
    gcl -load ~/quicklisp/setup.lisp -eval '(ql:add-to-init-file)' -eval '(quit)'

The following error occurs:

    Error: UNBOUND-VARIABLE :NAME #:CL-USER

The above error can be reproduced with `gcl -load quicklisp.lisp` too.


Run with MIT/GNU Scheme
-----------------------

### Install MIT/GNU Scheme

    rm -rf ~/.vim/pack/plugins/start/slimv

    sudo apt-get install vim-nox mit-scheme tmux git

    git clone https://github.com/kovisoft/slimv.git ~/.vim/pack/plugins/start/slimv
    git -C ~/.vim/pack/plugins/start/slimv checkout -b pinned 47a0070
    vim +'helptags ~/.vim/pack/plugins/start/slimv/doc' +q


### Run Swank Manually

    scheme --load ~/.vim/pack/plugins/start/slimv/slime/contrib/swank-mit-scheme.scm

### Run Vim

```
vim foo.scm
```

Connect to Swank by pressing `,c`.

Error on Debian 10.1 (MIT/GNU Scheme 10.1.5):

```
;The object #[package 12 (user)], passed as an argument to ->environment, is not an environment.
;To continue, call RESTART with an option number:
; (RESTART 1) => Return to read-eval-print level 1.
```

Works fine on Debian 9.11 (MIT/GNU Scheme 9.1.1.).


Run with Clojure (Debian/Default)
---------------------------------

### Install Clojure

    sudo apt-get install clojure


### Syntax Error

Attempting to start Swank server with Clojure manually leads to this
error:

    $ clojure -i ~/.vim/pack/plugins/start/slimv/swank-clojure/swank/swank.clj -e "(swank.swank/start-repl)" -r
    Syntax error (FileNotFoundException) compiling at (swank.clj:1:1).
    Could not locate swank/core__init.class, swank/core.clj or swank/core.cljc on classpath.
    user=>

Attempting to start it automatically also leads to the same error
because Slimv executes this command:

    clojure -i "/home/susam/.vim/pack/plugins/start/slimv/swank-clojure/swank/swank.clj" -e "(swank.swank/start-repl)" -r


### CompilerException

Prior to commit b052aab (18 Nov 2019), the following error used to
occur:

    $ CLASSPATH=~/.vim/pack/plugins/start/slimv/swank-clojure clojure -i ~/.vim/pack/plugins/start/slimv/swank-clojure/swank/swank.clj -e "(swank.swank/start-repl)" -r
    Syntax error (IllegalArgumentException) compiling new at (basic.clj:174:24).
    No matching ctor found for class clojure.lang.Compiler$CompilerException
    user=>


### Resolution

After this commit, the above command runs fine. Here is the command once
again:

    CLASSPATH=~/.vim/pack/plugins/start/slimv/swank-clojure clojure -i ~/.vim/pack/plugins/start/slimv/swank-clojure/swank/swank.clj -e "(swank.swank/start-repl)" -r


Run with Clojure (Debian/Maven)
-------------------------------

### Install Clojure

It takes about 3.5 to 4.5 minutes to build:

```
sudo apt-get install maven
git clone https://github.com/clojure/clojure.git ~/clojure
git -C ~/clojure checkout clojure-1.10.1
mvn -f ~/clojure/pom.xml -Plocal -Dmaven.test.skip=true package
java -jar ~/clojure/clojure.jar -e '(clojure-version)'
```

### Run Swank Manually

```
SWANK_DIR=~/.vim/pack/plugins/start/slimv/swank-clojure
java -cp "$HOME/clojure/clojure.jar:$SWANK_DIR" clojure.main -i "$SWANK_DIR/swank/swank.clj" -e '(swank.swank/start-repl)' -r
```

### Uninstall Clojure

```
rm -rf ~/.m2 ~/clojure
```
