about summary refs log tree commit diff stats
path: root/config.org
blob: 2a9c97bc0845e026168e7f924857f4f39801c541 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#+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"
<<w32-bat-common>>
"%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
<<w32-bat-common>>
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"
<<w32-bat-common>>
"%EMACS%" -Q %*
#+end_src

**** Emacs Debug

This runs Emacs with the =--debug-init= option enabled.

#+begin_src bat :tangle "bin/Emacs Debug.cmd"
<<w32-bat-common>>
"%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 -*-
  <<disclaimer>>
#+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; -*-
  <<disclaimer>>
#+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
  <<initial-frame-setup>>
#+end_src

** License
:PROPERTIES:
:header-args: :tangle LICENSE :comments no
:END:

Copyright © 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.

#+begin_src text
  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.
#+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!*