<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Nicolas Martyanoff – Brain dump</title>
    <description>Brain dump</description>
    <language>en-us</language>
    <managingEditor>nicolas@n16f.net (Nicolas Martyanoff)</managingEditor>
    <webMaster>nicolas@n16f.net (Nicolas Martyanoff)</webMaster>
    <link>https://www.n16f.net/tags/emacs/index.xml</link>
    <atom:link href="https://www.n16f.net/tags/emacs/index.xml" rel="self" type="application/rss+xml" />
    <lastBuildDate>Sun, 11 Aug 2024 00:00:00 +0000</lastBuildDate>

    
    <item xml:base="https://www.n16f.net/blog/controlling-link-opening-in-emacs/">
      <title>Controlling link opening in Emacs</title>
      <link>https://www.n16f.net/blog/controlling-link-opening-in-emacs/</link>
      <pubDate>Sun, 11 Aug 2024 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/controlling-link-opening-in-emacs/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;Multiple Emacs modes recognize URIs so that you can follow them in a web
browser, usually with a mouse left click. In modes that do not support it, one
can still call &lt;code&gt;browse-url-at-point&lt;/code&gt; interactively with the cursor positioned on
a URI.&lt;/p&gt;
&lt;p&gt;I wanted to make link handling more convenient for some time; this is what I
ended up with.&lt;/p&gt;
&lt;h2 id=&#34;key-bindings&#34;&gt;Key bindings&lt;/h2&gt;
&lt;p&gt;There is no global Emacs concept of following links, so modes deal with them
differently. I wanted to use the same key binding everywhere; I went for &lt;code&gt;C-o&lt;/code&gt;
(&amp;ldquo;o&amp;rdquo; for &amp;ldquo;open&amp;rdquo;) since I never use &lt;code&gt;open-line&lt;/code&gt;. I also wanted the ability to use
&lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_mono/eww.html&#34;&gt;Eww&lt;/a&gt; —the Emacs
builtin web browser— when it is convenient. So I added a function to select the
secondary browser when the prefix is set. This way &lt;code&gt;C-o&lt;/code&gt; opens Firefox, and &lt;code&gt;C-u C-o&lt;/code&gt; opens Eww.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(setq browse-url-secondary-browser-function &#39;eww-browse-url)

(defun g-browse-url-at-point (arg)
  (interactive &amp;quot;P&amp;quot;)
  (let ((browse-url-browser-function
         (if arg
             browse-url-secondary-browser-function
           browse-url-browser-function)))
    (browse-url-at-point)))

(keymap-global-set &amp;quot;C-o&amp;quot; &#39;g-browse-url-at-point)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ultimately the function calls &lt;code&gt;browse-url-at-point&lt;/code&gt; which identifies the URI
under the cursor (i.e. at the current point) and calls &lt;code&gt;browse-url&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;selecting-a-web-browser&#34;&gt;Selecting a web browser&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;browse-url&lt;/code&gt; function is fully configurable; two mechanisms are used to
select a web browser for each link:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the &lt;code&gt;browse-url-handlers&lt;/code&gt; and &lt;code&gt;browse-url-default-handlers&lt;/code&gt; association lists,
used to match specific URIs using predicates or regular expression;&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;browse-url-browser-function&lt;/code&gt; variable (by default
&lt;code&gt;browser-url-default-browser&lt;/code&gt;) referencing a function which looks for
available web browsers on the current machine.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Since I use multiple &lt;a href=&#34;https://support.mozilla.org/en-US/kb/profile-manager-create-remove-switch-firefox-profiles&#34;&gt;Firefox
profiles&lt;/a&gt;,
I needed the ability to open URIs in the right profile depending of the
context. For example links in a professional email read in Gnus should be open
in my work context.&lt;/p&gt;
&lt;p&gt;The following function returns a handler that can be used in
&lt;code&gt;browse-url-handlers&lt;/code&gt; or as value for &lt;code&gt;browse-url-browser-function&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-browse-url-firefox-function (profile)
  (lambda (uri &amp;amp;rest args)
    (let ((process-environment (browse-url-process-environment))
          (process-name (concat &amp;quot;firefox &amp;quot; uri))
          (process-args `(&amp;quot;--new-tab&amp;quot; ,uri
                          ,@(if profile (list &amp;quot;-P&amp;quot; profile)))))
      (apply #&#39;start-process process-name nil &amp;quot;firefox&amp;quot; process-args))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example, &lt;code&gt;(g-browse-url-firefox-function &amp;quot;test&amp;quot;)&lt;/code&gt; returns a browse function
that will open the link in a new tab of a Firefox window associated with the
&amp;ldquo;test&amp;rdquo; profile.&lt;/p&gt;
&lt;p&gt;To change the logic deciding how to handle links in a buffer with a specific
major mode, all you have to do is to add a hook to set
&lt;code&gt;browse-url-browser-function&lt;/code&gt; in a way that only affects the current buffer.
Since this variable is not
&lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_node/elisp/Buffer_002dLocal-Variables.html&#34;&gt;buffer-local&lt;/a&gt;
by default, you have to take care to use &lt;code&gt;setq-local&lt;/code&gt; when changing it.&lt;/p&gt;
&lt;h2 id=&#34;links-in-gnus-messages&#34;&gt;Links in Gnus messages&lt;/h2&gt;
&lt;p&gt;Gnus messages are displayed using the &lt;code&gt;gnus-article-mode&lt;/code&gt; major mode.&lt;/p&gt;
&lt;p&gt;We first write a function to identify the account associated with the current
group (assuming you are using &lt;code&gt;nnimap&lt;/code&gt; for your email accounts as I do):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-current-gnus-account ()
  (let ((group gnus-newsgroup-name))
    (when (string-match &amp;quot;^nnimap\\+\\([^:]+\\):&amp;quot; group)
      (match-string 1 group))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we define the actual browse function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-gnus-browse-url (url &amp;amp;rest args)
  (let ((profile
         (pcase (g-current-gnus-account)
           (&amp;quot;my-company&amp;quot;
            &amp;quot;my-company-profile&amp;quot;)
           (&amp;quot;a-client&amp;quot;
            &amp;quot;the-client-profile&amp;quot;)
           (_
            &amp;quot;default&amp;quot;))))
    (apply (g-browse-url-firefox-function profile) url args)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And the hook function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-gnus-set-browser-function ()
  (setq-local browse-url-browser-function &#39;g-gnus-browse-url))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally we update the hook:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(use-package gnus-art
  :hook
  ((gnus-article-mode-hook . g-gnus-set-browser-function)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Links in emails are now open in the right Firefox profile!&lt;/p&gt;
&lt;h2 id=&#34;links-in-the-circe-irc-client&#34;&gt;Links in the Circe IRC client&lt;/h2&gt;
&lt;p&gt;Same idea for Circe, select the right Firefox profile based on the
network and channel of the current channel buffer.&lt;/p&gt;
&lt;p&gt;We define the function to be called during the initialization of each channel
buffer:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-circe-set-browser-function ()
  (let* ((network (with-circe-server-buffer
                    circe-network))
         (channel circe-chat-target)
         (profile (cond
                   ((and (equal network &amp;quot;my-network&amp;quot;)
                         (equal channel &amp;quot;#my-channel&amp;quot;))
                    &amp;quot;the-right-profile&amp;quot;)
                   (t
                    &amp;quot;default&amp;quot;))))
    (setq-local browse-url-browser-function
                (g-browse-url-firefox-function profile))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And add it as a hook:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(use-package circe
  :hook
  ((circe-channel-mode-hook . g-circe-set-browser-function))
&lt;/code&gt;&lt;/pre&gt;
</description>

      
      <category>emacs</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/making-ielm-more-comfortable/">
      <title>Making IELM More Comfortable</title>
      <link>https://www.n16f.net/blog/making-ielm-more-comfortable/</link>
      <pubDate>Sat, 08 Apr 2023 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/making-ielm-more-comfortable/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;IELM is the Emacs Lisp REPL. It lets you evaluate any Elisp expression, prints
the output and keeps track of previous commands. While it is a serious step-up
from the humble &lt;code&gt;eval-expression&lt;/code&gt;, it can be uncomfortable on several aspects.
Let us improve it.&lt;/p&gt;
&lt;h2 id=&#34;adding-eldoc-hints&#34;&gt;Adding Eldoc hints&lt;/h2&gt;
&lt;p&gt;Eldoc is a generic module used to display realtime documentation hints while
you type. For Emacs Lisp, it means displaying docstrings when your
cursor is over a symbol or when you start writing a function call.&lt;/p&gt;
&lt;p&gt;When calling a function, you often need a quick remainder about the arguments.
Having Eldoc means you do not have to call &lt;code&gt;C-h f&lt;/code&gt; and deal with the
documentation buffer, and can just glance at the echo area.&lt;/p&gt;
&lt;p&gt;To enable Eldoc in IELM, let us add it to the initialization hook:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(add-hook &#39;ielm-mode-hook &#39;eldoc-mode)
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&#34;using-paredit&#34;&gt;Using Paredit&lt;/h2&gt;
&lt;p&gt;&lt;a href=&#34;https://paredit.org/&#34;&gt;Paredit&lt;/a&gt; is an essential minor mode that lets you
manipulate Lisp code as expressions and not as simple text. If you are not
using it and think that editing all these parentheses is annoying, you are
missing out. Give it a try, you will not be disappointed.&lt;/p&gt;
&lt;p&gt;To use Paredit in IELM, we add it to the initialization hook:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(add-hook &#39;ielm-mode-hook &#39;paredit-mode)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Infortunately there is a key conflict between Paredit and IELM. Paredit
overrides &lt;code&gt;return&lt;/code&gt; to execute &lt;code&gt;paredit-RET&lt;/code&gt;, meaning that the original
&lt;code&gt;ielm-return&lt;/code&gt; is not called.&lt;/p&gt;
&lt;p&gt;The simplest way to fix it is to alter the Paredit keymap:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(define-key paredit-mode-map (kbd &amp;quot;RET&amp;quot;) nil)
(define-key paredit-mode-map (kbd &amp;quot;C-j&amp;quot;) &#39;paredit-newline)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We remove the entry associated with &lt;code&gt;return&lt;/code&gt; and use &lt;code&gt;C-j&lt;/code&gt; to insert a newline
character, useful to write a multiline expression.&lt;/p&gt;
&lt;h2 id=&#34;making-the-command-history-persistent&#34;&gt;Making the command history persistent&lt;/h2&gt;
&lt;p&gt;The most annoying part of IELM is that it does not have persistent history. I
expect any interactive command system to store past entries since they can
always be useful again. ZSH has persistence, so does the
&lt;a href=&#34;https://slime.common-lisp.dev/&#34;&gt;SLIME&lt;/a&gt; Common Lisp REPL. IELM does not, even
though Comint (the mode IELM derives from) can handle it.&lt;/p&gt;
&lt;p&gt;Let us add it. First we will write a function to configure Comint and load any
entry stored in the history file if it exists:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-ielm-init-history ()
  (let ((path (expand-file-name &amp;quot;ielm/history&amp;quot; user-emacs-directory)))
    (make-directory (file-name-directory path) t)
    (setq-local comint-input-ring-file-name path))
  (setq-local comint-input-ring-size 10000)
  (setq-local comint-input-ignoredups t)
  (comint-read-input-ring))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We store the history in the &lt;code&gt;ielm/history&lt;/code&gt; file in the user Emacs directory,
mimicking the &lt;code&gt;eshell/history&lt;/code&gt; file used by Eshell.&lt;/p&gt;
&lt;p&gt;Nothing complicated: we increase the number of entries stored in the history
file, and tell Comint to drop duplicate entries. We also are careful and use
&lt;code&gt;setq-local&lt;/code&gt; to make sure Comint settings are only modified for the current
buffer and not globally, since other buffers using different Comint-based
modes could have different requirements.&lt;/p&gt;
&lt;p&gt;We want to call this function when IELM start:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(add-hook &#39;ielm-mode-hook &#39;g-ielm-init-history)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Reading the history is one thing. We need a way to add all expressions we
evaluate to it. It would have been nice to have a hook called everytime an
expression is entered, but we have to do without it. We are going to use Elisp
&lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_node/elisp/Advising-Functions.html&#34;&gt;advices&lt;/a&gt;
to evaluate code each time a specific function is called. We want to write
the history file when &lt;code&gt;ielm-send-input&lt;/code&gt; returns:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defun g-ielm-write-history (&amp;amp;rest _args)
  (with-file-modes #o600
    (comint-write-input-ring)))
  
(advice-add &#39;ielm-send-input :after &#39;g-ielm-write-history)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Advising functions are called with the same arguments as the advised function.
We do not care about them, so we use the &lt;code&gt;&amp;amp;rest&lt;/code&gt; keyword to match all
arguments passed to &lt;code&gt;g-ielm-write-history&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We are careful to use &lt;code&gt;with-file-modes&lt;/code&gt; to make sure the file is always
created as only readable by the user, since it may contain private
information.&lt;/p&gt;
&lt;p&gt;This was not easy, but persistent history is worth it!&lt;/p&gt;
&lt;h2 id=&#34;useful-key-bindings&#34;&gt;Useful key bindings&lt;/h2&gt;
&lt;p&gt;Finally we will bind two key combinations.&lt;/p&gt;
&lt;p&gt;First the very common &lt;code&gt;C-l&lt;/code&gt; to clear the buffer. All modes deriving from
Comint have &lt;code&gt;C-c M-o&lt;/code&gt;, but it is awkward to type and &lt;code&gt;C-l&lt;/code&gt; is very common in
various applications.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(define-key inferior-emacs-lisp-mode-map (kbd &amp;quot;C-l&amp;quot;)
            &#39;comint-clear-buffer)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we want a way to search through old expressions. Comint has
&lt;code&gt;comint-history-isearch-backward-regexp&lt;/code&gt; which is incredibly primitive. We can
get a far better experience with &lt;a href=&#34;https://github.com/emacs-helm/helm&#34;&gt;Helm&lt;/a&gt;.
The &lt;code&gt;helm-comint-input-ring&lt;/code&gt; function lets us browse among old expressions and
select one through incremental search. We bind it to &lt;code&gt;C-r&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(define-key inferior-emacs-lisp-mode-map (kbd &amp;quot;C-r&amp;quot;)
            &#39;helm-comint-input-ring)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;IELM is so much more comfortable to use now!&lt;/p&gt;
</description>

      
      <category>emacs</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/custom-font-lock-configuration-in-emacs/">
      <title>Custom Font Lock configuration in Emacs</title>
      <link>https://www.n16f.net/blog/custom-font-lock-configuration-in-emacs/</link>
      <pubDate>Fri, 24 Feb 2023 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/custom-font-lock-configuration-in-emacs/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;&lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_node/elisp/Font-Lock-Mode.html&#34;&gt;Font
Lock&lt;/a&gt;
is the builtin Emacs minor mode used to highlight textual elements in buffers.
Major modes usually configure it to detect various syntaxic constructions and
attach
&lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_node/emacs/Faces.html&#34;&gt;faces&lt;/a&gt;
to them.&lt;/p&gt;
&lt;p&gt;The reason I ended up deep into Font Lock is because I was not satisfied with
the way it is configured for &lt;code&gt;lisp-mode&lt;/code&gt;, the major mode used for both Common
Lisp and Emacs Lisp code. This forced me to get acquainted with various
aspects of Font Lock in order to change its configuration. If you want to
change highlighting for your favourite major mode, you will find this article
useful.&lt;/p&gt;
&lt;h2 id=&#34;common-lisp-highlighting-done-wrong&#34;&gt;Common Lisp highlighting done wrong&lt;/h2&gt;
&lt;p&gt;The core issue of Common Lisp highlighting in Emacs is that a lot of it is
arbitrary and inconsistent:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The mode highlights what it calls &amp;ldquo;definers&amp;rdquo; and &amp;ldquo;keywords&amp;rdquo;, but it does not
really make sense in Common Lisp. Why would &lt;code&gt;WITH-OUTPUT-TO-STRING&lt;/code&gt; be
listed as a keyword, but not &lt;code&gt;CLASS-OF&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SIGNAL&lt;/code&gt; uses &lt;code&gt;font-lock-warning-face&lt;/code&gt;. Why would it be a warning? Even
stranger, why would you use this warning face for &lt;code&gt;CHECK-TYPE&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;Keywords and uninterned symbols are all highlighted with
&lt;code&gt;font-lock-builtin-face&lt;/code&gt;. But they are not functions or variables. They are
not even special in any way, and their syntax already indicates clearly
their nature. Having so many yellow symbols everywhere is really
distracting.&lt;/li&gt;
&lt;li&gt;All symbols starting with &lt;code&gt;&amp;amp;&lt;/code&gt; are highlighted using &lt;code&gt;font-lock-type-face&lt;/code&gt;.
But lambda list arguments are not types, and symbols starting with &lt;code&gt;&amp;amp;&lt;/code&gt; are
not always lambda list arguments.&lt;/li&gt;
&lt;li&gt;All symbols preceded by &lt;code&gt;(&lt;/code&gt; whose name starts with &lt;code&gt;DO-&lt;/code&gt; or &lt;code&gt;WITH-&lt;/code&gt; are
highlighted as keywords. There is even a comment by RMS stating that it is
too general. He is right.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Beyond these issues, the mode sadly uses default Font Lock faces instead of
defining semantically appropriate faces and mapping them to existing ones as
default values.&lt;/p&gt;
&lt;p&gt;The chances of successfully driving this kind of large and disruptive change
directly into Emacs are incredibly low. Even if it was to be accepted, the
result would not be available until the next release, which could mean months.
Fortunately, Emacs is incredibly flexible and we can change all of this
ourselves.&lt;/p&gt;
&lt;p&gt;Note that you may not agree with the list of issues above, and this is fine.
The point of this article is to show you how you can change the way Emacs
highlights content in order to match your preferences. And you can do that for
all major modes!&lt;/p&gt;
&lt;h2 id=&#34;font-lock-configuration&#34;&gt;Font Lock configuration&lt;/h2&gt;
&lt;p&gt;Font Lock always felt a bit magic and it took me some time to find the
motivation to read the documentation. As is turned out, it can be used for
very complex highlighting schemes, but basic features are not that hard to
use.&lt;/p&gt;
&lt;p&gt;The main configuration of Font Lock is stored in the &lt;code&gt;font-lock-defaults&lt;/code&gt;
buffer-local variable. It is a simple list containing the following entries:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A list of symbols containing the value to use for &lt;code&gt;font-lock-keywords&lt;/code&gt; at
each
&lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_node/elisp/Levels-of-Font-Lock.html&#34;&gt;level&lt;/a&gt;,
the first symbol being the default value.&lt;/li&gt;
&lt;li&gt;The value used for &lt;code&gt;font-lock-keywords-only&lt;/code&gt;. If it is &lt;code&gt;nil&lt;/code&gt;, it enables
syntaxic highlighting (strings and comments) in addition of search-based
(keywords) highlighting.&lt;/li&gt;
&lt;li&gt;The value used for &lt;code&gt;font-lock-keywords-case-fold-search&lt;/code&gt;. If true,
highlighting is case insensitive.&lt;/li&gt;
&lt;li&gt;The value used for &lt;code&gt;font-lock-syntax-table&lt;/code&gt;, the association list
controlling syntaxic highlighting. If it is &lt;code&gt;nil&lt;/code&gt;, Font Lock uses the syntax
table configured with &lt;code&gt;set-syntax-table&lt;/code&gt;. In &lt;code&gt;lisp-mode&lt;/code&gt; this would mean
&lt;code&gt;lisp-mode-syntax-table&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;All remaining values are bindings using the form &lt;code&gt;(VARIABLE-NAME . VALUE)&lt;/code&gt;
used to set buffer-local values for other Font Lock variables.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The part we are interested about is search-based highlighting which uses
regular expressions to find specific text fragments and attach faces to them.&lt;/p&gt;
&lt;p&gt;Values used for &lt;code&gt;font-lock-keywords&lt;/code&gt; are also lists. Each element is a
construct used to specify one or more keywords to highlight. While these
constructs can have multiple forms for more complex use cases, we will only
use the two simplest ones:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;(REGEXP . FACE)&lt;/code&gt; tells Font Lock to use &lt;code&gt;FACE&lt;/code&gt; for text fragments which
match &lt;code&gt;REGEXP&lt;/code&gt;. For example, you could use &lt;code&gt;(&amp;quot;\\_&amp;lt;-?[0-9]+\\_&amp;gt;&amp;quot; . font-lock-constant-face)&lt;/code&gt; to highlight integers as constants (note the use
of &lt;code&gt;\_&amp;lt;&lt;/code&gt; and &lt;code&gt;\_&amp;gt;&lt;/code&gt; to match the start and end of a symbol; see the &lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_node/emacs/Regexp-Backslash.html&#34;&gt;regexp
documentation&lt;/a&gt;
for more information).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;(REGEXP (GROUP FACE)…)&lt;/code&gt; is a bit more advanced. When &lt;code&gt;REGEXP&lt;/code&gt; matches a
subset of the buffer, Font Lock assigns faces to the capture group
identified by their number. You could use this construction to detect a
complex syntaxic element and highlight some of its parts with different
faces.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;simplified-common-lisp-highlighting&#34;&gt;Simplified Common Lisp highlighting&lt;/h2&gt;
&lt;p&gt;We are going to configure keyword highlighting for the following types of
values:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Character literals, e.g. &lt;code&gt;#\Space&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Function names in the context of a function call for standard Common lisp
functions.&lt;/li&gt;
&lt;li&gt;Standard Common Lisp values such as &lt;code&gt;*STANDARD-OUTPUT*&lt;/code&gt; or &lt;code&gt;PI&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Additionally, we want to keep the default syntaxic highlighting configuration
which recognizes character strings, documentation strings and comments.&lt;/p&gt;
&lt;h3 id=&#34;faces&#34;&gt;Faces&lt;/h3&gt;
&lt;p&gt;Let us start by defining new faces for the different values we are going to
match:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defface g-cl-character-face
  &#39;((default :inherit font-lock-constant-face))
  &amp;quot;The face used to highlight Common Lisp character literals.&amp;quot;)

(defface g-cl-standard-function-face
  &#39;((default :inherit font-lock-keyword-face))
  &amp;quot;The face used to highlight standard Common Lisp function symbols.&amp;quot;)

(defface g-cl-standard-value-face
  &#39;((default :inherit font-lock-variable-name-face))
  &amp;quot;The face used to highlight standard Common Lisp value symbols.&amp;quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Nothing complicated here, we simply inherit from default Font Lock faces. You
can then configure these faces in your color theme without affecting other
modes using Font Lock.&lt;/p&gt;
&lt;h3 id=&#34;keywords&#34;&gt;Keywords&lt;/h3&gt;
&lt;p&gt;To detect standard Common Lisp functions and values, we are going to need a
regular expression. The first step is to build a list of strings for both
functions and values. Easy to do with a bit of Common Lisp code!&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun standard-symbol-names (predicate)
  (let ((symbols nil))
    (do-external-symbols (symbol :common-lisp)
      (when (funcall predicate symbol)
        (push (string-downcase (symbol-name symbol)) symbols)))
    (sort symbols #&#39;string&amp;lt;)))
    
(standard-symbol-names #&#39;fboundp)
(standard-symbol-names #&#39;boundp)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;STANDARD-SYMBOL-NAMES&lt;/code&gt; build a list of symbols exported from the
&lt;code&gt;:COMMON-LISP&lt;/code&gt; package which satisfy a predicate. The first call gives us the
name of all symbols bound to a function, and the second all which are bound to
a value.&lt;/p&gt;
&lt;p&gt;The astute reader will immediately wonder about symbols which are bound both a
function and a value. They are easy to find by calling &lt;code&gt;INTERSECTION&lt;/code&gt; on both
sets of names: &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;. It is not really a problem: we can
highlight function calls by matching function names preceded by &lt;code&gt;(&lt;/code&gt;, making
sure that these symbols will be correctly identified as either function
symbols or value symbols depending on the context.&lt;/p&gt;
&lt;p&gt;We store these lists of strings in the &lt;code&gt;g-cl-function-names&lt;/code&gt; and
&lt;code&gt;g-cl-value-names&lt;/code&gt; (the associated code is not reproduced here: these lists
are quite long; but I posted them as a
&lt;a href=&#34;https://gist.github.com/galdor/1c30c29471045d0365af72a4caf7b1f2&#34;&gt;Gist&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;With this lists, we can use the &lt;code&gt;regexp-opt&lt;/code&gt; Emacs Lisp function to build
optimized regular expressions matching them:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defvar g-cl-font-lock-keywords
  (let* ((character-re (concat &amp;quot;#\\\\&amp;quot; lisp-mode-symbol-regexp &amp;quot;\\_&amp;gt;&amp;quot;))
         (function-re (concat &amp;quot;(&amp;quot; (regexp-opt g-cl-function-names t) &amp;quot;\\_&amp;gt;&amp;quot;))
         (value-re (regexp-opt g-cl-value-names &#39;symbols)))
    `((,character-re . &#39;g-cl-character-face)
      (,function-re
       (1 &#39;g-cl-standard-function-face))
      (,value-re . &#39;g-cl-standard-value-face))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Characters literals are reasonably easy to match.&lt;/p&gt;
&lt;p&gt;Functions are a bit more complicated since we want to match the function name
when it is preceded by an opening parenthesis. We use a capture capture (see
the last argument of &lt;code&gt;regexp-opt&lt;/code&gt;) for the function name and highlight it
separately.&lt;/p&gt;
&lt;p&gt;Values are always matched as full symbols: we do not want to highlight parts
of a symbol, for example &lt;code&gt;MAP&lt;/code&gt; in a symbol named &lt;code&gt;MAPPING&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&#34;final-configuration&#34;&gt;Final configuration&lt;/h3&gt;
&lt;p&gt;Finally we can define the variable which will be used for &lt;code&gt;font-lock-defaults&lt;/code&gt;
in the initialization hook; we copy the original value from &lt;code&gt;lisp-mode&lt;/code&gt;, and
change the keyword list for what is going to be our own configuration:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defvar g-cl-font-lock-defaults
  &#39;((g-cl-font-lock-keywords)
    nil                                 ; enable syntaxic highlighting
    t                                   ; case insensitive highlighting
    nil                                 ; use the lisp-mode syntax table
    (font-lock-mark-block-function . mark-defun)
    (font-lock-extra-managed-props help-echo)
	(font-lock-syntactic-face-function
	 . lisp-font-lock-syntactic-face-function)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To configure &lt;code&gt;font-lock-defaults&lt;/code&gt;, we simply set it in the initialization hook
of &lt;code&gt;lisp-mode&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-init-lisp-font-lock ()
  (setq font-lock-defaults g-cl-font-lock-defaults))
  
(add-hook &#39;lisp-mode-hook &#39;g-init-lisp-font-lock)
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&#34;comparison&#34;&gt;Comparison&lt;/h2&gt;
&lt;p&gt;Let us compare highlighting for a fragment of code before and after our
changes:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;./before.png&#34; alt=&#34;Before&#34;&gt;
&lt;img src=&#34;./after.png&#34; alt=&#34;After&#34;&gt;&lt;/p&gt;
&lt;p&gt;The differences are subtle but important:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All standard functions are highlighted, helping to distinguish them from
user-defined functions.&lt;/li&gt;
&lt;li&gt;Standard values such as &lt;code&gt;*ERROR-OUTPUT*&lt;/code&gt; are highlighted.&lt;/li&gt;
&lt;li&gt;Character literals are highlighted the same way as character strings.&lt;/li&gt;
&lt;li&gt;Keywords are not highlighted anymore, avoiding the confusion with function
names.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;That was not easy; but as always, the effort of going through the
documentation and experimenting with different Emacs components was very
rewarding. Font Lock does not feel like a black box anymore, opening the road
for the customization of other major modes.&lt;/p&gt;
&lt;p&gt;In the future, I will work on a custom color scheme to use more subtle colors,
with the hope of reducing the rainbow effect of so many major modes, including
&lt;code&gt;lisp-mode&lt;/code&gt;.&lt;/p&gt;
</description>

      
      <category>emacs</category>
      
      <category>lisp</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/templating-in-emacs-with-tempo/">
      <title>Templating in Emacs with Tempo</title>
      <link>https://www.n16f.net/blog/templating-in-emacs-with-tempo/</link>
      <pubDate>Sun, 12 Feb 2023 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/templating-in-emacs-with-tempo/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;I have used text editors for more than 20 years; in all that time, I have
never used a templating system to generate content because copy pasting and
replacing always felt good enough. I recently decided to give it a try.&lt;/p&gt;
&lt;p&gt;In Emacs it is hard to talk about templating without mentionning
&lt;a href=&#34;https://github.com/joaotavora/yasnippet&#34;&gt;YASnippet&lt;/a&gt;. Developped by the
prolific João Távora, who also developped
&lt;a href=&#34;https://github.com/joaotavora/eglot&#34;&gt;Eglot&lt;/a&gt;, YASnippet lets you write
templates as text documents.&lt;/p&gt;
&lt;p&gt;But while I was researching the subject, I found out that Emacs already had
two builtin templating modules:
&lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_node/autotype/Skeleton-Language.html&#34;&gt;Skeleton&lt;/a&gt;
and
&lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_node/autotype/Tempo.html&#34;&gt;Tempo&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Tempo looked simpler, so I decided to give it a chance.&lt;/p&gt;
&lt;h2 id=&#34;using-tempo&#34;&gt;Using Tempo&lt;/h2&gt;
&lt;p&gt;Tempo templates are functions defined with &lt;code&gt;tempo-define-template&lt;/code&gt;. The
content to be inserted is a S-expression containing various kinds of elements
which control the insertion process.&lt;/p&gt;
&lt;p&gt;As an example, let us define a template to insert a HTML figure.&lt;/p&gt;
&lt;p&gt;First we define a variable to store a list of templates. When using
&lt;code&gt;html-mode&lt;/code&gt;, we instruct Tempo to use it.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defvar g-html-tempo-tags nil)

(defun g-init-html-tempo-templates ()
  (tempo-use-tag-list &#39;g-html-tempo-tags))

(add-hook &#39;html-mode-hook &#39;g-init-html-tempo-templates)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we define the template itself:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(tempo-define-template
 &amp;quot;g-html-figure&amp;quot;
 &#39;(&amp;quot;&amp;lt;figure&amp;gt;&amp;quot; n
   &amp;gt; &amp;quot;&amp;lt;img src=\&amp;quot;&amp;quot; (p &amp;quot;URI: &amp;quot;) &amp;quot;\&amp;quot;&amp;gt;&amp;quot; n
   &amp;gt; &amp;quot;&amp;lt;figcaption&amp;gt;&amp;quot; (p &amp;quot;Caption: &amp;quot;) &amp;quot;&amp;lt;/figcaption&amp;gt;&amp;quot; n
   &amp;quot;&amp;lt;/figure&amp;gt;&amp;quot;)
 &amp;quot;figure&amp;quot;
 &amp;quot;a figure containing an image and caption&amp;quot;
 &#39;g-html-tempo-tags)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This form creates a function named &lt;code&gt;tempo-template-g-html-figure&lt;/code&gt;. When it is
called, Tempo processes elements of the template:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Strings are inserted in the buffer.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;n&lt;/code&gt; symbol causes the insertion of a new line character&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;&amp;gt;&lt;/code&gt; symbol tells Emacs to indent the current line according to the rules
of the major mode of the buffer.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;p&lt;/code&gt; forms cause Tempo to ask the user for values to insert. Note that
you need to set &lt;code&gt;tempo-interative&lt;/code&gt; to &lt;code&gt;t&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that Tempo supports more elements; refer to the documentation string of
&lt;code&gt;tempo-define-template&lt;/code&gt; for more information.&lt;/p&gt;
&lt;p&gt;The template is associated to the &lt;code&gt;figure&lt;/code&gt; text tag which will be used for
completion. We also add a description, and finally add the template to the
&lt;code&gt;g-html-tempo-tags&lt;/code&gt; list. Note that these three last arguments are optional.&lt;/p&gt;
&lt;h2 id=&#34;tag-matching&#34;&gt;Tag matching&lt;/h2&gt;
&lt;p&gt;Of course we do not have to call the template manually. When we call
&lt;code&gt;tempo-complete-tag&lt;/code&gt;, Tempo uses the string before the cursor to decide which
template to insert. Open a HTML buffer, type &lt;code&gt;figure&lt;/code&gt; and execute
&lt;code&gt;tempo-complete-tag&lt;/code&gt; (I bind it to &lt;code&gt;M-S-&amp;lt;tab&amp;gt;&lt;/code&gt;): Tempo will automatically
insert our template.&lt;/p&gt;
&lt;p&gt;Tempo will handle the case where there is partial match for multiple
templates and will spawn a completion buffer.&lt;/p&gt;
&lt;p&gt;Now it would make sense to use &lt;code&gt;&amp;lt;figure&amp;gt;&lt;/code&gt; as tag. To do so, update the
&lt;code&gt;g-init-html-tempo-templates&lt;/code&gt; function to set the local variable that Tempo
uses to detect a tag:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(setq tempo-match-finder &amp;quot;\\(&amp;lt;[a-z]+&amp;gt;\\)\\=&amp;quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Doing so will use HTML tags as Tempo tags. We can then alter the call to
&lt;code&gt;tempo-define-template&lt;/code&gt; to use &lt;code&gt;&amp;lt;figure&amp;gt;&lt;/code&gt; as tag, and from now on use it for
template insertion.&lt;/p&gt;
&lt;p&gt;Of course this means we can customize it to allow different formats of Tempo
tags depending on the major mode we are in. Handy.&lt;/p&gt;
&lt;h2 id=&#34;writing-a-template-selector&#34;&gt;Writing a template selector&lt;/h2&gt;
&lt;p&gt;Calling template functions manually is unpractical and tag completion requires
remembering which tags have been defined. My interface of choice would be a
simple key spawning an incremental completion buffer
(&lt;a href=&#34;https://github.com/emacs-helm/helm&#34;&gt;Helm&lt;/a&gt; in my case) to let me select a
template to insert.&lt;/p&gt;
&lt;p&gt;As it turns out, it is not that hard:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-insert-tempo-template ()
  (interactive)
  (let* ((tags-data
          (mapcar (lambda (entry)
                    (let ((function (cdr entry)))
                      (list function (documentation function))))
                  (tempo-build-collection)))
         (completion-extra-properties
          `(:annotation-function
            (lambda (string)
              (let* ((data (alist-get string minibuffer-completion-table
                                      nil nil #&#39;string=))
                     (description (car data)))
                (format &amp;quot;  %s&amp;quot; description)))))
         (function-name (completing-read &amp;quot;Template: &amp;quot; tags-data))
         (function (intern function-name)))
    (funcall function)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As we have already seen in &lt;a href=&#34;posts/2023-01-12-switching-between-implementations-with-slime&#34;&gt;a previous
post&lt;/a&gt;,
&lt;code&gt;completing-read&lt;/code&gt; is quite limited in terms of presentation. I will probably
spend some time switching from Helm to a mix of
&lt;a href=&#34;https://github.com/minad/vertico&#34;&gt;Vertico&lt;/a&gt; and
&lt;a href=&#34;https://github.com/minad/marginalia&#34;&gt;Marginalia&lt;/a&gt; which apparently offers more
options.&lt;/p&gt;
&lt;p&gt;This will do the job in the mean time.&lt;/p&gt;
&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Tempo is reasonably satisfying. While it is a very simple module, it lets me
define templates as Emacs Lisp expressions, associate them to tags and store
them in tag lists which can be used in the major modes of my choice.&lt;/p&gt;
&lt;p&gt;I do not expect to start using dozens of tiny templates for the simplest
constructions, but Tempo is going to help with recurrent complex constructions
such as &lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_node/elisp/Simple-Packages.html&#34;&gt;Emacs module
skeletons&lt;/a&gt;
or Common Lisp &lt;a href=&#34;https://asdf.common-lisp.dev/asdf/The-defsystem-form.html&#34;&gt;system
definitions&lt;/a&gt;.&lt;/p&gt;
</description>

      
      <category>emacs</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/decluttering-dired-for-peace-of-mind/">
      <title>Decluttering Dired for peace of mind</title>
      <link>https://www.n16f.net/blog/decluttering-dired-for-peace-of-mind/</link>
      <pubDate>Fri, 03 Feb 2023 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/decluttering-dired-for-peace-of-mind/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;&lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_node/emacs/Dired.html&#34;&gt;Dired&lt;/a&gt;
is an Emacs mode providing an interactive interface to navigate and manipulate
files. It took me a while to start using it, but it is so efficient that I now
spend more time in it than in my usual shell.&lt;/p&gt;
&lt;p&gt;However I am no fond of the way files are listed. Dired displays a lot of
information which is — at least for me — rarely used. As a result the
important content (the name of the file) is flushed to the right of the
window, often leading to unpractical line wrapping.&lt;/p&gt;
&lt;p&gt;It is hard to find what you are searching for in this kind of interface:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;./dired-default-view.png&#34; alt=&#34;Dired default view&#34;&gt;&lt;/p&gt;
&lt;p&gt;So I decided to spend some time improving my Dired experience.&lt;/p&gt;
&lt;h2 id=&#34;configuring-a-simpler-view&#34;&gt;Configuring a simpler view&lt;/h2&gt;
&lt;p&gt;Dired comes with the &lt;code&gt;dired-hide-details-mode&lt;/code&gt; minor mode which hides extra
information. By default it is activated with the &lt;code&gt;(&lt;/code&gt; key.&lt;/p&gt;
&lt;p&gt;It has two drawbacks:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;First it also hides the display of symbolic link target while still showing
the &lt;code&gt;-&amp;gt;&lt;/code&gt; marker after the link. This behaviour can be changed by setting the
&lt;code&gt;dired-hide-details-hide-symlink-targets&lt;/code&gt; variable to &lt;code&gt;nil&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Second, Dired disables this minor mode each time you move to a different
directory, which is really annoying&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We create a &lt;code&gt;g-dired-minimal-view&lt;/code&gt; variable to store the current state. When
then write two functions: one to either enable or disable the minor mode
depending on the state, called by the &lt;code&gt;dired-mode-hook&lt;/code&gt;, and one to switch
between simple and extended view bound to the more accessible &lt;code&gt;&amp;lt;tab&amp;gt;&lt;/code&gt; key.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(setq g-dired-minimal-view t)

(defun g-dired-setup-view ()
  (dired-hide-details-mode (if g-dired-minimal-view 1 -1)))

(defun g-dired-switch-view ()
  (interactive)
  (setq g-dired-minimal-view (not g-dired-minimal-view))
  (g-dired-setup-view))

(use-package dired
  :config
  (setq dired-hide-details-hide-symlink-targets nil)

  :hook
  ((dired-mode-hook . g-dired-setup-view))

  :bind
  (:map dired-mode-map
        (&amp;quot;&amp;lt;tab&amp;gt;&amp;quot; . g-dired-switch-view)))
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&#34;a-better-extended-view&#34;&gt;A better extended view&lt;/h2&gt;
&lt;p&gt;While I prefer the simple view by default, the extended view still has its
use; but I find it hard to read:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The traditional representations of permissions takes a lot of horizontal
space, the 4 digit octal value would be shorter.&lt;/li&gt;
&lt;li&gt;The number of links is not really useful.&lt;/li&gt;
&lt;li&gt;The size of the file would be more convenient in a human readable format.&lt;/li&gt;
&lt;li&gt;The date and time uses an irregular US-style format which is either &amp;ldquo;month
day year&amp;rdquo; or &amp;ldquo;month day hour:minute&amp;rdquo;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To list files, Dired call the &lt;code&gt;ls&lt;/code&gt; program with a list of options (defined by
the &lt;code&gt;dired-listing-switches&lt;/code&gt; variable), insert the output in the buffer and
rely on regular expressions to extract information. This means that we have
very little control on formatting. We can change the options passed to &lt;code&gt;ls&lt;/code&gt;,
but we have to be careful not to break Dired features. For example, Dired can
highlight files with specific permissions, but rely on a regular expression
based on the format used by &lt;code&gt;ls&lt;/code&gt;, so we cannot change the permission column.&lt;/p&gt;
&lt;p&gt;First we use &lt;code&gt;dired-listing-switches&lt;/code&gt; to add the &lt;code&gt;-h&lt;/code&gt; (for human-readable
sizes) and &lt;code&gt;--time-style=long-iso&lt;/code&gt; option. Since these options are only
available for the GNU version of &lt;code&gt;ls&lt;/code&gt;, I will have to add platform detection
for FreeBSD support in a way that works when Dired is running with
&lt;a href=&#34;https://www.gnu.org/software/tramp/&#34;&gt;TRAMP&lt;/a&gt;. Something for another day.&lt;/p&gt;
&lt;p&gt;Then we add a &lt;code&gt;dired-after-readin-hook&lt;/code&gt; hook which is called by Dired after
the output of &lt;code&gt;ls&lt;/code&gt; has been collected and inserted in the buffer. In this
hook, we iterate on lines and process them. We highlight metadata with the
&lt;code&gt;shadow&lt;/code&gt; face to keep them visually distinct from the filename, and we hide
the link count column using the &lt;code&gt;invisible&lt;/code&gt; text property so that the text
stays in the buffer, making sure not to break Dired features.&lt;/p&gt;
&lt;p&gt;Finally we disable wrapping with a hook.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-dired-postprocess-ls-output ()
  &amp;quot;Postprocess the list of files printed by the ls program when
executed by Dired.&amp;quot;
  (save-excursion
    (goto-char (point-min))
    (while (not (eobp))
      ;; Go to the beginning of the next line representing a file
      (while (null (dired-get-filename nil t))
        (dired-next-line 1))
      (beginning-of-line)
      ;; Narrow to the line and process it
      (let ((start (line-beginning-position))
            (end (line-end-position)))
        (save-restriction
          (narrow-to-region start end)
          (setq inhibit-read-only t)
          (unwind-protect
              (g-dired-postprocess-ls-line)
            (setq inhibit-read-only nil))))
      ;; Next line
      (dired-next-line 1))))

(defun g-dired-disable-line-wrapping ()
  (setq truncate-lines t))

(defun g-dired-postprocess-ls-line ()
  &amp;quot;Postprocess a single line in the ls output, i.e. the information
about a single file. This function is called with the buffer
narrowed to the line.&amp;quot;
  ;; Highlight everything but the filename
  (when (re-search-forward directory-listing-before-filename-regexp nil t 1)
    (add-text-properties (point-min) (match-end 0) &#39;(font-lock-face shadow)))
  ;; Hide the link count
  (beginning-of-line)
  (when (re-search-forward &amp;quot; +[0-9]+&amp;quot; nil t 1)
    (add-text-properties (match-beginning 0) (match-end 0) &#39;(invisible t))))

(use-package dired
  :config
  (setq dired-listing-switches &amp;quot;-alh --time-style=long-iso&amp;quot;)

  :hook
  ((dired-mode-hook . g-dired-disable-line-wrapping)
   (dired-after-readin-hook . g-dired-postprocess-ls-output)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Much better now!&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;./dired-extended-view.png&#34; alt=&#34;Dired extended view&#34;&gt;&lt;/p&gt;
</description>

      
      <category>emacs</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/using-units-in-emacs-calc/">
      <title>Using units in Emacs Calc</title>
      <link>https://www.n16f.net/blog/using-units-in-emacs-calc/</link>
      <pubDate>Sat, 28 Jan 2023 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/using-units-in-emacs-calc/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;I was recently reminded of the ability of &lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_mono/calc.html&#34;&gt;Emacs
Calc&lt;/a&gt; to
perform computations on values with units. Like so many Emacs features, it was
on the list of the things I wanted to explore. Today is the day to do so!&lt;/p&gt;
&lt;h2 id=&#34;basic-operations-with-units&#34;&gt;Basic operations with units&lt;/h2&gt;
&lt;p&gt;The Emacs Calc manual presents a lot of features but makes it hard to
understand how to start. You cannot just enter an expression such as &amp;ldquo;25mm&amp;rdquo; in
&lt;code&gt;calc-mode&lt;/code&gt;: 25 will be correctly interpreted as a number, but &amp;ldquo;mm&amp;rdquo; will be
used to trigger the &lt;code&gt;m m&lt;/code&gt; key binding which executes &lt;code&gt;calc-save-modes&lt;/code&gt;. Not
what you had in mind.&lt;/p&gt;
&lt;p&gt;To enter a value with a unit, use the &lt;code&gt;&#39;&lt;/code&gt; (apostrophe) key to enter an
algebraic expression and hit &lt;code&gt;return&lt;/code&gt; to push it on the stack. You can now use
this value for your computations.&lt;/p&gt;
&lt;p&gt;Note that after hitting &lt;code&gt;&#39;&lt;/code&gt;, you can use it a second time to recall the
expression on the stack and edit it.&lt;/p&gt;
&lt;p&gt;To remove the unit associated with a value. Simply hit &lt;code&gt;u r&lt;/code&gt;, for &amp;ldquo;&lt;strong&gt;u&lt;/strong&gt;nit
&lt;strong&gt;r&lt;/strong&gt;emove&amp;rdquo;.&lt;/p&gt;
&lt;h3 id=&#34;simplifying-expressions&#34;&gt;Simplifying expressions&lt;/h3&gt;
&lt;p&gt;Expressions containing multiple units can be simplified. For example, enter
two values, &lt;code&gt;2m&lt;/code&gt; and &lt;code&gt;50cm&lt;/code&gt; and add them with &lt;code&gt;+&lt;/code&gt;. The resulting expression is
&lt;code&gt;2 m + 50 cm&lt;/code&gt;. Hit &lt;code&gt;u s&lt;/code&gt;, for &amp;ldquo;&lt;strong&gt;u&lt;/strong&gt;nit &lt;strong&gt;s&lt;/strong&gt;implify&amp;rdquo;, to have Calc simplify
it to &lt;code&gt;2.5 m&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&#34;converting-units&#34;&gt;Converting units&lt;/h3&gt;
&lt;p&gt;Unit conversion is of course possible. Use &lt;code&gt;u c&lt;/code&gt;, for &amp;ldquo;&lt;strong&gt;u&lt;/strong&gt;nit &lt;strong&gt;c&lt;/strong&gt;onvert&amp;rdquo;,
at any moment: Calc will ask for a unit and will try to convert the value at
the top of the stack into this unit. For example, entering &lt;code&gt;1.5mi&lt;/code&gt; (&lt;code&gt;mi&lt;/code&gt; being
the unit for miles) and typing &lt;code&gt;u c km &amp;lt;return&amp;gt;&lt;/code&gt; will correctly yield
&lt;code&gt;2.414016 km&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You will quickly discover that Calc tries way too hard to convert units and
will produce really strange results when types are not compatible. For
example, trying to convert &lt;code&gt;250mA&lt;/code&gt; into kilometers will produce &lt;code&gt;0.25A&lt;/code&gt; which
is clearly incorrect. I have no idea what causes this behaviour. Fortunately
you can use &lt;code&gt;u n&lt;/code&gt;, bound to &lt;code&gt;calc-convert-exact-units&lt;/code&gt;, which will check that
the unit you required is compatible with the unit of the expression on the
stack. To minimize the risk of producing incorrect results by mistake, I
replace the &lt;code&gt;calc-convert-units&lt;/code&gt; function by &lt;code&gt;calc-convert-exact-units&lt;/code&gt;. I
would prefer to remap &lt;code&gt;u c&lt;/code&gt;, but Calc has a strange way to handle key bindings
that prevent doing this kind of remapping.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(use-package calc
  :config
  (require &#39;calc-units)

  (setf (symbol-function &#39;calc-convert-units)
        (symbol-function &#39;calc-convert-exact-units)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As a shortcut, &lt;code&gt;u b&lt;/code&gt;, where &lt;code&gt;b&lt;/code&gt; stands for &amp;ldquo;&lt;strong&gt;b&lt;/strong&gt;ase&amp;rdquo;, will convert the value
to its base unit. For example, it will automatically convert &lt;code&gt;250mA&lt;/code&gt; to
&lt;code&gt;0.25A&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;defining-custom-units&#34;&gt;Defining custom units&lt;/h2&gt;
&lt;p&gt;Calc comes by default with more than 170 units (type &lt;code&gt;u v&lt;/code&gt; to list them all),
but none of them are about standard data size units. This is disappointing
given that Emacs is a software primarily used for programming. But we can add
new units, so it is not that much of a problem.&lt;/p&gt;
&lt;p&gt;New units can be defined with the &lt;code&gt;math-additional-units&lt;/code&gt; variable. Its format
is the same as &lt;code&gt;math-standard-units&lt;/code&gt;: each entry is a list of three elements:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A symbol identifying the unit.&lt;/li&gt;
&lt;li&gt;An expression indicating the value of the unit, or &lt;code&gt;nil&lt;/code&gt; for fundamental
units.&lt;/li&gt;
&lt;li&gt;A textual description.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let us add data sizes. We need two units, bits and bytes, with bytes being
defined as 8 bits. Note that using the &lt;code&gt;b&lt;/code&gt; symbol for the bit unit will
override the internal &amp;ldquo;Barn&amp;rdquo; unit; not a problem to me, I do not expect to use
it any time soon. Feel free to use &lt;code&gt;bit&lt;/code&gt; instead.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(setq math-additional-units
      &#39;((b nil &amp;quot;Bit&amp;quot;)
        (B &amp;quot;8 * b&amp;quot; &amp;quot;Byte&amp;quot;)))

(setq math-units-table nil)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We need to set &lt;code&gt;math-units-table&lt;/code&gt; to &lt;code&gt;nil&lt;/code&gt; after defining new units to force
Calc to recompute its internal table.&lt;/p&gt;
&lt;p&gt;These new units can of course be used with standard SI prefixes defined in
Calc. For example, &lt;code&gt;25kB&lt;/code&gt; will correctly be converted to &lt;code&gt;25000 B&lt;/code&gt;. And asking
for a convertion to the base unit will yield &lt;code&gt;200000 b&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;A limitation of Calc is its inability to handle unit prefixes of more than one
character, so we cannot define &lt;code&gt;ki&lt;/code&gt; as being a prefix with a multiplicative
value of 1024, which would for example allow us to manipulate kibibytes.&lt;/p&gt;
&lt;p&gt;A workaround is to define these power-of-two binary units with the prefix
included:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(setq math-additional-units
      &#39;((b nil &amp;quot;Bit&amp;quot;)
        (B &amp;quot;8 * b&amp;quot; &amp;quot;Byte&amp;quot;)

        (kiB &amp;quot;2^10 * B&amp;quot; &amp;quot;Kibibyte&amp;quot;)
        (MiB &amp;quot;2^20 * B&amp;quot; &amp;quot;Mebibyte&amp;quot;)
        (GiB &amp;quot;2^30 * B&amp;quot; &amp;quot;Gibibyte&amp;quot;)
        (TiB &amp;quot;2^40 * B&amp;quot; &amp;quot;Tebibyte&amp;quot;)
        (PiB &amp;quot;2^50 * B&amp;quot; &amp;quot;Pebibyte&amp;quot;)
        (EiB &amp;quot;2^60 * B&amp;quot; &amp;quot;Exbibyte&amp;quot;)))

(setq math-units-table nil)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Very helpful for data size conversion.&lt;/p&gt;
&lt;h2 id=&#34;going-farther-with-calc&#34;&gt;Going farther with Calc&lt;/h2&gt;
&lt;p&gt;Writing this post has been an interesting experience: it forced me to read
various parts of the Calc manual and some of its source files. It made me
realize that this package is full of useful features. I hope to find the time
to experiment with Calc features in the future.&lt;/p&gt;
</description>

      
      <category>emacs</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/taking-code-screenshots-in-emacs/">
      <title>Taking code screenshots in Emacs</title>
      <link>https://www.n16f.net/blog/taking-code-screenshots-in-emacs/</link>
      <pubDate>Wed, 18 Jan 2023 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/taking-code-screenshots-in-emacs/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;My friend &lt;a href=&#34;https://twitter.com/remilouf&#34;&gt;Rémi&lt;/a&gt; found an Emacs package
developed by &lt;a href=&#34;https://github.com/tecosaur&#34;&gt;Tecosaur&lt;/a&gt; to take screenshots of
your code. I usually do it with
&lt;a href=&#34;https://github.com/resurrecting-open-source-projects/scrot&#34;&gt;scrot&lt;/a&gt;, but it
always requires spending some time cropping the result with Gimp. A bit
annoying.&lt;/p&gt;
&lt;h2 id=&#34;installation&#34;&gt;Installation&lt;/h2&gt;
&lt;p&gt;Let us give it a try. The &lt;code&gt;screenshot&lt;/code&gt; package is not available on
&lt;a href=&#34;https://melpa.org/&#34;&gt;MELPA&lt;/a&gt; due to &lt;a href=&#34;https://github.com/melpa/melpa/pull/7327&#34;&gt;cosmetic
issues&lt;/a&gt;, so we have two options.&lt;/p&gt;
&lt;p&gt;The first one would be to clone the
&lt;a href=&#34;https://github.com/tecosaur/screenshot&#34;&gt;repository&lt;/a&gt;, add its location to
&lt;code&gt;load-path&lt;/code&gt;, and use &lt;code&gt;require&lt;/code&gt; to load the module. But doing so means having
to explicitely install its dependencies, &lt;code&gt;transient&lt;/code&gt; and &lt;code&gt;posframe&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The second option is to rely on
&lt;a href=&#34;https://github.com/jwiegley/use-package&#34;&gt;&lt;code&gt;use-package&lt;/code&gt;&lt;/a&gt; and
&lt;a href=&#34;https://github.com/radian-software/straight.el&#34;&gt;&lt;code&gt;straight.el&lt;/code&gt;&lt;/a&gt;. With
&lt;code&gt;straight.el&lt;/code&gt;, you can load packages from various sources, including Git
repositories.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(use-package screenshot
  :straight (:type git :host github :repo &amp;quot;tecosaur/screenshot&amp;quot;))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Evaluate the form with &lt;code&gt;C-x C-e&lt;/code&gt;, and &lt;code&gt;straight.el&lt;/code&gt; will clone the repository
and load the &lt;code&gt;screenshot&lt;/code&gt; package.&lt;/p&gt;
&lt;h2 id=&#34;usage&#34;&gt;Usage&lt;/h2&gt;
&lt;p&gt;Simply run &lt;code&gt;M-x screenshot&lt;/code&gt; to take a screenshot. If you are currently
selecting a region, it will screenshot this part only. If not, it will use the
entire buffer.&lt;/p&gt;
&lt;p&gt;Since Screenshot uses the
&lt;code&gt;transient&lt;/code&gt; library, it has the same well designed interface system as
&lt;a href=&#34;https://magit.vc/&#34;&gt;Magit&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;./interface.png&#34; alt=&#34;Screenshot interface&#34;&gt;&lt;/p&gt;
&lt;p&gt;I really like the ability to adjust the font size just for the screenshot. By
default, the &lt;code&gt;save&lt;/code&gt; action will save the image in the same directory as the
buffer which was selected when &lt;code&gt;screenshot&lt;/code&gt; was executed.&lt;/p&gt;
&lt;h2 id=&#34;customization&#34;&gt;Customization&lt;/h2&gt;
&lt;p&gt;Screenshot lets you change the default value of each setting. This is not
obvious when reading the code, because the variables used are defined
programmatically through two layers of macros: first
&lt;code&gt;screenshot--define-infix&lt;/code&gt; then &lt;code&gt;transient-define-infix&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It is too bad these values are not defined as proper settings using
&lt;code&gt;defcustom&lt;/code&gt;, but we can still set them.&lt;/p&gt;
&lt;p&gt;This is a list of available settings:&lt;/p&gt;
&lt;table&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th style=&#34;text-align: left&#34;&gt;Variable&lt;/th&gt;
          &lt;th style=&#34;text-align: left&#34;&gt;Description&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;&lt;code&gt;screenshot-line-numbers-p&lt;/code&gt;&lt;/td&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;Whether to display line numbers or not.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;&lt;code&gt;screenshot-relative-line-numbers-p&lt;/code&gt;&lt;/td&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;Whether to use relative line numbers or not.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;&lt;code&gt;screenshot-text-only-p&lt;/code&gt;&lt;/td&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;Whether to show code as simple text or not.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;&lt;code&gt;screenshot-truncate-lines-p&lt;/code&gt;&lt;/td&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;Whether to truncate long lines instead of wrapping them.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;&lt;code&gt;screenshot-font-family&lt;/code&gt;&lt;/td&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;The name of the font to use.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;&lt;code&gt;screenshot-font-size&lt;/code&gt;&lt;/td&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;The size of the font to use.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;&lt;code&gt;screenshot-border-width&lt;/code&gt;&lt;/td&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;The width of the border.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;&lt;code&gt;screenshot-radius&lt;/code&gt;&lt;/td&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;The corner radius of the border.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;&lt;code&gt;screenshot-min-width&lt;/code&gt;&lt;/td&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;The minimal width of the screenshot as a number of characters.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;&lt;code&gt;screenshot-max-width&lt;/code&gt;&lt;/td&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;The maximal width of the screenshot as a number of characters.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;&lt;code&gt;screenshot-shadow-radius&lt;/code&gt;&lt;/td&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;The corner radius of the shadow.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;&lt;code&gt;screenshot-shadow-intensity&lt;/code&gt;&lt;/td&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;The intensity of the shadow.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;&lt;code&gt;screenshot-shadow-color&lt;/code&gt;&lt;/td&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;The hexadecimal code of the shadow.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;&lt;code&gt;screenshot-shadow-offset-horizontal&lt;/code&gt;&lt;/td&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;The horizontal offset of the shadow in pixels.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;&lt;code&gt;screenshot-shadow-offset-vertical&lt;/code&gt;&lt;/td&gt;
          &lt;td style=&#34;text-align: left&#34;&gt;The vertical offset of the shadow in pixels.&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Another nice feature is the presence of a hook executed in the temporary
buffer used for the screenshot, allowing to execute code affecting this buffer
just before the actual screenshot is being taken.&lt;/p&gt;
&lt;p&gt;In my case, I use it to disable the &lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_node/emacs/Displaying-Boundaries.html&#34;&gt;fill column
indicator&lt;/a&gt;
and to remove the additional line spacing added by default by Screenshot.&lt;/p&gt;
&lt;p&gt;This is my configuration:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-screenshot-on-buffer-creation ()
  (setq display-fill-column-indicator-column nil)
  (setq line-spacing nil))

(use-package screenshot
  :straight (:type git :host github :repo &amp;quot;tecosaur/screenshot&amp;quot;)

  :config
  (setq screenshot-line-numbers-p nil)

  (setq screenshot-min-width 80)
  (setq screenshot-max-width 80)
  (setq screenshot-truncate-lines-p nil)

  (setq screenshot-text-only-p nil)

  (setq screenshot-font-family &amp;quot;Berkeley Mono&amp;quot;)
  (setq screenshot-font-size 10)

  (setq screenshot-border-width 16)
  (setq screenshot-radius 0)

  (setq screenshot-shadow-radius 0)
  (setq screenshot-shadow-offset-horizontal 0)
  (setq screenshot-shadow-offset-vertical 0)

  :hook
  ((screenshot-buffer-creation-hook . g-screenshot-on-buffer-creation)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All in all a very useful package!&lt;/p&gt;
</description>

      
      <category>emacs</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/improving-git-diffs-for-lisp/">
      <title>Improving Git diffs for Lisp</title>
      <link>https://www.n16f.net/blog/improving-git-diffs-for-lisp/</link>
      <pubDate>Sun, 08 Jan 2023 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/improving-git-diffs-for-lisp/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;All my code is stored in various Git repositories. When Git formats a diff
between two objects, it generates a list of hunks, or groups of changes.&lt;/p&gt;
&lt;p&gt;Each hunk can be displayed with a title which is automatically extracted. Git
ships with support for multiple languages, but Lisp dialects are not part of
it. Fortunately Git lets users configure their own extraction.&lt;/p&gt;
&lt;p&gt;The first step is to identify the language using a pattern applied to the
filename. Edit your Git attribute file at &lt;code&gt;$HOME/.gitattributes&lt;/code&gt; and add
entries for both Emacs Lisp and Common Lisp:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;*.lisp diff=common-lisp
*.el diff=elisp
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then edit your Git configuration file at &lt;code&gt;$HOME/.gitconfig&lt;/code&gt; and configure the
path of the Git attribute file:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[core]
    attributesfile = ~/.gitattributes
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, set the regular expression used to match a top-level function name:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[diff &amp;quot;common-lisp&amp;quot;]
    xfuncname=&amp;quot;^\\((def\\S+\\s+\\S+)&amp;quot;
    
[diff &amp;quot;elisp&amp;quot;]
    xfuncname=&amp;quot;^\\((((def\\S+)|use-package)\\s+\\S+)&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For Lisp dialects, we do not just identify function names: it is convenient to
identify hunks for all sorts of top-level definitions. We use a regular
expression which captures the first symbol of the form and the name that
follows.&lt;/p&gt;
&lt;p&gt;Of course you can modifiy these expressions to identify more complex top-level
forms. For example, for Emacs Lisp, I also want to identify &lt;code&gt;use-package&lt;/code&gt;
expressions.&lt;/p&gt;
&lt;p&gt;You can see the result in all tools displaying Git diffs, for example in
Magit with Common Lisp code:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;./common-lisp-diff.png&#34; alt=&#34;Common Lisp diff&#34;&gt;&lt;/p&gt;
&lt;p&gt;Or for my Emacs configuration file:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;./elisp-diff.png&#34; alt=&#34;Emacs Lisp diff&#34;&gt;&lt;/p&gt;
&lt;p&gt;Hunk titles, highlighted in blue, now contain the type and name of the
top-level construction the changes are associated with.&lt;/p&gt;
&lt;p&gt;A simple change, but one which really helps reading diffs.&lt;/p&gt;
</description>

      
      <category>lisp</category>
      
      <category>emacs</category>
      
      <category>git</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/eshell-key-bindings-and-completion/">
      <title>Eshell key bindings and completion</title>
      <link>https://www.n16f.net/blog/eshell-key-bindings-and-completion/</link>
      <pubDate>Fri, 06 Jan 2023 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/eshell-key-bindings-and-completion/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;I have been having completion issues in Eshell for some time, and I decided to
fix it. Of course the &lt;a href=&#34;https://en.wiktionary.org/wiki/yak_shaving&#34;&gt;yak
shaving&lt;/a&gt; is always strong with
Emacs, so it ended up being a bit more complicated than expected.&lt;/p&gt;
&lt;h2 id=&#34;defining-keys-with-use-package&#34;&gt;Defining keys with use-package&lt;/h2&gt;
&lt;p&gt;Eshell key bindings are stored in &lt;code&gt;eshell-mode-map&lt;/code&gt;. For quite some time,
Eshell has been initializing this value to &lt;code&gt;nil&lt;/code&gt;, making it impossible to
define keys before Eshell has been started. As &lt;a href=&#34;https://lists.gnu.org/archive/html/bug-gnu-emacs/2016-02/msg01532.html&#34;&gt;initially
reported&lt;/a&gt;,
the workaround was to call &lt;code&gt;define-key&lt;/code&gt; in a hook, &lt;code&gt;eshell-mode-hook&lt;/code&gt; to be
precise.&lt;/p&gt;
&lt;p&gt;Apparently Emacs 28.1 fixed it. However to be able to use the key binding
mechanism of &lt;code&gt;use-package&lt;/code&gt;, &lt;code&gt;eshell-mode-map&lt;/code&gt; must actually be defined. The
solution is to load the right module &lt;em&gt;before&lt;/em&gt; &lt;code&gt;use-package&lt;/code&gt; configures key
bindings, therefore using &lt;code&gt;:init&lt;/code&gt; and not &lt;code&gt;:config&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(use-package eshell
  :init
  (require &#39;esh-mode) ; eshell-mode-map

  :bind
  ((&amp;quot;C-x s&amp;quot; . g-eshell)
   :map eshell-mode-map
   ((&amp;quot;C-l&amp;quot; . &#39;g-eshell-clear)
    (&amp;quot;C-r&amp;quot; . helm-eshell-history))))
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&#34;configuring-company-for-completion&#34;&gt;Configuring Company for completion&lt;/h2&gt;
&lt;p&gt;Eshell supports the standard &lt;code&gt;completion-at-point&lt;/code&gt; completion system, the
function being bound to &lt;code&gt;&amp;lt;tab&amp;gt;&lt;/code&gt; by default. If you are using Helm, it will be
automatically used to present the list of possible completions. This is a bit
cumbersome: I want completion to be inline, not in a separate buffer in the
bottom of the screen.&lt;/p&gt;
&lt;p&gt;Since I use &lt;a href=&#34;https://company-mode.github.io/&#34;&gt;Company&lt;/a&gt;, I can instead bind
&lt;code&gt;&amp;lt;tab&amp;gt;&lt;/code&gt; to &lt;code&gt;company-complete&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(define-key eshell-mode-map (kbd &amp;quot;&amp;lt;tab&amp;gt;&amp;quot;) &#39;company-complete)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;./company-completion.png&#34; alt=&#34;Company completion&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;fixing-completion-of-commands-in-subdirectories&#34;&gt;Fixing completion of commands in subdirectories&lt;/h2&gt;
&lt;p&gt;As it turns out, completion works as intended for global commands, but fails
when calling a command in a subdirectory. For example, when completing
&lt;code&gt;./utils/create-blog-post&lt;/code&gt;, &lt;code&gt;completion-at-point&lt;/code&gt; will replace the line by
&lt;code&gt;create-blog-post&lt;/code&gt;, which is of course incorrect. This bug is also present
when completing the absolute path of an executable.&lt;/p&gt;
&lt;p&gt;After spending way too much time on Google and in the &lt;code&gt;em-cmpl&lt;/code&gt; module, which
handles completion for Eshell, it turned out to be a &lt;a href=&#34;https://debbugs.gnu.org/cgi/bugreport.cgi?bug=48995&#34;&gt;known
bug&lt;/a&gt;, introduced &lt;a href=&#34;https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=82c76e3aeb2465d1d1e66eae5db13ba53e38ed84&#34;&gt;almost
two years
ago&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The workaround is to redefine &lt;code&gt;eshell--complete-commands-list&lt;/code&gt; as it was
before the commit:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun eshell--complete-commands-list ()
  &amp;quot;Generate list of applicable, visible commands.&amp;quot;
  (let ((filename (pcomplete-arg)) glob-name)
    (if (file-name-directory filename)
        (if eshell-force-execution
            (pcomplete-dirs-or-entries nil #&#39;file-readable-p)
          (pcomplete-executables))
      (if (and (&amp;gt; (length filename) 0)
               (eq (aref filename 0) eshell-explicit-command-char))
          (setq filename (substring filename 1)
                pcomplete-stub filename
                glob-name t))
      (let* ((paths (eshell-get-path))
             (cwd (file-name-as-directory
                   (expand-file-name default-directory)))
             (path &amp;quot;&amp;quot;) (comps-in-path ())
             (file &amp;quot;&amp;quot;) (filepath &amp;quot;&amp;quot;) (completions ()))
        ;; Go thru each path in the search path, finding completions.
        (while paths
          (setq path (file-name-as-directory
                      (expand-file-name (or (car paths) &amp;quot;.&amp;quot;)))
                comps-in-path
                (and (file-accessible-directory-p path)
                     (file-name-all-completions filename path)))
          ;; Go thru each completion found, to see whether it should
          ;; be used.
          (while comps-in-path
            (setq file (car comps-in-path)
                  filepath (concat path file))
            (if (and (not (member file completions)) ;
                     (or (string-equal path cwd)
                         (not (file-directory-p filepath)))
                     (if eshell-force-execution
                         (file-readable-p filepath)
                       (file-executable-p filepath)))
                (setq completions (cons file completions)))
            (setq comps-in-path (cdr comps-in-path)))
          (setq paths (cdr paths)))
        ;; Add aliases which are currently visible, and Lisp functions.
        (pcomplete-uniquify-list
         (if glob-name
             completions
           (setq completions
                 (append (if (fboundp &#39;eshell-alias-completions)
                             (eshell-alias-completions filename))
                         (eshell-winnow-list
                          (mapcar
                           (lambda (name)
                             (substring name 7))
                           (all-completions (concat &amp;quot;eshell/&amp;quot; filename)
                                            obarray #&#39;functionp))
                          nil &#39;(eshell-find-alias-function))
                         completions))
           (append (and (or eshell-show-lisp-completions
                            (and eshell-show-lisp-alternatives
                                 (null completions)))
                        (all-completions filename obarray #&#39;functionp))
                   completions)))))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I have no idea what is wrong in the new implementation of the function, but
hopefully someone can find a fix to be merged before Emacs 29 is released.&lt;/p&gt;
</description>

      
      <category>emacs</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/org-mode-headline-tips/">
      <title>Org-mode headline tips</title>
      <link>https://www.n16f.net/blog/org-mode-headline-tips/</link>
      <pubDate>Mon, 02 Jan 2023 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/org-mode-headline-tips/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;&lt;a href=&#34;https://orgmode.org/&#34;&gt;Org-mode&lt;/a&gt; is what got me into Emacs in the first place.
I use it to take notes, keep track of what I have to do and plan projects.&lt;/p&gt;
&lt;p&gt;An Org file is a tree of elements, each one starting with a headline.
Manipulating headlines can be confusing; here are a few tips that you may find
helpful.&lt;/p&gt;
&lt;h2 id=&#34;do-not-split-lines-when-adding-headlines&#34;&gt;Do not split lines when adding headlines&lt;/h2&gt;
&lt;p&gt;At any point, you can insert a new headline below the current one using the
&lt;code&gt;C-&amp;lt;return&amp;gt;&lt;/code&gt; shortcut, which calls &lt;code&gt;org-insert-heading&lt;/code&gt;. However if the cursor
is at the middle of a line (headline or content), Org will split it and add
the end of the line to the new headline.&lt;/p&gt;
&lt;p&gt;&lt;del&gt;Quite annoying, really. Fortunately there is a function to add a headline
without splitting the currentline, &lt;code&gt;org-insert-heading-respect-content&lt;/code&gt;.
I simply bind it to &lt;code&gt;C-&amp;lt;return&amp;gt;&lt;/code&gt;:&lt;/del&gt;&lt;/p&gt;
&lt;p&gt;&lt;del&gt;Obviously I want the same behaviour for &lt;code&gt;C-M-&amp;lt;return&amp;gt;&lt;/code&gt; which inserts a TODO
headline:&lt;/del&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Edit (2023-01-07)&lt;/em&gt;&lt;br&gt;
As it turns out, a simpler way to do that is to set
&lt;code&gt;org-insert-heading-respect-content&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(setq org-insert-heading-respect-content t)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;No need to rebind keys.&lt;/p&gt;
&lt;h2 id=&#34;jump-quickly-to-the-right-headline&#34;&gt;Jump quickly to the right headline&lt;/h2&gt;
&lt;p&gt;Some of my Org files are quite long. For example, my main development tracking
file contains more than 700 lines.&lt;/p&gt;
&lt;p&gt;I never really thought about navigation, and usually either search for
elements by title with &lt;code&gt;C-s&lt;/code&gt; or move between folded items manually (I use
&lt;code&gt;org-startup-folded&lt;/code&gt; so that all items are folded by default).&lt;/p&gt;
&lt;p&gt;As it turns out, Org has a buitin navigation system triggered by &lt;code&gt;C-c C-j&lt;/code&gt;. At
first I could not see how it could help; but then I realized that this feature
can use native Emacs completion. This is incredibly useful when combined with
a vertical completion framework such as Helm, making it easy to jump to any
headline just by typing a couple characters.&lt;/p&gt;
&lt;p&gt;In order to do that, we enable completion with &lt;code&gt;org-goto-interface&lt;/code&gt;, disable
the multi-steps navigation system, and set the maximum depth level used to
select headlines which will be listed during completion.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(setq org-goto-interface &#39;outline-path-completion)
(setq org-outline-path-complete-in-steps nil)
(setq org-goto-max-level 2)
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&#34;stop-highlighting-todo-headlines&#34;&gt;Stop highlighting TODO headlines&lt;/h2&gt;
&lt;p&gt;By default, Org highlights the entire headline for TODO items, not just
the &lt;code&gt;TODO&lt;/code&gt; or &lt;code&gt;DONE&lt;/code&gt; keyword.&lt;/p&gt;
&lt;p&gt;I believe this is a mistake: headlines mark the hierarchy of items, and having
multiple red or green headlines is confusing. This behaviour can be configured
with two settings:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(setq org-fontify-todo-headline nil)
(setq org-fontify-done-headline nil)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This way, headlines keep their original color (defined by &lt;code&gt;org-level-*&lt;/code&gt;
faces), and keywords only are highlighted with the &lt;code&gt;org-todo&lt;/code&gt; and &lt;code&gt;org-done&lt;/code&gt;
faces.&lt;/p&gt;
</description>

      
      <category>emacs</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/replacing-projectile-by-project/">
      <title>Replacing Projectile by Project</title>
      <link>https://www.n16f.net/blog/replacing-projectile-by-project/</link>
      <pubDate>Tue, 27 Dec 2022 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/replacing-projectile-by-project/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;I have been using &lt;a href=&#34;https://github.com/bbatsov/projectile&#34;&gt;Projectile&lt;/a&gt; for
years, and it served me well. While Projectile is really useful, it is a third
party package made of more than 8000 lines of code. Since the built-in
&lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_node/emacs/Projects.html&#34;&gt;Project&lt;/a&gt;
module has received lots of improvements since Emacs 28.1, I decided to give
it a try and see if I could remove Projectile from my Emacs setup.&lt;/p&gt;
&lt;h2 id=&#34;key-binding&#34;&gt;Key binding&lt;/h2&gt;
&lt;p&gt;The Project keymap is bound to &lt;code&gt;C-x p&lt;/code&gt; by default, but I am used to &lt;code&gt;C-c p&lt;/code&gt;.
This is easy to change, for example with &lt;code&gt;use-package&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(use-package project
  :bind-keymap
  ((&amp;quot;C-c p&amp;quot; . project-prefix-map)))
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&#34;switching-between-projects&#34;&gt;Switching between projects&lt;/h2&gt;
&lt;p&gt;By default, the project switching command uses a prompt letting you select a
command to run for the selected project. If you always use the same command,
you can configure it, allowing to switch project without any prompt.&lt;/p&gt;
&lt;p&gt;For example, to always display the project directory with Dired:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(setq project-switch-commands &#39;project-dired)
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&#34;search-using-helm-and-ag&#34;&gt;Search using Helm and AG&lt;/h2&gt;
&lt;p&gt;The Projectile feature I used the most is incremental project search using
&lt;code&gt;helm-projectile&lt;/code&gt; and &lt;code&gt;helm-ag&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Adding this feature to Project is not that hard. Let us write the search
function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-project-search ()
  (interactive)
  (let ((project (project-current t)))
    (helm-do-ag (project-root project))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And bind it to &lt;code&gt;C-c p s&lt;/code&gt; (replacing &lt;code&gt;project-shell&lt;/code&gt;, a command I do not use):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(use-package project
  :bind
  (:map project-prefix-map
        ((&amp;quot;s&amp;quot; . &#39;g-project-search))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;One thing to be aware of: &lt;code&gt;helm-ag&lt;/code&gt; does not take patterns returned by
&lt;code&gt;project-ignores&lt;/code&gt; into consideration when filtering results. It is not that
much of a problem to me, since &lt;code&gt;ag&lt;/code&gt; already supports &lt;code&gt;.gitignore&lt;/code&gt; files&lt;/p&gt;
&lt;p&gt;But it would be an improvement to use &lt;code&gt;project-ignores&lt;/code&gt; and pass the pattern
list to &lt;code&gt;ag&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In the mean time, I can also remove the &lt;code&gt;helm-projectile&lt;/code&gt; dependency.&lt;/p&gt;
&lt;h2 id=&#34;starting-eshell&#34;&gt;Starting Eshell&lt;/h2&gt;
&lt;p&gt;While both Projectile and Project can run Eshell in the current project, I
want the ability to start it the same way whether I am in a project or not.
Therefore I have a small function looking for a current project and running
Eshell the right way. Migrating to Project is easy:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-eshell ()
  &amp;quot;Start eshell at the root of the current project, or in the
current directory if the current buffer is not part of a
project.&amp;quot;
  (interactive)
  (if (project-current)
      (project-eshell)
    (eshell)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I bind it to &lt;code&gt;C-x s&lt;/code&gt; (&amp;ldquo;s&amp;rdquo; being for &amp;ldquo;shell&amp;rdquo;):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(global-set-key (kbd &amp;quot;C-x s&amp;quot;) &#39;g-eshell)
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;The result is quite satisfying. It may not do everything Projectile supports,
but the features I use are all implemented. As a result, my Emacs setup is
simpler and lighter, relying on one less external dependency.&lt;/p&gt;
</description>

      
      <category>emacs</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/clearing-the-eshell-buffer/">
      <title>Clearing the Eshell Buffer</title>
      <link>https://www.n16f.net/blog/clearing-the-eshell-buffer/</link>
      <pubDate>Sat, 24 Dec 2022 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/clearing-the-eshell-buffer/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;I have used the &lt;code&gt;C-l&lt;/code&gt; shortcut in &lt;a href=&#34;https://www.zsh.org/&#34;&gt;ZSH&lt;/a&gt; to clear the
screen for as long as I can remember. When I started using
&lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_node/eshell/&#34;&gt;Eshell&lt;/a&gt;, one of
the first problems I encountered was the inability to clear the buffer. I like
to remove the output of previous commands to be able to focus on the current
one, so let us script Emacs once again.&lt;/p&gt;
&lt;p&gt;After a quick look to the &lt;code&gt;eshell&lt;/code&gt; module, it seems we can do that in two
steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Clear the entire buffer with &lt;code&gt;(eshell/clear t)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Recreate the prompt with &lt;code&gt;(eshell-emit-prompt)&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;A non-obvious edge case is that it should be possible to hit &lt;code&gt;C-l&lt;/code&gt; while
typing a command without losing what was typed. For that, we have to keep a
copy of the input content before clearing the screen and reinsert it after.&lt;/p&gt;
&lt;p&gt;The result is the following function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-eshell-clear ()
  (interactive)
  (let ((input (eshell-get-old-input)))
    (eshell/clear t)
    (eshell-emit-prompt)
    (insert input)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We then write a hook which binds the function to &lt;code&gt;C-l&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-eshell-init-keys ()
  (define-key eshell-mode-map (kbd &amp;quot;C-l&amp;quot;) &#39;g-eshell-clear))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And register the hook:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(add-hook &#39;eshell-mode-hook &#39;g-eshell-init-keys)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that clearing the screen means removing shell history from the buffer.
Not really an issue: instead of looking in the buffer, I use
&lt;code&gt;helm-eshell-history&lt;/code&gt; which is much more convenient.&lt;/p&gt;
</description>

      
      <category>emacs</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/investigating-a-ffap-issue-in-emacs/">
      <title>Investigating a FFAP issue in Emacs</title>
      <link>https://www.n16f.net/blog/investigating-a-ffap-issue-in-emacs/</link>
      <pubDate>Thu, 22 Dec 2022 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/investigating-a-ffap-issue-in-emacs/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;I just encountered the strangest behaviour in Emacs. I was editing a shell
script, hit &lt;code&gt;C-x b&lt;/code&gt; to switch to another buffer, and Emacs suddenly froze with
the following message:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Pinging &amp;lt;hostname&amp;gt; (Commercial)...
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Where &lt;code&gt;&amp;lt;hostname&lt;/code&gt; was the hostname of a server used in the script.&lt;/p&gt;
&lt;p&gt;I really do not like the idea of my text editor pinging random servers just
because their name appears in a buffer, so I went to the bottom of it.&lt;/p&gt;
&lt;p&gt;Since I had no idea what caused this behaviour, I simply ran grep for the
&amp;ldquo;Pinging&amp;rdquo; message in the Emacs source repository and found it in the
&lt;code&gt;ffap-machine-p&lt;/code&gt; function. To find out how it was called, I enabled tracing,
making sure to print the current backtrace for each trace:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(trace-function &#39;ffap-machine-p nil &#39;backtrace)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This yielded the following (truncated) trace:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ffap-machine-p(&amp;quot;pkg.exograd.com&amp;quot;)
ffap-machine-at-point()
ffap-guesser()
ffap-guess-file-name-at-point()
run-hook-with-args-until-success(ffap-guess-file-name-at-point)
helm-guess-filename-at-point()
helm-fuzzy-highlight-matches(…)
helm--collect-matches(…)
helm-update(nil)
helm-read-from-minibuffer(nil nil nil nil nil nil nil)
helm-internal(…)
apply(helm-internal (…))
helm((helm-source-buffers-list helm-source-buffer-not-found) …)
apply(helm ((helm-source-buffers-list helm-source-buffer-not-found) …)
helm(…)
helm-buffers-list()
funcall-interactively(helm-buffers-list)
call-interactively(helm-buffers-list nil nil)
command-execute(helm-buffers-list)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Apparently, Helm tries to guess if the cursor is pointing to a filename using
the &lt;code&gt;ffap&lt;/code&gt; (for &amp;ldquo;find file at point&amp;rdquo;) module. For some reason, it does not
just identify paths, but also URLs and machine names. And the default strategy
to check if a machine is &amp;ldquo;real&amp;rdquo; and &amp;ldquo;reachable&amp;rdquo; (as documented in
&lt;code&gt;ffap-machine-p&lt;/code&gt; is to ping it.&lt;/p&gt;
&lt;p&gt;This kind is insanity is really disappointing from Emacs: a text editor should
absolutely not use the network in such a hidden way by default.&lt;/p&gt;
&lt;p&gt;The behaviour of &lt;code&gt;ffap-machine-p&lt;/code&gt; depends on three settings:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ffap-machine-p-local&lt;/code&gt;: what to do if the hostname does not have a domain.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ffap-machine-p-known&lt;/code&gt;: what to do if the hostname has a known domain.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ffap-machine-p-unknown&lt;/code&gt;: what to do if the hostname has an unknown domain.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For all these variables, the value is either &lt;code&gt;accept&lt;/code&gt;, to indicate that the
hostname is a valid hostname, &lt;code&gt;reject&lt;/code&gt; to indicate it is not, or &lt;code&gt;ping&lt;/code&gt; to
check by sending a ping message.&lt;/p&gt;
&lt;p&gt;The way to fix this mess is to accept local and known hostnames while
rejecting unknown ones:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(setq ffap-machine-p-local &#39;accept)
(setq ffap-machine-p-known &#39;accept)
(setq ffap-machine-p-unknown &#39;reject)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you are curious, &lt;code&gt;ffap&lt;/code&gt; uses &lt;code&gt;mail-extr-all-top-level-domains&lt;/code&gt; in the
&lt;code&gt;mail-extr&lt;/code&gt; module to assess if a domain is &amp;ldquo;known&amp;rdquo;. This function will
identify most common TLDs, but has never been updated for modern TLDs.
Something to keep in mind if you want to use it.&lt;/p&gt;
</description>

      
      <category>emacs</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/fixing-unquote-splicing-behaviour-with-paredit/">
      <title>Fixing unquote-splicing behaviour with Paredit</title>
      <link>https://www.n16f.net/blog/fixing-unquote-splicing-behaviour-with-paredit/</link>
      <pubDate>Tue, 13 Dec 2022 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/fixing-unquote-splicing-behaviour-with-paredit/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;&lt;a href=&#34;http://paredit.org/&#34;&gt;Paredit&lt;/a&gt; is an Emacs package for structural editing. It
is particularly useful in Lisp languages to manipulate expressions instead of
just characters.&lt;/p&gt;
&lt;p&gt;One of the numerous little features of Paredit is the automatic insertion of a
space character before a delimiting pair. For example, if you are typing
&lt;code&gt;(length&lt;/code&gt;, typing &lt;code&gt;(&lt;/code&gt; will have Paredit automatically insert a space character
before the opening parenthesis, to produce the expected &lt;code&gt;(length (&lt;/code&gt; content.&lt;/p&gt;
&lt;p&gt;Paredit is smart enough to avoid doing so after quote, backquote or comma
characters, but not after an unquote-splicing sequence (&lt;code&gt;,@&lt;/code&gt;) which is quite
annoying in languages such as Scheme or Common Lisp. As almost always in
Emacs, this behaviour can be customized.&lt;/p&gt;
&lt;p&gt;Paredit decides whether to add a space or not using the
&lt;code&gt;paredit-space-for-delimiter-p&lt;/code&gt; function, ending up with applying a list of
predicates from &lt;code&gt;paredit-space-for-delimiter-predicates&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Let us add our own. For more flexibility, we will start by defining a list of
prefixes which are not to be followed by a space:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defvar g-paredit-no-space-prefixes (list &amp;quot;,@&amp;quot;))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We then write our predicate which simply checks if we are right after one of
these prefixes:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-paredit-space-for-delimiter (endp delimiter)
  (let ((point (point)))
    (or endp
        (seq-every-p
         (lambda (prefix)
           (and (&amp;gt; point (length prefix))
                (let ((start (- point (length prefix)))
                      (end point))
                  (not (string= (buffer-substring start end) prefix)))))
         g-paredit-no-space-prefixes))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally we add a Paredit hook to append our predicate to the list:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun g-init-paredit-space-for-delimiter ()
  (add-to-list &#39;paredit-space-for-delimiter-predicates
               &#39;g-paredit-space-for-delimiter))

(add-hook &#39;paredit-mode-hook &#39;g-init-paredit-space-for-delimiter)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Not only does it fix the problem for unquote-slicing, but it makes it easy to
add new prefixes. For example I immediately added &lt;code&gt;#p&lt;/code&gt; (used for pathnames in
Common Lisp, e.g. &lt;code&gt;#p&amp;quot;/usr/bin/&amp;quot;&lt;/code&gt;) to the list.&lt;/p&gt;
</description>

      
      <category>lisp</category>
      
      <category>emacs</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/eye-level-window-centering-in-emacs/">
      <title>Eye level window centering in Emacs</title>
      <link>https://www.n16f.net/blog/eye-level-window-centering-in-emacs/</link>
      <pubDate>Sun, 30 Oct 2022 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/eye-level-window-centering-in-emacs/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;In the default Emacs configuration, &lt;code&gt;C-l&lt;/code&gt; is bound to &lt;code&gt;recenter-top-bottom&lt;/code&gt;.
This function has an interesting behaviour: when called once, it scrolls the
current window so that the current line appears at the middle of the screen.
When called several times in a row, it cycles through three scrolling
position: middle, top and bottom.&lt;/p&gt;
&lt;p&gt;This has always bugged me because I only need one of the three behaviours, and
none of these positions are useful to me. In pratice, I want to focus on the
line I am working on, which means putting it at eye level. In my case at
roughly 20% of the top of the window.&lt;/p&gt;
&lt;p&gt;Fortunately Emacs is easy to customize. As it turns out, &lt;code&gt;recenter-top-bottom&lt;/code&gt;
is based on &lt;code&gt;recenter&lt;/code&gt; which is easy to use:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defcustom g-recenter-window-eye-level 0.2
  &amp;quot;The relative position of the line considered as eye level in the
current window, as a ratio between 0 and 1.&amp;quot;)

(defun g-recenter-window ()
  &amp;quot;Scroll the window so that the current line is at eye level.&amp;quot;
  (interactive)
  (let ((line (round (* (window-height) g-recenter-window-eye-level))))
    (recenter line)))

(global-set-key (kbd &amp;quot;C-l&amp;quot;) &#39;g-recenter-window)
&lt;/code&gt;&lt;/pre&gt;
</description>

      
      <category>emacs</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/local-clhs-access-in-emacs/">
      <title>Local CLHS access in Emacs</title>
      <link>https://www.n16f.net/blog/local-clhs-access-in-emacs/</link>
      <pubDate>Wed, 01 Jan 2020 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/local-clhs-access-in-emacs/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;The CLHS, or Common Lisp HyperSpec, is one of the most important resource for
any Common Lisp developer. It is derived from the official Common Lisp
standard, and contains documentation for every aspect of the language.&lt;/p&gt;
&lt;p&gt;While it is currently made available
&lt;a href=&#34;http://www.lispworks.com/documentation/HyperSpec/Front/index.htm&#34;&gt;online&lt;/a&gt; by
&lt;a href=&#34;http://www.lispworks.com/documentation/common-lisp.html&#34;&gt;LispWorks&lt;/a&gt;, it can
be useful to be able to access it locally, for example when you do not have
any internet connection available.&lt;/p&gt;
&lt;p&gt;For this purpose, LispWorks provides an
&lt;a href=&#34;ftp://ftp.lispworks.com/pub/software_tools/reference/HyperSpec-7-0.tar.gz&#34;&gt;archive&lt;/a&gt;
which can be downloaded and browsed offline.&lt;/p&gt;
&lt;p&gt;While the HyperSpec is valuable on its own, the
&lt;a href=&#34;https://common-lisp.net/project/slime/&#34;&gt;SLIME&lt;/a&gt; Emacs mode provides various
functions to make it even more useful.&lt;/p&gt;
&lt;p&gt;I have found the following functions particularily useful:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;slime-documentation-lookup&lt;/code&gt;, or &lt;code&gt;C-c C-d h&lt;/code&gt; to browse the
documentation associated with a symbol.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;common-lisp-hyperspec-format&lt;/code&gt;, or &lt;code&gt;C-c C-d ~&lt;/code&gt;, to lookup &lt;code&gt;format&lt;/code&gt; control
characters.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;common-lisp-hyperspec-glossary-term&lt;/code&gt;, or &lt;code&gt;C-c C-d g&lt;/code&gt;, to access terms in
the
&lt;a href=&#34;http://www.lispworks.com/documentation/lw50/CLHS/Front/index.htm&#34;&gt;glossary&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With the default configuration, Emacs will use the online HyperSpec. You can
have it use a local copy by setting &lt;code&gt;common-lisp-hyperspec-root&lt;/code&gt; to a file
URI. For example, if you downloaded the content of the CLHS archive to
&lt;code&gt;~/common-lisp/&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(setq common-lisp-hyperspec-root
  (concat &amp;quot;file://&amp;quot; (expand-file-name &amp;quot;~/common-lisp/HyperSpec/&amp;quot;)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And if you configure Emacs to use the
&lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_mono/eww.html&#34;&gt;EWW&lt;/a&gt; web
browser, you can work with the CLHS without leaving your editor.&lt;/p&gt;
</description>

      
      <category>lisp</category>
      
      <category>emacs</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/reading-rfc-documents-in-emacs/">
      <title>Reading RFC documents in Emacs</title>
      <link>https://www.n16f.net/blog/reading-rfc-documents-in-emacs/</link>
      <pubDate>Sun, 02 Jun 2019 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/reading-rfc-documents-in-emacs/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;I am used to read RFC documents in Emacs by opening them as simple text
files. But I was always annoyed by the impossibility to follow references to
other documents. Additionally, searching for the right RFC usually involve
going back to the web browser and Google while I would prefer staying in
Emacs.&lt;/p&gt;
&lt;p&gt;Since I enjoy &lt;a href=&#34;https://github.com/emacs-helm/helm&#34;&gt;Helm&lt;/a&gt; so much, I thought it
would be easy to parse the index of all RFC documents, and create a browser
allowing me to pick a RFC. And since I was writing some elisp, I ended up
writing a major mode to highlight parts of RFC documents, including adding
links when another RFC is mentioned.&lt;/p&gt;
&lt;p&gt;So here is &lt;a href=&#34;https://github.com/galdor/rfc-mode&#34;&gt;rfc-mode&lt;/a&gt;. It is quite simple
for the time being, and I have a few ideas for more features, but it is
already quite useful.&lt;/p&gt;
&lt;p&gt;It is also available on &lt;a href=&#34;https://melpa.org/#/rfc-mode&#34;&gt;MELPA&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;./rfc-mode-browser.png&#34; alt=&#34;RFC browser&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;./rfc-mode-reader.png&#34; alt=&#34;RFC reader&#34;&gt;&lt;/p&gt;
</description>

      
      <category>emacs</category>
      
    </item>
    
    <item xml:base="https://www.n16f.net/blog/buffer-ordering-with-helm/">
      <title>Buffer ordering with Helm</title>
      <link>https://www.n16f.net/blog/buffer-ordering-with-helm/</link>
      <pubDate>Sun, 21 Oct 2018 18:00:00 +0000</pubDate>
      <guid isPermaLink="true">https://www.n16f.net/blog/buffer-ordering-with-helm/</guid>
      <author>nicolas@n16f.net (Nicolas Martyanoff)</author>

      <description>&lt;p&gt;I have been using Emacs for some time, and
&lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_mono/ido.html&#34;&gt;ido&lt;/a&gt; has
always been my favourite way to switch buffers.&lt;/p&gt;
&lt;p&gt;But recently I discovered and started using
&lt;a href=&#34;https://github.com/emacs-helm/helm&#34;&gt;Helm&lt;/a&gt;, a package based on incremental
completion which can provide lots of interesting features. One of them being
buffer switching.&lt;/p&gt;
&lt;p&gt;The simplest way to use Helm to switch buffers is
&lt;code&gt;helm-buffers-list&lt;/code&gt;. Personally I use &lt;code&gt;C-x b&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(global-set-key (kbd &amp;quot;C-x b&amp;quot;) &#39;helm-buffers-list)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I really like that &lt;code&gt;helm-buffer-list&lt;/code&gt; orders buffers using the last time they
were used. However as soon as I start typing something to narrow the
selection, Helm switches to a different ordering based on the length of buffer
names. A &lt;a href=&#34;https://github.com/emacs-helm/helm/issues/763&#34;&gt;bug report&lt;/a&gt; was
submitted, but apparently, this is the intended behaviour.&lt;/p&gt;
&lt;p&gt;Fortunately, it is possible to override this behaviour, as suggested in
another &lt;a href=&#34;https://github.com/emacs-helm/helm/issues/1492&#34;&gt;Github issue&lt;/a&gt;.
Ordering is done in the &lt;code&gt;helm-buffers-sort-transformer&lt;/code&gt; function; we could
replace it, but it is cleaner to
&lt;a href=&#34;https://www.gnu.org/software/emacs/manual/html_node/elisp/Advising-Functions.html&#34;&gt;advise&lt;/a&gt;
it.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-lisp&#34;&gt;(defun nm-around-helm-buffers-sort-transformer (candidates source)
  candidates)

(advice-add &#39;helm-buffers-sort-transformer
            :override #&#39;nm-around-helm-buffers-sort-transformer)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This way we keep the LRU-style ordering even when narrowing the selection.&lt;/p&gt;
</description>

      
      <category>emacs</category>
      
    </item>
    
  </channel>
</rss>
