Emacs config

Table of Contents

Welp, here it goes. This is a literal config for my emacs setup I will prbably copy-paste what I have in my literal config, and provide small, fun explanations along the way For now, prepare this to be not the best documented literal config you've seen. Also, if you just stumbled accross this at random, there is an easy tangle button at readme.org

1. Before we start lisping

This emacs config's dependencies are mostly managed by nix, therefore we have to make a nix file before we do any emacs configuring by ourselves We also add webkitgtk support for (eventual) web browsing support

let
  sources = import ./nix/sources.nix;
  packageOverlay = final: old: {
    emacs = old.emacs.override {
      withXwidgets = true;
    };
    emacsWithPackages = final.emacs.pkgs.withPackages;
  };
in
import sources.emacs {
  # put 'packageOverlay' in the overlays array for XWidgets (whenever they are stable i guess...)
  pkgs = import <nixpkgs> { overlays = [ ]; };
  configDir = ./emacsconfig;
}

2. Emacs configuring

Now the real configuring begins! for some backwards compat purposes I have decided to but different config subjects into different files This won't matter too much as the nix framework is expecting multiple files anyway!

2.1. The bases

This is only better-defaults. It comes with ido-mode and some other defaults that I forgot to set in my init file that I stole from emacs-bootstrap months ago

(use-package better-defaults)

2.1.1. Multiple terminals

By default, term only supports 1 terminal. This version supports multiple terminals

(defun new-term (program)
  "start a new terminal emulator in a new buffer"
  (interactive (list (read-from-minibuffer "Run program: "
                                           (or explicit-shell-file-name
                                               (getenv "ESHELL")
                                               shell-file-name))))
  (let* ((term-list (seq-filter
                     (lambda (s) (string-match-p "terminal" (buffer-name s)))
                     (buffer-list)))
         (term-num (number-to-string (length term-list))))
    (set-buffer (make-term (concat "terminal-" term-num) program))
    (term-mode)
    (term-char-mode)
    (switch-to-buffer (concat "*terminal-" term-num "*"))))

2.2. Generally nice emacs tools

2.2.1. Magit

Simple. Nice. default config. (almost)

(use-package magit
  :init
  ;; import missing function
  (defun seq-keep (function sequence)
    "Apply FUNCTION to SEQUENCE and return the list of all the non-nil results"
    (delq nil (seq-map function sequence)))
  :config
  (magit-add-section-hook 'magit-status-sections-hook
                          'magit-insert-modules
                          'magit-insert-stashes
                          'append)
  :bind
  ("C-x g" . magit-status))
(use-package forge
  :after magit)

2.2.2. IRC

I use IRC in emacs, with ERC. There is even a small macro so that I can easily join new servers with my preferred nickname

(use-package erc
  :custom
  (erc-server-reconnect-attempts 10)
  (erc-nick "crazazy")
  (erc-autojoin-channels-alist '(("tilde.chat"
                                  "#cafe"
                                  "#meta")
                                 ("libera.chat"
                                  "#emacs"
                                  "#haskell"
                                  "#systemcrafters")))
  :config
  (defmacro irc-quickjoin (servername url &optional nick)
    "create a function to quickly join a server. Servers can be joined with M-x SERVERNAME-irc"
    `(defun ,(intern (concat (symbol-name servername) "-irc")) (password)
       (interactive (list (password-read "Password: ")))
       (erc-tls :server ,url
                :nick ,(if nick
                           nick
                         "crazazy")
                :port 6697
                :password password)))

  (irc-quickjoin tilde "eu.tilde.chat")
  (irc-quickjoin libera "irc.libera.chat"))

2.2.3. Dashboard

Gives me access to the most recent files I edited, and some other stuff that I don't really care about

(use-package dashboard
  :config (dashboard-setup-startup-hook))

2.2.4. Elfeed

I don't use elfeed in emacs that much anymore, as I have switched to seamonkey for my RSS needs. Here is my old config anyways. If you want to browse through some default news feel free to remove the last :custom option

(use-package elfeed
  :bind
  ("C-x w" . elfeed)
  (:map elfeed-search-mode-map
        ("C-c a" . elfeed-add-feed)
        ("C-c u" . elfeed-update)
        ("C-c f" . elfeed-update-feed)
        ("C-c r" . elfeed-mark-all-as-read))
  :config
  (defun elfeed-mark-all-as-read ()
    (interactive)
    (mark-whole-buffer)
    (elfeed-search-untag-all-unread))
  :custom
  (elfeed-search-filter  "@6-months-ago +quality")
  (elfeed-feeds  '(("http://feeds.feedburner.com/tweakers/nieuws" NL tech exportable)
                   ("https://discourse.nixos.org/c/announcements/8.rss" nixos programming quality)
                   ("http://www.dnbradio.com/feeds" music podcasts)
                   ("https://codepen.io/spark/feed/" programming quality)
                   ("https://envs.net/~lucidiot/rsrsss/feed.xml" RSS quality)
                   ("https://falseknees.tumblr.com/rss" comics quality)
                   ("https://hackspace.raspberrypi.org/feed" tech programming)
                   ("https://lobste.rs/rss" programming)
                   ("https://news.rickcarlino.com/rss.rss" tech)
                   ("https://nu.nl/rss" NL news exportable)
                   ("https://planet.haskell.org/atom.xml" haskell programming)
                   ("https://planet.nixos.org/atom.xml" nixos programming quality)
                   ("https://reddit.com/r/dnb/.rss" reddit music)
                   ("https://reddit.com/r/programming/.rss" reddit programming)
                   ("https://reddit.com/r/realdubstep/.rss" reddit music)
                   ("https://reddit.com/r/thenetherlands/.rss" NL reddit)
                   ("https://sachachua.com/blog/category/emacs-news/feed" emacs quality programming)
                   ("https://webzine.puffy.cafe/atom.xml" openbsd tech quality programming)
                   ("https://www.fosskers.ca/en/rss" programming quality exportable)
                   ("https://xkcd.com/atom.xml" comics)))
  (elfeed-feeds nil)
  (elfeed-search-filter  ""))

2.2.5. Org mode

While the brunt of this file is relatively vanilla, There are still some things that I want to customize about the org-mode experience For now, I think it's best if I don't start depending on org-contrib, as it contains a lot of features and I would waste quite some time figuring out what all those features are.

First of all, auto-indenting isn't enabled everywhere, so let's do that

(use-package org
  :custom
  (org-adapt-indentation t))
  1. Syntax highlighting for HTML exports

    Normally, when I export my document to an html file for the website, syntax highlighting isn't automatically turned on. htmlize changes this by putting some colorful spans in all my source code. No need to configure anything either

    (use-package htmlize)
    
  2. Denote

    I used to want to try out org-roam, but nothing came of that. However, right now I do have notes in denote, and since emacs-ng doesn't work with denote anymore (since its stuck at version 28.0.50) I have to move to the default version of emacs

    (use-package denote
      :ensure t
      :custom
      (denote-directory "~/Documents/notes/")
      (denote-file-type nil)
      (denote-prompts '(title keywords))
      (denote-date-prompt-use-org-read-date t)
      :bind
      (("C-c n n" . denote)
       ("C-c n N" . denote-type)
       ("C-c n d" . denote-date)
       ("C-c n s" . denote-subdirectory)
       ("C-c n t" . denote-template)
       ("C-c n r" . denote-rename-file)
       :map org-mode-map
       ("C-c n i" . denote-link)
       ("C-c n I" . denote-link-add-links)
       ("C-c n l" . denote-link-find-file)
       ("C-c n b" . denote-link-backlinks)))
    

2.2.6. Evil mode

I am originally a vim user, and the standard vim bindings have not left my hands yet, I am not a big configurer, so this is mostly just the standard configuration that evil-mode advices you to do

(use-package evil
  :init
  (setq evil-want-integration t) ;; This is optional since it's already set to t by default.
  (setq evil-want-keybinding nil)
  :config
  (evil-define-key 'normal global-map "," 'evil-execute-in-god-state)
  (evil-mode 1))

(use-package evil-collection
  :after evil
  :config
  (evil-collection-init))
  1. God-mode

    To further prove that I have no intentions at all to be busy with making evil bindings for eveything I find I have also installed god-mode to help me do effortless keybinding for those programs that don't have evil support

    (use-package evil-god-state
      :bind
      ("M-," . evil-god-state-bail))
    
    (use-package god-mode)
    
  2. Tree-sitter

    There is a pre-configured package that allows for easy tree-sitter support for a few languages. Nixos + tree-sitter is surprisingly painless, so I figured I'd just add it

    (use-package evil-tree-edit
      :hook
      (java-mode . evil-tree-edit-mode)
      (python-mode . evil-tree-edit-mode))
    

2.2.7. Which-key

Has god-mode support

(use-package which-key
  :after god-mode
  :config
  (which-key-enable-god-mode-support)
  (which-key-mode))

2.2.8. PDF-tools

I don't want to be at the mercy of my browser to figure out how to read a PDF

(use-package pdf-tools
  :config
  (add-to-list 'auto-mode-alist '("\\.pdf$" . pdf-view-mode)))

2.3. Programming-related emacs tools

I do computer science, so from now and then I tend to program a bit. Here are all the tools I use to accomplish that stuff

2.3.1. Company

Generally nice for auto-completion anywhere. I think here is also the place to note that I am not a huge fan of language-servers, as I've had bad experiences with them when I was still using vim

(use-package company
  :hook
  (prog-mode . company-mode)
  :bind
  ("C-SPC" . company-complete))

2.3.2. Matching of parentheses and other stuff

"ERROR: Unmatched parenteses/braces/whatever" is really a thing I would prefer to never see again It has nix and lisp support, but other langages are fairly standards so not much configuration needed there

(use-package smartparens
  :hook
  (emacs-lisp-mode . smartparens-mode)
  (nix-mode . smartparens-mode)
  (haskell-mode . smartparens-mode)
  (lisp-mode . smartparens-mode)
  :config
  (sp-with-modes '(lisp-mode emacs-lisp-mode)
    (sp-local-pair "'" nil :actions nil)
    (sp-local-pair "`" nil :actions nil))

  (sp-with-modes '(haskell-mode)
    (sp-local-pair "'" nil :actions nil))
  (sp-with-modes '(nix-mode)
    (sp-local-pair "'" nil :actions nil)
    (sp-local-pair "''" "''")))

2.3.3. Languages

Other than that, there are some language-specific setups that I want to do,which are a bit more complicated than just "editing a file"

  1. Nix

    Nix is a fairly essential language here. Not just because I run NixOS, but also because it provides external packages (interpreters, compilers, tooling etc.) to the programs that need it

    1. Nix mode

      This config is just part of editing nix config. It is supposed to come with company support, but the auto-completion backend turns out to not be that great. We'll see what I do with it

      (use-package nix-mode
        :mode "\\.nix\\'"
        :config
        ;; the company-nix backend is not available in melpa, but has no new dependencies
        (unless (package-installed-p 'company-nix)
          (with-temp-buffer
            (url-insert-file-contents "https://github.com/NixOS/nix-mode/raw/master/nix-company.el")
            (eval-buffer)))
        (add-hook 'nix-mode-hook (lambda ()
                                   (set (make-local-variable 'company-backends)
                                        '((company-nix))))))
      
    2. Nix package management

      I use nix-sandbox for managing nix package for other languages. One of the functions doesn't work that well for me so I replaced it with something that does

      (use-package nix-sandbox
        :demand
        :config
        (defun nix-executable-find (sandbox executable)
          "finds an EXECUTABLE in SANDBOX"
          (set (make-local-variable 'exec-path) (nix-exec-path sandbox))
          (executable-find executable))
      

      Beyond that, there is a helper function that makes it easy to quickly define environments with the required packages for a programming language

      (defun intersperse (el ls) (if (cdr ls) `(,(car ls) ,el . ,(intersperse el (cdr ls))) ls))
      (defun nix-env-from-packages (name &rest packages)
        "create a nix environment from nix packages. returns the location of the environment"
        (interactive (append
                      (list (read-string "Environment name: " nil nil "nameless"))
                      (split-string (read-string "Packages: "))))
        (with-temp-buffer
          (insert (format "
              { pkgs ? import %s {}}:
              pkgs.mkShell {
              buildInputs = with pkgs;[
              %s
              ];
              }
                  " (or nix-nixpkgs-path "<nixpkgs>") (apply 'concat (intersperse "\n" packages))))
          (write-file (concat temporary-file-directory name "-env/shell.nix"))
          (nix-find-sandbox (concat temporary-file-directory name "-env")))))
      
  2. Python

    Python is a bit weird. It had no intentions at all to do things the way I wanted it to do things with use-package, so I had to find a weird work-arounds with add-hook and all that stuff

    (add-hook 'python-mode-hook (lambda ()
                                  <<el-python-config>>))
    
    

    First we set an interpreter with nix, it comes with all the python autocompletion tools we need

    (setq python-shell-interpreter
          (nix-executable-find
           (nix-env-from-packages "python" "(python3.withPackages (p: with p; [pygame virtualenvwrapper pip sqlite jedi flake8 yapf autopep8 black]))")
           "python"))
    
    

    Then we introduce elpy with so that it can use all the tools immidiately without downloading them

    (use-package elpy
      :config
      (elpy-enable)
      (setq elpy-rpc-python-command python-shell-interpreter))
    
    
  3. Haskell

    I have to use haskell in my new module, so there is now some haskell infra for that first we have to set up a nix environment for our haskell tools

    
    

    Then haskell-mode setup, which integrates company mode as well as the sandbox

    (use-package haskell-mode
      :mode "\\.hs\\'"
      :after nix-sandbox
      :hook
      (haskell-mode . set-haskell-company-backends)
      (haskell-mode . haskell-indentation-mode)
      :config
      (setq haskell-env (nix-env-from-packages "Haskell"
                                               "ghc"
                                               "cabal-install"
                                               "haskellPackages.fourmolu"))
      (setq haskell-process-wrapper-function
            (lambda (args)
              (cons
               (nix-executable-find haskell-env (car args))
               (cdr args))))
      (defun set-haskell-company-backends ()
        (set (make-local-variable 'company-backends)
             '((dante-company company-files)))))
    

    ormolu formats my haskell files, though I use fourmolu since I prefer prefixing chained infix functions

    (use-package ormolu
      :after haskell-mode
      :hook
      (haskell-mode . ormolu-format-on-save-mode)
      (haskell-mode . (lambda ()
                        (set (make-local-variable 'ormolu-process-path)
                             (nix-executable-find haskell-env "fourmolu"))))
      :bind
      (:map haskell-mode-map
            ("C-c r" . ormolu-format-buffer)))
    

    Finally, dante runs as background interpreter of my haskell codes and checks stuff for errors

    (use-package dante
      :after haskell-mode
      :hook
      (haskell-mode . dante-mode)
      :custom
      ;; (dante-repl-command-line (list (nix-executable-find haskell-env "ghci")))
      (dante-methods '(nix-ghci bare-cabal bare-ghci)))
    
  4. Web stuff

    I also have made some basic webpages. I haven't gotten too deep into JS with emacs yet, so this section might expand later, but so far this is what I have

    1. Web mode

      General xml stuff editing. Fairly standard

      (use-package web-mode
        :mode (("\\.x?html?\\'" . web-mode)
               ("\\.x[sm]l\\'"  . web-mode)
               ("\\.css\\'"     . web-mode)
               ("\\.jsx?\\'"    . web-mode)
               ("\\.tsx?\\'"    . web-mode)
               ("\\.json\\'"    . web-mode))
        :custom
        (web-mode-markup-indent-offset 2) ; HTML
        (web-mode-css-indent-offset 2)    ; CSS
        (web-mode-code-indent-offset 2)   ; JS/JSX/TS/TSX
        (web-mode-content-types-alist '(("jsx" . "\\.js[x]?\\'"))))
      (use-package elnode)
      
      
    2. Emmet mode

      for quickly inserting a whole XML tree

      (use-package emmet-mode
        :hook
        (sgml-mode . emmet-mode)
        (web-mode . emmet-mode)
        (css-mode . emmet-mode)
        :bind
        (:map emmet-mode-keymap
              ("TAB" . emmet-dwim))
        :config
        (defun emmet-dwim (prefix)
          (interactive "p")
          (or
           (emmet-go-to-edit-point prefix)
           (emmet-expand-line prefix)
           (evil-jump-forward prefix))))
      

2.4. Other, less useful stuff

2.4.1. Themes

I like to have a nice theme in my setup

(load-theme 'leuven)

Also I want to theme my config exports

(use-package htmlize)

2.4.2. Discord

My discord buddies have to know I'm using emacs whenever I have my computer running

(use-package elcord
  :config
  (elcord-mode))

2.4.3. Emenu

Since I'm using emacs as a daemon, it can also be benefical to make an application launcer

(defun all-commands ()
  "does a completing read of all the programs accessible to you in $PATH"
  (let ((ls-output (mapcan (lambda (path)
                             (when (file-readable-p path)
                               (cl-remove-if (lambda (file)
                                               (let ((fullpath (concat path "/" file)))
                                                 (or (file-directory-p fullpath)
                                                     (not (file-executable-p fullpath)))))
                                             (directory-files path nil directory-files-no-dot-files-regexp nil))))
                           (split-string (getenv "PATH") ":" t)))
        (alias-output (split-string (shell-command-to-string "alias -p | sed -E 's/^alias ([^=]*)=.*$/\\1/'") "\n")))
    (append ls-output alias-output)))

(defun emenu (&optional command-list)
  "A dmenu-inspired application launcher using a separate emacs frame"
  (interactive)
  (with-selected-frame (make-frame '((name . "emenu")
                                     (minibuffer . only)
                                     (width . 151)
                                     (height . 1)))
    (unwind-protect
        (progn
          (call-process
           (ido-completing-read "Command: " (or
                                             command-list
                                             (all-commands)))
           nil
           0)
          (keyboard-quit))
      (delete-frame))))

Author: Crazazy

Created: 2024-06-24 Mon 13:28

Validate