<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.23">
<title>EmacsBgBuildMode</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="./rouge-github.css">
<link rel="stylesheet" href="./mlton.css">

</head>
<body class="article">
<div id="mlton-header">
<div id="mlton-header-text">
<h2>
<a href="./Home">
MLton
20241230
</a>
</h2>
</div>
</div>
<div id="header">
<h1>EmacsBgBuildMode</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>Do you really want to think about starting a build of you project?
What if you had a personal slave that would restart a build of your
project whenever you save any file belonging to that project?  The
bg-build mode does just that.  Just save the file, a compile is
started (silently!), you can continue working without even thinking
about starting a build, and if there are errors, you are notified
(with a message), and can then jump to errors.</p>
</div>
<div class="paragraph">
<p>This mode is not specific to MLton per se, but is particularly useful
for working with MLton due to the longer compile times.  By the time
you start wondering about possible errors, the build is already on the
way.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_functionality_and_features">Functionality and Features</h2>
<div class="sectionbody">
<div class="ulist">
<ul>
<li>
<p>Each time a file is saved, and after a user configurable delay
period has been exhausted, a build is started silently in the
background.</p>
</li>
<li>
<p>When the build is finished, a status indicator (message) is
displayed non-intrusively.</p>
</li>
<li>
<p>At any time, you can switch to a build process buffer where all the
messages from the build are shown.</p>
</li>
<li>
<p>Optionally highlights (error/warning) message locations in (source
code) buffers after a finished build.</p>
</li>
<li>
<p>After a build has finished, you can jump to locations of warnings
and errors from the build process buffer or by using the <code>first-error</code>
and <code>next-error</code> commands.</p>
</li>
<li>
<p>When a build fails, bg-build mode can optionally execute a user
specified command.  By default, bg-build mode executes <code>first-error</code>.</p>
</li>
<li>
<p>When starting a build of a particular project, a possible previous
live build of the same project is interrupted first.</p>
</li>
<li>
<p>A project configuration file specifies the commands required to
build a project.</p>
</li>
<li>
<p>Multiple projects can be loaded into bg-build mode and bg-build mode
can build a given maximum number of projects concurrently.</p>
</li>
<li>
<p>Supports both <a href="http://www.gnu.org/software/emacs/">Gnu Emacs</a> and
<a href="http://www.xemacs.org">XEmacs</a>.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_download">Download</h2>
<div class="sectionbody">
<div class="paragraph">
<p>There is no package for the mode at the moment.  To install the mode you
need to fetch the Emacs Lisp, <code>*.el</code>, files from the MLton repository:
<a href="https://github.com/MLton/mlton/tree/master/ide/emacs"><code>emacs</code></a>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_setup">Setup</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The easiest way to load the mode is to first tell Emacs where to find the
files.  For example, add</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="cl"><span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'load-path</span> <span class="p">(</span><span class="nv">file-truename</span> <span class="s">"path-to-the-el-files"</span><span class="p">))</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>to your <code>~/.emacs</code> or <code>~/.xemacs/init.el</code>.  You&#8217;ll probably also want
to start the mode automatically by adding</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="cl"><span class="p">(</span><span class="nb">require</span> <span class="ss">'bg-build-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nv">bg-build-mode</span><span class="p">)</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>to your Emacs init file.  Once the mode is activated, you should see
the <code>BGB</code> indicator on the mode line.</p>
</div>
<div class="sect2">
<h3 id="_mlton_and_compilation_mode">MLton and Compilation-Mode</h3>
<div class="paragraph">
<p>At the time of writing, neither Gnu Emacs nor XEmacs contain an error
regexp that would match MLton&#8217;s messages.</p>
</div>
<div class="paragraph">
<p>If you use Gnu Emacs, insert the following code into your <code>.emacs</code> file:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="cl"><span class="p">(</span><span class="nb">require</span> <span class="ss">'compile</span><span class="p">)</span>
<span class="p">(</span><span class="nv">add-to-list</span>
 <span class="ss">'compilation-error-regexp-alist</span>
 <span class="o">'</span><span class="p">(</span><span class="s">"^\\(Warning\\|Error\\): \\(.+\\) \\([0-9]+\\)\\.\\([0-9]+\\)\\.$"</span>
   <span class="mi">2</span> <span class="mi">3</span> <span class="mi">4</span><span class="p">))</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>If you use XEmacs, insert the following code into your <code>init.el</code> file:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="cl"><span class="p">(</span><span class="nb">require</span> <span class="ss">'compile</span><span class="p">)</span>
<span class="p">(</span><span class="nv">add-to-list</span>
 <span class="ss">'compilation-error-regexp-alist-alist</span>
 <span class="o">'</span><span class="p">(</span><span class="nv">mlton</span>
   <span class="p">(</span><span class="s">"^\\(Warning\\|Error\\): \\(.+\\) \\([0-9]+\\)\\.\\([0-9]+\\)\\.$"</span>
    <span class="mi">2</span> <span class="mi">3</span> <span class="mi">4</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">compilation-build-compilation-error-regexp-alist</span><span class="p">)</span></code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_usage">Usage</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Typically projects are built (or compiled) using a tool like <a href="http://www.gnu.org/software/make/"><code>make</code></a>,
but the details vary.  The bg-build mode needs a project configuration file to
know how to build your project.  A project configuration file basically contains
an Emacs Lisp expression calling a function named <code>bg-build</code> that returns a
project object.  A simple example of a project configuration file would be the
(<a href="https://github.com/MLton/mltonlib/blob/master/com/ssh/async/unstable/example/smlbot/Build.bgb"><code>Build.bgb</code></a>)
file used with smlbot:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="cl"><span class="p">(</span><span class="nv">bg-build</span>
 <span class="ss">:name</span>  <span class="s">"SML-Bot"</span>
 <span class="ss">:shell</span> <span class="s">"nice -n5 make all"</span><span class="p">)</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>bg-build</code> function takes a number of keyword arguments:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>:name</code> specifies the name of the project.  This can be any
expression that evaluates to a string or to a nullary function that
returns a string.</p>
</li>
<li>
<p><code>:shell</code> specifies a shell command to execute.  This can be any
expression that evaluates to a string, a list of strings, or to a
nullary function returning a list of strings.</p>
</li>
<li>
<p><code>:build?</code> specifies a predicate to determine whether the project
should be built after some files have been modified.  The predicate is
given a list of filenames and should return a non-nil value when the
project should be built and nil otherwise.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>All of the keyword arguments, except <code>:shell</code>, are optional and can be left out.</p>
</div>
<div class="paragraph">
<p>Note the use of the <code>nice</code> command above.  It means that background
build process is given a lower priority by the system process
scheduler.  Assuming your machine has enough memory, using nice
ensures that your computer remains responsive.  (You probably won&#8217;t
even notice when a build is started.)</p>
</div>
<div class="paragraph">
<p>Once you have written a project file for bg-build mode.  Use the
<code>bg-build-add-project</code> command to load the project file for bg-build
mode.  The bg-build mode can also optionally load recent project files
automatically at startup.</p>
</div>
<div class="paragraph">
<p>After the project file has been loaded and bg-build mode activated,
each time you save a file in Emacs, the bg-build mode tries to build
your project.</p>
</div>
<div class="paragraph">
<p>The <code>bg-build-status</code> command creates a buffer that displays some
status information on builds and allows you to manage projects (start
builds explicitly, remove a project from bg-build, &#8230;&#8203;) as well as
visit buffers created by bg-build.  Notice the count of started
builds.  At the end of the day it can be in the hundreds or thousands.
Imagine the number of times you&#8217;ve been relieved of starting a build
explicitly!</p>
</div>
</div>
</div>
</div>
<div id="mlton-footer">
<div id="mlton-footer-text">
<div>
Last updated Thu Oct 21 15:53:06 2021 -0400 by Matthew Fluet.
<a href="https://github.com/MLton/mlton/commits/master/doc/guide/src/EmacsBgBuildMode.adoc">Log</a>
<a href="https://github.com/MLton/mlton/edit/master/doc/guide/src/EmacsBgBuildMode.adoc">Edit</a>
</div>
</div>
</body>
</html>