📜 ⬆️ ⬇️

Configuring SBCL and SLIME development environment for Windows. Walkthrough

In this article I will describe in detail how to install and configure SBCL and the environment for productive development for Windows.

In general, initially it was a small note for a colleague on a project that I was planning to analyze and visualize code and text data, but I decided to modify it and turn it into a full-fledged article, since I had not seen such kind of manuals on lisp, especially in Russian, and especially concerning windows.


')

Sbcl



First we need the lisp system itself.

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

This fork of SBCL, supported and developed by Anton Kovalenko, adds support for multithreading and fixes a huge number of windows-specific bugs of the official version for Windows. In addition, some very nice features have been added to it, which are not found in the main branch even in actively supported Linux versions. One of these features is stdcall-callbacks, which are so necessary on Windows, the other is the ability of Lisp callbacks to work in third-party threads (not created by Lisp, but, for example, by the operating system or third-party shared libraries, and not only by libraries).

Fork is constantly synchronized with the main repository, and someday it will eventually be merged with it.

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

It is not necessary to use an offset for Windows for any reason, because it is buggy and badly flawed.

After you have an installer in your hands, SBCL should be installed. I recommend putting it somewhere closer to the root of the file system, like in “D: \ SBCL \”, and not as suggested by default (in \ program files \), for the reasons that, firstly, MAX_PATH is not rubber and secondly, it will help to avoid conflicts with third-party libraries and utilities that do not like spaces in the file paths.

ASDF



After installing runtime lisp-system, we should configure ASDF.
ASDF is such a system for building Lisp libraries, and loading them into a working Lisp process, like Make, written entirely in CL and not using third-party programs. You can read more about it here , but for now, I will describe its setting.

ASDF is actually a de facto standard system of its kind among Lisp developers, and comes with almost all CL implementations, including SBCL, so I’ll skip the first step, “where to get it” (if you suddenly don’t have it, then it can be downloaded on its homepage at the link above, it is literally one file).

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

Then we will need to define the CL_SOURCE_REGISTRY environment CL_SOURCE_REGISTRY , and set it to the following value:
(:source-registry (:tree "D:/Lisp/libraries/") :inherit-configuration)
(environment variables in Windows 7, for example, are configured in -> -> -> )

The second environment variable that we will need to determine if we do not want compiled fasl files to clutter up source directories - ASDF_OUTPUT_TRANSLATIONS
It determines where the compilation cache will go and where it will be loaded from.
She needs to set a value like this:
(:output-translations (T ("D:/Lisp/fasl-cache/" :implementation)) :inherit-configuration)
Thus, the fasl files will be in paths like "D:/Lisp/fasl-cache/sbcl-1.0.52.1.mswinmt.969-6acb698-win-x86/d/Lisp/libraries/my-library/src/package.fasl" and will not interfere in the source directories.

Quicklisp



Now we need to install quicklisp.
Quicklisp is a batch manager for CL, which contains the most recent and most popular libraries. A very handy thing, and also, like ASDF, is actually a de facto standard.

Installing quicklisp is very simple - you need to download the quicklisp.lisp file and load it into the lisp system image, for example, by typing (load "quicklisp.lisp") in the REPL

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

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


We, respectively, need to type (quicklisp-quickstart: install) in the REPL and press "Enter".
In general, this function takes an optional named parameter: path, which indicates where to install ql, but in principle, its default value (% users% /% username% / quicklisp /) is suitable for many.

After the quicklisp installer downloads, installs and loads it into the Lisp process, you will need to type in the REPL (ql:add-to-init-file) so that the process of loading ql into the image will be automated in the future and we will not I had to load it by hand every time I started the lisp system.

To install or simply download a library, quicklisp provides the ql:quickload . This is really very convenient - it automatically downloads, compiles and loads the libraries completely without any extra effort and gestures. As its author says, the main advantage of quicklisp is that it “just works.”

In principle, now you can write the first program on a Lisp under Windows.
Type several forms in the REPL:
 (ql:quickload :cffi) (cffi:load-foreign-library "user32.dll") (defun main () (cffi:foreign-funcall "MessageBoxW" :pointer (cffi:null-pointer) (:string :encoding :utf-16le) "SBCL  Windows  !" (:string :encoding :utf-16le) "" :int #x40 :boolean)) (sb-ext:save-lisp-and-die "my-program.exe" :executable t :toplevel #'main) 


Run my-program.exe and check:


Development environment



Everyone probably heard about Emacs.
Opinions about him diverge - to some, it may seem to be a useless toy for geeks, for someone it is an abstruse but outdated console editor from the 70s, and for someone it is a daily tool.

Whatever opinion you have about Emacs, one fact remains a fact at the moment — the SLIME extension for Emacs is the only complete and convenient Common Lisp development environment at the moment.

In general, I personally have enough experience with advanced IDEs like Visual Studio, and I can assure you that SLIME is not so far away from them for convenience, and maybe even thanks to the flexibility of Emacs, and took the lead.

In general, Emacs is in its essence the runtime of one old lisp dialect, Emacs Lisp, and its extensibility and flexibility, and maybe even survival, it is due to this fact.

Okay, I digress.

First, “where can I get Emacs?”. Here: ftp.gnu.org/pub/gnu/emacs/windows
Download the newest one (at the moment it is emacs-23.3-bin-i386.zip ) and unpack the contents of the emacs-23.3 directory somewhere like "D:\Emacs\" (by the way, due to the initial Unix-oriented nature of Emaks, some problems with paths with spaces, so the advice here is the same as when installing SBCL).

Once unpacked, we run the addpm.exe program, located in %__Emacs%/bin - this is an optional step, it adds an icon to the Start menu and the like, but I still recommend going through it.

Now we need to download SLIME.

Take it here: common-lisp.net/project/slime
I personally prefer to update via CVS, but, in principle, there is always a link there and a simple archive with a more or less recent version.

The SLIME directory needs to be put in the library directory for which we configured ASDF (if you followed the above directly, it would be D:\Lisp\libraries\ )

Now about the setting.

To connect SLIME and bring Emacs to a more or less familiar Windows user, we need to write some things in its initialization file.

As I said above, Emacs is essentially Emacs Lisp's interpreter, which means, among other things, that its initialization file is just a file with code on this very Emacs Lisp.

The file is located if we are talking about Windows, usually in the %users%\%username%\.emacs.d\ , and is called init.el (the actual extension indicates that this is code in the said dialect). For example, I have C:\Users\Lovesan\.emacs.d\init.el If there is no .emacs.d directory in the user's directory, create it and, accordingly, a file.

Further I will init.el part of my init.el with some comments and explanations.

 ;;    . ;;          UTF-8, ;;     . (set-language-environment 'UTF-8) (setq default-buffer-file-coding-system 'utf-8-unix) ;;    make-backup-files, Emacs   ;;   . (setq-default make-backup-files nil) ;;    -     , ;; Emacs      ,    ;;        . (setq-default auto-save-defaults t) ;;   .        ;;      , ;;       . (setq-default indent-tabs-mode nil) ;;   Emacs      Windows. ;; ,        ;;  ,   -   /  ;;    Ctrl+V, Ctrl+X  Ctrl+C (setq w32-use-visible-system-caret t) (cua-mode) ;;     ,    ;;     ,      . (show-paren-mode) ;;     highlight-parentheses ;;  ,      . ;;    : 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) ;;    SLIME. ;; ,   ,       , ;;     . (setq inferior-lisp-program "D:/SBCL/sbcl.exe") ;;   ,     SLIME. (add-to-list 'load-path "D:/Lisp/libraries/slime-2011-10-27/") ;;  SLIME (require 'slime) ;; ,     SLIME  ;;   - (setq slime-net-coding-system 'utf-8-unix) ;;  SLIME,    ;;   - ,   ,  . ;;         ;;   SLIME. (slime-setup '(slime-repl slime-fuzzy slime-fancy-inspector slime-indentation)) ;;        (define-key slime-mode-map (kbd "TAB") 'slime-indent-and-complete-symbol) (define-key slime-mode-map (kbd "Cc TAB") 'slime-complete-symbol) ;;        SLIME, ;;     Emacs (setq lisp-indent-function 'common-lisp-indent-function) ;;       ;;         ;;   SLIME,   ;;  %__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 begin to work.
Run Emacs (via the start menu, or runemacs.exe in %__emacs%/bin/ ).
Type Alt+X and type slime .
As soon as SBCL compiles and loads the Lisp side of SLIME (a so-called swank server), REPL SLIME appears.

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

Here it is necessary to describe some hotkeys Emacs and SLIME in order to start at least something (I know for myself how easy it is to get lost in this editor without knowing the binding of the main teams).

Mx is the invocation of a command (that is, to any Emacs Lisp function) by name.

(In general, C- means that during dialing Ctrl should be pressed, and M- respectively Alt. Why “M”? Because “Meta key” , but since this key is not on standard keyboards, Alt is used instead .)

Cx Cc - exit from Emacs.

C-xb is a buffer change (a buffer is something like a file that is open in Emacs, except that some buffer is not necessarily associated with some file in the file system).

Cx k - close the buffer.

Cc Cf - opens a file, existing or new

Cx s - save the file.

Cx Cs - save the file without asking for rewriting.

Cx 0 - close the current window.

Cx 1 - close all windows except the current one.

Cx 2 - split the window into two vertically (in the e-mail there is a built-in tiling window manager. It may seem unusual to users of modern versions of Windows.)

Cx 3 - split the window into two, horizontally.

Cx o - go to another window

M-: - read and evaluate any Emacs Lisp expression in the minibuffer.

In SLIME REPL:

, - enter a specific for SLIME REPL command.

In the file buffer with which SLIME works (as a rule, files with the .lisp extension and the like):

M-. - jump to the definition of any symbol / function / etc.

M-, jump back.

Cx Ce - calculate the expression

Cc Ck - compile and immediately download the file.

TAB - autocompletion and alignment (we configured it in init.el) (by the way, it also works in REPL)

Cc TAB - just autocompletion

Auto-completion and other similar functions in SLIME work based on the state of the connected system log, and not just on the basis of analyzing files with code. This means that in order for SLIME to be able to give us something specific about the symbol / function / type / etc. to say, we need to load the file or library containing them into the Lisp system, through the REPL or something else.

Cc Cd Cd - describe the character and everything associated with it (works like the standard describe )

Cc Cd h - see the symbol description on CLHS

Cc Cd a - find and describe characters by their part (works like standard apropos )

By lispovyh expressions it is convenient to move the arrows, while holding down the Ctrl - the cursor moves through the words and blocks of text, and not by letter, and with the help of CMb and CMf - the cursor moves through the S-expressions.

That's basically it. You can start writing code.



Upd. For SLIME to work correctly, it is highly desirable to set the HOME environment variable to match the path of the user's home directory. I, on Windows 7, for example, have this C:\Users\Lovesan

Source: https://habr.com/ru/post/131418/


All Articles