-
Notifications
You must be signed in to change notification settings - Fork 0
/
2023-09-24-defining-a-formatter-for-cabal-files.html
75 lines (66 loc) · 4.42 KB
/
2023-09-24-defining-a-formatter-for-cabal-files.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="alternate"
type="application/rss+xml"
href="https://magnus.therning.org/feed.xml"
title="RSS feed for https://magnus.therning.org/">
<title>Defining a formatter for Cabal files</title>
<meta name="author" content="Magnus Therning"><meta name="referrer" content="no-referrer"><link href= "static/style.css" rel="stylesheet" type="text/css" /><link href= "static/htmlize.css" rel="stylesheet" type="text/css" /><link href= "static/extra_style.css" rel="stylesheet" type="text/css" /></head>
<body>
<div id="preamble" class="status"><div class="nav-bar"><a class="nav-link" href="./index.html">Top</a><a class="nav-link" href="./archive.html">Archive</a><a class="nav-link align-right" href="./feed.xml"><img src="static/rss-feed-icon.png" style="height: 24px;" /></a></div></div>
<div id="content">
<div class="post-date">24 Sep 2023</div><h1 class="post-title"><a href="https://magnus.therning.org/2023-09-24-defining-a-formatter-for-cabal-files.html">Defining a formatter for Cabal files</a></h1>
<p>
For Haskell code I can use <code>lsp-format-buffer</code> and <code>lsp-format-region</code> to keep
my file looking nice, but I've never found a function for doing the same for
Cabal files. There's a nice command line tool, <code>cabal-fmt</code>, for doing it, but it
means having to jump to a terminal. It would of course be nicer to satisfy my
needs for aesthetics directly from Emacs. A few times I've thought of writing
the function myself, I mean how hard can it be? But then I've forgotten about it
until then next time I'm editing a Cabal file.
</p>
<p>
A few days ago I noticed <a href="https://github.com/purcell/emacs-reformatter">emacs-reformatter</a> popping up in my feeds. That
removed all reasons to procrastinate. It turned out to be very easy to set up.
</p>
<p>
The package doesn't have a recipe for <a href="https://github.com/radian-software/straight.el">straight.el</a> so it needs a <code>:straight</code>
section. Also, the naming of the file in the package doesn't fit the package
name, hence the slightly different name in the <code>use-package</code> declaration:<sup><a id="fnr.1" class="footref" href="#fn.1" role="doc-backlink">1</a></sup>
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-keyword">use-package</span> <span class="org-constant">reformatter</span>
<span class="org-builtin">:straight</span> <span class="org-rainbow-delimiters-depth-2">(</span><span class="org-builtin">:host</span> github
<span class="org-builtin">:repo</span> <span class="org-string">"purcell/emacs-reformatter"</span><span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
<p>
Now the formatter can be defined
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-keyword">reformatter-define</span> cabal-format
<span class="org-builtin">:program</span> <span class="org-string">"cabal-fmt"</span>
<span class="org-builtin">:args</span> '<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-string">"/dev/stdin"</span><span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
<p>
in order to create functions for formatting, <code>cabal-format-buffer</code> and
<code>cabal-format-region</code>, as well as a minor mode for formatting on saving a Cabal
file.
</p>
<div id="footnotes">
<h2 class="footnotes">Footnotes: </h2>
<div id="text-footnotes">
<div class="footdef"><sup><a id="fn.1" class="footnum" href="#fnr.1" role="doc-backlink">1</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">
I'm sure it's possible to use <code>:files</code> to deal with this, but I'm not sure
how and my naive guess failed. It's OK to be like this until I figure it out
properly.
</p></div></div>
</div>
</div><div class="taglist"><a href="https://magnus.therning.org/tags.html">Tags</a>: <a href="https://magnus.therning.org/tag-emacs.html">emacs</a> <a href="https://magnus.therning.org/tag-haskell.html">haskell</a> </div>
<div id="comments">Comment <a href=https://www.reddit.com/r/emacs/comments/16qt1m1/defining_a_formatter_for_cabal_files/>here</a>.</div></div>
<div id="postamble" class="status"><!-- org-static-blog-page-postamble --></div>
</body>
</html>