main
  1<!DOCTYPE html>
  2<html lang="en">
  3<head>
  4<!-- Sep 03, 2024 -->
  5<meta charset="utf-8" />
  6<meta name="viewport" content="width=device-width, initial-scale=1" />
  7<title>emacs: keep it clean</title>
  8<meta name="author" content="Vincent Demeester" />
  9<meta name="generator" content="Org Mode" />
 10<link rel='icon' type='image/x-icon' href='/images/favicon.ico'/>
 11<meta name='viewport' content='width=device-width, initial-scale=1'>
 12<link rel='stylesheet' href='/css/new.css' type='text/css'/>
 13<link rel='stylesheet' href='/css/syntax.css' type='text/css'/>
 14<link href='/index.xml' rel='alternate' type='application/rss+xml' title='Vincent Demeester' />
 15</head>
 16<body>
 17<main id="content" class="content">
 18<header>
 19<h1 class="title">emacs: keep it clean</h1>
 20</header><p>
 21I want to keep the <code>~/.emacs.d</code> folder as clean as possible. The <a href="https://github.com/emacscollective/no-littering">no-littering</a> project
 22helps wit that.
 23</p>
 24
 25<blockquote>
 26<p>
 27The default paths used to store configuration files and persistent data are not consistent
 28across Emacs packages. This isn&rsquo;t just a problem with third-party packages but even with
 29built-in packages.
 30</p>
 31
 32<p>
 33Some packages put these files directly in user-emacs-directory or $HOME or in a
 34subdirectory of either of the two or elsewhere. Furthermore sometimes file names are used
 35that don&rsquo;t provide any insight into what package might have created them.
 36</p>
 37
 38<p>
 39This package sets out to fix this by changing the values of path variables to put
 40configuration files in no-littering-etc-directory (defaulting to ~/.emacs.d/etc/) and
 41persistent data files in no-littering-var-directory (defaulting to ~/.emacs.d/var/), and
 42by using descriptive file names and subdirectories when appropriate. This is similar to a
 43color-theme; a &ldquo;path-theme&rdquo; if you will.
 44</p>
 45</blockquote>
 46
 47<p>
 48Let&rsquo;s configure it <b>and</b> make sure we load it as soon as possible (hence the
 49<code>config/00-clean.el</code>).
 50</p>
 51
 52<p>
 53As I am loading <code>recentf</code> during this cleanup part, I need to setup recentf before 😅. In
 54a gist:
 55</p>
 56
 57<ul class="org-ul">
 58<li>I keep about 200 items.</li>
 59<li>I don&rsquo;t want the <i>auto-cleanup</i> of recentf items to happen when the mode is loaded (a.k.a.
 60at startup). It is configured to run after 360s of idle time.</li>
 61<li>I don&rsquo;t really want to show the Nth number of the items.</li>
 62<li>I don&rsquo;t want recentf to save remote, <code>su</code> and <code>sudo</code> items (<code>ssh:</code>, <code>sudo:</code>, …)</li>
 63</ul>
 64
 65<div class="org-src-container">
 66<pre class="src src-emacs-lisp">;;; 00-clean.el --- -*- lexical-binding: t; -*-
 67;;; Commentary:
 68;;; no-littering and recentf configurations
 69;;; Note: this file is autogenerated from an org-mode file.
 70;;; Code:
 71(use-package recentf
 72  :config
 73  (setq recentf-max-saved-items 200
 74        recentf-auto-cleanup 360
 75        recentf-show-file-shortcuts-flag nil)
 76  (recentf-mode 1)
 77  (add-to-list 'recentf-exclude "^/\\(?:ssh\\|su\\|sudo\\)?:")
 78  ;; Magic advice to rename entries in recentf when moving files in
 79  ;; dired.
 80  (defun rjs/recentf-rename-notify (oldname newname &amp;rest args)
 81    (if (file-directory-p newname)
 82        (rjs/recentf-rename-directory oldname newname)
 83      (rjs/recentf-rename-file oldname newname)))
 84
 85  (defun rjs/recentf-rename-file (oldname newname)
 86    (setq recentf-list
 87          (mapcar (lambda (name)
 88                    (if (string-equal name oldname)
 89                        newname
 90                      oldname))
 91                  recentf-list))
 92    recentf-cleanup)
 93
 94  (defun rjs/recentf-rename-directory (oldname newname)
 95    ;; oldname, newname and all entries of recentf-list should already
 96    ;; be absolute and normalised so I think this can just test whether
 97    ;; oldname is a prefix of the element.
 98    (setq recentf-list
 99          (mapcar (lambda (name)
100                    (if (string-prefix-p oldname name)
101                        (concat newname (substring name (length oldname)))
102                      name))
103                  recentf-list))
104    recentf-cleanup)
105
106  (advice-add 'dired-rename-file :after #'rjs/recentf-rename-notify))
107
108(use-package no-littering               ; Keep .emacs.d clean
109  :config
110  (require 'recentf)
111  (add-to-list 'recentf-exclude no-littering-var-directory)
112  (add-to-list 'recentf-exclude no-littering-etc-directory)
113
114  ;; Move this in its own thing
115  (setq
116   create-lockfiles nil
117   delete-old-versions t
118   kept-new-versions 6
119   kept-old-versions 2
120   version-control t)
121
122  (setq
123   backup-directory-alist
124   `((".*" . ,(no-littering-expand-var-file-name "backup/")))
125   auto-save-file-name-transforms
126   `((".*" ,(no-littering-expand-var-file-name "auto-save/") t))))
127
128(provide '00-clean)
129;;; 00-clean.el ends here
130</pre>
131</div>
132</main>
133<footer id="postamble" class="status">
134<footer>
135     <small><a href="/" rel="history">Index</a> • <a href="/sitemap.html">Sitemap</a> • <a href="https://dl.sbr.pm/">Files</a></small><br/>
136     <small class='questions'>Questions, comments ? Please use my <a href="https://lists.sr.ht/~vdemeester/public-inbox">public inbox</a> by sending a plain-text email to <a href="mailto:~vdemeester/public-inbox@lists.sr.ht">~vdemeester/public-inbox@lists.sr.ht</a>.</small><br/>
137     <small class='copyright'>
138      Content and design by Vincent Demeester
139      (<a rel='licence' href='http://creativecommons.org/licenses/by-nc-sa/3.0/'>Some rights reserved</a>)
140    </small><br />
141</footer>
142</footer>
143</body>
144</html>