Endless Parentheses2019-08-21T18:17:48+00:00https://endlessparentheses.com/Artur Malabarbaartur@endlessparentheses.comWhat tests you shouldn’t write: an essay on negative tests2019-05-19T00:00:00+00:00https://endlessparentheses.com//what-tests-you-shouldn-t-write-an-essay-on-negative-tests<p>
Software tests are great! I’m fortunate enough to have only worked with
code-bases with reasonable-to-excellent test coverage, and I wouldn’t want to
work in a world without tests. In fact, a thoroughly tested system is nothing
short of liberating.
</p>
<p>
That said, tests are not free. I’m not talking about CI time, that is a cost but
it’s usually reducible. Nor am I referring to the effort it takes to write the
test, that’s a very real cost, but people are usually very mindful of that (it’s
easy to take it into account the very real effort you’re having right now).
</p>
<p>
The cost that people tend to underestimate is the time wasted with false
failures.
</p>
<p>
Let’s get the basics right. Tests are designed to fail. A test that never fails
under any circumstance is a useless test. But there are good failures and bad
failures. Which gets me to the entire point of this post.
</p>
<div id="outline-container-org77d9622" class="outline-2">
<h2 id="org77d9622">Write tests with real failures</h2>
<div class="outline-text-2" id="text-org77d9622">
<p>
Real failures are good, false failures are bad.<br />
You want a test to fail when you break functionality, not when you harmlessly
change code.
</p>
<p>
Let’s start with a quick Ruby example. Consider a model with an attribute called
<code>value</code>. It wouldn’t be surprising to see a test like this for such a model.
</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="n">it</span> <span class="p">{</span> <span class="n">is_expected</span><span class="p">.</span><span class="nf">to</span> <span class="n">respond_to</span><span class="p">(</span><span class="ss">:value</span><span class="p">)</span> <span class="p">}</span></code></pre></figure>
<p>
If you don’t know Rspec, this is roughly testing that you can call <code>.value</code> on
the model.
</p>
<p>
But when will this test ever fail?<br />
Under any reasonable condition, this will only fail if you rename the column in
the database. Nobody will ever do that by accident!
</p>
<p>
What’s worse, this failure will never carry any useful information. It doesn’t
tell the developer that this change is unexpectedly breaking something. All it
ever does is give us yet another piece of code to fix while in the middle of an
already long refactoring.
</p>
<p>
And how do we fix it? By editing the test to use the new name.
</p>
<p>
<b>A false failure is one that you fix by editing the test, while a real failure
is one you fix by editing the code.</b>
</p>
<p>
False failures are unavoidable. Every test is exposed to them, and every time they
happen the developer wastes some amount of time identifying and fixing the
failure. That is a negative cost that every test carries and not everyone takes
into account.
</p>
<p>
For most cases, we happily pay this cost because not having a test is way worse
than having to fix it. Because one real failure preventing a bug from going live
outweighs several false failures, adding up to a positive net effect.
</p>
<p>
But some tests (such as the example above) virtually never have real failures.
Without any positive upside to them, they are strictly <b>negative tests</b>.
</p>
<p>
And how do we avoid negative tests?
</p>
</div>
</div>
<div id="outline-container-orgfa6806d" class="outline-2">
<h2 id="orgfa6806d">Test function, not code</h2>
<div class="outline-text-2" id="text-orgfa6806d">
<p>
Let’s expand on our previous example.<br />
Rails provides a helpful one-liner to validate the presence of a mandatory
attribute.
</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="n">validates</span> <span class="ss">:value</span><span class="p">,</span> <span class="ss">presence: </span><span class="kp">true</span></code></pre></figure>
<p>
That’s fine and good. The problem is when you see a similar one-liner testing
that validation.
</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="n">it</span> <span class="p">{</span> <span class="n">is_expected</span><span class="p">.</span><span class="nf">to</span> <span class="n">validate_presence_of</span><span class="p">(</span><span class="ss">:value</span><span class="p">)</span> <span class="p">}</span></code></pre></figure>
<p>
Pause on that for a moment. We’ve written a spec to test a single line of code.
</p>
<p>
The only way that can fail is if we rename the attribute or remove the
validation. Again, nobody is ever going to do that by accident. We’re not <i>really</i>
testing that a specific functionality works as it should, we’re just testing that
a particular line of code is written in a particular way.
</p>
<p>
<b>That is a code test, not a function test, and code tests are negative tests.</b>
</p>
<p>
A function test is one that verifies non-trivial functionality, functionality that
could be <i>accidentally</i> broken by a number of reasons.
</p>
<p>
Testing the interface of a service, for instance, is basically always good. As
there’s usually at least a few branching code paths inside it where one could
inadvertently break a branch while editing another or while adding functionality.
</p>
<p>
Unit tests for simple functions and methods, in my opinion, are <i>not</i>
no-brainers. People like to go nuts with them, because they’re easy to write and
quick to run (so “why not?”), but a lot of them fall under the category of
negative tests.
</p>
<p>
Unit tests are good when testing some reasonably complicated algorithm, as
someone could actually break an edge case while trying to optimize the
implementation. And even then, you shouldn’t just write a couple of mindless tests,
as they will probably be negative. You should put some effort into figuring out
the edge cases and testing them specifically.
</p>
</div>
</div>
<div id="outline-container-orgfc11689" class="outline-2">
<h2 id="orgfc11689">Think before you test</h2>
<div class="outline-text-2" id="text-orgfc11689">
<p>
Hopefully, you started thinking well before you wrote that first line of code,
so there’s no reason to stop now just because you changed from the <code>app/</code> to the
<code>specs/</code> directory.
</p>
<p>
Thinking and being mindful of what you’re testing will not only help you avoid
negative tests, but will go a long way to making your positive tests more
effective at catching the bugs they’re supposed to catch.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/what-tests-you-shouldn-t-write-an-essay-on-negative-tests.html?source=rss#disqus_thread">Comment on this.</a></p>Mold Slack entirely to your liking with Emacs2017-10-09T23:43:00+00:00https://endlessparentheses.com//mold-slack-entirely-to-your-liking-with-emacs<p>
Although <a href="/keep-your-slack-distractions-under-control-with-emacs.html">fine-tuning your slack notifications</a> is already reason enough to run
slack in Emacs, that’s only the beginning. Once everything is up and running
<b>you</b> get to decide what you want out of your slack. Some of the snippets below
simply make up for missing functionality, other customize the package beyond
what you can do on the Slack Webapp.
</p>
<p>
Priorities first. The most important improvement you can implement is install
<a href="https://github.com/iqbalansari/emacs-emojify">emojify-mode</a> and turn it on for slack chats.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'slack-mode-hook</span> <span class="nf">#'</span><span class="nv">emojify-mode</span><span class="p">)</span></code></pre></figure>
<p>
Secondly, make sure you customize the chat faces to your liking. Just open a
chat buffer, place your cursor on a piece of text whose face you want to
customize, and call <a href="https://doc.endlessparentheses.com/Fun/customize-face"><code>customize-face</code></a>.
</p>
<p>
In order to keep track of new messages in the mode-line, slack.el uses a package
called tracking, which is the same one <a href="https://github.com/jorgenschaefer/circe">circe</a> uses for IRC chats. The command
<a href="https://doc.endlessparentheses.com/Fun/tracking-next-buffer"><code>tracking-next-buffer</code></a> is a fantastic way to cycle through your pending
messages, bind it to something short.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">with-eval-after-load</span> <span class="ss">'tracking</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">tracking-mode-map</span> <span class="nv">[f11]</span>
<span class="nf">#'</span><span class="nv">tracking-next-buffer</span><span class="p">))</span>
<span class="c1">;; Ensure the buffer exists when a message arrives on a</span>
<span class="c1">;; channel that wasn't open.</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">slack-buffer-create-on-notify</span> <span class="no">t</span><span class="p">)</span></code></pre></figure>
<p>
I’ll never know who thought user statuses were a good idea for Slack. But, thanks
to a tip by <a href="https://news.ycombinator.com/item?id=15277244">_asummers on HackerNews</a>, I can live in a world where they don’t exist.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">slack-user-status</span> <span class="p">(</span><span class="nv">_id</span> <span class="nv">_team</span><span class="p">)</span> <span class="s">""</span><span class="p">)</span></code></pre></figure>
<p>
I like notifications with minimal titles, and the package is kind enough to make
these configurable.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;;; Channels</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">slack-message-notification-title-format-function</span>
<span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">_team</span> <span class="nb">room</span> <span class="nv">threadp</span><span class="p">)</span>
<span class="p">(</span><span class="nv">concat</span> <span class="p">(</span><span class="k">if</span> <span class="nv">threadp</span> <span class="s">"Thread in #%s"</span><span class="p">)</span> <span class="nb">room</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/-cleanup-room-name</span> <span class="p">(</span><span class="nv">room-name</span><span class="p">)</span>
<span class="s">"Make group-chat names a bit more human-readable."</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span>
<span class="s">"--"</span> <span class="s">" "</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span> <span class="s">"#mpdm-"</span> <span class="s">""</span> <span class="nv">room-name</span><span class="p">)))</span>
<span class="c1">;;; Private messages and group chats</span>
<span class="p">(</span><span class="k">setq</span>
<span class="nv">slack-message-im-notification-title-format-function</span>
<span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">_team</span> <span class="nb">room</span> <span class="nv">threadp</span><span class="p">)</span>
<span class="p">(</span><span class="nv">concat</span> <span class="p">(</span><span class="k">if</span> <span class="nv">threadp</span> <span class="s">"Thread in %s"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">endless/-cleanup-room-name</span> <span class="nb">room</span><span class="p">))))</span></code></pre></figure>
<p>
Slack.el uses lui for the chat buffers. If you, like me, are a <a href="/ispell-and-abbrev-the-perfect-auto-correct.html">heavy user of
abbrevs</a> in Emacs, you’ll find it annoying that the final word of each message
won’t get expanded unless you explicitly hit <kbd>SPC</kbd> before <kbd>RET</kbd>. That’s easy to
remedy with an advice.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">advice-add</span> <span class="nf">#'</span><span class="nv">lui-send-input</span> <span class="ss">:before</span>
<span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="k">&rest</span> <span class="nv">_</span><span class="p">)</span>
<span class="p">(</span><span class="nb">ignore-errors</span> <span class="p">(</span><span class="nv">expand-abbrev</span><span class="p">))))</span></code></pre></figure>
<p>
<del>Finally, the biggest missing feature from this package is that it displays the
author on every message output.</del><br />
Never mind, this feature has now been implemented by the package author!
</p>
<p>
You don’t have to stop here, of course. Want to fine-tune which buffers get
tracked on the mode-line? Hack into <code>tracking.el</code>. Want to change the face used
for your own messages, or even align them to the right? Redefine
<code>slack-buffer-insert</code>. Your workflow is yours to build.
</p>
<div id="outline-container-org9e21605" class="outline-2">
<h2 id="org9e21605">Update <span class="timestamp-wrapper"><span class="timestamp">17 mar 2019</span></span></h2>
<div class="outline-text-2" id="text-org9e21605">
<p>
Noted that the message-merging is now part of the package.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/mold-slack-entirely-to-your-liking-with-emacs.html?source=rss#disqus_thread">Comment on this.</a></p>Turbo up your Ruby console in Emacs2017-10-02T20:57:00+00:00https://endlessparentheses.com//turbo-up-your-ruby-console-in-emacs<p>
Keeping a REPL (or a console) always by your side is never a bad habit, and if
you use an IDE-package (like <a href="https://github.com/dgutov/robe">Robe</a> for Ruby, or <a href="https://github.com/clojure-emacs/cider/">Cider</a> for Clojure) it’s nigh
unavoidable. Being an essential part of your environment, it would be ridiculous
not to invest some time optimizing it.
</p>
<p>
One obvious optimization is to bind a key to your “start console” command, but
that’s just the start. You pretty much never need two running consoles for the
same project, so why not have the same key switch to it if it’s already running?
</p>
<p>
But we can go a bit farther with very little work. I have a file where I define
a lot of small helper methods for my Ruby console, so let’s require it
automatically whenever a new console is started.
</p>
<figure class="highlight"><pre><code class="language-emacs-lisp" data-lang="emacs-lisp"><span class="p">(</span><span class="nv">defcustom</span> <span class="nv">endless/ruby-extensions-file</span>
<span class="s">"../console_extensions.rb"</span>
<span class="s">"File loaded when a ruby console is started.
Name is relative to the project root."</span><span class="p">)</span>
<span class="c1">;; Skip ENV prompt that shows up in some cases.</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">inf-ruby-console-environment</span> <span class="s">"development"</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/run-ruby</span> <span class="p">()</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nb">require</span> <span class="ss">'inf-ruby</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">default-directory</span> <span class="p">(</span><span class="nv">projectile-project-root</span><span class="p">))</span>
<span class="p">(</span><span class="nv">was-running</span> <span class="p">(</span><span class="nv">get-buffer-process</span> <span class="nv">inf-ruby-buffer</span><span class="p">)))</span>
<span class="c1">;; This function automatically decides between starting</span>
<span class="c1">;; a new console or visiting an existing one.</span>
<span class="p">(</span><span class="nv">inf-ruby-console-auto</span><span class="p">)</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nb">not</span> <span class="nv">was-running</span><span class="p">)</span>
<span class="p">(</span><span class="nv">get-buffer-process</span> <span class="p">(</span><span class="nv">current-buffer</span><span class="p">))</span>
<span class="p">(</span><span class="nv">file-readable-p</span> <span class="nv">endless/ruby-extensions-file</span><span class="p">))</span>
<span class="c1">;; If this brand new buffer has lots of lines then</span>
<span class="c1">;; some exception probably happened.</span>
<span class="p">(</span><span class="nv">send-string</span>
<span class="p">(</span><span class="nv">get-buffer-process</span> <span class="p">(</span><span class="nv">current-buffer</span><span class="p">))</span>
<span class="p">(</span><span class="nv">concat</span> <span class="s">"require '"</span> <span class="nv">endless/ruby-extensions-file</span>
<span class="s">"'\n"</span><span class="p">)))))</span>
<span class="c1">;; CIDER users might recognize this key.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">ruby-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-c M-j"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">endless/run-ruby</span><span class="p">)</span></code></pre></figure>
<p>
If you use Projectile and want to go even faster, check out the <kbd>j</kbd> key
on <a href="/improving-projectile-with-extra-commands.html">my post about Projectile</a>.
</p>
<p><a href="https://endlessparentheses.com/turbo-up-your-ruby-console-in-emacs.html?source=rss#disqus_thread">Comment on this.</a></p>It’s Magit! And you’re the magician!2017-09-20T00:00:00+00:00https://endlessparentheses.com//it-s-magit-and-you-re-the-magician<p>
There’s nothing I can praise about <a href="https://magit.vc/">Magit</a> that hasn’t been written in a dozen
blogs already, but since Jonas started a <a href="https://www.kickstarter.com/projects/1681258897/its-magit-the-magical-git-client">kickstarter campaign</a> for it I knew I
had to say something. If you use Magit, you already know the greatness of it.
And if you don’t, hopefully I can convince you to try it in time to back the
campaign.
</p>
<p>
I could go on and on about the virtues of this gem. It’s probably the package
that most saves me time, and has taught me more about git than I learned reading
a whole book on it. But that’s all just sparks and glitter on top of the real
show. For Magit is a magic show, and its real feature is making <i>you</i> the
magician.
</p>
<p>
Controlling Magit feels like putting on a performance. Move your fingers quickly
enough, and you’ll be rebasing faster than the eye can see. But if your crowd is
not impressed yet, you move on to tagging, cherry-picking, rewriting, reflogging
until they’re left staring unblinkingly at your monitor, their jaws unknowingly
open in awe.
</p>
<p>
But, if you’re one of those that only cares about practical benefits, then here
are few Magit commands I use just about everyday.
</p>
<dl class="org-dl">
<dt><kbd>f a</kbd> Fetch all remotes</dt><dd>Usually how I start my day. Updates information
about all remote branches, without messing with your local branch. Really
helps that it’s a one-handed combo, so I can do it while sipping the
morning coffee.</dd>
<dt><kbd>r u</kbd> Rebase upstream</dt><dd>Upstream usually means <code>origin/master</code>. If fetch-all
indicates that my local branch is out of date with upstream, this will
quickly bring it up to date. If I don’t want to rebase this branch, I can
<kbd>m m</kbd> and merge instead. Since fetch-all has already done all of the slow
networking, both merge and rebase are super quick.</dd>
<dt><kbd>b s</kbd> Branch spin-off</dt><dd>Creates and checks out a new branch, while carrying
over any ongoing (and possibly committed) changes. It even undoes the
commits on the previous branch. Super useful if you realise you forgot to
branch-off from master before starting to work.</dd>
<dt><kbd>b c</kbd> Branch create</dt><dd>Over time I’ve been gradually using less <kbd>b s</kbd> and
more <kbd>b c</kbd>. When I need a new working branch, instead of having to switch
to master, pull origin, and then spin-off, I simply <kbd>b c</kbd> straight from
<code>origin/master</code> (it never really matters if my local <code>master</code> is outdated).</dd>
<dt><kbd>P …</kbd> Push!</dt><dd>Feels silly that pushing is so far down my list, but here you
go. <kbd>P p</kbd> is 95% of my usage, but <kbd>P t</kbd> is also useful and
you’ll occasionally need a <kbd>-f</kbd> in there.</dd>
<dt><kbd>c …</kbd> Commit powers</dt><dd><kbd>c c</kbd> is your basic commit. But I couldn’t live without
<kbd>c w</kbd> (for quickly fixing a commit message) or <kbd>c e</kbd> (for throwing more
changes into the last commit).</dd>
<dt><kbd>l l</kbd> Log current</dt><dd>Magit is pretty good at keeping you informed of the
commit difference between local and remote, but <kbd>l l</kbd> is a quick way to get
your bearings if you ever feel lost.</dd>
<dt><kbd>y</kbd> Branch viewer</dt><dd>Concise list of all refs. Great for finding and removing
old branches with <kbd>k</kbd> (both local and remote). Magit even warns if you try
to delete a branch that isn’t merged.</dd>
</dl>
<p>
And a few commands I don’t really use everyday, but rather eagerly look forward
to the next chance to use them.
</p>
<dl class="org-dl">
<dt><kbd>l r</kbd> Reflog</dt><dd>Don’t know what reflog is? Neither did I! Magit taught me
about reflog without saying a word, and you should learn it
too (it feels like a superpower).</dd>
<dt><kbd>r s</kbd> Rebase subset</dt><dd>At work, when I need to hotfix something I branch-off
from <code>origin/production</code> instead of <code>origin/master</code>. Occasionally though,
I’ll forget to do that, commit my hotfix, only to realise my branch is now
100 commits ahead of production. Rebase subset solves this is a blink, by
taking only the commit I want and placing it on top of another branch.</dd>
<dt><kbd>r i</kbd> Rebase interactively</dt><dd>I admit, I was a little scared when I first tried
this feature. I had no idea what it did, but that “interactively” menu
option had been teasing me for months. If I explain it here I feel like I’d
be robing you of the discovery, so I’ll just describe it as “omnipotence
over history”.</dd>
</dl>
<p>
All of this arsenal combines into some fast and powerful git manipulation. Just
yesterday a colleague asked for help on one of his branches. The whole checkout,
edit, commit, push process took less than 30 seconds. One more <kbd>b b</kbd> and I was
back in my branch doing my own stuff.
</p>
<p>
What’s more, it all <i>feels</i> right. My editor is where I manipulate the source
code, that’s where I ought to be changing branches, not alt-tabbing to a terminal.
</p>
<p><a href="https://endlessparentheses.com/it-s-magit-and-you-re-the-magician.html?source=rss#disqus_thread">Comment on this.</a></p>Keep your Slack distractions under control with Emacs2017-09-18T00:00:00+00:00https://endlessparentheses.com//keep-your-slack-distractions-under-control-with-emacs<p>
There’s no denying slack is a useful tool for intrateam communication, but it’s
also a powerful source of distractions. Though I can’t just turn it off all
day, I can certainly keep the spam in check.
</p>
<p>
Slack’s Webapp does allow you to partially mute certain channels, but that’s
about as far as it goes. On the other hand, with the power of Emacs and the
<a href="https://github.com/jwiegley/alert">Alert</a> package, we can perfectly filter out anything we don’t care about.
</p>
<p>
Start by installing the <a href="https://github.com/yuya373/emacs-slack">Slack</a> package from Melpa. It takes a bit of effort to
get it set up, but once you’re done <a href="https://github.com/yuya373/emacs-slack#how-to-get-token">following the instructions</a> you should have a
(surprisingly featureful) Slack client running inside Emacs.
</p>
<p>
I’ll probably write further posts on how you can perfect your workflow with this
package, but for now we can just go over some basic keybinds.
</p>
<figure class="highlight"><pre><code class="language-emacs-lisp" data-lang="emacs-lisp"><span class="c1">;;; Big QOL changes.</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">slack-completing-read-function</span>
<span class="nf">#'</span><span class="nv">ido-completing-read</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">slack-buffer-function</span> <span class="nf">#'</span><span class="nv">switch-to-buffer</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">slack-prefer-current-team</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">slack-display-team-name</span> <span class="no">nil</span><span class="p">)</span>
<span class="c1">;;; Go to any channel with `C-x j'.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">ctl-x-map</span> <span class="s">"j"</span> <span class="nf">#'</span><span class="nv">slack-select-rooms</span><span class="p">)</span>
<span class="c1">;;; Quick 'n dirty way of opening the most recent link</span>
<span class="c1">;;; in the current chat room.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">slack-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"M-o"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">kbd</span> <span class="s">"<backtab> RET M->"</span><span class="p">))</span>
<span class="c1">;;; I thumbs-up a lot. Don't judge me.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">slack-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-;"</span><span class="p">)</span> <span class="s">":+1:"</span><span class="p">)</span>
<span class="c1">;;; Bring up the mentions menu with `@', and insert a</span>
<span class="c1">;;; space afterwards.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">slack-mode-map</span> <span class="s">"@"</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/slack-message-embed-mention</span> <span class="p">()</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">call-interactively</span> <span class="nf">#'</span><span class="nv">slack-message-embed-mention</span><span class="p">)</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">" "</span><span class="p">)))</span>
<span class="c1">;;; Pretty straightforward.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">slack-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-c C-d"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">slack-message-delete</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">slack-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-c C-e"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">slack-message-edit</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">slack-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-c C-k"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">slack-channel-leave</span><span class="p">)</span></code></pre></figure>
<p>
Now you might think I’ve got it all backwards. Connecting Emacs with Slack could
only bring the distractions closer to me. But that’s where Alert comes in.
The Slack package automatically uses Alert for sending notifications, so you
have full control over them by customizing <code>alert-user-configuration</code>.
</p>
<p>
That’s super easy to do via the customization interface (<kbd>M-x</kbd>
<code>customize-variable</code>). But the examples below use plain Elisp. Just keep in mind
that the first element is an alist determining which messages to match, and the
second element is a symbol specifying what to do (the third is not important
here).
</p>
<p>
For instance, let’s start by telling alert <b>not</b> to notify anything. Sounds
blissful, doesn’t it?
</p>
<figure class="highlight"><pre><code class="language-emacs-lisp" data-lang="emacs-lisp"><span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'alert-user-configuration</span>
<span class="o">'</span><span class="p">(((</span><span class="ss">:category</span> <span class="o">.</span> <span class="s">"slack"</span><span class="p">))</span> <span class="k">ignore</span> <span class="no">nil</span><span class="p">))</span></code></pre></figure>
<p>
There are a couple of important channels I’d like to be notified about
anything, so add a rule for them.
</p>
<figure class="highlight"><pre><code class="language-emacs-lisp" data-lang="emacs-lisp"><span class="p">(</span><span class="nv">add-to-list</span>
<span class="ss">'alert-user-configuration</span>
<span class="o">'</span><span class="p">(((</span><span class="ss">:title</span> <span class="o">.</span> <span class="s">"\\(bigchannel\\|hugechannel\\)"</span><span class="p">)</span>
<span class="p">(</span><span class="ss">:category</span> <span class="o">.</span> <span class="s">"slack"</span><span class="p">))</span>
<span class="nv">libnotify</span> <span class="no">nil</span><span class="p">))</span></code></pre></figure>
<p>
Then there are a few channels where I only need to pay attention if explicitly
mentioned.
</p>
<figure class="highlight"><pre><code class="language-emacs-lisp" data-lang="emacs-lisp"><span class="p">(</span><span class="nv">add-to-list</span>
<span class="ss">'alert-user-configuration</span>
<span class="o">'</span><span class="p">(((</span><span class="ss">:message</span> <span class="o">.</span> <span class="s">"@artur\\|Artur"</span><span class="p">)</span>
<span class="p">(</span><span class="ss">:title</span> <span class="o">.</span> <span class="s">"\\(okchannel\\|sosochannel\\)"</span><span class="p">)</span>
<span class="p">(</span><span class="ss">:category</span> <span class="o">.</span> <span class="s">"slack"</span><span class="p">))</span>
<span class="nv">libnotify</span> <span class="no">nil</span><span class="p">))</span></code></pre></figure>
<p>
Both of the rules above are more-or-less supported by Slack already (although
you can’t <b>really</b> mute channels completely without leaving them). Below is one
example where Alert really shines.
</p>
<p>
We use Rollbar for exception tracking, and I like being notified whenever
something explodes in the server. However, it makes no sense to notify me
whenever someone resolves an issue. That can be resolved with two short rules.
</p>
<figure class="highlight"><pre><code class="language-emacs-lisp" data-lang="emacs-lisp"><span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'alert-user-configuration</span>
<span class="o">'</span><span class="p">(((</span><span class="ss">:title</span> <span class="o">.</span> <span class="s">"rollbar"</span><span class="p">)</span>
<span class="p">(</span><span class="ss">:category</span> <span class="o">.</span> <span class="s">"slack"</span><span class="p">))</span>
<span class="nv">libnotify</span> <span class="no">nil</span><span class="p">))</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'alert-user-configuration</span>
<span class="o">'</span><span class="p">(((</span><span class="ss">:message</span> <span class="o">.</span> <span class="s">"Resolved by"</span><span class="p">)</span>
<span class="p">(</span><span class="ss">:title</span> <span class="o">.</span> <span class="s">"rollbar"</span><span class="p">)</span>
<span class="p">(</span><span class="ss">:category</span> <span class="o">.</span> <span class="s">"slack"</span><span class="p">))</span>
<span class="k">ignore</span> <span class="no">nil</span><span class="p">))</span></code></pre></figure>
<p>
I have 4 other rules similar to this one. This kind of fine-grained control is
great for reducing spam while staying aware of what matters.
</p>
<p>
Overall, I’m really happy with the setup I’ve got, and I’ll try to post about
other aspects of it.
</p>
<p><a href="https://endlessparentheses.com/keep-your-slack-distractions-under-control-with-emacs.html?source=rss#disqus_thread">Comment on this.</a></p>Emacs 25 is out! What are the new features and what were my predictions2016-10-11T00:00:00+00:00https://endlessparentheses.com//emacs-25-is-out-what-are-the-new-features-and-what-were-my-predictions<p>
Four Saturdays ago, on September 17, <a href="https://lists.gnu.org/archive/html/emacs-devel/2016-09/msg00451.html">Emacs 25 was finally released</a>. Almost two
years before that, I wrote a post predicting a few <a href="/big-things-to-expect-from-emacs-25.html">Big things to expect from
Emacs 25</a>. Throughout the months since then, I’ve also been reporting interesting
new features as they arrived on the dev builds. Today, we compile a list of all
of those news posts, and review which predictions I actually got right.
</p>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">News posts</h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
For the convenience of those looking for actually practical information, I feel
we should start with a list of the posts. I must strive to be clear that this
is <b>not a comprehensive list</b> of new features. Not by a mile! If you want
everything, you can have a look at the news file that ships with Emacs,
<code>/etc/NEWS</code>, or you can read Mickey Petersen’s <a href="https://www.masteringemacs.org/article/whats-new-in-emacs-25-1">commented version</a>.
</p>
<p>
The list below merely sums up most (not even all) of the features that piqued
my interest over the last couple of years.
</p>
<ol class="org-ol">
<li><a href="/new-in-emacs-25-1-easily-search-non-ascii-characters.html">Easily search for non-ASCII characters</a> (char-folding search)</li>
<li><a href="/new-in-emacs-25-1-round-quotes-in-help-buffers.html">Round quotes in Help buffers</a></li>
<li><a href="/new-in-emacs-25-1-query-replace-history-is-enhanced.html">Query-replace history is enhanced</a></li>
<li><a href="/new-in-emacs-25-1-comment-line.html">The <code>comment-line</code> command</a></li>
<li><a href="/new-on-elpa-and-in-emacs-25-1-seq-el.html">The <code>seq.el</code> library</a></li>
<li><a href="/new-on-elpa-and-in-emacs-25-1-let-alist.html">The <code>let-alist</code> library</a></li>
<li><a href="/new-in-emacs-25-1-map-el-library.html">The <code>map.el</code> library</a></li>
<li><a href="/new-in-emacs-25-1-have-prettify-symbols-mode-reveal-the-symbol-at-point.html">Have prettify-symbols-mode reveal the symbol at point</a></li>
<li><a href="/new-in-emacs-25-1-more-flow-control-macros.html">More flow control macros</a></li>
<li><a href="/new-in-emacs-25-1-eww-improvements.html">EWW improvements</a></li>
<li><a href="/new-in-emacs-25-1-easily-install-multifile-package-from-a-directory.html">Easily install multifile package from a directory</a></li>
<li><a href="/new-in-emacs-25-1-better-rectangles.html">Better Rectangles</a></li>
<li><a href="/new-in-package-el-in-emacs-25-1-better-dependency-management.html">Better dependency management</a></li>
<li><a href="/new-in-package-el-in-emacs-25-1-user-selected-packages.html">User-selected packages</a></li>
<li><a href="/new-in-emacs-25-1-asynchronous-package-menu.html">Asynchronous Package Menu</a></li>
<li><a href="/new-in-emacs-25-1-filtering-by-status-and-archive.html">Filtering by status and archive</a></li>
<li><a href="/new-in-emacs-25-1-archive-priorities-and-downgrading-packages.html">Archive priorities and downgrading packages</a></li>
</ol>
<p>
If you’re short on time, I personally recommend checking out char-folding
search (1), archive priorities (17), and the <code>comment-line</code> command (4), which are all
features you might not realize are there. The round-quotes in Help buffers (2) and
the improved query-replace history (3) are also among my favorites, but you don’t
need to do anything special to see them in action.
</p>
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">Reviewing predictions</h2>
<div class="outline-text-2" id="text-orgheadline2">
<p>
Now, for the indulgence of those looking for more than just practical
information. On the <a href="/big-things-to-expect-from-emacs-25.html">aforementioned post</a>, I talked about 6 things I hoped to see
by the time Emacs 25 came out.
</p>
<dl class="org-dl">
<dt>Moving the code base to git</dt><dd>This one even had a schedule date when I wrote
the post, so it obviously doesn’t count as a prediction. Still, I’m
extremely glad it happened as I would never have contributed myself if not
for this change.</dd>
<dt>Dynamic library loading</dt><dd><p>
Now <i>this</i> is exciting. I originally wrote it was
<i>“looking almost in shape for release”</i>, but I have no idea what led me to
say that. This feature actually almost didn’t make it into release. It only
got merged to the <code>emacs-25</code> branch more than one year later, <a href="https://lists.gnu.org/archive/html/emacs-devel/2015-11/msg01697.html">after the
feature-freeze took place</a>!
</p>
<p>
Nonetheless, it’s finally out and about, and I’m eager to see what comes
out of it. If you’re interested, it’s actually very easy to get started
(well… easy by C-coding standards). Aurélien Aptel has a <a href="http://diobla.info/blog-archive/modules-tut.html">super short
tutorial</a> on how to write, compile, and use a dynamic module in Emacs.
</p></dd>
<dt>Concurrency in Elisp</dt><dd>I’ll count this as a half point. We don’t have proper
concurrency in Elisp yet, and whether that’s something we want is still an
<a href="https://lists.gnu.org/archive/html/emacs-devel/2016-10/msg00200.html">ongoing discussion</a>. Still, most of the points in my rant have already been
addressed. The packages list does <a href="/new-in-emacs-25-1-asynchronous-package-menu.html">refresh asynchronously</a> now (despite the
lack of concurrency), and several optimizations have been made which speed
up the actual rendering of the list.</dd>
<dt>A Better Package Menu</dt><dd>I shouldn’t have to say much here. The last 5 links on
list above are all improvements to the package menu. While there’s still
room for improvement, it’s quite ahead of where it was 2 years ago. Better
dependency management, asynchronous refreshing, and more filtering options
were all much needed improvements. Meanwhile, archive priorities are a nice
cherry to top of it all off.</dd>
<dt>A More Robust Customize Interface</dt><dd>Out of the two issues that led me to
write this point, one of them has been fixed (<code>custom-theme-load-path</code> is no
longer a <code>defcustom</code>). Still, I’m counting this as a miss. Over the last
couple of years I’ve seen other fundamental issues with the Customize
interface be reported, and there’s still quite a bit of work to be done here.</dd>
<dt>Namespaces</dt><dd>Well, OK. We still don’t have namespaces. Honestly, though, it
doesn’t bother me anymore. I’ve released two packages, <a href="https://github.com/Malabarba/names">Names</a> and <a href="https://github.com/Malabarba/nameless">Nameless</a>,
which are available on Elpa and help mitigate the the lack of namespaces. I
use Nameless for all my Elisp coding now and that’s the end of the story
for me.</dd>
</dl>
<p>
Summarizing, even if we exclude the Git point (which wasn’t really a
prediction), it’s a 2½ out of 5 (or 3½ if we count namespaces). That’s a
pretty good outcome considering I had never even made a single contribution to
Emacs core when I wrote that post. I’d love to write another one of those, but
I’m not nearly as involved in the news as I was 2 years ago. Maybe in a couple
of months I’ll manage to catch up.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/emacs-25-is-out-what-are-the-new-features-and-what-were-my-predictions.html?source=rss#disqus_thread">Comment on this.</a></p>A quick guide to directory-local (or project-specific) variables2016-07-05T00:00:00+00:00https://endlessparentheses.com//a-quick-guide-to-directory-local-variables<p>
One of the questions we get most often about CIDER is “can I configure X on a
per-project basis?”. Occasionally, you find someone suggesting (or even
implementing) some sophisticated configurable variable trying to account for
multiple simultaneous use-cases. Fortunately that’s one effort we don’t need to
make. Emacs already has that built-in in the form of directory-local variables
(dir-local for short).
</p>
<p>
As the name implies, dir-local variable values (can) apply to all files inside a
given directory (also applying recursively to its sub-directories), though you
can also restrict them by major-mode or by subdirectory. To configure it:
</p>
<ol class="org-ol">
<li>Invoke <kbd>M-x</kbd> <a href="https://doc.endlessparentheses.com/Fun/add-dir-local-variable"><code>add-dir-local-variable</code></a>.</li>
<li>This will prompt you for a major-mode, then a variable name, and finally a
variable value.</li>
<li>Finally, it will drop you in a (newly-created) file called <code>.dir-locals.el</code>
in the current directory. Just save this file and you’re done!</li>
</ol>
<p>
By doing this, you have configured the given variable to always have the
provided value anywhere inside the <b>current directory</b>, so make sure you’re at
the root of your project when you call the command. If you use <a href="https://github.com/bbatsov/projectile">Projectile</a>,
there’s the <code>projectile-edit-dir-locals</code> command for doing just that.
</p>
<p>
Also worth noting:
</p>
<ul class="org-ul">
<li>If you type <code>nil</code> for the major-mode, it applies to all major-modes.</li>
<li>There is (of course) tab-completion in the variable-name prompt.</li>
<li>If you answer <code>eval</code> for the variable name, it will prompt you for a lisp
expression, instead of a value. This expression will be saved and will be
evaluated every time a file is visited.</li>
<li>After doing this, call <a href="https://doc.endlessparentheses.com/Fun/revert-buffer"><code>revert-buffer</code></a> on any previously-open files to
apply the new value.</li>
</ul>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Update <span class="timestamp-wrapper"><span class="timestamp">21 Jul 2016</span></span></h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
It’s worth mentioning that Joel McCracken posted <a href="http://joelmccracken.github.io/entries/project-local-variables-in-projectile-with-dirlocals/">a similar quick guide</a> a few
years ago. Some of the information is the same, but some is complementary, so
you might want to have a look.</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/a-quick-guide-to-directory-local-variables.html?source=rss#disqus_thread">Comment on this.</a></p>A few paredit keys that take over the world2016-06-29T00:00:00+00:00https://endlessparentheses.com//a-few-paredit-keys-that-take-over-the-world<p>
Once you learn paredit, you really can’t go back. Even when you’re not editing
lisp you crave for the ease of manipulating everything as balanced sexps.
Although that’s (sadly) not always possible, you can still hack your way into a
bit of guilty paren-pleasure in pretty much any editing session.
</p>
<p>
While not all programming languages are lisps, most of them do have brackets and
quotes. And paredit should have no problem moving you forward-up out of a C
string, wrapping curly brackets around a work in LaTeX, or even splicing out a
pair of parenthesis in plain prose.
</p>
<p>
Below are a few keys I find useful pretty much everywhere, so I’ve allowed them
to take over the global keymap.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-M-u"</span><span class="p">)</span> <span class="nf">#'</span><span class="nv">paredit-backward-up</span><span class="p">)</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-M-n"</span><span class="p">)</span> <span class="nf">#'</span><span class="nv">paredit-forward-up</span><span class="p">)</span>
<span class="c1">;; This one's surpisingly useful for writing prose.</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="s">"\M-S"</span>
<span class="nf">#'</span><span class="nv">paredit-splice-sexp-killing-backward</span><span class="p">)</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="s">"\M-R"</span> <span class="nf">#'</span><span class="nv">paredit-raise-sexp</span><span class="p">)</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="s">"\M-("</span> <span class="nf">#'</span><span class="nv">paredit-wrap-round</span><span class="p">)</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="s">"\M-["</span> <span class="nf">#'</span><span class="nv">paredit-wrap-square</span><span class="p">)</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="s">"\M-{"</span> <span class="nf">#'</span><span class="nv">paredit-wrap-curly</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/a-few-paredit-keys-that-take-over-the-world.html?source=rss#disqus_thread">Comment on this.</a></p>Restarting the compilation buffer in comint-mode2016-06-17T00:00:00+00:00https://endlessparentheses.com//restarting-the-compilation-buffer-in-comint-mode<p>
After <a href="/provide-input-to-the-compilation-buffer.html">last week's post</a>, Clément Pit-Claudel informed us of an alternative method
for providing input to compilations. I have no idea how I’d never learned about
that before, but I figure that other people might be in the same situation so
it’s worth a post. Have a look at the Update at the end of the post.
</p>
<p>
I’ve also updated an older post accordingly: <a href="/better-compile-command.html">Better compile command</a>.</p>
<p><a href="https://endlessparentheses.com/restarting-the-compilation-buffer-in-comint-mode.html?source=rss#disqus_thread">Comment on this.</a></p>Provide input to the compilation buffer2016-06-09T00:00:00+00:00https://endlessparentheses.com//provide-input-to-the-compilation-buffer<p>
The Emacs <a href="https://doc.endlessparentheses.com/Fun/compile"><code>compile</code></a> command is a severely underused tool. It allows you to run
any build tool under the sun and provides error-highlighting and jump-to-error
functionality for dozens of programming languages, but many an Emacser is still
in the habit of switching to a terminal in order to run <code>make</code>, <code>lein test</code>, or
<code>bundle exec</code>. It does have one limitation, though. The compilation buffer is
not a real shell, so if the command being run asks for user input (even a simple
<code>y/n</code> confirmation) there’s no way to provide it.
</p>
<p>
<i>(Since posting this, I’ve learned that part of it is mildly futile. Read the
update below for more information.)</i>
</p>
<p>
Fortunately, that’s not hard to fix. The snippet below defines two commands. The
first one prompts you for input and then sends it to the underlying terminal
followed by a newline, designed for use with prompts and momentary REPLs. The
second is a command that simply sends the key that was pressed to invoke it,
designed for easily replying to <code>y/n</code> questions or quickly quitting REPLs with
<kbd>C-d</kbd> or <kbd>C-j</kbd>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/send-input</span> <span class="p">(</span><span class="nv">input</span> <span class="k">&optional</span> <span class="nv">nl</span><span class="p">)</span>
<span class="s">"Send INPUT to the current process.
Interactively also sends a terminating newline."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"MInput: \nd"</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nb">string</span> <span class="p">(</span><span class="nv">concat</span> <span class="nv">input</span> <span class="p">(</span><span class="k">if</span> <span class="nv">nl</span> <span class="s">"\n"</span><span class="p">))))</span>
<span class="c1">;; This is just for visual feedback.</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">inhibit-read-only</span> <span class="no">t</span><span class="p">))</span>
<span class="p">(</span><span class="nv">insert-before-markers</span> <span class="nb">string</span><span class="p">))</span>
<span class="c1">;; This is the important part.</span>
<span class="p">(</span><span class="nv">process-send-string</span>
<span class="p">(</span><span class="nv">get-buffer-process</span> <span class="p">(</span><span class="nv">current-buffer</span><span class="p">))</span>
<span class="nb">string</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/send-self</span> <span class="p">()</span>
<span class="s">"Send the pressed key to the current process."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">endless/send-input</span>
<span class="p">(</span><span class="nb">apply</span> <span class="nf">#'</span><span class="nb">string</span>
<span class="p">(</span><span class="nb">append</span> <span class="p">(</span><span class="nv">this-command-keys-vector</span><span class="p">)</span> <span class="no">nil</span><span class="p">))))</span>
<span class="p">(</span><span class="nb">dolist</span> <span class="p">(</span><span class="nv">key</span> <span class="o">'</span><span class="p">(</span><span class="s">"\C-d"</span> <span class="s">"\C-j"</span> <span class="s">"y"</span> <span class="s">"n"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">compilation-mode-map</span> <span class="nv">key</span>
<span class="nf">#'</span><span class="nv">endless/send-self</span><span class="p">))</span></code></pre></figure>
<p>
This is something I’ve run into for years, but I finally decided to fix it
because it meant I couldn’t run Ruby’s <code>rspec</code> in the compilation buffer if my
code contained a <code>binding.pry</code> (which spawns a REPL). Now I can actually interact
with this REPL via <kbd>C-c i</kbd> or just quickly get rid of it with <kbd>C-d</kbd>. If you run
into the same situation, you should also set the following option in your
<code>.pryrc</code> file.
</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="no">Pry</span><span class="p">.</span><span class="nf">config</span><span class="p">.</span><span class="nf">pager</span> <span class="o">=</span> <span class="kp">false</span> <span class="k">if</span> <span class="no">ENV</span><span class="p">[</span><span class="s2">"INSIDE_EMACS"</span><span class="p">]</span></code></pre></figure>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Update <span class="timestamp-wrapper"><span class="timestamp">17 Jun 2016</span></span></h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
As Clément points out in the comments, you can run compilation commands in
<code>comint-mode</code> by providing the <kbd>C-u</kbd> prefix to <kbd>M-x</kbd> <code>compile</code>. You still have
all of the usual <code>compilation-mode</code> features (like <code>next/previous-error</code>), with
the additional benefit that the buffer accepts input like a regular shell does.
</p>
<p>
The only caveat is that, since the buffer is modifiable, you lose some
convenience keys like <kbd>q</kbd> to quit the buffer or <kbd>g</kbd> to recompile, so you’ll need
to bind them somewhere else
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-key</span> <span class="nv">compilation-minor-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"<f5>"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">recompile</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">compilation-minor-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"<f9>"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">quit-window</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">compilation-shell-minor-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"<f5>"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">recompile</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">compilation-shell-minor-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"<f9>"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">quit-window</span><span class="p">)</span></code></pre></figure>
<p>
I still like it that the previous solution gives me quick access to <kbd>C-d</kbd> and
<kbd>y/n</kbd> for those cases when I forget to use <code>comint-mode</code>, but the solution I had
for inputting long strings is definitely redundant now. Instead, we can have a
key that restarts the current compilation in <code>comint-mode</code>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">require</span> <span class="ss">'cl-lib</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/toggle-comint-compilation</span> <span class="p">()</span>
<span class="s">"Restart compilation with (or without) `comint-mode'."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">cl-callf</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">mode</span><span class="p">)</span> <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">eq</span> <span class="nv">mode</span> <span class="no">t</span><span class="p">)</span> <span class="no">nil</span> <span class="no">t</span><span class="p">))</span>
<span class="p">(</span><span class="nb">elt</span> <span class="nv">compilation-arguments</span> <span class="mi">1</span><span class="p">))</span>
<span class="p">(</span><span class="nv">recompile</span><span class="p">))</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">compilation-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-c i"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">endless/toggle-comint-compilation</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">compilation-minor-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-c i"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">endless/toggle-comint-compilation</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">compilation-shell-minor-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-c i"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">endless/toggle-comint-compilation</span><span class="p">)</span></code></pre></figure>
</div>
</div>
<p><a href="https://endlessparentheses.com/provide-input-to-the-compilation-buffer.html?source=rss#disqus_thread">Comment on this.</a></p>Fill and unfill paragraphs with a single key2016-05-31T00:00:00+00:00https://endlessparentheses.com//fill-and-unfill-paragraphs-with-a-single-key<p>
<a href="https://doc.endlessparentheses.com/Fun/fill-paragraph"><code>fill-paragraph</code></a> is probably among the most underappreciated Emacs commands. I
use it dozens of times a day, and never stop to think of just how awesome and
practical it is. Still, we can make it a little bit better. Every once in a
while I need to “unfill” (or “unwrap”) a paragraph that’s broken over many
lines.
</p>
<p>
By being clever enough, we can make this into a <a href="/hungry-delete-mode.html">free feature</a>. There’s never any
reason to hit <kbd>M-q</kbd> twice on the same paragraph, so we can use that as our
keybind for the “unfill” command.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/fill-or-unfill</span> <span class="p">()</span>
<span class="s">"Like `fill-paragraph', but unfill if used twice."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">fill-column</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">eq</span> <span class="nv">last-command</span> <span class="ss">'endless/fill-or-unfill</span><span class="p">)</span>
<span class="p">(</span><span class="k">progn</span> <span class="p">(</span><span class="k">setq</span> <span class="nv">this-command</span> <span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="nv">point-max</span><span class="p">))</span>
<span class="nv">fill-column</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">call-interactively</span> <span class="nf">#'</span><span class="nv">fill-paragraph</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="nv">[remap</span> <span class="nv">fill-paragraph]</span>
<span class="nf">#'</span><span class="nv">endless/fill-or-unfill</span><span class="p">)</span></code></pre></figure>
<p>
With this, <kbd>M-q</kbd> will act as a toggle. Hitting it once will do its usual thing
(even if the paragraph is already filled), but hitting it twice will completely
unwrap the current paragraph into a single line.
</p>
<p><a href="https://endlessparentheses.com/fill-and-unfill-paragraphs-with-a-single-key.html?source=rss#disqus_thread">Comment on this.</a></p>A review of Mickey Petersen’s “Mastering Emacs” book, for beginners and advanced users2016-05-29T00:00:00+00:00https://endlessparentheses.com//a-review-of-mickey-petersen-s-mastering-emacs-book-for-beginners-and-advanced-users<p>
I wish I had reviewed <a href="https://www.masteringemacs.org/">this book</a> when it first came out, over one year ago. Alas,
those were busier times and this piece of work deserved more than a short post
hastily written between seminars and group meetings. Fortunately, Mickey has
unknowingly gifted me a second opportunity, by making it half-off for its 1 year
anniversary, and you have until tomorrow to grab the discount.
</p>
<div class="figure">
<p><a href="https://www.masteringemacs.org/book"><img src="/images/mastering-emacs-cover.png" alt="mastering-emacs-cover.png" /></a>
</p>
</div>
<p>
When it comes to technical reading, it’s rare to find a book that is both packed
with content and high in quality. Anyone who’s ever written a long document will
tell you how difficult it is to maintain a consistent level of quality
throughout the pages. Somewhere around the 100th or so page the complexity
spirals out of control and you spend more time organizing the chapters (to avoid
repetition or maintain the ideal order) than actually writing anything.
</p>
<p>
And yet, somehow, Mastering Emacs masterfully delivers on both areas, clearly
showing itself as a labor of love, and a whole lotta sweat and hard work. The
book’s beautiful cover smoothly gives way to an interior that is consistently
easy on the eyes and a pleasure to read. Most importantly, all of this eye-candy
is made all the sweeter by the healthy dose of knowledge that lies underneath.
</p>
<p>
If you’re a relative newcomer to Emacs (and I say “relative newcomer” in the
broadest sense), this book will be your initiation into the cult. It starts with
an entire chapter on “The Way of Emacs”, before it ever mentions keybinds or
commands, which is exactly how I’d start a book as well. Once that is done, it
goes on to teach you hundreds of pages of Emacs fundamentals.
</p>
<p>
If I had to be nit-picky, it’s hard to say whether these first few chapters are
too long or just about right. When it comes to introducing new concepts, the
ideal speed varies largely from reader to reader anyway. I also disagree with
recommending starter kits, but that’s about all the criticism I can come up
with for a couple hundred of pages.
</p>
<p>
Given all of the above, it’s very easy to recommend Mastering Emacs to Emacs
beginners. They are, after all, the target audience of this book, and they’ll
probably still be digesting its lessons 6 months from now.
</p>
<p>
But what about intermediate to advanced users? Despite not being the target
audience, it’s far from a worthless experience for them. The thing to understand
is that whenever a guru like Mickey puts down so many thoughts into words,
everyone is bound to learn something.
</p>
<p>
In particular, chapters 4, 5, and 6, contain a large collection of fairly
advanced tips. These are actually quite short, to the point that I was
frequently left wishing for more (which is not necessarily a bad thing), but
they’re usually just enough to get you started with the feature. Any
intermediate user is sure to not know many of them, and even advanced users
might benefit from a few. For instance, chapter 4 finally convinced me to try
Helm, and chapter 6 made me wonder why I never use Emacs to read log files.
</p>
<p>
Whether or not a few new ideas wrapped in a beautiful package is worth the price
being asked is entirely up to you (and, I suppose, up to how the price
translates in your currency). All I can say is that I’m happy for this book, and
I’m secretly wishing for a second volume.
</p>
<p><a href="https://endlessparentheses.com/a-review-of-mickey-petersen-s-mastering-emacs-book-for-beginners-and-advanced-users.html?source=rss#disqus_thread">Comment on this.</a></p>Locally configure or disable show-paren-mode2016-05-18T00:00:00+00:00https://endlessparentheses.com//locally-configure-or-disable-show-paren-mode<p>
<code>show-paren-mode</code> is a minor-mode that highlights the bracket at point (be it
round, square, or curly) as well as its corresponding open/close counterpart. I
find it a must-have for Elisp and Clojure programming. On the other hand, when
editing Ruby code, it also highlights whole block delimiters, like <code>def</code>, <code>do</code>,
<code>if</code>, and <code>end</code>, and all that must-haviness quickly turns into
in-your-faceviness.
</p>
<p>
The catch here is that <code>show-paren-mode</code> is a global minor-mode. So you can’t
just enable it locally in <code>lisp-mode-hook</code>, and if you try to do
<code>(show-paren-mode -1)</code> in <code>ruby-mode-hook</code> you’re going to disable it globally
every time you visit a ruby file.
</p>
<p>
Fortunately, <code>show-paren-function</code> checks the value of the variable
<code>show-paren-mode</code>, so we can pseudo-disable it by setting this variable locally.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">show-paren-mode</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/locally-disable-show-paren</span> <span class="p">()</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">setq-local</span> <span class="nv">show-paren-mode</span> <span class="no">nil</span><span class="p">))</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'ruby-mode-hook</span>
<span class="nf">#'</span><span class="nv">endless/locally-disable-show-paren</span><span class="p">)</span></code></pre></figure>
<p>
With this, the mode will still be active in <code>ruby-mode</code> buffers, but it won’t
actually do anything.
</p>
<p>
Alternatively, you could reset <code>show-paren-data-function</code> to its original value
(also inside <code>ruby-mode-hook</code>). This will keep only the basic bracket
highlighting.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">setq-local</span> <span class="nv">show-paren-data-function</span> <span class="nf">#'</span><span class="nv">show-paren--default</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/locally-configure-or-disable-show-paren-mode.html?source=rss#disqus_thread">Comment on this.</a></p>validate.el: Schema validation for Emacs-Lisp2016-05-10T00:00:00+00:00https://endlessparentheses.com//validate-el-schema-validation-for-emacs-lisp<p>
Emacs’ customizable variables (a.k.a., <code>defcustom</code>) are allowed to specify a
<code>:type</code> parameter for setting its custom-type. The customize interface uses this
information to produce a sophisticated menu for the user to customize that
variable. However, a large fraction of users use <code>setq</code> to directly edit custom
variables, and even some packages programmatically change the value of other
package’s custom variables. Ultimately, there are no guarantees that the value
in question matches the <code>:type</code> specified in the variable.
</p>
<p>
<code>validate.el</code> tries to address that by offering a small set of functions and
macros to validate that a value matches a <code>:type</code> schema, throwing an error when
it doesn’t. Most importantly, in case of error it provides very informative
messages about what part of the value failed to validate. So, instead of getting
some obscure <code>wrong-type-argument</code> error deep down in the code, you’ll get a
message like the following as soon as the variable is used:
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">Looking for ‘(repeat (choice string number))’ in ‘("aa" 90 la)’ failed because:
Looking for ‘(choice string number)’ in ‘la’ failed because:
all of the options failed
Looking for ‘string’ in ‘la’ failed because:
not a string
Looking for ‘number’ in ‘la’ failed because:
not a number</code></pre></figure>
<p>
There are 3 main use-cases for this:
</p>
<ol class="org-ol">
<li><p>
As an end-user of a package, you can use <code>validate-setq</code> instead of <code>setq</code>
for editing variables. This will ensure the configuration you provide matches
the schema specified by the developer, and thus prevents you from
misconfiguring stuff.
</p>
<p>
Also note that this works on any <code>defcustom</code> defined with a <code>:type</code>. That is,
it doesn’t matter if the package itself uses <code>validate.el</code>.
</p></li>
<li>As a developer, when using a variable, use <code>(validate-variable 'var-name)</code>.
This will be identical to just using <code>var-name</code> if the value passes
validation, but will immediately throw an error if it doesn’t.</li>
<li>Also as a developer, you can call <code>(validate-mark-safe-local 'var-name)</code>
which will create a safe-local predicate for the variable whenever the local
value satisfies its schema.</li>
</ol>
<p>
<code>validate.el</code> is available on <a href="https://elpa.gnu.org/packages/validate.html">GNU Elpa</a>, so you can install it from the
package-menu or add it as a dependency in your package.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;; Package-Requires: ((validate "0.3"))</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/validate-el-schema-validation-for-emacs-lisp.html?source=rss#disqus_thread">Comment on this.</a></p>Disable Mouse only inside Emacs2016-05-02T00:00:00+00:00https://endlessparentheses.com//disable-mouse-only-inside-emacs<p>
As laptop touchpads seem to be steadily increasing in size, one unfortunate
consequence is that it becomes increasingly harder to avoid touching them by
accident while you type. Most systems have safeguards in place that disable the
touchpad as you’re typing, but they always seem to fall short for me when it
comes to Emacs. While in Emacs, my hands are permanently resting on the keyboard
(and over the touchpad), so even if I stop typing for several seconds I don’t
want the touchpad to reactivate.
</p>
<p>
There are ways to permanently deactivate the touchpad with a hotkey, but, so
far, the solution that best fits my use-style is to disable it only inside
Emacs.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-minor-mode</span> <span class="nv">disable-mouse-mode</span>
<span class="s">"A minor-mode that disables all mouse keybinds."</span>
<span class="ss">:global</span> <span class="no">t</span>
<span class="ss">:lighter</span> <span class="s">" 🐭"</span>
<span class="ss">:keymap</span> <span class="p">(</span><span class="nv">make-sparse-keymap</span><span class="p">))</span>
<span class="p">(</span><span class="nb">dolist</span> <span class="p">(</span><span class="k">type</span> <span class="o">'</span><span class="p">(</span><span class="nv">mouse</span> <span class="nv">down-mouse</span> <span class="nv">drag-mouse</span>
<span class="nv">double-mouse</span> <span class="nv">triple-mouse</span><span class="p">))</span>
<span class="p">(</span><span class="nb">dolist</span> <span class="p">(</span><span class="nv">prefix</span> <span class="o">'</span><span class="p">(</span><span class="s">""</span> <span class="nv">C-</span> <span class="nv">M-</span> <span class="nv">S-</span> <span class="nv">M-S-</span> <span class="nv">C-M-</span> <span class="nv">C-S-</span> <span class="nv">C-M-S-</span><span class="p">))</span>
<span class="c1">;; Yes, I actually HAD to go up to 7 here.</span>
<span class="p">(</span><span class="nb">dotimes</span> <span class="p">(</span><span class="nv">n</span> <span class="mi">7</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">k</span> <span class="p">(</span><span class="nb">format</span> <span class="s">"%s%s-%s"</span> <span class="nv">prefix</span> <span class="k">type</span> <span class="nv">n</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">disable-mouse-mode-map</span>
<span class="p">(</span><span class="nb">vector</span> <span class="p">(</span><span class="nb">intern</span> <span class="nv">k</span><span class="p">))</span> <span class="nf">#'</span><span class="k">ignore</span><span class="p">)))))</span></code></pre></figure>
<p>
All we do here is define a minor-mode that binds all mouse-related keys to
<code>ignore</code>. This is a slight improvement over the code on <a href="http://stackoverflow.com/questions/4906534/disable-mouse-clicks-in-emacs/4906698#4906698">this StackOverflow
answer</a>. Of course, let’s not forget to enable this.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">disable-mouse-mode</span> <span class="mi">1</span><span class="p">)</span></code></pre></figure>
<p>
Two relevant limitations:
</p>
<ul class="org-ul">
<li>This doesn’t distinguish between a touchpad an an actual mouse.</li>
<li>It is still possible for modes to define specific buttons that interact with
the mouse, but that’s not a huge problem because these buttons take a small
portion of the screen so it’s fairly difficult to touch them by accident.</li>
</ul>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Update <span class="timestamp-wrapper"><span class="timestamp">29 Jun 2016</span></span></h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
By mere coincidence, it looks like Steve Purcell implemented something extremely
similar (comment below). Unlike me, he actually had the decency of wrapping the
minor-mode in a <a href="https://github.com/purcell/disable-mouse">Melpa package</a>, and I know some might prefer that over
lengthening their init file.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/disable-mouse-only-inside-emacs.html?source=rss#disqus_thread">Comment on this.</a></p>ANSI-colors in the compilation buffer output2016-04-26T00:00:00+00:00https://endlessparentheses.com//ansi-colors-in-the-compilation-buffer-output<p>
Countless build tools and shell scripts use ANSI escape codes to colorize their
output. This provides impressive improvements to readability when running from a
terminal that supports them, but tends to cause a catastrophic mess anywhere
else. Emacs’ compilation buffer is one such place. It doesn’t support ANSI
colors by default, but that’s very easy to fix.
</p>
<p>
Emacs already has a library for interpreting ANSI escape. All we need is to hook
it onto <code>compilation-mode</code>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">require</span> <span class="ss">'ansi-color</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/colorize-compilation</span> <span class="p">()</span>
<span class="s">"Colorize from `compilation-filter-start' to `point'."</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">inhibit-read-only</span> <span class="no">t</span><span class="p">))</span>
<span class="p">(</span><span class="nv">ansi-color-apply-on-region</span>
<span class="nv">compilation-filter-start</span> <span class="p">(</span><span class="nv">point</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'compilation-filter-hook</span>
<span class="nf">#'</span><span class="nv">endless/colorize-compilation</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/ansi-colors-in-the-compilation-buffer-output.html?source=rss#disqus_thread">Comment on this.</a></p>Emacs is available on Chromebook and Chrome2016-04-19T00:00:00+00:00https://endlessparentheses.com//emacs-is-available-on-chromebook-and-chrome<p>
Are you a Chromebook user or thinking of becoming one? Are you a die-hard
Emacser who needs to see it run even in your browser for no good reason (no
judgement)? Either way, Emacs has you covered. Thanks to the efforts of Pete
Williamson (and friends), there is now an Emacs port for Chromebook and Chrome.
</p>
<p>
If that spikes your interest, you should read <a href="https://plus.google.com/u/0/118416397954909331171/posts/8dt2sM8YeeM">his post about it</a> on Google Plus,
which provides installation instructions, a <a href="https://docs.google.com/document/d/1gEUBUkVDQR1oDx18ZSM39PlbQBvlmNMwOQBhbSm_9mc">list of known issues</a>, and some tips
on how to make the most of it. In short:
</p>
<ol class="org-ol">
<li>Enable Chrome’s <a href="chrome://flags/#enable-nacl">Native Client flag</a>.</li>
<li>Install the <a href="https://chrome.google.com/webstore/detail/nacl-development-environm/aljpgkjeipgnmdpikaajmnepbcfkglfa">NaCl Development Environment</a> from the Chrome web store.</li>
<li>Launch the app and wait for it finish setting everything up. YMMV, but I had
to restart the app at this point.</li>
<li>Run <code>emacs</code> at the terminal you’re given (and celebrate!).</li>
</ol>
<p>
Again, <a href="https://plus.google.com/u/0/118416397954909331171/posts/8dt2sM8YeeM">his post</a> contains a lot more information, including his email so you can
report bugs or file suggestions. Although I did run into a couple of small bumps
with the DevEnv app, the Emacs executable ran just fine and performed admirably.
</p>
<p>
Pete has been doing this as his 20% project — in which Google employees take
20% of their time to work on something else. If you follow the steps above, you
may notice that it uses Emacs 24.3, already two minor versions behind the latest
stable. That’s because 24.3 was the latest release back when he started this
endeavour.
</p>
<p>
If you’re wondering how hard it was, he talked about it on FOSDEM last year and
you can have a glance at the <a href="https://fosdem.org/2015/schedule/event/emacs_and_elisp_on_the_chromebook/attachments/slides/797/export/events/attachments/emacs_and_elisp_on_the_chromebook/slides/797/FOSDEM_Building_Emacs_with_NaCl.pdf">slides</a> for this talk. It was harder than I would
have expected and clearly took some patience. Of course, all of the code is
publicly available under the <a href="https://chromium.googlesource.com/webports/+/master/ports/emacs/">webports project</a>. It’s basically a shell script and
a patch file, so it’s worth a look if you’re curious.
</p>
<p><a href="https://endlessparentheses.com/emacs-is-available-on-chromebook-and-chrome.html?source=rss#disqus_thread">Comment on this.</a></p>Improving Projectile with extra commands2016-04-11T00:00:00+00:00https://endlessparentheses.com//improving-projectile-with-extra-commands<p>
Admittedly, I’m a very late passenger in this boat — only after 4 years of
using Emacs did I decide to try a project manager. Nowadays I can’t even
remember my daily workflow without <a href="https://github.com/bbatsov/projectile">Projectile</a>. This package mostly stays out of
your way, and provides a series of useful commands for dealing with a project
(which are aware of a lot of languages out-of-the-box). As usual, you can find
details in <a href="https://github.com/bbatsov/projectile#synopsis">the readme</a>, and we’ll jump straight into useful configurations.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">projectile-keymap-prefix</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-x p"</span><span class="p">))</span></code></pre></figure>
<p>
This places all Projectile keybinds under <kbd>C-x p</kbd> and requires no explanation.
<a href="/launcher-keymap-for-standalone-features.html">Mnemonic</a> <a href="/the-toggle-map-and-wizardry.html">keymaps</a> <a href="/emacs-narrow-or-widen-dwim.html">are</a> <a href="/keymap-for-launching-external-applications-and-websites.html">the</a> <a href="/multiple-cursors-keybinds.html">best</a>. Most used
commands are <kbd>C-x p f</kbd> to find a file, and <kbd>C-x p p</kbd> to <code>switch-project</code> and
bring up the commander menu (see below)
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">projectile-create-missing-test-files</span> <span class="no">t</span><span class="p">)</span></code></pre></figure>
<p>
<kbd>C-x p t</kbd> creates test files for me.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">projectile-switch-project-action</span>
<span class="nf">#'</span><span class="nv">projectile-commander</span><span class="p">)</span></code></pre></figure>
<p>
By default, Projectile brings up the file-finder when you switch project with
<kbd>C-x p p</kbd>. That’s a reasonable default, but I find a lot of times I’m also looking for
<code>magit-status</code> or a shell buffer. Using <code>projectile-commander</code> means I have to
hit an extra key, but it always gets me where I want.
</p>
<p>
Furthermore, the menu of alternatives presented by <code>projectile-commander</code> is
very customizable, so we can add anything we want in there.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">require</span> <span class="ss">'projectile</span><span class="p">)</span>
<span class="p">(</span><span class="nv">def-projectile-commander-method</span> <span class="nv">?s</span>
<span class="s">"Open a *shell* buffer for the project."</span>
<span class="c1">;; This requires a snapshot version of Projectile.</span>
<span class="p">(</span><span class="nv">projectile-run-shell</span><span class="p">))</span>
<span class="p">(</span><span class="nv">def-projectile-commander-method</span> <span class="nv">?c</span>
<span class="s">"Run `compile' in the project."</span>
<span class="p">(</span><span class="nv">projectile-compile-project</span> <span class="no">nil</span><span class="p">))</span></code></pre></figure>
<p>
The first of those brings up a shell buffer in the project root and the second
runs <kbd>M-x</kbd> <code>compile</code>. Both are super duper convenient for quickly running builds
or custom commands, and which one you use is entirely up to situational
preference.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">def-projectile-commander-method</span> <span class="nv">?\C-?</span>
<span class="s">"Go back to project selection."</span>
<span class="p">(</span><span class="nv">projectile-switch-project</span><span class="p">))</span></code></pre></figure>
<p>
The <kbd>s</kbd> key would normally be bound to project-switching. Since we’ve changed that
above, it’s useful to make <kbd>Backspace</kbd> take that role. This makes sense to me.
It’s like I’m “backing out” of the commander menu.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">def-projectile-commander-method</span> <span class="nv">?d</span>
<span class="s">"Open project root in dired."</span>
<span class="p">(</span><span class="nv">projectile-dired</span><span class="p">))</span></code></pre></figure>
<p>
By default <kbd>d</kbd> would be bound to <code>projectile-find-dir</code>, but that’s something I
never use. <code>projectile-dired</code> takes you to the root directory instead, which I
find more useful.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">def-projectile-commander-method</span> <span class="nv">?F</span>
<span class="s">"Git fetch."</span>
<span class="p">(</span><span class="nv">magit-status</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">fboundp</span> <span class="ss">'magit-fetch-from-upstream</span><span class="p">)</span>
<span class="p">(</span><span class="nv">call-interactively</span> <span class="nf">#'</span><span class="nv">magit-fetch-from-upstream</span><span class="p">)</span>
<span class="p">(</span><span class="nv">call-interactively</span> <span class="nf">#'</span><span class="nv">magit-fetch-current</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">def-projectile-commander-method</span> <span class="nv">?j</span>
<span class="s">"Jack-in."</span>
<span class="p">(</span><span class="k">let*</span> <span class="p">((</span><span class="nv">opts</span> <span class="p">(</span><span class="nv">projectile-current-project-files</span><span class="p">))</span>
<span class="p">(</span><span class="nv">file</span> <span class="p">(</span><span class="nv">ido-completing-read</span>
<span class="s">"Find file: "</span>
<span class="nv">opts</span>
<span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span>
<span class="p">(</span><span class="nb">car</span> <span class="p">(</span><span class="nv">cl-member-if</span>
<span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">f</span><span class="p">)</span>
<span class="p">(</span><span class="nv">string-match</span> <span class="s">"core\\.clj\\'"</span> <span class="nv">f</span><span class="p">))</span>
<span class="nv">opts</span><span class="p">)))))</span>
<span class="p">(</span><span class="nv">find-file</span> <span class="p">(</span><span class="nv">expand-file-name</span>
<span class="nv">file</span> <span class="p">(</span><span class="nv">projectile-project-root</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">run-hooks</span> <span class="ss">'projectile-find-file-hook</span><span class="p">)</span>
<span class="p">(</span><span class="nv">cider-jack-in</span><span class="p">)))</span></code></pre></figure>
<p>
These two are more situational, but I’ve found I use them a lot. Whenever I sit
down to work, there’s a good chance I’m either going to start a REPL (<kbd>j</kbd>) or
fetch git remotes (<kbd>F</kbd>).
</p>
<p>
And last but not nearly least.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">projectile-global-mode</span><span class="p">)</span></code></pre></figure>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Update <span class="timestamp-wrapper"><span class="timestamp">14 Apr 2016</span></span></h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Used Projectile’s built-in <code>shell</code> and <code>compile</code> commands.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/improving-projectile-with-extra-commands.html?source=rss#disqus_thread">Comment on this.</a></p>Running Emacs on Android2016-04-06T00:00:00+00:00https://endlessparentheses.com//running-emacs-on-android<p>
As Android phones rise in power, bluetooth keyboards become cheaper, and your
addiction to Emacs grows, it’s only natural that you start thinking of combining
the three. Fortunately for you, it’s not as hard as it used to be. In fact, it’s
perfectly possible to reproduce (most of) your desktop config, if you know how
to get past a few obstacles.
</p>
<p>
I was going to precede these instructions with a short tale, but I got a little
carried a way and it grew a little too long. Because of that, I’ll give you
the instructions first and let you decide whether to read my self-indulgent
delusions below.
</p>
<ol class="org-ol">
<li>Install <a href="https://play.google.com/store/apps/details?id=com.termux">Termux</a> (and optionally <a href="https://play.google.com/store/apps/details?id=org.pocketworkstation.pckeyboard">Hacker’s keyboard</a>) from the Play store.</li>
<li><p>
Start it, and run:
</p>
<figure class="highlight"><pre><code class="language-sh" data-lang="sh">apt update
apt install emacs</code></pre></figure></li>
<li>Copy your desktop’s <code>init.el</code> file to your phone (read below for a way to
keep it synchronized).</li>
<li>Run <code>emacs</code> (celebrate a little!).
<ul class="org-ul">
<li>If Emacs spits a “void-function …” error at you, comment out the relevant
part of your init file and try again. See below for the explanation.</li>
</ul></li>
<li>Celebrate a lot!</li>
</ol>
<div class="figure">
<p><a href="/images/android-emacs-result.png"><img src="/images/android-emacs-result.png" alt="android-emacs-result.png" /></a>
</p>
</div>
<p>
I was surprised at how well this worked in the end. Even <a href="https://github.com/Malabarba/beacon">Beacon</a>, which is a
largely graphical package, worked out of the box (if a bit laggy).
</p>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Update <span class="timestamp-wrapper"><span class="timestamp">11 Apr 2016</span></span></h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Fredrik Fornwall has patched the Emacs package on Termux to no longer require
the <code>tmp</code> dir workarounds. So now it’s easy as pie to get Emacs on your Android.
</p>
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">A tale of two thumbs</h2>
<div class="outline-text-2" id="text-orgheadline2">
<p>
Last night, in a fit of boredom, far away from my laptop and from any physical
keyboard, I did what any reasonable person would have done: decided to install
Emacs on my phone. My last attempt at crossing this bridge was over a year ago,
and resulted in nothing but absolute failure. Still… Something felt different
this time. Something felt right.
</p>
<p>
My first stop was an obvious one. There’s an <a href="https://play.google.com/store/apps/details?id=com.zielm.emacs">Emacs app</a> on the play store
claiming to do exactly what I needed. On my previous attempts, this app had
given inconsistent results, crashing more often than running. This time,
however, it wouldn’t even install. The Play Store just greeted me with a
“Failure to install” error instead.
</p>
<p>
Not all was lost, though. A <a href="https://play.google.com/store/apps/details?id=com.zielm.emacs&reviewId=Z3A6QU9xcFRPSGQ2WG1fR1M3MlhacU13MmtWd2NlaGpza1k0czVkaVlVUm85QThiZ3V6RjRNQWU2bUFTcE0yWWVzM3VheG1ERkYxOVctbnhfWVJBeUZsc0E">generous comment</a> (with an less-than-generous rating)
points me in a new direction. <a href="https://play.google.com/store/apps/details?id=com.termux">Termux</a>, my beacon of hope, is a robust app that
claims to offer “powerful terminal emulation with an extensive Linux package
collection”.
</p>
<p>
After installing it, I am indeed greeted with a terminal emulator, but I’m not
impressed. I’ve used terminal apps before. Tentatively, I run the commands (as
instructed by the man-from-the-review).
</p>
<figure class="highlight"><pre><code class="language-sh" data-lang="sh">apt update
apt install emacs</code></pre></figure>
<div class="figure">
<p><a href="/images/termux-emacs.png"><img src="/images/termux-emacs.png" alt="termux-emacs.png" /></a>
</p>
</div>
<p>
Surprisingly, it played out even better than expected. Installing Emacs would
have been fine, but installing version 25 is exceptional! My init file is sure
to be incompatible with Emacs 24, but now that we have 25… Dare I even hope?
Could I reproduce my entire setup on this minute device?
</p>
<p>
Before proceeding, I hopped back over to the play store and installed the
<a href="https://play.google.com/store/apps/details?id=org.pocketworkstation.pckeyboard">Hacker’s keyboard</a>, to get access to modifier keys. Then, starting <code>emacs</code> from the
terminal worked as expected, if a bit slowly, but my streak of good fortune was
about to end.
</p>
<p>
The first command I issued (<kbd>M-x</kbd> <code>list-packages</code>) failed with a cryptic message
about nonexistent file in <code>/tmp/asdlij198h1</code> (/this should not happen to you
thanks to Fredrik’s patch). The package menu displayed, but refused to refresh.
So I <kbd>C-z</kbd> out of Emacs, and try <code>mkdir /tmp/</code>.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">Permission denied</code></pre></figure>
<p>
Ok, I should have expected that. <code>sudo</code> doesn’t work either, so I issue <code>fg</code> to
go back to Emacs and try a different approach.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">temporary-file-directory</span> <span class="s">"~/tmp/"</span><span class="p">)</span></code></pre></figure>
<p>
Now <code>list-packages</code> again, and it works! I try to install <code>company</code> from the
<code>gnu</code> repository, and it works too! In fact, everything works. I run <kbd>M-x</kbd>
<code>global-company-mode</code> and it’s all there, popup menu and everything.
</p>
<p>
At this point I’m probably way more satisfied with myself than I should for such
a senseless pursuit, but I’m too high to realize.
</p>
<p>
Next step is to port over my real init file. Fortunately, I keep it sync’d via
Dropbox, so it’s easy to make available on the phone. Just open the Dropbox app,
find my <code>init.el</code>, and mark it to be “Available offline”. Same thing for my
<code>init.org</code>. Dropbox saves the files somewhere deep inside its own data
directory, and automatically syncs them up when you make local changes (though
you have to manually ask it to pull down remote changes).
</p>
<p>
Back on Termux, I send Emacs to the background again with <kbd>C-z</kbd>, and
</p>
<figure class="highlight"><pre><code class="language-sh" data-lang="sh"><span class="nb">cd</span> ~/.emacs.d
ln <span class="nt">-s</span> /sdcard/Android/data/com.dropbox.android/files/SOME_GIBBERISH/.emacs.d/init.el
ln <span class="nt">-s</span> /sdcard/Android/data/com.dropbox.android/files/SOME_GIBBERISH/.emacs.d/init.org</code></pre></figure>
<p>
Actually finding the file took a bit of trial and error. After getting to the
<code>files</code> directory I had to tab-complete my way through several gibberish
directory names before finding the right one.
</p>
<p>
<code>fg</code> and we’re back in Emacs.
</p>
<p>
The next obstacle are the packages. I know my init file is not going to load
before I install the necessary packages. Sadly, I never had the foresight
of rewriting my configuration in something like <code>use-package</code>, but there’s
something almost as good. Like I’ve mentioned before, Emacs 25
<a href="/new-in-package-el-in-emacs-25-1-user-selected-packages.html">keeps track of user-selected packages</a>, so I was able to go into my
<code>custom-set-variables</code> and find a sizeable list under
<code>package-selected-packages</code>. With no further ceremony, I just evaluate the whole
thing with <kbd>C-M-x</kbd>, and issue <kbd>M-x</kbd> <code>package-install-selected-packages</code>.
</p>
<p>
This takes a while…
</p>
<p>
This is a good moment to find out if Termux works well in the background. Switch
to the Youtube app, watch a couple of videos, switch back… And it worked!
</p>
<p>
This is it now — the moment of truth. I’m still feeling high and mighty after
all this success, but I have to respect the odds. My init file has 4 thousand
lines of Elisp and involves over 100 packages. That’s a whole lot of
could-go-wrong potential.
</p>
<p>
I take a deep breath and mentally try to lower my expectations. I <kbd>C-x C-c</kbd> back
to the terminal, and then carefully type <code>emacs</code> followed by <kbd>↵</kbd>, only to plunge
head-first into an error.
</p>
<p>
Something about the <code>tmp</code> directory again. Haven’t I solved this one? A bit more
digging reveals it’s being triggered by <code>(server-start)</code>, because
<code>server-socket-dir</code> is bound to <code>/tmp/emacs1000</code>. The comments above the
variable explain why.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;; We do not use `temporary-file-directory' here,</span>
<span class="c1">;; because emacsclient does not read the init file.</span>
<span class="p">(</span><span class="nb">defvar</span> <span class="nv">server-socket-dir</span>
<span class="o">...</span><span class="p">)</span></code></pre></figure>
<p>
That’s understandable, but it doesn’t help me. Impatiently, I just wrap the call
to <code>server-start</code> in <code>ignore-errors</code> and try again…
</p>
<p>
Another error. This time it’s complaining that <code>set-fringe-mode</code> is a void
function. That’s a lot easier to understand. The Emacs binary provided in Termux
is compiled for terminal (why wouldn’t it be?), so many graphical functions
aren’t defined at all. It’s also easy to solve. Just add a conditional around
the function call.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">fboundp</span> <span class="ss">'set-fringe-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nv">set-fringe-mode</span> <span class="o">'</span><span class="p">(</span><span class="no">nil</span> <span class="o">.</span> <span class="mi">0</span><span class="p">)))</span></code></pre></figure>
<p>
A third time. <kbd>C-x C-c</kbd> <code>emacs</code> <kbd>↵</kbd>… And it works! No more errors. Not even a
warning! My beloved <code>darktooth</code> theme is unrecognizable, but all the other
indicative signs of my Emacs setup are there. Smart-mode-line discretely smiles
at me from the bottom of the screen. Beacon eagerly blinks at me as soon as I
start scrolling. And the unmistakable coziness of my personal keybinds reach me
even through the limited interface of a touchscreen-keyboard.
</p>
<p>
As we get to this point, a lesser person might feel an anticlimax — a hint of
a “what for?”, perhaps. But not me. I just feel safe and reassured, knowing
that, wherever I am and whenever I need it, Emacs will always be sitting in my
pocket.
</p>
<p>
Now I just need to get myself one of those bluetooth keyboards I mentioned…
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/running-emacs-on-android.html?source=rss#disqus_thread">Comment on this.</a></p>Eval-result-overlays in Emacs-lisp2016-03-29T00:00:00+00:00https://endlessparentheses.com//eval-result-overlays-in-emacs-lisp<p>
One of the things I like most in CIDER is how evaluation results are displayed
by inline overlays. And yet, for some reason, it’s taken me almost a year to
transfer that to Elisp.
</p>
<div class="figure">
<p><a href="/images/elisp-inline-eval-result.png"><img src="/images/elisp-inline-eval-result.png" alt="elisp-inline-eval-result.png" /></a>
</p>
</div>
<p>
It’s the tiniest of changes — you’re just taking something that would be
displayed in the minibuffer and moving it up a dozen or so lines — but the
difference is palpable. By displaying the result inline, you display it where
the user is looking, instead of forcing them to shift focus. The quick-feedback
loop we all love in our lisps becomes even faster (something I would’ve thought
impossible one year ago).
</p>
<p>
Assuming you already have <a href="https://github.com/clojure-emacs/cider#installation">CIDER</a> installed, porting this feature to Elisp is
almost trivial. We just define a small wrapper around the function that creates
the overlay, and then advise the relevant elisp commands to call it.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">autoload</span> <span class="ss">'cider--make-result-overlay</span> <span class="s">"cider-overlays"</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/eval-overlay</span> <span class="p">(</span><span class="nv">value</span> <span class="nv">point</span><span class="p">)</span>
<span class="p">(</span><span class="nv">cider--make-result-overlay</span> <span class="p">(</span><span class="nb">format</span> <span class="s">"%S"</span> <span class="nv">value</span><span class="p">)</span>
<span class="ss">:where</span> <span class="nv">point</span>
<span class="ss">:duration</span> <span class="ss">'command</span><span class="p">)</span>
<span class="c1">;; Preserve the return value.</span>
<span class="nv">value</span><span class="p">)</span>
<span class="p">(</span><span class="nv">advice-add</span> <span class="ss">'eval-region</span> <span class="ss">:around</span>
<span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">f</span> <span class="nv">beg</span> <span class="nv">end</span> <span class="k">&rest</span> <span class="nv">r</span><span class="p">)</span>
<span class="p">(</span><span class="nv">endless/eval-overlay</span>
<span class="p">(</span><span class="nb">apply</span> <span class="nv">f</span> <span class="nv">beg</span> <span class="nv">end</span> <span class="nv">r</span><span class="p">)</span>
<span class="nv">end</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">advice-add</span> <span class="ss">'eval-last-sexp</span> <span class="ss">:filter-return</span>
<span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">r</span><span class="p">)</span>
<span class="p">(</span><span class="nv">endless/eval-overlay</span> <span class="nv">r</span> <span class="p">(</span><span class="nv">point</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">advice-add</span> <span class="ss">'eval-defun</span> <span class="ss">:filter-return</span>
<span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">r</span><span class="p">)</span>
<span class="p">(</span><span class="nv">endless/eval-overlay</span>
<span class="nv">r</span>
<span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nv">end-of-defun</span><span class="p">)</span>
<span class="p">(</span><span class="nv">point</span><span class="p">)))))</span></code></pre></figure>
<p>
If I like this enough, I might implement it more properly and propose its
addition to Emacs core. For now the advices are more than enough.
</p>
<p>
If you don’t want to install CIDER, you can just <a href="https://github.com/clojure-emacs/cider/blob/master/cider-overlays.el#L121">copy that function</a> to your
configs (you’ll also have to copy the functions above it in the same file, and
the <code>when-let</code> definition from <code>cider-compat.el</code>).
</p>
<p><a href="https://endlessparentheses.com/eval-result-overlays-in-emacs-lisp.html?source=rss#disqus_thread">Comment on this.</a></p>Leave the cursor at start of match after isearch2016-03-21T00:00:00+00:00https://endlessparentheses.com//leave-the-cursor-at-start-of-match-after-isearch<p>
Have you ever stopped to think about why isearch leaves point at the end of the
match? It does make some intuitive sense to leave you after the characters you
have just typed, but that doesn’t make it the most practical behaviour.
</p>
<p>
This is something I’d never even given half a thought in the past, but it’s one
of the first things a friend questioned me as he was diving into Emacs (yes, the
same one as <a href="/a-small-improvement-to-clj-refactor.html">last week</a>). Why not leave the cursor at the start of the match,
instead of the end?
</p>
<p>
It’s great when someone’s fresh perspective shakes new ideas into my
gradually-calcifying init file, so I immediately started looking for a way to do
this. Fortunately, Google was quick to find me an answer, in <a href="https://github.com/thisirs/dotemacs/blob/master/lisp/init-isearch.el#L1">the dotfiles</a> of one
Sylvain Rousseau. I should go through the entire file later to look for more
snippets, but for the current purpose, the following is all we need.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'isearch-mode-end-hook</span>
<span class="nf">#'</span><span class="nv">endless/goto-match-beginning</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/goto-match-beginning</span> <span class="p">()</span>
<span class="s">"Go to the start of current isearch match.
Use in `isearch-mode-end-hook'."</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">and</span> <span class="nv">isearch-forward</span>
<span class="p">(</span><span class="nv">number-or-marker-p</span> <span class="nv">isearch-other-end</span><span class="p">)</span>
<span class="p">(</span><span class="nb">not</span> <span class="nv">mark-active</span><span class="p">)</span>
<span class="p">(</span><span class="nb">not</span> <span class="nv">isearch-mode-end-hook-quit</span><span class="p">))</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="nv">isearch-other-end</span><span class="p">)))</span></code></pre></figure>
<p>
After using this for a few days, I’ve already ran into a couple of cases where I
would have preferred the original behaviour. Still, the net balance has been
positive so far so it’s probably going to stay.
</p>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Update <span class="timestamp-wrapper"><span class="timestamp">23 Mar 2016</span></span></h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
One alternative to the approach above is <a href="/leave-the-cursor-at-start-of-match-after-isearch.html#comment-2581635318%5D">cutejumper's suggestion</a> below, straight
from the Emacs wiki. This will only leave point at the start of search if you
exit the search with <kbd>C-↵</kbd> instead of <kbd>↵</kbd>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-key</span> <span class="nv">isearch-mode-map</span> <span class="nv">[</span><span class="p">(</span><span class="nv">control</span> <span class="nb">return</span><span class="p">)</span><span class="nv">]</span>
<span class="nf">#'</span><span class="nv">isearch-exit-other-end</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">isearch-exit-other-end</span> <span class="p">()</span>
<span class="s">"Exit isearch, at the opposite end of the string."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">isearch-exit</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="nv">isearch-other-end</span><span class="p">))</span></code></pre></figure>
</div>
</div>
<p><a href="https://endlessparentheses.com/leave-the-cursor-at-start-of-match-after-isearch.html?source=rss#disqus_thread">Comment on this.</a></p>A small improvement to clj-refactor2016-03-15T00:00:00+00:00https://endlessparentheses.com//a-small-improvement-to-clj-refactor<p>
I’ve said before that <a href="/clj-refactor-unleash-your-clojure-wizard.html">clj-refactor is a magical package</a>, and you wouldn’t catch
me bad-mouthing it in a million release cycles, but it’s impossible to please
everybody.
</p>
<p>
One consistent annoyance I have with it, is that I always have to type <kbd>Tab</kbd>
twice after invoking <code>cljr-add-require-to-ns</code>. That’s because I only ever use
requires of the form <code>[x.y :as y]</code>, so I always have to skip the first two
“modes” offered by the snippet.
</p>
<p>
As I was teaching a friend about this command last week, he immediately asked me
if you couldn’t just write some Elisp to do that for you. Of course, the answer
is yes. And that’s exactly what I did — once I was done feeling embarrassed
that I hadn’t thought of that before.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">advice-add</span> <span class="ss">'cljr-add-require-to-ns</span> <span class="ss">:after</span>
<span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="k">&rest</span> <span class="nv">_</span><span class="p">)</span>
<span class="p">(</span><span class="nv">yas-next-field</span><span class="p">)</span>
<span class="p">(</span><span class="nv">yas-next-field</span><span class="p">)))</span></code></pre></figure>
<p>
Note that this uses the new advice interface, which relies on Emacs 24.5. While
this API in its entirety can be quite intimidating, you don’t need to learn it
all in one go. You can go a long way (and make your life a lot easier) by just
writing yourself a few <code>:before</code> and <code>:after</code> advices.
</p>
<p>
Just remember to be responsible. Advices let you run code in places where
packages don’t expect code to be run. So don’t be surprised if something else
breaks.</p>
<p><a href="https://endlessparentheses.com/a-small-improvement-to-clj-refactor.html?source=rss#disqus_thread">Comment on this.</a></p>Conditional breakpoints in the CIDER Debugger2016-03-07T00:00:00+00:00https://endlessparentheses.com//conditional-breakpoints-in-the-cider-debugger<p>
<code>CIDER 0.11.0</code> has been out for less than week and already the snapshots are
getting new features. This one comes from a gentleman called <a href="https://github.com/clojure-emacs/cider-nrepl/pull/303">Chris Perkins</a>. It
provides an easy way to automatically skip some breakpoints during evaluation,
and it even comes with 300 brand new lines of tests.
</p>
<p>
The feature is very straightforward, and is already available on the snapshots
of <code>0.12.0</code>. You’ve always been able to debug functions by adding a <code>#dbg</code> in
before it and then reevaluating its definition.
</p>
<figure class="highlight"><pre><code class="language-clojure" data-lang="clojure"><span class="o">#</span><span class="n">dbg</span><span class="w">
</span><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">buggy-function</span><span class="w"> </span><span class="p">[]</span><span class="w">
</span><span class="p">(</span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">i</span><span class="w"> </span><span class="p">(</span><span class="nb">range</span><span class="w"> </span><span class="mi">3000</span><span class="p">)]</span><span class="w">
</span><span class="p">(</span><span class="nb">some</span><span class="w"> </span><span class="p">(</span><span class="nf">kinda</span><span class="w"> </span><span class="n">complicated</span><span class="p">)</span><span class="w">
</span><span class="n">code</span><span class="w"> </span><span class="p">(</span><span class="nf">logic</span><span class="p">)</span><span class="w"> </span><span class="n">i</span><span class="p">)))</span></code></pre></figure>
<p>
However, if you’re trying to debug a problem that only happens somewhere around
<code>i = 1000</code>, then you’re going to have to step through the body of that <code>for</code> one
thousand times before getting to where you need.
</p>
<p>
This new feature lets you specify a condition, and the debugger will
automatically skip all breakpoints as long as that condition is false. You
specify it via the <code>:break/when</code> metadata. In the above example, that would be
something like this.
</p>
<figure class="highlight"><pre><code class="language-clojure" data-lang="clojure"><span class="o">#</span><span class="n">dbg</span><span class="w">
</span><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">buggy-function</span><span class="w"> </span><span class="p">[]</span><span class="w">
</span><span class="p">(</span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">i</span><span class="w"> </span><span class="p">(</span><span class="nb">range</span><span class="w"> </span><span class="mi">3000</span><span class="p">)]</span><span class="w">
</span><span class="o">^</span><span class="p">{</span><span class="no">:break/when</span><span class="w"> </span><span class="p">(</span><span class="nb">></span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="mi">1000</span><span class="p">)}</span><span class="w">
</span><span class="p">(</span><span class="nb">some</span><span class="w"> </span><span class="p">(</span><span class="nf">kinda</span><span class="w"> </span><span class="n">complicated</span><span class="p">)</span><span class="w">
</span><span class="n">code</span><span class="w"> </span><span class="p">(</span><span class="nf">logic</span><span class="p">)</span><span class="w"> </span><span class="n">i</span><span class="p">)))</span></code></pre></figure>
<p>
Thus, everything inside the <code>(some ...)</code> form would be skipped as long as <code>i ≤ 1000</code>.
</p>
<p>
The debugger’s inner workings are not what I’d call simple, so it makes me happy
to know I’ve made the code legible enough for people to understand without my
help. Of course, it certainly helped that Chris is no average fellow. Did I
mention he even wrote a DSL for the new tests?
</p>
<p><a href="https://endlessparentheses.com/conditional-breakpoints-in-the-cider-debugger.html?source=rss#disqus_thread">Comment on this.</a></p>Commands to thread and unwind code in Emacs-Lisp2016-03-01T00:00:00+00:00https://endlessparentheses.com//commands-to-thread-and-unwind-code-in-emacs-lisp<p>
As <a href="/clj-refactor-unleash-your-clojure-wizard.html">you may remember</a>, one of the commands I like the most from the <code>clj-refactor</code>
package are the ones that thread and unwind Clojure code forms for you. Now that
Emacs is also getting <a href="/new-in-emacs-25-1-more-flow-control-macros.html">built-in threading macros</a>, I figured the best way to give
them a fair chance in life is to also make them pretty convenient to use.
</p>
<p>
The point here is that you can place point before a paren, invoke a command, and
a regular code form gets transformed into a threading macro, or vice-versa. See
the <a href="/new-in-emacs-25-1-more-flow-control-macros.html">linked post</a> for what that means. Instead of writing whole new commands for
that, I had a fun time just hacking <code>clj-refactor</code> commands to work on
<code>thread-first/last</code>.
</p>
<p>
It should go without saying, you need <a href="https://github.com/clojure-emacs/clj-refactor.el#readme">clj-refactor</a> installed for this to work.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-key</span> <span class="nv">emacs-lisp-mode-map</span> <span class="s">"\C-ctf"</span>
<span class="nf">#'</span><span class="nv">endless/elisp-thread-first</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">emacs-lisp-mode-map</span> <span class="s">"\C-ctl"</span>
<span class="nf">#'</span><span class="nv">endless/elisp-thread-last</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">emacs-lisp-mode-map</span> <span class="s">"\C-ctu"</span>
<span class="nf">#'</span><span class="nv">endless/elisp-unwind</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">emacs-lisp-mode-map</span> <span class="s">"\C-cta"</span>
<span class="nf">#'</span><span class="nv">endless/elisp-unwind-all</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/elisp-thread-last</span> <span class="p">()</span>
<span class="s">"Turn the form at point into a `thread-last' form."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">cljr-thread-last-all</span> <span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">search-backward</span> <span class="s">"->>"</span> <span class="no">nil</span> <span class="ss">'noerror</span><span class="p">)</span>
<span class="p">(</span><span class="nv">replace-match</span> <span class="s">"thread-last"</span><span class="p">))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/elisp-thread-first</span> <span class="p">()</span>
<span class="s">"Turn the form at point into a `thread-first' form."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">cljr-thread-first-all</span> <span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">search-backward</span> <span class="s">"->"</span> <span class="no">nil</span> <span class="ss">'noerror</span><span class="p">)</span>
<span class="p">(</span><span class="nv">replace-match</span> <span class="s">"thread-first"</span><span class="p">))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/elisp-unwind</span> <span class="p">()</span>
<span class="s">"Unwind thread at point or above point by one level.
Return nil if there are no more levels to unwind."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">p</span> <span class="p">(</span><span class="nv">point</span><span class="p">)))</span>
<span class="c1">;; Find a thread above.</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nv">forward-sexp</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nv">search-backward-regexp</span> <span class="s">"\\_<thread-\\(first\\|last\\)\\_>"</span> <span class="no">nil</span> <span class="ss">'noerror</span><span class="p">)</span>
<span class="c1">;; Ensure that it contains the original point.</span>
<span class="p">(</span><span class="nv">save-match-data</span> <span class="p">(</span><span class="nv">forward-char</span> <span class="mi">-1</span><span class="p">)</span>
<span class="p">(</span><span class="nv">forward-sexp</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="nb">></span> <span class="p">(</span><span class="nv">point</span><span class="p">)</span> <span class="nv">p</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">replace-match</span> <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">string=</span> <span class="p">(</span><span class="nv">match-string</span> <span class="mi">1</span><span class="p">)</span> <span class="s">"first"</span><span class="p">)</span>
<span class="s">"->"</span> <span class="s">"->>"</span><span class="p">))</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">thread-beginnig</span> <span class="p">(</span><span class="nv">match-beginning</span> <span class="mi">0</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">prog1</span> <span class="p">(</span><span class="nv">cljr-unwind</span><span class="p">)</span>
<span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="nv">thread-beginnig</span><span class="p">)</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">looking-at</span> <span class="s">"\\_<->>?\\_>"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">replace-match</span> <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">string=</span> <span class="p">(</span><span class="nv">match-string</span> <span class="mi">0</span><span class="p">)</span> <span class="s">"->"</span><span class="p">)</span>
<span class="s">"thread-first"</span> <span class="s">"thread-last"</span><span class="p">)))))))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/elisp-unwind-all</span> <span class="p">()</span>
<span class="s">"Fully unwind thread at point or above point."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nv">endless/elisp-unwind</span><span class="p">)))</span></code></pre></figure>
<p>
Maybe just writing it from scratch would have made for shorter code (it would
certainly be more robust). But such is life. I try not to dwell too much on
quick hacks.</p>
<p><a href="https://endlessparentheses.com/commands-to-thread-and-unwind-code-in-emacs-lisp.html?source=rss#disqus_thread">Comment on this.</a></p>New in Emacs 25.1: More flow control macros2016-02-22T00:00:00+00:00https://endlessparentheses.com//new-in-emacs-25-1-more-flow-control-macros<p>
One of my personal favorite new additions to Emacs 25 is, in fact, completely
invisible to most users. The new macros <code>if-let</code> and <code>when-let</code>, although simple
in purpose, are a delight to use and are frequently finding their way into my
code. The other two additions, <code>thread-first</code> and <code>thread-last</code>, are a bit more
specific, and take a bit getting-used-to if you’ve never seen them before.
</p>
<p>
The two <code>*-let</code> macros are easier to explain by just expanding them. All they do
is summarize a very common situation for lisp programmers. Instead of writing
this:
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">x</span> <span class="p">(</span><span class="nv">some-func</span><span class="p">)))</span>
<span class="p">(</span><span class="k">if</span> <span class="nv">x</span>
<span class="nv">do-then</span>
<span class="nv">do-else</span>
<span class="nv">do-else-2</span><span class="p">))</span></code></pre></figure>
<p>
you can write this:
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">if-let</span> <span class="p">((</span><span class="nv">x</span> <span class="p">(</span><span class="nv">some-func</span><span class="p">)))</span>
<span class="nv">do-then</span>
<span class="nv">do-else</span>
<span class="nv">do-else-2</span><span class="p">)</span></code></pre></figure>
<p>
Seeing this example it shouldn’t be hard to understand <code>when-let</code> as well.
</p>
<p>
As you can see, it’s not exactly a revolution — you barely even save any
typing. But that extra level of nesting you save, along with an added bit of
clarity, for some reason makes me smile every time I get to use them.
</p>
<p>
The threading macros are something you see all the time in Clojure (under a
shorter name) but are probably not going to be as common in Emacs-Lisp — your
average Elisp code is far less functional than Clojure code. Still, they’re good
to know as they can be a huge help in some situations. Explaining again by
example:
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;; First</span>
<span class="p">(</span><span class="nv">thread-first</span> <span class="nv">x</span>
<span class="p">(</span><span class="nv">concat</span> <span class="nv">y</span><span class="p">)</span>
<span class="p">(</span><span class="nb">format</span> <span class="nv">z</span><span class="p">))</span>
<span class="c1">;; Expands to this:</span>
<span class="p">(</span><span class="nb">format</span> <span class="p">(</span><span class="nv">concat</span> <span class="nv">x</span> <span class="nv">y</span><span class="p">)</span> <span class="nv">z</span><span class="p">)</span>
<span class="c1">;; Last</span>
<span class="p">(</span><span class="nv">thread-last</span> <span class="nv">x</span>
<span class="p">(</span><span class="nv">concat</span> <span class="nv">y</span><span class="p">)</span>
<span class="p">(</span><span class="nb">format</span> <span class="nv">z</span><span class="p">))</span>
<span class="c1">;; Expands to this:</span>
<span class="p">(</span><span class="nb">format</span> <span class="nv">z</span> <span class="p">(</span><span class="nv">concat</span> <span class="nv">y</span> <span class="nv">x</span><span class="p">))</span></code></pre></figure>
<p>
If the examples above don’t look too useful to you, that’s because they’re not.
<code>thread-first</code> and <code>thread-last</code> are relatively short names by Elisp standards,
but they’re still long enough that you’re almost always going to be typing
<b>more</b>, instead of less, when you use these macros. Instead of saving space,
their value lies in improving readability of some rather extreme scenarios
(which are all too common in Elisp). For instance, which one of the forms below
would you find easier to read?
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;; This?</span>
<span class="p">(</span><span class="nv">thread-last</span> <span class="nv">some-string</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span> <span class="s">"regexp-1"</span> <span class="s">"replace-1"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span> <span class="s">"regexp-2"</span> <span class="s">"replace-2"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span> <span class="s">"regexp-3"</span> <span class="s">"replace-3"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span> <span class="s">"regexp-4"</span> <span class="s">"replace-4"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span> <span class="s">"regexp-5"</span> <span class="s">"replace-5"</span><span class="p">))</span>
<span class="c1">;; Or this?</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span>
<span class="s">"regexp-5"</span> <span class="s">"replace-5"</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span>
<span class="s">"regexp-4"</span> <span class="s">"replace-4"</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span>
<span class="s">"regexp-3"</span> <span class="s">"replace-3"</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span>
<span class="s">"regexp-2"</span> <span class="s">"replace-2"</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span>
<span class="s">"regexp-1"</span> <span class="s">"replace-1"</span> <span class="nv">some-string</span><span class="p">)))))</span></code></pre></figure>
<p>
Finally, for those of you who use my <a href="https://elpa.gnu.org/packages/sotlisp.html">speed-of-thought-lisp</a> package, it already
has abbrevs for these macros under <code>il</code>, <code>wl</code>, <code>tf</code>, and <code>tl</code>.</p>
<p><a href="https://endlessparentheses.com/new-in-emacs-25-1-more-flow-control-macros.html?source=rss#disqus_thread">Comment on this.</a></p>New in Emacs 25.1: map.el library2016-02-16T00:00:00+00:00https://endlessparentheses.com//new-in-emacs-25-1-map-el-library<p>
Another library by the productive Nicolas Petton. <code>map.el</code> is a cousin to
<code>seq.el</code> (<a href="/new-on-elpa-and-in-emacs-25-1-seq-el.html">remember?</a>), but instead of manipulating plain sequences, it
manipulates map-like collections (also known as dictionaries).
</p>
<p>
The range of functions should include everything you expect, from <code>map-values</code>
and <code>map-keys</code> to get a list of the values and keys (duh), to general getters
and setters like <code>map-elt</code> and <code>map-put</code>. Inside it, you’ll even find some
fancier stuff like <code>map-let</code>, <code>map-values-apply</code>, or <code>map-nested-elt</code>. For those
who know what that means, it also comes with a <code>pcase</code> macro!
</p>
<p>
Note that this library does <b>not</b> introduce a new data structure. It simply
provides a consistent and unified API for dealing with any map-like structure
you need. This applies to alists, hash-maps, and even vectors (it treats the
vector as a dictionary whose keys are all integers).</p>
<p><a href="https://endlessparentheses.com/new-in-emacs-25-1-map-el-library.html?source=rss#disqus_thread">Comment on this.</a></p>New in Emacs 25.1: EWW improvements2016-02-08T00:00:00+00:00https://endlessparentheses.com//new-in-emacs-25-1-eww-improvements<p>
In the upcoming version, EWW is getting a number of small improvements. This web
browser, written by Lars Ingebrigtsen, is something of a new kid on the block,
as it just came to life at the very end of the Emacs 24 cycle. Although it’s
hard, if not impossible, to reliably render HTML inside an editor that’s 100%
line-based, EWW tends to find a reasonable compromise and deserves at least a
short post to cherish new features.
</p>
<blockquote>
<p>
HTML can now be rendered using variable-width fonts.
</p>
<p>
A new command <kbd>F</kbd> (<code>eww-toggle-fonts</code>) can be used to toggle whether to use
variable-pitch fonts or not.
</p>
</blockquote>
<p>
It goes without saying that this is very good, monospace fonts are not the most
appropriate for reading webpages. It might sound like something simple, but
this was probably the hardest feature to implement. Filling paragraphs in
variable-width fonts is not something Emacs does by default.
</p>
<blockquote>
<p>
A new command <kbd>R</kbd> (<code>eww-readable</code>) will try do identify the main textual parts
of a web page and display only that, leaving menus and the like off the page.
</p>
</blockquote>
<p>
I just tested this on <a href="/new-in-emacs-25-1-round-quotes-in-help-buffers.html">the round quotes post</a> and it worked well—the buffer
correctly hid everything but the post content. Unfortunately, it also hid images
that were inside the post.
</p>
<blockquote>
<p>
https pages with valid certificates have headers marked in green, while invalid
certificates are marked in red.
</p>
</blockquote>
<p>
Awareness about security is always a good thing.
</p>
<blockquote>
<p>
You can now use several eww buffers in parallel by renaming eww buffers you
want to keep separate.
</p>
<p>
Partial state of the eww buffers (the URIs and the titles of the pages visited)
is now preserved in the desktop file.
</p>
<p>
The new <kbd>S</kbd> command will list all eww buffers, and allow managing them.
</p>
</blockquote>
<p>
All of these contribute to giving EWW a more complete browser experience. While
we don’t have actual tabs yet, you can have multiple open pages, manage them
all with <kbd>S</kbd>, and even “remember open tabs” by enabling <code>desktop-mode</code>.
</p>
<p><a href="https://endlessparentheses.com/new-in-emacs-25-1-eww-improvements.html?source=rss#disqus_thread">Comment on this.</a></p>New in Emacs 25.1: Easily search for non-ASCII characters2016-02-02T00:00:00+00:00https://endlessparentheses.com//new-in-emacs-25-1-easily-search-non-ascii-characters<p>
Since <a href="/new-in-emacs-25-1-round-quotes-in-help-buffers.html">last week’s post</a> was about Unicode characters, it makes sense to continue
that trend today. This feature might go unnoticed by a lot of people who live in
an ASCII world, but it will probably jump out at everyone else at one point or
another. The name, if a bit odd, is “character-folding search”.
</p>
<p>
In Emacs 25, Isearch can find a wide range of Unicode characters (like á, ⓐ, or
𝒶) when you search for ASCII characters (<code>a</code> in this example). To enable this
feature, set the variable <code>search-default-mode</code> to <code>char-fold-to-regexp</code>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">search-default-mode</span> <span class="nf">#'</span><span class="nv">char-fold-to-regexp</span><span class="p">)</span></code></pre></figure>
<p>
This first came up due to a new feature of a recent version of Texinfo, where
some markup styles are exported in “round quotes” when generating the info
manual. This is a nice improvement in readability, but when you’re trying to
search for something with <kbd>C-s</kbd>, things can get a little difficult if your
keyboard can’t type some of the characters.
</p>
<p>
With char-folding, when you hit <kbd>C-s</kbd> and search for ", Emacs will also search
for a variety of double quotes, from the aforementioned “” to many others like
«» and ❝❞. The same goes for the single quote, and for pretty much any other
ASCII character.
</p>
<p>
You could always type any Unicode character by name with <kbd>C-x 8 RET</kbd>, and many
of them even have their own shortcuts under <kbd>C-x 8</kbd>, but not <i>having</i> to type
them can be a significant convenience when it comes up. As any Brazilian, I am a
daily user of diacritical marks (ó, ã, ê, and the likes), and even though my
keyboard <i>can</i> type these characters, I still enjoy the simplicity of not having
to.
</p>
<p>
Finally, you can extend this feature to the <code>query-replace</code> command, which is
also as easy as setting a variable.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">replace-char-fold</span> <span class="no">t</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/new-in-emacs-25-1-easily-search-non-ascii-characters.html?source=rss#disqus_thread">Comment on this.</a></p>New in Emacs 25.1: Round quotes in Help buffers2016-01-26T00:00:00+00:00https://endlessparentheses.com//new-in-emacs-25-1-round-quotes-in-help-buffers<p>
Don’t be fooled by the apparent simplicity of this feature. Its implementation
has been the most controversial addition to the upcoming Emacs release — <a href="https://twitter.com/AMalabarba/status/641598687467163648">to a
comical degree</a>. This post, however, is not about arguments or implementation,
it’s about Emacs 25. And this little nugget is all set for the next release.
</p>
<p>
The feature is simple, next time you’re reading a docstring (maybe via <kbd>C-h f</kbd>
or <kbd>C-h v</kbd>), instead of looking like this<br />
<a href="/images/help-buffer-grave-quote.png"><img src="/images/help-buffer-grave-quote.png" alt="help-buffer-grave-quote.png" /></a>
it’ll look like this<br />
<a href="/images/help-buffer-round-quote.png"><img src="/images/help-buffer-round-quote.png" alt="help-buffer-round-quote.png" /></a>
It’s a simple, but noticeable, improvement for user experience.
</p>
<p>
If you don’t like the change, it’s configurable via the new option:
<code>text-quoting-style</code>.
</p>
<p>
Note that these quotes are introduced directly in the <code>*Help*</code> buffer. The
function that does this conversion is <a href="https://doc.endlessparentheses.com/Fun/substitute-command-keys"><code>substitute-command-keys</code></a>, so you can use
it if you’d like the same effect for other purposes. You should still use
`grave-and-straight' quotes when <i>writing</i> docstrings for your <code>defun</code>.
</p>
<p>
On the other hand, if you need to write a straight quote that <i>shouldn’t</i> be
converted to a round quote when it’s printed in the <code>*Help*</code> buffer, you’ll need
to write <code>\\=</code> before it.
</p>
<p><a href="https://endlessparentheses.com/new-in-emacs-25-1-round-quotes-in-help-buffers.html?source=rss#disqus_thread">Comment on this.</a></p>Quickly search for occurrences of the symbol at point2016-01-18T00:00:00+00:00https://endlessparentheses.com//quickly-search-for-occurrences-of-the-symbol-at-point<p>
Isearch is one of Emacs’ most useful (and probably most used) features. Getting
in the habit of quickly hitting <kbd>C-s</kbd> followed by 2–4 letters will forever
change the way you navigate buffers, and adding it to your repertoire is a
tremendous productivity improvement. What, then, could we possibly improve on
such a phenomenal command?
</p>
<p>
One of the search actions I find myself doing a lot is to search forward for
other occurrences of the symbol at point. You can do that in Isearch by typing
<kbd>C-w</kbd> a few times (which copies the next word at point into the prompt) until
you’ve got the entire symbol, and then you type <kbd>M-s _</kbd> to toggle symbol-search.
</p>
<p>
However, this is a lot of keys, and it only works if the cursor is at the start
of the symbol. Fortunately, it’s very easy to improve this because the action we
want is already implemented as the <a href="https://doc.endlessparentheses.com/Fun/isearch-forward-symbol-at-point"><code>isearch-forward-symbol-at-point</code></a> command.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/isearch-symbol-with-prefix</span> <span class="p">(</span><span class="nv">p</span><span class="p">)</span>
<span class="s">"Like isearch, unless prefix argument is provided.
With a prefix argument P, isearch for the symbol at point."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"P"</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">current-prefix-arg</span> <span class="no">nil</span><span class="p">))</span>
<span class="p">(</span><span class="nv">call-interactively</span>
<span class="p">(</span><span class="k">if</span> <span class="nv">p</span> <span class="nf">#'</span><span class="nv">isearch-forward-symbol-at-point</span>
<span class="nf">#'</span><span class="nv">isearch-forward</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="nv">[remap</span> <span class="nv">isearch-forward]</span>
<span class="nf">#'</span><span class="nv">endless/isearch-symbol-with-prefix</span><span class="p">)</span></code></pre></figure>
<p>
The result here is that <kbd>C-s</kbd> starts Isearch as usual, but if I type <kbd>C-u C-s</kbd>
instead it’s going to search for other occurrences of the symbol at point—a
huge improvement over the 6+ keys of our previous option.
</p>
<p>
Normally, calling Isearch with a prefix would start it in regexp-mode. That’s
not something I’ve ever used, but you should be aware of it before you override
it.
</p>
<p><a href="https://endlessparentheses.com/quickly-search-for-occurrences-of-the-symbol-at-point.html?source=rss#disqus_thread">Comment on this.</a></p>An improvement to Emacs auto-correct2016-01-11T00:00:00+00:00https://endlessparentheses.com//an-improvement-to-emacs-auto-correct<p>
My <a href="/ispell-and-abbrev-the-perfect-auto-correct.html">Emacs auto-correct</a> is one of the oldest posts on this blog, and I still see
it pop up here and there on occasion. Last week, Norman Ramsey <a href="/ispell-and-abbrev-the-perfect-auto-correct.html#comment-2440958792">asked</a> about an
improvement to that command and I figured it’s worth an update post.
</p>
<p>
As of today, the code in the post will search backwards for a typo, instead of
just correcting the current word. So you can invoke it without trouble even if
the typo happened several words ago.</p>
<p><a href="https://endlessparentheses.com/an-improvement-to-emacs-auto-correct.html?source=rss#disqus_thread">Comment on this.</a></p>Define context-aware keys in Emacs2016-01-05T00:00:00+00:00https://endlessparentheses.com//define-context-aware-keys-in-emacs<p>
What do you do if you want to override a key only in a certain context? Take
this <a href="/prettify-your-quotation-marks.html">Quotation Marks</a> post as an example. We want to change the <kbd>"</kbd> key in
general, but retain the regular behaviour if we’re inside a code-block. In this
case the solution was to just call the old behaviour manually, but what if
you’re writing a more general command and you don’t know what this “old
behaviour” is?
</p>
<p>
As it turns out, it’s a little-known feature of Emacs that you can specify
filter functions to determine whether a keybind should be active. We use this
quite a bit in <a href="https://github.com/vermiculus/sx.el">SX.el</a>. While the syntax is far from simple, it’s very easy to
copy-paste and just fill in your predicate.
</p>
<p>
First, we redefine <code>endless/round-quotes</code> without that ugly part that
conditionally calls <code>self-insert-command</code> (previous version <a href="/prettify-your-quotation-marks.html">here</a>).
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/round-quotes</span> <span class="p">(</span><span class="nv">italicize</span><span class="p">)</span>
<span class="s">"Insert “” and leave point in the middle.
With prefix argument ITALICIZE, insert /“”/ instead (meant
for org-mode)."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"P"</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">looking-at</span> <span class="s">"”[/=_\\*]?"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">match-end</span> <span class="mi">0</span><span class="p">))</span>
<span class="p">(</span><span class="nb">when</span> <span class="nv">italicize</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">derived-mode-p</span> <span class="ss">'markdown-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">"__"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">"//"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">forward-char</span> <span class="mi">-1</span><span class="p">))</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">"“”"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">forward-char</span> <span class="mi">-1</span><span class="p">)))</span></code></pre></figure>
<p>
Then, we define the same key as before, but instead of just passing the command
we use a menu-item bound to <code>nil</code>. The fact that it’s a menu-item is irrelevant
here, it behaves exactly like a key bound to <code>nil</code> (i.e., an empty keybind).
However, this allows us to setup a filter that changes the keybind to
<code>endless/round-quotes</code> if we’re <b>not</b> inside an org-src-block.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-key</span> <span class="nv">org-mode-map</span> <span class="s">"\""</span>
<span class="o">'</span><span class="p">(</span><span class="nv">menu-item</span> <span class="s">"maybe-round-quotes"</span> <span class="no">nil</span>
<span class="ss">:filter</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="k">&optional</span> <span class="nv">_</span><span class="p">)</span>
<span class="p">(</span><span class="nb">unless</span> <span class="p">(</span><span class="nv">org-in-src-block-p</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">endless/round-quotes</span><span class="p">))))</span></code></pre></figure>
<p>
That <code>maybe-round-quotes</code> is just a useless name for the menu-item, and you can
learn more about all of this on <a href="http://www.gnu.org/software/emacs/manual/html_node/elisp/Extended-Menu-Items.html#Extended-Menu-Items">this manual page</a>. For now, it suffices to say we
deserve a more convenient way to use this feature.
</p>
<p>
Of course, that’s nothing a good macro can’t fix.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defmacro</span> <span class="nv">endless/define-conditional-key</span> <span class="p">(</span><span class="nv">keymap</span> <span class="nv">key</span> <span class="nv">def</span>
<span class="k">&rest</span> <span class="nv">body</span><span class="p">)</span>
<span class="s">"In KEYMAP, define key sequence KEY as DEF conditionally.
This is like `define-key', except the definition
\"disappears\" whenever BODY evaluates to nil."</span>
<span class="p">(</span><span class="k">declare</span> <span class="p">(</span><span class="nv">indent</span> <span class="mi">3</span><span class="p">)</span>
<span class="p">(</span><span class="nv">debug</span> <span class="p">(</span><span class="nv">form</span> <span class="nv">form</span> <span class="nv">form</span> <span class="k">&rest</span> <span class="nv">sexp</span><span class="p">)))</span>
<span class="o">`</span><span class="p">(</span><span class="nv">define-key</span> <span class="o">,</span><span class="nv">keymap</span> <span class="o">,</span><span class="nv">key</span>
<span class="o">'</span><span class="p">(</span><span class="nv">menu-item</span>
<span class="o">,</span><span class="p">(</span><span class="nb">format</span> <span class="s">"maybe-%s"</span> <span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nb">car</span> <span class="p">(</span><span class="nv">cdr-safe</span> <span class="nv">def</span><span class="p">))</span> <span class="nv">def</span><span class="p">))</span>
<span class="no">nil</span>
<span class="ss">:filter</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="k">&optional</span> <span class="nv">_</span><span class="p">)</span>
<span class="p">(</span><span class="nb">when</span> <span class="o">,</span><span class="p">(</span><span class="nv">macroexp-progn</span> <span class="nv">body</span><span class="p">)</span>
<span class="o">,</span><span class="nv">def</span><span class="p">)))))</span></code></pre></figure>
<p>
Which leads to the much nicer syntax:
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">endless/define-conditional-key</span> <span class="nv">org-mode-map</span> <span class="s">"\""</span>
<span class="nf">#'</span><span class="nv">endless/round-quotes</span>
<span class="p">(</span><span class="nb">not</span> <span class="p">(</span><span class="nv">org-in-src-block-p</span><span class="p">)))</span></code></pre></figure>
<p>
And a similar keybind for Markdown, which is a bit more of a mouth-full.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">endless/define-conditional-key</span> <span class="nv">markdown-mode-map</span> <span class="s">"\""</span>
<span class="nf">#'</span><span class="nv">endless/round-quotes</span>
<span class="p">(</span><span class="nb">not</span> <span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nv">markdown-code-at-point-p</span><span class="p">)</span>
<span class="p">(</span><span class="nv">memq</span> <span class="ss">'markdown-pre-face</span>
<span class="p">(</span><span class="nv">face-at-point</span> <span class="no">nil</span> <span class="ss">'mult</span><span class="p">)))))</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/define-context-aware-keys-in-emacs.html?source=rss#disqus_thread">Comment on this.</a></p>New in Emacs 25.1: Have prettify-symbols-mode reveal the symbol at point2015-12-28T00:00:00+00:00https://endlessparentheses.com//new-in-emacs-25-1-have-prettify-symbols-mode-reveal-the-symbol-at-point<p>
I’ve written before about what <a href="https://doc.endlessparentheses.com/Fun/prettify-symbols-mode"><code>prettify-symbols-mode</code></a> can do for your buffers,
ranging from <a href="/using-prettify-symbols-in-clojure-and-elisp-without-breaking-indentation.html">pure eye-candy</a> to <a href="/improving-latex-equations-with-font-lock.html">signficant readability improvements</a>. Simply put,
this minor-mode “disguises” some strings in your buffer to look like something
else. For instance, in <code>emacs-lisp-mode</code> it makes <code>lambda</code> be displayed as <code>λ</code>,
and (for the next release) it’ll apply to a wide range of symbols in
<code>(la)tex-mode</code> too.
</p>
<p>
The only problem is that this (obviously) hides the symbol itself, and there’s
no way of revealing it other than disabling the mode. That’s usually not a big
deal, but it can get just a little annoying sometimes—specially when a mode
adds over 400 elements to <code>prettify-symbols-alist</code>.
</p>
<p>
In Emacs 25.1, thanks to Tassilo Horn, there’s a new variable called
<code>prettify-symbols-unprettify-at-point</code>. If you set it to <code>t</code>, <code>prettify-symbols</code>
will “unprettify” a symbol as long as the cursor is inside it. So you can easily
(and temporarily) reveal a symbol by just moving over to it.
</p>
<p>
My preference is to set it to <code>right-edge</code> which also reveals the symbol if the
point is immediately after it.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">prettify-symbols-unprettify-at-point</span> <span class="ss">'right-edge</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/new-in-emacs-25-1-have-prettify-symbols-mode-reveal-the-symbol-at-point.html?source=rss#disqus_thread">Comment on this.</a></p>Faster pop-to-mark command2015-12-21T00:00:00+00:00https://endlessparentheses.com//faster-pop-to-mark-command<p>
Today’s tip is one I <a href="https://github.com/magnars/expand-region.el/issues/159#issuecomment-83538021">learned from Magnar</a>. A lot of Emacsers don’t know this, but
most commands that move point large distances (like <code>isearch</code> or
<code>end-of-buffer</code>) push the old position to the <a href="https://doc.endlessparentheses.com/Var/mark-ring"><code>mark-ring</code></a>. The advantage is that
you can easily jump back through this history of positions by hitting <kbd>C-u
C-SPC</kbd>.
</p>
<p>
This is a hugely convenient take-me-back-to-that-last-place command. The only
problem is that sometimes the ring gets filled with repeated entries, so you
find yourself hitting <kbd>C-u C-SPC</kbd> 2 to 4 times in the same place. Of course,
this is Emacs, so all it takes to solve our problem is one simple advice.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;; When popping the mark, continue popping until the cursor</span>
<span class="c1">;; actually moves</span>
<span class="p">(</span><span class="nv">defadvice</span> <span class="nv">pop-to-mark-command</span>
<span class="p">(</span><span class="nv">around</span> <span class="nv">ensure-new-position</span> <span class="nv">activate</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">p</span> <span class="p">(</span><span class="nv">point</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">dotimes</span> <span class="p">(</span><span class="nv">i</span> <span class="mi">10</span><span class="p">)</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">=</span> <span class="nv">p</span> <span class="p">(</span><span class="nv">point</span><span class="p">))</span>
<span class="nv">ad-do-it</span><span class="p">))))</span></code></pre></figure>
<p>
Finally, a simple <code>setq</code> ensures we can quickly pop the mark several times by
typing <kbd>C-u C-SPC C-SPC</kbd>, instead of having to type <kbd>C-u C-SPC C-u C-SPC</kbd>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">set-mark-command-repeat-pop</span> <span class="no">t</span><span class="p">)</span></code></pre></figure>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Update <span class="timestamp-wrapper"><span class="timestamp">20 Jan 2016</span></span></h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Kaushal Modi <a href="https://github.com/kaushalmodi/.emacs.d/blob/f2be887c9454b769fdd1236fc911bcfc5679d7d1/setup-files/setup-editing.el#L746-L753">provides us</a> with how this advice would look using the new
<code>advice-add</code> interface.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">modi/multi-pop-to-mark</span> <span class="p">(</span><span class="nv">orig-fun</span> <span class="k">&rest</span> <span class="nv">args</span><span class="p">)</span>
<span class="s">"Call ORIG-FUN until the cursor moves.
Try the repeated popping up to 10 times."</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">p</span> <span class="p">(</span><span class="nv">point</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">dotimes</span> <span class="p">(</span><span class="nv">i</span> <span class="mi">10</span><span class="p">)</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">=</span> <span class="nv">p</span> <span class="p">(</span><span class="nv">point</span><span class="p">))</span>
<span class="p">(</span><span class="nb">apply</span> <span class="nv">orig-fun</span> <span class="nv">args</span><span class="p">)))))</span>
<span class="p">(</span><span class="nv">advice-add</span> <span class="ss">'pop-to-mark-command</span> <span class="ss">:around</span>
<span class="nf">#'</span><span class="nv">modi/multi-pop-to-mark</span><span class="p">)</span></code></pre></figure>
</div>
</div>
<p><a href="https://endlessparentheses.com/faster-pop-to-mark-command.html?source=rss#disqus_thread">Comment on this.</a></p>Improving Emacs file-name completion2015-12-14T00:00:00+00:00https://endlessparentheses.com//improving-emacs-file-name-completion<p>
Although there’s a surprising number of packages offering alternative minibuffer
selection systems, the default minibuffer completion in Emacs is nothing to be
scoffed at. Hitting <kbd>Tab</kbd> in the minibuffer gives you a slightly beefed up
version of the bash completion, and after all these years that is still my
preferred method for completing file-names (though I do have some <a href="/visit-directory-inside-a-set-of-directories.html">custom-written
alternatives</a>).
</p>
<p>
Still, there are two small changes you can make that significantly improve the
convenience of this feature. The simplest one is to make it case-insensitive.
The second, and more fine-tuned, is to exclude extensions which you never really
want. These are usually auto-generated files whose names are similar to files
you care about. My list is useful for elisp and LaTeX, so you may need to figure
out yours.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">read-file-name-completion-ignore-case</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">read-buffer-completion-ignore-case</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="nb">mapc</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'completion-ignored-extensions</span> <span class="nv">x</span><span class="p">))</span>
<span class="o">'</span><span class="p">(</span><span class="s">".aux"</span> <span class="s">".bbl"</span> <span class="s">".blg"</span> <span class="s">".exe"</span>
<span class="s">".log"</span> <span class="s">".meta"</span> <span class="s">".out"</span> <span class="s">".pdf"</span>
<span class="s">".synctex.gz"</span> <span class="s">".tdo"</span> <span class="s">".toc"</span>
<span class="s">"-pkg.el"</span> <span class="s">"-autoloads.el"</span>
<span class="s">"Notes.bib"</span> <span class="s">"auto/"</span><span class="p">))</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/improving-emacs-file-name-completion.html?source=rss#disqus_thread">Comment on this.</a></p>Marking Emacs chat buffers as read (erc, jabber, etc)2015-12-07T00:00:00+00:00https://endlessparentheses.com//marking-emacs-chat-buffers-as-read<p>
I’m an occasional user of some of the Emacs chat clients. Erc and jabber are
both powerful packages, and it’s great to be able to use Slack, Gitter, and
Google Chat from the cosy comfort of my Emacs frame. If I have one complain,
though, it’s that neither of them has a functionality for keeping track of what
part of the conversation I’ve already read.
</p>
<p>
Of course, this is Emacs. And the solution is but a hack away.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/mark-read</span> <span class="p">()</span>
<span class="s">"Mark buffer as read up to current line."</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">inhibit-read-only</span> <span class="no">t</span><span class="p">))</span>
<span class="p">(</span><span class="nv">put-text-property</span>
<span class="p">(</span><span class="nv">point-min</span><span class="p">)</span> <span class="p">(</span><span class="nv">line-beginning-position</span><span class="p">)</span>
<span class="ss">'face</span> <span class="ss">'font-lock-comment-face</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/bury-buffer</span> <span class="p">()</span>
<span class="s">"Bury buffer and maybe close its window."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">endless/mark-read</span><span class="p">)</span>
<span class="p">(</span><span class="nv">bury-buffer</span><span class="p">)</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">cdr</span> <span class="p">(</span><span class="nv">window-list</span> <span class="no">nil</span> <span class="ss">'nomini</span><span class="p">))</span>
<span class="p">(</span><span class="nv">delete-window</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">eval-after-load</span> <span class="ss">'jabber</span>
<span class="o">'</span><span class="p">(</span><span class="nv">define-key</span> <span class="nv">jabber-chat-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"<escape>"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">endless/bury-buffer</span><span class="p">))</span>
<span class="p">(</span><span class="nv">eval-after-load</span> <span class="ss">'erc</span>
<span class="o">'</span><span class="p">(</span><span class="nv">define-key</span> <span class="nv">erc-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"<escape>"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">endless/bury-buffer</span><span class="p">))</span></code></pre></figure>
<p>
Whenever you’re done reading a conversation, just hit <kbd>Esc</kbd>. The buffer will be
buried, and next time you open it again everything you had read before will be
marked in grey.</p>
<p><a href="https://endlessparentheses.com/marking-emacs-chat-buffers-as-read.html?source=rss#disqus_thread">Comment on this.</a></p>Using Paradox for Github notifications2015-11-30T00:00:00+00:00https://endlessparentheses.com//using-paradox-for-github-notifications<p>
A few weeks ago I noticed a new package on Melpa called <a href="https://github.com/xuchunyang/github-notifier.el">github-notifier</a> by
Chunyang, which displays a count of your Github notifications on the mode-line.
Instead of just installing the package like a normal person, I had an urge to
try and see how hard it would be to write from scratch. Paradox already has a
function for interacting with the Github API, so it’s just a matter of putting
it to work.
</p>
<p>
The first thing you need to do is visit your <a href="https://github.com/settings/tokens">Github tokens</a> page, and edit the
Paradox token to allow access to your notifications.
</p>
<p>
Next, define a command to visit your notifications, and a function to display a
button in the mode-line.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/visit-notifications</span> <span class="p">()</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">endless/count-for-mode-line</span> <span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="nv">browse-url</span> <span class="s">"https://github.com/notifications"</span><span class="p">))</span>
<span class="p">(</span><span class="nb">defvar</span> <span class="nv">endless/gh-mode-line</span> <span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/count-for-mode-line</span> <span class="p">(</span><span class="nv">data</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">endless/gh-mode-line</span>
<span class="p">(</span><span class="nb">when</span> <span class="nv">data</span>
<span class="p">(</span><span class="nb">format</span> <span class="s">" GH-%s"</span> <span class="p">(</span><span class="nb">length</span> <span class="nv">data</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">force-mode-line-update</span><span class="p">))</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'global-mode-string</span>
<span class="o">'</span><span class="p">(</span><span class="nv">endless/gh-mode-line</span>
<span class="p">(</span><span class="ss">:propertize</span> <span class="nv">endless/gh-mode-line</span>
<span class="nv">local-map</span> <span class="p">(</span><span class="nv">keymap</span> <span class="p">(</span><span class="nv">mode-line</span> <span class="nv">keymap</span> <span class="p">(</span><span class="nv">mouse-1</span> <span class="o">.</span> <span class="nv">endless/visit-notifications</span><span class="p">)))</span>
<span class="nv">mouse-face</span> <span class="nv">mode-line-highlight</span><span class="p">)))</span></code></pre></figure>
<p>
For convenience, I’ve also added <code>visit-notifications</code> to my <a href="/launcher-keymap-for-standalone-features.html">launcher-map</a> under
<kbd>n</kbd>.
</p>
<p>
Next, write a function to query the Github API and pass the vector of
results to <code>count-for-mode-line</code>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;; If already installed, this does nothing.</span>
<span class="p">(</span><span class="nv">package-install</span> <span class="ss">'paradox</span><span class="p">)</span>
<span class="p">(</span><span class="nv">autoload</span> <span class="ss">'paradox--github-action</span> <span class="s">"paradox-github"</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/check-gh-notifications</span> <span class="p">()</span>
<span class="s">"Check for github notifications and update the mode-line."</span>
<span class="p">(</span><span class="nv">paradox--github-action</span> <span class="s">"notifications"</span>
<span class="ss">:reader</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">()</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">json-false</span> <span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="nv">json-array-type</span> <span class="ss">'list</span><span class="p">))</span>
<span class="p">(</span><span class="nv">json-read</span><span class="p">)))</span>
<span class="ss">:callback</span> <span class="nf">#'</span><span class="nv">endless/count-for-mode-line</span><span class="p">))</span></code></pre></figure>
<p>
Finally, just set the timer to some convenient interval. I run it at every 30
seconds of idle time.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defvar</span> <span class="nv">endless/gh-timer</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">bound-and-true-p</span> <span class="nv">paradox-github-token</span><span class="p">)</span>
<span class="p">(</span><span class="nv">run-with-idle-timer</span> <span class="mi">30</span> <span class="ss">'repeat</span>
<span class="nf">#'</span><span class="nv">endless/check-gh-notifications</span><span class="p">)))</span></code></pre></figure>
<p>
And that’s all! Roughly 30 lines of code and you have a convenient notification
system. Whenever you see that <code>GH-2</code> show up at the corner of your mode-line,
just click on it (or type <kbd>C-x l n</kbd>). That will remove the button and take you
to Github. On Github you can navigate with <kbd>j</kbd>, <kbd>k</kbd>, and <kbd>RET</kbd>, and “mark as
read” with <kbd>m</kbd>.
</p>
<p><a href="https://endlessparentheses.com/using-paradox-for-github-notifications.html?source=rss#disqus_thread">Comment on this.</a></p>Update on tdd-mode with CIDER2015-11-23T00:00:00+00:00https://endlessparentheses.com//update-on-tdd-mode-with-cider<p>
I can’t write a whole new post this week due to being buried under some
once-in-a-lifetime stuff. Still, the Monday post is one I refuse to miss, so I
leave you today with an update on last weeks post.
</p>
<p>
Running tests on file save really didn’t work out for me, because I’ve configured
CIDER to save files before loading them when I use <code>cider-load-file</code>. So the
tests were always being run against outdated definitions.
</p>
<p>
The fix is simple, though. Just run the tests after <code>load-file</code>. CIDER even had
a hook for that already. I also made the minor-mode global (even though it only
applies to clojure buffers) so that it’s easier to disable everywhere with a
single command.
</p>
<p>
The new code is in the <a href="/test-driven-development-in-cider-and-emacs.html">original post</a>.</p>
<p><a href="https://endlessparentheses.com/update-on-tdd-mode-with-cider.html?source=rss#disqus_thread">Comment on this.</a></p>Test-Driven-Development in CIDER and Emacs2015-11-15T00:00:00+00:00https://endlessparentheses.com//test-driven-development-in-cider-and-emacs<p>
As I was catching up on a few <a href="http://www.parens-of-the-dead.com/">Parens of the Dead</a> episodes this weekend, I was
amused at how Magnar set up his Emacs to run tests whenever the file is saved.
At first I thought it wasn’t for me (I’m one of those who obsessively saves
every few seconds), but I’ve been trying it out lately and it’s starting to grow
on me.
</p>
<p>
At its core, all you really need is an hook. But I walked the extra yard and
wrote a minor mode for it, so I can easily call <kbd>M-x</kbd> <code>tdd-mode</code> to disable it
if it ever gets on my nerves.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">tdd-test</span> <span class="p">()</span>
<span class="s">"Thin wrapper around `cider-test-run-tests'."</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">cider-connected-p</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">cider-auto-select-test-report-buffer</span> <span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="nv">cider-test-show-report-on-success</span> <span class="no">nil</span><span class="p">))</span>
<span class="p">(</span><span class="nv">cider-test-run-ns-tests</span> <span class="no">nil</span> <span class="ss">'soft</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">define-minor-mode</span> <span class="nv">tdd-mode</span>
<span class="s">"Run all tests whenever a file is loaded."</span>
<span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span>
<span class="ss">:global</span> <span class="no">t</span>
<span class="p">(</span><span class="k">if</span> <span class="nv">tdd-mode</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'cider-file-loaded-hook</span> <span class="nf">#'</span><span class="nv">tdd-test</span><span class="p">)</span>
<span class="p">(</span><span class="nv">remove-hook</span> <span class="ss">'cider-file-loaded-hook</span> <span class="nf">#'</span><span class="nv">tdd-test</span><span class="p">)))</span></code></pre></figure>
<p>
I also had to change the <code>cider-test-success-face</code> to something a little less
“screamy”.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">custom-set-faces</span>
<span class="o">'</span><span class="p">(</span><span class="nv">cider-test-success-face</span>
<span class="p">((</span><span class="no">t</span> <span class="p">(</span><span class="ss">:foreground</span> <span class="s">"green"</span> <span class="ss">:background</span> <span class="no">nil</span><span class="p">)))))</span></code></pre></figure>
<div id="outline-container-org770db58" class="outline-2">
<h2 id="org770db58">Update <span class="timestamp-wrapper"><span class="timestamp">21 nov 2015</span></span></h2>
<div class="outline-text-2" id="text-org770db58">
<p>
Made it a global mode, so it’s easy to disable everywhere, and set the initial
value to <code>t</code>.
</p>
</div>
</div>
<div id="outline-container-org0ada69c" class="outline-2">
<h2 id="org0ada69c">Update <span class="timestamp-wrapper"><span class="timestamp">04 mar 2016</span></span></h2>
<div class="outline-text-2" id="text-org0ada69c">
<p>
Fixed a function call to the new name.</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/test-driven-development-in-cider-and-emacs.html?source=rss#disqus_thread">Comment on this.</a></p>New Clojure lib: lazy-map2015-11-15T00:00:00+00:00https://endlessparentheses.com//new-clojure-lib-lazy-map<p>
The concept of a <a href="https://github.com/Malabarba/lazy-map-clojure#the-lazy-map">lazy-map</a> might sounds odd at first. How do you know if a map
contains an entry without resolving the whole map? But it’s not the entries that
are lazy, it’s the values they hold. See this example from the Readme:
</p>
<figure class="highlight"><pre><code class="language-clojure" data-lang="clojure"><span class="n">user></span><span class="w"> </span><span class="p">(</span><span class="k">def</span><span class="w"> </span><span class="n">my-map</span><span class="w">
</span><span class="p">(</span><span class="nf">lazy-map</span><span class="w"> </span><span class="p">{</span><span class="no">:cause</span><span class="w"> </span><span class="p">(</span><span class="nf">do</span><span class="w"> </span><span class="p">(</span><span class="nb">println</span><span class="w"> </span><span class="s">"Getting Cause"</span><span class="p">)</span><span class="w">
</span><span class="no">:major-failure</span><span class="p">)</span><span class="w">
</span><span class="no">:name</span><span class="w"> </span><span class="p">(</span><span class="nf">do</span><span class="w"> </span><span class="p">(</span><span class="nb">println</span><span class="w"> </span><span class="s">"Getting Name"</span><span class="p">)</span><span class="w">
</span><span class="s">"Some Name"</span><span class="p">)}))</span><span class="w">
</span><span class="o">#</span><span class="ss">'user/my-map</span><span class="w">
</span><span class="n">user></span><span class="w"> </span><span class="p">(</span><span class="no">:name</span><span class="w"> </span><span class="n">my-map</span><span class="p">)</span><span class="w">
</span><span class="n">Getting</span><span class="w"> </span><span class="n">Name</span><span class="w">
</span><span class="s">"Some Name"</span><span class="w">
</span><span class="n">user></span><span class="w"> </span><span class="p">(</span><span class="no">:name</span><span class="w"> </span><span class="n">my-map</span><span class="p">)</span><span class="w">
</span><span class="s">"Some Name"</span><span class="w">
</span><span class="n">user></span><span class="w"> </span><span class="p">(</span><span class="no">:cause</span><span class="w"> </span><span class="n">my-map</span><span class="p">)</span><span class="w">
</span><span class="n">Getting</span><span class="w"> </span><span class="n">Cause</span><span class="w">
</span><span class="no">:major-failure</span></code></pre></figure>
<p>
<a href="https://github.com/Malabarba/lazy-map-clojure#the-lazy-map">Lazy-map</a> is on Clojars, so you can just add it as a dep and play around:
</p>
<div class="figure">
<p><a href="https://clojars.org/malabarba/lazy-map"><img src="https://clojars.org/malabarba/lazy-map/latest-version.svg"></a>
</p>
</div>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">How would this ever be useful?</h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
One of my side projects a few months ago (when I was playing around with
Clojure-on-Android) was an Android client for a desktop Java application.
Because of this, a lot of the code was about interfacing with Java objects
defined by this package. These objects were from many different classes, but
the one thing they had in common is that they were full of <code>get</code> methods.
</p>
<p>
I wanted so much to be able to use these objects as maps that I wrote <a href="https://github.com/Malabarba/lazy-map-clojure#the-to-lazy-map-protocol">a protocol</a>
for converting general objects to Clojure maps. Here’s an example of how it
worked on a string.
</p>
<figure class="highlight"><pre><code class="language-clojure" data-lang="clojure"><span class="n">user></span><span class="w"> </span><span class="p">(</span><span class="nf">to-map</span><span class="w"> </span><span class="s">"My own String!"</span><span class="p">)</span><span class="w">
</span><span class="p">{</span><span class="no">:to-char-array</span><span class="w"> </span><span class="o">#</span><span class="n">object</span><span class="p">[</span><span class="s">"[C"</span><span class="w"> </span><span class="mi">0</span><span class="n">xdf4ddb3</span><span class="w"> </span><span class="s">"[C@df4ddb3"</span><span class="p">]</span><span class="n">,</span><span class="w">
</span><span class="no">:empty?</span><span class="w"> </span><span class="n">false,</span><span class="w">
</span><span class="no">:to-string</span><span class="w"> </span><span class="s">"My own String!"</span><span class="n">,</span><span class="w">
</span><span class="no">:chars</span><span class="w"> </span><span class="o">#</span><span class="n">object</span><span class="p">[</span><span class="n">java.util.stream.IntPipeline$Head</span><span class="w"> </span><span class="mi">0</span><span class="n">xad35343</span><span class="w"> </span><span class="s">"java.util.stream.IntPipeline$Head@ad35343"</span><span class="p">]</span><span class="n">,</span><span class="w">
</span><span class="no">:class</span><span class="w"> </span><span class="n">java.lang.String,</span><span class="w">
</span><span class="no">:length</span><span class="w"> </span><span class="mi">14</span><span class="n">,</span><span class="w">
</span><span class="no">:trim</span><span class="w"> </span><span class="s">"My own String!"</span><span class="n">,</span><span class="w">
</span><span class="no">:bytes</span><span class="w"> </span><span class="o">#</span><span class="n">object</span><span class="p">[</span><span class="s">"[B"</span><span class="w"> </span><span class="mi">0</span><span class="n">x75ef7d8f</span><span class="w"> </span><span class="s">"[B@75ef7d8f"</span><span class="p">]</span><span class="n">,</span><span class="w">
</span><span class="no">:hash-code</span><span class="w"> </span><span class="mi">1673659170</span><span class="n">,</span><span class="w">
</span><span class="no">:object</span><span class="w"> </span><span class="s">"My own String!"</span><span class="n">,</span><span class="w">
</span><span class="no">:to-upper-case</span><span class="w"> </span><span class="s">"MY OWN STRING!"</span><span class="p">}</span></code></pre></figure>
<p>
For comparison, here’s how <code>bean</code> works (a similar function from
<code>clojure.core</code>).
</p>
<figure class="highlight"><pre><code class="language-clojure" data-lang="clojure"><span class="n">user></span><span class="w"> </span><span class="p">(</span><span class="nb">bean</span><span class="w"> </span><span class="s">"My own String!"</span><span class="p">)</span><span class="w">
</span><span class="p">{</span><span class="no">:bytes</span><span class="w"> </span><span class="o">#</span><span class="n">object</span><span class="p">[</span><span class="s">"[B"</span><span class="w"> </span><span class="mi">0</span><span class="n">x1ad60072</span><span class="w"> </span><span class="s">"[B@1ad60072"</span><span class="p">]</span><span class="n">,</span><span class="w">
</span><span class="no">:class</span><span class="w"> </span><span class="n">java.lang.String,</span><span class="w">
</span><span class="no">:empty</span><span class="w"> </span><span class="n">false</span><span class="p">}</span></code></pre></figure>
<p>
The protocol is actually quite smart. It uses a number of heuristics to only
convert methods that look like they’re side-effect free. Of course, it’s not
foolproof (this <i>is</i> Java we’re talking about), but the macro used to extend the
protocol lets you specify methods to exclude.
</p>
<p>
The only problem was the performance cost. Some of these methods were very
expensive to run, and eagerly calling all methods of all objects just so I could
later access some of these was obviously a bad deal. The solution, clearly, was
to only call these methods when the map entries were actually accessed. And so
<code>lazy-map</code> was born.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/new-clojure-lib-lazy-map.html?source=rss#disqus_thread">Comment on this.</a></p>Using prettify-symbols in Clojure and Elisp without breaking indentation2015-11-09T00:00:00+00:00https://endlessparentheses.com//using-prettify-symbols-in-clojure-and-elisp-without-breaking-indentation<p>
<code>prettify-symbols-mode</code> is a very nice minor-mode that is a little too modest
for its own good. You can turn it on right now if you’re using a recent Emacs,
but it’ll do nothing more than turn <code>lambda</code> into <code>λ</code> in <code>emacs-lisp-mode</code>.
Still, it’s powerful and versatile and deserves that you give it a try. To
extend its feature-set you can install packages or customize it yourself, and
that’s what we’re here to do today, specifically in <code>clojure-mode</code>.
</p>
<p>
First of all, let’s make sure it’s turned on.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-prettify-symbols-mode</span> <span class="mi">1</span><span class="p">)</span>
<span class="c1">;; We're going to play with this below.</span>
<span class="p">(</span><span class="nb">defvar</span> <span class="nv">endless/clojure-prettify-alist</span> <span class="o">'</span><span class="p">())</span></code></pre></figure>
<p>
If you read the docstring for this mode, it’ll explain that any symbol can be
displayed as any character, so the first thing that comes to my mind is
displaying <code><=</code> or <code>>=</code> as <code>≤</code> or <code>≥</code>. But that comes with a drawback. Suddenly
that symbol is 1 character shorter, so Emacs is going to indent sexps
accordingly, and people reading your code will see bad indentation.
</p>
<p>
The solution is to configure <code>prettify-symbols-mode</code> to compose these symbols in
a special way. This feature is somewhat accidental, and wasn’t even documented
in the last release. The composition rules are slightly complicated to write,
and for that I’ll point to the docstrings of <a href="https://doc.endlessparentheses.com/Fun/compose-region"><code>compose-region</code></a> (see the third
argument) and <a href="https://doc.endlessparentheses.com/Var/reference-point-alist"><code>reference-point-alist</code></a>. Below are several examples you can toy
around with.
</p>
<p>
One way to fix the width, is to join two spaces together, and then stick the
inequality on top of them.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'endless/clojure-prettify-alist</span>
<span class="o">'</span><span class="p">(</span><span class="s">">="</span> <span class="o">.</span> <span class="p">(</span><span class="nv">?\s</span> <span class="p">(</span><span class="nv">Br</span> <span class="o">.</span> <span class="nv">Bl</span><span class="p">)</span> <span class="nv">?\s</span> <span class="p">(</span><span class="nv">Bc</span> <span class="o">.</span> <span class="nv">Bc</span><span class="p">)</span> <span class="nv">?</span><span class="err">≥</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'endless/clojure-prettify-alist</span>
<span class="o">'</span><span class="p">(</span><span class="s">"<="</span> <span class="o">.</span> <span class="p">(</span><span class="nv">?\s</span> <span class="p">(</span><span class="nv">Br</span> <span class="o">.</span> <span class="nv">Bl</span><span class="p">)</span> <span class="nv">?\s</span> <span class="p">(</span><span class="nv">Bc</span> <span class="o">.</span> <span class="nv">Bc</span><span class="p">)</span> <span class="nv">?</span><span class="err">≤</span><span class="p">)))</span></code></pre></figure>
<p>
However, I find this looks a little “too spacey”.<br />
<a href="/images/prettify-inequalities-2.png"><img src="/images/prettify-inequalities-2.png" alt="prettify-inequalities-2.png" /></a>
</p>
<p>
The option I prefer is to just add a small dot before the symbol. This makes it
clear that the symbol occupies the space of two characters, while still looking
nicer than a plain <code><=</code>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'endless/clojure-prettify-alist</span>
<span class="o">'</span><span class="p">(</span><span class="s">"<="</span> <span class="o">.</span> <span class="p">(</span><span class="nv">?</span><span class="err">·</span> <span class="p">(</span><span class="nv">Br</span> <span class="o">.</span> <span class="nv">Bl</span><span class="p">)</span> <span class="nv">?</span><span class="err">≤</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'endless/clojure-prettify-alist</span>
<span class="o">'</span><span class="p">(</span><span class="s">">="</span> <span class="o">.</span> <span class="p">(</span><span class="nv">?</span><span class="err">·</span> <span class="p">(</span><span class="nv">Br</span> <span class="o">.</span> <span class="nv">Bl</span><span class="p">)</span> <span class="nv">?</span><span class="err">≥</span><span class="p">)))</span></code></pre></figure>
<p>
Which will look like this:<br />
<a href="/images/prettify-inequalities-1.png"><img src="/images/prettify-inequalities-1.png" alt="prettify-inequalities-1.png" /></a>
</p>
<p>
Then there are the <code>-></code> and <code>->></code> macros, which are in dire need of a makeover
if you ask me. The solution I currently use is a spaced-out version of <code>🠊</code>
(you could also use <code>⇨</code>, <code>🡒</code>, or <code>→</code>).
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'endless/clojure-prettify-alist</span>
<span class="o">'</span><span class="p">(</span><span class="s">"->"</span> <span class="o">.</span> <span class="p">(</span><span class="nv">?\s</span> <span class="p">(</span><span class="nv">Br</span> <span class="o">.</span> <span class="nv">Bl</span><span class="p">)</span> <span class="nv">?\s</span> <span class="p">(</span><span class="nv">Bc</span> <span class="o">.</span> <span class="nv">Bc</span><span class="p">)</span> <span class="nv">?</span><span class="err">🠊</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'endless/clojure-prettify-alist</span>
<span class="o">'</span><span class="p">(</span><span class="s">"->>"</span> <span class="o">.</span> <span class="p">(</span><span class="nv">?\s</span> <span class="p">(</span><span class="nv">Br</span> <span class="o">.</span> <span class="nv">Bl</span><span class="p">)</span> <span class="nv">?\s</span> <span class="p">(</span><span class="nv">Br</span> <span class="o">.</span> <span class="nv">Bl</span><span class="p">)</span> <span class="nv">?\s</span>
<span class="p">(</span><span class="nv">Bc</span> <span class="o">.</span> <span class="nv">Br</span><span class="p">)</span> <span class="nv">?</span><span class="err">🠊</span> <span class="p">(</span><span class="nv">Bc</span> <span class="o">.</span> <span class="nv">Bl</span><span class="p">)</span> <span class="nv">?</span><span class="err">🠊</span><span class="p">)))</span></code></pre></figure>
<p>
Because the <code>🠊</code> character is wider than a regular character (at least on my
font), this turns out look quite nice.<br />
<a href="/images/prettify-threading-2.png"><img src="/images/prettify-threading-2.png" alt="prettify-threading-2.png" /></a>
</p>
<p>
If you don’t like that, there’s also the option of adding one or two dashes
inside the symbols to make our fake arrow prettier.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'endless/clojure-prettify-alist</span>
<span class="o">'</span><span class="p">(</span><span class="s">"->"</span> <span class="o">.</span> <span class="p">(</span><span class="nv">?-</span> <span class="p">(</span><span class="nv">Br</span> <span class="o">.</span> <span class="nv">Bc</span><span class="p">)</span> <span class="nv">?-</span> <span class="p">(</span><span class="nv">Br</span> <span class="o">.</span> <span class="nv">Bc</span><span class="p">)</span> <span class="nv">?></span><span class="p">)))</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'endless/clojure-prettify-alist</span>
<span class="o">'</span><span class="p">(</span><span class="s">"->>"</span> <span class="o">.</span> <span class="p">(</span><span class="nv">?\s</span> <span class="p">(</span><span class="nv">Br</span> <span class="o">.</span> <span class="nv">Bl</span><span class="p">)</span> <span class="nv">?\s</span> <span class="p">(</span><span class="nv">Br</span> <span class="o">.</span> <span class="nv">Bl</span><span class="p">)</span> <span class="nv">?\s</span>
<span class="p">(</span><span class="nv">Bl</span> <span class="o">.</span> <span class="nv">Bl</span><span class="p">)</span> <span class="nv">?-</span> <span class="p">(</span><span class="nv">Bc</span> <span class="o">.</span> <span class="nv">Br</span><span class="p">)</span> <span class="nv">?-</span> <span class="p">(</span><span class="nv">Bc</span> <span class="o">.</span> <span class="nv">Bc</span><span class="p">)</span> <span class="nv">?></span>
<span class="p">(</span><span class="nv">Bc</span> <span class="o">.</span> <span class="nv">Bl</span><span class="p">)</span> <span class="nv">?-</span> <span class="p">(</span><span class="nv">Br</span> <span class="o">.</span> <span class="nv">Br</span><span class="p">)</span> <span class="nv">?></span><span class="p">)))</span></code></pre></figure>
<p>
Here’s what they look like with this, compared to what they usually look like.<br />
<a href="/images/prettify-threading-1.png"><img src="/images/prettify-threading-1.png" alt="prettify-threading-1.png" /></a>
</p>
<p>
And finally, none of this will work if we don’t set it up. Note that
<code>clojure-mode</code> already defines <code>fn</code> to display as <code>λ</code>, so we don’t need to
configure this one.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">eval-after-load</span> <span class="ss">'clojure-mode</span>
<span class="o">'</span><span class="p">(</span><span class="k">setq</span> <span class="nv">clojure--prettify-symbols-alist</span>
<span class="p">(</span><span class="nb">append</span> <span class="nv">endless/clojure-prettify-alist</span>
<span class="nv">clojure--prettify-symbols-alist</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">eval-after-load</span> <span class="ss">'lisp-mode</span>
<span class="o">'</span><span class="p">(</span><span class="k">setq</span> <span class="nv">lisp--prettify-symbols-alist</span>
<span class="p">(</span><span class="nb">append</span> <span class="nv">endless/clojure-prettify-alist</span>
<span class="nv">lisp--prettify-symbols-alist</span><span class="p">)))</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/using-prettify-symbols-in-clojure-and-elisp-without-breaking-indentation.html?source=rss#disqus_thread">Comment on this.</a></p>clj-refactor — Unleash your Clojure wizard.2015-11-02T00:00:00+00:00https://endlessparentheses.com//clj-refactor-unleash-your-clojure-wizard<p>
When I first started learning Clojure, I was charmed by how well integrated
CIDER was with Emacs. In many ways, it felt just like writing Emacs-lisp.
Nowadays, that feeling has gone slightly past the goal mark, and there are
actually features I miss when I’m writing elisp. <a href="https://github.com/clojure-emacs/clj-refactor.el#readme">Clj-refactor</a> is one of them.
</p>
<p>
This package is a Swiss knife of refactoring utilities for Clojure. From simple
operations like turning a form into a <code>-></code> thread, to the more complex
situations like renaming entire namespaces, it’s hard to find something
clj-refactor can’t do.
</p>
<p>
I won’t list all of them here, they already have a nice <a href="https://github.com/clojure-emacs/clj-refactor.el/wiki">wiki page</a> for that. The
point here is to expose a few features (the ones I find most useful) and
hopefully that’ll get you started on the road to enlightenment. To see some of
the features in play (at a pretty quick pace), you can check out Magnar’s <a href="http://www.parens-of-the-dead.com/">Parens
of the dead</a> videos. I also plan on doing a proper and thorough video review of
this package in the future.
</p>
<p>
First, and most importantly, are the threading commands. These allow you to
instantly turn something like <code>(filter odd? (map inc (range n)))</code> into
</p>
<figure class="highlight"><pre><code class="language-clojure" data-lang="clojure"><span class="p">(</span><span class="nf">->></span><span class="w"> </span><span class="p">(</span><span class="nb">range</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w">
</span><span class="p">(</span><span class="nb">map</span><span class="w"> </span><span class="nb">inc</span><span class="p">)</span><span class="w">
</span><span class="p">(</span><span class="nb">filter</span><span class="w"> </span><span class="n">odd?</span><span class="p">))</span></code></pre></figure>
<p>
or back again. Clj-refactor default keybinds are pretty well thought out, but I
use these commands so frequently that I felt the need to shorten them to just 2
or 3 keys.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">with-eval-after-load</span> <span class="ss">'clj-refactor</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">cljr-thread-all-but-last</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">clj-refactor-map</span> <span class="s">"\C-ctf"</span> <span class="nf">#'</span><span class="nv">cljr-thread-first-all</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">clj-refactor-map</span> <span class="s">"\C-ctl"</span> <span class="nf">#'</span><span class="nv">cljr-thread-last-all</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">clj-refactor-map</span> <span class="s">"\C-cu"</span> <span class="nf">#'</span><span class="nv">cljr-unwind</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">clj-refactor-map</span> <span class="s">"\C-cU"</span> <span class="nf">#'</span><span class="nv">cljr-unwind-all</span><span class="p">))</span></code></pre></figure>
<p>
Next is another feature that comes up a lot, but this one doesn’t even take up a
keybind. When you type <code>/</code>, clj-refactor will look at the alias you’ve just
written, and try to add a require for it in the <code>ns</code> form. The current snapshot
release is very smart about it, and actually learns what alias you use for each
namespace by looking at other files in the same project. You can also define
your preferences with the <code>cljr-magic-require-namespaces</code> variable.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">with-eval-after-load</span> <span class="ss">'clj-refactor</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'cljr-magic-require-namespaces</span>
<span class="o">'</span><span class="p">(</span><span class="s">"s"</span> <span class="o">.</span> <span class="s">"clojure.string"</span><span class="p">)))</span></code></pre></figure>
<p>
Another command you should definitely memorize is <code>cljr-create-fn-from-example</code>.<br />
<a href="/images/create-fn-from-example.gif"><img src="/images/create-fn-from-example.gif" alt="create-fn-from-example.gif" /></a><br />
Instead of specifying a custom key for this one, we’ll just let the package
define all other keys for us.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">with-eval-after-load</span> <span class="ss">'clj-refactor</span>
<span class="p">(</span><span class="nv">cljr-add-keybindings-with-prefix</span> <span class="s">"\C-cr"</span><span class="p">))</span></code></pre></figure>
<p>
The nice thing about the default keys is that they’re mnemonic, and you might
remember I have an <a href="/the-toggle-map-and-wizardry.html">entire series</a> on this topic. The point being: it’s very easy
to remember that <code>cljr-create-fn-from-example</code> is bound to <kbd>C-c r f e</kbd> when you
can actually sing <i>“clojure refactor from example”</i> as you’re typing.
</p>
<p>
A few others I use a lot (and the keys they’re bound to) are <code>introduce-let</code> (<kbd>i
l</kbd>), <code>move-to-let</code> (<kbd>m l</kbd>), and <code>promote-function</code> (<kbd>p f</kbd>).
</p>
<p>
Lastly, some miscellaneous configurations that simply reflect my personal
preferences, but which you should be aware of nonetheless.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">cljr-auto-sort-ns</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">cljr-favor-prefix-notation</span> <span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">cljr-favor-private-functions</span> <span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">cljr-clojure-test-declaration</span>
<span class="s">"[clojure.test :refer :all]"</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/clj-refactor-unleash-your-clojure-wizard.html?source=rss#disqus_thread">Comment on this.</a></p>Changing the org-mode ellipsis2015-11-02T00:00:00+00:00https://endlessparentheses.com//changing-the-org-mode-ellipsis<p>
The dot-dot-dot ellipsis that org-mode uses to indicate hidden content is
usually just fine. It’s only when you’re staring at a document where every line
is a folded headline, that you start to feel like they’re a little too much “in
your face”. I have a few org files with thousands of lines and hundreds of
headlines, and changing that <code>...</code> to something shorter greatly reduces visual
clutter.
</p>
<p>
The more straightforward option is to use a proper ellipsis character (the same
effect with a third the length).
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">org-ellipsis</span> <span class="s">"…"</span><span class="p">)</span></code></pre></figure>
<p>
The one I’m currently using is a cornered arrow.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">org-ellipsis</span> <span class="s">"⤵"</span><span class="p">)</span></code></pre></figure>
<p>
Other interesting characters are <code>▼</code>, <code>↴</code>, <code>⬎</code>, <code>⤷</code>, and <code>⋱</code>.</p>
<p><a href="https://endlessparentheses.com/changing-the-org-mode-ellipsis.html?source=rss#disqus_thread">Comment on this.</a></p>Beacon — Never lose your cursor again2015-10-27T00:00:00+00:00https://endlessparentheses.com//beacon-never-lose-your-cursor-again<p>
What started out as a cute idea I was playing around with, eventually turned to
be one of my favorite packages. Beacon won’t help you type faster, code
better, or cure cancer like some of the other packages. Its effect is mostly
cosmetic, but with practical benefits. Put simply, if you turn on this minor
mode, whenever the window scrolls up or down a light will blink on your cursor.
That’s it.
</p>
<p>
At first, I thought this would be purely cosmetic. But my super scientific
experience has been that there’s actually a practical benefit to it. The amount
of brain energy you save and the level of eye strain you avoid by having this
simple “eye-guide” is noticeable, specially during those late-night writing
marathons.
</p>
<p>
Just install from Gelpa with the usual <kbd>M-x</kbd> <code>list-packages</code> and turn it on.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">beacon-mode</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">beacon-push-mark</span> <span class="mi">35</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">beacon-color</span> <span class="s">"#666600"</span><span class="p">)</span></code></pre></figure>
<p>
It has a lot of configuration options for you to specify situations where it
shouldn’t blink, as well as different details like duration, delay, size, and
color. Either have a look at the <a href="https://github.com/Malabarba/beacon">Readme</a> or just issue <kbd>M-x</kbd> <code>customize-group</code> to
see them all.
</p>
<p><a href="https://endlessparentheses.com/beacon-never-lose-your-cursor-again.html?source=rss#disqus_thread">Comment on this.</a></p>Improving LaTeX equations with font-lock2015-10-19T00:00:00+00:00https://endlessparentheses.com//improving-latex-equations-with-font-lock<p>
It’s difficult for me to spend much time interacting with a major-mode and not
find something to tweak. Even when that mode is my oldest companion in the world
of Emacs, something will surely pop up. So it’s only fitting that in the final
week of my thesis submission deadline I start tinkering with <code>latex-mode</code> again.
</p>
<p>
I wrote the following snippet after compiling a recent Emacs snapshot which
defines a myriad of rules for <code>prettify-symbols-mode</code> in <code>tex-mode</code>. Equations
become much easier to read when you replace the verbose math symbols like
<code>\alpha</code> with <code>α</code>, but that only makes all the LaTeX styling commands stand out
even more. Suddenly, all those <code>\left</code>, <code>\right</code>, <code>\!</code> and <code>\;</code>, are sticking
out like that mole on a hag’s nose.
</p>
<p>
What do we do about that? We hide them away. Not completely, of course. We still
need to see those commands. We just tuck them away a little, where we <i>can</i> see
them without <i>having</i> to see them.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">defface</span> <span class="nv">endless/unimportant-latex-face</span>
<span class="o">'</span><span class="p">((</span><span class="no">t</span> <span class="ss">:height</span> <span class="mf">0.7</span>
<span class="ss">:inherit</span> <span class="nv">font-lock-comment-face</span><span class="p">))</span>
<span class="s">"Face used on less relevant math commands."</span><span class="p">)</span>
<span class="p">(</span><span class="nv">font-lock-add-keywords</span>
<span class="ss">'latex-mode</span>
<span class="o">`</span><span class="p">((</span><span class="o">,</span><span class="p">(</span><span class="nv">rx</span> <span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nb">and</span> <span class="s">"\\"</span> <span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nv">any</span> <span class="s">",.!;"</span><span class="p">)</span>
<span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nb">or</span> <span class="s">"left"</span> <span class="s">"right"</span>
<span class="s">"big"</span> <span class="s">"Big"</span><span class="p">)</span>
<span class="nv">symbol-end</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">any</span> <span class="s">"_^"</span><span class="p">)))</span>
<span class="mi">0</span> <span class="ss">'endless/unimportant-latex-face</span> <span class="nv">prepend</span><span class="p">))</span>
<span class="ss">'end</span><span class="p">)</span></code></pre></figure>
<p>
Here’s a sample of what it might look like for you.
</p>
<div class="figure">
<p><a href="/images/latex-unimportant-font-lock.png"><img src="/images/latex-unimportant-font-lock.png" alt="latex-unimportant-font-lock.png" /></a>
</p>
</div>
<p><a href="https://endlessparentheses.com/improving-latex-equations-with-font-lock.html?source=rss#disqus_thread">Comment on this.</a></p>Paradoxical Hydras2015-10-15T00:00:00+00:00https://endlessparentheses.com//paradoxical-hydras<p>
Quick update to say that I’m quite pleased with the <a href="https://github.com/abo-abo/hydra">Hydra</a> package. Turns out
it’s not just eye-candy on top of keymaps, it also offers convenient
functionality that is rather dull to implement on plain keymaps.
</p>
<p>
If you use <a href="https://github.com/Malabarba/paradox">Paradox</a>, next time you upgrade you’ll find that a sneaky hydra has
made its way into it. It’s a very simple one under the <kbd>f</kbd> key, but it’s a nice
improvement over the plain keymap.
</p>
<p>
Also, if you happen to follow the Emacs master branch, you’ll see that this new
hydra is actually not that simple and includes some filtering options that the
previous keymap didn’t even have. You have <a href="https://github.com/kaushalmodi/.emacs.d/blob/17e5cdc46e2e0a71070bf63e98be2ba727d1a579/setup-files/setup-paradox.el#L21-L41">Kaushal Modi</a> to thank for this.
</p>
<p>
If you’re the kind of person who hates mythical creatures, you can revert to the
plain keymap with this snippet.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">eval-after-load</span> <span class="ss">'paradox</span>
<span class="o">'</span><span class="p">(</span><span class="nv">define-key</span> <span class="nv">paradox-menu-mode-map</span> <span class="s">"f"</span>
<span class="nf">#'</span><span class="nv">paradox--filter-map</span><span class="p">))</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/paradoxical-hydras.html?source=rss#disqus_thread">Comment on this.</a></p>Multiple Cursors keybinds2015-10-12T00:00:00+00:00https://endlessparentheses.com//multiple-cursors-keybinds<p>
The Multiple Cursors package has been given much praise throughout the
Emacsphere. It has a smaller use-case than keyboard macros, but it is usually
quicker and just plain looks awesome. To make full use of its commands, I
combine two of the concepts I've explained here before, <a href="/Meta-Binds-Part-1%253A-Drunk-in-the-Dark.html">rebinding <code>M-number</code></a> and
<a href="/the-toggle-map-and-wizardry.html">intuitive keymaps</a>.
</p>
<p>
I won’t go into what this package is, as Magnar already has <a href="http://emacsrocks.com/e13.html">a whole video on
that</a>. Instead, I’ll just explain how I use it.
</p>
<p>
Firstly, the following keys make the most sense to me.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">require</span> <span class="ss">'multiple-cursors-core</span><span class="p">)</span>
<span class="c1">;; This is globally useful, so it goes under `C-x', and `m'</span>
<span class="c1">;; for "multiple-cursors" is easy to remember.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">ctl-x-map</span> <span class="s">"\C-m"</span> <span class="nf">#'</span><span class="nv">mc/mark-all-dwim</span><span class="p">)</span>
<span class="c1">;; Usually, both `C-x C-m' and `C-x RET' invoke the</span>
<span class="c1">;; `mule-keymap', but that's a waste of keys. Here we put it</span>
<span class="c1">;; _just_ under `C-x RET'.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">ctl-x-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"<return>"</span><span class="p">)</span> <span class="nv">mule-keymap</span><span class="p">)</span>
<span class="c1">;; Remember `er/expand-region' is bound to M-2!</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"M-3"</span><span class="p">)</span> <span class="nf">#'</span><span class="nv">mc/mark-next-like-this</span><span class="p">)</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"M-4"</span><span class="p">)</span> <span class="nf">#'</span><span class="nv">mc/mark-previous-like-this</span><span class="p">)</span></code></pre></figure>
<p>
These three commands do most of the hard work. I'll never forget about them, so
their keys don't need to be clever, they just need to be quick. Having
<code>mc/mark-next-like-this</code> <a href="/where-do-you-bind-expand-region-.html">right next to <code>er/expand-region</code></a> is really the best
place, although now-a-days I use <code>mc/mark-all-dwim</code> almost exclusively.
</p>
<p>
Something that took me a long time to figure out is that you can unmark stuff
you just marked. Previously, whenever I marked-next-like-this once too many I’d
just abort and start again. It makes sense to bind this to the same keys as
above with the <kbd>Shift</kbd> modifier.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;; These vary between keyboards. They're supposed to be</span>
<span class="c1">;; Shifted versions of the two above.</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"M-£"</span><span class="p">)</span> <span class="nf">#'</span><span class="nv">mc/unmark-next-like-this</span><span class="p">)</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"M-$"</span><span class="p">)</span> <span class="nf">#'</span><span class="nv">mc/unmark-previous-like-this</span><span class="p">)</span></code></pre></figure>
<p>
On the other hand, this package has a myriad of commands which are extremely
useful on a less-than-daily basis, and this is where we invoke the power of
intuitive key-maps.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-prefix-command</span> <span class="ss">'endless/mc-map</span><span class="p">)</span>
<span class="c1">;; C-x m is usually `compose-mail'. Bind it to something</span>
<span class="c1">;; else if you use this command.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">ctl-x-map</span> <span class="s">"m"</span> <span class="ss">'endless/mc-map</span><span class="p">)</span>
<span class="c1">;;; Really really nice!</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/mc-map</span> <span class="s">"i"</span> <span class="nf">#'</span><span class="nv">mc/insert-numbers</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/mc-map</span> <span class="s">"h"</span> <span class="nf">#'</span><span class="nv">mc-hide-unmatched-lines-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/mc-map</span> <span class="s">"a"</span> <span class="nf">#'</span><span class="nv">mc/mark-all-like-this</span><span class="p">)</span>
<span class="c1">;;; Occasionally useful</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/mc-map</span> <span class="s">"d"</span>
<span class="nf">#'</span><span class="nv">mc/mark-all-symbols-like-this-in-defun</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/mc-map</span> <span class="s">"r"</span> <span class="nf">#'</span><span class="nv">mc/reverse-regions</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/mc-map</span> <span class="s">"s"</span> <span class="nf">#'</span><span class="nv">mc/sort-regions</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/mc-map</span> <span class="s">"l"</span> <span class="nf">#'</span><span class="nv">mc/edit-lines</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/mc-map</span> <span class="s">"\C-a"</span>
<span class="nf">#'</span><span class="nv">mc/edit-beginnings-of-lines</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/mc-map</span> <span class="s">"\C-e"</span>
<span class="nf">#'</span><span class="nv">mc/edit-ends-of-lines</span><span class="p">)</span></code></pre></figure>
<p>
Note how easy these keys are to remember. I use <code>mc/insert-numbers</code> barely once
a week, but I never forget it's bound to <kbd>C-x m i</kbd> (and when I do use it, it’s a
godsend). Other commands that I rarely use but save me a lot of trouble when I
do are <code>sort/reverse-regions</code> and <code>mc-hide-unmatched-lines-mode</code>.
</p>
<p><a href="https://endlessparentheses.com/multiple-cursors-keybinds.html?source=rss#disqus_thread">Comment on this.</a></p>Better time-stamps in org-export2015-10-05T00:00:00+00:00https://endlessparentheses.com//better-time-stamps-in-org-export<p>
<code>org-mode</code> has a very useful command, <a href="https://doc.endlessparentheses.com/Fun/org-time-stamp"><code>org-time-stamp</code></a>, which helps you insert
dates from a calendar. So you can quickly type <kbd>C-c . RET</kbd> to insert
<code><2015-10-05 Mon></code>, for instance. These time-stamps are used by Org in a variety
of ways, so they are wrapped in <code><></code> to make them easy to parse. The downside
being that they look less than optimal when exported.
</p>
<p>
I was bit by this again today while updating the <a href="/donations-donations-donations.html">post on donations</a>, and I
finally decided to look for a way to fix it. Of course, <code>org-mode</code> is nothing if
not configurable, so the answer wasn’t very far away.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'org-export-filter-timestamp-functions</span>
<span class="nf">#'</span><span class="nv">endless/filter-timestamp</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/filter-timestamp</span> <span class="p">(</span><span class="nv">trans</span> <span class="nv">back</span> <span class="nv">_comm</span><span class="p">)</span>
<span class="s">"Remove <> around time-stamps."</span>
<span class="p">(</span><span class="nv">pcase</span> <span class="nv">back</span>
<span class="p">((</span><span class="nb">or</span> <span class="o">`</span><span class="nv">jekyll</span> <span class="o">`</span><span class="nv">html</span><span class="p">)</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span> <span class="s">"&[lg]t;"</span> <span class="s">""</span> <span class="nv">trans</span><span class="p">))</span>
<span class="p">(</span><span class="o">`</span><span class="nv">latex</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span> <span class="s">"[<>]"</span> <span class="s">""</span> <span class="nv">trans</span><span class="p">))))</span></code></pre></figure>
<p>
The loyal readers might notice how similar this is to the second <code>lambda</code> we
used for <a href="/embedding-youtube-videos-with-org-mode-links.html">exporting Youtube links</a>. Org is quite consistent in its use of export
filters.
</p>
<p>
The above is enough to remove the surrounding <code><></code>, but we can still make it
better. The <code>YYYY-MM-DD weekday</code> format isn’t commonly used in prose, so let’s
switch that as well.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">setq-default</span> <span class="nv">org-display-custom-times</span> <span class="no">t</span><span class="p">)</span>
<span class="c1">;;; Before you ask: No, removing the <> here doesn't work.</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">org-time-stamp-custom-formats</span>
<span class="o">'</span><span class="p">(</span><span class="s">"<%d %b %Y>"</span> <span class="o">.</span> <span class="s">"<%d/%m/%y %a %H:%M>"</span><span class="p">))</span></code></pre></figure>
<p>
As a bonus, this format will also be used to display time-stamps in your
<code>org-mode</code> buffers. If don’t want that, you can let-bind the
<code>org-display-custom-times</code> variable when calling the export function, instead of
setting it globally.
</p>
<p>
<b>If any of this doesn’t work for you</b>, you might need to update your Org
package. Fortunately, <a href="https://elpa.gnu.org/packages/org.html">Org is on GElpa</a>, so anyone can do that with <kbd>M-x</kbd>
<code>list-packages</code>.
</p>
<p><a href="https://endlessparentheses.com/better-time-stamps-in-org-export.html?source=rss#disqus_thread">Comment on this.</a></p>Predicting the future with M-n2015-09-28T00:00:00+00:00https://endlessparentheses.com//predicting-the-future-with-the-m-n-key<p>
This is one of those small functionalities that makes your life considerably
easier, and yet a surprising number of people don’t know about it. When Emacs
prompts you for something in the minibuffer, you might be aware that you can
navigate back and forth in the prompt’s history with <kbd>M-p</kbd> and <kbd>M-n</kbd>, but did you
know you can even step into the future?
</p>
<p>
Well, sort of… At least that’s the spirit. If you type <kbd>M-p</kbd> at the prompt,
Emacs fills the prompt with previous items from the history. On the other hand,
if you type <kbd>M-n</kbd>, Emacs will try to guess from context what your <i>next</i> input
is going to be. The meaning of this depends on the command (some of these
require Emacs 24.5).
</p>
<ul class="org-ul">
<li>In file prompts, hitting <kbd>M-n</kbd> fills in the name of the current file. This is
very useful with <a href="https://doc.endlessparentheses.com/Fun/write-file"><code>write-file</code></a> or with dired commands like <code>copy</code> or <code>move</code>.</li>
<li><kbd>M-x</kbd> tries to find a command name under point.</li>
<li>In <a href="https://doc.endlessparentheses.com/Fun/query-replace-regexp"><code>query-replace-regexp</code></a> it fills in the symbol at point, which is usually a
pretty good guess of what you want to replace. And you can hit <kbd>M-n</kbd> a second
time to get <code>\_<symbol-delimiters\_></code> around the symbol.</li>
<li>As Marco points out in the comments, if you have two dired windows open, file
or directory prompts in one window fill in the directory name of the other
window.</li>
</ul>
<p>
These are the ones I use most often, but I’m sure there are more commands that
support this feature, and it’s always worth trying it out with your favorite
ones.
</p>
<p><a href="https://endlessparentheses.com/predicting-the-future-with-the-m-n-key.html?source=rss#disqus_thread">Comment on this.</a></p>Flycheck a directory and report the results2015-09-21T00:00:00+00:00https://endlessparentheses.com//flycheck-a-directory-and-report-the-results<p>
This weekend I found myself doing some heavy-weight refactoring in CIDER. This
is the kind of situation where Flycheck helps a lot, but I still needed it to do
a bit more. Every time I made a significant change to a file, I had to visit 3
or 5 other files and trigger Flycheck on each one of them. It wasn’t long before
I decide there had to be a way to just Flycheck a whole directory.
</p>
<p>
Enter <code>endless/flycheck-dir</code>. This command runs Flycheck on all files in the
current directory and reports the result to the <code>*Compile-Log*</code> buffer. You can
then navigate through all issues for the entire directory by <kbd>TAB</kbd>-ing through
the buffer or using <a href="https://doc.endlessparentheses.com/Fun/next-error"><code>next-error</code></a>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-key</span> <span class="nv">flycheck-command-map</span> <span class="s">"d"</span>
<span class="nf">#'</span><span class="nv">endless/flycheck-dir</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/flycheck-dir</span> <span class="p">(</span><span class="nv">dir</span><span class="p">)</span>
<span class="s">"Run flycheck for each file in current directory.
Results are reported in a compilation buffer."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"DDirectory: "</span><span class="p">)</span>
<span class="p">(</span><span class="nv">displaying-byte-compile-warnings</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">p</span> <span class="no">nil</span><span class="p">))</span>
<span class="p">(</span><span class="nv">with-current-buffer</span> <span class="p">(</span><span class="nv">get-buffer-create</span>
<span class="nv">byte-compile-log-buffer</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">default-directory</span> <span class="nv">dir</span><span class="p">)</span>
<span class="p">(</span><span class="nb">unless</span> <span class="p">(</span><span class="nb">eq</span> <span class="nv">major-mode</span> <span class="ss">'compilation-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nv">compilation-mode</span><span class="p">))</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">point-max</span><span class="p">))</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">inhibit-read-only</span> <span class="no">t</span><span class="p">))</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">"\n\xc\n\n"</span><span class="p">))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">p</span> <span class="p">(</span><span class="nv">point</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">dolist</span> <span class="p">(</span><span class="nv">file</span> <span class="p">(</span><span class="nv">directory-files</span> <span class="s">"./"</span> <span class="no">nil</span>
<span class="s">"\\`[^\\.].*\\'"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">endless/-flycheck-file</span> <span class="nv">file</span><span class="p">))</span>
<span class="p">(</span><span class="nv">with-selected-window</span> <span class="p">(</span><span class="nv">display-buffer</span>
<span class="nv">byte-compile-log-buffer</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="nv">p</span><span class="p">)</span>
<span class="p">(</span><span class="nv">recenter</span> <span class="mi">1</span><span class="p">)))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/-report-error</span> <span class="p">(</span><span class="nv">fmt</span> <span class="k">&rest</span> <span class="nv">args</span><span class="p">)</span>
<span class="s">"Print an error on `byte-compile-log-buffer'."</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">inhibit-read-only</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="nv">fill-prefix</span> <span class="s">" "</span><span class="p">))</span>
<span class="p">(</span><span class="nv">with-current-buffer</span> <span class="nv">byte-compile-log-buffer</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">l</span> <span class="p">(</span><span class="nv">point</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">"\n"</span> <span class="p">(</span><span class="nb">apply</span> <span class="nf">#'</span><span class="nb">format</span> <span class="nv">fmt</span> <span class="nv">args</span><span class="p">))</span>
<span class="p">(</span><span class="nv">fill-region</span> <span class="p">(</span><span class="nb">1+</span> <span class="nv">l</span><span class="p">)</span> <span class="p">(</span><span class="nv">point</span><span class="p">))))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/-flycheck-file</span> <span class="p">(</span><span class="nv">file</span><span class="p">)</span>
<span class="s">"Check FILE and report to `byte-compile-log-buffer'."</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">was-visited</span> <span class="p">(</span><span class="nv">find-buffer-visiting</span> <span class="nv">file</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">with-current-buffer</span> <span class="p">(</span><span class="nb">or</span> <span class="nv">was-visited</span>
<span class="p">(</span><span class="k">progn</span> <span class="p">(</span><span class="nv">find-file</span> <span class="nv">file</span><span class="p">)</span>
<span class="p">(</span><span class="nv">current-buffer</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">ignore-errors</span> <span class="p">(</span><span class="nv">flycheck-buffer</span><span class="p">))</span>
<span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nv">flycheck-running-p</span><span class="p">)</span>
<span class="p">(</span><span class="nv">accept-process-output</span> <span class="no">nil</span> <span class="mf">0.1</span><span class="p">))</span>
<span class="p">(</span><span class="nv">pcase</span> <span class="nv">flycheck-last-status-change</span>
<span class="p">((</span><span class="nb">or</span> <span class="o">`</span><span class="nv">errored</span> <span class="o">`</span><span class="nv">suspicious</span><span class="p">)</span>
<span class="p">(</span><span class="nv">endless/-report-error</span>
<span class="s">"%s: Something wrong here!"</span>
<span class="p">(</span><span class="nv">file-name-nondirectory</span> <span class="p">(</span><span class="nv">buffer-file-name</span><span class="p">))))</span>
<span class="p">(</span><span class="o">`</span><span class="nv">finished</span>
<span class="p">(</span><span class="nb">dolist</span> <span class="p">(</span><span class="nv">e</span> <span class="nv">flycheck-current-errors</span><span class="p">)</span>
<span class="p">(</span><span class="nv">endless/-report-error</span>
<span class="s">"%s:%s:%s:%s: %s"</span>
<span class="p">(</span><span class="nv">file-name-nondirectory</span> <span class="p">(</span><span class="nv">buffer-file-name</span><span class="p">))</span>
<span class="p">(</span><span class="nv">flycheck-error-line</span> <span class="nv">e</span><span class="p">)</span>
<span class="p">(</span><span class="nv">flycheck-error-column</span> <span class="nv">e</span><span class="p">)</span>
<span class="p">(</span><span class="nv">flycheck-error-level</span> <span class="nv">e</span><span class="p">)</span>
<span class="p">(</span><span class="nv">flycheck-error-message</span> <span class="nv">e</span><span class="p">))))))</span>
<span class="p">(</span><span class="k">if</span> <span class="nv">was-visited</span>
<span class="p">(</span><span class="nv">bury-buffer</span> <span class="nv">was-visited</span><span class="p">)</span>
<span class="p">(</span><span class="nv">kill-buffer</span> <span class="p">(</span><span class="nv">current-buffer</span><span class="p">))))))</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/flycheck-a-directory-and-report-the-results.html?source=rss#disqus_thread">Comment on this.</a></p>Improving page (section) navigation2015-09-14T00:00:00+00:00https://endlessparentheses.com//improving-page-navigation<p>
If you’ve taken the time to browse some Elisp source files, you’ve no doubt run
into that odd little <code>^L</code>, a.k.a. the form feed character. Emacs uses these
white space characters as page delimiters. This makes for a very convenient way
to split a file into sections, and quickly navigate between them. I won’t go too
deep into them, as Eric James has already written a great <a href="http://ericjmritz.name/2015/08/29/using-page-breaks-in-gnu-emacs/">crash course on pages</a>
that you should go check out.
</p>
<p>
What I wanted to write about is the way that I do page navigation in Emacs.
Firstly, I find the default keys to be nothing short of abhorrent. Take a prefix
key with the <kbd>Control</kbd> modifier, attach to it a non-modified key, and then make that key
be something not-so-easy to hit, like <kbd>]</kbd>, and you have the recipe for painful
fingers.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-key</span> <span class="nv">prog-mode-map</span> <span class="s">"\C-x\C-n"</span> <span class="nf">#'</span><span class="nv">forward-page</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">prog-mode-map</span> <span class="s">"\C-x\C-p"</span> <span class="nf">#'</span><span class="nv">backward-page</span><span class="p">)</span></code></pre></figure>
<p>
These keys would normally be bound to <code>set-goal-column</code> and <code>mark-page</code>, which
I’ve never ever <i>ever</i> used (in fact, the former is disabled by default).
</p>
<p>
Then there’s a minor peeve. In some corner cases Emacs might leave the cursor at
the bottom of the screen after moving. Here we make sure that never happens.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/-recenter-advice</span> <span class="p">(</span><span class="k">&rest</span> <span class="nv">_</span><span class="p">)</span>
<span class="s">"Recenter to page start."</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">called-interactively-p</span> <span class="ss">'any</span><span class="p">)</span>
<span class="p">(</span><span class="nv">recenter</span> <span class="mi">5</span><span class="p">)))</span>
<span class="c1">;; Requires Emacs 24.5</span>
<span class="p">(</span><span class="nv">advice-add</span> <span class="nf">#'</span><span class="nv">backward-page</span> <span class="ss">:after</span>
<span class="nf">#'</span><span class="nv">endless/-recenter-advice</span><span class="p">)</span>
<span class="p">(</span><span class="nv">advice-add</span> <span class="nf">#'</span><span class="nv">forward-page</span> <span class="ss">:after</span>
<span class="nf">#'</span><span class="nv">endless/-recenter-advice</span><span class="p">)</span></code></pre></figure>
<p>
And then there’s the best part. It turns out you don’t need the form-feed
character to delimit pages. That’s important because some languages aren’t that
nice about them, and some dev teams might prefer you don’t stick those
<code>^L</code> all over the place. In Clojure, for instance, <code>cljfmt</code> confuses it for a
blank line and freaks out a little.
</p>
<p>
Fortunately, Elisp style already recommends using <code>;;;</code> to indicate comment
sections, and the form feed character is most commonly placed right above these
sections. So why not use that instead?
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">page-delimiter</span>
<span class="p">(</span><span class="nv">rx</span> <span class="nv">bol</span> <span class="p">(</span><span class="nb">or</span> <span class="s">"\f"</span> <span class="s">";;;"</span><span class="p">)</span>
<span class="p">(</span><span class="nb">not</span> <span class="p">(</span><span class="nv">any</span> <span class="s">"#"</span><span class="p">))</span> <span class="p">(</span><span class="nb">*</span> <span class="nv">not-newline</span><span class="p">)</span> <span class="s">"\n"</span>
<span class="p">(</span><span class="nb">*</span> <span class="p">(</span><span class="nb">*</span> <span class="nv">blank</span><span class="p">)</span> <span class="p">(</span><span class="nv">opt</span> <span class="s">";"</span> <span class="p">(</span><span class="nb">*</span> <span class="nv">not-newline</span><span class="p">))</span> <span class="s">"\n"</span><span class="p">)))</span>
<span class="c1">;; Expanded regexp:</span>
<span class="c1">;; "^;;;[^#].*\n\\(?:[[:blank:]]*\\(?:;.*\\)?\n\\)*"</span></code></pre></figure>
<p>
The regexp above is a bit special. We’re setting the page delimiter to be a
<code>;;;</code> at the start of a line, plus any number of empty lines or comment lines
that follow it (that <code>#</code> part is to exclude <code>;;;###autoload</code> cookies).
</p>
<p>
Consequently, when we hit <kbd>C-x C-n</kbd> or <kbd>C-x C-p</kbd>, the point is left right at the
start of the first code-line after the <code>;;;</code>. That’s usually where I want to be,
and it works even on buffers without <code>^L</code>, Clojure and Elisp. No doubt you can
extended that to your programming language of choice by replacing the semicolons
with the appropriate comment character.
</p>
<p>
Even better, why not write up a general solution based on the <a href="https://doc.endlessparentheses.com/Var/comment-start"><code>comment-start</code></a>
variable?
</p>
<p><a href="https://endlessparentheses.com/improving-page-navigation.html?source=rss#disqus_thread">Comment on this.</a></p>Donations, donations, donations2015-09-07T00:00:00+00:00https://endlessparentheses.com//donations-donations-donations<p>
Every computer user, to some extend, is a user of Open Source Software (even if
most of them are oblivious to that). This is only possible because the
developers of these pieces of software have donated their time to us, who are
nothing short of complete strangers to them. These are regular people, with just
as much free time as you or I—sometimes a bit more, sometimes even less.
</p>
<p>
There are many ways we can thank them for this. Just saying “thank you” is one
way, anyone likes to be appreciated and would be happy to hear that they did
something that helped you. Another way is to spread the word, share the
software, and thus help them help more people. A third option is to help back,
send a patch or a PR, write documentation, or even write your own software to
help your own complete strangers, adding one more link to this selfless chain.
And lastly, you can also donate.
</p>
<p>
A few weeks ago, I setup a Gratipay page for Endless Parentheses. If you’d like
to help, you can donate to the blog or to my packages by clicking on the button
below.
</p>
<a href="https://gratipay.com/endless-parentheses/" onclick="javascript:ga('send', 'event', 'Button', 'Click', 'Gratipay Button');" target="_blank"><img src="/images/gratipay.svg" class="centered-image" alt="Say thanks on Gratipay"/></a>
<p>
But that was weeks ago, it was something else that prompted me to write this
post today. Over the last four days, two of Emacs most popular packages have
setup new donation channels, so I saw this as a chance to group all these links
together into a post.
</p>
<p>
<a href="https://twitter.com/bbatsov/status/639456568300470272">CIDER</a> now has <a href="https://salt.bountysource.com/teams/cider">a page on Salt</a> (Bountysource’s new monthly donations scheme), and
<a href="https://twitter.com/magit_emacs/status/640585548806135812">Magit</a> has got its <a href="https://gratipay.com/magit/">Gratipay page</a> up and running again after some down time. Both
of these are true feats of Emacs development, way beyond your usual weekend
programming exercise, so consider donating if they make your life easier. They
also offer several other channels, so see these links to their respective
manuals.
</p>
<ul class="org-ul">
<li><a href="http://magit.vc/donations.html">Magit! Help making it better</a></li>
<li><a href="https://github.com/clojure-emacs/cider#donations">CIDER - Donations</a></li>
<li><a href="https://gratipay.com/org-mode/">Org-mode Gratipay</a></li>
<li><a href="https://github.com/flycheck/flycheck#readme">Flycheck</a></li>
<li><a href="https://github.com/syl20bnr/spacemacs#readme">Spacemacs</a></li>
</ul>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Update <span class="timestamp-wrapper"><span class="timestamp">05 Oct 2015</span></span></h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Bastien Guerry points out that Org-mode now has <a href="https://gratipay.com/org-mode/">Gratipay page</a> as well. You can
also donate to it via Paypal on <a href="http://orgmode.org/">the website</a> (top-right corner). I shouldn’t have
to tell you that this blog probably wouldn’t exist without org-mode!
</p>
<p>
Two more huge and important Emacs projects I forgot to mention before are
<a href="https://github.com/flycheck/flycheck#readme">Flycheck</a> and <a href="https://github.com/syl20bnr/spacemacs#readme">Spacemacs</a>, and you can find their Paypal links on their respective
Readmes.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/donations-donations-donations.html?source=rss#disqus_thread">Comment on this.</a></p>Nameless, less is more2015-09-06T00:00:00+00:00https://endlessparentheses.com//nameless-less-is-more<p>
<a href="https://github.com/Malabarba/Nameless">Nameless</a> is an Emacs package for hiding namespace prefixes in elisp code. It
is a short and simple minor-mode that changes the display, without changing the
contents of the buffer. Using it is as simple as turning it on, there’s no need
to change your package in any way.
</p>
<div class="figure">
<p><a href="/images/nameless-on.png"><img src="/images/nameless-on.png" alt="nameless-on.png" /></a>
</p>
</div>
<p>
<a href="/introducing-names-practical-namespaces-for-emacs-lisp.html">Nine months</a> ago I introduced <a href="https://github.com/Malabarba/names">Names</a>, a package that allows you to write elisp
code inside a namespace. Since then, I’ve used it on two of my packages without
issues (as have several other Emacs hackers) and it’s been delightful. I’ve been
hacking elisp for several years now, but stripping away all those namespace
prefixes really makes it more pleasurable to write and read code.
</p>
<p>
Still, there’s no denying that it’s a bit heavy-handed. It’s quite a large
library that your package has to depend on, and it changes the way you write the
file itself, which means it messes with anything from <code>grep</code> to <code>find-function</code>.
Nameless, on the other hand, hooks onto <code>font-lock-mode</code> and changes the way
symbols are displayed. This means it doesn’t interfere with any other tools you
might use for code management or navigation. At the end of the day, it tries a
lot less and therefore does a lot more.
</p>
<p>
Nameless is available from <a href="https://elpa.gnu.org/packages/nameless.html">GNU Elpa</a>. To give it a try, just <kbd>M-x</kbd>
<code>package-install</code> it, and turn on the minor-mode wherever you want.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'emacs-lisp-mode-hook</span>
<span class="nf">#'</span><span class="nv">nameless-mode-from-hook</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">nameless-global-aliases</span>
<span class="o">'</span><span class="p">((</span><span class="s">"fl"</span> <span class="o">.</span> <span class="s">"font-lock"</span><span class="p">)</span>
<span class="p">(</span><span class="s">"s"</span> <span class="o">.</span> <span class="s">"seq"</span><span class="p">)</span>
<span class="p">(</span><span class="s">"me"</span> <span class="o">.</span> <span class="s">"macroexp"</span><span class="p">)</span>
<span class="p">(</span><span class="s">"c"</span> <span class="o">.</span> <span class="s">"cider"</span><span class="p">)</span>
<span class="p">(</span><span class="s">"q"</span> <span class="o">.</span> <span class="s">"queue"</span><span class="p">)))</span></code></pre></figure>
<p>
See the <a href="https://github.com/Malabarba/Nameless">Readme</a> for more information.</p>
<p><a href="https://endlessparentheses.com/nameless-less-is-more.html?source=rss#disqus_thread">Comment on this.</a></p>Org-mode subtrees and file-local variables2015-09-01T00:00:00+00:00https://endlessparentheses.com//org-mode-subtrees-and-file-local-variables<p>
As you grow accustomed to fine-tuning your Emacs experience, it’s not unusual to
start using local variables in your files. These are specified as comment lines
at the end of the file, and are extremely practical in a number of scenarios. Here’s
a very simple org file.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">* Some headline
Ramblings no one cares about.
* Another headline
Thoughtfully rescinded.
# Local Variables:
# fill-column: 666
# End:</code></pre></figure>
<p>
The only problem is that <code>org-mode</code> thinks that everything after a headline is
part of its contents, which is clearly not the case here. One example where this
is bad is when you want to shuffle the headlines around with <kbd>M-↓</kbd> or <kbd>M-↑</kbd>, and
you end up with something like this.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">* Another headline
Thoughtfully rescinded.
# Local Variables:
# fill-column: 666
# End:
* Some headline
Ramblings no one cares about.</code></pre></figure>
<p>
I asked about that on <a href="http://emacs.stackexchange.com/q/2236/50">Emacs.StackExchange</a> and got a very simple solution: just
add a “dummy” level-1 headline before the local variable specification.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">* Some headline
Ramblings no one cares about.
* Another headline
Thoughtfully rescinded.
* COMMENT Footer
# Local Variables:
# fill-column: 42
# End:</code></pre></figure>
<p>
Now you can move, archive, and refile all your headlines as you wish, without
fear of destroying your precious variables. You can even <a href="/fine-tuning-subtree-visibility-in-org-mode.html">fine-tune the
visibility</a> to <code>folded</code>, so that the footer is always folded, and you won’t have
to see those variables at all.
</p>
<p>
But this wouldn’t be our weekly Endless Parentheses if I didn’t give you some
code to practice your elisp. The following command is just like <a href="https://doc.endlessparentheses.com/Fun/end-of-buffer"><code>end-of-buffer</code></a>,
except it places point before the footer. If invoked again, it will move to the
real end of buffer.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/org-eob</span> <span class="p">()</span>
<span class="s">"Move to end of content, then end of buffer."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nb">unless</span> <span class="p">(</span><span class="nv">use-region-p</span><span class="p">)</span>
<span class="p">(</span><span class="nv">push-mark</span><span class="p">))</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">looking-at-p</span> <span class="s">"\n\\* COMMENT Footer"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">point-max</span><span class="p">))</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">point-min</span><span class="p">))</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">search-forward</span> <span class="s">"\n* COMMENT Footer"</span>
<span class="no">nil</span> <span class="ss">'noerror</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">match-beginning</span> <span class="mi">0</span><span class="p">))</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">point-max</span><span class="p">)))))</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">org-mode-map</span> <span class="nv">[remap</span> <span class="nv">end-of-buffer]</span>
<span class="nf">#'</span><span class="nv">endless/org-eob</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/org-mode-subtrees-and-file-local-variables.html?source=rss#disqus_thread">Comment on this.</a></p>Making Ispell work with org-mode2015-08-24T00:00:00+00:00https://endlessparentheses.com//ispell-and-org-mode<p>
If you’ve every tried to do some spell-checking in <code>org-mode</code> you know how
finicky that can be. Ispell is happy to check absolutely anything, even code
blocks and property drawers! When you’re blogging about code-snippets from an
org file this annoyance quickly turns into irritation. Here’s how you fix it.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/org-ispell</span> <span class="p">()</span>
<span class="s">"Configure `ispell-skip-region-alist' for `org-mode'."</span>
<span class="p">(</span><span class="nv">make-local-variable</span> <span class="ss">'ispell-skip-region-alist</span><span class="p">)</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'ispell-skip-region-alist</span> <span class="o">'</span><span class="p">(</span><span class="nv">org-property-drawer-re</span><span class="p">))</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'ispell-skip-region-alist</span> <span class="o">'</span><span class="p">(</span><span class="s">"~"</span> <span class="s">"~"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'ispell-skip-region-alist</span> <span class="o">'</span><span class="p">(</span><span class="s">"="</span> <span class="s">"="</span><span class="p">))</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'ispell-skip-region-alist</span> <span class="o">'</span><span class="p">(</span><span class="s">"^#\\+BEGIN_SRC"</span> <span class="o">.</span> <span class="s">"^#\\+END_SRC"</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'org-mode-hook</span> <span class="nf">#'</span><span class="nv">endless/org-ispell</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/ispell-and-org-mode.html?source=rss#disqus_thread">Comment on this.</a></p>A comment-or-uncomment-sexp command2015-08-17T00:00:00+00:00https://endlessparentheses.com//a-comment-or-uncomment-sexp-command<p>
Commenting is a very frequent piece of a programmer’s workflow, and it’s
important to make it seamless and simple. For the more statemental languages,
that’s as easy as writing a <a href="/implementing-comment-line.html">custom <code>comment-line</code> command</a>. However, when
you’re writing in Lisp languages, that just won’t do. Trying to comment out
lines in a sexp-oriented structure, feels a lot like trying to hit a nail with a
heavy screwdriver—it sometimes gets the job done, but it mostly just leads to
frustration.
</p>
<p>
That said, a <code>comment-sexp</code> command is considerably more complicated to write.
Not because commenting sexps is hard, but because it is quite difficult to
identify sexps when <i>removing</i> comments. Still, I’m nothing if not stubborn. So
after much hair pulling and teeth gritting, I have finally found a version I’m
happy with.
</p>
<div class="figure">
<p><a href="/images/comment-or-uncomment-sexp.gif"><img src="/images/comment-or-uncomment-sexp.gif" alt="comment-or-uncomment-sexp.gif" /></a>
</p>
</div>
<p>
The gif above speaks for itself, so I’ll just give you the code and let you play
with it.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">uncomment-sexp</span> <span class="p">(</span><span class="k">&optional</span> <span class="nv">n</span><span class="p">)</span>
<span class="s">"Uncomment a sexp around point."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"P"</span><span class="p">)</span>
<span class="p">(</span><span class="k">let*</span> <span class="p">((</span><span class="nv">initial-point</span> <span class="p">(</span><span class="nv">point-marker</span><span class="p">))</span>
<span class="p">(</span><span class="nv">inhibit-field-text-motion</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="nv">p</span><span class="p">)</span>
<span class="p">(</span><span class="nv">end</span> <span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">elt</span> <span class="p">(</span><span class="nv">syntax-ppss</span><span class="p">)</span> <span class="mi">4</span><span class="p">)</span>
<span class="p">(</span><span class="nv">re-search-backward</span> <span class="nv">comment-start-skip</span>
<span class="p">(</span><span class="nv">line-beginning-position</span><span class="p">)</span>
<span class="no">t</span><span class="p">))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">p</span> <span class="p">(</span><span class="nv">point-marker</span><span class="p">))</span>
<span class="p">(</span><span class="nv">comment-forward</span> <span class="p">(</span><span class="nv">point-max</span><span class="p">))</span>
<span class="p">(</span><span class="nv">point-marker</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">beg</span> <span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nv">forward-line</span> <span class="mi">0</span><span class="p">)</span>
<span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nb">not</span> <span class="p">(</span><span class="nv">bobp</span><span class="p">))</span>
<span class="p">(</span><span class="nb">=</span> <span class="nv">end</span> <span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nv">comment-forward</span> <span class="p">(</span><span class="nv">point-max</span><span class="p">))</span>
<span class="p">(</span><span class="nv">point</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">forward-line</span> <span class="mi">-1</span><span class="p">))</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">line-end-position</span><span class="p">))</span>
<span class="p">(</span><span class="nv">re-search-backward</span> <span class="nv">comment-start-skip</span>
<span class="p">(</span><span class="nv">line-beginning-position</span><span class="p">)</span>
<span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="nb">ignore-errors</span>
<span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nv">looking-at-p</span> <span class="nv">comment-start-skip</span><span class="p">)</span>
<span class="p">(</span><span class="nv">forward-char</span> <span class="mi">-1</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">point-marker</span><span class="p">))))</span>
<span class="p">(</span><span class="nb">unless</span> <span class="p">(</span><span class="nb">=</span> <span class="nv">beg</span> <span class="nv">end</span><span class="p">)</span>
<span class="p">(</span><span class="nv">uncomment-region</span> <span class="nv">beg</span> <span class="nv">end</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="nv">p</span><span class="p">)</span>
<span class="c1">;; Indentify the "top-level" sexp inside the comment.</span>
<span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nb">ignore-errors</span> <span class="p">(</span><span class="nv">backward-up-list</span><span class="p">)</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="nb">>=</span> <span class="p">(</span><span class="nv">point</span><span class="p">)</span> <span class="nv">beg</span><span class="p">))</span>
<span class="p">(</span><span class="nv">skip-chars-backward</span> <span class="p">(</span><span class="nv">rx</span> <span class="p">(</span><span class="nv">syntax</span> <span class="nv">expression-prefix</span><span class="p">)))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">p</span> <span class="p">(</span><span class="nv">point-marker</span><span class="p">)))</span>
<span class="c1">;; Re-comment everything before it. </span>
<span class="p">(</span><span class="nb">ignore-errors</span>
<span class="p">(</span><span class="nv">comment-region</span> <span class="nv">beg</span> <span class="nv">p</span><span class="p">))</span>
<span class="c1">;; And everything after it.</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="nv">p</span><span class="p">)</span>
<span class="p">(</span><span class="nv">forward-sexp</span> <span class="p">(</span><span class="nb">or</span> <span class="nv">n</span> <span class="mi">1</span><span class="p">))</span>
<span class="p">(</span><span class="nv">skip-chars-forward</span> <span class="s">"\r\n[:blank:]"</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb"><</span> <span class="p">(</span><span class="nv">point</span><span class="p">)</span> <span class="nv">end</span><span class="p">)</span>
<span class="p">(</span><span class="nb">ignore-errors</span>
<span class="p">(</span><span class="nv">comment-region</span> <span class="p">(</span><span class="nv">point</span><span class="p">)</span> <span class="nv">end</span><span class="p">))</span>
<span class="c1">;; If this is a closing delimiter, pull it up.</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="nv">end</span><span class="p">)</span>
<span class="p">(</span><span class="nv">skip-chars-forward</span> <span class="s">"\r\n[:blank:]"</span><span class="p">)</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">eq</span> <span class="mi">5</span> <span class="p">(</span><span class="nb">car</span> <span class="p">(</span><span class="nv">syntax-after</span> <span class="p">(</span><span class="nv">point</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">delete-indentation</span><span class="p">))))</span>
<span class="c1">;; Without a prefix, it's more useful to leave point where</span>
<span class="c1">;; it was.</span>
<span class="p">(</span><span class="nb">unless</span> <span class="nv">n</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="nv">initial-point</span><span class="p">))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">comment-sexp--raw</span> <span class="p">()</span>
<span class="s">"Comment the sexp at point or ahead of point."</span>
<span class="p">(</span><span class="nv">pcase</span> <span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nv">bounds-of-thing-at-point</span> <span class="ss">'sexp</span><span class="p">)</span>
<span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nv">skip-chars-forward</span> <span class="s">"\r\n[:blank:]"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">bounds-of-thing-at-point</span> <span class="ss">'sexp</span><span class="p">)))</span>
<span class="p">(</span><span class="o">`</span><span class="p">(</span><span class="o">,</span><span class="nv">l</span> <span class="o">.</span> <span class="o">,</span><span class="nv">r</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="nv">r</span><span class="p">)</span>
<span class="p">(</span><span class="nv">skip-chars-forward</span> <span class="s">"\r\n[:blank:]"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nv">comment-region</span> <span class="nv">l</span> <span class="nv">r</span><span class="p">))</span>
<span class="p">(</span><span class="nv">skip-chars-forward</span> <span class="s">"\r\n[:blank:]"</span><span class="p">))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">comment-or-uncomment-sexp</span> <span class="p">(</span><span class="k">&optional</span> <span class="nv">n</span><span class="p">)</span>
<span class="s">"Comment the sexp at point and move past it.
If already inside (or before) a comment, uncomment instead.
With a prefix argument N, (un)comment that many sexps."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"P"</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nb">elt</span> <span class="p">(</span><span class="nv">syntax-ppss</span><span class="p">)</span> <span class="mi">4</span><span class="p">)</span>
<span class="p">(</span><span class="nb"><</span> <span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nv">skip-chars-forward</span> <span class="s">"\r\n[:blank:]"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">point</span><span class="p">))</span>
<span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nv">comment-forward</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="nv">point</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">uncomment-sexp</span> <span class="nv">n</span><span class="p">)</span>
<span class="p">(</span><span class="nb">dotimes</span> <span class="p">(</span><span class="nv">_</span> <span class="p">(</span><span class="nb">or</span> <span class="nv">n</span> <span class="mi">1</span><span class="p">))</span>
<span class="p">(</span><span class="nv">comment-sexp--raw</span><span class="p">))))</span></code></pre></figure>
<p>
And, of course, don’t forget to bind it.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-M-;"</span><span class="p">)</span> <span class="nf">#'</span><span class="nv">comment-or-uncomment-sexp</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/a-comment-or-uncomment-sexp-command.html?source=rss#disqus_thread">Comment on this.</a></p>Markdown style link IDs in org-mode2015-08-10T00:00:00+00:00https://endlessparentheses.com//markdown-style-link-ids-in-org-mode<p>
Link handling and exporting is one of the most versatile aspects of org-mode.
Did you know you can make org-mode understand Markdown style link IDs?
</p>
<p>
You should already know <a href="/use-org-mode-links-for-absolutely-anything.html">org-links can do absolutely anything</a>, but it takes a
little creativity to know how to apply that. <a href="http://emacs.stackexchange.com/q/594/50">Here's the question</a> by Kaushal that
prompted this over at <a href="http://emacs.stackexchange.com/">Emacs.StackExchange</a>.
</p>
<blockquote>
<p>
At times, I need to use the same link at multiple places in a long document. For
those cases it would be useful to have link IDs like in markdown.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">This is [an example][some-id] reference-style link. </code></pre></figure>
<p>
Then, anywhere in the document, you define your link label like this, on a line by itself:
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text"> [some-id]: http://example.com/</code></pre></figure>
</blockquote>
<p>
<a href="http://emacs.stackexchange.com/a/599/50">The solution</a> I came up with defines three different functions, so I’ll let you
follow the link instead of clogging up this blog post. The result is that you
can define link IDs like this anywhere in your document (probably the top or the
bottom).
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">#+LINK-ID: wiki http://www.emacswiki.org</code></pre></figure>
<p>
And then you can use that URL in links throughout the document by writing them
like this.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">Here is a [[lid:wiki][wiki link]], and here
is [[lid:wiki][another one]].</code></pre></figure>
<p><a href="https://endlessparentheses.com/markdown-style-link-ids-in-org-mode.html?source=rss#disqus_thread">Comment on this.</a></p>Transposing keybinds in Emacs2015-08-03T00:00:00+00:00https://endlessparentheses.com//transposing-keybinds-in-emacs<p>
Transposing is another of <a href="/hungry-delete-mode.html">those features</a> that I really miss when not in Emacs.
It took me several months of actively reminding myself in order to finally
incorporate it into my regular arsenal. Now, not a day goes by that I don’t
transpose a few lines, and usually some words and sexps as well, but the
usefulness of <code>transpose-char</code> still seems to elude me.
</p>
<p>
I hear this command is great at fixing some typos, so perhaps I don’t find it as
useful because <a href="/ispell-and-abbrev-the-perfect-auto-correct.html">auto-correct</a> takes care of those for me. Meanwhile, the other
transpose commands have the very different role of refactoring code or moving
text around (<code>transpose-lines</code> is specially nice with <a href="/longlines-mode-in-latex.html">one-line-per-sentence text</a>).
</p>
<p>
Whatever the reason, the point is that <kbd>C-t</kbd> is too good a key for a command
that I’m not going to use and <kbd>C-x C-t</kbd> is too long for a command I use so
often. The answer, of course, is to swap the two around.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-set-key</span> <span class="s">"\C-t"</span> <span class="nf">#'</span><span class="nv">transpose-lines</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">ctl-x-map</span> <span class="s">"\C-t"</span> <span class="nf">#'</span><span class="nv">transpose-chars</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/transposing-keybinds-in-emacs.html?source=rss#disqus_thread">Comment on this.</a></p>Embedding Youtube videos with org-mode links2015-07-28T00:00:00+00:00https://endlessparentheses.com//embedding-youtube-videos-with-org-mode-links<p>
If you’re a frequent reader, no doubt you noticed an embedded Youtube video on a
post <a href="/debugger-improvements-in-cider-0-10-0.html">a couple of weeks ago</a>. Youtube makes it pretty simple to embed videos, they
give you the entire <code>iframe</code> HTML code to use, but this wouldn’t really be Emacs
if we couldn’t make things just a little bit easier. Just add the snippet below
to your init file, and you’re good to go.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defvar</span> <span class="nv">yt-iframe-format</span>
<span class="c1">;; You may want to change your width and height.</span>
<span class="p">(</span><span class="nv">concat</span> <span class="s">"<iframe width=\"440\""</span>
<span class="s">" height=\"335\""</span>
<span class="s">" src=\"https://www.youtube.com/embed/%s\""</span>
<span class="s">" frameborder=\"0\""</span>
<span class="s">" allowfullscreen>%s</iframe>"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">org-add-link-type</span>
<span class="s">"yt"</span>
<span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">handle</span><span class="p">)</span>
<span class="p">(</span><span class="nv">browse-url</span>
<span class="p">(</span><span class="nv">concat</span> <span class="s">"https://www.youtube.com/embed/"</span>
<span class="nv">handle</span><span class="p">)))</span>
<span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">path</span> <span class="nv">desc</span> <span class="nv">backend</span><span class="p">)</span>
<span class="p">(</span><span class="nv">cl-case</span> <span class="nv">backend</span>
<span class="p">(</span><span class="nv">html</span> <span class="p">(</span><span class="nb">format</span> <span class="nv">yt-iframe-format</span>
<span class="nv">path</span> <span class="p">(</span><span class="nb">or</span> <span class="nv">desc</span> <span class="s">""</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">latex</span> <span class="p">(</span><span class="nb">format</span> <span class="s">"\href{%s}{%s}"</span>
<span class="nv">path</span> <span class="p">(</span><span class="nb">or</span> <span class="nv">desc</span> <span class="s">"video"</span><span class="p">))))))</span></code></pre></figure>
<p>
To use this, just write your org links in the following way (optionally adding a description).
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">[[yt:A3JAlWM8qRM]]</code></pre></figure>
<p>
When you export to HTML, this will produce that same inlined snippet that
Youtube specifies. The advantage (over simply writing out the <code>iframe</code>) is that
this link can be clicked in <code>org-mode</code>, and can be exported to other formats as
well.
</p>
<p><a href="https://endlessparentheses.com/embedding-youtube-videos-with-org-mode-links.html?source=rss#disqus_thread">Comment on this.</a></p>Fixing DOuble CApitals as you type2015-07-20T00:00:00+00:00https://endlessparentheses.com//fixing-double-capitals-as-you-type<p>
This is something that’s bothered me for a very long time. My pinky is slow when
it comes to releasing the <kbd>Shift</kbd> key, and frequently leads to typos. MOst typos
(hitting letters in the wrong order) are already covered by <a href="/ispell-and-abbrev-the-perfect-auto-correct.html">auto-correction</a>, but
there’s another common typo that it doesn’t fix. EVery now and then, I’ll start
a sentence with two uppercase letters.
</p>
<p>
After the billionth time that it happened to me, I finally took the time to
write up <a href="http://emacs.stackexchange.com/q/13970/50">a question</a> on <a href="http://emacs.stackexchange.com/">Emacs.StackExchange</a>. The answers were fast, and so high
quality that I don’t even have improvements to suggest. Instead, I’ll just point
you straight to <a href="http://emacs.stackexchange.com/a/13975/50">Dan's answer</a>, and show how I activate it.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'text-mode-hook</span> <span class="nf">#'</span><span class="nv">dubcaps-mode</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/fixing-double-capitals-as-you-type.html?source=rss#disqus_thread">Comment on this.</a></p>You won’t believe this simple trick for using Emacs with Java!2015-07-19T00:00:00+00:00https://endlessparentheses.com//you-won-t-believe-this-simple-trick-for-using-emacs-with-java<p>
Install JDEE! Ok, maybe that’s an overstatement. JDEE is far from simple, and it
hasn’t been able to keep up very well since Java 1.4. However, thanks to Stephen
Leake & folks, that might be starting to change. <a href="https://github.com/jdee-emacs/jdee/">JDEE is now on Github</a>, and it
could definitely use your help.
</p>
<p>
Stephen has already been updating the code base for the recent Emacs versions,
and other people have shown interest in this as well. There are already some
<a href="https://github.com/jdee-emacs/jdee/issues">productive discussions</a> going on in the issues, and some of those items are
things anyone could do. So if you like Emacs and you use Java, now this is a
great chance to give something back.
</p>
<p>
However, if JDEE is not to your taste, don’t forget you have <a href="https://github.com/senny/emacs-eclim">emacs-eclim</a> and
<a href="https://github.com/m0smith/malabar-mode">malabar-mode</a> too. I’ve never used malabar-mode myself, but last time I had to
deal with Java I gave eclim a try and it worked pretty well. You do need an
eclipse server running in the background, but once you do it facilitates a good
amount of features.
</p>
<p><a href="https://endlessparentheses.com/you-won-t-believe-this-simple-trick-for-using-emacs-with-java.html?source=rss#disqus_thread">Comment on this.</a></p>Debugger improvements in Cider 0.10.02015-07-13T00:00:00+00:00https://endlessparentheses.com//debugger-improvements-in-cider-0-10-0<p>
Over the last couple of weeks I had a few more days to work on the Cider
debugger, and it’s getting a slew of improvements on the next release
(<code>0.10.0</code>). This starts with a complete rewrite, so it now supports almost
everything, and ends with some small features and UI improvements. Without
further delay, here’s a video.
</p>
<p>
<iframe width="440" height="335" src="https://www.youtube.com/embed/A3JAlWM8qRM" frameborder="0" allowfullscreen></iframe>
</p>
<p>
There’s a lot going on in there, so let’s start dissecting.
</p>
<ol class="org-ol">
<li>Maps are now supported. This may sound trivial, but the previous version
couldn’t debug inside maps (it’s trickier than it seems).
</li>
<li>More specifically, prepost-maps are supported! If this sounds sweet, that’s
because it is. <a href="/images/cider-debug-prepost.png"><img src="/images/cider-debug-prepost.png" alt="cider-debug-prepost.png" /></a>
</li>
<li>Even some code-rewriting stuff is supported, like the beloved threading macros.
<a href="/images/cider-debug-threading-macros.png"><img src="/images/cider-debug-threading-macros.png" alt="cider-debug-threading-macros.png" /></a>
</li>
<li>Function literals are also supported.
<a href="/images/cider-debug-function-literals.png"><img src="/images/cider-debug-function-literals.png" alt="cider-debug-function-literals.png" /></a>
</li>
<li>The <kbd>n</kbd>, <kbd>c</kbd>, <kbd>i</kbd>, and <kbd>q</kbd> keys were already available in the previous
version, so I’ll just link you to the <a href="/cider-debug-a-visual-interactive-debugger-for-clojure.html">previous post</a> on them.
</li>
<li>The <kbd>o</kbd> key, showcased halfway through the video, moves you *o*ut of a sexp,
without moving back in. It’s useful in <code>while</code> loops and in <code>map</code>-like
operations.
</li>
<li>The <kbd>l</kbd> key presents an inspector buffer detailing local variables.
<a href="/images/cider-debug-inspect-locals.png"><img src="/images/cider-debug-inspect-locals.png" alt="cider-debug-inspect-locals.png" /></a>
</li>
<li>Unlike in the previous version, you are allowed to move around and do other
stuff while the debugger waits for input. You can even evaluate stuff in the
current lexical environment with <kbd>e</kbd> or the usual <kbd>C-x C-e</kbd>.
</li>
<li>In addition to the command used the video (which simply debugs an entire
function) you can also debug specific forms with <code>#dbg</code>, and you can place a
single breakpoint anywhere with <code>#break</code>. If you use one of these, just
evaluate the form with your usual evaluation commands (like <kbd>C-x C-e</kbd> or <kbd>C-c
C-c</kbd>) and you’re good to go.
</li>
<li>Functions which are currently instrumented are marked with a red box around
the name.
<a href="/images/cider-debug-red-box.png"><img src="/images/cider-debug-red-box.png" alt="cider-debug-red-box.png" /></a>
</li>
<li>You can list all instrumented functions with <kbd>M-x</kbd>
<code>cider-browse-instrumented-defs</code>.
<a href="/images/cider-debug-browse-instrumented.png"><img src="/images/cider-debug-browse-instrumented.png" alt="cider-debug-browse-instrumented.png" /></a>
</li>
</ol>
<p>
The next planned step for the debugger is ClojureScript support, but that’s
likely going to take a while. In the meantime, why not try the Cider <code>0.10.0</code>
snapshots and help us find any bugs?
</p>
<p><a href="https://endlessparentheses.com/debugger-improvements-in-cider-0-10-0.html?source=rss#disqus_thread">Comment on this.</a></p>Applying Markup to Strings in org-mode2015-07-07T00:00:00+00:00https://endlessparentheses.com//applying-markup-to-strings-in-org-mode<p>
Normally, <code>org-mode</code> ignores your attempts to markup text that starts with
" or '. That’s probably a safe measure because <code>"~/"</code> is a very
common string to write but <code>~</code> is one of Org’s markup elements.
</p>
<p>
Fixing that is a simple matter, but it takes a bit of digging around. We just
need to remove those two characters from the 3rd element of
<a href="https://doc.endlessparentheses.com/Var/org-emphasis-regexp-components"><code>org-emphasis-regexp-components</code></a>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;; This HAS to come before (require 'org)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">org-emphasis-regexp-components</span>
<span class="o">'</span><span class="p">(</span><span class="s">" ('\"{“”"</span>
<span class="s">"- .,!?;''“”\")}/\\“”"</span>
<span class="s">" \r\n,"</span>
<span class="s">"."</span>
<span class="mi">1</span><span class="p">))</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/applying-markup-to-strings-in-org-mode.html?source=rss#disqus_thread">Comment on this.</a></p>Fine-tuning subtree visibility in org-mode2015-06-29T00:00:00+00:00https://endlessparentheses.com//fine-tuning-subtree-visibility-in-org-mode<p>
Org is one of those packages that you can use for a lifetime and still not know
all of its features. One of the first things you learn is how to use the
<code>#+STARTUP</code> header to define the initial visibility of headlines when you first
open an org-mode file. But did you know you can also use that on a per headline
basis?
</p>
<p>
Besides the usual 4 attributes you can chose from for the header (<code>overview</code>,
<code>content</code>, <code>showall</code> and <code>showeverything</code>), you can also assign similar options
to the <code>VISIBILITY</code> property of any headline. The <a href="http://orgmode.org/manual/Initial-visibility.html#Initial-visibility">specific options</a> are <code>folded</code>,
<code>children</code>, <code>content</code>, and <code>all</code>, and their names are fairly self-explanatory.
</p>
<p>
As an example, remember this screenshot of my <code>init.org</code> from the <a href="/how-i-blog-one-year-of-posts-in-a-single-org-file.html">birthday post</a>?
</p>
<div class="figure">
<p><a href="/images/init-org-1.png"><img src="/images/init-org-1.png" alt="init-org-1.png" /></a>
</p>
</div>
<p>
To achieve that, you just had to set the <code>* init.el</code> headline to have
<code>:VISIBILITY: children</code>, along with the usual startup header you see in the
image. I was almost implementing this myself, when I decided to Google first and
wasn’t surprised to find org-mode already had it.
</p>
<p><a href="https://endlessparentheses.com/fine-tuning-subtree-visibility-in-org-mode.html?source=rss#disqus_thread">Comment on this.</a></p>How I blog: One year of posts in a single org file2015-06-26T00:00:00+00:00https://endlessparentheses.com//how-i-blog-one-year-of-posts-in-a-single-org-file<p>
When this blog was conceived, I decided that I wanted it to be entirely
contained in a single org file, and that this would also be my Emacs init file.
On the blog’s <a href="/init-org-Without-org-mode.html">very first post</a> I explained how to implement the latter, an init
file that also serves other purposes. Today, Endless Parentheses turns 1 year
old, and it’s time to explain the former, how to turn a file into a blog.
</p>
<p>
As is usually the case with first birthdays, the child has no clue of what’s
going on, and it’s really just an excuse to indulge the parents into praising
themselves for keeping the kid alive 12 whole months. As such, this will not be
a productive post. Expect no code snippets, no Emacs news, and no productivity
tips, as I release myself from my usual shackles and go off on a tangent for a
change. Still, just in case someone else likes the idea, I’ve <a href="https://github.com/Malabarba/ox-jekyll-subtree">pushed the code</a> to
Github.
</p>
<p>
Before anything else, it should go without saying that the content of the posts
is written in <code>org-mode</code>. The engine I use for exporting is a large wrapper
around <a href="https://github.com/yoshinari-nomura/org-octopress/blob/master/ox-jekyll.el">ox-jekyll</a>, and the posts are all pushed to Github and rendered by their
built-in Jekyll support.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Why keep a blog in a single file?</h2>
<div class="outline-text-2" id="text-1">
<p>
First of all because org, once you learn its knobs and bobs, is just plain
powerful. This is all the more true because the post contents are also in org
format, so you’re effectively removing one layer of distance between you and the
content. For example, if I want to reschedule a post, I just find it with <kbd>C-c
C-j</kbd> and hit <kbd>C-c C-s</kbd>; whereas, if the posts were separate files, I’d have to
find it in <code>dired</code>, then visit it, and <i>then</i> hit <kbd>C-c C-s</kbd>.
</p>
<p>
This is a small difference, but it applies all around. If I want to link to a
previous post, I find it with <kbd>C-c C-j</kbd> and then move back with <kbd>C-u C-SPC</kbd>, all
without leaving the buffer. When I look at the posts list, the tags are listed
right beside the title, I don’t have to open a file to see them.
</p>
<p>
Now let me be honest with you. I didn’t predict these advantages before I
started the blog, so it’s not why I chose this approach. Rather, it was linked
to the fact that I wanted to blog about all these elisp snippets I had built
over years and accumulated in my init file.
</p>
<p>
You see, if posts were separate files I would have to copy the snippets to a
separate org file, and then write about them there, and <i>then</i> export them to
Jekyll. In this scenario, I just know I would eventually change some snippet (a
healthy init file is a fluid creature) and forget to update the corresponding
org file, and the thought of leaving out-dated code lying around sent a chill
through my spine. Not to mention, this whole flow of “init file → org post →
jekyll post” has one layer too many for my taste, and redundancy is an evil I
slay with a chainsaw.
</p>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">How it works</h2>
<div class="outline-text-2" id="text-2">
<p>
First of all, here’s what I see when Emacs starts up.
</p>
<div class="figure">
<p><a href="/images/init-org-1.png"><img src="/images/init-org-1.png" alt="init-org-1.png" /></a>
</p>
</div>
<p>
If you read (and remember) the <a href="/init-org-Without-org-mode.html">first post</a>, you’ll know that the actual init file
is composed of code blocks inside that <code>init.el</code> headline. Everything else is
text. Also, notice that last <code>SEQ_TODO</code> header. It specifies that headlines in
this file have three possible <code>TODO</code> states, <code>TODO</code>, <code>READY</code> and <code>DONE</code>.
</p>
<div class="figure">
<p><a href="/images/init-org-meta-binds.png"><img src="/images/init-org-meta-binds.png" alt="init-org-meta-binds.png" /></a>
</p>
</div>
<p>
Every post is marked with one of these states, and that is what defines them.
<code>TODO</code> is something I plan on writing about; <code>READY</code> is, well, ready to publish;
and <code>DONE</code> is published.
</p>
<p>
This three-states setup has several uses, mainly:
</p>
<ul class="org-ul">
<li>I can review my entire history and my future schedule with a <a href="http://emacs.stackexchange.com/a/680/50">custom agenda
command</a>, leveraging all the features of <code>org-agenda</code>.
</li>
<li>Whenever I change a snippet, and the headline above it is marked <code>DONE</code>, I’m
immediately reminded to update the post (as simple as <kbd>C-c b</kbd>). So I’ll never
leave outdated code around.
</li>
<li>When Monday arrives sooner than expected, and I didn’t write anything new, I
can issue <kbd>C-c / T READY</kbd> and get an <a href="https://doc.endlessparentheses.com/Fun/org-sparse-tree"><code>org-sparse-tree</code></a> of all <code>READY</code> posts.
</li>
</ul>
<div class="figure">
<p><a href="/images/init-org-ready-state.png"><img src="/images/init-org-ready-state.png" alt="init-org-ready-state.png" /></a>
</p>
</div>
<p>
See how practical that is? This file is not just the blog and the init file,
it’s also the future posts queue and the “vague ideas” list. All without having
to do manual maintenance. Of course, the headline is automatically marked <code>DONE</code>
when posted.
</p>
<p>
Then there are a few more advantages that arise simply from the fact we’re using
org-mode.
</p>
<ul class="org-ul">
<li>I can physically move posts around with <kbd>M-up/down</kbd> and <kbd>M-S-right/left</kbd>, to
whatever order makes more sense, instead of being constrained by alphabetical
or historical order of files.
</li>
<li>By nesting several posts under a headline, any tags or properties applied to
the parent are inherited by the posts. (Killing redundancy, remember?)
<div class="figure">
<p><a href="/images/init-org-news.png"><img src="/images/init-org-news.png" alt="init-org-news.png" /></a>
</p>
</div>
</li>
<li>The <kbd>C-c C-j</kbd> (<a href="https://doc.endlessparentheses.com/Fun/org-goto"><code>org-goto</code></a>) command makes it a breeze to jump around.
</li>
<li>It feels more Emacsy than anything else you could possibly do.
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3">Last, but not least</h2>
<div class="outline-text-2" id="text-3">
<p>
This part applies to any of the org blogging methods (not just my setup), but
still, the org-export engine is extensive and even more so if you know where to
hack it (org links, in particular, are <a href="/use-org-mode-links-for-absolutely-anything.html">extremely versatile</a>).
</p>
<p>
With a single <kbd>C-c b</kbd>, Emacs will spell-check the contents, export to Jekyll,
clean up some links, move the file to the right directory, bring up
<code>magit-status</code>, and even save a commit message to the kill-ring. You could
even have it commit and push automatically too, if you’re a bit of a thrill
seeker.
</p>
</div>
</div>
<div id="outline-container-sec-4" class="outline-2">
<h2 id="sec-4">Good night, and thanks for coming</h2>
<div class="outline-text-2" id="text-4">
<p>
Well, now that I’ve finished monologuing, the cake is completely gone, there’s
more candy on the floor than on the tables, and I think I hear a child crying
somewhere, so it’s clearly time to wind down the party. Thanks for reading, and
I mean that generally. Thanks for commenting too. Thanks for the emails and the
tweets. Thanks for the bug reports and the pull requests, and any other form of
positive interaction. This was a fun year.
</p>
<p>
Oh, and thanks for coming to the party. Little EP totally didn’t notice that
you didn’t bring a gift. (Though he might notice next year… just saying.)
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/how-i-blog-one-year-of-posts-in-a-single-org-file.html?source=rss#disqus_thread">Comment on this.</a></p>Create Github PRs from Emacs with Magit (again)2015-06-22T00:00:00+00:00https://endlessparentheses.com//create-github-prs-from-emacs-with-magit<p>
I don’t usually dedicate an entire post to something I’ve already done in a
previous one, but this nugget is so useful it deserves the attention. Remember
how you can <a href="/easily-create-github-prs-from-magit.html">create Github PRs</a> straight from Magit? Magit <code>2.1.0</code> is barely
<a href="https://twitter.com/magit_emacs/status/611204423906426880">two weeks away</a>, and it brings so many (awesome) changes that our little snippet is
going to break.
</p>
<p>
Luckily for you, I’ve been using Magit’s next version for a couple of months
now, so I have a fix all shiny and ready for you. This function is also slightly
more robust, and fixes some corner-case issues in the other. So if you found
that the previous function didn’t cover your use-case, you should definitely try
the new one.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/visit-pull-request-url</span> <span class="p">()</span>
<span class="s">"Visit the current branch's PR on Github."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">browse-url</span>
<span class="p">(</span><span class="nb">format</span> <span class="s">"https://github.com/%s/pull/new/%s"</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span>
<span class="s">"\\`.+github\\.com:\\(.+\\)\\.git\\'"</span> <span class="s">"\\1"</span>
<span class="p">(</span><span class="nv">magit-get</span> <span class="s">"remote"</span>
<span class="p">(</span><span class="nv">magit-get-remote</span><span class="p">)</span>
<span class="s">"url"</span><span class="p">))</span>
<span class="p">(</span><span class="nb">cdr</span> <span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nv">magit-get-remote-branch</span><span class="p">)</span>
<span class="p">(</span><span class="nv">user-error</span> <span class="s">"No remote branch"</span><span class="p">))))))</span></code></pre></figure>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">eval-after-load</span> <span class="ss">'magit</span>
<span class="o">'</span><span class="p">(</span><span class="nv">define-key</span> <span class="nv">magit-mode-map</span> <span class="s">"v"</span>
<span class="nf">#'</span><span class="nv">endless/visit-pull-request-url</span><span class="p">))</span></code></pre></figure>
<p>
Note that I’ve changed the keybind to <kbd>v</kbd>. In Magit <code>2.1</code>, that key is bound in
buffer overlays, not on the mode-map, which means we can just bind it in the map
and we’ll get a semi “do-what-I-mean” functionality.
</p>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Update <span class="timestamp-wrapper"><span class="timestamp">24 Jan 2016</span></span></h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
The latest Magit from Melpa deals with branches in a slightly different way, so
the code above won’t quite work. If you run into that problem, the update below
should work fine.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/visit-pull-request-url</span> <span class="p">()</span>
<span class="s">"Visit the current branch's PR on Github."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">browse-url</span>
<span class="p">(</span><span class="nb">format</span> <span class="s">"https://github.com/%s/pull/new/%s"</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span>
<span class="s">"\\`.+github\\.com:\\(.+\\)\\.git\\'"</span> <span class="s">"\\1"</span>
<span class="p">(</span><span class="nv">magit-get</span> <span class="s">"remote"</span>
<span class="p">(</span><span class="nv">magit-get-push-remote</span><span class="p">)</span>
<span class="s">"url"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">magit-get-current-branch</span><span class="p">))))</span></code></pre></figure>
</div>
</div>
<p><a href="https://endlessparentheses.com/create-github-prs-from-emacs-with-magit.html?source=rss#disqus_thread">Comment on this.</a></p>Better compile command2015-06-15T00:00:00+00:00https://endlessparentheses.com//better-compile-command<p>
Having to confirm-before-save buffers every time you call <a href="https://doc.endlessparentheses.com/Fun/compile"><code>compile</code></a> is nearly as
outrageous as having no key bound to <a href="https://doc.endlessparentheses.com/Fun/compile"><code>compile</code></a> in the first place. This snippet
takes care of both and, as a bonus, makes the compilation window follow a
predetermined size and ensures that point will follow the output.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;; This gives a regular `compile-command' prompt.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">prog-mode-map</span> <span class="nv">[C-f9]</span> <span class="nf">#'</span><span class="nb">compile</span><span class="p">)</span>
<span class="c1">;; This just compiles immediately.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">prog-mode-map</span> <span class="nv">[f9]</span>
<span class="nf">#'</span><span class="nv">endless/compile-please</span><span class="p">)</span>
<span class="c1">;; I'm not scared of saving everything.</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">compilation-ask-about-save</span> <span class="no">nil</span><span class="p">)</span>
<span class="c1">;; Stop on the first error.</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">compilation-scroll-output</span> <span class="ss">'next-error</span><span class="p">)</span>
<span class="c1">;; Don't stop on info or warnings.</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">compilation-skip-threshold</span> <span class="mi">2</span><span class="p">)</span>
<span class="p">(</span><span class="nv">defcustom</span> <span class="nv">endless/compile-window-size</span> <span class="mi">105</span>
<span class="s">"Width given to the non-compilation window."</span>
<span class="ss">:type</span> <span class="ss">'integer</span>
<span class="ss">:group</span> <span class="ss">'endless</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/compile-please</span> <span class="p">(</span><span class="nv">comint</span><span class="p">)</span>
<span class="s">"Compile without confirmation.
With a prefix argument, use comint-mode."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"P"</span><span class="p">)</span>
<span class="c1">;; Do the command without a prompt.</span>
<span class="p">(</span><span class="nv">save-window-excursion</span>
<span class="p">(</span><span class="nb">compile</span> <span class="p">(</span><span class="nb">eval</span> <span class="nv">compile-command</span><span class="p">)</span> <span class="p">(</span><span class="nb">and</span> <span class="nv">comint</span> <span class="no">t</span><span class="p">)))</span>
<span class="c1">;; Create a compile window of the desired width.</span>
<span class="p">(</span><span class="nv">pop-to-buffer</span> <span class="p">(</span><span class="nv">get-buffer</span> <span class="s">"*compilation*"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">enlarge-window</span>
<span class="p">(</span><span class="nb">-</span> <span class="p">(</span><span class="nv">frame-width</span><span class="p">)</span>
<span class="nv">endless/compile-window-size</span>
<span class="p">(</span><span class="nv">window-width</span><span class="p">))</span>
<span class="ss">'horizontal</span><span class="p">))</span></code></pre></figure>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Update <span class="timestamp-wrapper"><span class="timestamp">16 Jun 2015</span></span></h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Thanks to Clément and abo-abo for the variable suggestions, which also led me to
find the <a href="https://doc.endlessparentheses.com/Var/compilation-skip-threshold"><code>compilation-skip-threshold</code></a> variable. Warnings are important to fix,
but in some languages it’s common to have warnings you can’t fix, so it’s nice
for the compilation buffer to not stop scrolling on them.
</p>
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">Update <span class="timestamp-wrapper"><span class="timestamp">17 Jun 2016</span></span></h2>
<div class="outline-text-2" id="text-orgheadline2">
<p>
The command now propagates the prefix argument to the <code>compile</code> function, so
that you can start the buffer in <code>comint-mode</code>. See the update on <a href="/provide-input-to-the-compilation-buffer.html">this post</a> for
more information.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/better-compile-command.html?source=rss#disqus_thread">Comment on this.</a></p>New in Emacs 25.1: Archive priorities and downgrading packages2015-06-08T00:00:00+00:00https://endlessparentheses.com//new-in-emacs-25-1-archive-priorities-and-downgrading-packages<p>
This is the feature I’ve been wanting to show off the most. Anyone who’s
configured Emacs to use more than one package archive knows this problem.
The package menu displays countless redundant entries, as it must list a package
once for each archive that offers it. Even worse, if you install a package from
one archive, the package menu will gladly upgrade it to a newer version on
another archive, clueless to the fact that it may be giving you unstable code.
</p>
<p>
All of this has been addressed now, with the <code>package-archive-priorities</code>
variable. It is an alist where you can assign a priority number to each archive
you use. Thanks to Jorgen Schaefer, the first thing it does is change the
<a href="https://doc.endlessparentheses.com/Fun/package-install"><code>package-install</code></a> command to download the package from the highest priority
archive instead of just choosing the highest version.
</p>
<p>
Here’s an example configuration.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">package-archive-priorities</span>
<span class="o">'</span><span class="p">((</span><span class="s">"melpa-stable"</span> <span class="o">.</span> <span class="mi">20</span><span class="p">)</span>
<span class="p">(</span><span class="s">"marmalade"</span> <span class="o">.</span> <span class="mi">20</span><span class="p">)</span>
<span class="p">(</span><span class="s">"gnu"</span> <span class="o">.</span> <span class="mi">10</span><span class="p">)</span>
<span class="p">(</span><span class="s">"melpa"</span> <span class="o">.</span> <span class="mi">0</span><span class="p">)))</span></code></pre></figure>
<p>
Since you all know I’m a bit of a menu fanatic, I took my time extending that
functionality to the package menu. Now, instead of listing all entries for a
given package, the menu will only list the package that’s offered by the highest
priority archive. With the above configuration, you will only see Melpa packages
if they are not also available from another archive. More importantly, after
installing a stable version you will never be inadvertently upgraded to an
unstable version.
</p>
<p>
This behaviour can be permanently configured with the
<code>package-menu-hide-low-priority</code> variable, and you can temporarily toggle it on
and off with <kbd>(</kbd>. Therefore, if a package has stable version available but you
prefer the Melpa version, you can display it with <kbd>(</kbd> and then install it as
usual. And what’s more, <code>package.el</code> will identify that situation and will keep
upgrading you on the bleeding-edge for that specific package, even though Melpa
is the lowest priority archive.
</p>
<p>
Furthermore, the <kbd>(</kbd> key is now a general key for displaying possibly unwanted
packages (much like it hides/displays details in <code>dired</code> buffers). In addition
to the behaviour above, this key will also display packages that are considered
obsolete. That is, those whose version is lower than something you already have
installed. This was not previously possible, and it can be used to downgrade a
package. As long as the older version is available from some archive, you can
mark it for installation and mark the current version for deletion.
</p>
<p>
This concludes our series on package.el. There are other small improvements here
and there, but it’s time we go back to our weekly code snippets and productivity
tips. Hopefully, this was enough to inspire some of you to clone the Emacs repo
and start building the master branch.
</p>
<p><a href="https://endlessparentheses.com/new-in-emacs-25-1-archive-priorities-and-downgrading-packages.html?source=rss#disqus_thread">Comment on this.</a></p>New in Emacs 25.1: Filtering by status and archive2015-06-01T00:00:00+00:00https://endlessparentheses.com//new-in-emacs-25-1-filtering-by-status-and-archive<p>
For several reasons, the Package Menu’s <kbd>f</kbd> key has always flown a bit under the
radar for me. Though the <a href="https://doc.endlessparentheses.com/Fun/package-menu-filter"><code>package-menu-filter</code></a> command is great in principle, in
practice its usefulness falls a little short for several reasons.
</p>
<p>
One reason, which is in no way its own fault, is that Emacs already has great
searching facilities such as <code>isearch</code> or <code>occur</code>. It’s just easier and faster
to fall back on what you know rather than try out something new. This not
exactly something that needs fixing, but it can be improved by focusing the
filter command on doing things what other facilities can’t do.
</p>
<p>
Now you might be thinking, <i>“doesn’t this command filter by package keywords?
Isearch can’t do that.”</i>
</p>
<p>
And that takes us to the second (probably most relevant) reason. The list of
keywords used by packages is <a href="https://lists.gnu.org/archive/html/emacs-devel/2015-04/msg01192.html">an ugly mess</a>. And that’s not something easy to fix.
Before investing too much resource on this never-ending struggle against entropy,
Emacs 25.1 will make a simple improvement that goes a long way. You can filter
multiple comma-separated keywords at once. So, for instance, you can mitigate
the redundancy between the <code>theme</code> and <code>themes</code> keywords by just searching for
<code>theme,themes</code>.
</p>
<p>
Furthermore, sometimes there’s a good improvement available that is completely
orthogonal to the problem you have. Instead of fixing up keywords, we can also
have <code>package-menu-filter</code> offer <i>other</i> features that alternate facilities
don’t —preferably one that doesn’t rely on the supernatural cooperation of a
thousand monkeys typing elisp. As of Emacs 25.1, you may now filter packages by
status or by archive, by writing <code>arc:ARCHIVE</code> and <code>status:STATUS</code> respectively
when you invoke the command.
</p>
<p>
Of lesser importance, there was also a third usability issue with the command.
It took a couple of seconds to load every time you invoked it. That,
fortunately, was much simpler to solve, and I’ll even leave it as an exercise to
the reader!
</p>
<p>
In the following (very streamline) function, place point anywhere you want and
then invoke a single keybind (from <code>emacs -q</code>, your custom keybinds don’t
count). If you do it right, you can improve its speed a hundred-fold. Can you
see how?
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">package-all-keywords</span> <span class="p">()</span>
<span class="s">"Collect all package keywords"</span>
<span class="c1">;; `package--mapc' simply calls the lambda on each</span>
<span class="c1">;; known package. `package-desc--keywords' returns</span>
<span class="c1">;; the package's list of keywords.</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">key-list</span><span class="p">))</span>
<span class="p">(</span><span class="nv">package--mapc</span>
<span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">pkg</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">key-list</span>
<span class="p">(</span><span class="nb">append</span> <span class="nv">key-list</span>
<span class="p">(</span><span class="nv">package-desc--keywords</span> <span class="nv">pkg</span><span class="p">)))))</span>
<span class="nv">key-list</span><span class="p">))</span></code></pre></figure>
<p>
Check in again next Monday for the last post on this series, where we talk about
prioritizing your package archives.
</p>
<p><a href="https://endlessparentheses.com/new-in-emacs-25-1-filtering-by-status-and-archive.html?source=rss#disqus_thread">Comment on this.</a></p>New in Emacs 25.1: Asynchronous Package Menu2015-05-28T00:00:00+00:00https://endlessparentheses.com//new-in-emacs-25-1-asynchronous-package-menu<p>
It was six months ago, to the day, when I alluded to the fact that Emacs’
package menu <a href="/big-things-to-expect-from-emacs-25.html">needed to go async</a>. The time it took to do a simple <code>list-packages</code>
bothered me the most, closely followed by having to go play Minesweeper every
time I did a package upgrade. The latter was partially addressed when I added
<a href="/asynchronous-package-upgrades-with-paradox.html">asynchronous package transactions</a> to Paradox, but the former took a bit more
work. In Emacs 25.1, at last, the package menu is going async.
</p>
<p>
You don’t need to do anything special to benefit from this. As soon as you issue
<kbd>M-x</kbd> <code>list-packages</code>, instead of those “<code>Contacting host: ...</code>” messages which
always foretell a many-second hang, the package menu will come up almost
instantly. The download of archive information will go on in the background, and
once it is done the new information is updated in place.
</p>
<p>
This has two big advantages.
</p>
<ol class="org-ol">
<li>A fraction of a second after issuing the command you’re already in the menu,
free to navigate, search, or mark stuff while the background download is
happening.
</li>
<li>If you have multiple archives configured (which you should), they are fetched
simultaneously. So the entire download will be 2–4 times faster now, even if
you decide to sit and wait for it to finish.
</li>
</ol>
<p>
It should be noted this only applies to refreshing. Package transactions
(installation, upgrade, and deletion) are still synchronous in <code>package.el</code>.
Async transactions where implemented for a while, but the outcome was quite far
from satisfactory. However, so as not to end on a sad note, you can always go to
<a href="https://github.com/Malabarba/paradox">Paradox</a> for that.
</p>
<p>
Lastly, if you’re the kind of person that hates nice things, you can disable
this feature with the <code>package-menu-async</code> variable.
</p>
<p>
That should be enough about (a)synchronicity for the moment. Come back on
Monday, when we go into some big improvements on the filtering engine.
</p>
<p><a href="https://endlessparentheses.com/new-in-emacs-25-1-asynchronous-package-menu.html?source=rss#disqus_thread">Comment on this.</a></p>New in Emacs 25.1: User-selected packages2015-05-25T00:00:00+00:00https://endlessparentheses.com//new-in-package-el-in-emacs-25-1-user-selected-packages<p>
In <a href="/new-in-package-el-in-emacs-25-1-better-dependency-management.html">Thurday's post</a> on dependency management, I briefly mentioned that
<code>package.el</code> now keeps track of which packages the user explicitly requested,
and which were pulled in as dependencies. But there’s a bit more to this
feature, so it deserves some time in the spolight.
</p>
<p>
Simply put, there is now a new custom variable <code>package-selected-packages</code>. This
variable stores the names of packages installed explicitly by user. So every
time you do <kbd>M-x</kbd> <a href="https://doc.endlessparentheses.com/Fun/package-install"><code>package-install</code></a> or you do <kbd>i x</kbd> in the Package Menu, the
name of that package gets added to this list. Packages which get pulled in as
dependencies are <b>not</b> added to this list, and those which are explictly deleted
get removed from the list. This is how <code>package-autoremove</code> knows what to
remove, it just finds packages which <i>(a)</i> are not on this list and <i>(b)</i> are
not required by anything else.
</p>
<p>
But this variable comes with other benefits too. First, the user can edit it
manually with the usual <a href="https://doc.endlessparentheses.com/Fun/customize-variable"><code>customize-variable</code></a> and use it to keep track of their
list of wanted packages. Second, there’s now another command,
<code>package-install-selected-packages</code>, which ensures that all packages on the
list are installed. This means you can safely move to a new computer, or even
just delete your <code>elpa/</code> subdir. As long as you keep your custom settings you
can just invoke the command and all your packages will be reinstalled.
</p>
<p>
There’s one small caveat, which some of you may have noticed. This bookkeeping is
done during installation. So, when you finally upgrade to Emacs 25.1, how is it
going to know which of your installed packages were user-selected and which were
dependencies?
</p>
<p>
Well, it’s just impossible to know for sure, so it makes an educated guess. It
takes all installed packages that are not required by any other installed
package, and considers them to have been explicitly installed. This can (and
probably will) yield both false positives and negatives, but that only happens
the very first time you start Emacs 25. So just keep in mind you may need to
<code>customize-variable</code> and fine-tune this list.
</p>
<p><a href="https://endlessparentheses.com/new-in-package-el-in-emacs-25-1-user-selected-packages.html?source=rss#disqus_thread">Comment on this.</a></p>New in Emacs 25.1: Better dependency management2015-05-21T00:00:00+00:00https://endlessparentheses.com//new-in-package-el-in-emacs-25-1-better-dependency-management<p>
Package.el has gotten a series of improvements after the release of 24.4.
Since I’ve found that people like to read about upcoming features, I’m starting
a new series exclusively about our favorite package manager. Today, we talk
dependencies.
</p>
<p>
Package.el’s dependency management has always been just enough to do the job
it’s supposed to do. If you install a package that has dependencies, those
requirements get installed as well. End of story.
</p>
<p>
This leaves a little to be desired for two reasons. Firstly, if you later remove
the first package, the dependencies will be left on your computer. Secondly,
even if you notice those dependencies lying around and want to remove them, you
never know if that’s safe because Package.el won’t tell you if another package
depends on them and, what’s worse, it will let you remove it even if another
installed package does depend on them (which will lead to breakage).
</p>
<p>
The situation is being improved on both accounts, thanks to <a href="https://github.com/thierryvolpiatto">Thierry Volpiatto</a>.
</p>
<ol class="org-ol">
<li>Package.el will differentiate between packages you’ve installed explicitly
and those which were just pulled along as dependencies. So you get a new
<code>package-autoremove</code> command to cleanup dependencies that are no longer
needed.
</li>
<li>Package.el will never let you remove a package if some other package depends
on it. So you’re free to try to delete anything you don’t want, and nothing
is going to break if it happens to be a dependency (you’ll just fail).
</li>
</ol>
<p>
And this led to some UI improvements to the package menu, thanks to yours truly.
</p>
<ol class="org-ol">
<li>Packages that were pulled in as dependencies are marked as such in the
package menu.
</li>
<li>The description buffer (the one you get when hitting <kbd>RET</kbd> on a package) is
now kind enough to list all packages that depend on this one.
</li>
</ol>
<p>
<a href="/images/package-menu-dependencies-1.png"><img src="/images/package-menu-dependencies-1.png" alt="package-menu-dependencies-1.png" /></a>
<a href="/images/package-menu-dependencies-2.png"><img src="/images/package-menu-dependencies-2.png" alt="package-menu-dependencies-2.png" /></a>
</p>
<p>
As a whole, this is a great user experience improvement. Instead of seeing
<code>installed</code> packages that they never actually installed, the user will see
<code>dependency</code> packages. Every once in a while, after removing some package, they
might get notified that <i>“these dependencies are no longer necessary”</i>, and can
easily clean them up with <code>package-autoremove</code>.
</p>
<p>
If you want more 25.1 news, there’s also a <a href="/new-in-emacs-25-1-query-replace-history-is-enhanced-.html">whole series</a> just about that, and you
can always just check out the <a href="/tags-expanded.html#emacs-25">emacs-25</a> tag.</p>
<p><a href="https://endlessparentheses.com/new-in-package-el-in-emacs-25-1-better-dependency-management.html?source=rss#disqus_thread">Comment on this.</a></p>Proof general configuration for the Coq Software Foundations tutorial2015-05-18T00:00:00+00:00https://endlessparentheses.com//proof-general-configuration-for-the-coq-software-foundations-tutorial<p>
<a href="http://proofgeneral.inf.ed.ac.uk/">Proof-general</a> is a powerful client for the <a href="https://coq.inria.fr/">Coq proof assistant</a>, and <a href="http://www.cis.upenn.edu/~bcpierce/sf/current/index.html">Software
Foundations</a> is great interactive tutorial for the language. As I was following
the tutorial, I felt the need to speed things up a little bit. Today’s post is
just some configuration code I wrote for that effect.
</p>
<p>
Parts of it might be useful for coq in general, but its mostly optimized to
minimize keystrokes in the tutorial, and sometimes it leaves bad indentation or
extra lines all over the place.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;; I appreciate the effort of writing a splash-screen, but the angry</span>
<span class="c1">;; general on the gif scares me.</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">proof-splash-seen</span> <span class="no">t</span><span class="p">)</span>
<span class="c1">;;; Hybrid mode is by far the best.</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">proof-three-window-mode-policy</span> <span class="ss">'hybrid</span><span class="p">)</span>
<span class="c1">;;; I don't know who wants to evaluate comments</span>
<span class="c1">;;; one-by-one, but I don't.</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">proof-script-fly-past-comments</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="nv">with-eval-after-load</span> <span class="ss">'coq</span>
<span class="c1">;; The most common command by far. Having a 3(!)</span>
<span class="c1">;; keys long sequence for this command is just a</span>
<span class="c1">;; crime.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">coq-mode-map</span> <span class="s">"\M-n"</span>
<span class="nf">#'</span><span class="nv">proof-assert-next-command-interactive</span><span class="p">)</span>
<span class="c1">;; Proof navigation didn't work for me. So please</span>
<span class="c1">;; stand aside for my paragraph navigation.</span>
<span class="c1">;; https://endlessparentheses.com/meta-binds-part-2-a-peeve-with-paragraphs.html</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">coq-mode-map</span> <span class="s">"\M-e"</span> <span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">coq-mode-map</span> <span class="s">"\M-a"</span> <span class="no">nil</span><span class="p">)</span>
<span class="c1">;; Small convenience for commonly written commands.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">coq-mode-map</span> <span class="s">"\C-c\C-m"</span> <span class="s">"\nend\t"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">coq-mode-map</span> <span class="s">"\C-c\C-e"</span>
<span class="nf">#'</span><span class="nv">endless/qed</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/qed</span> <span class="p">()</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nb">unless</span> <span class="p">(</span><span class="nv">memq</span> <span class="p">(</span><span class="nv">char-before</span><span class="p">)</span> <span class="o">'</span><span class="p">(</span><span class="nv">?\s</span> <span class="nv">?\n</span> <span class="nv">?\r</span><span class="p">))</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">" "</span><span class="p">))</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">"Qed."</span><span class="p">)</span>
<span class="p">(</span><span class="nv">proof-assert-next-command-interactive</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">open-after-coq-command</span> <span class="p">()</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">looking-at-p</span> <span class="s">" *(\\*"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">open-line</span> <span class="mi">1</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">advice-add</span> <span class="ss">'proof-assert-next-command-interactive</span>
<span class="ss">:after</span> <span class="nf">#'</span><span class="nv">open-after-coq-command</span><span class="p">)</span></code></pre></figure>
<p>
These are some common abbrevs, and an advice so you don’t have to hit <kbd>SPC</kbd>
before <kbd>M-n</kbd>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-abbrev-table</span> <span class="ss">'coq-mode-abbrev-table</span> <span class="o">'</span><span class="p">())</span>
<span class="p">(</span><span class="nv">define-abbrev</span> <span class="nv">coq-mode-abbrev-table</span> <span class="s">"re"</span> <span class="s">"reflexivity."</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-abbrev</span> <span class="nv">coq-mode-abbrev-table</span> <span class="s">"id"</span> <span class="s">"induction"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-abbrev</span> <span class="nv">coq-mode-abbrev-table</span> <span class="s">"si"</span> <span class="s">"simpl."</span><span class="p">)</span>
<span class="p">(</span><span class="nv">advice-add</span> <span class="ss">'proof-assert-next-command-interactive</span>
<span class="ss">:before</span> <span class="nf">#'</span><span class="nv">expand-abbrev</span><span class="p">)</span></code></pre></figure>
<p>
And finally, the most important snippet. Just make sure you install
<code>company-coq</code> from Melpa.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">fboundp</span> <span class="ss">'company-coq-initialize</span><span class="p">)</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'coq-mode-hook</span> <span class="nf">#'</span><span class="nv">company-coq-initialize</span><span class="p">))</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/proof-general-configuration-for-the-coq-software-foundations-tutorial.html?source=rss#disqus_thread">Comment on this.</a></p>Ispell and Apostrophes2015-05-11T00:00:00+00:00https://endlessparentheses.com//ispell-and-apostrophes<p>
If you’ve been following our journey of typography, you must now have <a href="/prettify-you-apostrophes.html">pretty
apostrophes</a> all over your org documents. But if that’s the case, you probably
also noticed a drawback. Ispell doesn’t like them very much. Now how are we
supposed to use our amazing <a href="/ispell-and-abbrev-the-perfect-auto-correct.html">auto-correct</a>?
</p>
<p>
Don’t despair, dear reader, I have the solution. It wasn’t trivial to come up
with, but it’s quite simple to understand.
</p>
<ol class="org-ol">
<li>Tell Ispell that apostrophes are just like hard-quotes, where it comes
to constituting a word.
</li>
<li>Convert all ’ to ' as they’re being sent to the Aspell subprocess.
</li>
<li>Convert them back when reading the subprocess output. We even take care to
only do that in <code>org-mode</code>, so that it won’t get in the way of your LaTeX
buffers.
</li>
</ol>
<p>
The following code uses the new advice system in Emacs 24.4. It can certainly be
made to work on older Emacs by changing it to use <a href="https://doc.endlessparentheses.com/Fun/defadvice"><code>defadvice</code></a>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">require</span> <span class="ss">'ispell</span><span class="p">)</span>
<span class="c1">;;; Tell ispell.el that ’ can be part of a word.</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">ispell-local-dictionary-alist</span>
<span class="o">`</span><span class="p">((</span><span class="no">nil</span> <span class="s">"[[:alpha:]]"</span> <span class="s">"[^[:alpha:]]"</span>
<span class="s">"['\x2019]"</span> <span class="no">nil</span> <span class="p">(</span><span class="s">"-B"</span><span class="p">)</span> <span class="no">nil</span> <span class="nv">utf-8</span><span class="p">)))</span>
<span class="c1">;;; Don't send ’ to the subprocess.</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/replace-apostrophe</span> <span class="p">(</span><span class="nv">args</span><span class="p">)</span>
<span class="p">(</span><span class="nb">cons</span> <span class="p">(</span><span class="nv">replace-regexp-in-string</span>
<span class="s">"’"</span> <span class="s">"'"</span> <span class="p">(</span><span class="nb">car</span> <span class="nv">args</span><span class="p">))</span>
<span class="p">(</span><span class="nb">cdr</span> <span class="nv">args</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">advice-add</span> <span class="nf">#'</span><span class="nv">ispell-send-string</span> <span class="ss">:filter-args</span>
<span class="nf">#'</span><span class="nv">endless/replace-apostrophe</span><span class="p">)</span>
<span class="c1">;;; Convert ' back to ’ from the subprocess.</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/replace-quote</span> <span class="p">(</span><span class="nv">args</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">not</span> <span class="p">(</span><span class="nv">derived-mode-p</span> <span class="ss">'org-mode</span><span class="p">))</span>
<span class="nv">args</span>
<span class="p">(</span><span class="nb">cons</span> <span class="p">(</span><span class="nv">replace-regexp-in-string</span>
<span class="s">"'"</span> <span class="s">"’"</span> <span class="p">(</span><span class="nb">car</span> <span class="nv">args</span><span class="p">))</span>
<span class="p">(</span><span class="nb">cdr</span> <span class="nv">args</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">advice-add</span> <span class="nf">#'</span><span class="nv">ispell-parse-output</span> <span class="ss">:filter-args</span>
<span class="nf">#'</span><span class="nv">endless/replace-quote</span><span class="p">)</span></code></pre></figure>
<p>
This is, admittedly, a hacky solution. But it’s been working well for me for the
last few weeks. Feel free to shout and scream at me if you run into issues.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Update <span class="timestamp-wrapper"><span class="timestamp"><2015-05-18 Mon></span></span></h2>
<div class="outline-text-2" id="text-1">
<p>
Fixed the third regexp.</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/ispell-and-apostrophes.html?source=rss#disqus_thread">Comment on this.</a></p>Upgrading ace-jump for avy2015-05-04T00:00:00+00:00https://endlessparentheses.com//upgrading-ace-jump-for-ace-window<p>
It was a few years ago that I learned about <a href="https://github.com/winterTTr/ace-jump-mode"><code>ace-jump-mode</code></a> in one of Magnar’s
<a href="http://emacsrocks.com/e10.html">Emacs Rocks</a> episodes. Over this time, slowly but surely, this one simple command
has completely taken over my workflow. It was only last week that I realised how
ingrained it is on my muscle memory. As I <a href="https://twitter.com/AMalabarba/status/594575241638182912">shared this thought</a> on twitter,
<a href="http://twitter.com/_abo_abo">@_abo_abo</a>’s <a href="https://github.com/abo-abo/avy"><code>avy</code></a> was mentioned in the conversation and I decided to give it a
try.
</p>
<p>
Until now, I was under the impression that it was just <code>ace-jump</code> for windows,
but it’s a little more than that. It is actually a full replacement for
<code>ace-jump</code>, with small improvements here and there (plus quite a bit of parallel
functionality which I haven’t explored yet).
</p>
<p>
A couple of small but important differences.
</p>
<ul class="org-ul">
<li>By default, the buffer text doesn’t get shadowed while you’re doing a jump. I
didn’t like this at first, but now I much prefer it. Shadowing the text is
prettier, but I would commonly get lost if I didn’t focus intently on that
letter before the shadowing was applied.
</li>
<li>When more than one key is needed for a jump, it’ll show you the full path,
instead of one key at a time.
</li>
</ul>
<p>
And this is all the configuration I needed.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">avy-keys</span>
<span class="o">'</span><span class="p">(</span><span class="nv">?c</span> <span class="nv">?a</span> <span class="nv">?s</span> <span class="nv">?d</span> <span class="nv">?e</span> <span class="nv">?f</span> <span class="nv">?h</span> <span class="nv">?w</span> <span class="nv">?y</span> <span class="nv">?j</span> <span class="nv">?k</span> <span class="nv">?l</span> <span class="nv">?n</span> <span class="nv">?m</span> <span class="nv">?v</span> <span class="nv">?r</span> <span class="nv">?u</span> <span class="nv">?p</span><span class="p">))</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"M-s"</span><span class="p">)</span> <span class="nf">#'</span><span class="nv">avy-goto-word-1</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/upgrading-ace-jump-for-ace-window.html?source=rss#disqus_thread">Comment on this.</a></p>Comment boxes2015-04-28T00:00:00+00:00https://endlessparentheses.com//comment-boxes<p>
This tip comes from colleague of mine. Ben has a mailing group at work where he
sends weekly <a href="http://www.star.bris.ac.uk/bjm/emacs-tips.html">Emacs tips</a>. They’re always short and useful, but today’s tip was
quite the gem for me. <a href="http://www.star.bris.ac.uk/bjm/emacs-tips.html#sec-1-10">Emacs has a <code>comment-box</code> command</a>.
</p>
<div class="figure">
<p><img src="/images/comment-box.gif" alt="comment-box.gif" />
</p>
</div>
<p>
I’m sure half the readers know about this already, but I didn’t, so I thought
I’d share with the second half. He also offers an improved version of the
command, <a href="http://irreal.org/blog/?p=374">originally by Snader</a>, which extends the box up to the <code>fill-column</code>.</p>
<p><a href="https://endlessparentheses.com/comment-boxes.html?source=rss#disqus_thread">Comment on this.</a></p>Debug your Emacs init file with the Bug-Hunter2015-04-27T00:00:00+00:00https://endlessparentheses.com//debug-your-emacs-init-file-with-the-bug-hunter<p>
<i>“With great power comes great responsibility,”</i> and Emacs is a prime example of
that. The versatility of having an editor that’s a lisp interpreter is truly
empowering, but it can also backfire on you in the most unexpected ways. If
you’ve ever ran into a foggy incompatibility issue between two unrelated
packages, that manifested itself by turning on your mother’s coffee machine every other
weekday, then you know how difficult this can be to track down.
</p>
<p>
One recurring theme on <a href="http://emacs.stackexchange.com/">Emacs.StackExchange</a> is that users will come to us with
some random arcane issue, and either provide no more information or dump their
init file on the question. The best answer we can give in these cases is for
them to <a href="http://emacs.stackexchange.com/search?q=bisect">bisect their init file</a>. But if that’s always the case, why not automate
it?
</p>
<p>
<a href="https://github.com/Malabarba/elisp-bug-hunter">The Bug Hunter</a> is an Emacs library that does that for you.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Hunting real errors</h2>
<div class="outline-text-2" id="text-1">
<p>
If there’s an error being thrown during initialization, all it takes is a single
command.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">M-x bug-hunter-init-file RET RET</code></pre></figure>
<p>
The Bug-Hunter will do a bisection search in your init file for the source of
the error. Thanks to the magic powers of bisection, it is surprisingly fast even
on huge init files.
</p>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Hunting unexpected behaviour</h2>
<div class="outline-text-2" id="text-2">
<p>
If no actual error is being thrown, but some behaviour is still clearly wrong,
then it’s a little more tricky: you need to come up with an assertion. That is,
you need a snippet of Emacs-Lisp code that will return <code>t</code> if something is wrong
and <code>nil</code> if all is fine.
</p>
<p>
For instance, I wanted to figure out why the <code>cl</code> library was being loaded even
though I didn’t explicitly require it anywhere. In this case, the snippet
<code>(featurep 'cl)</code> gives me what I need. It returns <code>nil</code> before the library is
loaded, and returns <code>t</code> afterwards.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">M-x bug-hunter-init-file RET (featurep 'cl) RET</code></pre></figure>
<div class="figure">
<p><img src="/images/cl-example.png" alt="cl-example.png" />
</p>
</div>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3">Interactive debugging</h2>
<div class="outline-text-2" id="text-3">
<p>
Unfortunately this is not supported yet. Communicating with a background Emacs
process that is not in batch mode is complicated.
</p>
<p>
Usually, though, you shouldn’t need it. There’s almost always a snippet that
will work for your needs. If your problem is not triggering an error, and you
don’t know enough Elisp to write an assertion, let me know about your problem
and maybe I can write one for you.
</p>
<p>
Even better, shoot us a question over at <a href="http://emacs.stackexchange.com/">Emacs.StackExchange</a>. We’re always glad
to help.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/debug-your-emacs-init-file-with-the-bug-hunter.html?source=rss#disqus_thread">Comment on this.</a></p>Better backspace during isearch2015-04-20T00:00:00+00:00https://endlessparentheses.com//better-backspace-during-isearch<p>
I’ve never been too pleased with the default behaviour of <kbd>Backspace</kbd> during
<code>isearch</code>. If the last key you hit was <kbd>C-s</kbd>, then it does the same as <kbd>C-r</kbd>
(albeit with less repetition), and if your match failed several characters ago,
you need to hit it that many times to get back on track. Fortunately, <a href="http://emacs.stackexchange.com/users/118/asmeurer">asmeurer</a>
took the time to <a href="http://emacs.stackexchange.com/q/10359/50">phrase this problem</a> I barely realised I had.
</p>
<p>
In response, <a href="http://emacs.stackexchange.com/users/105/drew">Drew</a> provides a command to:
</p>
<ol class="org-ol">
<li>delete the entire portion of <code>isearch</code> string that doesn’t match,
</li>
<li>if everything matches, fallback on deleting last char instead of moving backwards.
</li>
</ol>
<p>
Finally, John Mastro has <a href="https://gist.github.com/johnmastro/508fb22a2b4e1ce754e0">yet another improvement</a> for the command, which is the
one I’m using now. I’ll let you follow <a href="https://gist.github.com/johnmastro/508fb22a2b4e1ce754e0">the link</a> for the code, and just give you
the keybind you need for it to work.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-key</span> <span class="nv">isearch-mode-map</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"<backspace>"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">isearch-delete-something</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/better-backspace-during-isearch.html?source=rss#disqus_thread">Comment on this.</a></p>(Very Late) SX.el Announcement, and more launcher-map2015-04-13T00:00:00+00:00https://endlessparentheses.com//sx-el-announcement-and-more-launcher-map<p>
<a href="https://github.com/vermiculus/sx.el#sx--stack-exchange-for-emacs">SX.el</a>, the awesome Emacs client for the <a href="http://emacs.stackexchange.com/">StackExchange network</a>, has been stable
and happy for many months now, and it’s about time I mentioned it here. We have
put considerable effort into making the interface intuitive, teaching you how to
use it without the need for explanations. In fact, it would probably be a
disservice to the package for me to post a tutorial, so I won’t just yet.
Instead, I’ll just urge you to give it a try.
</p>
<p>
After installing it from <a href="http://stable.melpa.org/#/sx">Melpa</a>, the only thing you need to do is authenticate
with <kbd>M-x</kbd> <code>sx-authenticate</code>. Once that is done, you’re ready to invoke any of
the entry commands.
</p>
<p>
The main command is <code>sx-tab-all-questions</code>. But that’s so many letters, we
simply must to bind it to a key. And if you read the post title, you know <a href="/launcher-keymap-for-standalone-features.html">where
I’m going</a> with this.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;; Ordered by frequency of use, for no particular reason.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"qq"</span> <span class="nf">#'</span><span class="nv">sx-tab-all-questions</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"qi"</span> <span class="nf">#'</span><span class="nv">sx-inbox</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"qo"</span> <span class="nf">#'</span><span class="nv">sx-open-link</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"qu"</span> <span class="nf">#'</span><span class="nv">sx-tab-unanswered-my-tags</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"qa"</span> <span class="nf">#'</span><span class="nv">sx-ask</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"qs"</span> <span class="nf">#'</span><span class="nv">sx-search</span><span class="p">)</span></code></pre></figure>
<p>
Before signing off, I will also bring to your attention the magnitude of the
<code>SX.el</code> feature set. This is not just a cute toy that lets you browse or search
questions. The client has so many features it could take the website itself in a
fight. Voting, editing, commenting, answering, asking, deleting, inbox viewing,
and source code font-locking are all things that just popped into my head. And
that’s without mentioning a host of Emacs-oriented features like tabbing through
buttons, or folding (hiding) comments like org headlines.
</p>
<p>
I’m probably saying too much already, let’s leave some content for future posts.
If you have any questions or would like to give feedback, <a href="https://github.com/vermiculus">Sean</a>, <a href="https://github.com/jleechpe">Jonathan</a> and I
are always floating around our <a href="https://gitter.im/vermiculus/sx.el">Gitter chat room</a> (and we do love to hear
compliments).
</p>
<p><a href="https://endlessparentheses.com/sx-el-announcement-and-more-launcher-map.html?source=rss#disqus_thread">Comment on this.</a></p>Kill Sexp or Directory2015-04-07T00:00:00+00:00https://endlessparentheses.com//kill-sexp-or-directory<p>
Cluttered as our keyboards are with key-binds, it's always nice when we can
combine two disjoint functionalities in the same key. I have <kbd>M-k</kbd> and <kbd>C-M-k</kbd>
bound to killing the next and previous sexp, respectively, but that is never
something I need inside a string or when typing a file name in the minibuffer.
Then these keys become utterly useless!
</p>
<p>
The solution to this conundrum is, of course, to find another use for them in
these cases. Something that comes up regularly inside strings (or file-name
prompts) is killing an entire directory name.
</p>
<p>
The code below, binds <kbd>M-k</kbd> to a command that automatically decides whether to
kill a sexp or a directory name. The decision making is a little tricky, but it
has yet to let me down.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;; Note you may want to swap the following two keybinds.</span>
<span class="c1">;; Emacs' default keymap has `kill-sexp' on `C-M-k'.</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"M-k"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">endless/forward-kill-sexp-or-dir</span><span class="p">)</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-M-k"</span><span class="p">)</span>
<span class="nf">#'</span><span class="nv">endless/backward-kill-sexp-or-dir</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/forward-kill-sexp-or-dir</span> <span class="p">(</span><span class="k">&optional</span> <span class="nv">p</span><span class="p">)</span>
<span class="s">"Kill forward sexp or directory.
If inside a string or minibuffer, and if it looks like
we're typing a directory name, kill forward until the next
/. Otherwise, `kill-sexp'"</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"p"</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb"><</span> <span class="nv">p</span> <span class="mi">0</span><span class="p">)</span>
<span class="p">(</span><span class="nv">endless/backward-kill-sexp-or-dir</span> <span class="p">(</span><span class="nb">-</span> <span class="nv">p</span><span class="p">))</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">r</span> <span class="p">(</span><span class="nv">point</span><span class="p">)))</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nv">in-string-p</span><span class="p">)</span>
<span class="p">(</span><span class="nv">minibuffer-window-active-p</span>
<span class="p">(</span><span class="nv">selected-window</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">looking-at</span> <span class="s">"[^[:blank:]\n\r]*[/\\\\]"</span><span class="p">))</span>
<span class="p">(</span><span class="k">progn</span> <span class="p">(</span><span class="nv">search-forward-regexp</span>
<span class="s">"[/\\\\]"</span> <span class="no">nil</span> <span class="no">nil</span> <span class="nv">p</span><span class="p">)</span>
<span class="p">(</span><span class="nv">kill-region</span> <span class="nv">r</span> <span class="p">(</span><span class="nv">point</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">kill-sexp</span> <span class="nv">p</span><span class="p">)))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/backward-kill-sexp-or-dir</span> <span class="p">(</span><span class="k">&optional</span> <span class="nv">p</span><span class="p">)</span>
<span class="s">"Kill backwards sexp or directory."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"p"</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb"><</span> <span class="nv">p</span> <span class="mi">0</span><span class="p">)</span>
<span class="p">(</span><span class="nv">endless/forward-kill-sexp-or-dir</span> <span class="p">(</span><span class="nb">-</span> <span class="nv">p</span><span class="p">))</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">r</span> <span class="p">(</span><span class="nv">point</span><span class="p">))</span>
<span class="p">(</span><span class="nv">l</span> <span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nv">point</span><span class="p">))))</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nv">in-string-p</span><span class="p">)</span>
<span class="p">(</span><span class="nv">minibuffer-window-active-p</span>
<span class="p">(</span><span class="nv">selected-window</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">looking-back</span> <span class="s">"[/\\\\][^[:blank:]\n\r]*"</span><span class="p">))</span>
<span class="p">(</span><span class="k">progn</span> <span class="p">(</span><span class="nv">backward-char</span><span class="p">)</span>
<span class="p">(</span><span class="nv">search-backward-regexp</span>
<span class="s">"[/\\\\]"</span> <span class="p">(</span><span class="nv">point-min</span><span class="p">)</span> <span class="no">nil</span> <span class="nv">p</span><span class="p">)</span>
<span class="p">(</span><span class="nv">forward-char</span><span class="p">)</span>
<span class="p">(</span><span class="nv">kill-region</span> <span class="p">(</span><span class="nv">point</span><span class="p">)</span> <span class="nv">l</span><span class="p">))</span>
<span class="p">(</span><span class="nv">kill-sexp</span> <span class="p">(</span><span class="nb">-</span> <span class="nv">p</span><span class="p">))))))</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/kill-sexp-or-directory.html?source=rss#disqus_thread">Comment on this.</a></p>Cider-debug, a visual, interactive, debugger for Clojure2015-03-30T00:00:00+00:00https://endlessparentheses.com//cider-debug-a-visual-interactive-debugger-for-clojure<p>
Over the last couple of weeks, I’ve been working on a Clojure debugger for <a href="https://github.com/clojure-emacs/cider">Cider</a>
that is strongly inspired by Edebug. Stepping trough code of any form and
injecting values into running code are the features available in this first
release.
</p>
<p>
This little project started shortly after I my first practical delve into
Clojure. I was impressed at how well Cider integrated Clojure into Emacs, it
almost felt like writing Elisp! When <a href="https://twitter.com/bbatsov/status/574112622167068672">Bozhidar mentioned</a> that they were in need
of a debugger, I figured that would be as a good a chance as any to practice some
Clojure. Since my praise for Edebug <a href="/debugging-emacs-lisp-part-1-earn-your-independence.html">is no secret</a> around here, it should come as
no surprise that I’d model <code>cider-debug</code> after it (albeit, a bit more limited).
</p>
<p>
I should also mention the <a href="https://github.com/razum2um/clj-debugger">clj-debugger</a> plugin, which we don’t use anymore but
was a great help while writing this feature.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Usage</h2>
<div class="outline-text-2" id="text-1">
<p>
First, if you’re still using <code>cider</code> 0.8, you’ll need to upgrade to the latest
stable version <a href="http://stable.melpa.org/#/cider"><code>cider</code> 0.9</a>, which is available on Melpa. Additionally, ensure
your “~/.lein/profiles.clj” file is consistent with that.
</p>
<figure class="highlight"><pre><code class="language-clojure" data-lang="clojure"><span class="p">{</span><span class="no">:user</span><span class="w"> </span><span class="p">{</span><span class="no">:plugins</span><span class="w"> </span><span class="p">[[</span><span class="n">cider/cider-nrepl</span><span class="w"> </span><span class="s">"0.9.0"</span><span class="p">]]}}</span></code></pre></figure>
<p>
Once that is done, using <code>cider-debug</code> could hardly be more straightforward.
Just instrument an expression with <kbd>C-u C-M-x</kbd>, and you’ll be taken through the
code step-by-step. There, you can move on with <kbd>n</kbd>, quit with <kbd>q</kbd>, or inject
values with <kbd>i</kbd>. Note that if you instrument a <code>defn</code> the debugger will not
start immediately, instead, you’ll be taken to the debugger each time the
function is executed.
</p>
<p>
This sort of thing is easier to explain with a gif.
</p>
<div class="figure">
<p><a href="/images/cider-debug.gif"><img src="/images/cider-debug.gif" alt="cider-debug.gif" /></a>
</p>
</div>
<p>
We’d very much appreciate the feedback of any Clojurists out there. <a href="https://github.com/clojure-emacs/cider/issues">File an
issue</a> if you spot something, or just share your thoughts with us on <a href="http://twitter.com/AMalabarba">Twitter</a> or
<a href="https://gitter.im/clojure-emacs/cider">Gitter</a>.
</p>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Update <span class="timestamp-wrapper"><span class="timestamp"><2015-06-16 Tue></span></span></h2>
<div class="outline-text-2" id="text-2">
<p>
That screen-cast above is a little outdated now. Since it was recorded, the
debugger has acquired <a href="https://github.com/clojure-emacs/cider#cider-debug">even more features</a>:
</p>
<ul class="org-ul">
<li>listing local variables,
</li>
<li>evaluating code in the current lexical scope,
</li>
<li>injecting values into the code,
</li>
<li>a move-out command similar to Edebug.
</li>
</ul>
<p>
I’ll make another post eventually, but it’s perfectly intuitive and easy to use.
So give it a try yourself if you use cider.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/cider-debug-a-visual-interactive-debugger-for-clojure.html?source=rss#disqus_thread">Comment on this.</a></p>New on Elpa and in Emacs 25.1: seq.el2015-03-23T00:00:00+00:00https://endlessparentheses.com//new-on-elpa-and-in-emacs-25-1-seq-el<p>
Thanks to Nicolas Petton, Emacs is getting a new built-in sequence library in
25.1, and it’s already <a href="http://elpa.gnu.org/packages/seq.html">available on GNU Elpa</a> for everyone. There’s not much to
be said about this besides the obvious <i>“it’s about time”</i>.
</p>
<p>
The popularity of <a href="https://github.com/magnars/dash.el">dash.el</a> speaks volumes about the need for this feature.
Combined with the recent inclusion of <code>if-let</code>, <code>when-let</code>, and the threading
macros into Emacs core, this should eventually allow most packages to phase out
that extra dependency. Not that there’s anything wrong with <code>dash</code>, of course,
but it’s nice to retire an external dependency if a built-in one becomes
available.
</p>
<p>
Still, in the field of sequence-processing, there are some noteworthy
differences that may lead you to prefer one over the other.
</p>
<ul class="org-ul">
<li><code>seq</code> provides a unified interface for lists, vectors, and strings (i.e.,
sequences).
</li>
<li>Some functions take arguments in a different order, such as <code>(-take n list)</code>
versus <code>(seq-take list n)</code>.
</li>
</ul>
<p>
Are there any operations you feel ought to be added to this arsenal? What about
other utility libraries that really should be built-in?
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Update <span class="timestamp-wrapper"><span class="timestamp"><2015-04-28 Tue></span></span></h2>
<div class="outline-text-2" id="text-1">
<p>
This post used to say that “<code>dash</code> still covers a range of operations that <code>seq</code>
doesn’t”. However, Nico seems very intent on <a href="http://disq.us/8n45jb">proving me wrong</a>, and we now have
<code>seq-difference</code> and <code>seq-intersection</code>.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/new-on-elpa-and-in-emacs-25-1-seq-el.html?source=rss#disqus_thread">Comment on this.</a></p>Easily Create Github PRs from Magit2015-03-17T00:00:00+00:00https://endlessparentheses.com//easily-create-github-prs-from-magit<p>
In contrast with fetching pull requests, which either requires <a href="/merging-github-pull-requests-from-emacs.html">a minor-mode</a> or
some <a href="/automatically-configure-magit-to-access-github-prs.html">long-named branches</a>, <i>creating</i> pull requests is as easy as a one-key deal.
</p>
<p>
*If you’re reading this now, note that this code was written for an older Magit
version. An up-to-date version of the code (working with latest Magit) can be
found on this <a href="/create-github-prs-from-emacs-with-magit.html">other post</a>.*
</p>
<p>
Because <a href="https://github.com/vermiculus/sx.el">sx.el</a> (a package which I really ought to blog about) was developed as a
group effort, <a href="https://github.com/vermiculus">Sean</a> and I decided early on to use PRs for everything.
Predictably, the need arose for a quick way to create them, and I promptly asked
<a href="http://emacs.stackexchange.com/q/3900/50">Emacs.StackExchange</a> for help. The solution below is a courtesy of <a href="http://emacs.stackexchange.com/users/2355/constantine">Constantine</a>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/visit-pull-request-url</span> <span class="p">()</span>
<span class="s">"Visit the current branch's PR on Github."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">browse-url</span>
<span class="p">(</span><span class="nb">format</span> <span class="s">"https://github.com/%s/pull/new/%s"</span>
<span class="p">(</span><span class="nv">replace-regexp-in-string</span>
<span class="s">"\\`.+github\\.com:\\(.+\\)\\.git\\'"</span> <span class="s">"\\1"</span>
<span class="p">(</span><span class="nv">magit-get</span> <span class="s">"remote"</span>
<span class="p">(</span><span class="nv">magit-get-current-remote</span><span class="p">)</span>
<span class="s">"url"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">magit-get-current-branch</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">eval-after-load</span> <span class="ss">'magit</span>
<span class="o">'</span><span class="p">(</span><span class="nv">define-key</span> <span class="nv">magit-mode-map</span> <span class="s">"V"</span>
<span class="nf">#'</span><span class="nv">endless/visit-pull-request-url</span><span class="p">))</span></code></pre></figure>
<p>
After hitting <kbd>V</kbd>, you just need to press the big green button at the Github
page.
</p>
<p><a href="https://endlessparentheses.com/easily-create-github-prs-from-magit.html?source=rss#disqus_thread">Comment on this.</a></p>Automatically configure Magit to access Github PRs2015-03-11T00:00:00+00:00https://endlessparentheses.com//automatically-configure-magit-to-access-github-prs<p>
Did you know you can fetch <a href="https://github.com/">Github</a> pull requests with git by adding a
<code>remote.origin.fetch</code> configuration? That insightful tip is <a href="http://oremacs.com/2015/03/11/git-tricks/">a courtesy of</a> Oleh
at <a href="http://oremacs.com/">(or emacs</a>, a very active blog that has a habit of unbalancing parentheses
wherever it goes). I like the tip so much I wanted to add something to it.
Instead of manually adding that line to you <code>.git/config</code> file, why not have
Magit do that for you?
</p>
<p>
The following snippet will ensure that you always have this fetch configured on
Github repositories as soon as you issue <code>magit-status</code>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/add-PR-fetch</span> <span class="p">()</span>
<span class="s">"If refs/pull is not defined on a GH repo, define it."</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">fetch-address</span>
<span class="s">"+refs/pull/*/head:refs/pull/origin/*"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">magit-remotes</span>
<span class="p">(</span><span class="nv">magit-get-all</span> <span class="s">"remote"</span> <span class="s">"origin"</span> <span class="s">"fetch"</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">unless</span> <span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nb">not</span> <span class="nv">magit-remotes</span><span class="p">)</span>
<span class="p">(</span><span class="nb">member</span> <span class="nv">fetch-address</span> <span class="nv">magit-remotes</span><span class="p">))</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">string-match</span>
<span class="s">"github"</span> <span class="p">(</span><span class="nv">magit-get</span> <span class="s">"remote"</span> <span class="s">"origin"</span> <span class="s">"url"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">magit-git-string</span>
<span class="s">"config"</span> <span class="s">"--add"</span> <span class="s">"remote.origin.fetch"</span>
<span class="nv">fetch-address</span><span class="p">)))))</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'magit-mode-hook</span> <span class="nf">#'</span><span class="nv">endless/add-PR-fetch</span><span class="p">)</span></code></pre></figure>
<p>
This means you can always access pull requests from Magit by hitting <kbd>b b</kbd>, the
usual branch command. The reference names start with <code>/refs/pull/origin/</code>, but
completion does most of the job here.
</p>
<p>
This is more lightweight approach than my <a href="/merging-github-pull-requests-from-emacs.html">previous post</a> on the subject, which
used <code>gh-pulls-mode</code>. You should try both and see which one you prefer.
</p>
<p><a href="https://endlessparentheses.com/automatically-configure-magit-to-access-github-prs.html?source=rss#disqus_thread">Comment on this.</a></p>New on Elpa: Spinner.el, mode-line spinners and progress-bars2015-03-09T00:00:00+00:00https://endlessparentheses.com//new-on-elpa-spinner-el-mode-line-spinners-and-progress-bars<p>
After adding <a href="/asynchronous-package-upgrades-with-paradox.html">asynchronous operations</a> to <a href="https://github.com/Malabarba/paradox/">Paradox</a>, I saw the need to provide some
visual feedback to the user. In the simplest sense, this could be a fixed
message on the mode-line, such as <i>“Upgrading…”</i> or <i>“Working”</i>, but this is
not enough. I needed movement. Movement implies something is ongoing. It catches
your eye and gives you that subconscious reassurance that progress is being
made. A tiny spinning wheel, hourglass, or rainbow is enough to sooth all your
doubts, unerringly restoring your confidence on the software and those who made
it.
</p>
<p>
Today, I introduce <a href="https://github.com/Malabarba/spinner.el">spinner.el</a>, Emacs’ version of the spinning hourglass.
</p>
<div class="figure">
<p><img src="https://github.com/Malabarba/spinner.el/raw/master/all-spinners.gif" alt="all-spinners.gif" />
</p>
</div>
<p>
The spinner is added to the mode-line of a specific buffer, and stays there
until stopped by the program. In the case of Paradox, for instance, you get a
spinner on the <code>*Packages*</code> buffer to indicate background operations are
ongoing, and it is immediately removed once they are finished.
</p>
<p>
Using the package is as simple as calling <code>(spinner-start 'vertical-rising)</code>
(eventually followed by <code>spinner-stop</code>). There are currently 17 different
spinners available (see above), and you can also specify the animation speed or
even add your own animations.
</p>
<p>
Spinner.el is available to all Emacs versions running <code>package.el</code>, so hopefully
other packages will make use of it as well.
</p>
<p><a href="https://endlessparentheses.com/new-on-elpa-spinner-el-mode-line-spinners-and-progress-bars.html?source=rss#disqus_thread">Comment on this.</a></p>Prettify your Apostrophes2015-03-02T00:00:00+00:00https://endlessparentheses.com//prettify-you-apostrophes<p>
Now that you’ve started your journey on the Typography Express by <a href="/prettify-your-quotation-marks.html">using round
double quotes</a>, take a seat and extend that to your apostrophes as well. This
snippet binds a round apostrophe to the <kbd>'</kbd> key, but also inserts a pair of
single round quotes with a prefix.
</p>
<p>
Finally, like the previous one, it also falls back on <a href="https://doc.endlessparentheses.com/Fun/self-insert-command"><code>self-insert-command</code></a>
inside a code block.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-key</span> <span class="nv">org-mode-map</span> <span class="s">"'"</span> <span class="nf">#'</span><span class="nv">endless/apostrophe</span><span class="p">)</span>
<span class="c1">;; (eval-after-load 'markdown-mode</span>
<span class="c1">;; '(define-key markdown-mode-map "'"</span>
<span class="c1">;; #'endless/apostrophe))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/apostrophe</span> <span class="p">(</span><span class="nv">opening</span><span class="p">)</span>
<span class="s">"Insert ’ in prose or `self-insert-command' in code.
With prefix argument OPENING, insert ‘’ instead and
leave point in the middle.
Inside a code-block, just call `self-insert-command'."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"P"</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nv">derived-mode-p</span> <span class="ss">'org-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nv">org-in-block-p</span> <span class="o">'</span><span class="p">(</span><span class="s">"src"</span> <span class="s">"latex"</span> <span class="s">"html"</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">call-interactively</span> <span class="nf">#'</span><span class="nv">self-insert-command</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">looking-at</span> <span class="s">"['’][=_/\\*]?"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">match-end</span> <span class="mi">0</span><span class="p">))</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">null</span> <span class="nv">opening</span><span class="p">)</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">"’"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">"‘’"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">forward-char</span> <span class="mi">-1</span><span class="p">)))))</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/prettify-you-apostrophes.html?source=rss#disqus_thread">Comment on this.</a></p>Visit Directory inside a Set of Directories2015-02-23T00:00:00+00:00https://endlessparentheses.com//visit-directory-inside-a-set-of-directories<p>
Almost every directory I work with, is either directly under “~/Dropbox/” or
under “~/Dropbox/Work/”. However, I never actually visit these two directories,
only the directories inside them. The number of possible targets for <code>find-file</code>
is approaching the outer borders of two-digit land, so there's no hope for my
brain to remember registers for all of these.
</p>
<p>
The best solution I've found is to compile a list of all possible targets
(directories inside those directories) and offer them using <a href="http://www.emacswiki.org/InteractivelyDoThings">ido</a>. This is
somewhat similar to using a bookmark for each possible destination, except it
it's always up to date with the directory contents and doesn't clog up my actual
bookmarks.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">require</span> <span class="ss">'ido</span><span class="p">)</span>
<span class="p">(</span><span class="nb">require</span> <span class="ss">'cl-lib</span><span class="p">)</span>
<span class="p">(</span><span class="nv">defcustom</span> <span class="nv">endless/favorite-directories</span>
<span class="o">'</span><span class="p">(</span><span class="s">"~/Dropbox/Trabalho/"</span> <span class="s">"~/Dropbox/"</span><span class="p">)</span>
<span class="s">"List of favorite directories.
Used in `endless/visit-favorite-dir'. The order here
affects the order that completions will be offered."</span>
<span class="ss">:type</span> <span class="o">'</span><span class="p">(</span><span class="nv">repeat</span> <span class="nb">directory</span><span class="p">)</span>
<span class="ss">:group</span> <span class="ss">'endless</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/visit-favorite-dir</span> <span class="p">(</span><span class="nv">files-too</span><span class="p">)</span>
<span class="s">"Offer all directories inside a set of directories.
Compile a list of all directories inside each element of
`endless/favorite-directories', and visit one of them with
`ido-completing-read'.
With prefix argument FILES-TOO also offer to find files."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"P"</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">completions</span>
<span class="p">(</span><span class="nb">mapcar</span> <span class="nf">#'</span><span class="nv">abbreviate-file-name</span>
<span class="p">(</span><span class="nv">cl-remove-if-not</span>
<span class="p">(</span><span class="k">if</span> <span class="nv">files-too</span> <span class="nf">#'</span><span class="nv">file-readable-p</span>
<span class="nf">#'</span><span class="nv">file-directory-p</span><span class="p">)</span>
<span class="p">(</span><span class="nb">apply</span> <span class="nf">#'</span><span class="nb">append</span>
<span class="p">(</span><span class="nb">mapcar</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span>
<span class="p">(</span><span class="nv">directory-files</span>
<span class="p">(</span><span class="nv">expand-file-name</span> <span class="nv">x</span><span class="p">)</span>
<span class="no">t</span> <span class="s">"^[^\.].*"</span> <span class="no">t</span><span class="p">))</span>
<span class="nv">endless/favorite-directories</span><span class="p">))))))</span>
<span class="p">(</span><span class="nv">dired</span>
<span class="p">(</span><span class="nv">ido-completing-read</span> <span class="s">"Open directory: "</span>
<span class="nv">completions</span> <span class="ss">'ignored</span> <span class="no">nil</span> <span class="s">""</span><span class="p">))))</span>
<span class="c1">;; Note that C-x d is usually bound to dired. I find</span>
<span class="c1">;; this redundant with C-x C-f, so I don't mind</span>
<span class="c1">;; overriding it, but you should know before you do.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">ctl-x-map</span> <span class="s">"d"</span> <span class="nf">#'</span><span class="nv">endless/visit-favorite-dir</span><span class="p">)</span></code></pre></figure>
<p>
Some random notes:
</p>
<ul class="org-ul">
<li>Having a quick key for this is fantastic. Whenever I want to get work done, I
just hit <kbd>C-x d</kbd> and I'll my options are presented before me.
</li>
<li>With a prefix argument it also shows files instead of just directories.
</li>
<li>If ido is not your completion engine of choice, that's trivial to change.
</li>
<li>This works best when combined with <code>ido-vertical</code>.
</li>
</ul>
<p><a href="https://endlessparentheses.com/visit-directory-inside-a-set-of-directories.html?source=rss#disqus_thread">Comment on this.</a></p>Paradox 2.0 Released: Execution hook, Interface improvements, Async Execution2015-02-16T00:00:00+00:00https://endlessparentheses.com//paradox-2-0-released-execution-hook-interface-improvements-async-execution<p>
I've just released Paradox version 2.0. If you've been following the blog, you
already know this features the ability to do background upgrades. Below is a list
of other features.
</p>
<p>
However, I'd first like to say this release requires an Emacs version of at
least 24.4. <code>package.el</code> changed a lot in that release, and the trouble of
supporting two separate versions was keeping me from writing new features. I may
implement backwards compatibility if I see people asking for it.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Some new features</h2>
<div class="outline-text-2" id="text-1">
<dl class="org-dl">
<dt> <code>paradox-execute-asynchronously</code> variable </dt><dd>Set to <code>t</code> or <code>nil</code> depending on
whether you want this feature.
</dd>
<dt> <code>paradox-upgrade-packages</code> command </dt><dd>Upgrade everything, wherever you are.
Combines well with the above.
</dd>
<dt> <code>paradox-after-execute-functions</code> variable </dt><dd>A hook run after every
transaction. Remove functions from it to streamline your experience, or add
your own functions to extend functionality. For instance, if you don't like
the new <code>*Paradox Report*</code> buffer, you can prevent its creation by removing
the relevant functions from the hook:
</dd>
</dl>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">remove-hook</span> <span class="ss">'paradox-after-execute-functions</span>
<span class="nf">#'</span><span class="nv">paradox--report-buffer-print</span><span class="p">)</span></code></pre></figure>
<dl class="org-dl">
<dt> Better interaction </dt><dd>Now, when you hit <code>x</code> in the Paradox Menu to execute a
set of installations and deletions, the buffer narrows to display only
those packages (see below). Much nicer than the usual message in the echo
area.
</dd>
</dl>
<div class="figure">
<p><img src="/images/paradox-narrowed-buffer.png" alt="paradox-narrowed-buffer.png" />
</p>
</div>
<p>
These are the main changes. You can also <a href="/view-the-change-log-for-packages-before-upgrading-with-paradox.html">view package commit lists</a>, but that's
been the case for a while.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/paradox-2-0-released-execution-hook-interface-improvements-async-execution.html?source=rss#disqus_thread">Comment on this.</a></p>New in Emacs 25.1: comment-line2015-02-11T00:00:00+00:00https://endlessparentheses.com//new-in-emacs-25-1-comment-line<p>
After the <a href="/implementing-comment-line.html">previous post</a>, I got in touch with the nice fellas at <a href="http://lists.gnu.org/archive/html/emacs-devel/">emacs-devel</a>
about <a href="http://lists.gnu.org/archive/html/emacs-devel/2015-01/msg00884.html">including comment-line in Emacs</a>. Understandably, there was a wee bit of
concern with using up one of the oh-so-important <kbd>C-</kbd> binds, so it's been put
under <kbd>C-x C-;</kbd> for now.
</p>
<p>
On the bright side, the entire previous code-block gets replaced by a single
keybind.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-;"</span><span class="p">)</span> <span class="nf">#'</span><span class="nv">comment-line</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/new-in-emacs-25-1-comment-line.html?source=rss#disqus_thread">Comment on this.</a></p>View the Change-Log for packages before upgrading, with Paradox2015-02-08T00:00:00+00:00https://endlessparentheses.com//view-the-change-log-for-packages-before-upgrading-with-paradox<p>
<a href="https://github.com/Malabarba/paradox/">Paradox</a> has had this feature
for a while, but I've never blogged about it. When you're in the Packages Menu, and you're about to upgrade your
packages with the always reliable <kbd>U x</kbd>, do you ever wonder what's actually
changed in them? When I do it, there's usually something between 5 and 20 of
them just waiting for me to answer <kbd>y</kbd>, but rarely do I see any actual
difference in the new versions.
</p>
<p>
With Paradox you can find out what's been happening to a package, by listing all
of its commits that happened since the version you have installed. Of course,
this isn't a proper Change-Log —for starters, it's a lot noisier— but since
most packages don't have one, this is the closest thing we have.
</p>
<ol class="org-ol">
<li>Open the menu as usual with <code>paradox-list-packages</code> (which really ought to be
in your <a href="/launcher-keymap-for-standalone-features.html">launcher map</a>).
</li>
<li>Hit <kbd>f u</kbd> to filter by upgradeable packages (did you know you could do that,
by the way?).
</li>
<li>Hit <kbd>l</kbd> on any package to list the commits.
</li>
</ol>
<p>
Here's an example of what you might see.
</p>
<div class="figure">
<p><img src="/images/paradox-commit-list.png" alt="paradox-commit-list.png" />
</p>
</div>
<p>
Note how the first three commits are highlighted. These are the ones I'm about
to install. It even tells me there a comments discussing one of them, so I might
want to have a closer look. Hitting <kbd>RET</kbd> on a line will visit the relevant
commit on Github.
</p>
<p>
As you may have guessed, this only applies to packages on Github. But, at this
point, that's where most of them are.</p>
<p><a href="https://endlessparentheses.com/view-the-change-log-for-packages-before-upgrading-with-paradox.html?source=rss#disqus_thread">Comment on this.</a></p>New on (M)Elpa: speed-of-thought-lisp2015-02-02T00:00:00+00:00https://endlessparentheses.com//new-on-elpa-speed-of-thought-lisp<p>
When your computer is feeling slow and you decide to upgrade it, where do you
start? You start by finding the bottleneck, of course. That awesome CPU won't do
you any good with crappy RAM disk. The same logic holds for your coding skills.
</p>
<p>
Whenever you're writing code, there are two main processes going on —this is a
very rough model, but it gets the job done.
</p>
<ol class="org-ol">
<li>You think of what you'll write,
</li>
<li>and you write it.
</li>
</ol>
<p>
As you become more experienced with a language, the first part gets faster and
easier, to the point where you can immediately envisage the solution to any
problem you encounter. On the other hand, your actual writing speed is much
slower to improve, and caps at a very disappointing maximum.
</p>
<p>
The solution? Upgrade the bottle-neck, i.e., your hands. <code>speed-of-thought-lisp</code>
(or <code>sotlisp</code>) is all about helping your hands keep up with your brain. The mode
itself is quite simple, it provides two sets of tools to help your fingers follow
your thought-flow. To turn it on, just issue <kbd>M-x</kbd> <code>speed-of-thought-mode</code>.
While you're at it, make sure you've got something like <a href="https://doc.endlessparentheses.com/Fun/electric-pair-mode"><code>electric-pair-mode</code></a> or
<code>paredit</code> active, <code>sotlisp</code> is a lot more fluid with these.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Minimal typing with abbrevs</h2>
<div class="outline-text-2" id="text-1">
<p>
A large number of intelligent abbrevs which expand a function's initials to its
name. A few examples:
</p>
<ul class="org-ul">
<li><b>i</b> → <code>insert</code>
</li>
<li><b>a</b> → <code>and</code>
</li>
<li><b>r</b> → <code>require '</code>
</li>
<li><b>wcb</b> → <code>with-current-buffer</code>
</li>
<li><b>efn</b> → <code>expand-file-name</code>
</li>
</ul>
<p>
It's pointless to list all of them here, there are 136 of them at the moment.
The whole purpose of these abbrevs is that you don't need to learn them. Instead
of typing <code>insert SPC</code> or maybe <code>ins TAB</code>, you just write <code>i SPC</code> for the same
effect. You'll be surprised how quickly this abbreviations become more natural
to you than the actual names.
</p>
<p>
And before you start worrying, these are defined in a way such that they <i>only</i>
expand in a place where you would use a function. So hitting <kbd>SPC</kbd> after <code>(r</code>
gives <code>(require '</code>, but hitting <kbd>SPC</kbd> after <code>(delete-region r</code> will <i>not</i> expand
the <code>r</code>, because that's obviously not a function. You can safely use short
variable names without running into conflicts.
</p>
<p>
You may I have noticed I've only mentioned functions. Variable abbrevs are
on the way!
</p>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Speedy keybinds</h2>
<div class="outline-text-2" id="text-2">
<p>
It also defines 4 commands, which fit tightly into this <i>“follow the
thought-flow”</i> way of writing.
</p>
<dl class="org-dl">
<dt> <kbd>M-RET</kbd> </dt><dd>Break line and insert <code>()</code> with point in the middle.
</dd>
<dt> <kbd>C-RET</kbd> </dt><dd>Do <code>forward-up-list</code> then do the above.
</dd>
</dl>
<p>
Hitting <kbd>RET</kbd> followed by a <kbd>(</kbd> was one of the most common key sequences for me
while writing elisp, so giving it a quick-to-hit key is a significant
improvement.
</p>
<dl class="org-dl">
<dt> <kbd>C-c f</kbd> </dt><dd>Find function under point. If it is not defined, create a
definition for it above the current function and leave point
inside.
</dd>
<dt> <kbd>C-c v</kbd> </dt><dd>Same, but for variable.
</dd>
</dl>
<p>
With these commands, you just write your code as you think of it. Once you hit a
“stop-point” of sorts in your thought-flow, you hit <kbd>C-c v</kbd> on any undefined
variables. Then you just write its definition, and hit <kbd>C-u C-SPC</kbd>
(<a href="https://doc.endlessparentheses.com/Fun/pop-global-mark"><code>pop-global-mark</code></a>) to go back to where you were.
</p>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3">Conclusion</h2>
<div class="outline-text-2" id="text-3">
<p>
<code>sotlisp</code> is floating around the Elpas, so you can issue the usual <kbd>M-x</kbd>
<code>package-install</code> <kbd>RET</kbd> <code>sotlisp</code>, and activate it in your init file.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">speed-of-thought-mode</span><span class="p">)</span></code></pre></figure>
<p>
Then you'll be able to do magic such as writing
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">with-temp-buffer</span>
<span class="p">(</span><span class="nv">insert</span> <span class="nv">text</span><span class="p">)</span>
<span class="p">(</span><span class="nv">buffer-string</span><span class="p">))</span></code></pre></figure>
<p>
by typing <code>(wtb</code> <kbd>M-RET</kbd> <code>i</code> <kbd>SPC</kbd> <code>text</code> <kbd>C-RET</kbd> <code>bs</code> <kbd>SPC</kbd>.
</p>
<p>
Also coming up is a <code>sotlatex</code> package and perhaps even a <code>sotclojure</code>.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/new-on-elpa-speed-of-thought-lisp.html?source=rss#disqus_thread">Comment on this.</a></p>Implementing comment-line2015-01-26T00:00:00+00:00https://endlessparentheses.com//implementing-comment-line<p>
Why we don't have a <code>comment/uncomment-line</code> function is beyond me. While we fix
that, might as well make it as complete as possible.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/comment-line</span> <span class="p">(</span><span class="nv">n</span><span class="p">)</span>
<span class="s">"Comment or uncomment current line and leave point after it.
With positive prefix, apply to N lines including current one.
With negative prefix, apply to -N lines above."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"p"</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">range</span> <span class="p">(</span><span class="nb">list</span> <span class="p">(</span><span class="nv">line-beginning-position</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">line-end-position</span> <span class="nv">n</span><span class="p">)))))</span>
<span class="p">(</span><span class="nv">comment-or-uncomment-region</span>
<span class="p">(</span><span class="nb">apply</span> <span class="nf">#'</span><span class="nb">min</span> <span class="nv">range</span><span class="p">)</span>
<span class="p">(</span><span class="nb">apply</span> <span class="nf">#'</span><span class="nb">max</span> <span class="nv">range</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">forward-line</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="nv">back-to-indentation</span><span class="p">))</span></code></pre></figure>
<p>
Short and sweet, isn't it? In analogy to <kbd>M-;</kbd>. I find <kbd>C-;</kbd> to be a great
binding for it. This way I can quickly comment 3 lines by hitting <kbd>C-3 C-;</kbd>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-;"</span><span class="p">)</span> <span class="nf">#'</span><span class="nv">endless/comment-line</span><span class="p">)</span></code></pre></figure>
<p>
Also, because it moves forward after commenting, you can conveniently do things
like comment every other line by repeatedly doing <kbd>C-; C-n</kbd>.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Update <span class="timestamp-wrapper"><span class="timestamp"><2015-02-01 Sun></span></span></h2>
<div class="outline-text-2" id="text-1">
<p>
The previous version didn't quite work for negative prefix arguments, so I've
updated the above snippet to fix that.
</p>
<p>
Also, quite a few people requested (or proposed) a version which acts on region
if active. I already use <kbd>M-;</kbd> for that (and I also its other modes of
operation), but I can see why people would want that so I've added it below.
It's similar to <a href="https://github.com/kaushalmodi/.emacs.d/blob/13bc1313e786ce1f1ab41d5aaff3dc39dfc57852/setup-files/setup-editing.el#L110-117">the one suggested by Kaushal Modi</a>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/comment-line-or-region</span> <span class="p">(</span><span class="nv">n</span><span class="p">)</span>
<span class="s">"Comment or uncomment current line and leave point after it.
With positive prefix, apply to N lines including current one.
With negative prefix, apply to -N lines above.
If region is active, apply to active region instead."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"p"</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">use-region-p</span><span class="p">)</span>
<span class="p">(</span><span class="nv">comment-or-uncomment-region</span>
<span class="p">(</span><span class="nv">region-beginning</span><span class="p">)</span> <span class="p">(</span><span class="nv">region-end</span><span class="p">))</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">range</span>
<span class="p">(</span><span class="nb">list</span> <span class="p">(</span><span class="nv">line-beginning-position</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">line-end-position</span> <span class="nv">n</span><span class="p">)))))</span>
<span class="p">(</span><span class="nv">comment-or-uncomment-region</span>
<span class="p">(</span><span class="nb">apply</span> <span class="nf">#'</span><span class="nb">min</span> <span class="nv">range</span><span class="p">)</span>
<span class="p">(</span><span class="nb">apply</span> <span class="nf">#'</span><span class="nb">max</span> <span class="nv">range</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">forward-line</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="nv">back-to-indentation</span><span class="p">)))</span></code></pre></figure>
</div>
</div>
<p><a href="https://endlessparentheses.com/implementing-comment-line.html?source=rss#disqus_thread">Comment on this.</a></p>New in Emacs 25.1: Easily install multifile package from a directory2015-01-20T00:00:00+00:00https://endlessparentheses.com//new-in-emacs-25-1-easily-install-multifile-package-from-a-directory<p>
When developing a package, <a href="https://doc.endlessparentheses.com/Fun/package-install-from-buffer"><code>package-install-from-buffer</code></a> is a very useful
command. It installs the current buffer as an Elpa package, so you can test
installation, byte-compilation, autoloading, and activation, all in one fell
swoop. If your package has multiple files, however, it gets a little more
complicated.
</p>
<p>
Earlier this month, I <a href="http://emacs.stackexchange.com/q/7284/50">asked on Emacs.SE</a> whether there was a simple way to
manually install a multifile package. As discussed in <a href="http://emacs.stackexchange.com/a/7285/50">Phils' answer</a>, you need to
create a <code>-pkg.el</code> file, tar the package, and then invoke a command on the tar
file. That's two steps too many for me, so a push to <code>master</code> was in order.
</p>
<p>
As of today, <a href="http://lists.gnu.org/archive/html/emacs-diffs/2015-01/msg00230.html">Emacs 25.1 has a new feature.</a> From the <code>NEWS</code> file:
</p>
<blockquote>
<p>
** <code>package-install-from-buffer</code> and <code>package-install-file</code> work on directories.
This follows the same rules as installing from a <code>.tar</code> file, except the
<code>-pkg.el</code> file is optional.
</p>
</blockquote>
<p>
There are no new commands to remember. Just issue <a href="https://doc.endlessparentheses.com/Fun/package-install-from-buffer"><code>package-install-from-buffer</code></a>
from a dired buffer, or invoke <a href="https://doc.endlessparentheses.com/Fun/package-install-file"><code>package-install-file</code></a> and give a directory.
Whichever package is contained in that directory will be read and installed, be
it single or multifile, no taring or <code>-pkg.el</code> file necessary.
</p>
<p><a href="https://endlessparentheses.com/new-in-emacs-25-1-easily-install-multifile-package-from-a-directory.html?source=rss#disqus_thread">Comment on this.</a></p>Be a 4clojure hero with Emacs2015-01-19T00:00:00+00:00https://endlessparentheses.com//be-a-4clojure-hero-with-emacs<p>
This year I made it my resolution to learn clojure. After reading through the
unexpectedly engaging romance that is <a href="http://braveclojure.com">Clojure for the Brave and True</a>, it was
time to boldly venture through the dungeons of <a href="http://www.4clojure.com/">4clojure</a>. Sword in hand, I
install <code>4clojure.el</code> and start hacking, but I felt the interface could use some
improvements.
</p>
<p>
For starters, it's annoying that you need two commands to check the answer and
move to next question. Sacha has <a href="http://sachachua.com/blog/2014/05/playing-around-clojure-cider-4clojure/">a nice suggestion</a> on this matter, a single
command which checks the answer and moves to the next question. Nonetheless, I
needed more.
</p>
<p>
Having to manually erase the <code>__</code> fields to type my answer is absurd. Not to
mention some answers are cumbersome to type inline. With the following command,
you type your code at the end of the buffer; a lone archer lining up a shot to
slay the problems above.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/4clojure-check-and-proceed</span> <span class="p">()</span>
<span class="s">"Check the answer and show the next question if it worked."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nb">unless</span>
<span class="p">(</span><span class="nv">save-excursion</span>
<span class="c1">;; Find last sexp (the answer).</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">point-max</span><span class="p">))</span>
<span class="p">(</span><span class="nv">forward-sexp</span> <span class="mi">-1</span><span class="p">)</span>
<span class="c1">;; Check the answer.</span>
<span class="p">(</span><span class="nv">cl-letf</span> <span class="p">((</span><span class="nv">answer</span>
<span class="p">(</span><span class="nv">buffer-substring</span> <span class="p">(</span><span class="nv">point</span><span class="p">)</span> <span class="p">(</span><span class="nv">point-max</span><span class="p">)))</span>
<span class="c1">;; Preserve buffer contents, in case you failed.</span>
<span class="p">((</span><span class="nv">buffer-string</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">point-min</span><span class="p">))</span>
<span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nv">search-forward</span> <span class="s">"__"</span> <span class="no">nil</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="nv">replace-match</span> <span class="nv">answer</span><span class="p">))</span>
<span class="p">(</span><span class="nv">string-match</span> <span class="s">"failed."</span> <span class="p">(</span><span class="nv">4clojure-check-answers</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">4clojure-next-question</span><span class="p">)))</span></code></pre></figure>
<p>
The second encounter is simple in comparison. Just sharpen the blade, polish the
shield, and we're ready for battle.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">defadvice</span> <span class="nv">4clojure/start-new-problem</span>
<span class="p">(</span><span class="nv">after</span> <span class="nv">endless/4clojure/start-new-problem-advice</span> <span class="p">()</span> <span class="nv">activate</span><span class="p">)</span>
<span class="c1">;; Prettify the 4clojure buffer.</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">point-min</span><span class="p">))</span>
<span class="p">(</span><span class="nv">forward-line</span> <span class="mi">2</span><span class="p">)</span>
<span class="p">(</span><span class="nv">forward-char</span> <span class="mi">3</span><span class="p">)</span>
<span class="p">(</span><span class="nv">fill-paragraph</span><span class="p">)</span>
<span class="c1">;; Position point for the answer</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">point-max</span><span class="p">))</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">"\n\n\n"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">forward-char</span> <span class="mi">-1</span><span class="p">)</span>
<span class="c1">;; Define our key.</span>
<span class="p">(</span><span class="nv">local-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"M-j"</span><span class="p">)</span> <span class="nf">#'</span><span class="nv">endless/4clojure-check-and-proceed</span><span class="p">))</span></code></pre></figure>
<p>
These two snippets have me cleaving effortlessly through the initial questions
and I'm eager for the final challenges.
</p>
<p>
Both of these websites were recommended to me by <a href="https://twitter.com/mwfogleman/status/549916676343750656">Michael Fogleman</a>. Do you know
any other good clojure resources?</p>
<p><a href="https://endlessparentheses.com/be-a-4clojure-hero-with-emacs.html?source=rss#disqus_thread">Comment on this.</a></p>Automate a package's group and version number2015-01-12T00:00:00+00:00https://endlessparentheses.com//automate-a-package-s-group-and-version-number<p>
One month ago, I <a href="/introducing-names-practical-namespaces-for-emacs-lisp.html">officially announced</a> <b>Names</b>, a package that writes your elisp
namespaces for you. Today, I go into other ways in which Names can help. Think of
these as delicious Easter eggs hidden inside the shabby wood cabin that is the
<code>define-namespace</code> macro (which is built on top of an underground Machiavellic
engine of infinite cogs and spikes, but that's beyond the point).
</p>
<p>
Names is about turning your code into an actual cohesive package, as opposed to
a collection of functions with a common goal. Since it knows everything about
your namespace, it can use that information to simplify your code. These
features are all implemented as keywords you can pass to the macro, and are
documented inside the <code>names--keyword-list</code> variable.
</p>
<p>
As a practical example, take this simple snippet from <a href="https://github.com/Malabarba/camcorder.el.git">camcorder.el</a>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;;;###autoload</span>
<span class="p">(</span><span class="nv">define-namespace</span> <span class="nv">camcorder-</span>
<span class="ss">:package</span> <span class="nv">camcorder</span>
<span class="ss">:group</span> <span class="nv">emacs</span>
<span class="ss">:version</span> <span class="s">"0.1"</span>
<span class="c1">;; ...</span>
<span class="p">)</span></code></pre></figure>
<p>
The first two lines should be no surprise to you if you've read my <a href="/introducing-names-practical-namespaces-for-emacs-lisp.html">introduction
to Names</a>, but next three might. Those three tiny keywords are expressive and
easy to read, and save you a lot of code. The macro above expands to the
following.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">defgroup</span> <span class="nv">camcorder</span> <span class="no">nil</span>
<span class="s">"Customization group for camcorder."</span>
<span class="ss">:prefix</span> <span class="s">"camcorder-"</span>
<span class="ss">:group</span> <span class="ss">'emacs</span><span class="p">)</span>
<span class="p">(</span><span class="nv">defconst</span> <span class="nv">camcorder-version</span> <span class="s">"0.1"</span>
<span class="s">"Version of the camcorder package."</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">camcorder-version</span> <span class="p">()</span>
<span class="s">"Version of the camcorder package."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">message</span> <span class="s">"camcorder version: 0.1"</span><span class="p">)</span>
<span class="s">"0.1"</span><span class="p">)</span>
<span class="c1">;; ...</span></code></pre></figure>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Package name</h2>
<div class="outline-text-2" id="text-1">
<p>
This just defines the name of the package, which is also the name of the group.
If you don't provide it, Names will calculate it by taking the namespace (here,
<code>camcorder-</code>) and removing the last character.
</p>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Group definition</h2>
<div class="outline-text-2" id="text-2">
<p>
Most packages have a customization group. Names can define the group for you,
all you need to do is give it the <code>:group</code> keyword and tell it which group is
the parent of your package's <code>defgroup</code>.
</p>
<p>
The code above is specifying that Names should create a group for this package,
whose parent is the <code>emacs</code> group.
</p>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3">Version numbers</h2>
<div class="outline-text-2" id="text-3">
<p>
It is considered good practice by many for a package to define its version
number as both a constant and an interactive command. If you don't believe me,
see for yourself:
</p>
<ol class="org-ol">
<li>hit <kbd>M-x</kbd>,
</li>
<li>type <code>-version</code>,
</li>
<li>hit <kbd>TAB</kbd>.
</li>
</ol>
<p>
By using the <code>:version</code> keyword, which is pretty self explanatory, you get a
constant and a command defined —both named <code>PACKAGE-version</code>— which return
the version you specify.
</p>
<hr />
<p>
This concludes the most useful current keywords. The purpose of these
facilitators is not to write less code, writing is easy to automate. The
objective here is the same overarching goal behind Names itself, <i>making the
source code shorter to read and nicer to look at</i>.
</p>
<p>
To make use of the described features, make sure you require <code>(names
"20150000")</code> in your package. Also in the works are <a href="https://github.com/Malabarba/names/issues/11"><code>:require</code> and <code>:use</code></a>, but
I'll let you know when these come out.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/automate-a-package-s-group-and-version-number.html?source=rss#disqus_thread">Comment on this.</a></p>What's a defconst and why you should use it2015-01-05T00:00:00+00:00https://endlessparentheses.com//what-s-a-defconst-and-why-you-should-use-it<p>
Any Emacs package developer worth their salt knows the difference between a
<a href="https://doc.endlessparentheses.com/Fun/defvar"><code>defvar</code></a> and <a href="https://doc.endlessparentheses.com/Fun/defcustom"><code>defcustom</code></a>. These two comprise the vast majority of variable
definitions in Elisp code, but there's a third child, the <a href="https://doc.endlessparentheses.com/Fun/defconst"><code>defconst</code></a>. While
regular variables and customizable variables only really differ when it comes to
Emacs' <a href="https://doc.endlessparentheses.com/Fun/customize"><code>customize</code></a> system, constants differ in loading logic in a subtle but
important way.
</p>
<p>
When a <code>defvar</code> is evaluated, the value specified is assigned to the variable
only if the previous value was void (if the variable wasn't defined yet).
Evaluating a <code>defconst</code>, on the other hand, <i>always</i> sets the symbol's value to
the (new) given value. This becomes most relevant when a package is upgraded
<i>after</i> having been loaded in Emacs. Any variables defined by the package will
retain their old values until Emacs is restarted, whereas constants will be
updated to their new values.
</p>
<p>
In a sense, somewhat ironically, Elisp constants are the least constant of all
variables. Rest assured though, they were given this name for a reason. This
behaviour is indeed suited for storing values that should be constant. Here's a
simple use-case which arose for us in <a href="https://github.com/vermiculus/sx.el#readme">sx.el</a>.
</p>
<p>
The StackExchange API requires that we provide filters to specify which
information we want to receive. Of course, we were storing these filters in
variables and then using that information while printing the question.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defvar</span> <span class="nv">sx-browse-filter</span>
<span class="o">'</span><span class="p">(</span><span class="nv">question.title</span> <span class="nv">question.owner</span><span class="p">))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">sx-question--print-info</span> <span class="p">(</span><span class="nv">question-data</span><span class="p">)</span>
<span class="p">(</span><span class="nv">let-alist</span> <span class="nv">question-data</span>
<span class="o">.</span><span class="nv">title</span> <span class="s">"\nAsked by:"</span> <span class="o">.</span><span class="nv">owner</span><span class="p">))</span></code></pre></figure>
<p>
The problem with this approach would only be seen after an upgrade. Lets say we
decide to also display a question's score. Without a doubt, we'd change the
above to something like this.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defvar</span> <span class="nv">sx-browse-filter</span>
<span class="o">'</span><span class="p">(</span><span class="nv">question.score</span> <span class="nv">question.title</span> <span class="nv">question.owner</span><span class="p">))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">sx-question--print-info</span> <span class="p">(</span><span class="nv">question-data</span><span class="p">)</span>
<span class="p">(</span><span class="nv">let-alist</span> <span class="nv">question-data</span>
<span class="p">(</span><span class="nv">number-to-string</span> <span class="o">.</span><span class="nv">score</span><span class="p">)</span> <span class="s">" "</span>
<span class="o">.</span><span class="nv">title</span> <span class="s">"\nAsked by:"</span> <span class="o">.</span><span class="nv">owner</span><span class="p">))</span></code></pre></figure>
<p>
Now the user upgrades <code>sx.el</code>, tries to run it again, and gets hit on the face
with an error!
</p>
<p>
<code>sx-browse-filter</code> is still using the old value, so the question score is not
returned by the API. That means <code>.score</code> is nil and <code>number-to-string</code> throws an
error. Changing the <code>defvar</code> to <code>defconst</code> was all we needed to prevent this
problem.
</p>
<p>
If you're wondering why you've never run into such an issue, that's because
<code>package.el</code> used to have another bug, which would never load new package
versions upon upgrade. That has been fixed in 25.1—packages are now reloaded
on upgrade—so you should ensure your package won't run into new problems by
making proper use of constants.
</p>
<p>
Simply put, you should <code>defconst</code> whenever there are functions or macros in a
package which rely on the variable having that specific value (kind of like a
proper constant). This way you'll know you never have old values hanging around.
</p>
<p><a href="https://endlessparentheses.com/what-s-a-defconst-and-why-you-should-use-it.html?source=rss#disqus_thread">Comment on this.</a></p>Asynchronous package upgrades with Paradox2014-12-29T00:00:00+00:00https://endlessparentheses.com//asynchronous-package-upgrades-with-paradox<p>
Two months ago, I listed <a href="/big-things-to-expect-from-emacs-25.html">a few big things</a> I was looking forward to for Emacs 25.
I knew I was being unrealistically optimistic to mention concurrency in Elisp,
but there was a point behind it. It bothered me a lot that I had to get up and
go for a coffee whenever I upgraded more than a few packages, and asynchronous
upgrades were the only way I saw of fixing that. This Christmas, as promised and
well ahead of schedule, I've implemented asynchronous execution into <a href="https://github.com/Malabarba/paradox/#paradox">Paradox</a>,
thanks to the fantastic <a href="https://github.com/jwiegley/emacs-async">async</a> library.
</p>
<p>
What this means is that whenever you hit <kbd>x</kbd> on Paradox's Package Menu, be it an
upgrade, installation, or deletion, if you've installed the <code>async</code> library,
you'll have the option to perform the operation in the background. Then you can
get back to work and forget about it. Paradox will message you once it's done,
and that's it.
</p>
<p>
You can customize whether or not you want this feature with the
<code>paradox-execute-asynchronously</code> variable (the default is to ask you each time).
It is still considered a beta feature, so you won't find it on Melpa-stable yet.
You can get it on Melpa or wait a couple of weeks for it to be marked stable.
</p>
<p>
As a bonus, there's also a new command <code>paradox-upgrade-packages</code>, inspired by
<a href="http://emacs.stackexchange.com/a/4052/50">this Emacs.SE question</a>, which upgrades everything without the usual manual
labor.
</p>
<p><a href="https://endlessparentheses.com/asynchronous-package-upgrades-with-paradox.html?source=rss#disqus_thread">Comment on this.</a></p>Where do YOU bind expand-region?2014-12-22T00:00:00+00:00https://endlessparentheses.com//where-do-you-bind-expand-region<p>
<a href="https://github.com/magnars/expand-region.el">Expand region</a> is one of those packages that deserves to be built-in. It's so
simple and useful it makes Emacs worthwhile all by itself. Fundamentally, the
entire package boils down to a single command, <code>expand-region</code>, which
incrementally increases the selected region by semantic units. So deciding where
to bind this command is an important decision.
</p>
<p>
<a href="https://github.com/magnars/expand-region.el">The Readme</a> suggests <kbd>C-=</kbd> and I've seen recurring recommendations for <kbd>S-SPC</kbd>,
which I used for a while, but neither felt quite right for me. After marking a
small region, the most common operation for me is to copy it, but it's very
awkward for my fingers to hit <kbd>S-SPC</kbd> <kbd>M-w</kbd>. With that in mind, in one of my
brightest epiphanies, I defined the following keybind.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"M-2"</span><span class="p">)</span> <span class="nf">#'</span><span class="nv">er/expand-region</span><span class="p">)</span></code></pre></figure>
<p>
Not only does <kbd>M-2</kbd> use the same modifier as <kbd>M-w</kbd>, but it's right above the
latter. I couldn't hope for a better key. Besides, after years of switching tabs
in Firefox, my fingers are sniper rifles when it comes to hitting <kbd>M-2</kbd>, making
this combo extra speedy. The downside is that you forgo using Meta for prefix
arguments, but <a href="/Meta-Binds-Part-1%253A-Drunk-in-the-Dark.html">I'm fine with that</a>.
</p>
<p>
When I select large regions, I'm probably killing instead of copying. While
<kbd>M-2</kbd> <kbd>C-w</kbd> is not terribly natural, it's also not awkward, so the key is good
enough here. Finally, <kbd>M-2</kbd> also fits well with my <code>multiple-cursors</code> keybinds,
but that's a topic for another post.
</p>
<p><a href="https://endlessparentheses.com/where-do-you-bind-expand-region.html?source=rss#disqus_thread">Comment on this.</a></p>New on Elpa and in Emacs 25.1: let-alist2014-12-15T00:00:00+00:00https://endlessparentheses.com//new-on-elpa-and-in-emacs-25-1-let-alist<p>
<code>let-alist</code> is the best thing to happen to associative lists since the
invention of the cons cell. This little macro lets you easily access the
contents of an alist, concisely and efficiently, without having to specify them
preemptively. It comes built-in with 25.1, and is also available on GNU Elpa for
older Emacsen.
</p>
<p>
If you've ever had to process the output of a web API, you've certainly had to
deal with endless alists returned by <a href="https://doc.endlessparentheses.com/Fun/json-read"><code>json-read</code></a>. I'll spare you the rant and go
straight to the example.
</p>
<p>
Here's a very simplified version of a function of the <a href="https://github.com/vermiculus/sx.el">SX</a> package, before <code>let-alist</code>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">sx-question-list--print-info</span> <span class="p">(</span><span class="nv">question-data</span><span class="p">)</span>
<span class="s">"DOC"</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">tags</span> <span class="p">(</span><span class="nb">cdr</span> <span class="p">(</span><span class="nv">assq</span> <span class="ss">'tags</span> <span class="nv">question-data</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">answer_count</span> <span class="p">(</span><span class="nb">cdr</span> <span class="p">(</span><span class="nv">assq</span> <span class="ss">'answer_count</span> <span class="nv">question-data</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">title</span> <span class="p">(</span><span class="nb">cdr</span> <span class="p">(</span><span class="nv">assq</span> <span class="ss">'title</span> <span class="nv">question-data</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">last_activity_date</span> <span class="p">(</span><span class="nb">cdr</span> <span class="p">(</span><span class="nv">assq</span> <span class="ss">'last_activity_date</span> <span class="nv">question-data</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">score</span> <span class="p">(</span><span class="nb">cdr</span> <span class="p">(</span><span class="nv">assq</span> <span class="ss">'score</span> <span class="nv">question-data</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">owner-name</span> <span class="p">(</span><span class="nb">cdr</span> <span class="p">(</span><span class="nv">assq</span> <span class="ss">'display_name</span> <span class="p">(</span><span class="nb">cdr</span> <span class="p">(</span><span class="nv">assq</span> <span class="ss">'owner</span> <span class="nv">question-data</span><span class="p">))))))</span>
<span class="p">(</span><span class="nb">list</span>
<span class="nv">question-data</span>
<span class="p">(</span><span class="nb">vector</span>
<span class="p">(</span><span class="nv">int-to-string</span> <span class="nv">score</span><span class="p">)</span>
<span class="p">(</span><span class="nv">int-to-string</span> <span class="nv">answer_count</span><span class="p">)</span>
<span class="nv">title</span> <span class="s">" "</span>
<span class="nv">owner-name</span>
<span class="nv">last_activity_date</span>
<span class="nv">sx-question-list-ago-string</span>
<span class="s">" "</span> <span class="nv">tags</span><span class="p">))))</span></code></pre></figure>
<p>
And this is what the same function looks like now (again, simplified).
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">sx-question-list--print-info</span> <span class="p">(</span><span class="nv">question-data</span><span class="p">)</span>
<span class="s">"DOC"</span>
<span class="p">(</span><span class="nv">let-alist</span> <span class="nv">question-data</span>
<span class="p">(</span><span class="nb">list</span>
<span class="nv">question-data</span>
<span class="p">(</span><span class="nb">vector</span>
<span class="p">(</span><span class="nv">int-to-string</span> <span class="o">.</span><span class="nv">score</span><span class="p">)</span>
<span class="p">(</span><span class="nv">int-to-string</span> <span class="o">.</span><span class="nv">answer_count</span><span class="p">)</span>
<span class="o">.</span><span class="nv">title</span> <span class="s">" "</span>
<span class="o">.</span><span class="nv">owner.display_name</span>
<span class="o">.</span><span class="nv">last_activity_date</span> <span class="nv">sx-question-list-ago-string</span>
<span class="s">" "</span> <span class="o">.</span><span class="nv">tags</span><span class="p">))))</span></code></pre></figure>
<p>
How much nicer is that? <code>let-alist</code> detects all those symbols that start with a
<code>.</code>, and wraps the body in a <code>let</code> form essentially identical to the one above.
The resulting code is much nicer to write, and the byte-compiled result is
exactly as efficient as the manually written version. (If it's not
byte-compiled, there will be a performance difference, though it should be
small.)
</p>
<p>
And just to make things nicer, you can use this snippet to highlight those <code>.</code>
symbols as if they were keywords.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">font-lock-add-keywords</span>
<span class="ss">'emacs-lisp-mode</span>
<span class="o">'</span><span class="p">((</span><span class="s">"\\_<\\.\\(?:\\sw\\|\\s_\\)+\\_>"</span> <span class="mi">0</span>
<span class="nv">font-lock-builtin-face</span><span class="p">)))</span></code></pre></figure>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Update <span class="timestamp-wrapper"><span class="timestamp"><2014-12-20 Sat></span></span></h2>
<div class="outline-text-2" id="text-1">
<p>
Due to popular demand, <code>let-alist</code> now does nested alists. The example above
shows how you can use <code>.owner.display_name</code> to access the value of
<code>display_name</code> inside the value of <code>owner</code>.</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/new-on-elpa-and-in-emacs-25-1-let-alist.html?source=rss#disqus_thread">Comment on this.</a></p>Introducing Names: practical namespaces for Emacs-Lisp2014-12-10T00:00:00+00:00https://endlessparentheses.com//introducing-names-practical-namespaces-for-emacs-lisp<p>
A little over a month ago, I released a package called <a href="https://github.com/Malabarba/names/"><b>Names</b></a>, designed for
mitigating Emacs' namespace issue. Before I even had a chance to announced it,
it made a bit of <a href="http://www.reddit.com/r/emacs/comments/2j4168/names_namespaces_for_emacs_lisp/">a splash</a> on r/emacs, which I've taken to mean that people are
interested. I've been holding off on this post until I had a couple of
Names-using packages under my belt, so I could actually speak from experience as
opposed to expectation, and that's finally the case.
</p>
<p>
<b>Names</b> aims to provide an implementation of namespaces in Emacs-Lisp with four
guiding principles:
</p>
<dl class="org-dl">
<dt> Practical </dt><dd>Actually useful and easy to grasp.
</dd>
<dt> Complete </dt><dd>Support any function, macro, or special-form available in
<code>emacs-lisp</code>, <i>even</i> the ones defined by you or a third party.
</dd>
<dt> Robust </dt><dd>No-surprises, well-tested, and with clearly stated limitations.
</dd>
<dt> Debuggable </dt><dd>Support <code>edebug</code>, <code>find-function</code>/ <code>variable</code>/ <code>face</code>,
<code>eval-defun</code>, and <code>eval-last-sexp</code> (also known as <kbd>C-x C-e</kbd>).
Integration with other developing tools are under way.
</dd>
</dl>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Why a namespace package?</h2>
<div class="outline-text-2" id="text-1">
<p>
The lack of actual namespaces in elisp is a recurring topic in the Emacs
community. The discussion is long and beyond the scope of this post, so I'll
refer those interested to Nic Ferrier's <a href="http://nic.ferrier.me.uk/blog/2013_06/adding-namespaces-to-elisp">great essay on the subject</a> and to a
<a href="http://irreal.org/blog/?p=3500">concise explanation</a> by Jon Snader as well.
</p>
<p>
In short, Emacs takes the approach of prefixing every symbol name with the name
of the package. This successfully avoids name clashes between packages, but it
quickly leads to code that's repetitive and annoying to write. The links above
mentions many shortcomings of this approach, but this code-cluttering repetition
of the package name has always bothered me the most. It makes the code longer
and more tiresome to read
</p>
<p>
See this example from <kbd>package.el</kbd>. The word “package” is repeated 7 times in
a 10-line function.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;;;###autoload</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">package-initialize</span> <span class="p">(</span><span class="k">&optional</span> <span class="nv">no-activate</span><span class="p">)</span>
<span class="s">"[DOC]"</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">package-alist</span> <span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="nv">package-load-all-descriptors</span><span class="p">)</span>
<span class="p">(</span><span class="nv">package-read-all-archive-contents</span><span class="p">)</span>
<span class="p">(</span><span class="nb">unless</span> <span class="nv">no-activate</span>
<span class="p">(</span><span class="nb">dolist</span> <span class="p">(</span><span class="nb">elt</span> <span class="nv">package-alist</span><span class="p">)</span>
<span class="p">(</span><span class="nv">package-activate</span> <span class="p">(</span><span class="nb">car</span> <span class="nb">elt</span><span class="p">))))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">package--initialized</span> <span class="no">t</span><span class="p">))</span></code></pre></figure>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">What does Names do?</h2>
<div class="outline-text-2" id="text-2">
<p>
It doesn't change this approach, nor does it try to revolutionize Emacs or
reinvent packages. <b>Names</b> fully adheres to Emacs conventions and is completely
invisible to the end-user, it simply gives <i>you</i> (the developer) a convenient
way of writing code that adheres to this standard.
</p>
<p>
Here's what the code above would look like inside a namespace.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;;;###autoload</span>
<span class="p">(</span><span class="nv">define-namespace</span> <span class="nv">package-</span>
<span class="ss">:autoload</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">initialize</span> <span class="p">(</span><span class="k">&optional</span> <span class="nv">no-activate</span><span class="p">)</span>
<span class="s">"[DOC]"</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">alist</span> <span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="nv">load-all-descriptors</span><span class="p">)</span>
<span class="p">(</span><span class="nv">read-all-archive-contents</span><span class="p">)</span>
<span class="p">(</span><span class="nb">unless</span> <span class="nv">no-activate</span>
<span class="p">(</span><span class="nb">dolist</span> <span class="p">(</span><span class="nb">elt</span> <span class="nv">alist</span><span class="p">)</span>
<span class="p">(</span><span class="nv">activate</span> <span class="p">(</span><span class="nb">car</span> <span class="nb">elt</span><span class="p">))))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">-initialized</span> <span class="no">t</span><span class="p">))</span>
<span class="p">)</span></code></pre></figure>
<p>
<code>define-namespace</code> is a macro. At compilation, it expands to generate the exact
same code as the original, thus being completely invisible to the user.
</p>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3">How reliable is it?</h2>
<div class="outline-text-2" id="text-3">
<p>
I'll forgive you for being sceptical, wrapping your entire package in a macro
sounds risky. Rest assured, <b>Names</b> is very smart in what it does, and it
strives to avoid surprises.
</p>
<p>
Furthermore, it features a thorough <a href="https://github.com/Malabarba/names#tested-on">test suite</a> which leverages off other
well-tested packages to the respectable sum of 244 tests. Of course, the number
by itself has no objective meaning, but it shows that I'm commited to making it
as reliable and robust as possible. To prove I mean business, the last <a href="https://github.com/Malabarba/camcorder.el/">two</a>
<a href="https://github.com/Malabarba/aggressive-indent-mode">packages</a> I pushed to Melpa already make use of it. And if that's not enough, I
just got news that <a href="https://github.com/DamienCassou/shell-switcher">shell-switcher</a> has joined the party and seems to be <a href="https://travis-ci.org/DamienCassou/shell-switcher">passing
all tests</a>.
</p>
<p>
In terms of availability, it's on GNU Elpa, so it's a safe dependency for any
Emacs starting with <code>24.1</code>.
</p>
</div>
</div>
<div id="outline-container-sec-4" class="outline-2">
<h2 id="sec-4">And how do I use it?</h2>
<div class="outline-text-2" id="text-4">
<p>
If you learn better by example, you can have a quick look at this <a href="https://github.com/Malabarba/names/blob/master/UsageExample.org">short dummy
package</a> available on the repo or, for real world packages, see <a href="https://github.com/Malabarba/camcorder.el/">camcorder.el</a> or
<a href="https://github.com/Malabarba/aggressive-indent-mode">aggressive-indent</a>. In any case, it's important to check the <a href="https://github.com/Malabarba/names">Readme</a> for the most
up-to-date instructions but here's the gist of it:
</p>
<ul class="org-ul">
<li>List <code>names</code> as a dependency. A typical header will contain
</li>
</ul>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;; Package-Requires: ((names "0.5") (emacs "24"))</span></code></pre></figure>
<ul class="org-ul">
<li>Write all code inside the <code>define-namespace</code> macro, preceded by an
<code>;;;###autoload</code> cookie. The first argument of the macro is the prefix which
will be applied to all definitions. <code>require</code> and <code>provide</code> statements go
outside the macro.
</li>
</ul>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;; `require' statements come first.</span>
<span class="p">(</span><span class="nb">require</span> <span class="ss">'whatever</span><span class="p">)</span>
<span class="c1">;;;###autoload</span>
<span class="p">(</span><span class="nv">define-namespace</span> <span class="nv">example-</span>
<span class="c1">;;; Code goes here</span>
<span class="p">)</span>
<span class="p">(</span><span class="nb">provide</span> <span class="ss">'example</span><span class="p">)</span>
<span class="c1">;;; example.el ends here</span></code></pre></figure>
<ul class="org-ul">
<li>Inside the macro, instead of using <code>;;;###autoload</code> cookies, use the
<code>:autoload</code> keyword.
</li>
</ul>
<p>
And that's pretty much it. Just write your code without prefixing every symbol
with the package name, and Names will take care of that for you.
</p>
</div>
<div id="outline-container-sec-4-1" class="outline-3">
<h3 id="sec-4-1">1. Every definition gets namespaced</h3>
<div class="outline-text-3" id="text-4-1">
<p>
Any definitions inside will have the prefix prepended to the
symbol given. So the code
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;;;###autoload</span>
<span class="p">(</span><span class="nv">define-namespace</span> <span class="nv">foo-</span>
<span class="p">(</span><span class="nb">defvar</span> <span class="nv">bar</span> <span class="mi">1</span> <span class="s">"docs"</span><span class="p">)</span>
<span class="ss">:autoload</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">free</span> <span class="p">()</span>
<span class="s">"DOC"</span>
<span class="p">(</span><span class="nv">message</span> <span class="s">"hi"</span><span class="p">))</span>
<span class="p">)</span></code></pre></figure>
<p>
essentially expands to
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defvar</span> <span class="nv">foo-bar</span> <span class="mi">1</span> <span class="s">"docs"</span><span class="p">)</span>
<span class="c1">;;;###autoload</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">foo-free</span> <span class="p">()</span>
<span class="s">"DOC"</span>
<span class="p">(</span><span class="nv">message</span> <span class="s">"hi"</span><span class="p">))</span></code></pre></figure>
</div>
</div>
<div id="outline-container-sec-4-2" class="outline-3">
<h3 id="sec-4-2">2. Function calls and variables are namespaced if defined</h3>
<div class="outline-text-3" id="text-4-2">
<p>
This is best explained by example. This code:
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-namespace</span> <span class="nv">foo-</span>
<span class="p">(</span><span class="nb">defvar</span> <span class="nv">var</span> <span class="nv">infinite</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">infinite</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span>
<span class="p">(</span><span class="nv">infinite</span> <span class="nv">x</span><span class="p">))</span>
<span class="p">(</span><span class="nv">infinite</span> <span class="mi">2</span><span class="p">)</span> <span class="c1">;; Local function call</span>
<span class="p">(</span><span class="nv">something-else</span> <span class="no">t</span><span class="p">)</span> <span class="c1">;; External function call</span>
<span class="p">(</span><span class="err">:</span><span class="ss">:infinite</span> <span class="nv">var</span><span class="p">)</span> <span class="c1">;; External function call</span>
<span class="nv">infinite</span> <span class="c1">;; Variable</span>
<span class="p">)</span></code></pre></figure>
<p>
expands to this code:
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defvar</span> <span class="nv">foo-myvar</span> <span class="nv">infinite</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">foo-infinite</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span>
<span class="p">(</span><span class="nv">foo-infinite</span> <span class="nv">x</span><span class="p">))</span>
<span class="p">(</span><span class="nv">foo-infinite</span> <span class="mi">2</span><span class="p">)</span> <span class="c1">;; Local function call</span>
<span class="p">(</span><span class="nv">something-else</span> <span class="no">t</span><span class="p">)</span> <span class="c1">;; External function call</span>
<span class="p">(</span><span class="nv">infinite</span> <span class="nv">foo-var</span><span class="p">)</span> <span class="c1">;; External function call.</span>
<span class="nv">infinite</span> <span class="c1">;; Variable.</span></code></pre></figure>
<p>
Note how:
</p>
<ul class="org-ul">
<li>The <code>infinite</code> symbol gets namespaced only as a function name, <i>not</i> when it's
used as a variable. That's because <b>Names</b> knows that <code>foo-infinite</code> is not a
defined variable.
</li>
<li>The symbol <code>::infinite</code> is not namespaced, because it had been protected with
<code>::</code>.
</li>
<li><code>something-else</code> is not namespaced, because it is not a locally
defined function, so it must be external.
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-4-3" class="outline-3">
<h3 id="sec-4-3">3. Quoted forms are not namespaced.</h3>
<div class="outline-text-3" id="text-4-3">
<p>
Whenever a form is not meant for evaluation, it is left completely untouched.
The most significant example of this are lists and symbols quoted with a simple
quote (e.g. <code>'foo</code>). These are regarded as data, not code, so you'll have to
write the prefix explicitly inside these quoted forms.
</p>
<p>
Some examples of the <b>opposite</b>:
</p>
<ul class="org-ul">
<li>Symbols quoted with a function quote (e.g. <code>#'foo</code>) are regarded as function
names, and are namespaced as explained in <i>item 2</i>. Remember you should <a href="/get-in-the-habit-of-using-sharp-quote.html">use the
function quote</a> for functions anyway.
</li>
<li>Comma forms inside a back-tick form (e.g. <code>`(nothing ,@(function) ,variable)</code>)
<b>are</b> meant for evaluation and so <b>will</b> be namespaced.
</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-sec-5" class="outline-2">
<h2 id="sec-5">But is it all worth it?</h2>
<div class="outline-text-2" id="text-5">
<p>
Absolutely! Like I said, I've already written two packages using <b>Names</b>, and I
had a blast! Of course, my opinion is biased. But I can say with all honesty
that it's an absolute <i>delight</i> to not need to worry about all those prefixes.
</p>
<p>
I invite people to try it out. If you do, make sure you <code>(require 'names-dev)</code>
in your init file. This will enable integration with <code>edebug</code>, <code>find-function</code>,
<code>eval-defun</code>, and <code>eval-last-sexp</code>. I've already got news that <a href="https://github.com/DamienCassou/shell-switcher">shell-switcher</a>
has made the conversion, and <code>alchemist</code> seems to be on the way.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/introducing-names-practical-namespaces-for-emacs-lisp.html?source=rss#disqus_thread">Comment on this.</a></p>Tab Completion for Prose2014-12-08T00:00:00+00:00https://endlessparentheses.com//tab-completion-for-prose<p>
When writing prose, I find auto-completion to be more of a distraction
then an aid. However, my field of research involves much
repetition of some annoyingly long words, such as <i>“thermalization”</i> or
<i>“distinguishability”</i>.
</p>
<p>
Instead of completely disabling auto-complete, I've found it useful to dial it
down a notch, while still letting it trigger on these monstrosities.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">ac-auto-start</span> <span class="mi">3</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">company-minimum-prefix-length</span> <span class="mi">3</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/config-prose-completion</span> <span class="p">()</span>
<span class="s">"Make auto-complete less agressive in this buffer."</span>
<span class="p">(</span><span class="nv">setq-local</span> <span class="nv">company-minimum-prefix-length</span> <span class="mi">6</span><span class="p">)</span>
<span class="p">(</span><span class="nv">setq-local</span> <span class="nv">ac-auto-start</span> <span class="mi">6</span><span class="p">))</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'text-mode-hook</span>
<span class="nf">#'</span><span class="nv">endless/config-prose-completion</span><span class="p">)</span></code></pre></figure>
<p>
This will offer completions based on previously typed text, but only when I
stumble while typing a long word.
</p>
<p>
Do you use any kind of tab completion for writing prose? It seems
<code>company-mode</code> has an Ispell back-end, has anyone ever tried that?</p>
<p><a href="https://endlessparentheses.com/tab-completion-for-prose.html?source=rss#disqus_thread">Comment on this.</a></p>New in Emacs 25.1: Better Rectangles2014-12-03T00:00:00+00:00https://endlessparentheses.com//new-in-emacs-25-1-better-rectangles<p>
Continuing on this cheerful series, we now go into rectangles. The release notes
are pretty self-explanatory on this one, so a few gifs should be enough to
convey what's needed.
</p>
<blockquote>
<p>
<b>* Rectangle editing
**</b> Rectangle Mark mode can have corners past EOL or in the middle of a TAB.
</p>
</blockquote>
<div class="figure">
<p><img src="/images/rectangle-eol.gif" alt="rectangle-eol.gif" />
</p>
</div>
<blockquote>
<p>
<b>*</b> C-x C-x in rectangle-mark-mode now cycles through the four corners.
</p>
</blockquote>
<div class="figure">
<p><img src="/images/rectangle-cycling.gif" alt="rectangle-cycling.gif" />
</p>
</div>
<blockquote>
<p>
<b>*</b> `string-rectangle' provides on-the-fly preview of the result.
</p>
</blockquote>
<div class="figure">
<p><img src="/images/string-rectangle.gif" alt="string-rectangle.gif" />
</p>
</div>
<p><a href="https://endlessparentheses.com/new-in-emacs-25-1-better-rectangles.html?source=rss#disqus_thread">Comment on this.</a></p>Debugging Elisp Part 2: Advanced topics2014-12-01T00:00:00+00:00https://endlessparentheses.com//debugging-elisp-part-2-advanced-topics<p>
Now that the <a href="/debugging-emacs-lisp-part-1-earn-your-independence.html">previous post</a> has leveled the playing field, we go into slightly
more advanced debugging features. First we go deeper into Edebug navigation
commands, and then we discuss when Edebug just won't do and explain the power of
Emacs' built-in debugger.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Edebug</h2>
<div class="outline-text-2" id="text-1">
<p>
Stepping through a function with <code>n</code> and finishing with <code>c</code> is just a subset of
what Edebug can do. You can find the full list of commands on <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Edebug.html#Edebug">Edebug's manual
page</a> (under “Modes”, “Jumping”, and “Misc”), but here are the most useful ones:
</p>
<dl class="org-dl">
<dt> <kbd>q</kbd> </dt><dd>Quit current execution, occasionally more useful than <kbd>c</kbd>, <kbd>C</kbd> and <kbd>G</kbd>.
</dd>
<dt> <kbd>h</kbd> </dt><dd>Move point somewhere else, then hit <kbd>h</kbd>. Everything in between will be
“skipped” (executed, but not displayed), and Edebug continues from
there. Highly useful in very long functions.
</dd>
<dt> <kbd>o</kbd> </dt><dd>Move out of the containing sexp. Tremendously useful in loops.
</dd>
<dt> <kbd>i</kbd> </dt><dd>Go “inside” the function called at point. In other words, if point is
immediately before a function call, <kbd>i</kbd> will instrument the function as
if you had called <kbd>C-u C-M-x</kbd> on it and leave you inside this new
Edebug session.</dd>
</dl>
<p>
You also have the possibility of setting breakpoints, which I may go into later.
But the keys above are enough to do what Edebug is best at, interactive
investigation when you just don't know where the problem is.
</p>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2"><code>debug</code> and <code>debug-on-entry</code></h2>
<div class="outline-text-2" id="text-2">
<p>
As shiny as it is, Edebug isn't exactly clean. Its instrumentation, though
invisible, is not free of side-effects. It is not surprising then, that some
bugs <a href="https://github.com/Malabarba/fancy-narrow/issues/9">simply vanish</a> when you Edebug them. Fortunately, that's not our only
option. Emacs also offers the <a href="https://doc.endlessparentheses.com/Fun/debug"><code>debug</code></a> function, which is powerful and
straightforward.
</p>
<p>
Just add <code>(debug)</code> anywhere inside a function. Next time the function is
executed, Emacs will present you with a backtrace of the code state at that
point. Alternatively, you invoke the command <a href="https://doc.endlessparentheses.com/Fun/debug-on-entry"><code>debug-on-entry</code></a> and give it the
name of a function.
</p>
<p>
Now, you might be forgiven to think there's nothing special about that. Any
programming language worth its salt can spew out backtraces on demand. But
that's not the end of it. Though this backtrace display might not be as pretty
as Edebug, it's every bit as interactive.
</p>
<p>
<a href="/images/debug-png9-20fps.gif">There's a gif here</a> (linked separately to avoid annoyance) which shows what you
can do by just hitting <kbd>d</kbd> to step deeper into the structure, and then <kbd>c</kbd> to
quickly get out. You can also jump around a bit with <kbd>j</kbd>, and evaluate
expressions under an environment as if you were inside the function with <kbd>e</kbd>.
</p>
<p>
These are all much easier to use than to explain, so give it a shot yourself.
Write <code>(debug)</code> inside a function of yours and start jumping around in there.
You should get the hang of things quickly enough.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/debugging-elisp-part-2-advanced-topics.html?source=rss#disqus_thread">Comment on this.</a></p>Debugging Elisp Part 1: Earn your independence2014-11-24T00:00:00+00:00https://endlessparentheses.com//debugging-emacs-lisp-part-1-earn-your-independence<p>
Running into errors is not only a consequence of tinkering with your
editor, it is the <i>only</i> road to graduating in Emacs. Therefore, it
stands to reason that Emacs would contain the most impressive
debugging utilities know to mankind, <b><a href="http://www.gnu.org/software/emacs/manual/html_node/elisp/Using-Edebug.html">Edebug</a></b>.
</p>
<p>
Learning to Edebug is the right decision for anyone who doesn't know
how to Edebug.
</p>
<ul class="org-ul">
<li>It's easy as pie, you have no excuse not to learn it.
</li>
<li>It helps you solve your own problems. Not that the community isn't
helpful, it just allows you to help yourself—which will eventually
allow you to help the community.
</li>
<li>If you're a newcomer, it will teach you more about elisp than most
tutorials out there.
</li>
<li>If you're anything other than a newcomer, then it's about time and
you should be ashamed of yourself.
</li>
</ul>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">A quick first stop</h2>
<div class="outline-text-2" id="text-1">
<p>
Before delving into Edebug, you should be aware of <a href="https://doc.endlessparentheses.com/Fun/toggle-debug-on-error"><code>toggle-debug-on-error</code></a> and
<a href="https://doc.endlessparentheses.com/Fun/toggle-debug-on-quit"><code>toggle-debug-on-quit</code></a> (which happen to be in our <a href="/the-toggle-map-and-wizardry.html">toggle-map</a>). Though not as
powerful as the alternative, they're a quick n' dirty way to get a backtrace of
your problem.
</p>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">How to Edebug a function</h2>
<div class="outline-text-2" id="text-2">
<ol class="org-ol">
<li>Go to where the function is defined. You can usually do that with
<kbd>C-h f</kbd> (which calls describe function) or just
<kbd>M-x find-function</kbd>.
</li>
<li>Press <kbd>C-u C-M-x</kbd>. This turns on Edebug for that function.
</li>
<li>Now, just invoke the function (or some other command that calls
that function).
</li>
</ol>
<p>
The next time the Edebugged function gets called, Emacs will display
its source code and will take you through it step-by-step. Press
<kbd>n</kbd> to continue to the next step or
<kbd>c</kbd> to stop fooling around and skip to the end.
</p>
<p>
For instance, to solve the <a href="https://github.com/Malabarba/paradox/issues/15">issue that lead me to write this</a>, we could
perform the following sequence of commands:
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">M-x find-function RET package-menu--generate RET
C-u C-M-x
M-x paradox-list-packages RET</code></pre></figure>
<p>
Once a function has been instrumented, it will trigger Edebug <i>every time</i> it is
called. To undo this effect (to remove instrumentation) simply visit its
definition again and hit <kbd>C-M-x</kbd> without the prefix.q
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/debugging-emacs-lisp-part-1-earn-your-independence.html?source=rss#disqus_thread">Comment on this.</a></p>Emacs Rocks Again!2014-11-19T00:00:00+00:00https://endlessparentheses.com//emacs-rocks-again<p>
The emacs community received some fantastic news yesterday.
</p>
<blockquote class="twitter-tweet" lang="en"><p>Okay, I admit it: Being #1 on Hacker News improves my morale. :-) After 19 months, here's episode 15 of Emacs Rocks! <a href="http://t.co/tjlWJHUMVz">http://t.co/tjlWJHUMVz</a></p>— Emacs Rocks (@EmacsRocks) <a href="https://twitter.com/EmacsRocks/status/534447778349338624">November 17, 2014</a></blockquote>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
<p>
Magnar Sveen's blog, <a href="http://whattheemacsd.com/">What the emacs.d?!</a>, was the greatest inspiration behind
Endless Parentheses, and these 5 extra minutes of content from him cheered up my
entire day.
</p>
<p><a href="https://endlessparentheses.com/emacs-rocks-again.html?source=rss#disqus_thread">Comment on this.</a></p>Inserting the kbd tag in interactively2014-11-17T00:00:00+00:00https://endlessparentheses.com//inserting-the-kbd-tag-in-org-mode<p>
It doesn't take a psychic to guess the <code><kbd></code> tag will be useful when
writing about Emacs. When Jorge Navarro asked for the best way to do this, over
at <a href="http://emacs.stackexchange.com/q/2206/50">Emacs.SE</a>, I thought I'd share the snippet I use.
</p>
<p>
This simple command will ask for a key sequence (just like <kbd>C-h
k</kbd> would) and will insert it for you. If you want to write the
sequence manually, just hit <kbd>RET</kbd> when prompted. <kbd>C-c k</kbd> feels
like a nice binding for it.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">eval-after-load</span> <span class="ss">'ox-html</span>
<span class="c1">;; If you prefer to use ~ for <code> tags. Replace "code" with</span>
<span class="c1">;; "verbatim" here, and replace "~" with "=" below.</span>
<span class="o">'</span><span class="p">(</span><span class="nb">push</span> <span class="o">'</span><span class="p">(</span><span class="nv">code</span> <span class="o">.</span> <span class="s">"<kbd>%s</kbd>"</span><span class="p">)</span> <span class="nv">org-html-text-markup-alist</span><span class="p">))</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">org-mode-map</span> <span class="s">"\C-ck"</span> <span class="nf">#'</span><span class="nv">endless/insert-key</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/insert-key</span> <span class="p">(</span><span class="nv">key</span><span class="p">)</span>
<span class="s">"Ask for a key then insert its description.
Will work on both org-mode and any mode that accepts plain html."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"kType key sequence: "</span><span class="p">)</span>
<span class="p">(</span><span class="k">let*</span> <span class="p">((</span><span class="nv">orgp</span> <span class="p">(</span><span class="nv">derived-mode-p</span> <span class="ss">'org-mode</span><span class="p">))</span>
<span class="p">(</span><span class="nv">tag</span> <span class="p">(</span><span class="k">if</span> <span class="nv">orgp</span> <span class="s">"~%s~"</span> <span class="s">"<kbd>%s</kbd>"</span><span class="p">)))</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">null</span> <span class="p">(</span><span class="nb">equal</span> <span class="nv">key</span> <span class="s">"\C-m"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">insert</span>
<span class="p">(</span><span class="nb">format</span> <span class="nv">tag</span> <span class="p">(</span><span class="nv">help-key-description</span> <span class="nv">key</span> <span class="no">nil</span><span class="p">)))</span>
<span class="c1">;; If you just hit RET.</span>
<span class="p">(</span><span class="nv">insert</span> <span class="p">(</span><span class="nb">format</span> <span class="nv">tag</span> <span class="s">""</span><span class="p">))</span>
<span class="p">(</span><span class="nv">forward-char</span> <span class="p">(</span><span class="k">if</span> <span class="nv">orgp</span> <span class="mi">-1</span> <span class="mi">-6</span><span class="p">)))))</span></code></pre></figure>
<p>
It should work in both <code>org-mode</code> and <code>html</code>-like modes.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Update <span class="timestamp-wrapper"><span class="timestamp"><2014-11-30 Sun></span></span></h2>
<div class="outline-text-2" id="text-1">
<p>
A big kudos to <a href="http://www.reddit.com/r/emacs/comments/2mpjfu/inserting_the_kbd_tag_in_interactively/cm8v0p2">u/abo-abo</a>, for suggesting the use of <code>~</code>.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/inserting-the-kbd-tag-in-org-mode.html?source=rss#disqus_thread">Comment on this.</a></p>New in Emacs 25.1: Query-replace history is enhanced.2014-11-15T00:00:00+00:00https://endlessparentheses.com//new-in-emacs-25-1-query-replace-history-is-enhanced<p>
Since the previous post on <a href="/big-things-to-expect-from-emacs-25.html">Emacs 25</a> has been well received, I've decided to
start a series on the topic. It's time to get off the rocking chair, step off
the porch and onto the sidewalk, and start walking towards that ominous “25” on
the horizon. Unlike the <a href="/big-things-to-expect-from-emacs-25.html">birthday post</a>, we're not here to discuss possibilities,
but to meet and greet every new feature that's already implemented in Emacs
<code>master</code>.
</p>
<p>
This time, we stick to the style of the blog. Short posts listing a single
feature, or two at most. It's going to be a long walk, so we might as well take
it one step at a time.
</p>
<blockquote>
<p>
When [in] query-replace […], typing <kbd>M-p</kbd> will now show
previous replacements as "FROM SEP TO", where FROM and TO are the original text
and its replacement, and SEP is an arrow string defined by the new variable
<code>query-replace-from-to-separator</code>. […]
</p>
</blockquote>
<p>
This is actually rather cute.<br />
<img src="/images/query-replace-prompt.png" alt="query-replace-prompt.png" />
</p>
<p>
The arrow is intangible, so it really doesn't get in the way. You can edit
either sides of the prompt, switch them around for a kind of “undo” effect, and
you can even delete the arrow to turn the prompt into a regular <i>FROM</i> prompt.
</p>
<p>
If you keep cycling back through all the previous replacements, you will
eventually be offered replacements in the old format (just <i>TO</i> and then just
<i>FROM</i>).
</p>
<p><a href="https://endlessparentheses.com/new-in-emacs-25-1-query-replace-history-is-enhanced.html?source=rss#disqus_thread">Comment on this.</a></p>Get in the habit of using sharp quote2014-11-10T00:00:00+00:00https://endlessparentheses.com//get-in-the-habit-of-using-sharp-quote<p>
The sharp quote (or function quote, or simply <code>#'</code>) is an abbreviation
for the <a href="https://doc.endlessparentheses.com/Fun/function"><code>function</code></a> form. It is essentially a version of <a href="https://doc.endlessparentheses.com/Fun/quote"><code>quote</code></a> (or <code>'</code>)
which enables byte-compilation, but its actual usefulness has changed
throughout the years.
</p>
<p>
A little over two decades ago, it was used to quote <a href="https://doc.endlessparentheses.com/Fun/lambda"><code>lambda</code></a> forms.
You see, <code>'(lambda (x) (* x x))</code> was just a raw list to the
byte-compiler, but <code>#'(lambda (x) (* x x))</code> was an actual function
that could be compiled. Now-a-days—or rather, now-a-decades—the
lambda form sharp-quotes itself, meaning a plain <code>(lambda (x) (* x
x))</code> <a href="http://www.gnu.org/software/emacs/manual/html_node/elisp/Anonymous-Functions.html">is identical</a> to the <code>#'</code> version. In fact, you should <i>never</i>
quote your lambdas with either quotes.
</p>
<p>
On the other hand, just as you'd expect the sharp quote to become
redundant for the elisp programmer, a new use arises for it. The
compiler throws a warning whenever it notices you've used an undefined
function, say <code>(not-defined "oops")</code>, but it can't do the same for
something like <code>(mapcar 'not-defined some-list)</code> because it doesn't
know that symbol is the name of a function. The sharp quote is a way
of conveying that information to the compiler, so if it runs into
<code>(mapcar #'not-defined some-list)</code>, it can throw a warning
accordingly.
</p>
<p>
So it is always good practice to sharp quote every symbol that is the
name of a function, whether it's going into a <code>mapcar</code>, an <code>apply</code>, a
<code>funcall</code>, or anything else. Adhering to this actually unearthed a
small bug in one of my packages.
</p>
<p>
And of course, we can make things more convenient.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/sharp</span> <span class="p">()</span>
<span class="s">"Insert #' unless in a string or comment."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">call-interactively</span> <span class="nf">#'</span><span class="nv">self-insert-command</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">ppss</span> <span class="p">(</span><span class="nv">syntax-ppss</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">unless</span> <span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nb">elt</span> <span class="nv">ppss</span> <span class="mi">3</span><span class="p">)</span>
<span class="p">(</span><span class="nb">elt</span> <span class="nv">ppss</span> <span class="mi">4</span><span class="p">)</span>
<span class="p">(</span><span class="nb">eq</span> <span class="p">(</span><span class="nv">char-after</span><span class="p">)</span> <span class="nv">?</span><span class="o">'</span><span class="p">))</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">"'"</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">emacs-lisp-mode-map</span> <span class="s">"#"</span> <span class="nf">#'</span><span class="nv">endless/sharp</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/get-in-the-habit-of-using-sharp-quote.html?source=rss#disqus_thread">Comment on this.</a></p>Super Smart Capitalization2014-11-03T00:00:00+00:00https://endlessparentheses.com//super-smart-capitalization<p>
<a href="https://doc.endlessparentheses.com/Fun/capitalize-word"><code>capitalize-word</code></a> and <a href="https://doc.endlessparentheses.com/Fun/downcase-word"><code>downcase-word</code></a> are godsends. Like many other
commands, they've spoiled me to the point that I can no longer write
prose without Emacs. There's just one peeve that bothers me quite often.
</p>
<p>
Let's say I want to join the following two sentences, and point is at the “B”.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">Languages are fleeting. But Emacs is forever.</code></pre></figure>
<p>
You could do <kbd>DEL DEL , M-l</kbd>, but that's so long! When I
<code>downcase</code> the word “But”, it's perfectly obvious that I'll want to get rid of
that full stop, so why can't <kbd>M-l</kbd> do that for me?
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-set-key</span> <span class="s">"\M-c"</span> <span class="ss">'endless/capitalize</span><span class="p">)</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="s">"\M-l"</span> <span class="ss">'endless/downcase</span><span class="p">)</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="s">"\M-u"</span> <span class="ss">'endless/upcase</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/convert-punctuation</span> <span class="p">(</span><span class="nv">rg</span> <span class="nv">rp</span><span class="p">)</span>
<span class="s">"Look for regexp RG around point, and replace with RP.
Only applies to text-mode."</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">f</span> <span class="s">"\\(%s\\)\\(%s\\)"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">space</span> <span class="s">"?:[[:blank:]\n\r]*"</span><span class="p">))</span>
<span class="c1">;; We obviously don't want to do this in prog-mode.</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nv">derived-mode-p</span> <span class="ss">'text-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nv">looking-at</span> <span class="p">(</span><span class="nb">format</span> <span class="nv">f</span> <span class="nv">space</span> <span class="nv">rg</span><span class="p">))</span>
<span class="p">(</span><span class="nv">looking-back</span> <span class="p">(</span><span class="nb">format</span> <span class="nv">f</span> <span class="nv">rg</span> <span class="nv">space</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">replace-match</span> <span class="nv">rp</span> <span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="mi">1</span><span class="p">))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/capitalize</span> <span class="p">()</span>
<span class="s">"Capitalize region or word.
Also converts commas to full stops, and kills
extraneous space at beginning of line."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">endless/convert-punctuation</span> <span class="s">","</span> <span class="s">"."</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">use-region-p</span><span class="p">)</span>
<span class="p">(</span><span class="nv">call-interactively</span> <span class="ss">'capitalize-region</span><span class="p">)</span>
<span class="c1">;; A single space at the start of a line:</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">looking-at</span> <span class="s">"^\\s-\\b"</span><span class="p">)</span>
<span class="c1">;; get rid of it!</span>
<span class="p">(</span><span class="nv">delete-char</span> <span class="mi">1</span><span class="p">))</span>
<span class="p">(</span><span class="nv">call-interactively</span> <span class="ss">'subword-capitalize</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/downcase</span> <span class="p">()</span>
<span class="s">"Downcase region or word.
Also converts full stops to commas."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">endless/convert-punctuation</span> <span class="s">"\\."</span> <span class="s">","</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">use-region-p</span><span class="p">)</span>
<span class="p">(</span><span class="nv">call-interactively</span> <span class="ss">'downcase-region</span><span class="p">)</span>
<span class="p">(</span><span class="nv">call-interactively</span> <span class="ss">'subword-downcase</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/upcase</span> <span class="p">()</span>
<span class="s">"Upcase region or word."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">use-region-p</span><span class="p">)</span>
<span class="p">(</span><span class="nv">call-interactively</span> <span class="ss">'upcase-region</span><span class="p">)</span>
<span class="p">(</span><span class="nv">call-interactively</span> <span class="ss">'subword-upcase</span><span class="p">)))</span></code></pre></figure>
<p>
The snippet above will automatically convert between commas and full-stops when
you're (un)capitalizing prose. It comes up on every single writing session for
me.
</p>
<p>
Now I can just hit <kbd>M-l</kbd> and get
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">Languages are fleeting, but Emacs is forever.</code></pre></figure>
<p><a href="https://endlessparentheses.com/super-smart-capitalization.html?source=rss#disqus_thread">Comment on this.</a></p>Big things to expect from Emacs 252014-10-27T00:00:00+00:00https://endlessparentheses.com//big-things-to-expect-from-emacs-25<p>
Emacs turned 24.4 this Monday. Admittedly, it's not a hugely significant number,
but every birthday is a big deal. The <a href="http://lists.gnu.org/archive/html/emacs-devel/2014-10/msg00713.html">cries</a> of <a href="https://news.ycombinator.com/item?id=8484027">joy</a> and <a href="https://news.ycombinator.com/item?id=8484027">celebration</a> <a href="http://irreal.org/blog/?p=3337">echoed</a>
<a href="http://emacsredux.com/blog/2014/10/21/emacs-24-dot-4/">throughout</a> the <a href="https://twitter.com/search?q=%2523emacs%252024.4&src=typd">websphere</a>. And now, while Emacs sleeps off an alcohol-induced
headache, as the janitor sweeps off the inch-thick confetti covering the floor,
we gather ourselves and look forward. Just before the horizon, a prominently
ominous, silvery, unavoidable “25” is within sight. And it brings changes.
</p>
<p>
<a href="/sweet-new-features-in-24-4.html">Last week</a>, I exposed my favorite perks about the version that is now upon us.
Today, I bring to you my expectations. Features I'd be overjoyed to find in the
“News” file. Some of these are guaranteed, others are hopeful ramblings, but
they're all at least implementable. Most importantly, they all have something in
common: their absence, in my opinion, holds back the Emacs community from
growing even faster.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Git</h2>
<div class="outline-text-2" id="text-1">
<p>
No, I'm not talking about git support, anyone knows Emacs is <a href="http://magit.github.io/index.html">better at git</a> than
git itself. I'm referring to moving the codebase from <code>bzr</code> to <code>git</code>. At the
moment, this is <a href="http://lists.gnu.org/archive/html/emacs-devel/2014-10/msg00864.html">planned to happen</a> on November 11, and a cheerful day that will be.
</p>
<p>
In <a href="https://lists.gnu.org/archive/html/emacs-devel/2014-01/msg00005.html">the words of</a> Eric S. Raymond,
</p>
<blockquote>
<p>
The bzr version control system is dying; by most measures it is already
moribund. The dev list has flat-lined, most of Canonical's in-house projects have
abandoned bzr for git, and one of its senior developers has written a remarkably
<a href="http://www.stationary-traveller.eu/pages/bzr-a-retrospective.html">candid
assessment</a> of why bzr failed[.]
</p>
</blockquote>
<p>
I'm not an expert on bzr, but I've noticed it discourages some new contributors.
Ultimately, adopting a system people already tend to be familiar with is a big
step in the right direction.
</p>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Dynamic library loading</h2>
<div class="outline-text-2" id="text-2">
<p>
Dynamically loading libraries is a big deal, and it's closer than ever. Some
<a href="https://lists.gnu.org/archive/html/emacs-devel/2014-10/msg00292.html">very inspiring results</a> has been reported in the last month and, while there are
still some obstacles under discussion, it's looking almost in shape for release.
</p>
<p>
Of course, not every package needs this—in fact, the vast majority doesn't.
But this would be a huge boon to a number of packages which, so far, have had to
resort to sub-processes or resign to elisp.
</p>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3">Concurrency in Elisp</h2>
<div class="outline-text-2" id="text-3">
<p>
This is not a new idea in any way but it's become increasingly urgent with the
growth of the package repositories. I'll be honest, I haven't seen much talk
about this feature. Still, sometime next year, if there's no sign whatsoever of
concurrency being a thing, I'll write a hack for it work with <code>package.el</code>.
Here's why I think that's important.
</p>
<p>
Consider this situation a little more than a year ago. I noticed my
laptop was taking a while to display the <code>*Packages*</code> buffer.
<i>“No doubt,”</i> I thought to myself, <i>“1477 packages is a bit much for this
laptop. No worries though, 5 seconds of waiting isn't that bad.”</i>.
</p>
<p>
Fast-forward fifteen months. I invoke <kbd>M-x</kbd> <code>list-packages</code>,
wait 20 seconds, and the buffer I'm given crosses the 3000 lines threshold.
That's partially aggravated by the fact that <code>package.el</code> displays separate
entries for each repository a package can be found in. But even if we eliminate
these redundancies, since July 2013 <a href="http://tracker.endlessparentheses.com/">one thousand unique new packages</a> have been
released. That is an impressive rate of over 2 new packages <i>every single day</i>.
</p>
<p>
Now consider that this rate shows absolutely <a href="http://tracker.endlessparentheses.com/newPackagePlotEver.png">no sign of diminishing</a>. Worse yet,
consider that the rate in which packages are updated on Melpa is almost two
orders of magnitude higher. Where will we be by the time Emacs 25 is released?
</p>
<p>
Gradually and surely, the time it takes to upgrade one's Emacs packages will
surpass that of updating one's Operating System. And what are we to do in the
mean time? Play Minesweeper?
</p>
<p>
We need asynchronicity, now more than ever, because the <code>*Packages*</code> buffer
plays one of the most fundamental roles in the expansion of the community. We
need a package manager that checks for new releases in the background while we
navigate the previously cached list, and one that performs updates in the
background without forcing us to go for a coffee.
</p>
</div>
</div>
<div id="outline-container-sec-4" class="outline-2">
<h2 id="sec-4">A Better Package Menu</h2>
<div class="outline-text-2" id="text-4">
<p>
This is an extension to the previous topic. Sluggishness is not the only
consequence of an ever-increasing package menu. Navigating the menu in its
current form is akin to exploring the Amazon rain-forest. There's plenty of
dazzling richness to be found, but the sheer size and roughness are
overwhelming—not mention the piranhas.
</p>
<p>
Emacs 24.4 introduces keyword filtering to the menu, and Paradox offers <a href="https://github.com/Malabarba/paradox/#several-improvements">a couple
other forms</a> of filtering, but they're still very crude. We need a navigable
tree of categories—which would to facilitate discovery for times when
<code>isearch</code> is a bit too narrow. And we need a simple and honest search box, even
if its only purpose is to invoke something similar to <code>occur</code>.
</p>
</div>
</div>
<div id="outline-container-sec-5" class="outline-2">
<h2 id="sec-5">A More Robust Customize Interface</h2>
<div class="outline-text-2" id="text-5">
<p>
The customize interface is powerful and beautiful. Its major achievement, in my
opinion, is to make it Emacs customization accessible even for those who know no
elisp at all. When correctly configured, I could see my grandmother using it.
</p>
<p>
Still, it is for that same reason that we must not admit it to have recurring
bugs. A sizeable part of its user base might never be aware that they're running
into a bug, they'll just think <i>“Emacs is complicated”</i>. So it doesn't help, you
see, that it has <a href="http://emacs.stackexchange.com/q/2112/50">a couple of issues</a> so deep-seated that even experienced lispers
might be led to think they're doing something wrong.
</p>
<p>
There have been <a href="https://lists.gnu.org/archive/html/emacs-devel/2014-10/msg00707.html">talks of fixing this</a> on the dev list, so I'm optimistic.
</p>
</div>
</div>
<div id="outline-container-sec-6" class="outline-2">
<h2 id="sec-6">Namespaces</h2>
<div class="outline-text-2" id="text-6">
<p>
I could go on about this for several posts. Plain and simple: Emacs doesn't
have namespaces, so we have to prepend the package name or prefix to every
single defined symbol. Here's a snippet of what that looks like:
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">package-initialize</span> <span class="p">(</span><span class="k">&optional</span> <span class="nv">no-activate</span><span class="p">)</span>
<span class="s">"[DOC]"</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">package-alist</span> <span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="nv">package-load-all-descriptors</span><span class="p">)</span>
<span class="p">(</span><span class="nv">package-read-all-archive-contents</span><span class="p">)</span>
<span class="p">(</span><span class="nb">unless</span> <span class="nv">no-activate</span>
<span class="p">(</span><span class="nb">dolist</span> <span class="p">(</span><span class="nb">elt</span> <span class="nv">package-alist</span><span class="p">)</span>
<span class="p">(</span><span class="nv">package-activate</span> <span class="p">(</span><span class="nb">car</span> <span class="nb">elt</span><span class="p">))))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">package--initialized</span> <span class="no">t</span><span class="p">))</span></code></pre></figure>
<p>
I'm sure you see how that's annoying to write <i>and</i> read. Not to mention every
symbol is permanently available in the global namespace.
</p>
<p>
There have been talks of implementing a system similar to common-lisp packages,
which takes a lot of adaptation to how symbols are interned. While we wait, I
wrote a <a href="https://github.com/Malabarba/names/">namespacing engine</a> in the form of a macro. Which turns the above into
something like this.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-namespace</span> <span class="nv">package-</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">initialize</span> <span class="p">(</span><span class="k">&optional</span> <span class="nv">no-activate</span><span class="p">)</span>
<span class="s">"[DOC]"</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">alist</span> <span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="nv">load-all-descriptors</span><span class="p">)</span>
<span class="p">(</span><span class="nv">read-all-archive-contents</span><span class="p">)</span>
<span class="p">(</span><span class="nb">unless</span> <span class="nv">no-activate</span>
<span class="p">(</span><span class="nb">dolist</span> <span class="p">(</span><span class="nb">elt</span> <span class="nv">alist</span><span class="p">)</span>
<span class="p">(</span><span class="nv">activate</span> <span class="p">(</span><span class="nb">car</span> <span class="nb">elt</span><span class="p">))))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">-initialized</span> <span class="no">t</span><span class="p">))</span>
<span class="p">)</span></code></pre></figure>
<p>
One way or the other, some form of solution should make its way into Emacs. And
I'll fight for that to happen before 25.
</p>
</div>
</div>
<div id="outline-container-sec-7" class="outline-2">
<h2 id="sec-7">Conclusion</h2>
<div class="outline-text-2" id="text-7">
<p>
That's my list of most immediate improvements. Did I miss something big? Do you
feel insulted and disagree aggressively, or do you feel enlightened and praise
such accuracy?
</p>
<p>
May the year to come be filled with parentheses.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/big-things-to-expect-from-emacs-25.html?source=rss#disqus_thread">Comment on this.</a></p>Aggressive-indent just got better!2014-10-25T00:00:00+00:00https://endlessparentheses.com//aggressive-indent-just-got-better<p>
<a href="https://github.com/Malabarba/aggressive-indent-mode/"><code>aggressive-indent</code></a> is quite something. I’ve only just released it and it seems
to have been very well received. As such, it’s only fair that I invest a bit more
time into it.
</p>
<p>
<a href="/permanent-auto-indentation.html">The original version</a> was really just a hack that was born as <a href="http://emacs.stackexchange.com/a/385/50">an answer on
Emacs.SE</a>. It worked phenomenally well on <code>emacs-lisp-mode</code> (to my delight), but
it lagged a bit on <code>c</code>-like modes.
</p>
<p>
The new version, which is already on Melpa, is much smarter and more optimised.
It should work quite well on any mode where automatic indentation makes sense
(python users, <a href="https://github.com/Malabarba/aggressive-indent-mode/issues/9">voice your suggestions</a>).
</p>
<p>
As a bonus, here’s a stupendous screencast, <a href="https://github.com/Malabarba/aggressive-indent-mode/pull/8">courtesy of Tu Do</a>!
</p>
<div class="figure">
<p><img src="https://github.com/Malabarba/aggressive-indent-mode/raw/master/lisp-example.gif" alt="lisp-example.gif" />
</p>
</div>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Usage</h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Instructions are still the same! So long as you have Melpa configured, you can
install it with.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">M-x package-install RET aggressive-indent</code></pre></figure>
<p>
Then simply turn it on and you’ll <b>never</b> have unindented code again.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-aggressive-indent-mode</span><span class="p">)</span></code></pre></figure>
<p>
You can also turn it on locally with.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'emacs-lisp-mode-hook</span> <span class="nf">#'</span><span class="nv">aggressive-indent-mode</span><span class="p">)</span></code></pre></figure>
</div>
</div>
<p><a href="https://endlessparentheses.com/aggressive-indent-just-got-better.html?source=rss#disqus_thread">Comment on this.</a></p>Kill Entire Line with Prefix Argument2014-10-19T00:00:00+00:00https://endlessparentheses.com//kill-entire-line-with-prefix-argument<p>
I won’t repeat myself on the <a href="/Meta-Binds-Part-1%253A-Drunk-in-the-Dark.html">usefulness of prefix arguments</a>, though it
would hardly be an overstatement. Killing 7 lines of text in two
keystrokes is a bliss most people will never know.
</p>
<p>
Today's post was prompted by <a href="http://emacs.stackexchange.com/q/2347/50">this question</a> on Emacs Stack Exchange. Itsjeyd has
grown tired of using 3 keystrokes to kill a line (<kbd>C-a C-k
C-k</kbd>) and asks for an alternative. The <a href="http://emacs.stackexchange.com/a/2348/50">straightforward answer</a> is to use
<a href="https://doc.endlessparentheses.com/Fun/kill-whole-line"><code>kill-whole-line</code></a> instead, but then you either need another keybind,
<kbd>C-S-backspace</kbd>, or you need to lose the regular <code>kill-line</code>
functionality.
</p>
<p>
<a href="http://emacs.stackexchange.com/a/2353/50">The solution</a> I've found for myself is to use prefix arguments.<br />
You see, killing half a line is a useful feature, but slaughtering three and a
half lines make very little sense. So it stands to reason to have <a href="https://doc.endlessparentheses.com/Fun/kill-line"><code>kill-line</code></a>
meticulously murder everything in its sight when given a prefix argument.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defmacro</span> <span class="nv">bol-with-prefix</span> <span class="p">(</span><span class="k">function</span><span class="p">)</span>
<span class="s">"Define a new function which calls FUNCTION.
Except it moves to beginning of line before calling FUNCTION when
called with a prefix argument. The FUNCTION still receives the
prefix argument."</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">name</span> <span class="p">(</span><span class="nb">intern</span> <span class="p">(</span><span class="nb">format</span> <span class="s">"endless/%s-BOL"</span> <span class="k">function</span><span class="p">))))</span>
<span class="o">`</span><span class="p">(</span><span class="k">progn</span>
<span class="p">(</span><span class="nb">defun</span> <span class="o">,</span><span class="nv">name</span> <span class="p">(</span><span class="nv">p</span><span class="p">)</span>
<span class="o">,</span><span class="p">(</span><span class="nb">format</span>
<span class="s">"Call `%s', but move to BOL when called with a prefix argument."</span>
<span class="k">function</span><span class="p">)</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"P"</span><span class="p">)</span>
<span class="p">(</span><span class="nb">when</span> <span class="nv">p</span>
<span class="p">(</span><span class="nv">forward-line</span> <span class="mi">0</span><span class="p">))</span>
<span class="p">(</span><span class="nv">call-interactively</span> <span class="ss">',function</span><span class="p">))</span>
<span class="ss">',name</span><span class="p">)))</span></code></pre></figure>
<p>
And we bind them, of course.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-set-key</span> <span class="nv">[remap</span> <span class="nv">paredit-kill]</span> <span class="p">(</span><span class="nv">bol-with-prefix</span> <span class="nv">paredit-kill</span><span class="p">))</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="nv">[remap</span> <span class="nv">org-kill-line]</span> <span class="p">(</span><span class="nv">bol-with-prefix</span> <span class="nv">org-kill-line</span><span class="p">))</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="nv">[remap</span> <span class="nv">kill-line]</span> <span class="p">(</span><span class="nv">bol-with-prefix</span> <span class="nv">kill-line</span><span class="p">))</span></code></pre></figure>
<p>
With this little macro, <kbd>C-k</kbd> still kills from point, but
<kbd>C-3 C-k</kbd> swallows three whole lines. As a bonus, we get the
<code>kill-whole-line</code> behavior by doing <kbd>C-1 C-k</kbd>.
</p>
<p>
Are there any other functions which might benefit from this macro?
</p>
<p><a href="https://endlessparentheses.com/kill-entire-line-with-prefix-argument.html?source=rss#disqus_thread">Comment on this.</a></p>Old Packages and New Packages in 24.42014-10-14T00:00:00+00:00https://endlessparentheses.com//old-packages-and-new-packages-in-24-4<p>
Our final post of the series starts with a sober note, but swiftly
moves to a happy ending. Just as we cherish each improvement to our
favorite packages, so must we honor the dead who served us and give
the newborn a chance to thrive. These are the packages marked
obsolete, followed by the new packages you didn’t even know you wanted.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Obsolete packages</h2>
<div class="outline-text-2" id="text-1">
<p>
First, some short comments on the obituary.
</p>
<blockquote>
<p>
longlines.el; use visual-line-mode.
</p>
</blockquote>
<p>
<a href="/longlines-mode-in-latex.html">This is sad</a>, but don’t weep yet. I have a feeling a dead will rise
from this grave on Emacs 25 (stay tuned).
</p>
<blockquote>
<p>
iswitchb.el; use icomplete-mode.
</p>
</blockquote>
<p>
This news flowed around the Emacsphere some months ago. Between
<code>icomplete-mode</code> and <code>ido-mode</code>, <code>iswitchb</code> was no longer necessary.
</p>
<blockquote>
<p>
terminal.el; use term.el instead.
</p>
</blockquote>
<p>
They were redundant, and now they’re identical.
</p>
<blockquote>
<p>
meese.el.
</p>
</blockquote>
<blockquote>
<p>
sup-mouse.el.
</p>
</blockquote>
<blockquote>
<p>
the old version of todo-mode.el (renamed to otodo-mode.el).
</p>
</blockquote>
<blockquote>
<p>
xesam.el (owing to the cancellation of the XESAM project).
</p>
</blockquote>
<blockquote>
<p>
yow.el; use fortune.el or cookie1.el instead.
</p>
</blockquote>
<p>
Five packages I’ve never ever ever heard of. But I’m sure somebody
will miss them.
</p>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">New Modes and Packages in Emacs 24.4</h2>
<div class="outline-text-2" id="text-2">
<p>
Now, the interesting part.
</p>
<blockquote>
<p>
New package eww.el provides a built-in web browser.
This requires Emacs to have been compiled with libxml2 support.
</p>
</blockquote>
<p>
I remember a lot of talk on <code>eww</code>’s speed and visual accuracy. It’s
not always that I salute a package whose role is already filled by
several others, but this one looks promising.
</p>
<blockquote>
<p>
New package nadvice.el offers lighter-weight advice facilities.
It is layered as:<br />
<code>add-function</code> and <code>remove-function</code>, which can be used to add/remove code on
any function-carrying place, such as process filters or <code><foo>-function</code> hooks.<br />
<code>advice-add</code> and <code>advice-remove</code> to add/remove a piece of advice on a named
function, much like <code>defadvice</code> does.
</p>
</blockquote>
<p>
Perhaps the advice interface needed a light-weight alternative. I
never really noticed, and this package (while nice) feels rather
underwhelming. Here’s a usage example.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defvar</span> <span class="nv">x</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">test</span> <span class="p">()</span>
<span class="s">"Description"</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">message</span> <span class="s">"%s"</span> <span class="nv">x</span><span class="p">))</span>
<span class="p">(</span><span class="nv">advice-add</span>
<span class="ss">'test</span> <span class="ss">:around</span>
<span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">fun</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">x</span> <span class="mi">2</span><span class="p">))</span>
<span class="p">(</span><span class="nb">funcall</span> <span class="nv">fun</span><span class="p">))))</span></code></pre></figure>
<blockquote>
<p>
New package frameset.el provides a set of operations to save a frameset
(the state of all or a subset of the existing frames and windows, somewhat
similar to a frame configuration), both in-session and persistently, and
restore it at some point in the future.
</p>
</blockquote>
<p>
Now we’re talking! Can you believe Emacs had no way of saving
window/frame configurations between sessions? I ran into this problem
before, and so did <a href="http://emacs.stackexchange.com/q/822/50">this user at Emacs.SO</a>. Before, all you could do was
save it within a single session.
Thanks to <code>frameset.el</code>, not only can you save configurations
manually, but <a href="https://doc.endlessparentheses.com/Fun/desktop-save-mode"><code>desktop-save-mode</code></a> hooks into that automatically!
</p>
<blockquote>
<p>
New package filenotify.el provides an interface for file system
notifications. It requires that Emacs be compiled with one of the
low-level libraries gfilenotify.c, inotify.c or w32notify.c.
</p>
</blockquote>
<p>
I can imagine some nice uses for this, but we’ll have to see what
comes of it. For what it’s worth, <a href="https://doc.endlessparentheses.com/Fun/auto-revert-mode"><code>auto-revert-mode</code></a> already makes use
of this interface when it’s available, making it much less demanding.
</p>
<blockquote>
<p>
New minor mode <code>superword-mode</code>, which overrides the default word motion
commands to treat "symbol_words" as a single word, similar to what
<code>subword-mode</code> does.
</p>
</blockquote>
<p>
Bozhidar <a href="http://emacsredux.com/blog/2014/08/27/a-peek-at-emacs-24-dot-4-superword-mode/">has more to say on this</a> than I do. I’m a <code>subword-mode</code>
worshipper, so I won’t be using this mode.
</p>
<blockquote>
<p>
New minor modes <code>prettify-symbols-mode</code> and <code>global-prettify-symbols-mode</code>
display specified symbols as composed characters. E.g., in Emacs Lisp mode,
this replaces the string "lambda" with the Greek lambda character.
</p>
</blockquote>
<p>
And we go off with a bang! By default, in <code>emacs-lisp-mode</code>, this will
only turn <code>lambda</code> into <code>λ</code>, but that’s enough reason to turn it on.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-prettify-symbols-mode</span> <span class="mi">1</span><span class="p">)</span></code></pre></figure>
</div>
</div>
<p><a href="https://endlessparentheses.com/old-packages-and-new-packages-in-24-4.html?source=rss#disqus_thread">Comment on this.</a></p>Useful New Features in 24.42014-10-13T00:00:00+00:00https://endlessparentheses.com//useful-new-features-in-24-4<p>
Following yesterday's list of the <a href="/sweet-new-features-in-24-4.html">Sweetest New Features in 24.4</a>, today
I go through the ones that strike me as most useful, either to me or to
the Emacs environment itself. This is the part where I had to cut the
most. Emacs 24.4 exhibits a downpour of usability improvements, and it
almost feels unjust to not list them all. Nonetheless, I've narrowed
them down to 8.
</p>
<blockquote>
<p>
The commands <code>eval-expression</code> (<code>M-:</code>), <code>eval-last-sexp</code> (<code>C-x C-e</code>),
and <code>eval-print-last-sexp</code> (<code>C-j</code> in Lisp Interaction mode) can take a
zero prefix argument. This disables truncation of lists in the output,
equivalent to setting <code>(eval-expression-)print-length</code> and
<code>(eval-expression-)print-level</code> to nil. Additionally, it causes integers
to be printed in other formats (octal, hexadecimal, and character).
</p>
</blockquote>
<p>
Fantastic! The default behaviour of truncating lists proves frustrating
every so often. My solution had been to set <a href="https://doc.endlessparentheses.com/Var/print-length"><code>print-length</code></a> to nil, but
that's also not ideal as you don't always want to see everything either.
</p>
<p>
Being able to quickly toggle between the two is just perfect.
</p>
<blockquote>
<p>
<code>electric-indent-mode</code> is now enabled by default.
</p>
</blockquote>
<blockquote>
<p>
New buffer-local <code>electric-indent-local-mode</code>.
</p>
</blockquote>
<p>
I personally have fallen in love with <a href="/permanent-auto-indentation.html">Aggressive Auto-indentation</a>, but
this is a smart and convenient default. Also, see Bozhidar's <a href="http://emacsredux.com/blog/2014/01/19/a-peek-at-emacs-24-dot-4-auto-indentation-by-default/">Emacs
Redux post</a> on this feature.
</p>
<blockquote>
<p>
New hooks <code>focus-in-hook</code>, <code>focus-out-hook</code>. These are normal hooks run when an Emacs frame gains or loses input focus.
</p>
</blockquote>
<p>
There are <a href="http://emacs.stackexchange.com/questions/521/turn-off-jabber-notifications-when-emacs-has-focus">already</a> <a href="http://emacs.stackexchange.com/questions/265/how-to-auto-save-buffers-when-emacs-loses-focus">two</a> questions on <a href="http://emacs.stackexchange.com/">Emacs.StackExchange</a> which are
solved by these hooks! And I expect they'll pull their weight for a
long time still.
</p>
<blockquote>
<p>
Uniquify is enabled by default
</p>
</blockquote>
<p>
This doesn't really affect me, as <a href="https://github.com/Malabarba/smart-mode-line/">smart-mode-line</a> has its own way of
uniquifying names, but it's a huge improvement over the old numbering
system.
</p>
<blockquote>
<p>
letf is now just an alias for cl-letf.
</p>
</blockquote>
<p>
I shouldn't have to tell you <a href="/understanding-letf-and-how-it-replaces-flet.html">why this is a big deal</a>.
</p>
<blockquote>
<p>
New Dired minor mode <code>dired-hide-details-mode</code>
</p>
</blockquote>
<p>
This used to be a very popular third party extension for dired. It's
always nice to see good ideas getting promoted to built-in.
</p>
<blockquote>
<p>
More packages look for ~/.emacs.d/<foo> additionally to ~/.<foo>.
</p>
</blockquote>
<p>
I won't list each of the affected files here, suffice to say that they
add up to 17 and include some very popular packages, such as <code>places</code>
and <code>ido</code>. You could always do this yourself by configuring a score of
different variables, but it's nice to see Emacs taking initiative
against home clutter.
</p>
<blockquote>
<p>
New macro <code>define-alternatives</code> can be used to define generic commands.
</p>
</blockquote>
<p>
I had to write up some code to see what this was about and it
certainly piqued my interest. <a href="https://doc.endlessparentheses.com/Fun/define-alternatives"><code>define-alternatives</code></a> is a command which
defines a function with several possible implementations. The user is
then asked to choose the implementation he prefers upon invoking the command
for the first time.
</p>
<p>
For instance, say I'm writing up a major-mode for the Julia language.
I want to offer a compilation command, but I don't known whether the
user will prefer synchronous or asynchronous compilation. So I use
define alternatives.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-alternatives</span> <span class="nv">julia-mode-compile</span>
<span class="c1">;; These keywords are used on the defcustom</span>
<span class="c1">;; for `julia-mode-compile-alternatives'.</span>
<span class="ss">:group</span> <span class="nv">julia-mode</span>
<span class="ss">:version</span> <span class="s">"1.0"</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">julia-mode-compile-alternatives</span>
<span class="o">'</span><span class="p">((</span><span class="s">"Compile synchronously"</span> <span class="o">.</span>
<span class="nv">julia-mode-compile-synchronously</span><span class="p">)</span>
<span class="p">(</span><span class="s">"Compile asynchronously"</span> <span class="o">.</span>
<span class="nv">julia-mode-compile-asynchronously</span><span class="p">)))</span></code></pre></figure>
<p>
The first time the user invokes <kbd>M-x</kbd>
<code>julia-mode-compile</code>, they will be asked to choose an implementation.
</p>
<p><a href="https://endlessparentheses.com/useful-new-features-in-24-4.html?source=rss#disqus_thread">Comment on this.</a></p>Sweet New Features in 24.42014-10-12T00:00:00+00:00https://endlessparentheses.com//sweet-new-features-in-24-4<p>
At last, Emacs 24.4 is <a href="https://lists.gnu.org/archive/html/emacs-devel/2014-10/msg00376.html">getting released next Monday</a> (the 20th). To
commemorate, I took a stroll through the <i>“News”</i> file and picked a
few new features that spoke to me the most. I had to cut dozens of
interesting items for it to fit the format of this blog. And I still
had to divide it into 3 parts.
</p>
<p>
Today, I’ll just list my reaction to some sweet new features. Worthy
of mention: back in December 2013 Mickey made <a href="http://www.masteringemacs.org/articles/2013/12/29/whats-new-in-emacs-24-4/">a huge post</a> also listing
new features, and Bozhidar has been doing a <a href="http://emacsredux.com/blog/categories/emacs24-dot-4/">series of great posts</a> on
the subject.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Favourite New Features</h2>
<div class="outline-text-2" id="text-1">
<blockquote>
<p>
New [Tramp] connection method "adb", which allows to access Android.
</p>
</blockquote>
<p>
Woohoo!
</p>
<blockquote>
<p>
New hook <code>eval-expression-minibuffer-setup-hook</code> run by <code>eval-expression</code> on entering the minibuffer.
</p>
</blockquote>
<blockquote>
<p>
You can enable ElDoc inside the <code>eval-expression</code> minibuffer.
</p>
</blockquote>
<p>
This is awesome! Try it now! Evaluate the following, then call
<kbd>M-:</kbd> and type something like <code>(message</code>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-hook</span>
<span class="ss">'eval-expression-minibuffer-setup-hook</span>
<span class="nf">#'</span><span class="nv">eldoc-mode</span><span class="p">)</span></code></pre></figure>
<p>
It displays in the mode-line!
</p>
<blockquote>
<p>
The <b>Messages</b> buffer is created in <code>messages-buffer-mode</code>, a new
major mode, with read-only status. Any code that might create the
<b>Messages</b> buffer should call the function <code>messages-buffer</code> to do so
and set up the mode.
</p>
</blockquote>
<p>
<a href="/new-messages-buffer-mode.html">I've announced my joy for this before.</a>
</p>
<blockquote>
<p>
New option <code>load-prefer-newer</code> affects how the <code>load</code> function chooses the file to load.
</p>
</blockquote>
<p>
Hopefully, someday, <code>t</code> will be the default value of this. For the
moment, make sure you add <code>(setq load-prefer-newer t)</code> so you’re never
accidentally using outdated compiled files.
</p>
<blockquote>
<p>
New library subr-x.el with miscellaneous small utility functions: <code>hash-table-keys</code>, <code>hash-table-values</code>, <code>string-blank-p</code>, <code>string-empty-p</code>, <code>string-join</code>, <code>string-reverse</code>, <code>string-trim-left</code>, <code>string-trim-right</code>, <code>string-trim</code>, <code>string-remove-prefix</code>, <code>string-remove-suffix</code>
</p>
</blockquote>
<p>
Get in the habit of using these functions. They greatly reduce the need
for the <code>s-</code> package, not that there’s anything wrong with it but less
package dependencies lead to a cleaner environment.
</p>
<blockquote>
<p>
New macro <code>with-eval-after-load</code>.
</p>
</blockquote>
<p>
Like <a href="https://doc.endlessparentheses.com/Fun/eval-after-load"><code>eval-after-load</code></a>, but you don’t need to quote the body.
</p>
<blockquote>
<p>
New commands <code>toggle-frame-fullscreen</code> and <code>toggle-frame-maximized</code>, bound to <f11> and M-<f10>, respectively.
</p>
</blockquote>
<blockquote>
<p>
Support for menus on text-mode terminals.
</p>
</blockquote>
<blockquote>
<p>
Multi-monitor support
</p>
</blockquote>
<p>
User-experience, user-experience, user-experience.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/sweet-new-features-in-24-4.html?source=rss#disqus_thread">Comment on this.</a></p>Intelligent browse-url2014-10-11T00:00:00+00:00https://endlessparentheses.com//intelligent-browse-url<p>
When you have more than one browser installed, you must choose which
one's the default. The OS will then proceed to open all URLs in the
default browser, even if it happens to be closed while another one
happens to be running.
</p>
<p>
At least in Emacs we can fix that behavior. The following code
configures Emacs to use whatever browser happens to be open right now,
instead of always defaulting to the same one.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;;; We don't need to `require' because this function is only</span>
<span class="c1">;;; ever called by `browse-url' itself. But if you have</span>
<span class="c1">;;; problems, try uncommenting this:</span>
<span class="c1">;; (require 'browse-url)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/browse-url-best-browser</span> <span class="p">(</span><span class="nv">url</span> <span class="k">&rest</span> <span class="nv">_</span><span class="p">)</span>
<span class="s">"Use a running browser or start the preferred one."</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">url</span> <span class="p">(</span><span class="nv">browse-url-encode-url</span> <span class="nv">url</span><span class="p">))</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">process-environment</span>
<span class="p">(</span><span class="nv">browse-url-process-environment</span><span class="p">))</span>
<span class="p">(</span><span class="nv">command</span> <span class="p">(</span><span class="nv">endless/decide-browser</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">start-process</span> <span class="p">(</span><span class="nv">concat</span> <span class="nv">command</span> <span class="s">" "</span> <span class="nv">url</span><span class="p">)</span>
<span class="no">nil</span> <span class="nv">command</span> <span class="nv">url</span><span class="p">)))</span>
<span class="c1">;; Use this for all links. If you actually don't want some</span>
<span class="c1">;; links to be viewed externally, change this line here.</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">browse-url-browser-function</span>
<span class="o">'</span><span class="p">((</span><span class="s">"."</span> <span class="o">.</span> <span class="nv">endless/browse-url-best-browser</span><span class="p">)))</span></code></pre></figure>
<p>
Here we configure our preferences through the <kbd>endless/browser-list</kbd>
variable. Its value is a list of cons cells, each representing a
browser. The <kbd>car</kbd> is a regexp to match the name of the browser's
process (used to determine whether the browser is running), and the <kbd>cdr</kbd>
is the name of the executable.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">defcustom</span> <span class="nv">endless/browser-list</span>
<span class="o">'</span><span class="p">((</span><span class="s">"xulrunner\\|conkeror"</span> <span class="o">.</span> <span class="s">"conkeror.sh"</span><span class="p">)</span>
<span class="p">(</span><span class="s">"xulrunner\\|conkeror"</span> <span class="o">.</span> <span class="s">"conkeror"</span><span class="p">)</span>
<span class="s">"conkeror.exe"</span> <span class="c1">;; This works if it's in your $PATH</span>
<span class="s">"luakit"</span>
<span class="p">(</span><span class="s">"chrome$"</span> <span class="o">.</span> <span class="s">"google-chrome-stable"</span><span class="p">)</span>
<span class="s">"chromium"</span>
<span class="s">"chromium-browser"</span>
<span class="p">(</span><span class="s">"firefox\\|mozilla"</span> <span class="o">.</span> <span class="s">"firefox"</span><span class="p">)</span>
<span class="c1">;; This works regardless of your $PATH</span>
<span class="p">(</span><span class="s">"firefox.exe"</span> <span class="o">.</span>
<span class="s">"c:/Program Files (x86)/Mozilla Firefox/firefox.exe"</span><span class="p">))</span>
<span class="s">"List of browsers by order of preference.
Each element is a cons cell (REGEXP . EXEC-FILE).
If REGEXP matches the name of a currently running process and if
EXEC-FILE a valid executable, EXEC-FILE will be used to open the
given URL.
An element can also be a string, in this case, it is used as both
the REGEXP and the EXEC-FILE.
It is safe to have items referring to not-installed browsers,
they are gracefully ignored."</span>
<span class="ss">:type</span> <span class="o">'</span><span class="p">(</span><span class="nv">repeat</span> <span class="p">(</span><span class="nv">choice</span> <span class="nb">string</span> <span class="p">(</span><span class="nb">cons</span> <span class="nv">regexp</span> <span class="nv">file</span><span class="p">))))</span></code></pre></figure>
<p>
And this is the function responsible for checking the running
processes and finding a browser in there.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/decide-browser</span> <span class="p">()</span>
<span class="s">"Decide best browser to use based on `endless/browser-list'."</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">process-list</span>
<span class="p">(</span><span class="nb">mapcar</span> <span class="nf">#'</span><span class="nv">endless/process-name</span>
<span class="p">(</span><span class="nv">list-system-processes</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">browser-list</span> <span class="nv">endless/browser-list</span><span class="p">)</span>
<span class="nv">browser</span> <span class="nv">out</span><span class="p">)</span>
<span class="c1">;; Find the first browser on the list that is open.</span>
<span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nb">and</span> <span class="nv">browser-list</span> <span class="p">(</span><span class="nb">null</span> <span class="nv">out</span><span class="p">))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">browser</span> <span class="p">(</span><span class="nb">car</span> <span class="nv">browser-list</span><span class="p">))</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nv">cl-member</span> <span class="p">(</span><span class="nv">car-or-self</span> <span class="nv">browser</span><span class="p">)</span>
<span class="nv">process-list</span> <span class="ss">:test</span> <span class="ss">'string-match</span><span class="p">)</span>
<span class="p">(</span><span class="nv">executable-find</span> <span class="p">(</span><span class="nv">cdr-or-self</span> <span class="nv">browser</span><span class="p">)))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">out</span> <span class="p">(</span><span class="nv">cdr-or-self</span> <span class="nv">browser</span><span class="p">))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">browser-list</span> <span class="p">(</span><span class="nb">cdr</span> <span class="nv">browser-list</span><span class="p">))))</span>
<span class="c1">;; Use the one we found, or the first one available.</span>
<span class="p">(</span><span class="nb">or</span> <span class="nv">out</span> <span class="p">(</span><span class="nv">endless/first-existing-browser</span><span class="p">))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/first-existing-browser</span> <span class="p">()</span>
<span class="s">"Return the first installed browser in `endless/browser-list'."</span>
<span class="p">(</span><span class="nb">require</span> <span class="ss">'cl-lib</span><span class="p">)</span>
<span class="p">(</span><span class="nv">cdr-or-self</span>
<span class="p">(</span><span class="nb">car</span>
<span class="p">(</span><span class="nv">cl-member-if</span>
<span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span> <span class="p">(</span><span class="nv">executable-find</span> <span class="p">(</span><span class="nv">cdr-or-self</span> <span class="nv">x</span><span class="p">)))</span>
<span class="nv">endless/browser-list</span><span class="p">))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/process-name</span> <span class="p">(</span><span class="nv">proc</span><span class="p">)</span>
<span class="p">(</span><span class="nb">cdr</span> <span class="p">(</span><span class="nb">assoc</span> <span class="ss">'comm</span> <span class="p">(</span><span class="nv">process-attributes</span> <span class="nv">proc</span><span class="p">))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">car-or-self</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span>
<span class="s">"If X is a list, return the car. Otherwise, return X."</span>
<span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nv">car-safe</span> <span class="nv">x</span><span class="p">)</span> <span class="nv">x</span><span class="p">))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">cdr-or-self</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span>
<span class="s">"If X is a list, return the cdr. Otherwise, return X."</span>
<span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nv">cdr-safe</span> <span class="nv">x</span><span class="p">)</span> <span class="nv">x</span><span class="p">))</span></code></pre></figure>
<p>
I've tested it on Windows and a couple of Linux distros. Could a Mac
user test it for me as well?</p>
<p><a href="https://endlessparentheses.com/intelligent-browse-url.html?source=rss#disqus_thread">Comment on this.</a></p>And the Beta goes Public2014-10-07T00:00:00+00:00https://endlessparentheses.com//and-the-beta-goes-public<p>
The site goes well and the curtains are raised. The
<a href="http://emacs.stackexchange.com/">Emacs.StackExchange</a> beta has left the privacy of its dressing room and
ventures onto the public stage. Even if you have nothing in your mind to
ask, go pay it a visit and have a browse through the questions. The
quality of content is impressive, and you’re sure to learn something
of interest.
</p>
<p>
I’ve already brought here several lessons I would never have learnt if
not for the beta. <a href="/auto-focus-a-relevant-file-in-dired-buffers.html">Smart Dired</a>, <a href="/longlines-mode-in-latex.html">Longlines mode</a>, and <a href="/permanent-auto-indentation.html">Aggressive
Auto-indentation</a> are now essential parts of my init file. Of course,
I’ll keep bringing more, they come up faster than I can write about them.
</p>
<p>
Not to mention, the community is friendly and intelligent. Not that
it’s any surprise in the Emacs ecosystem.
</p>
<p><a href="https://endlessparentheses.com/and-the-beta-goes-public.html?source=rss#disqus_thread">Comment on this.</a></p>Keymap for Launching External Applications and Websites2014-10-05T00:00:00+00:00https://endlessparentheses.com//keymap-for-launching-external-applications-and-websites<p>
The <a href="/launcher-keymap-for-standalone-features.html">launcher-map</a> wouldn't be half of what it is if not for the <code>run</code>
macro. With it, we can easily turn Emacs into a quick 'n dirty app
launcher.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defmacro</span> <span class="nv">run</span> <span class="p">(</span><span class="nv">exec</span><span class="p">)</span>
<span class="s">"Return a named function that runs EXEC."</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">func-name</span> <span class="p">(</span><span class="nb">intern</span> <span class="p">(</span><span class="nv">concat</span> <span class="s">"endless/run-"</span> <span class="nv">exec</span><span class="p">))))</span>
<span class="o">`</span><span class="p">(</span><span class="k">progn</span>
<span class="p">(</span><span class="nb">defun</span> <span class="o">,</span><span class="nv">func-name</span> <span class="p">()</span>
<span class="o">,</span><span class="p">(</span><span class="nb">format</span> <span class="s">"Run the %s executable."</span> <span class="nv">exec</span><span class="p">)</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">start-process</span> <span class="s">""</span> <span class="no">nil</span> <span class="o">,</span><span class="nv">exec</span><span class="p">))</span>
<span class="nf">#'</span><span class="o">,</span><span class="nv">func-name</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"m"</span> <span class="p">(</span><span class="nv">run</span> <span class="s">"Mathematica"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"k"</span> <span class="p">(</span><span class="nv">run</span> <span class="s">"keepass"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"v"</span> <span class="p">(</span><span class="nv">run</span> <span class="s">"steam"</span><span class="p">))</span> <span class="c1">; Vapor =P</span></code></pre></figure>
<p>
We could use a lambda instead of the macro, but the macro defines a
nicely documented function.
</p>
<p>
While we're at it, let's do something similar for websites.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defmacro</span> <span class="nv">browse</span> <span class="p">(</span><span class="nv">url</span><span class="p">)</span>
<span class="s">"Return a named function that calls `browse-url' on URL."</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">func-name</span> <span class="p">(</span><span class="nb">intern</span> <span class="p">(</span><span class="nv">concat</span> <span class="s">"endless/browse-"</span> <span class="nv">url</span><span class="p">))))</span>
<span class="o">`</span><span class="p">(</span><span class="k">progn</span>
<span class="p">(</span><span class="nb">defun</span> <span class="o">,</span><span class="nv">func-name</span> <span class="p">()</span>
<span class="o">,</span><span class="p">(</span><span class="nb">format</span> <span class="s">"Browse to the url %s."</span> <span class="nv">url</span><span class="p">)</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">browse-url</span> <span class="o">,</span><span class="nv">url</span><span class="p">))</span>
<span class="nf">#'</span><span class="o">,</span><span class="nv">func-name</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"t"</span>
<span class="p">(</span><span class="nv">browse</span> <span class="s">"http://twitter.com/"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"?"</span> <span class="c1">;; See also SX.el.</span>
<span class="p">(</span><span class="nv">browse</span> <span class="s">"http://emacs.stackexchange.com/"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"r"</span>
<span class="p">(</span><span class="nv">browse</span> <span class="s">"http://www.reddit.com/r/emacs/"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"w"</span>
<span class="p">(</span><span class="nv">browse</span> <span class="s">"http://www.emacswiki.org/"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"+"</span>
<span class="p">(</span><span class="nv">browse</span> <span class="s">"https://plus.google.com/communities/114815898697665598016"</span><span class="p">))</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/keymap-for-launching-external-applications-and-websites.html?source=rss#disqus_thread">Comment on this.</a></p>Longlines mode in LaTeX2014-09-30T00:00:00+00:00https://endlessparentheses.com//longlines-mode-in-latex<p>
Emacs.SE has truly revived my init file. Every day, a new snippet gets
added. I <a href="/auto-focus-a-relevant-file-in-dired-buffers.html">started this series</a> only 6 days ago, and it’s already on its
fourth episode.
</p>
<p>
<a href="http://emacs.stackexchange.com/q/443/50">Today's question</a> was asked by me, and answered by Tikhon Jelvis,
Francesco, and Sacha Chua. As usual, this link might not work for you
until the beta goes public.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Question</h2>
<div class="outline-text-2" id="text-1">
<p>
I’ve long been keeping my LaTeX documents under version control.
Still, the effectiveness of this initiative would be greatly improved
if I could bring myself to adopt a <i>“one-sentence-per-line”</i> approach.
It would facilitate managing people’s conributions and reverting old
changes.
</p>
<p>
If you’re wondering what’s so difficult about that, here is what such
a document would look like. (<i>Long sentences are to be avoided, but in
scientific writing you don’t always have a choice</i>).
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">A quite short sentence here.
New sentence on the same paragraph, with more text with more text with more text with more text.
Some more text, still on the same paragraph.
This is another very very very very very very very very very very very very very very very very very very very very long sentence.</code></pre></figure>
<p>
The lack of readability above is evident. Having text extend beyond 80
columns is trouble enough, but intertwining long and short lines just
makes my brain weep in despair.
</p>
<p>
The official solution is to activate <a href="https://doc.endlessparentheses.com/Fun/visual-line-mode"><code>visual-line-mode</code></a> and add window
margins (or just decrease window width). That causes the lines to wrap
(visually, not in the file) and increases readability by an order of
magnitude.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">A quite short sentence here.
New sentence on the same paragraph, with more text with more
text with more text with more text.
Some more text, still on the same paragraph.
This is another very very very very very very very very very
very very very very very very very very very very very long
sentence.</code></pre></figure>
<p>
But alas, it comes at a cost. If this text were indented by a few
spaces (quite common in LaTeX), the outcome looks like it went through
a blender. And don’t get me started on broken equations.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text"> A quite short sentence here.
New sentence on the same paragraph, with more text with
more text with more text with more text.
Some more text, still on the same paragraph.
This is another very very very very very very very very
very very very very very very very very very very very very
long sentence.
\begin{equation}
H = equation * lines + sometimes - \need{to}{be /
long}
\end{equation}</code></pre></figure>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Solution</h2>
<div class="outline-text-2" id="text-2">
<p>
<i>tdsh points out in the comments, the <a href="http://elpa.gnu.org/packages/adaptive-wrap.html"><code>adaptive-wrap</code></a> package fixes
the indentation issue for <code>visual-line-mode</code>.</i>
</p>
<p>
The solution reached is a hack to make <a href="https://doc.endlessparentheses.com/Fun/longlines-mode"><code>longlines-mode</code></a> act more
intelligently on LaTeX buffers. This minor mode is similar to the
combination of <code>visual-line-mode</code> and window margins mentioned
above. It makes lines wrap at <a href="https://doc.endlessparentheses.com/Var/fill-column"><code>fill-column</code></a>, intead of <code>window-width</code>.
</p>
<p>
The advantage to <code>longlines-mode</code> is that it’s implemented in elisp
(while window margins are done in C code), so we can hack it to our
hearts’ content. Bafflingly, this little gold nugget has been marked
obsolete on Emacs 24.4.
</p>
<p>
The hack below does 3 things:
</p>
<ol class="org-ol">
<li>It enables “soft” wrapping of text. That is, the text is wrapped to
<code>fill-column</code> in the buffer, but that does not reflect in the file.
</li>
<li>It fixes the indentation problem. So wrapping an indented line follows
the line’s indentation. So environments with indented text (like itemize, or
theorem) are <b>much</b> more readable.
</li>
<li>It even prevents wrapping of equations!
</li>
</ol>
<p>
The code is a bit heavy, so I’ve added comments describing the lines
which were actually changed.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">require</span> <span class="ss">'longlines</span> <span class="no">nil</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'LaTeX-mode-hook</span> <span class="nf">#'</span><span class="nv">longlines-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">longlines-encode-region</span> <span class="p">(</span><span class="nv">beg</span> <span class="nv">end</span> <span class="k">&optional</span> <span class="nv">_buffer</span><span class="p">)</span>
<span class="s">"Replace each soft newline between BEG and END with exactly one space.
Hard newlines are left intact. The optional argument BUFFER exists for
compatibility with `format-alist', and is ignored."</span>
<span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">reg-max</span> <span class="p">(</span><span class="nb">max</span> <span class="nv">beg</span> <span class="nv">end</span><span class="p">))</span>
<span class="p">(</span><span class="nb">mod</span> <span class="p">(</span><span class="nv">buffer-modified-p</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nb">min</span> <span class="nv">beg</span> <span class="nv">end</span><span class="p">))</span>
<span class="c1">;; Changed this line to "swallow" indendation when decoding.</span>
<span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nv">search-forward-regexp</span> <span class="s">" *\\(\n\\) *"</span> <span class="nv">reg-max</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">pos</span> <span class="p">(</span><span class="nv">match-beginning</span> <span class="mi">1</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">unless</span> <span class="p">(</span><span class="nv">get-text-property</span> <span class="nv">pos</span> <span class="ss">'hard</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">match-end</span> <span class="mi">0</span><span class="p">))</span> <span class="c1">; This line too</span>
<span class="p">(</span><span class="nv">insert-and-inherit</span> <span class="s">" "</span><span class="p">)</span>
<span class="p">(</span><span class="nv">replace-match</span> <span class="s">""</span> <span class="ss">:fixedcase</span> <span class="ss">:literal</span><span class="p">)</span> <span class="c1">; This line too</span>
<span class="p">(</span><span class="nv">remove-text-properties</span> <span class="nv">pos</span> <span class="p">(</span><span class="nb">1+</span> <span class="nv">pos</span><span class="p">)</span> <span class="ss">'hard</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">set-buffer-modified-p</span> <span class="nb">mod</span><span class="p">)</span>
<span class="nv">end</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">longlines-wrap-line</span> <span class="p">()</span>
<span class="s">"If the current line needs to be wrapped, wrap it and return nil.
If wrapping is performed, point remains on the line. If the line does
not need to be wrapped, move point to the next line and return t."</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nv">bound-and-true-p</span> <span class="nv">latex-extra-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nb">null</span> <span class="p">(</span><span class="nv">latex/do-auto-fill-p</span><span class="p">)))</span>
<span class="p">(</span><span class="k">progn</span> <span class="p">(</span><span class="nv">forward-line</span> <span class="mi">1</span><span class="p">)</span> <span class="no">t</span><span class="p">)</span>
<span class="c1">;; The conditional above was added for latex equations. It relies</span>
<span class="c1">;; on the latex-extra package (on Melpa).</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nv">longlines-set-breakpoint</span><span class="p">)</span>
<span class="c1">;; Make sure we don't break comments.</span>
<span class="p">(</span><span class="nb">null</span> <span class="p">(</span><span class="nb">nth</span> <span class="mi">4</span> <span class="p">(</span><span class="nv">parse-partial-sexp</span>
<span class="p">(</span><span class="nv">line-beginning-position</span><span class="p">)</span> <span class="p">(</span><span class="nv">point</span><span class="p">)))))</span>
<span class="p">(</span><span class="k">progn</span>
<span class="c1">;; This `let' and the `when' below add indentation to the</span>
<span class="c1">;; wrapped line.</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">indent</span> <span class="p">(</span><span class="nv">save-excursion</span> <span class="p">(</span><span class="nv">back-to-indentation</span><span class="p">)</span>
<span class="p">(</span><span class="nv">current-column</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">insert-before-markers-and-inherit</span> <span class="nv">?\n</span><span class="p">)</span>
<span class="p">(</span><span class="nv">backward-char</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="nv">delete-char</span> <span class="mi">-1</span><span class="p">)</span>
<span class="p">(</span><span class="nv">forward-char</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">></span> <span class="nv">indent</span> <span class="mi">0</span><span class="p">)</span>
<span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nv">insert</span> <span class="p">(</span><span class="nb">make-string</span> <span class="nv">indent</span> <span class="nv">?</span> <span class="p">)))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">longlines-wrap-point</span>
<span class="p">(</span><span class="nb">+</span> <span class="nv">longlines-wrap-point</span> <span class="nv">indent</span><span class="p">))))</span>
<span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">longlines-merge-lines-p</span><span class="p">)</span>
<span class="p">(</span><span class="k">progn</span> <span class="p">(</span><span class="nv">end-of-line</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nb">prog1</span> <span class="p">(</span><span class="nv">bolp</span><span class="p">)</span> <span class="p">(</span><span class="nv">forward-char</span> <span class="mi">1</span><span class="p">))</span> <span class="p">(</span><span class="nv">eolp</span><span class="p">))</span>
<span class="p">(</span><span class="k">progn</span>
<span class="p">(</span><span class="nv">delete-char</span> <span class="mi">-1</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">></span> <span class="nv">longlines-wrap-point</span> <span class="p">(</span><span class="nv">point</span><span class="p">))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">longlines-wrap-point</span>
<span class="p">(</span><span class="nb">1-</span> <span class="nv">longlines-wrap-point</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">insert-before-markers-and-inherit</span> <span class="nv">?\s</span><span class="p">)</span>
<span class="p">(</span><span class="nv">backward-char</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="nv">delete-char</span> <span class="mi">-1</span><span class="p">)</span>
<span class="p">(</span><span class="nv">forward-char</span> <span class="mi">1</span><span class="p">)</span>
<span class="c1">;; This removes whitespace added for indentation.</span>
<span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nb">eq</span> <span class="p">(</span><span class="nv">char-after</span><span class="p">)</span> <span class="nv">?</span> <span class="p">)</span>
<span class="p">(</span><span class="nv">delete-char</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">longlines-wrap-point</span>
<span class="p">(</span><span class="nb">1-</span> <span class="nv">longlines-wrap-point</span><span class="p">))))</span>
<span class="no">nil</span><span class="p">)</span>
<span class="p">(</span><span class="nv">forward-line</span> <span class="mi">1</span><span class="p">)</span>
<span class="no">t</span><span class="p">))))</span></code></pre></figure>
</div>
<div id="outline-container-sec-2-1" class="outline-3">
<h3 id="sec-2-1">Update <span class="timestamp-wrapper"><span class="timestamp"><2014-10-07 Tue></span></span></h3>
<div class="outline-text-3" id="text-2-1">
<p>
Fixed handling of comments. Now comments don’t get wrapped either.
</p>
</div>
</div>
</div>
<p><a href="https://endlessparentheses.com/longlines-mode-in-latex.html?source=rss#disqus_thread">Comment on this.</a></p>Aggressive Auto-indentation2014-09-28T00:00:00+00:00https://endlessparentheses.com//permanent-auto-indentation<p>
<a href="https://doc.endlessparentheses.com/Fun/electric-indent-mode"><code>electric-indent-mode</code></a> is enough to keep your code nicely
aligned when all you do is type. However, once you start shifting
blocks around, transposing lines, or slurping and barfing sexps,
indentation is bound to go wrong.
</p>
<p>
Today’s lesson is <a href="http://emacs.stackexchange.com/a/385/50">my answer to mgoszcz2's question</a>. Having the perfect
auto-indent is easier than you might think. I've turned this post into a
package. Emacsers, meet <a href="https://github.com/Malabarba/aggressive-indent-mode">aggressive-indent</a>.
</p>
<p>
So long as you have Melpa configured, you can install it with.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">M-x package-install RET aggressive-indent</code></pre></figure>
<p>
Then simply turn it on and you’ll <b>never</b> have unindented code again.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-aggressive-indent-mode</span><span class="p">)</span></code></pre></figure>
<p>
This will activate <code>aggressive-indent-mode</code> on every non-text buffer. If you're
a little shy, you can also turn it on only for specific major modes by using
hooks.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'emacs-lisp-mode-hook</span> <span class="nf">#'</span><span class="nv">aggressive-indent-mode</span><span class="p">)</span></code></pre></figure>
<p>
Here’s some code to try it on. After you’ve evaluated the above, open
a new <i>“.el”</i> file, paste the following code, and type something
before the opening parentheses.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">this</span> <span class="nv">is</span> <span class="nv">a</span>
<span class="nv">test</span><span class="p">)</span></code></pre></figure>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Update <span class="timestamp-wrapper"><span class="timestamp"><2014-10-20 Mon></span></span></h2>
<div class="outline-text-2" id="text-1">
<p>
Announce <a href="https://github.com/Malabarba/aggressive-indent-mode">new package</a>!
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/permanent-auto-indentation.html?source=rss#disqus_thread">Comment on this.</a></p>Updating org-mode #+INCLUDE: statements on the fly2014-09-26T00:00:00+00:00https://endlessparentheses.com//updating-org-mode-include-statements-on-the-fly<p>
Today’s post regards <a href="http://emacs.stackexchange.com/a/246/50">my answer</a> to <a href="http://emacs.stackexchange.com/q/64/50">kaushalmodi's question</a>. Since the
beta is <a href="/emacs-stack-exchange-enters-beta.html">still private</a>, you might not be able to follow those links
quite yet, so I’ll summ it up here.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">The Question</h2>
<div class="outline-text-2" id="text-1">
<p>
Kaushalmodi uses <code>#+INCLUDE:</code> statements with line specifications in his
org files. Here is an example similar to his. 14 and 80 are the first
and last line of a class declaration in the source file, so they’re
quite obvious for a human to identify.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">#+INCLUDE: "code/my-class.sv" :src systemverilog :lines "14-80"</code></pre></figure>
<p>
The problem here is that whenever <i>“my-class.sv”</i> is edited those
line numbers are likely to become outdated. So you would have to go
through each org file which might include <i>“my-class.sv”</i> and update
the numbers.
</p>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">The Solution</h2>
<div class="outline-text-2" id="text-2">
<p>
Unfortunately, org-mode doesn’t have a flexible way of declaring
include statements. You either specify the line numbers or you don’t.
</p>
<p>
The <a href="http://emacs.stackexchange.com/a/286/50">solution was</a> to add <code>:range-begin</code> and <code>:range-end</code> keywords to
the statement
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">#+INCLUDE: "code/my-class.sv" :src systemverilog :range-begin "^class" :range-end "^endclass" :lines "14-80"</code></pre></figure>
<p>
and then write a function which
</p>
<ol class="org-ol">
<li>goes through each <code>#+INCLUDE:</code> statement in the buffer,
</li>
<li>checks if it has <code>:range-begin</code> and/or <code>:range-end</code> keywords and
takes their arguments as regular expressions,
</li>
<li>visits the relevant file and searches for these regular expressions,
</li>
<li>checks what the line numbers are now,
</li>
<li>and updates them accordingly in the org buffer.
</li>
</ol>
<p>
This function can then be assigned to a key, added to
<a href="https://doc.endlessparentheses.com/Var/before-save-hook"><code>before-save-hook</code></a>, or added to one of org-mode’s bajillion available
hooks. Finally, to go the extra mile, we make the behaviour
customizable per file extension through a <code>defcustom</code>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'before-save-hook</span> <span class="nf">#'</span><span class="nv">endless/update-includes</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/update-includes</span> <span class="p">(</span><span class="k">&rest</span> <span class="k">ignore</span><span class="p">)</span>
<span class="s">"Update the line numbers of #+INCLUDE:s in current buffer.
Only looks at INCLUDEs that have either :range-begin or :range-end.
This function does nothing if not in org-mode, so you can safely
add it to `before-save-hook'."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">derived-mode-p</span> <span class="ss">'org-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">point-min</span><span class="p">))</span>
<span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nv">search-forward-regexp</span>
<span class="s">"^\\s-*#\\+INCLUDE: *\"\\([^\"]+\\)\".*:range-\\(begin\\|end\\)"</span>
<span class="no">nil</span> <span class="ss">'noerror</span><span class="p">)</span>
<span class="p">(</span><span class="k">let*</span> <span class="p">((</span><span class="nv">file</span> <span class="p">(</span><span class="nv">expand-file-name</span> <span class="p">(</span><span class="nv">match-string-no-properties</span> <span class="mi">1</span><span class="p">)))</span>
<span class="nv">lines</span> <span class="nv">begin</span> <span class="nv">end</span><span class="p">)</span>
<span class="p">(</span><span class="nv">forward-line</span> <span class="mi">0</span><span class="p">)</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">looking-at</span> <span class="s">"^.*:range-begin *\"\\([^\"]+\\)\""</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">begin</span> <span class="p">(</span><span class="nv">match-string-no-properties</span> <span class="mi">1</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">looking-at</span> <span class="s">"^.*:range-end *\"\\([^\"]+\\)\""</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">end</span> <span class="p">(</span><span class="nv">match-string-no-properties</span> <span class="mi">1</span><span class="p">)))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">lines</span> <span class="p">(</span><span class="nv">endless/decide-line-range</span> <span class="nv">file</span> <span class="nv">begin</span> <span class="nv">end</span><span class="p">))</span>
<span class="p">(</span><span class="nb">when</span> <span class="nv">lines</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">looking-at</span> <span class="s">".*:lines *\"\\([-0-9]+\\)\""</span><span class="p">)</span>
<span class="p">(</span><span class="nv">replace-match</span> <span class="nv">lines</span> <span class="ss">:fixedcase</span> <span class="ss">:literal</span> <span class="no">nil</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">line-end-position</span><span class="p">))</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">" :lines \""</span> <span class="nv">lines</span> <span class="s">"\""</span><span class="p">))))))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/decide-line-range</span> <span class="p">(</span><span class="nv">file</span> <span class="nv">begin</span> <span class="nv">end</span><span class="p">)</span>
<span class="s">"Visit FILE and decide which lines to include.
BEGIN and END are regexps which define the line range to use."</span>
<span class="p">(</span><span class="k">let</span> <span class="p">(</span><span class="nv">l</span> <span class="nv">r</span><span class="p">)</span>
<span class="p">(</span><span class="nv">save-match-data</span>
<span class="p">(</span><span class="nv">with-temp-buffer</span>
<span class="p">(</span><span class="nv">insert-file</span> <span class="nv">file</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">point-min</span><span class="p">))</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">null</span> <span class="nv">begin</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">l</span> <span class="s">""</span><span class="p">)</span>
<span class="p">(</span><span class="nv">search-forward-regexp</span> <span class="nv">begin</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">l</span> <span class="p">(</span><span class="nv">line-number-at-pos</span> <span class="p">(</span><span class="nv">match-beginning</span> <span class="mi">0</span><span class="p">))))</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">null</span> <span class="nv">end</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">r</span> <span class="s">""</span><span class="p">)</span>
<span class="p">(</span><span class="nv">search-forward-regexp</span> <span class="nv">end</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">r</span> <span class="p">(</span><span class="nb">1+</span> <span class="p">(</span><span class="nv">line-number-at-pos</span> <span class="p">(</span><span class="nv">match-end</span> <span class="mi">0</span><span class="p">)))))</span>
<span class="p">(</span><span class="nb">format</span> <span class="s">"%s-%s"</span> <span class="nv">l</span> <span class="nv">r</span><span class="p">)))))</span></code></pre></figure>
</div>
</div>
<p><a href="https://endlessparentheses.com/updating-org-mode-include-statements-on-the-fly.html?source=rss#disqus_thread">Comment on this.</a></p>Auto-focus a Relevant File in Dired Buffers2014-09-25T00:00:00+00:00https://endlessparentheses.com//auto-focus-a-relevant-file-in-dired-buffers<p>
The <a href="http://emacs.stackexchange.com/">emacs.stackexchange</a> beta has only just started and interesting
topics have already begun to pop up. Partially to share snippets I
find nifty, and partially to help promote the beta, I’ll be posting
here questions and answers I find engaging.
</p>
<p>
I’m kicking off this series with a <a href="http://emacs.stackexchange.com/q/140/50">question of my own</a>, answered by none
other than <a href="http://emacs.stackexchange.com/a/150/50">Sebastian Wiesner</a>. (Did you know he <a href="http://www.lunaryorn.com/">has a blog</a>)? The beta
is still private, so you might not be able to visit the links above,
but I’ll summ it up here.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Question</h2>
<div class="outline-text-2" id="text-1">
<p>
For a while now I’ve noticed my dired patterns are somewhat
predictable.
</p>
<ul class="org-ul">
<li>Whenever I visit the directory of one of my papers, I go straight to
the <i>“master.tex”</i> file.
</li>
<li>When I open the root of an android project, it’s almost always to
visit the the <i>“AndroidManifest.xml”</i>.
</li>
<li>Finally, when I go to a directory where I’m developing an emacs
package, 80% of the time I’m headed for the package's main source
file.
</li>
</ul>
<p>
See the pattern? For all these directories, the first file I visit is
very predictable, so I wanted dired to focus that file automatically
for me. So I would only need to hit <kbd>RET</kbd>—lazyness
to the extreme.
</p>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Solution</h2>
<div class="outline-text-2" id="text-2">
<p>
Sebastian looks into the <code>save-place</code> package’s source code, and finds
out about <a href="https://doc.endlessparentheses.com/Var/dired-initial-position-hook"><code>dired-initial-position-hook</code></a>, which is run exactly when we
need. Visiting a file whose name we already know is then a trivial
matter.<br />
<i>Note I’ve edited his code a bit.</i>
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">defcustom</span> <span class="nv">endless/important-files</span>
<span class="o">'</span><span class="p">(</span><span class="s">"master.tex"</span> <span class="s">"AndroidManifest.xml"</span> <span class="s">"init.org"</span><span class="p">)</span>
<span class="s">"List of files which dired should focus by default."</span>
<span class="ss">:type</span> <span class="o">'</span><span class="p">(</span><span class="nv">repeat</span> <span class="nb">string</span><span class="p">))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">my-dired-goto-important-file</span> <span class="p">()</span>
<span class="s">"Go to an important file in the current dired buffer."</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">candidates</span> <span class="nv">endless/important-files</span><span class="p">)</span>
<span class="p">(</span><span class="nv">matched</span> <span class="no">nil</span><span class="p">))</span>
<span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nb">and</span> <span class="nv">candidates</span> <span class="p">(</span><span class="nb">null</span> <span class="nv">matched</span><span class="p">))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">matched</span> <span class="p">(</span><span class="nv">dired-goto-file</span>
<span class="p">(</span><span class="nv">expand-file-name</span> <span class="p">(</span><span class="nb">pop</span> <span class="nv">candidates</span><span class="p">)))))</span>
<span class="p">(</span><span class="nb">unless</span> <span class="nv">matched</span>
<span class="p">(</span><span class="nv">endless/goto-elisp-file</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'dired-initial-position-hook</span>
<span class="c1">;; Append so we run after `save-place'</span>
<span class="nf">#'</span><span class="nv">my-dired-goto-important-file</span> <span class="ss">'append</span><span class="p">)</span></code></pre></figure>
<p>
Making dired focus the source file of elisp packages is tad bit
tricker because the file’s name is not fixed. The solution I found
(still borrowing some of Wiesner’s code) was to look for a file whose
name matched the current directory’s name, and if it that doesn’t
exist just focus any <i>“.el”</i> file. This works well for me because my
directory structure typically looks like <i>“~/Git/paradox/paradox.el”</i>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/goto-elisp-file</span> <span class="p">()</span>
<span class="s">"Go to a file with .el extension.
If more than one exists, go to the one with the same name as this
directory. This is enough to catch most package source files."</span>
<span class="p">(</span><span class="k">let*</span> <span class="p">((</span><span class="nv">files</span> <span class="p">(</span><span class="nv">endless/dired-file-list</span><span class="p">))</span>
<span class="p">(</span><span class="nv">dirname</span> <span class="p">(</span><span class="nv">file-name-base</span> <span class="p">(</span><span class="nv">directory-file-name</span> <span class="nv">default-directory</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">target</span> <span class="p">(</span><span class="nv">concat</span> <span class="nv">dirname</span> <span class="s">".el"</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">unless</span> <span class="p">(</span><span class="nv">dired-goto-file</span> <span class="p">(</span><span class="nv">expand-file-name</span> <span class="nv">target</span><span class="p">))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">target</span>
<span class="p">(</span><span class="nv">car-safe</span> <span class="p">(</span><span class="nv">cl-member-if</span>
<span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span> <span class="p">(</span><span class="nv">string-match</span> <span class="s">"\\.el$"</span> <span class="nv">x</span><span class="p">))</span>
<span class="nv">files</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">when</span> <span class="nv">target</span> <span class="p">(</span><span class="nv">dired-goto-file</span> <span class="p">(</span><span class="nv">expand-file-name</span> <span class="nv">target</span><span class="p">))))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/dired-file-list</span> <span class="p">()</span>
<span class="s">"List of files in this dired buffer."</span>
<span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="k">let</span> <span class="p">(</span><span class="nv">files</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">point-min</span><span class="p">))</span>
<span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nb">not</span> <span class="p">(</span><span class="nv">eobp</span><span class="p">))</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">filename</span> <span class="p">(</span><span class="nv">dired-get-filename</span> <span class="no">nil</span> <span class="ss">'no-error</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">when</span> <span class="nv">filename</span>
<span class="p">(</span><span class="nb">push</span> <span class="nv">filename</span> <span class="nv">files</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">forward-line</span> <span class="mi">1</span><span class="p">))</span>
<span class="nv">files</span><span class="p">)))</span></code></pre></figure>
</div>
</div>
<p><a href="https://endlessparentheses.com/auto-focus-a-relevant-file-in-dired-buffers.html?source=rss#disqus_thread">Comment on this.</a></p>Emacs Stack Exchange enters Beta2014-09-24T00:00:00+00:00https://endlessparentheses.com//emacs-stack-exchange-enters-beta<p>
If you read <a href="http://www.reddit.com/r/emacs/">r/emacs</a> or follow some Emacs bloggers on twitter, you’ll
have noticed a proposal for an Emacs stack exchange site was launched
a couple of weeks ago. The commitment rate of the proposal was <a href="http://discuss.area51.stackexchange.com/a/17913/28113">nearly
record-breaking</a>, and now it has entered <a href="http://emacs.stackexchange.com/">private beta</a>.
</p>
<p>
If I understand correctly, the private beta will only last a couple of
weeks, but members are allowed to send out invites. So, if you missed
the commitment stage but you’d like to help, drop a comment here.
</p>
<p>
Even if you don’t think you are expert enough to give good answers,
asking questions and voting is a great help already. The focus during
this stage should be <b>difficult</b> and <b>interesting</b> questions. So don’t
go easy!
This stage sets the tone of the entire website.
</p>
<p><a href="https://endlessparentheses.com/emacs-stack-exchange-enters-beta.html?source=rss#disqus_thread">Comment on this.</a></p>Fixing org-in-src-block-p2014-09-23T00:00:00+00:00https://endlessparentheses.com//fixing-org-in-src-block-p<p>
For reasons which ellude me, the <a href="https://doc.endlessparentheses.com/Fun/org-in-src-block-p"><code>org-in-src-block-p</code></a> function is
disappointingly inconsistent for me. Given its major role in
<a href="/emacs-narrow-or-widen-dwim.html">narrow-or-widen-dwim</a>, this frequently led to <a href="https://doc.endlessparentheses.com/Fun/org-edit-src-code"><code>org-edit-src-code</code></a> not
getting called when point was inside a code block.
</p>
<p>
I’ve edited the <a href="/emacs-narrow-or-widen-dwim.html">narrow-or-widen-dwim</a> to not need that function. Go
copy it again if you were using it.
</p>
<p><a href="https://endlessparentheses.com/fixing-org-in-src-block-p.html?source=rss#disqus_thread">Comment on this.</a></p>Exclude Directories from Grep2014-09-20T00:00:00+00:00https://endlessparentheses.com//exclude-directories-from-grep<p>
I keep a couple of subdirectories inside my <i>“.emacs.d/”</i> for the
purpose of organization, so <a href="https://doc.endlessparentheses.com/Fun/rgrep"><code>rgrep</code></a>'ing is tremendously useful for
finding where a variable is being changed. By default, that will also
search inside <i>“elpa</i>”/, which slows the search and pollutes the
results page.
</p>
<p>
We can make grep more selective and, while we're at it, enable line
truncation.
</p>
<figure class="highlight"><pre><code class="language-emacs-lisp" data-lang="emacs-lisp"><span class="p">(</span><span class="nv">eval-after-load</span> <span class="ss">'grep</span>
<span class="o">'</span><span class="p">(</span><span class="k">progn</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'grep-find-ignored-directories</span> <span class="s">"tmp"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'grep-find-ignored-directories</span> <span class="s">"node_modules"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'grep-find-ignored-directories</span> <span class="s">".bundle"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'grep-find-ignored-directories</span> <span class="s">"auto"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'grep-find-ignored-directories</span> <span class="s">"elpa"</span><span class="p">)))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">wgrep-enable-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-c C-c"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'grep-mode-hook</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">()</span> <span class="p">(</span><span class="nv">toggle-truncate-lines</span> <span class="mi">1</span><span class="p">)))</span></code></pre></figure>
<p>
And don't forget to bind <kbd>rgrep</kbd> to your <a href="/launcher-keymap-for-standalone-features.html">Launcher Keymap</a>!
</p>
<p><a href="https://endlessparentheses.com/exclude-directories-from-grep.html?source=rss#disqus_thread">Comment on this.</a></p>Prettify your Quotation Marks2014-09-18T00:00:00+00:00https://endlessparentheses.com//prettify-your-quotation-marks<p>
Typography is a long and blurry road, of which I know very little.
There are, still, some simple lessons that take you a long a way.
Round (Unicode) quotation marks is one of them.
</p>
<p>
LaTeX already does that for you, but many other prose environments
don’t. This snippet inserts “round” quotes for you, bind it to your
text-modes.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-key</span> <span class="nv">org-mode-map</span> <span class="s">"\""</span> <span class="nf">#'</span><span class="nv">endless/round-quotes</span><span class="p">)</span>
<span class="p">(</span><span class="nv">eval-after-load</span> <span class="ss">'markdown-mode</span>
<span class="o">'</span><span class="p">(</span><span class="nv">define-key</span> <span class="nv">markdown-mode-map</span> <span class="s">"\""</span>
<span class="nf">#'</span><span class="nv">endless/round-quotes</span><span class="p">))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/round-quotes</span> <span class="p">(</span><span class="nv">italicize</span><span class="p">)</span>
<span class="s">"Insert “” and leave point in the middle.
With prefix argument ITALICIZE, insert /“”/ instead
\(meant for org-mode).
Inside a code-block, just call `self-insert-command'."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"P"</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nv">derived-mode-p</span> <span class="ss">'org-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nv">org-in-block-p</span> <span class="o">'</span><span class="p">(</span><span class="s">"src"</span> <span class="s">"latex"</span> <span class="s">"html"</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">call-interactively</span> <span class="nf">#'</span><span class="nv">self-insert-command</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">looking-at</span> <span class="s">"”[/=_\\*]?"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">match-end</span> <span class="mi">0</span><span class="p">))</span>
<span class="p">(</span><span class="nb">when</span> <span class="nv">italicize</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">derived-mode-p</span> <span class="ss">'markdown-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">"__"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">"//"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">forward-char</span> <span class="mi">-1</span><span class="p">))</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">"“”"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">forward-char</span> <span class="mi">-1</span><span class="p">))))</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/prettify-your-quotation-marks.html?source=rss#disqus_thread">Comment on this.</a></p>Launcher Keymap for Standalone Features2014-09-13T00:00:00+00:00https://endlessparentheses.com//launcher-keymap-for-standalone-features<p>
Following on our series of mnemonic keymaps, we arrive on the
<b>launcher-map</b>. Where the <a href="/the-toggle-map-and-wizardry.html">toggle-map</a> was designed for toggling values
and minor-modes we only use every once in a while, the launcher-map
runs those standalone features of Emacs that also don't always see the
light of day.
</p>
<p>
To remember these keybinds, think of <i>“Emacs launch calc”</i> for instance.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-prefix-command</span> <span class="ss">'launcher-map</span><span class="p">)</span>
<span class="c1">;; `C-x l' is `count-lines-page' by default. If you</span>
<span class="c1">;; use that, you can try s-l or <C-return>.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">ctl-x-map</span> <span class="s">"l"</span> <span class="ss">'launcher-map</span><span class="p">)</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"s-l"</span><span class="p">)</span> <span class="ss">'launcher-map</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"p"</span> <span class="nf">#'</span><span class="nv">paradox-list-packages</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"c"</span> <span class="nf">#'</span><span class="nv">calc</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"d"</span> <span class="nf">#'</span><span class="nv">ediff-buffers</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"f"</span> <span class="nf">#'</span><span class="nv">find-dired</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"g"</span> <span class="nf">#'</span><span class="nv">lgrep</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"G"</span> <span class="nf">#'</span><span class="nv">rgrep</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"h"</span> <span class="nf">#'</span><span class="nv">man</span><span class="p">)</span> <span class="c1">; Help</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"i"</span> <span class="nf">#'</span><span class="nv">package-install-from-buffer</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"n"</span> <span class="nf">#'</span><span class="nv">endless/visit-notifications</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">launcher-map</span> <span class="s">"s"</span> <span class="nf">#'</span><span class="nv">shell</span><span class="p">)</span></code></pre></figure>
<p>
<code>calc</code>, <code>man</code>, <code>list-packages</code>… I use these features, on average, once or
twice every other moon. An intuitive keymap is exactly what I need to always
remember their keybinds and not have to <kbd>M-x</kbd> their whole name.
</p>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Update <span class="timestamp-wrapper"><span class="timestamp"><30 Nov 2015></span></span></h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Changed the <code>n</code> key from <code>nethack</code> to <code>endless/visit-notifications</code>, as per <a href="/using-paradox-for-github-notifications.html">the
new post</a>.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/launcher-keymap-for-standalone-features.html?source=rss#disqus_thread">Comment on this.</a></p>Swaping Variables with cl-lib2014-09-07T00:00:00+00:00https://endlessparentheses.com//emacs-advanced-basics-swaping-variables<p>
On today's episode, we again find ourselves admiring the <code>cl</code> package
and focus on a very little-known feature of Emacs, <a href="https://doc.endlessparentheses.com/Fun/psetq"><code>psetq</code></a>. Short for
<i>“parallel-setq”</i>, it takes the same syntax as <a href="https://doc.endlessparentheses.com/Fun/setq"><code>setq</code></a>, but saves all
values before assigning.
</p>
<p>
For instance, if one needs to swap the variables <code>old</code> and <code>new</code>,
assuming you've required <code>cl-lib</code>, you can simply do the following.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">cl-psetq</span> <span class="nv">old</span> <span class="nv">new</span>
<span class="nv">new</span> <span class="nv">old</span><span class="p">)</span></code></pre></figure>
<p>
And, of course, it has also been generalized to <a href="https://doc.endlessparentheses.com/Fun/psetf"><code>psetf</code></a> for assigning
generalized places.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Update <span class="timestamp-wrapper"><span class="timestamp"><2014-09-09 Tue></span></span></h2>
<div class="outline-text-2" id="text-1">
<p>
Truth be told, <code>psetq</code> is more useful when your demands are more
sophisticated. When all you need is to swap two variables (or places),
the <a href="https://www.reddit.com/r/emacs/comments/2fq2u5/swaping_variables_with_cllib/ckc43f2">nice</a> and <a href="https://plus.google.com/103652929131043355278/posts/MpLBUF6NKZS">quick</a> solution is <a href="https://doc.endlessparentheses.com/Fun/cl-rotatef"><code>cl-rotatef</code></a>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">cl-rotatef</span> <span class="nv">old</span> <span class="nv">new</span><span class="p">)</span></code></pre></figure>
</div>
</div>
<p><a href="https://endlessparentheses.com/emacs-advanced-basics-swaping-variables.html?source=rss#disqus_thread">Comment on this.</a></p>Quick update on narrow-or-widen-dwim2014-09-06T00:00:00+00:00https://endlessparentheses.com//quick-update-on-narrow-or-widen-dwim<p>
To those who were interested in <a href="/emacs-narrow-or-widen-dwim.html"><code>narrow-or-widen-dwim</code></a>, the code has
been updated a couple of times since being posted.
</p>
<p>
<a href="/emacs-narrow-or-widen-dwim.html">Visit the post</a> to see the improvements.
</p>
<p><a href="https://endlessparentheses.com/quick-update-on-narrow-or-widen-dwim.html?source=rss#disqus_thread">Comment on this.</a></p>Emacs Lisp Style Guide2014-09-05T00:00:00+00:00https://endlessparentheses.com//emacs-lisp-style-guide<p>
While the official elisp reference does contain some style tips, it's
hard to deny that the community needed a more comprehensive and (at
the same time) concise style guide. Mostly, new Emacs developers have
been left to learn the ropes by reading other source files. As practical
as that might be, it's dangerous in its propensity to propagate bad
habits.
</p>
<p>
To medicate this, Bozhidar Batsov, whose <a href="http://emacsredux.com/">blog</a> I'm sure you follow,
puts up a community-driven <a href="https://github.com/bbatsov/emacs-lisp-style-guide">style guide</a> for Emacs lisp.
</p>
<p>
If you write any sort of Emacs package, make sure you pay it a visit
and see if you can think of anything that's missing. I'm doing my part
over there, let's get that plane off the ground.
</p>
<p><a href="https://endlessparentheses.com/emacs-lisp-style-guide.html?source=rss#disqus_thread">Comment on this.</a></p>Understanding letf and how it replaces flet2014-08-31T00:00:00+00:00https://endlessparentheses.com//understanding-letf-and-how-it-replaces-flet<p>
Once you've come to terms with <a href="/emacs-advanced-basics-the-ins-and-outs-of-setf.html">power of <code>setf</code></a>, it is time to meet its
older sister, <a href="https://doc.endlessparentheses.com/Fun/cl-letf"><code>cl-letf</code></a>. As the name implies, <code>letf</code> is to <code>setf</code> like
<code>let</code> is to <code>setq</code>, but, once again, that is only the tip of the
iceberg.
</p>
<p>
To get started, let's have a variable and require the feature.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">require</span> <span class="ss">'cl-lib</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">my-list</span> <span class="o">'</span><span class="p">(</span><span class="mi">0</span> <span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span> <span class="mi">4</span> <span class="mi">5</span><span class="p">))</span></code></pre></figure>
<p>
When temporarily assigning a value to a variable, <code>letf</code> is (mostly)
identical to <code>let</code>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">cl-letf</span> <span class="p">((</span><span class="nv">my-list</span> <span class="s">"not actually a list"</span><span class="p">))</span>
<span class="p">(</span><span class="nv">message</span> <span class="s">"%s"</span> <span class="nv">my-list</span><span class="p">))</span> <span class="c1">;; ==> "not actually a list"</span></code></pre></figure>
<p>
They differs slightly in behaviour when you don't provide a value.
<code>let</code> binds the variable to nil and restores it upon exit (as I'm sure
you know), while <code>letf</code> preserves the current value and restores it
upon exit.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">cl-letf</span> <span class="p">((</span><span class="nv">my-list</span><span class="p">))</span>
<span class="p">(</span><span class="nv">message</span> <span class="s">"%s"</span> <span class="nv">my-list</span><span class="p">))</span> <span class="c1">;; ==> "(0 1 2 3 4 5)"</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">my-list</span><span class="p">))</span>
<span class="p">(</span><span class="nv">message</span> <span class="s">"%s"</span> <span class="nv">my-list</span><span class="p">))</span> <span class="c1">;; ==> "nil"</span></code></pre></figure>
<p>
Of course, that's not where <code>letf</code> shines. It's when you use place
expressions that it simply blows <code>let</code> out of the water. Let's take a
simple example using our list.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">cl-letf</span> <span class="p">(((</span><span class="nb">car</span> <span class="nv">my-list</span><span class="p">)</span> <span class="mi">1000</span><span class="p">)</span>
<span class="p">((</span><span class="nb">elt</span> <span class="nv">my-list</span> <span class="mi">3</span><span class="p">)</span> <span class="mi">200</span><span class="p">))</span>
<span class="p">(</span><span class="nv">message</span> <span class="s">"%s"</span> <span class="nv">my-list</span><span class="p">))</span> <span class="c1">;; ==> "(1000 1 2 200 4 5)"</span></code></pre></figure>
<p>
If you're having trouble reading past the three consecutive
parentheses, this snippet simply takes our list and temporarily
changes its first and fourth elements to <code>1000</code> and <code>200</code>,
respectively.
</p>
<p>
<i>“Alright, but how often is that actually useful?”</i>
</p>
<p>
It all depends on your creativity. <a href="http://stackoverflow.com/questions/18895605/should-flet-be-replaced-with-cl-flet-or-cl-letf">This stackoverflow question</a>
highlights a common issue that came up with the release of Emacs 24.3.
<code>flet</code> (short for function-let) was a macro which locally (and
dynamically) replaced the function definition associated to given
symbols. This is extremely useful for error testing, but if you try to
use this macro now you'll get following the message.
</p>
<blockquote>
<p>
Warning: `flet' is an obsolete macro (as of 24.3); use either `cl-flet' or `cl-letf'.
</p>
</blockquote>
<p>
Unfortunately, <code>cl-flet</code> is not identical to the original
<code>flet</code>—it's lexical, not dynamic.
</p>
<p>
For instance, the <code>url-retrieve-synchronously</code> usually prints a
message on the echo area. Now say you want to prevent that, you can
temporarily rebind the <code>message</code> function to do nothing.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">silent-retrieve</span> <span class="p">()</span>
<span class="s">"Example"</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="k">flet</span> <span class="p">((</span><span class="nv">message</span> <span class="p">(</span><span class="k">&rest</span> <span class="nv">args</span><span class="p">)</span> <span class="no">nil</span><span class="p">))</span>
<span class="p">(</span><span class="nv">url-retrieve-synchronously</span>
<span class="s">"http://www.google.com"</span><span class="p">)))</span></code></pre></figure>
<p>
Now do <kbd>M-x silent-retrieve</kbd>, and see that it works.
Unfortunately, if you try to be a good coder and replace the obsolete
<code>flet</code> with the recommended <code>cl-flet</code>, <a href="http://stackoverflow.com/a/18895704/491532">it won't work</a>!
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">letf as a replacement for flet</h2>
<div class="outline-text-2" id="text-1">
<p>
The solution I usually see <a href="http://emacsredux.com/blog/2013/09/05/a-proper-replacement-for-flet/">floating around</a> is to employ Nic Ferrier's
fantastic <a href="https://github.com/nicferrier/emacs-noflet">noflet</a> package. But I've never seen anyone mention the
built-in option, even though Emacs itself tells you to use it: <i>“use
either `cl-flet' or `cl-letf'”</i>.
</p>
<p>
You see, <code>(symbol-function SYMBOL)</code> is a valid place expression. Which
means you can bind it <i>dynamically</i> using <code>cl-letf</code>. Evaluate the
following defun and call <kbd>M-x new-silent-retrieve</kbd>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">new-silent-retrieve</span> <span class="p">()</span>
<span class="s">"Example"</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nv">cl-letf</span> <span class="p">(((</span><span class="nb">symbol-function</span> <span class="ss">'message</span><span class="p">)</span> <span class="nf">#'</span><span class="nb">format</span><span class="p">))</span>
<span class="p">(</span><span class="nv">url-retrieve-synchronously</span>
<span class="s">"http://www.google.com"</span><span class="p">)))</span></code></pre></figure>
<p>
It works! No messaging! All that we've done was tell <code>letf</code> to
temporarily replace the <code>message</code> function with the <code>format</code> function
(which does the same thing without echoing). We could also use
<code>ignore</code> instead of <code>format</code> (the latter just happens to have the same
return value as <code>message</code>).
</p>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Update</h2>
<div class="outline-text-2" id="text-2">
<ul class="org-ul">
<li>Wiesner <a href="http://www.reddit.com/r/emacs/comments/2f1v7w/understanding_letf_and_how_it_replaces_flet/">reminds me</a> of <code>ignore</code>.
</li>
<li>Ferrier <a href="https://twitter.com/nicferrier/status/506004827449868288">points out</a> that noflet's <code>this-fn</code> feature is something you
can't do with <code>cl-letf</code>.
</li>
<li>Hodique <a href="https://plus.google.com/103652929131043355278/posts/S5mztyczZ34">teaches</a> that Emacs 24.3.1 has a subtle bug in that regard.
</li>
</ul>
</div>
</div>
<p><a href="https://endlessparentheses.com/understanding-letf-and-how-it-replaces-flet.html?source=rss#disqus_thread">Comment on this.</a></p>The ins and outs of setf2014-08-24T00:00:00+00:00https://endlessparentheses.com//emacs-advanced-basics-the-ins-and-outs-of-setf<p>
<a href="https://doc.endlessparentheses.com/Fun/setf"><code>setf</code></a> is a modest name for a macro that does much more work than it
gets paid for. Quoting the doc page:
</p>
<blockquote><p class="verse">
This is a generalized version of `setq'; the PLACEs may be symbolic<br />
references such as (car x) or (aref x i), as well as plain symbols.<br />
For example, (setf (cadr x) y) is equivalent to (setcar (cdr x) y).<br />
The return value is the last VAL in the list.<br />
</p></blockquote>
<p>
Upon reading that, you'd be excused for thinking something mediocre
such as <i>“That's neat!”</i> or <i>“I'll try to remember that”</i>. That
understatement of a doc page merely touches on the power that lies
beneath the surface.
</p>
<p>
Let's start with an example from the <a href="http://www.gnu.org/software/emacs/manual/html_mono/cl.html#Generalized-Variables">manual</a>, depending on your Emacs
version you may need to <code>(require 'cl)</code> first.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">world</span> <span class="s">"world"</span><span class="p">))</span>
<span class="p">(</span><span class="nb">setf</span> <span class="p">(</span><span class="nv">substring</span> <span class="nv">world</span> <span class="mi">2</span> <span class="mi">4</span><span class="p">)</span> <span class="s">"o"</span><span class="p">))</span>
<span class="c1">;; world is now "wood"</span></code></pre></figure>
<p>
Now say you want to replace the entire contents of a buffer, instead
of the manual <a href="https://doc.endlessparentheses.com/Fun/erase-buffer"><code>erase-buffer</code></a> then <a href="https://doc.endlessparentheses.com/Fun/insert"><code>insert</code></a>, you could do
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">setf</span> <span class="p">(</span><span class="nv">buffer-string</span><span class="p">)</span> <span class="s">"replacement"</span><span class="p">)</span></code></pre></figure>
<p>
Let's get slightly more practical. Do you know which function changes
the buffer being displayed by a given window? How about changing the
height of a window? You don't need to!
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">setf</span> <span class="p">(</span><span class="nv">window-buffer</span> <span class="nv">given-window</span><span class="p">)</span> <span class="p">(</span><span class="nv">get-buffer</span> <span class="s">"*scratch*"</span><span class="p">))</span>
<span class="p">(</span><span class="nb">setf</span> <span class="p">(</span><span class="nv">window-height</span><span class="p">)</span> <span class="mi">10</span><span class="p">)</span></code></pre></figure>
<p>
And finally, if we just want to get cute,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">setf</span> <span class="p">(</span><span class="nv">mark</span><span class="p">)</span> <span class="mi">10</span>
<span class="p">(</span><span class="nv">point</span><span class="p">)</span> <span class="mi">20</span><span class="p">)</span></code></pre></figure>
<p>
For a wider list, have a look at cl package <a href="http://www.gnu.org/software/emacs/manual/html_mono/cl.html#Generalized-Variables">manual page</a>, which lists what you
get by requiring <code>cl</code> or <code>cl-lib</code>, or see the elisp <a href="http://www.gnu.org/software/emacs/manual/html_node/elisp/Setting-Generalized-Variables.html">manual page</a>, which lists
what's loaded by default on recent versions of Emacs. My thanks to <a href="http://nullprogram.com/blog/2014/05/27/#comment-1555848160">Christopher
Wellons</a> and <a href="http://www.reddit.com/r/emacs/comments/2eg87a/emacs_advanced_basics_the_ins_and_outs_of_setf/cjzfgae">Rob Thorpe</a> for the links, and it was <a href="http://nullprogram.com/blog/2014/05/27/">Wellons' post on string
mutability</a> which inspired this post.
</p>
<p><a href="https://endlessparentheses.com/emacs-advanced-basics-the-ins-and-outs-of-setf.html?source=rss#disqus_thread">Comment on this.</a></p>Meta Binds Part 3: Smart string insertion2014-08-17T00:00:00+00:00https://endlessparentheses.com//meta-binds-part-3-smart-string-insertion<p>
Have you ever stopped to consider what is the string you type most
often?
</p>
<p>
In my case the answer is two-fold: <code>“~/”</code> and <code>“.emacs.d/”</code>.
Minibuffer completion helps, but we can always make things better.
The following snippet intelligently inserts both of these with a
single key, and it's smart enough to know which one you want.
</p>
<p>
Besides its use in writing configurations, it's great for going
straight to one of these directories inside the <a href="https://doc.endlessparentheses.com/Fun/find-file"><code>find-file</code></a> prompt.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"M-ç"</span><span class="p">)</span> <span class="nf">#'</span><span class="nv">endless/~-or-emacs.d</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/~-or-emacs.d</span> <span class="p">()</span>
<span class="s">"Insert '~/', then '.emacs.d/'."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">looking-back</span> <span class="s">"~/"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">".emacs.d/"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">insert</span> <span class="s">"~/"</span><span class="p">)))</span></code></pre></figure>
<p>
The <kbd>ç</kbd> key is right under my right pinky, but find
one that fits your need.
</p>
<p><a href="https://endlessparentheses.com/meta-binds-part-3-smart-string-insertion.html?source=rss#disqus_thread">Comment on this.</a></p>An Update on gmail-message-mode for Firefox2014-08-12T00:00:00+00:00https://endlessparentheses.com//an-update-on-gmail-message-mode-for-firefox<p>
To those who were interested in <a href="/write-gmail-in-emacs-the-easy-way-gmail-message-mode.html">gmail-message-mode</a>. Thanks to <a href="https://github.com/patjak">patjak</a> and
<a href="https://github.com/oantolin">oantolin</a>, there is now a solution for using it in Firefox as well. See
the <a href="/write-gmail-in-emacs-the-easy-way-gmail-message-mode.html">original post</a> for the instructions.</p>
<p><a href="https://endlessparentheses.com/an-update-on-gmail-message-mode-for-firefox.html?source=rss#disqus_thread">Comment on this.</a></p>Use Org-Mode Links for Absolutely Anything2014-08-10T00:00:00+00:00https://endlessparentheses.com//use-org-mode-links-for-absolutely-anything<p>
One little-know feature of <a href="http://orgmode.org/">org-mode</a> is that you can define new types
of links with the aptly named <a href="https://doc.endlessparentheses.com/Fun/org-add-link-type"><code>org-add-link-type</code></a>. The applications of
this virtue are many. One might, for instance, write links which
search an entire code base for an expression.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">org-add-link-type</span>
<span class="s">"grep"</span> <span class="ss">'endless/follow-grep-link</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/follow-grep-link</span> <span class="p">(</span><span class="nv">regexp</span><span class="p">)</span>
<span class="s">"Run `rgrep' with REGEXP as argument."</span>
<span class="p">(</span><span class="nv">grep-compute-defaults</span><span class="p">)</span>
<span class="p">(</span><span class="nv">rgrep</span> <span class="nv">regexp</span> <span class="s">"*"</span> <span class="p">(</span><span class="nv">expand-file-name</span> <span class="s">"./"</span><span class="p">)))</span></code></pre></figure>
<p>
Then, when you click on something like the following link in an
<code>org-mode</code> buffer, you'll be taken to a list of results.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">** TODO Refactor [[grep:OldClassName][OldClassName]] into NewClassName </code></pre></figure>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">LINK header arguments</h2>
<div class="outline-text-2" id="text-1">
<p>
As <a href="http://www.reddit.com/r/emacs/comments/2d6hjs/use_orgmode_links_for_absolutely_anything/cjmyxfm">/u/blue1_ points out</a>, for links that are simple URL substitutions
you can also use <code>#+LINK</code> headers.
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">#+LINK: isbn http://www.amazon.com/dp/%s</code></pre></figure>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Tag Searches</h2>
<div class="outline-text-2" id="text-2">
<p>
For another use case, the following code defines links which search
your headlines for specific tags.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">org-add-link-type</span>
<span class="s">"tag"</span> <span class="ss">'endless/follow-tag-link</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/follow-tag-link</span> <span class="p">(</span><span class="nv">tag</span><span class="p">)</span>
<span class="s">"Display a list of TODO headlines with tag TAG.
With prefix argument, also display headlines without a TODO keyword."</span>
<span class="p">(</span><span class="nv">org-tags-view</span> <span class="p">(</span><span class="nb">null</span> <span class="nv">current-prefix-arg</span><span class="p">)</span> <span class="nv">tag</span><span class="p">))</span></code></pre></figure>
<p>
Then, merely write your links as
</p>
<figure class="highlight"><pre><code class="language-text" data-lang="text">[[tag:work+phonenumber-boss][Optional Description]]</code></pre></figure>
<p>
The syntax allowed, <a href="http://orgmode.org/manual/Matching-tags-and-properties.html#Match-syntax">described here</a>, is the same used for the
<a href="https://doc.endlessparentheses.com/Fun/org-tags-view"><code>org-tags-view</code></a> command.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/use-org-mode-links-for-absolutely-anything.html?source=rss#disqus_thread">Comment on this.</a></p>Write Gmail in Emacs the Easy Way: gmail-message-mode2014-08-09T00:00:00+00:00https://endlessparentheses.com//write-gmail-in-emacs-the-easy-way-gmail-message-mode<p>
Trying out the myriad of Emacs mail clients is no less than an
odyssey. I am proud to say I dove into this sea of protocols and
credentials and emerged from it a better man, albeit empty handed.
</p>
<p>
I do not blame the clients available. Gnus and Mew, in particular, are
both Herculean beasts of coding prowess. It was I who failed,
regrettably, to fit them into my workflow.
Thus I designed my own solution.
</p>
<p>
<a href="https://github.com/Malabarba/gmail-mode/">gmail-message-mode</a> combines all the features of Gmail's web interface,
with the editing prowess we all love about Emacs. How does it work?
</p>
<ul class="org-ul">
<li>First of all, it is <b>not</b> a mail client.
</li>
<li>In you browser, when composing a message, you invoke a hotkey that sends you to Emacs.
</li>
<li>In Emacs, you can use plain text or the full power of Markdown to write your email.
</li>
<li>Hit <kbd>C-x #</kbd> to finish your edits and <code>gmail-message-mode</code> seamlessly
converts the message back to Gmail's format (html).
</li>
</ul>
<p>
The installation process is as follows.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Install the mode</h2>
<div class="outline-text-2" id="text-1">
<p>
Good old <code>(package-install 'gmail-message-mode)</code> will do. If you'd
like to install manually, see <a href="https://github.com/Malabarba/gmail-mode/#activation">the Readme</a>.
</p>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Configure your browser</h2>
<div class="outline-text-2" id="text-2">
<p>
Chrome and Firefox require an extra add-on for editing text-fields in
Emacs. Conkeror works out of the box.
</p>
<ol class="org-ol">
<li><b>Google-Chrome or Chromium</b> - <a href="http://www.emacswiki.org/emacs/Edit_with_Emacs">Edit with emacs</a>
</li>
<li><b>Firefox</b> - A <a href="https://github.com/oantolin/itsalltext/compare/patjak:master...master">very slightly modified</a> version of the <a href="https://github.com/oantolin/itsalltext">It's all text</a> add-on, patched by <a href="https://github.com/patjak">patjak</a> and <a href="https://github.com/oantolin">oantolin</a>.
</li>
<li><b>Conkeror</b> - <a href="http://conkeror.org/ConkerorSpawnHelper">Spawn Helper (built-in)</a>.
</li>
<li><b>Others</b> - Tried it in another browser? Let me know!
</li>
</ol>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3">Install a Markdown converter</h2>
<div class="outline-text-2" id="text-3">
<p>
You need an executable so that Emacs can convert the Markdown to HTML.
I personally recommend <a href="http://johnmacfarlane.net/pandoc/installing.html">Pandoc</a>. Many distros also have a <code>markdown</code>
package.
</p>
<p>
If you choose something else, see the <code>ham-mode-markdown-command</code> variable.</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/write-gmail-in-emacs-the-easy-way-gmail-message-mode.html?source=rss#disqus_thread">Comment on this.</a></p>Merging Github Pull Requests from Emacs2014-08-06T00:00:00+00:00https://endlessparentheses.com//merging-github-pull-requests-from-emacs<p>
Last week, Bin Chen shared his workflow for <a href="http://blog.binchen.org/posts/how-to-accept-the-github-pull-request-efficiently.html">merging Github pull
requests</a>. Other than the use of Firefox instead of Conkeror, it was
identical to mine. Now I gladly come to admit that <a href="https://github.com/alexander-yakushev">Alexander Yakushev</a>
has outsmarted us both by fixing up <a href="https://github.com/sigma/magit-gh-pulls">magit-gh-pulls-mode</a>, a package
originally written by <a href="https://github.com/sigma">Yann Hodique</a> which does it all from within
Magit.
</p>
<p>
You can find the keybinds on the <a href="https://github.com/sigma/magit-gh-pulls#usage">Readme</a> page.
</p>
<p>
It bothers me very slightly that it immediately queries for
pull-requests when I first call <code>magit-status</code>, even if I’m not
interested in pull-requests at the moment. So we change the suggested
setup in order to fix that.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">fboundp</span> <span class="ss">'magit-gh-pulls-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nv">eval-after-load</span> <span class="ss">'magit</span>
<span class="o">'</span><span class="p">(</span><span class="nv">define-key</span> <span class="nv">magit-mode-map</span> <span class="s">"#gg"</span>
<span class="nf">#'</span><span class="nv">endless/load-gh-pulls-mode</span><span class="p">))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/load-gh-pulls-mode</span> <span class="p">()</span>
<span class="s">"Start `magit-gh-pulls-mode' only after a manual request."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="nb">require</span> <span class="ss">'magit-gh-pulls</span><span class="p">)</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'magit-mode-hook</span> <span class="nf">#'</span><span class="nv">turn-on-magit-gh-pulls</span><span class="p">)</span>
<span class="p">(</span><span class="nv">magit-gh-pulls-mode</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="nv">magit-gh-pulls-reload</span><span class="p">)))</span></code></pre></figure>
<p>
With this setup it’ll only activate <code>magit-gh-pulls-mode</code> after you
try to update the list of pull-requests.
</p>
<p><a href="https://endlessparentheses.com/merging-github-pull-requests-from-emacs.html?source=rss#disqus_thread">Comment on this.</a></p>Faster Keystroke Echo2014-08-05T00:00:00+00:00https://endlessparentheses.com//faster-keystroke-echo<p>
My laptop is so old it is almost getting out-performed by my smartphone,
the principal consequence being that its keyboard has started to give
up on life. Occasionally, a key won't register when I hit it, and
occasionally I'll get paranoid and hit a key twice without knowing it
actually had registered the first time.
</p>
<p>
Emacs' keystroke echoing is a fantastic aid in this regard, informing
me whether the key worked or not when I'm typing a complex sequenced command. I only need to make it faster so
that I don't have to wait for it.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">echo-keystrokes</span> <span class="mf">0.1</span><span class="p">)</span></code></pre></figure>
<p><a href="https://endlessparentheses.com/faster-keystroke-echo.html?source=rss#disqus_thread">Comment on this.</a></p>Banishing the Shift Key with Key-Chord in Emacs2014-08-02T00:00:00+00:00https://endlessparentheses.com//banishing-the-shift-key-with-key-chord-in-emacs<p>
Take a minute now and be honest with yourself. Do you like the shift
key, or do you just put up with it? Perhaps it's just because my hands
are the size of basketballs, but I've always found
<kbd>Shift</kbd> hard to reach.
</p>
<p>
<a href="http://emacsrocks.com/e07.html">Key-chord</a> has allowed me to ban my shift use at least when it comes to
inputting symbols.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">key-chord-define-global</span> <span class="s">"0o"</span> <span class="s">")"</span><span class="p">)</span>
<span class="c1">;; Sadly, "1q" is impossible to hit on my keyboard.</span>
<span class="p">(</span><span class="nv">key-chord-define-global</span> <span class="s">"1q"</span> <span class="s">"!"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">key-chord-define-global</span> <span class="s">"2w"</span> <span class="s">"@"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">key-chord-define-global</span> <span class="s">"3e"</span> <span class="s">"#"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">key-chord-define-global</span> <span class="s">"4r"</span> <span class="s">"$"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">key-chord-define-global</span> <span class="s">"5t"</span> <span class="s">"%"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">key-chord-define-global</span> <span class="s">"6y"</span> <span class="s">"^"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">key-chord-define-global</span> <span class="s">"6t"</span> <span class="s">"^"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">key-chord-define-global</span> <span class="s">"7y"</span> <span class="s">"&"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">key-chord-define-global</span> <span class="s">"8u"</span> <span class="s">"*"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">key-chord-define-global</span> <span class="s">"9i"</span> <span class="s">"("</span><span class="p">)</span>
<span class="p">(</span><span class="nv">key-chord-define-global</span> <span class="s">"-p"</span> <span class="s">"_"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">key-chord-define</span> <span class="nv">emacs-lisp-mode-map</span>
<span class="s">"7y"</span> <span class="s">"&optional "</span><span class="p">)</span>
<span class="p">(</span><span class="nv">key-chord-mode</span> <span class="mi">1</span><span class="p">)</span></code></pre></figure>
<p>
Anyone know of a way to get rid of shift for capitalization?</p>
<p><a href="https://endlessparentheses.com/banishing-the-shift-key-with-key-chord-in-emacs.html?source=rss#disqus_thread">Comment on this.</a></p>Emacs narrow-or-widen-dwim2014-07-29T00:00:00+00:00https://endlessparentheses.com//emacs-narrow-or-widen-dwim<p>
Narrowing is one of those features you won’t even hear about in a more
mundane editor, but Emacs has an entire keymap for it. While I
wouldn’t want to be without this feature, I’m all for simplification.
</p>
<p>
<a href="https://github.com/mwfogleman">Michael Fogleman</a> (the same justicier who took matters to his own hands on <a href="/hungry-delete-mode.html">Hungry
Delete Mode</a>) mentioned the following gem on the <a href="/the-toggle-map-and-wizardry.html">The Toggle-Map and Wizardry</a>
post. I took the liberty of adding a bit of functionality. To use it, you’ll
also need the <a href="/fixing-org-in-src-block-p.html">endless/inside-org-code-block-p</a> function.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">narrow-or-widen-dwim</span> <span class="p">(</span><span class="nv">p</span><span class="p">)</span>
<span class="s">"Widen if buffer is narrowed, narrow-dwim otherwise.
Dwim means: region, org-src-block, org-subtree, or
defun, whichever applies first. Narrowing to
org-src-block actually calls `org-edit-src-code'.
With prefix P, don't widen, just narrow even if buffer
is already narrowed."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"P"</span><span class="p">)</span>
<span class="p">(</span><span class="k">declare</span> <span class="p">(</span><span class="nv">interactive-only</span><span class="p">))</span>
<span class="p">(</span><span class="nb">cond</span> <span class="p">((</span><span class="nb">and</span> <span class="p">(</span><span class="nv">buffer-narrowed-p</span><span class="p">)</span> <span class="p">(</span><span class="nb">not</span> <span class="nv">p</span><span class="p">))</span> <span class="p">(</span><span class="nv">widen</span><span class="p">))</span>
<span class="p">((</span><span class="nv">region-active-p</span><span class="p">)</span>
<span class="p">(</span><span class="nv">narrow-to-region</span> <span class="p">(</span><span class="nv">region-beginning</span><span class="p">)</span>
<span class="p">(</span><span class="nv">region-end</span><span class="p">)))</span>
<span class="p">((</span><span class="nv">derived-mode-p</span> <span class="ss">'org-mode</span><span class="p">)</span>
<span class="c1">;; `org-edit-src-code' is not a real narrowing</span>
<span class="c1">;; command. Remove this first conditional if</span>
<span class="c1">;; you don't want it.</span>
<span class="p">(</span><span class="nb">cond</span> <span class="p">((</span><span class="nb">ignore-errors</span> <span class="p">(</span><span class="nv">org-edit-src-code</span><span class="p">)</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="nv">delete-other-windows</span><span class="p">))</span>
<span class="p">((</span><span class="nb">ignore-errors</span> <span class="p">(</span><span class="nv">org-narrow-to-block</span><span class="p">)</span> <span class="no">t</span><span class="p">))</span>
<span class="p">(</span><span class="no">t</span> <span class="p">(</span><span class="nv">org-narrow-to-subtree</span><span class="p">))))</span>
<span class="p">((</span><span class="nv">derived-mode-p</span> <span class="ss">'latex-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nv">LaTeX-narrow-to-environment</span><span class="p">))</span>
<span class="p">(</span><span class="no">t</span> <span class="p">(</span><span class="nv">narrow-to-defun</span><span class="p">))))</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/toggle-map</span> <span class="s">"n"</span>
<span class="nf">#'</span><span class="nv">narrow-or-widen-dwim</span><span class="p">)</span>
<span class="c1">;; This line actually replaces Emacs' entire narrowing</span>
<span class="c1">;; keymap, that's how much I like this command. Only</span>
<span class="c1">;; copy it if that's what you want.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">ctl-x-map</span> <span class="s">"n"</span> <span class="nf">#'</span><span class="nv">narrow-or-widen-dwim</span><span class="p">)</span>
<span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'LaTeX-mode-hook</span>
<span class="p">(</span><span class="k">lambda</span> <span class="p">()</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">LaTeX-mode-map</span> <span class="s">"\C-xn"</span>
<span class="no">nil</span><span class="p">)))</span></code></pre></figure>
<p>
If you’re the kind of person who knows how to use <a href="https://doc.endlessparentheses.com/Fun/narrow-to-page"><code>narrow-to-page</code></a>,
this command might not be for you. Meanwhile, for us mortals, it more
than fits the bill.
</p>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1"><a id="ID-629511b6-fc25-4a8e-914d-939d5908a801"></a>Update <span class="timestamp-wrapper"><span class="timestamp">05 Sep 2014</span></span></h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Sacha Chua’s comment below gave me a glimpse of inspiration.
</p>
<p>
I’ve never liked org’s default keybind for editing a source code
block, <kbd>C-c '</kbd>, and have been looking for a better
one. But if you meditate on it for a minute, the effect of
<kbd>C-c '</kbd> is a narrow command with some bells
attached. So it fits perfectly into <code>narrow-or-widen-dwim</code> (updated
above).
</p>
<p>
Now that I’m no longer using <kbd>C-c '</kbd> to edit code
blocks, I also need a better key to finish editing code blocks, and
<kbd>C-x C-s</kbd> makes perfect sense.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">eval-after-load</span> <span class="ss">'org-src</span>
<span class="o">'</span><span class="p">(</span><span class="nv">define-key</span> <span class="nv">org-src-mode-map</span>
<span class="s">"\C-x\C-s"</span> <span class="nf">#'</span><span class="nv">org-edit-src-exit</span><span class="p">))</span></code></pre></figure>
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2"><a id="ID-3ca97e87-2b3e-4b1e-8550-205341585b65"></a>Update <span class="timestamp-wrapper"><span class="timestamp">23 Sep 2014</span></span></h2>
<div class="outline-text-2" id="text-orgheadline2">
<p>
I’ve updated it to no longer use the <a href="https://doc.endlessparentheses.com/Fun/org-in-src-block-p"><code>org-in-src-block-p</code></a> function and
just try calling <a href="https://doc.endlessparentheses.com/Fun/org-edit-src-code"><code>org-edit-src-code</code></a> instead. <code>org-in-src-block-p</code>
has proven quite unreliable.
</p>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-2">
<h2 id="orgheadline3">Update <span class="timestamp-wrapper"><span class="timestamp">05 Dec 2015</span></span></h2>
<div class="outline-text-2" id="text-orgheadline3">
<p>
Now it should also work with any type of org-block supported by
<a href="https://doc.endlessparentheses.com/Fun/org-narrow-to-block"><code>org-narrow-to-block</code></a>.
</p>
</div>
</div>
<div id="outline-container-orgheadline4" class="outline-2">
<h2 id="orgheadline4">Update <span class="timestamp-wrapper"><span class="timestamp">07 Dec 2015</span></span></h2>
<div class="outline-text-2" id="text-orgheadline4">
<p>
Use <code>LaTeX-mode-hook</code> instead of <code>eval-after-load</code>, as suggested by Omar in the
comments.</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/emacs-narrow-or-widen-dwim.html?source=rss#disqus_thread">Comment on this.</a></p>Hungry Delete Mode2014-07-26T00:00:00+00:00https://endlessparentheses.com//hungry-delete-mode<p>
<code>hungry-delete-mode</code> is what I like to call a <i>“free feature”</i> —it
asks nothing of you. In contrast, most other worthwhile features
charge you a price. The cheapest novelties charge some mental effort on
your part to fit into your workflow, the moderate ones ask for some space on
your ever-diminishing set of free keys, and a few narcisistic gems demand
that you adopt a completely new state of mind (paredit, I'm looking at you).
</p>
<p>
<code>hungry-delete-mode</code> doesn't steal a key and takes absolutely no
effort to master. It isn't big and flashy, but it is one of the
features I miss the <b>most</b> when I'm not in Emacs.
</p>
<p>
Plain and simple, it makes <kbd>backspace</kbd> and
<kbd>C-d</kbd> erase <i>all</i> consecutive white space in a given
direction (instead of just one). Use it everywhere.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">unless</span> <span class="p">(</span><span class="nb">fboundp</span> <span class="ss">'hungry-delete-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nv">package-install</span> <span class="ss">'hungry-delete</span><span class="p">))</span>
<span class="p">(</span><span class="nb">require</span> <span class="ss">'hungry-delete</span><span class="p">)</span>
<span class="p">(</span><span class="nv">global-hungry-delete-mode</span><span class="p">)</span></code></pre></figure>
<hr />
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Update</h2>
<div class="outline-text-2" id="text-1">
<p>
Comments have been made with regards to this breaking
<a href="https://doc.endlessparentheses.com/Fun/delete-selection-mode"><code>delete-selection-mode</code></a>. Well, while the weak complained, <a href="https://github.com/mwfogleman">Michael
Fogleman</a> took it upon himself to <a href="https://github.com/nflath/hungry-delete/commit/588c5633e63c7a777c284debcec50f32e98313c5">fix it</a> and make the world a better
place.
</p>
<p>
The update is already on Melpa.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/hungry-delete-mode.html?source=rss#disqus_thread">Comment on this.</a></p>Manually Choose a Fallback Font for Unicode2014-07-22T00:00:00+00:00https://endlessparentheses.com//manually-choose-a-fallback-font-for-unicode<p>
Jon Snader <a href="http://irreal.org/blog/?p=2828">over at Irreal</a> mentioned that Trey Harris shared on G+ how
to use a fallback font for Unicode Symbols. I won't repeat what they
said here, you've probably seen it already (and if you haven't then
go). I merely come to offer my own solution to this predicament, which
doesn't require any package installation and is pretty configurable to boot.
</p>
<p>
I ran into this a few months ago, while optimising my jabber+gtalk
setup, and the answer turns out to be remarkably simple. After
installing <i>Symbola</i> on your system, it's a 1-line solution.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">set-fontset-font</span> <span class="s">"fontset-default"</span> <span class="no">nil</span>
<span class="p">(</span><span class="nv">font-spec</span> <span class="ss">:size</span> <span class="mi">20</span> <span class="ss">:name</span> <span class="s">"Symbola"</span><span class="p">))</span></code></pre></figure>
<p>
Some remarks:
</p>
<ul class="org-ul">
<li>If you just glanced over that, go back and read the first
function's name. Now try to say it 3 times quickly.
</li>
<li>The <a href="https://doc.endlessparentheses.com/Fun/set-fontset-font">documentation</a> of <a href="https://doc.endlessparentheses.com/Fun/set-fontset-font"><code>set-fontset-font</code></a> doesn't even remotely allude
to the fact it can be used for setting a fallback fonts. Makes me
wonder its intended purpose.
</li>
<li>The <code>:size 20</code> argument (which you're free to remove) increases the
size of this font only. It's useful because some complex unicode
symbols are hard to see in small font.
</li>
<li>The <code>nil</code> would allow you to restrict the range of glyphs affected
by this font. Setting it to <code>nil</code> makes it the fallback font.
</li>
</ul>
<p><a href="https://endlessparentheses.com/manually-choose-a-fallback-font-for-unicode.html?source=rss#disqus_thread">Comment on this.</a></p>The Toggle-Map and Wizardry2014-07-19T00:00:00+00:00https://endlessparentheses.com//the-toggle-map-and-wizardry<p>
I love intuitive keymaps. Some are so perfect, you just can’t avoid
mouthing the words every time you hit that blissful combo. A wizard
murmuring an incantation under his breath as his fingers draw the
arcane patterns.
</p>
<p>
The following keymap toggles some options which tend to be useful
throughout a session.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-prefix-command</span> <span class="ss">'endless/toggle-map</span><span class="p">)</span>
<span class="c1">;; The manual recommends C-c for user keys, but C-x t is</span>
<span class="c1">;; always free, whereas C-c t is used by some modes.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">ctl-x-map</span> <span class="s">"t"</span> <span class="ss">'endless/toggle-map</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/toggle-map</span> <span class="s">"c"</span> <span class="nf">#'</span><span class="nv">column-number-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/toggle-map</span> <span class="s">"d"</span> <span class="nf">#'</span><span class="nv">toggle-debug-on-error</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/toggle-map</span> <span class="s">"e"</span> <span class="nf">#'</span><span class="nv">toggle-debug-on-error</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/toggle-map</span> <span class="s">"f"</span> <span class="nf">#'</span><span class="nv">auto-fill-mode</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/toggle-map</span> <span class="s">"l"</span> <span class="nf">#'</span><span class="nv">toggle-truncate-lines</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/toggle-map</span> <span class="s">"q"</span> <span class="nf">#'</span><span class="nv">toggle-debug-on-quit</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/toggle-map</span> <span class="s">"t"</span> <span class="nf">#'</span><span class="nv">endless/toggle-theme</span><span class="p">)</span>
<span class="c1">;;; Generalized version of `read-only-mode'.</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/toggle-map</span> <span class="s">"r"</span> <span class="nf">#'</span><span class="nv">dired-toggle-read-only</span><span class="p">)</span>
<span class="p">(</span><span class="nv">autoload</span> <span class="ss">'dired-toggle-read-only</span> <span class="s">"dired"</span> <span class="no">nil</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">endless/toggle-map</span> <span class="s">"w"</span> <span class="nf">#'</span><span class="nv">whitespace-mode</span><span class="p">)</span></code></pre></figure>
<p>
There are eight keys being defined there, most of which aren’t even
used every day, but I know I’ll never forget a single one. That is the
beauty of mnemonics.
</p>
<p>
Doesn’t <i>“Emacs, toggle column”</i> just roll off your tongue as you’re
typing <kbd>C-x t c</kbd>? I feel like I’m commanding the
strands of reality, but that could just be my D&D past taking the
better of me.
</p>
<p>
<i>Also note: The manual recommends <kbd>C-c</kbd> for user keys, but I like using <kbd>C-x</kbd> for global keys and using <kbd>C-c</kbd> for mode-specific keys.</i></p>
<p><a href="https://endlessparentheses.com/the-toggle-map-and-wizardry.html?source=rss#disqus_thread">Comment on this.</a></p>Inserting Documentation Quotes2014-07-15T00:00:00+00:00https://endlessparentheses.com//inserting-documentation-quotes<p>
Hopefully I'm not the only one who obsessively tries to abide by the
high codes of good practice and writes descriptive documentation on
every last <a href="http://malabarba.github.io/emacs-online-documentation/Fun/defun"><code>defun</code></a> or <a href="http://malabarba.github.io/emacs-online-documentation/Fun/defcustom"><code>defcustom</code></a>. If so, I can't be the only one who
has grown tired of typing ` then ' inside Emacs doc strings.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-key</span> <span class="nv">emacs-lisp-mode-map</span> <span class="s">"\C-cm"</span> <span class="s">"`'\C-b"</span><span class="p">)</span></code></pre></figure>
<p>
The <kbd>m</kbd> key feels natural to me here, because I also
use <kbd>C-c m</kbd> in LaTeX for quoting math, but you might
find some other key more intuitive.
</p>
<p><a href="https://endlessparentheses.com/inserting-documentation-quotes.html?source=rss#disqus_thread">Comment on this.</a></p>Meta Binds Part 2: A peeve with paragraphs2014-07-12T00:00:00+00:00https://endlessparentheses.com//meta-binds-part-2-a-peeve-with-paragraphs<p>
Emacs is an intelligent editor in more ways than one. Instead of
hardcoding its movement functions (such as <a href="http://doc.endlessparentheses.com/Fun/end-of-defun"><code>end-of-defun</code></a> or
<a href="http://doc.endlessparentheses.com/Fun/forward-paragraph"><code>forward-paragraph</code></a>), they're implemented in terms of delimiters which
are configured by each major-mode. It works beautifully for functions,
but it feels like a stone in my shoe for paragraphs.
</p>
<p>
Say what you will, but a paragraph delimiter is a blank line.
Whichever mode I'm in, that's where I expect to go when I hit
<kbd>M-e</kbd> and it annoys me to no end when some
major-mode changes that.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"M-a"</span><span class="p">)</span> <span class="ss">'endless/backward-paragraph</span><span class="p">)</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="p">(</span><span class="nv">kbd</span> <span class="s">"M-e"</span><span class="p">)</span> <span class="ss">'endless/forward-paragraph</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/forward-paragraph</span> <span class="p">(</span><span class="k">&optional</span> <span class="nv">n</span><span class="p">)</span>
<span class="s">"Advance just past next blank line."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"p"</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">para-commands</span>
<span class="o">'</span><span class="p">(</span><span class="nv">endless/forward-paragraph</span>
<span class="nv">endless/backward-paragraph</span><span class="p">)))</span>
<span class="c1">;; Only push mark if it's not active and we're not</span>
<span class="c1">;; repeating.</span>
<span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nv">use-region-p</span><span class="p">)</span>
<span class="p">(</span><span class="nb">not</span> <span class="p">(</span><span class="nb">member</span> <span class="nv">this-command</span> <span class="nv">para-commands</span><span class="p">))</span>
<span class="p">(</span><span class="nb">member</span> <span class="nv">last-command</span> <span class="nv">para-commands</span><span class="p">)</span>
<span class="p">(</span><span class="nv">push-mark</span><span class="p">))</span>
<span class="c1">;; The actual movement.</span>
<span class="p">(</span><span class="nb">dotimes</span> <span class="p">(</span><span class="nv">_</span> <span class="p">(</span><span class="nb">abs</span> <span class="nv">n</span><span class="p">))</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">></span> <span class="nv">n</span> <span class="mi">0</span><span class="p">)</span>
<span class="p">(</span><span class="nv">skip-chars-forward</span> <span class="s">"\n[:blank:]"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">skip-chars-backward</span> <span class="s">"\n[:blank:]"</span><span class="p">))</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">search-forward-regexp</span>
<span class="s">"\n[[:blank:]]*\n[[:blank:]]*"</span>
<span class="no">nil</span> <span class="no">t</span> <span class="p">(</span><span class="nv">cl-signum</span> <span class="nv">n</span><span class="p">))</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">match-end</span> <span class="mi">0</span><span class="p">))</span>
<span class="p">(</span><span class="nv">goto-char</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">></span> <span class="nv">n</span> <span class="mi">0</span><span class="p">)</span> <span class="p">(</span><span class="nv">point-max</span><span class="p">)</span> <span class="p">(</span><span class="nv">point-min</span><span class="p">)))))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/backward-paragraph</span> <span class="p">(</span><span class="k">&optional</span> <span class="nv">n</span><span class="p">)</span>
<span class="s">"Go back up to previous blank line."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"p"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">endless/forward-paragraph</span> <span class="p">(</span><span class="nb">-</span> <span class="nv">n</span><span class="p">)))</span></code></pre></figure>
<p>
By no means am I the first to be bothered by this. Both <a href="http://ergoemacs.org/emacs/emacs_move_by_paragraph.html">Xah Lee</a> and
<a href="http://whattheemacsd.com/setup-html-mode.el-01.html">Magnar Sveen</a>, for instance, also complain about this in <a href="http://doc.endlessparentheses.com/Fun/html-mode"><code>html-mode</code></a>
(I'm more bothered by <a href="http://doc.endlessparentheses.com/Fun/LaTeX-mode"><code>LaTeX-mode</code></a>). Had I the bravery to delve into the mailing lists, I'd no doubt
find posts on this that are older than me.
</p>
<p>
Does paragraph navigation ever get on <i>your</i> nerves?</p>
<p><a href="https://endlessparentheses.com/meta-binds-part-2-a-peeve-with-paragraphs.html?source=rss#disqus_thread">Comment on this.</a></p>New messages-buffer-mode in Emacs 24.42014-07-08T00:00:00+00:00https://endlessparentheses.com//new-messages-buffer-mode<p>
I've been using 24.4 for months now, yet only today I came to
realise the <code>*Messages*</code> buffer has been granted <a href="https://doc.endlessparentheses.com/Fun/messages-buffer-mode.html">its own major-mode</a>. The
practical difference is that the buffer is now read-only and has a
non-writing-oriented key-map.
</p>
<p>
God-only knows how many times I've fruitlessly hit <kbd>q</kbd> while trying to
quit the buffer, so the change is welcome.
</p>
<p><a href="https://endlessparentheses.com/new-messages-buffer-mode.html?source=rss#disqus_thread">Comment on this.</a></p>Emacs Documentation v1.3 Android App: Built-in Viewer and Theme Choice2014-07-06T00:00:00+00:00https://endlessparentheses.com//emacs-documentation-v1-3-android-app-built-in-viewer-and-theme-choice<p>
I've just released version 1.3 of the Emacs Documentation Android app.
You can
<ul class="org-ul">
<li>download it at the <a href="https://doc.endlessparentheses.com/app.html#main_content">Emacs Online Documentation</a> website,
</li>
<li>or find it at the <a href="https://play.google.com/store/apps/details?id=com.malabarba.emacsdocumentation">Play Store</a> (donate version).
</li>
</ul>
</p>
<p>
This version addresses a previous annoyance in which you'd have to
zoom-in every time you open a page (because the mobile pages aren't
quite mobile-friendly). This was addressed by implementing a
(optional) built-in viewer which remembers your zoom level.
</p>
<p>
It also implements theme choosing through the settings menu.
</p>
<p><a href="https://endlessparentheses.com/emacs-documentation-v1-3-android-app-built-in-viewer-and-theme-choice.html?source=rss#disqus_thread">Comment on this.</a></p>Ispell and Abbrev, the Perfect Auto-Correct2014-07-05T00:00:00+00:00https://endlessparentheses.com//ispell-and-abbrev-the-perfect-auto-correct<p>
I am not a fantastic typist. My speed is acceptable, but I make a great
deal of mistakes. The following snippet has turned me into the Messi
of keyboards.
</p>
<p>
Whenever I make a typo:
</p>
<ol class="org-ol">
<li>Hit <kbd>C-x C-i</kbd>, instead of erasing the mistake;</li>
<li>Select the appropriate correction (thanks to Ispell);</li>
<li>Sleep easier at night knowing I'll never see that mistake again (thanks to abbrev).</li>
</ol>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define-key</span> <span class="nv">ctl-x-map</span> <span class="s">"\C-i"</span>
<span class="nf">#'</span><span class="nv">endless/ispell-word-then-abbrev</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/simple-get-word</span> <span class="p">()</span>
<span class="p">(</span><span class="nv">car-safe</span> <span class="p">(</span><span class="nv">save-excursion</span> <span class="p">(</span><span class="nv">ispell-get-word</span> <span class="no">nil</span><span class="p">))))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/ispell-word-then-abbrev</span> <span class="p">(</span><span class="nv">p</span><span class="p">)</span>
<span class="s">"Call `ispell-word', then create an abbrev for it.
With prefix P, create local abbrev. Otherwise it will
be global.
If there's nothing wrong with the word at point, keep
looking for a typo until the beginning of buffer. You can
skip typos you don't want to fix with `SPC', and you can
abort completely with `C-g'."</span>
<span class="p">(</span><span class="nv">interactive</span> <span class="s">"P"</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">(</span><span class="nv">bef</span> <span class="nv">aft</span><span class="p">)</span>
<span class="p">(</span><span class="nv">save-excursion</span>
<span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="k">setq</span> <span class="nv">bef</span> <span class="p">(</span><span class="nv">endless/simple-get-word</span><span class="p">))</span>
<span class="c1">;; Word was corrected or used quit.</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">ispell-word</span> <span class="no">nil</span> <span class="ss">'quiet</span><span class="p">)</span>
<span class="no">nil</span> <span class="c1">; End the loop.</span>
<span class="c1">;; Also end if we reach `bob'.</span>
<span class="p">(</span><span class="nb">not</span> <span class="p">(</span><span class="nv">bobp</span><span class="p">)))</span>
<span class="c1">;; If there's no word at point, keep looking</span>
<span class="c1">;; until `bob'.</span>
<span class="p">(</span><span class="nb">not</span> <span class="p">(</span><span class="nv">bobp</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">backward-word</span><span class="p">)</span>
<span class="p">(</span><span class="nv">backward-char</span><span class="p">))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">aft</span> <span class="p">(</span><span class="nv">endless/simple-get-word</span><span class="p">)))</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">and</span> <span class="nv">aft</span> <span class="nv">bef</span> <span class="p">(</span><span class="nb">not</span> <span class="p">(</span><span class="nb">equal</span> <span class="nv">aft</span> <span class="nv">bef</span><span class="p">)))</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">aft</span> <span class="p">(</span><span class="nv">downcase</span> <span class="nv">aft</span><span class="p">))</span>
<span class="p">(</span><span class="nv">bef</span> <span class="p">(</span><span class="nv">downcase</span> <span class="nv">bef</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">define-abbrev</span>
<span class="p">(</span><span class="k">if</span> <span class="nv">p</span> <span class="nv">local-abbrev-table</span> <span class="nv">global-abbrev-table</span><span class="p">)</span>
<span class="nv">bef</span> <span class="nv">aft</span><span class="p">)</span>
<span class="p">(</span><span class="nv">message</span> <span class="s">"\"%s\" now expands to \"%s\" %sally"</span>
<span class="nv">bef</span> <span class="nv">aft</span> <span class="p">(</span><span class="k">if</span> <span class="nv">p</span> <span class="s">"loc"</span> <span class="s">"glob"</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">user-error</span> <span class="s">"No typo at or before point"</span><span class="p">))))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">save-abbrevs</span> <span class="ss">'silently</span><span class="p">)</span>
<span class="p">(</span><span class="nv">setq-default</span> <span class="nv">abbrev-mode</span> <span class="no">t</span><span class="p">)</span></code></pre></figure>
<p>
In my innocence, I had initially assigned this command to a
3-key sequence. It took me less then a month to realise it needed a
shorter bind, I'd been using it 30 times a day.
</p>
<p>
Auto correction with abbrev is <a href="http://www.emacswiki.org/emacs/autocorrection_abbrev_defs">far from a new concept</a>. Do you use
anything similar?
</p>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Update <span class="timestamp-wrapper"><span class="timestamp">11 Jan 2016</span></span></h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
The command now searches backward for the closest wrong word. So you can just
hit <kbd>C-x C-i</kbd> even if the mistake happened several words ago.
</p>
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">Update <span class="timestamp-wrapper"><span class="timestamp">20 Jan 2016</span></span></h2>
<div class="outline-text-2" id="text-orgheadline2">
<p>
Fixed some corner-case issues with the previous update.</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/ispell-and-abbrev-the-perfect-auto-correct.html?source=rss#disqus_thread">Comment on this.</a></p>Require Feature or Install Package2014-07-03T00:00:00+00:00https://endlessparentheses.com//require-feature-or-install-package<p>
A gentleman (or woman) by the name of baam_waak <a href="http://www.reddit.com/r/emacs/comments/29duxk/require_or_install_package/">recently asked</a> on
<a href="http://www.reddit.com/r/emacs/">/r/emacs</a> how can one make sure a package gets installed in case
<a href="http://malabarba.github.io/emacs-online-documentation/Fun/require"><code>require</code></a>ing it fails. Sadly, this predicament has no perfect
solution, for you require features, not packages. Fortunately, it has
plenty of solutions that are good enough, since features commonly
share the name of their package.
</p>
<p>
Motivated by the question, I saw it fit to add this
functionality to <a href="https://github.com/Malabarba/paradox/">paradox's arsenal</a>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">paradox-require</span> <span class="p">(</span><span class="nv">feature</span> <span class="k">&optional</span> <span class="nv">filename</span> <span class="nv">noerror</span> <span class="nc">package</span> <span class="nv">refresh</span><span class="p">)</span>
<span class="s">"A replacement for `require' which also installs the feature if it is absent.
- If FEATURE is present, `require' it and return t.
- If FEATURE is not present, install PACKAGE with `package-install'.
If PACKAGE is nil, assume FEATURE is the package name.
After installation, `require' FEATURE.
FILENAME is passed to `require'.
If NOERROR is non-nil, don't complain if the feature couldn't be
installed, just return nil.
By default, the current package database (stored in
`package-archive-contents') is only updated if it is empty.
Passing a non-nil REFRESH argument forces this update."</span>
<span class="p">(</span><span class="nb">or</span> <span class="p">(</span><span class="nb">require</span> <span class="nv">feature</span> <span class="nv">filename</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nc">package</span> <span class="p">(</span><span class="nb">or</span> <span class="nc">package</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">stringp</span> <span class="nv">feature</span><span class="p">)</span>
<span class="p">(</span><span class="nb">intern</span> <span class="nv">feature</span><span class="p">)</span>
<span class="nv">feature</span><span class="p">))))</span>
<span class="p">(</span><span class="nb">require</span> <span class="ss">'package</span><span class="p">)</span>
<span class="p">(</span><span class="nb">unless</span> <span class="p">(</span><span class="nb">and</span> <span class="nv">package-archive-contents</span> <span class="p">(</span><span class="nb">null</span> <span class="nv">refresh</span><span class="p">))</span>
<span class="p">(</span><span class="nv">package-refresh-contents</span><span class="p">))</span>
<span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nv">condition-case</span> <span class="nv">e</span>
<span class="p">(</span><span class="nv">package-install</span> <span class="nc">package</span><span class="p">)</span>
<span class="p">(</span><span class="nb">error</span> <span class="p">(</span><span class="k">if</span> <span class="nv">noerror</span> <span class="no">nil</span> <span class="p">(</span><span class="nb">error</span> <span class="p">(</span><span class="nb">cadr</span> <span class="nv">e</span><span class="p">)))))</span>
<span class="p">(</span><span class="nb">require</span> <span class="nv">feature</span> <span class="nv">filename</span> <span class="nv">noerror</span><span class="p">)))))</span></code></pre></figure>
<p>
Just use <code>(paradox-require 'dash)</code> instead of <code>(require 'dash)</code> and
the package will be installed if necessary.</p>
<p><a href="https://endlessparentheses.com/require-feature-or-install-package.html?source=rss#disqus_thread">Comment on this.</a></p>Ido Bury Buffer2014-06-30T00:00:00+00:00https://endlessparentheses.com//Ido-Bury-Buffer<p>
A lesson for the less-informed: while using ido to switch buffers, you
can kill buffers with <kbd>C-k</kbd>.
</p>
<p>
For some reason, though, you can't bury them. <kbd>C-b</kbd>
seems like an obvious choice, but find your personal preference.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-hook</span>
<span class="ss">'ido-setup-hook</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/define-ido-bury-key</span> <span class="p">()</span>
<span class="p">(</span><span class="nv">define-key</span> <span class="nv">ido-completion-map</span>
<span class="p">(</span><span class="nv">kbd</span> <span class="s">"C-b"</span><span class="p">)</span> <span class="ss">'endless/ido-bury-buffer-at-head</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">endless/ido-bury-buffer-at-head</span> <span class="p">()</span>
<span class="s">"Bury the buffer at the head of `ido-matches'."</span>
<span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">enable-recursive-minibuffers</span> <span class="no">t</span><span class="p">)</span>
<span class="p">(</span><span class="nv">buf</span> <span class="p">(</span><span class="nv">ido-name</span> <span class="p">(</span><span class="nb">car</span> <span class="nv">ido-matches</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">nextbuf</span> <span class="p">(</span><span class="nb">cadr</span> <span class="nv">ido-matches</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">get-buffer</span> <span class="nv">buf</span><span class="p">)</span>
<span class="c1">;; If next match names a buffer use the buffer object;</span>
<span class="c1">;; buffer name may be changed by packages such as</span>
<span class="c1">;; uniquify.</span>
<span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">and</span> <span class="nv">nextbuf</span> <span class="p">(</span><span class="nv">get-buffer</span> <span class="nv">nextbuf</span><span class="p">))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">nextbuf</span> <span class="p">(</span><span class="nv">get-buffer</span> <span class="nv">nextbuf</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">bury-buffer</span> <span class="nv">buf</span><span class="p">)</span>
<span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">bufferp</span> <span class="nv">nextbuf</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">nextbuf</span> <span class="p">(</span><span class="nv">buffer-name</span> <span class="nv">nextbuf</span><span class="p">)))</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">ido-default-item</span> <span class="nv">nextbuf</span>
<span class="nv">ido-text-init</span> <span class="nv">ido-text</span>
<span class="nv">ido-exit</span> <span class="ss">'refresh</span><span class="p">)</span>
<span class="p">(</span><span class="nv">exit-minibuffer</span><span class="p">))))</span></code></pre></figure>
<p>
Don't be overly impressed by the apparent robustness of the code,
it's merely an adaptation of <a href="http://malabarba.github.io/emacs-online-documentation/Fun/ido-kill-buffer-at-head"><code>ido-kill-buffer-at-head</code></a>.
</p>
<p><a href="https://endlessparentheses.com/Ido-Bury-Buffer.html?source=rss#disqus_thread">Comment on this.</a></p>Meta Binds Part 1: Drunk in the Dark2014-06-29T00:00:00+00:00https://endlessparentheses.com//Meta-Binds-Part-1%3A-Drunk-in-the-Dark<p>
Learning numeric prefixes is a vital step in the road to Emacs
mastery, and it's probably one of the editor's least appreciated
features. It took me more than a year to get the hang of doing
<kbd>C-3 C-k</kbd> instead of <kbd>C-a C-space C-n C-n C-n C-w</kbd>.
</p>
<p>
Still, having three different ways of invoking this marvel is
one-too-many for my taste, specially when they take up premium
keyboard space. <kbd>M-9</kbd> isn't just a nice key, it's a
key I can hit while drunk in the dark and wearing boxing gloves —it
deserves an equally important command.
</p>
<p>
Coincidentally, <a href="https://doc.endlessparentheses.com/Fun/backward-sexp"><code>backward-sexp</code></a> and <a href="https://doc.endlessparentheses.com/Fun/forward-sexp"><code>forward-sexp</code></a> are divine commands
with abhorrent default keybinds.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-set-key</span> <span class="s">"\M-9"</span> <span class="ss">'backward-sexp</span><span class="p">)</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="s">"\M-0"</span> <span class="ss">'forward-sexp</span><span class="p">)</span>
<span class="p">(</span><span class="nv">global-set-key</span> <span class="s">"\M-1"</span> <span class="ss">'delete-other-windows</span><span class="p">)</span></code></pre></figure>
<p>
<kbd>C-x 1</kbd> for <a href="https://doc.endlessparentheses.com/Fun/delete-other-windows"><code>delete-other-windows</code></a> isn't quite
abhorrent, but it also deserves better.</p>
<p><a href="https://endlessparentheses.com/Meta-Binds-Part-1-3A-Drunk-in-the-Dark.html?source=rss#disqus_thread">Comment on this.</a></p>Checkdoc, Package Developing, and Cakes2014-06-28T00:00:00+00:00https://endlessparentheses.com//Checkdoc-2C-Package-Developing-2C-and-Cakes<p>
You're not a true Emacs package developer until you run
<a href="https://doc.endlessparentheses.com/Fun/checkdoc"><code>checkdoc</code></a> through your packages. You'll be amazed at the
number of style errors it finds. Unfortunately, not enough
adepts are aware of this tool, perhaps because it's more of
an icing spatula than a carving knife.
</p>
<p>
Just visit your package file and run <kbd>M-x</kbd> <code>checkdoc</code>.
</p>
<p>
It runs through the buffer and pinpoints style and spelling
errors in your docstrings and comments. It won't change the
taste of your chocolate cake, but it will give the icing
that lovely silky smoothness.
</p>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Update <span class="timestamp-wrapper"><span class="timestamp">29 Jun 2014</span></span></h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
As Grant Retke and lunaryorn were kind to point out in the comments,
<a href="http://www.wisdomandwonder.com/?p=8795">Flycheck does checkdoc</a> for you.
</p>
</div>
</div>
<p><a href="https://endlessparentheses.com/Checkdoc-2C-Package-Developing-2C-and-Cakes.html?source=rss#disqus_thread">Comment on this.</a></p>init.org Without org-mode2014-06-26T00:00:00+00:00https://endlessparentheses.com//init-org-Without-org-mode<p>
When I decided to share my limited wisdom, I realized I’d
need something better than a 4-year-old, thrown together,
init file. The words <i>“init dot org”</i> had crossed my ears
through one of <a href="http://sachachua.com/blog/emacs-chat/">Sacha Chua’s previous videos</a>; however, due to
63m of water above my head, internet searches weren’t quite
operating at top efficiency.
</p>
<p>
Without a second thought, and armed with nothing but its
name (init.org), I took to writing —I thought at the
moment— the simplest way of implementing such a feature.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="c1">;;; init.el</span>
<span class="p">(</span><span class="nb">defvar</span> <span class="nv">endless/init.org-message-depth</span> <span class="mi">3</span>
<span class="s">"What depth of init.org headers to message at startup."</span><span class="p">)</span>
<span class="p">(</span><span class="nv">with-temp-buffer</span>
<span class="p">(</span><span class="nv">insert-file</span> <span class="s">"~/.emacs.d/init.org"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">point-min</span><span class="p">))</span>
<span class="p">(</span><span class="nv">search-forward</span> <span class="s">"\n* init.el"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nb">not</span> <span class="p">(</span><span class="nv">eobp</span><span class="p">))</span>
<span class="p">(</span><span class="nv">forward-line</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">(</span><span class="nb">cond</span>
<span class="c1">;; Report Headers</span>
<span class="p">((</span><span class="nv">looking-at</span>
<span class="p">(</span><span class="nb">format</span> <span class="s">"\\*\\{2,%s\\} +.*$"</span>
<span class="nv">endless/init.org-message-depth</span><span class="p">))</span>
<span class="p">(</span><span class="nv">message</span> <span class="s">"%s"</span> <span class="p">(</span><span class="nv">match-string</span> <span class="mi">0</span><span class="p">)))</span>
<span class="c1">;; Evaluate Code Blocks</span>
<span class="p">((</span><span class="nv">looking-at</span> <span class="s">"^#\\+BEGIN_SRC +emacs-lisp *$"</span><span class="p">)</span>
<span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">l</span> <span class="p">(</span><span class="nv">match-end</span> <span class="mi">0</span><span class="p">)))</span>
<span class="p">(</span><span class="nv">search-forward</span> <span class="s">"\n#+END_SRC"</span><span class="p">)</span>
<span class="p">(</span><span class="nv">eval-region</span> <span class="nv">l</span> <span class="p">(</span><span class="nv">match-beginning</span> <span class="mi">0</span><span class="p">))))</span>
<span class="c1">;; Finish on the next level-1 header</span>
<span class="p">((</span><span class="nv">looking-at</span> <span class="s">"^\\* "</span><span class="p">)</span>
<span class="p">(</span><span class="nv">goto-char</span> <span class="p">(</span><span class="nv">point-max</span><span class="p">))))))</span></code></pre></figure>
<p>
Once back within the range of cell towers, a quick search
revealed it could have been <a href="https://github.com/seth/my-emacs-dot-d/blob/master/emacs-init.org">slightly shorter</a>.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">require</span> <span class="ss">'org</span><span class="p">)</span>
<span class="p">(</span><span class="nv">org-babel-load-file</span>
<span class="p">(</span><span class="nv">expand-file-name</span> <span class="s">"emacs-init.org"</span>
<span class="nv">user-emacs-directory</span><span class="p">))</span></code></pre></figure>
<p>
Nonetheless, I stuck with my guns and kept the first version.
Primarily for stubbornness, but also a few other reasons:
</p>
<dl class="org-dl">
<dt> Fine grained control </dt><dd>I can choose exactly what gets
evaluated. In this case, anything inside the
<code>init.el</code> header. This is important because this blog
<b>is</b> my init file, and I don’t want to evaluate everything
I post.
</dd>
<dt> Lots of messaging </dt><dd>It calls <a href="https://doc.endlessparentheses.com/Fun/message"><code>message</code></a> on each header it
finds (up to a configurable level). Whenever something goes
wrong, forget about restarting with <code>--debug-init</code>, the
messages buffer tells me exactly where it happened.
</dd>
<dt> It doesn’t <code>(require 'org)</code> </dt><dd>This might sound silly, but
that inconspicuous line forms one of the most
time-consuming statements you could possibly write. I’d
challenge anyone to find a 14-character statement that
takes longer than that (other than an empty loop or a
sleep command, of course).
</dd>
</dl>
<p><a href="https://endlessparentheses.com/init-org-Without-org-mode.html?source=rss#disqus_thread">Comment on this.</a></p>