20 KiB
Emacs configuration, literate-style
- Settings
- System-specific
- Appendices
Settings
Basic settings necessary for a decent editing experience in Emacs. These should not require non-built-in packages.
Prelude
Enable lexical binding
;; config.el -*- lexical-binding: t -*-
Disclaimer
;; This file is automatically tangled from config.org.
;; Hand edits will be overwritten!
Customization
Emulate use-package's :custom
(defmacro cuss (var val &optional _docstring)
"`use-package''s `:custom', without `use-package'."
(declare (doc-string 3)
(indent 2))
`(funcall (or (get ',var 'custom-set) #'set-default)
',var ,val))
Emulate use-package's :custom-face
(defvar acdw--custom-faces ()
"List of custom faces to run through `acdw/set-custom-faces'.")
(defun acdw/set-custom-faces ()
"Customize faces using `customize-set-faces'.
I only want to run this once, per the documentation of `customize-set-faces'."
(when acdw--custom-faces
(let ((msg "Customizing faces"))
(message "%s..." msg)
(apply #'custom-set-faces acdw--custom-faces)
(message "%s...Done." msg)
(remove-function after-focuse-change-function #'acdw/set-custom-faces))))
(add-function :before after-focus-change-function #'acdw/set-custom-faces)
(defmacro cussface (face spec &optional _docstring)
"Add the form (FACE SPEC) to `acdw--custom-faces'."
(declare (doc-string 3)
(indent defun))
`(add-to-list 'acdw--custom-faces '(,face ,spec)))
Only do something when Emacs is unfocused
Since Emacs is single-threaded, I only want to run really expensive operations when I won't notice, say .. when I'm focused on another window.
(defun when-unfocused (func &rest args)
"Run FUNC with ARGS iff all frames are out of focus."
(when (seq-every-p #'null (mapcar #'frame-focus-state (frame-list)))
(apply func args)))
Throw customizations away
I use Emacs's Customize interface, but really only to learn about what options a package presents to be customized. I don't want to use the custom file for anything at all.
(cuss custom-file null-device)
About me
My name and email address.
(setq user-full-name "Case Duckworth"
user-mail-address "acdw@acdw.net")
Look and feel
Cursor
;; Show a vertical bar cursor
(cuss cursor-type 'bar)
;; Hide the cursor in other windows
(cuss cursor-in-non-selected-windows nil)
;; Don't blink the cursor
(blink-cursor-mode -1)
Tabs
Tab names should be current buffer + a count of windows
(cuss tab-bar-tab-name-function
#'tab-bar-tab-name-current-with-count)
Only show the tab bar when there's more than one tab
For some reason, this doesn't work with multiple frames.
(cuss tab-bar-show 1)
Frames
Frames are Emacs's concepts that generally correspond to other programs' windows – that is, they're the boxen on the screen that contain the Emacs programmen.
Initial frame setup
Tool bar
(add-to-list 'default-frame-alist
'(tool-bar-lines . 0))
(tool-bar-mode -1)
Menu bar
(add-to-list 'default-frame-alist
'(menu-bar-lines . 0))
(menu-bar-mode -1)
Scroll bars
(add-to-list 'default-frame-alist
'(vertical-scroll-bars . nil)
'(horizontal-scroll-bars . nil))
(scroll-bar-mode -1)
(horizontal-scroll-bar-mode -1)
Frame titles
Set the frame title to something more useful than the default: include the current buffer and the current filename.
(cuss frame-title-format
(concat invocation-name "@" (system-name)
": %b %+%+ %f"))
Fringes
I have grown to love Emacs's little fringes on the side of the windows. In fact, I love them so much that I really went overboard and have made a custom fringe bitmap.
Indicate empty lines after the end of the buffer
(cuss indicate-empty-lines t)
Indicate the boundaries of the buffer
(cuss indicate-buffer-boundaries 'right)
Indicate continuation lines, but only on the left fringe
(cuss visual-line-fringe-indicators '(left-curly-arrow nil))
Customize fringe bitmaps
(define-fringe-bitmap 'left-curly-arrow
[#b11000000
#b01100000
#b00110000
#b00011000])
(define-fringe-bitmap 'right-curly-arrow
[#b00011000
#b00110000
#b01100000
#b11000000])
(define-fringe-bitmap 'left-arrow
[#b00000000
#b01010100
#b01010100
#b00000000])
(define-fringe-bitmap 'right-arrow
[#b00000000
#b00101010
#b00101010
#b00000000])
Windows
Winner mode
I don't really use winner-mode as of yet, but it seems like a really
good thing to have. It lets you move between window configurations
with C-c <-/->
.
(when (fboundp 'winner-mode)
(winner-mode +1))
Switch windows, or buffers if there's only one
from u/astoff1.
(defun other-window-or-buffer ()
"Switch to the other window, or previous buffer."
(interactive)
(if (eq (count-windows) 1)
(switch-to-buffer nil)
(other-window 1)))
Buffers
Startup buffers
I don't want to see Emacs's splash screen, and I want the *scratch*
buffer to have a little message.
(cuss inhibit-startup-screen t
"Don't show the startup buffer.")
(cuss initial-buffer-choice t
"Start with *scratch*.")
(cuss initial-scratch-message
(concat ";; Hello, " (nth 0 (split-string user-full-name)) "!\n"
";; Happy hacking ..."))
Immortal *scratch*
buffer
I don't want to accidentally kill the *scratch*
buffer.
(defun immortal-scratch ()
(if (eq (current-buffer) (get-buffer "*scratch*"))
(progn (bury-buffer)
nil)
t))
(add-hook 'kill-buffer-query-functions #'immortal-scratch)
Uniquify buffers
I like the forward
style, which uniquifies buffers by including path
elements up the tree until the names are unique.
(require 'uniquify)
(cuss uniquify-buffer-name-style 'forward)
Kill buffers more smarter-ly
(defun kill-a-buffer (&optional prefix)
"Kill buffers and windows sanely.
`kill-a-buffer' works based on the prefix argument as follows:
- 0 => kill the CURRENT buffer and window
- 4 (C-u) => kill the OTHER window and its buffer
- 16 (C-u C-u) => kill ALL OTHER buffers and windows
Prompt iff there are unsaved changes."
(interactive "P")
(pcase (or (car prefix) 0)
(0 (kill-current-buffer)
(unless (one-window-p) (delete-window)))
(4 (other-window 1)
(kill-current-buffer)
(unless (one-window-p) (delete-window)))
(16 (mapc #'kill-buffer (delq (current-buffer) (buffer-list)))
(delete-other-windows))))
Fonts
Function: set-face-from-alternatives
To be honest, this might be better off using cussface
, but that's
another story for another day.
(defun set-face-from-alternatives (face frame &rest fontspecs)
"Set FACE on FRAME from first available font from FONTSPECS.
FACE and FRAME work the same as with `set-face-attribute'."
(catch :return
(dolist (spec fontspecs)
(when-let ((found (find-font (apply #'font-spec spec))))
(set-face-attribute face frame :font found)
(throw :return found)))))
Add a hook to setup fonts after the first window focus change
Of course, I only need to setup the fonts on a graphical session.
(when (display-graphic-p)
(add-function :before after-focus-change-function #'acdw/setup-fonts))
Setup my fonts
Notice that this function removes itself from
after-focus-change-function
, since ideally you'll only need to set
the fonts once.
(defun acdw/setup-fonts ()
"Setup fonts.
This has to happen after the frame is setup for the first time,
so it should be added to `after-focus-change-function'. It
removes itself."
(set-face-from-alternatives 'default nil
'(:family "Input Mono"
:slant normal
:weight normal
:height 110)
'(:family "Consolas"
:slant normal
:weight normal
:height 100))
;; `fixed-pitch' should just inherit from `default'
(set-face-attribute 'fixed-pitch nil :inherit 'default)
(set-face-from-alternatives 'variable-pitch nil
'(:family "Input Sans"
:slant normal
:weight normal)
'(:family "Georgia"
:slant normal
:weight normal))
(remove-function after-focus-change-function #'acdw/setup-fonts))
Underlines
(cuss x-underline-at-descent-line t)
Interactivity
Dialogs and alerts
Don't use a dialog box
(cuss use-dialog-box nil)
Yes or no questions
(fset 'yes-or-no-p #'y-or-n-p)
The Bell
;; Don't flash the whole screen on bell
(cuss visible-bell nil)
;; Instead, flash the mode line
(cuss ring-bell-function #'flash-mode-line)
(defun flash-mode-line ()
(invert-face 'mode-line)
(run-with-timer 0.2 nil #'invert-face 'mode-line))
t*** Minibuffer
Minibuffer
Keep the cursor away from the minibuffer prompt
(cuss minibuffer-prompt-properties
'(read-only t cursor-intangible t face minibuffer-prompt))
Enable recursive minibuffer
(cuss enable-recursive-minibuffers t)
Show how deep the minibuffer goes in the modeline
(minibuffer-depth-indicate-mode +1)
Completing-read
Shadow file names
When typing ~
or /
in the file-selection dialog, Emacs "pretends"
that you've typed them at the beginning of the line. By default,
however, it only fades out the previous contents of the line. I
want to hide those contents.
(cuss file-name-shadow-properties '(invisible t))
(file-name-shadow-mode +1)
Ignore case
(cuss completion-ignore-case t)
(cuss read-buffer-completion-ignore-case t)
(cuss read-file-name-completion-ignore-case t)
Persistence
Minibuffer history
The savehist
package saves the minibuffer history between sessions.
It can also save some other variables alongside the minibuffer
history. Since storage is cheap, I'm also going to keep all my
history.
(require 'savehist)
(cuss savehist-additional-variables
'(kill-ring
search-ring
regexp-search-ring))
;; Don't truncate history
(cuss history-length t)
;; Delete history duplicates
(cuss history-delete-duplicates t)
(savehist-mode +1)
File places
The saveplace
package saves where I've been in the files I've
visited, so I can return back to them.
(require 'saveplace)
;; Forget the place in unreadable files
(cuss save-place-forget-unreadable-files t)
(save-place-mode +1)
Recent files
(require 'recentf)
;; Limit the number of items in the recentf menu
(cuss recentf-max-menu-items 100)
;; But not the number of items in the actual list
(cuss recentf-max-saved-items nil)
(recentf-mode +1)
Save the recentf list periodically
(defun acdw/maybe-save-recentf ()
"Save `recentf-file every five minutes, but only when out of focus."
(defvar recentf--last-save (time-convert nil 'integer)
"When we last ran `recentf-save-list'.")
(when (> (time-convert (time-since recentf--last-save) 'integer)
(* 60 5))
(setq recentf--last-save (time-convert nil 'integer))
(acdw/when-unfocused #'recentf-save-list)))
(add-function :after after-focus-change-function #'acdw/maybe-save-recentf)
Files
Encoding
System-specific
I use both Linux (at home) and Windows (at work). To make Emacs easier to use in both systems, I've included various system-specific settings and written some ancillary scripts.
Determine where I am
(defmacro when-at (conditions &rest commands)
"Run COMMANDS when at a specific place.
CONDITIONS are one of `:work', `:home', or a list beginning with
those and other conditions to check. COMMANDS are only run if
all CONDITIONS are met."
(declare (indent 1))
(let ((at-work (memq system-type '(ms-dos windows-nt)))
(at-home (memq system-type '(gnu gnu/linux gnu/kfreebsd))))
(pcase conditions
(:work `(when ',at-work ,@commands))
(:home `(when ',at-home ,@commands))
(`(:work ,others) `(when (and ',at-work ,others)
,@commands))
(`(:home ,others) `(when (and ',at-home ,others)
,@commands)))))
Linux
Settings
Scripts
em
Here's a wrapper script that'll start emacs --daemon
if there isn't
one, and then launch emacsclient
with the arguments. Install it to
your $PATH
somewhere.
if ! emacsclient -nc "$@"; then
emacs --daemon
emacsclient -nc "$@"
fi
emacsclient.desktop
I haven't really tested this yet, but it should allow me to open other files and things in Emacs. From taingram.
[Desktop Entry]
Name=Emacs Client
GenericName=Text Editor
Comment=Edit text
MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;
Exec=emacsclient -c %f
Icon=emacs
Type=Application
Terminal=false
Categories=Utility;TextEditor;
Windows
I use Windows at work, where I also don't have Admin rights. So I kind of fly-by-night there. Much of the ideas and scripts in this section come from termitereform on Github.
Settings
(when (memq system-type '(windows-nt ms-dos cygwin))
(setq w32-allow-system-shell t ; enable cmd.exe as shell
))
Scripts
Common variables
set HOME=%~dp0..\..\
set EMACS=%~dp0..\..\..\bin\runemacs.exe
Emacs Daemon
Either run this once at startup, or put a shortcut of it in the
Startup folder:
%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
.
<<w32-bat-common>>
"%EMACS%" --daemon
Emacs Client
This will try to connect to the daemon above. If that fails, it'll
run runemacs.exe
.
This is the main shortcut for running Emacs.
<<w32-bat-common>>
set EMACSC=%~dp0..\..\..\bin\emacsclientw.exe
"%EMACSC%" -n -c -a "%EMACS%" %*
Emacs Safe Start
This runs Emacs with the factory settings.
<<w32-bat-common>>
"%EMACS%" -Q %*
Emacs Debug
This runs Emacs with the --debug-init
option enabled.
<<w32-bat-common>>
"%EMACS%" --debug-init %*
Appendices
Emacs's files
init.el
The classic Emacs initiation file.
Use lexical binding when evaluating init.el
;; init.el -*- lexical-binding: t -*-
<<disclaimer>>
Prefer newer files to older files
(setq load-prefer-newer t)
Load the config
I keep most of my config in config.el
, which is tangled directly
from this file. This init just loads that file, either from lisp or
directly from Org if it's newer.
(let* (;; Speed up init
(gc-cons-threshold most-positive-fixnum)
(file-name-handler-alist nil)
;; Config file names
(conf (expand-file-name "config"
user-emacs-directory))
(conf-el (concat conf ".el"))
(conf-org (concat conf ".org")))
(unless (and (file-newer-than-file-p conf-el conf-org)
(load conf 'no-error))
;; A plain require here just loads the older `org'
;; in Emacs' install dir. We need to add the newer
;; one to the `load-path', hopefully that's all.
(add-to-list 'load-path (expand-file-name "straight/build/org"
user-emacs-directory))
(require 'org)
(org-babel-load-file conf-org)))
early-init.el
Beginning with 27.1, Emacs also loads an early-init.el
file, before
the package manager or the UI code. The Info says we should put as
little as possible in this file, so I only have what I need.
Don't byte-compile this file
;; early-init.el -*- no-byte-compile: t; -*-
<<disclaimer>>
Disable loading of package.el
I use straight.el
instead.
(setq package-enable-at-startup nil)
Don't resize the frame when loading fonts
(setq frame-inhibit-implied-resize t)
Resize frame by pixels
(setq frame-resize-pixelwise t)
Shoe-horned from elsewhere in config.org
A fundamental tension of literal programming is logical versus programmatic ordering. I understand that's a problem it's meant to solve but hey, maybe I'm not quite there yet. I feel that having this weird shoe-horning of other bits of my config here, in a backwater heading in an appendix, isn't quite the future I wanted. But it's what I have for now.
<<initial-frame-setup>>
License
Copyright <20> 2020 Case Duckworth <acdw@acdw.net>
This work is free. You can redistribute it and/or modify it under the
terms of the Do What the Fuck You Want To Public License, Version 2,
as published by Sam Hocevar. See the LICENSE
file, tangled from the
following source block, for details.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified copies of
this license document, and changing it is allowed as long as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
Note on the license
It's highly likely that the WTFPL is completely incompatible with the GPL, for what should be fairly obvious reasons. To that, I say:
SUE ME, RMS!