#+TITLE: Emacs configuration, literate-style #+AUTHOR: Case Duckworth #+PROPERTY: header-args :tangle yes :tangle-mode (identity #o444) :comments both :mkdirp yes * Settings Basic settings necessary for a decent editing experience in Emacs. These should not require non-built-in packages. ** Prelude *** Enable lexical binding #+begin_src emacs-lisp :comments no ;; config.el -*- lexical-binding: t -*- #+end_src *** Disclaimer #+NAME: disclaimer #+begin_src emacs-lisp :comments no ;; This file is automatically tangled from config.org. ;; Hand edits will be overwritten! #+end_src ** Customization *** Emulate use-package's =:custom= #+begin_src emacs-lisp (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)) #+end_src *** Emulate use-package's =:custom-face= #+begin_src emacs-lisp (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', and add a hook to run `acdw/set-custom-faces' after init." (declare (doc-string 3) (indent 2)) `(add-to-list acdw--custom-faces '(,face ,spec))) #+end_src *** 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. #+begin_src emacs-lisp (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))) #+end_src *** 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. #+begin_src emacs-lisp (cuss custom-file null-device) #+end_src ** About me My name and email address. #+begin_src emacs-lisp (setq user-full-name "Case Duckworth" user-mail-address "acdw@acdw.net") #+end_src ** Look and feel *** Cursor #+begin_src emacs-lisp ;; 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) #+end_src *** Dialogs and alerts **** Don't use a dialog box #+begin_src emacs-lisp (cuss use-dialog-box nil) #+end_src **** Yes or no questions #+begin_src emacs-lisp (fset 'yes-or-no-p #'y-or-n-p) #+end_src **** The Bell #+begin_src emacs-lisp ;; 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)) #+end_src t*** Minibuffer **** Keep the cursor away from the minibuffer prompt #+begin_src emacs-lisp (cuss minibuffer-prompt-properties '(read-only t cursor-intangible t face minibuffer-prompt)) #+end_src *** Tabs **** Tab names should be current buffer + a count of windows #+begin_src emacs-lisp (cuss tab-bar-tab-name-function #'tab-bar-tab-name-current-with-count) #+end_src **** Only show the tab bar when there's more than one tab For some reason, this doesn't work with multiple frames. #+begin_src emacs-lisp (cuss tab-bar-show 1) #+end_src *** 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 :PROPERTIES: :header-args: :noweb-ref initial-frame-setup :END: ***** Tool bar #+begin_src emacs-lisp (add-to-list 'default-frame-alist '(tool-bar-lines . 0)) (tool-bar-mode -1) #+end_src ***** Menu bar #+begin_src emacs-lisp (add-to-list 'default-frame-alist '(menu-bar-lines . 0)) (menu-bar-mode -1) #+end_src ***** Scroll bars #+begin_src emacs-lisp (add-to-list 'default-frame-alist '(vertical-scroll-bars . nil) '(horizontal-scroll-bars . nil)) (scroll-bar-mode -1) (horizontal-scroll-bar-mode -1) #+end_src **** Frame titles Set the frame title to something more useful than the default: include the current buffer and the current filename. #+begin_src emacs-lisp (cuss frame-title-format (concat invocation-name "@" (system-name) ": %b %+%+ %f")) #+end_src **** 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 #+begin_src emacs-lisp (cuss indicate-empty-lines t) #+end_src ***** Indicate the boundaries of the buffer #+begin_src emacs-lisp (cuss indicate-buffer-boundaries 'right) #+end_src ***** Indicate continuation lines, but only on the left fringe #+begin_src emacs-lisp (cuss visual-line-fringe-indicators '(left-curly-arrow nil)) ;; And make the `left-curly-arrow' indicator less distracting. (define-fringe-bitmap 'left-curly-arrow [#b11000000 #b01100000 #b00110000 #b00011000]) #+end_src *** Windows *** Buffers * 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 #+begin_src emacs-lisp (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))))) #+end_src ** Linux *** Settings *** Scripts **** em :PROPERTIES: :header-args: :tangle-mode (identity #o755) :tangle bin/em :END: 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. #+begin_src sh :shebang "#!/bin/sh" if ! emacsclient -nc "$@"; then emacs --daemon emacsclient -nc "$@" fi #+end_src **** emacsclient.desktop :PROPERTIES: :header-args: :tangle bin/emacsclient.desktop :END: I haven't really tested this yet, but it should allow me to open other files and things in Emacs. From [[https://www.taingram.org/blog/emacs-client.html][taingram]]. #+begin_src conf-desktop [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; #+end_src ** 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 [[https://github.com/termitereform/JunkPile/blob/master/emacs-on-windows.md][termitereform]] on Github. *** Settings #+NAME: w32-settings #+begin_src emacs-lisp (when (memq system-type '(windows-nt ms-dos cygwin)) (setq w32-allow-system-shell t ; enable cmd.exe as shell )) #+end_src *** Scripts :PROPERTIES: :header-args: :noweb tangle :END: **** Common variables #+NAME: w32-bat-common #+begin_src bat set HOME=%~dp0..\..\ set EMACS=%~dp0..\..\..\bin\runemacs.exe #+end_src **** 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=. #+begin_src bat :tangle "bin/Emacs Daemon.cmd" <> "%EMACS%" --daemon #+end_src **** 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.* #+begin_src bat :tangle bin/Emacs.cmd <> set EMACSC=%~dp0..\..\..\bin\emacsclientw.exe "%EMACSC%" -n -c -a "%EMACS%" %* #+end_src **** Emacs Safe Start This runs Emacs with the factory settings. #+begin_src bat :tangle "bin/Emacs Safe Start.cmd" <> "%EMACS%" -Q %* #+end_src **** Emacs Debug This runs Emacs with the =--debug-init= option enabled. #+begin_src bat :tangle "bin/Emacs Debug.cmd" <> "%EMACS%" --debug-init %* #+end_src * Appendices ** Emacs's files *** init.el :PROPERTIES: :header-args: :tangle init.el :comments both :END: The classic Emacs initiation file. **** Use lexical binding when evaluating =init.el= #+begin_src emacs-lisp :comments no :noweb tangle ;; init.el -*- lexical-binding: t -*- <> #+end_src **** Prefer newer files to older files #+begin_src emacs-lisp (setq load-prefer-newer t) #+end_src **** 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. #+begin_src emacs-lisp (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))) #+end_src *** early-init.el :PROPERTIES: :header-args: :tangle early-init.el :comments both :END: 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 #+begin_src emacs-lisp :comments no :noweb tangle ;; early-init.el -*- no-byte-compile: t; -*- <> #+end_src **** Disable loading of =package.el= I use =straight.el= instead. #+begin_src emacs-lisp (setq package-enable-at-startup nil) #+end_src **** Don't resize the frame when loading fonts #+begin_src emacs-lisp (setq frame-inhibit-implied-resize t) #+end_src **** Resize frame by pixels #+begin_src emacs-lisp (setq frame-resize-pixelwise t) #+end_src **** 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. #+begin_src emacs-lisp :noweb tangle <> #+end_src ** License :PROPERTIES: :header-args: :tangle LICENSE :comments no :END: Copyright © 2020 Case Duckworth 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. #+begin_src text DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2004 Sam Hocevar 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. #+end_src *** 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!*