Setup SBCL and SLIME development environment under Windows. Walkthrough

In this article I will describe how to install and configure SBCL and environments for productive development under Windows.

Actually, originally it was a small note to colleagues in planning my project for the analysis and visualization code and data labels, but I decided to Refine it and turn it into a full article, as I have seen few tutorials regarding this kind of Lisp, especially in Russian, especially concerning the Windows.



SBCL



First, we need the very Lisp-system.

SBCL for Windows is should be taken here:
github.com/akovalenko/sbcl-win32-threads

This fork of SBCL, supported and developed by Anton Kovalenko, adds threading support and fixes a huge number of windows-specific bugs of the official version under Windows. In addition, it added some very nice features, which in the main branch is not even actively support Linux versions. One of these features — stdcall-colbecki, as required on Windows, other — ability to work lipovich of Korbakov in external threads(not created by Lisp, but, for example, the operating system or third-party sysname, and not only sysname, libraries).

Fork is constantly synchronized with the main repository, and someday will eventually be smargin with him.

Personally, I regularly collect SBCL from source, directly from branch HEAD mswinmt, with MinGW, but on the wiki page the link also regularly uploaded. msi installers, so if you are too lazy to install and configure MinGW, you can download the binaries directly there.
github.com/akovalenko/sbcl-win32-threads/wiki

Offersyou under Windows should not be used completely for any reason, it is buggy and badly flawed.

After you have setup SBCL to install. I recommend to put it somewhere closer to the root of the file system, like in "D:\SBCL\" and not as proposed defaultno(in \program files\), for reasons that, first, MAX_PATH is not rubber, and secondly, it will help to avoid conflicts with third party libraries and utilities which don't like spaces in the file paths.

ASDF



After you install the Lisp runtime system, we need to configure ASDF.
ASDF is a system build lipovich libraries, and load them into the running Lisp process, like Make, written entirely in CL and not using third-party programs. More about it you can read here, but I'll describe the setup.

Actually ASDF is the de facto standard system of its kind in the environment the Lisp developers, and comes with almost all implementations of CL, and SBCL, so I skip the first phase, "where to take it"(if you suddenly do not, then you can download it on its home page at the link above, it's just one file).

The second step is to configure where ASDF searches for Lisp libraries.
Suppose we have a directory "D:\Lisp" where we have stored all sorts of things related to CL.
Suppose also that we have in this directory there is a subdirectory "libraries", which will contain libraries that are not associated with quicklisp(about it below).

Then we will have to define the environment variable CL_SOURCE_REGISTRY, and ask her the following value:
(:source-registry (:tree "D:/Lisp/libraries/") :inherit-configuration)
(environment variables in Windows 7, for example, configured in control panel -> system -> advanced system settings -> environment variables)

The second environment variable, which we will need to determine if we don't want the compiled fasl files can clog up the directory with the source code — ASDF_OUTPUT_TRANSLATIONS
It determines where will be the compile cache, and where to boot.
She needs to set the value like this:
(:output-translations (T ("D:/Lisp/fasl-cache/" :implementation)) :inherit-configuration)

Quicklisp



Now we need to install quicklisp.
Quicklisp is a package Manager for CL, which contains the most recent and most popular libraries. Very handy thing, too, like ASDF, in fact, is the de facto standard.

To install quicklisp is very simple — you need to download the file quicklisp.lisp and upload it in the image of a Lisp system, for example by typing (load "quicklisp.lisp") in REPL'e

After downloading the file, the REPL will appear:
==== quicklisp quickstart loaded ====

To continue, evaluate: (quicklisp-quickstart:install)


We therefore need to recruit (quicklisp-quickstart:install) in the REPL, and press "Enter".
In General, this function takes an optional named parameter :path, which specifies where to install the ql, but, in principle, default(%users%/%username%/quicklisp/) will be good.

After the installer quicklisp'and will download, install and load it into the Lisp listener process, you will need to type in REPL'e (ql:add-to-init-file) to the boot process ql in the image in the future being automated, and we don't have at the start of the Lisp system to load by hand.

To install or just download any of the libraries quicklisp provides a function ql:quickload. It really is very convenient — it automatically downloads, compiles and loads the library absolutely without any unnecessary efforts and movements. Says the author, the main advantage of quicklisp that it "just works."

In principle, we can already write the first program in Lisp under Windows.
Type in the REPL several forms:
the
(ql:quickload :cffi)

(cffi:load-foreign-library "user32.dll")

(defun main ()
(cffi:foreign-funcall "MessageBoxW"
:pointer (cffi:null-pointer)
(:string :encoding :utf-16le) "SBCL on Windows is easy!"
(:string :encoding :utf-16le) "Message"
:int #x40
:boolean))

(sb-ext:save-lisp-and-die "my-program.exe" :executable t :toplevel #'main)


Start my-program.exe check:


development Environment



About Emacs, probably heard everything.
Opinions differ about him — someone he may appear to be a useless toy for geeks, for someone that's clever, but outdated console editor from the ' 70s, but for someone — a daily tool.

What would you have an opinion about Emacs'e was not, one fact at the moment remains the fact — the extension for Emacs SLIME is the only thing at the moment, a complete and convenient development environment for Common Lisp.

Actually, I personally have enough experience with advanced IDE like Visual Studio, and I can assure you — for convenience SLIME from them have gone far, and maybe even, thanks to the flexibility of Emacs, and took the lead.

In General, Emacs at its core is the runtime of an old dialect of Lisp, Emacs Lisp, and its extensibility and flexibility, and maybe even the survival he owes to this fact.

Well, I digress.

First — "where to get yourself Emacs?". Here: ftp.gnu.org/pub/gnu/emacs/windows
Download the newest (at the moment this emacs-23.3-bin-i386.zip) and unpack the content of the directory emacs-23.3 somewhere like "D:\Emacs\"(by the way, because the original UNIX-based Emacs, there may be some problems with paths with spaces, so there advice is the same as when you install SBCL).

Once unpacked — run the program addpm.exe located in %Puttee%/bin is an optional step, it adds an icon in the start menu and such, but I still recommend it.

Now we need to download SLIME.

Take it here: common-lisp.net/project/slime
I personally prefer to update through CVS, but in principle, there is the link there is always a simple archive with the latest version.

SLIME directory to put in directory libraries, for which we have configured ASDF(in case you just should have written above, it will be D:\Lisp\libraries\)

Now about the setting.

To connect SLIME and bring Emacs'and more or less familiar to a Windows user, we need to write some things in his initialization file.
As I have said above, Emacs is essentially an interpreter for Emacs Lisp, this means, among other things, that its initialization file is a simple file with code on this very Emacs Lisp.

Is file if to speak about Windows, usually in the directory %users%\%username%\.emacs.d\, and called init.el(actually the extension that is the code for the mentioned dialect of Lisp). I, for example, this C:\Users\Lovesan\.emacs.d\init.el. If directory .emacs.d in the user directory is not present — create it and, accordingly, file.

Then I will give part of its init.el with some comments and explanations.

the
;; configure the encodings and the environment.
;; We want our code files were saved in UTF-8
;; while not explicitly specify another.
(set-language-environment 'UTF-8)
(setq default-buffer-file-coding-system 'utf-8-unix)

;; If you do not cancel make-backup-files Emacs will clog
;; file system backups.
(setq-default make-backup-files nil)

;; This includes auto save while you work with the file
;; Emacs from time to time automatically creates a copy and removes it
;; once you upload a file with which to work.
(setq-default auto-save-defaults t)

;; This turns off tabs. No matter how you treat tabs
;; in the context of the code of the other  programming  languages
;; in Lisp to work with them is absolutely impossible.
(setq-default indent-tabs-mode nil)

;; This makes Emacs a little more friendly to Windows users.
;; Specifically, the first line includes the regular Windows cursor for
;; text editor, and the second - Bindi hotkeys insert/clippings and
;; copy the standard Ctrl+V, Ctrl+X and Ctrl+C 
(setq w32-use-visible-system-caret t)
(cua-mode)

;; This configures the matching brackets highlighting, very useful thing
;; to avoid getting lost and seeing where the brackets are superfluous.
(show-paren-mode)

;; This sets an additional module highlight-parentheses
;; Useful stuff, illuminates the nested parentheses in a few levels.
;; You can download it here: http://www.emacswiki.org/emacs/HighlightParentheses
(byte-compile-file "d:/Emacs/lisp/highlight-parentheses.el")
(require 'highlight-parentheses)
(define-globalized-minor-mode global-highlight-parentheses-mode
highlight-parentheses-mode
(lambda ()
(highlight-parentheses-mode t)))
(global-highlight-parentheses-mode t)


;; Then the actual setting SLIME.
;; The first thing to do is to specify the path to the runtime Lisp implementations
;; with which we will work.
(setq inferior-lisp-program "D:/SBCL/sbcl.exe")

;; Next you need to specify where is SLIME.
(add-to-list 'load-path "D:/Lisp/libraries/slime-2011-10-27/")

;; Load SLIME
(require 'slime)

;; Set some encoding will be SLIME
;; communicate with the Lisp system
(setq slime-net-coding-system 'utf-8-unix)

;; Modules SLIME that we connect
;; Here referred to is, in my opinion, the basic minimum.
;; In the future, can do more to read about them
;; SLIME in the manual.
(slime-setup '(slime-repl
slime-fuzzy
slime-fancy-inspector
slime-indentation))

;; Set up hotkeys for auto-completion and leveling up indents
(define-key slime-mode-map (kbd "TAB")
'slime-indent-and-complete-symbol)
(define-key slime-mode-map (kbd "C-C TAB")
'slime-complete-symbol)

;; This includes leveling Lisp source code from SLIME,
;; in contrast to the standard Emacs
(setq lisp-indent-function 'common-lisp-indent-function)

;; Set the alignment of the indents under him
;; Learn more about customization of this case you can read
;; SLIME source, namely
;; %путь_к_slime%/contrib/slime-cl-indent.el
(define-common-lisp-style "my-indent-style"
"My custom indent style."
(:inherit "modern")
(:variables
(lisp-loop-indent-subclauses t)) 
(:indentation
(if (4 2 2))
(define (&lambda 2))
(with-gensyms ((&whole 4 &rest 1) &body))
(once-only (as with-gensyms))))

(setq common-lisp-style-default "my-indent-style")


In principle, you can start to work.
Run Emacs (from the start menu, or runemacs.exe in %puttee%/bin/).
Typed Alt+X and slime.
As soon as SBCL will accomplished and download lipovoy side of the SLIME(so called swank-server), there will be REPL SLIME.

You can, of course, to work completely in it, but better, of course, mostly work with files.

Here it is necessary to describe some hotkeys Emacs and SLIME to at least somewhere to start(for myself I know how easy this editor is to get lost, not knowing binding basic commands).

M-x command(i.e., function from Emacs Lisp) by name.

(Actually, C - means that while you should be pressing Ctrl, Alt - Alt, respectively. Why "M"? Because "Meta key", but because the key on the conventional keyboard, instead it uses Alt.)
C-x C-c — exit Emacs.

C-x b change buffer (a buffer is something like a file that is opened in Emacs, except for the fact that some buffer is not necessary with some file in the file system related).

C-x k close buffer.

C-c c-f — opens file existing or new

C-x s to save the file.

C-x C-s — save file without asking for overwrite.

C-x 0 -- close current window.

C-x 1 close all Windows except the current one.

C-x 2 to split the window into two vertically (in omaxe built-in ' tiling window Manager. Users of modern versions of Windows, it may seem unusual.)

C-x 3 to split the window into two, horizontally.

C-x o switch to other window

M-: — reading and evaluation of any expressions in the Emacs Lisp minibuffer.

In the SLIME REPL:

— the deployment of specific SLIME REPL command.

In the buffer of the file you are working with SLIME(usually files with the extension .lisp and the like):

M-. — jump to definition of any symbols/functions/etc.

M -, to jump back.

C-x C-e evaluate the expression

C-C C-k to compile and immediately load the file.

TAB — completion and alignment (we set up in init.el) (by the way, also works in the REPL)

C-C TAB — completion, just

auto-Completion and other similar capabilities within SLIME work based on the status of the connected Lisp system, and not just on the basis of the analysis of the code files. This means that SLIME could us something specific about the symbol/function/type/etc. to say we need to download a file or library that contains them, in Lisp-system through the REPL or something.

C-C C-d C-d — describe the character and everything associated with it (works like a standard describe)

C-C C-d h — see description of the symbol on CLHS

C-C C-d a — to find and describe the characters by their parts(works like a standard apropos)

In a Lisp expression is easy to navigate with arrows, while holding Ctrl — the cursor moves through the words and blocks of text, not the letters, and using C-M-b and C-M-f — moves the cursor to S-expressions.

That is, in principle, that's all. You can begin to write code.



the Upd. For the correct operation of SLIME it is desirable to set the environment variable HOME to the value corresponding to the path the user's home directory. I have Windows 7, for example, C:\Users\Lovesan

Article based on information from habrahabr.ru

Comments

Popular posts from this blog

Powershell and Cyrillic in the console (updated)

Active/Passive PostgreSQL Cluster, using Pacemaker, Corosync

Automatic deployment ElasticBeanstalk using Bitbucket Pipelines