Files
dotfiles/.emacs.d/config.org
T
2025-04-23 20:58:15 +01:00

41 KiB

Emacs Config

Straight Package Manager

I'm using the Straight package manager instead of use-package because it is only available in Emacs 29 and above.

  (defvar bootstrap-version)
  (let ((bootstrap-file
         (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
        (bootstrap-version 6))
    (unless (file-exists-p bootstrap-file)
      (with-current-buffer
          (url-retrieve-synchronously
           "https://raw.githubusercontent.com/radian-software/straight.el/develop/install.el"
           'silent 'inhibit-cookies)
        (goto-char (point-max))
        (eval-print-last-sexp)))
    (load bootstrap-file nil 'nomessage))

  (setq straight-use-package-by-default t)
  (setq package-native-compile t)

Configs

Custom Functions

Cursor follow on split

This is so that action can be taken as soon as a split takes place, to either close/move the window or perform an action inside of it.

  (defun split-and-follow-horizontally()
    (interactive)
    (split-window-below)
    (balance-windows)
    (other-window 1))

  (defun split-and-follow-vertically()
    (interactive)
    (split-window-right)
    (balance-windows)
    (other-window 1))

Duplicate Line

Duplicates the current line to the line below.

  (defun duplicate-line ()
    (interactive)
    (let ((column (- (point) (point-at-bol)))
    (line (let ((s (thing-at-point 'line t)))
      (if s (string-remove-suffix "\n" s) ""))))
      (move-end-of-line 1)
      (newline)
      (insert line)
      (move-beginning-of-line 1)
      (forward-char column)))

  (keymap-global-set "C-c y d" 'duplicate-line)

Open Line above and below

Vim's o and S-o implemented for Emacs bindings.

  (defun vi-open-line-above ()
    (interactive)
    (unless (bolp)
      (beginning-of-line))
    (newline)
    (forward-line -1)
    (indent-according-to-mode))

  (defun vi-open-line-below ()
    (interactive)
    (unless (eolp)
      (end-of-line))
    (newline-and-indent))

  (keymap-global-set "C-c O" 'vi-open-line-below)
  (keymap-global-set "C-c o" 'vi-open-line-above)

C-a to indentation

Have c-a move the cursor to the start after indentation unless already there.

  (defun smart-beginning-of-line ()
    (interactive)
    (let ((initial-point (point)))
      (back-to-indentation)
      (when (eq initial-point (point))
        (move-beginning-of-line 1))))

  (defun setup-prog-mode-c-a ()
    (local-set-key (kbd "C-a") 'smart-beginning-of-line))

  (add-hook 'prog-mode-hook 'setup-prog-mode-c-a)

Automatically Cull Whitespace

Remove trailing whitespaces when saving in prog-modes.

  (defun prog-nuke-trailing-whitespace ()
    (when (derived-mode-p 'prog-mode)
      (delete-trailing-whitespace)))

  (add-hook 'before-save-hook 'prog-nuke-trailing-whitespace)

Quick Grep

Quickly grep both my current project and the system include directory.

  (defun grep-symbol-at-point ()
    (interactive)
    (let ((symbol (thing-at-point 'symbol)))
      (if symbol
          (counsel-rg (concat symbol))
        (message "No symbol at point."))))

  (defun grep-symbol-in-include-at-point ()
    (interactive)
    (let ((symbol (thing-at-point 'symbol)))
      (if symbol
          (counsel-rg (concat symbol) "/usr/include/")
        (message "No symbol at point."))))

  (global-set-key (kbd "C-c g") 'grep-symbol-at-point)
  (global-set-key (kbd "C-c G") 'grep-symbol-in-include-at-point)

Fix Annoyances

Add Scroll Marginn

This adds a scroll margin at the top and bottom of 10 lines to make it easier to scroll through the buffer.

  (setq scroll-margin 10)

Disable Backups

Living on the edge.

  (setq backup-directory-alist
  `(("." . ,(concat user-emacs-directory "backups"))))
  (setq auto-save-file-name-transforms
  `(("." ,(concat user-emacs-directory "autosaves/") t)))
  (setq lock-file-name-transforms
  `(("." ,(concat user-emacs-directory "lock") t)))

Disable Bell

Only flash, no sound, it gets annoying.

  (setq ring-bell-function 'ignore)

Hide Warnings

Fixing warnings is for nerds. This is basically necessary after more than 5 packages.

  (setq warning-minimum-level :emergency)

Local Bin

Add the local bin directory to PATH.

  (setenv "PATH" (concat (concat
                            (concat (expand-file-name "~/.local/bin") ":")
                              (getenv "PATH"))))

    (setq exec-path (append exec-path (list (expand-file-name "~/.local/bin"))))

Save Place

Go back to old position on file open.

  (save-place-mode 1)

Keybindings

Reload Emacs

Reload Emacs by sourcing the init.el file.

  (keymap-global-set "C-c e c" (lambda () (interactive) (load-file "~/.emacs.d/init.el")))

Open Common Files

Open the TODO, Bookmarks and Notes from synced Documents and the config file with simple shortcuts.

  (keymap-global-set "C-c f c" (lambda () (interactive) (find-file "~/.emacs.d/config.org")))
  (keymap-global-set "C-c f t" (lambda () (interactive) (find-file "~/Documents/TODO.org")))
  (keymap-global-set "C-c f b" (lambda () (interactive) (find-file "~/Documents/Bookmarks.org")))
  (keymap-global-set "C-c f n" (lambda () (interactive) (find-file "~/Documents/Notes.org")))
  (keymap-global-set "C-c f i" (lambda () (interactive) (find-file "~/Documents/Ideas.org")))

Switch and Revert Buffers

Add shortcuts to switch buffers and to revert buffer.

  (keymap-global-set "C-x C-b" 'ibuffer)
  (keymap-global-set "C-c b p" 'previous-buffer)
  (keymap-global-set "C-c b n" 'next-buffer)
  (keymap-global-set "C-c b r" 'revert-buffer)

Compile Mode

Add shortcuts to build in compile-mode.

  (setq compilation-scroll-output t)
  (keymap-global-set "C-c m c" 'compile)
  (keymap-global-set "C-c m r" 'recompile)

Emacs

Add shortcuts to various Emacs functions.

  (keymap-global-set "C-c e b" 'eval-buffer)
  (keymap-global-set "C-c e r" 'eval-region)
  (keymap-global-set "C-c e s" 'eshell)
  (keymap-global-set "C-c e t" 'theme-toggle)

Regex and Copy from Above

Add shortcuts to entering a regex replace and copying a line from above.

  (keymap-global-set "C-c r" 'replace-regexp)
  (keymap-global-set "C-c y a" 'copy-from-above-command)

Reverse C-x o

Add a reversed C-x o to go to previous window.

  (keymap-global-set "C-x O" 'previous-multiframe-window)

Window Management

Add shortcuts to scrolling and moving between windows.

  (keymap-global-set "C-c v" 'scroll-other-window)
  (keymap-global-set "C-c V" 'scroll-other-window-down)

  (keymap-global-set "C-c w n" 'other-window)
  (keymap-global-set "C-c w f" 'other-window)
  (keymap-global-set "C-c w p" (lambda () (interactive) (other-window -1)))
  (keymap-global-set "C-c w b" (lambda () (interactive) (other-window -1)))

Setup Line Numbers

Always show line numbers

Show line numbers and highlight the current line.

  (add-hook 'prog-mode-hook 'display-line-numbers-mode)
  (global-visual-line-mode t)
  (add-hook 'prog-mode-hook (lambda () (visual-line-mode -1)))
  (setq-default truncate-lines t)
  (global-hl-line-mode 1)

Generally use spaces instead of tabs

Generally, as in everywhere but Go.

  (setq-default indent-tabs-mode nil)
  (setq tab-width 2)
  (setq tab-stop-list (number-sequence 2 100 2))

Highlight Column

Highlight a column at 120 chars to respect GNOME style.

  (setq-default display-fill-column-indicator-column 120)
  (add-hook 'prog-mode-hook #'display-fill-column-indicator-mode)

General

Avy

Keybind superfast cursor movement to "C-q".

  (use-package avy
    :config
    (setq avy-keys '(?a ?r ?s ?t ?n ?e ?i ?o)
          avy-all-windows nil)
    (bind-key* "C-q" 'avy-goto-char))

Colors

Highlight Todo

Highlight TODO items so they can be reviewed later.

  (use-package hl-todo
    :hook ((org-mode . hl-todo-mode)
           (prog-mode . hl-todo-mode))

    :config
    (setq hl-todo-highlight-punctuation ":"
          hl-todo-highlight-faces
          `(("TODO"       warning bold)
            ("FIXME"      error bold)
            ("HACK"       font-lock-constant-face bold)
            ("REVIEW"     font-lock-doc-face bold)
            ("NOTE"       success bold)
            ("DEPRECATED" font-lock-doc-face bold))))

Rainbow Delimiters

Makes different brackets and other delimeters levels different colors so they can be quickly distinguished.

  (use-package rainbow-delimiters
    :hook (prog-mode . rainbow-delimiters-mode))

Dired

A powerful built-in file manager.

  (setq dired-listing-switches "-lh")

EShell

A built in posix shell.

  ;; Define custom faces for the prompt. These inherit from standard faces so they
  ;; automatically adapt to your current theme.
  (defface eshell-prompt-user-face
    '((t (:inherit font-lock-keyword-face)))
    "Face for the username in the eshell prompt."
    :group 'eshell-prompt)

  (defface eshell-prompt-venv-face
    '((t (:inherit font-lock-string-face)))
    "Face for the virtualenv name in the eshell prompt."
    :group 'eshell-prompt)

  (defface eshell-prompt-dir-face
    '((t (:inherit dired-directory)))
    "Face for the directory path in the eshell prompt."
    :group 'eshell-prompt)

  (defface eshell-prompt-git-face
    '((t (:inherit magit-tag)))
    "Face for the git branch in the eshell prompt."
    :group 'eshell-prompt)

  (defface eshell-prompt-git-dirty-face
    '((t (:inherit eshell-prompt-git-face)))
    "Face for the git dirty status in the eshell prompt."
    :group 'eshell-prompt)

  (defface eshell-prompt-symbol-face
    '((t (:inherit font-lock-builtin-face)))
    "Face for the prompt symbol (e.g. λ) in the eshell prompt."
    :group 'eshell-prompt)

  ;; A helper function to abbreviate long directory names.
  (defun my-abbreviate-dir (dir)
    "Abbreviate DIR to show only the first and the last two components.
  For example, \"~/Projects/Learning/C/GeneticsProject/TsodingGenetics\" becomes
  \"~/.../GeneticsProject/TsodingGenetics\"."
    (let* ((abbrev (abbreviate-file-name dir))
           (components (split-string abbrev "/" t))  ; t omits empty strings
           (num-components (length components)))
      (if (>= num-components 4)
          (concat (car components) "/.../"
                  (mapconcat #'identity (last components 2) "/"))
        abbrev)))

  ;; These are the same helper functions for git information you had.
  (defun my-prompt-git-branch ()
    "Return the current git branch or short commit hash, if available."
    (when (and (executable-find "git")
               (locate-dominating-file default-directory ".git"))
      (with-temp-buffer
        (let ((ret (call-process "git" nil t nil "symbolic-ref" "--short" "HEAD")))
          (if (zerop ret)
              (string-trim (buffer-string))
            (when (zerop (call-process "git" nil t nil "rev-parse" "--short" "HEAD"))
              (string-trim (buffer-string))))))))

  (defun my-prompt-git-dirty ()
    "Return a dirty flag (✗ if dirty, ✓ if clean) for the git repository."
    (when (my-prompt-git-branch)
      (with-temp-buffer
        (call-process "git" nil t nil "status" "--porcelain")
        (if (> (buffer-size) 0) "✗" "✓"))))

  ;; Finally, build the prompt itself.
  (defun eshell-prompt ()
    "Custom eshell prompt with theme-derived colors and shortened directory path."
    (let* ((user (propertize (user-login-name) 'face 'eshell-prompt-user-face))
           (venv (when-let ((venv (getenv "VIRTUAL_ENV")))
                   (concat (propertize "(" 'face 'eshell-prompt-venv-face)
                           (propertize (file-name-nondirectory venv) 'face 'eshell-prompt-venv-face)
                           (propertize ")" 'face 'eshell-prompt-venv-face))))
           (path (propertize (my-abbreviate-dir (eshell/pwd)) 'face 'eshell-prompt-dir-face))
           (git-branch (my-prompt-git-branch))
           (git-info (when git-branch
                       (concat " " (propertize "on" 'face 'font-lock-number-face) " "
                               (propertize git-branch 'face 'eshell-prompt-git-face)
                               (propertize (my-prompt-git-dirty) 'face 'eshell-prompt-git-dirty-face))))
           (prompt (concat user " " (propertize "in" 'face 'font-lock-number-face) " "
                           (if venv (concat venv " ") "")
                           path git-info "\n"
                           (propertize "λ" 'face 'eshell-prompt-symbol-face) " ")))
      prompt))

  ;; Tell eshell to use our prompt function.
  (setq eshell-prompt-function 'eshell-prompt)
  (setq eshell-prompt-regexp "^[^λ\n]*λ ")

Git

Tools for the primary version control system.

Magit

A very extensive Git GUI for Emacs.

  (use-package magit
    :after nerd-icons
    :custom
    (magit-format-file-function #'magit-format-file-nerd-icons))

Magit Todos

Show Todo list in Magit.

  (use-package magit-todos
    :after magit
    :config (magit-todos-mode 1))

Magit Forge

Work with Git Forges from within Emacs.

    (use-package forge
      :after magit
      :config
      (setq auth-sources '("~/.authinfo")))

Move Text

Move text up and down with simple keybindings.

  (use-package move-text
    :config
    (keymap-global-set "M-p" 'move-text-up)
    (keymap-global-set "M-n" 'move-text-down))

Ivy (Counsel)

Counsel

Adds better fuzzy completion to many Emacs commands.

  (use-package counsel
    :after ivy
    :config
    (counsel-mode 1)
    (keymap-global-set "C-c f r" 'counsel-buffer-or-recentf)
    (keymap-global-set "C-c f f" 'counsel-fzf)
    (keymap-global-set "C-c f g" 'counsel-rg)
    (keymap-global-set "C-c f l" 'counsel-locate)
    (keymap-global-set "C-c s" 'swiper))

Ivy

Adds fuzzy completion to basic Emacs commands.

  (use-package ivy
    :custom
    (setq ivy-use-virtual-buffers t)
    (setq ivy-count-format "(%d/%d) ")
    (setq enable-recursive-minibuffers t)
    :config
    (ivy-mode 1))

Ivy Rich

Adds Icons to all the new fuzzy completed Emacs commands.

  (use-package ivy-rich
    :after ivy
    :init (ivy-rich-mode 1)
    :custom
    (ivy-virtual-abbreviate 'full
                            ivy-rich-switch-buffer-align-virtual-buffer t
                            ivy-rich-path-style 'abbrev))

Simple Modeline

Make the modeline simple by hiding modes.

  (setq-default mode-line-format (delq 'mode-line-modes mode-line-format)
                display-time-24hr-format t
                display-time-default-load-average nil)
  (display-battery-mode 1)
  (display-time-mode 1)

Smart Parentheses Pairing

Automatically deals with parentheses in pairs.

  (use-package smartparens
    :hook (prog-mode text-mode markdown-mode)
    :config
    (require 'smartparens-config)
    (sp-use-paredit-bindings))

Undo Tree

Makes undo history like a Git commit tree, very powerful.

  (use-package undo-tree
    :config
    (setq undo-tree-history-directory-alist
          `(("." . ,(concat user-emacs-directory "undo"))))
    (global-undo-tree-mode 1))

Org Mode

Table of Contents

Automatically generate a table of contents for an Org file.

  (use-package toc-org
    :commands toc-org-enable
    :init (add-hook 'org-mode-hook 'toc-org-enable)
    :config (setq org-src-window-setup 'current-window))
  (add-hook 'org-mode-hook 'org-indent-mode)

Bullet Headers

Stylize Org Mode headers with Nerd Icons.

  (use-package org-bullets
    :config
    (setq org-bullets-bullet-list '(
                                    "•"
                                    "•"
                                    "◦"
                                    "◦"
                                    "◦")))
  (add-hook 'org-mode-hook (lambda () (org-bullets-mode 1)))

Org Agenda

Manage a Todo list, a Calendar, and other organization tools with Org.

  (setq org-agenda-files '("~/Documents/Org/agenda.org"))
  (setq org-fancy-priorities-list '("[A]" "[B]" "[C]")
        org-priority-faces
        '((?A :foreground "#ff6c6b" :weight bold)
          (?B :foreground "#ffff91" :weight bold)
          (?C :foreground "#aaffaa" :weight bold)))
  (setq org-agenda-custom-commands
        '(("v" "View Agenda"
           ((tags "PRIORITY=\"A\""
                  ((org-agenda-skip-function '(org-agenda-skip-entry-if 'todo 'done))
                   (org-agenda-overriding-header "HIGH PRIORITY:")))
            (tags "PRIORITY=\"B\""
                  ((org-agenda-skip-function '(org-agenda-skip-entry-if 'todo 'done))
                   (org-agenda-overriding-header "Medium Priority:")))
            (tags "PRIORITY=\"C\""
                  ((org-agenda-skip-function '(org-agenda-skip-entry-if 'todo 'done))
                   (org-agenda-overriding-header "low priority:")))
            (agenda "")
            (alltodo "")))))

Org Babel

Setup literate progamming in Org Mode.

Config

Don't ask for conformation when evaluating source blocks.

  (setq org-confirm-babel-evaluate nil)

Load Languages

  (org-babel-do-load-languages
   'org-babel-load-languages
   '((python . t)
     (lua . t)))

Org Tempo

This provides shorthands for Org functions.

  (require 'org-tempo)

Styling

Hide markers for bold, italic and other types of text styling.

  (setq org-hide-emphasis-markers t)

Programming

Projects

Manage Project folders from within Emacs.

Project.el

Use project.el and set shortcuts with prefix "C-c p".

  (require 'project)

  (keymap-global-set "C-c p b" 'project-list-buffers)
  (keymap-global-set "C-c p c" 'project-compile)
  (keymap-global-set "C-c p e" 'project-dired)
  (keymap-global-set "C-c p f" 'project-find-file)
  (keymap-global-set "C-c f p" 'project-find-file)
  (keymap-global-set "C-c p g" 'project-find-regexp)
  (keymap-global-set "C-c p o" 'project-find-dir)
  (keymap-global-set "C-c p p" 'project-switch-project)

Disproject

Magit like interface ontop of project.el.

  (use-package disproject
    ;; Replace `project-prefix-map' with `disproject-dispatch'.
    :bind ( :map ctl-x-map
            ("p" . disproject-dispatch)))

Run Project

Run the current project.

  (require 'project)

  (defvar-local project-binary-name nil)
  (defvar-local project-binary-args nil)
  (defvar-local project-debugger "gf2")

  (defun get-project-binary-info ()
    (let* ((project (project-current t))
           (project-root (project-root project))
           (binary-name (or project-binary-name
                            (downcase
                             (file-name-nondirectory (directory-file-name project-root)))))
           (binary-path (expand-file-name binary-name project-root))
           (args (or project-binary-args "")))
      (list binary-path args)))

  (defun run-project-binary ()
    (interactive)
    (let* ((binary-info (get-project-binary-info))
           (binary-path (nth 0 binary-info))
           (args (nth 1 binary-info))
           (original-compile-command compile-command))
      (if (file-executable-p binary-path)
          (progn
            (setq compile-command (concat binary-path " " args))
            (unwind-protect
                (compile compile-command)
              (setq compile-command original-compile-command)))
        (error "Binary '%s' not found or not executable in project root" binary-path))))

  (defun debug-project-binary ()
    (interactive)
    (let* ((binary-info (get-project-binary-info))
           (binary-path (nth 0 binary-info))
           (args (nth 1 binary-info))
           (debugger (or project-debugger "gdb"))
           (debug-command (concat debugger " --args " binary-path " " args))
           (original-compile-command compile-command))
      (if (file-executable-p binary-path)
          (progn
            (setq compile-command debug-command)
            (unwind-protect
                (compile compile-command)
              (setq compile-command original-compile-command)))
        (error "Binary '%s' not found or not executable in project root" binary-path))))

  ;; Mark 'project-binary-name' and 'project-binary-args' as safe for dir-locals.
  (put 'project-binary-name 'safe-local-variable #'stringp)
  (put 'project-binary-args 'safe-local-variable #'stringp)

  (keymap-global-set "C-c p r" 'run-project-binary)
  (keymap-global-set "C-c p d" 'debug-project-binary)

LSP

LSP Mode

Use language servers to provide info and completion.

  (use-package lsp-mode
    :init
    (setq lsp-completion-provider :none
          lsp-keymap-prefix "C-c l"
          lsp-headerline-breadcrumb-enable nil
          lsp-modeline-code-actions-enable nil)
    :commands lsp lsp-deferred
    :config
    (lsp-enable-snippet t))

  (keymap-global-set "C-c l d" 'lsp-describe-thing-at-point)

LSP Mode - Undo Tree compatibility

  (with-eval-after-load 'lsp-mode
    (defun my/lsp--should-ignore-file (filename)
      (or (string-match-p "\\.emacs[-_.]backups?/" filename)
          (string-match-p "\\.emacs[-_.]undo/" filename)
          (string-match-p "/\\.emacs\\.d/undo/" filename)
          (string-match-p "/\\.emacs\\.d/backups/" filename)
          (string-match-p "/#.*#$" filename)
          (string-match-p "/.*~$" filename)
          (string-match-p "/.*\\.~undo-tree~$" filename)))

    (advice-add 'lsp--on-did-open :before
                (lambda (&rest args)
                  (let ((file (lsp--uri-to-path (gethash "uri" (cl-first args)))))
                    (when (my/lsp--should-ignore-file file)
                      (cl-return-from lsp--on-did-open))))
                '((name . ignore-emacs-backup-open)))

    (advice-add 'lsp--on-did-save :before
                (lambda (&rest args)
                  (let ((file (lsp--uri-to-path (gethash "uri" (cl-first args)))))
                    (when (my/lsp--should-ignore-file file)
                      (cl-return-from lsp--on-did-save))))
                '((name . ignore-emacs-backup-save))))

YASnippet

Autocomplete Snippets to write common code patterns faster.

  (use-package yasnippet
    :config
    (setq yas-snippet-dirs '("~/Documents/snippets"))
    (yas-global-mode 1)
    (keymap-global-set "M-<tab>" 'yas-next-field-or-maybe-expand)
    (keymap-global-set "C-'" 'yas-expand))

  (straight-use-package
   '(yasnippet-capf :type git :host github :repo "elken/yasnippet-capf"))

  (use-package yasnippet-treesitter-shim
    :straight (:host github :repo "fbrosda/yasnippet-treesitter-shim"
                     :files ("snippets/*"))
    :no-require t
    :config
    (add-to-list 'yas-snippet-dirs
                 (straight--build-dir "yasnippet-treesitter-shim")))

DevDocs

Search documentation from within Emacs.

  (use-package devdocs
    :config
    (keymap-global-set "C-c d" 'devdocs-lookup))

Sidebar

Project file sidebar for project wide visual grepping.

  (use-package dired-sidebar
    :init
    (setq dired-sidebar-theme 'icons
          dired-sidebar-use-term-integration t
          dired-sidebar-width 30
          dired-sidebar-no-delete-other-windows t
          dired-sidebar-should-follow-file t))

  (defun open-project-sidebar ()
    "Toggle `dired-sidebar' at the project root."
    (interactive)
    (let ((default-directory (project-root (project-current t))))
      (dired-sidebar-toggle-sidebar)))

  (keymap-global-set "C-c p s" 'open-project-sidebar)

  (defun dired-sidebar-header-line ()
    "Set a minimal header line for dired-sidebar buffers."
      (setq header-line-format
            (propertize
             (concat " " (file-name-nondirectory
                            (directory-file-name default-directory)))
             'face 'bold)))

  (add-hook 'dired-sidebar-mode-hook #'dired-sidebar-header-line)

  (defun dired-sidebar-clean-top-line ()
    "Hide the top directory line in `dired-sidebar` buffers."
      (save-excursion
        (goto-char (point-min))
        (when (looking-at "^  \\(/.*\\):.*$")
          (let ((inhibit-read-only t))
            (delete-region (point) (progn (forward-line 1) (point)))))))

  (add-hook 'dired-sidebar-mode-hook #'dired-sidebar-clean-top-line)

  (add-hook 'dired-sidebar-mode-hook
            (lambda ()
              (setq mode-line-format nil))) ;; disable modeline in sidebar

TreeSitter

Use advanced highlighting by default.

  (require 'treesit)
  (use-package treesit-auto
    :config
    (global-treesit-auto-mode))
  (setq major-mode-remap-alist (treesit-auto--build-major-mode-remap-alist))

Languages

C

The Classic.

  (add-hook 'c-mode-hook #'lsp)
  (add-hook 'c-ts-mode-hook #'lsp)

  ;; Use GNU Style in LSP-Mode for C
  (with-eval-after-load 'lsp-clangd
    (setq lsp-clients-clangd-args '("--header-insertion-decorators=0" "--fallback-style=GNU")))

  ;; Automatically write header guards
  (defun insert-header-guards ()
    (when (and (buffer-file-name)
               (string-match "\\.h\\'" (buffer-file-name)))
      (let* ((filename (file-name-nondirectory (buffer-file-name)))
             (guard (upcase (replace-regexp-in-string "[^a-zA-Z0-9]" "_" filename))))
        (when (zerop (buffer-size)) ; Only insert if the file is empty
          (insert (format "#ifndef %s\n#define %s\n\n\n$0\n\n#endif // %s\n"
                          guard guard guard))
          (goto-char (point-min))
          (search-forward "$0")
          (delete-char -3)))))

  (add-hook 'find-file-hook 'insert-header-guards)

Go

A simple C-like language with a GC.

  (use-package go-mode
    :config
    (setenv "PATH" (concat (concat
                            (concat (expand-file-name "/usr/local/go/bin") ":")
                            (getenv "PATH"))))
    (setenv "PATH" (concat (concat
                            (concat (expand-file-name "~/.go/bin") ":")
                            (getenv "PATH"))))

    (setq exec-path (append exec-path (list (expand-file-name "/usr/local/go/bin"))))
    (setq exec-path (append exec-path (list (expand-file-name "~/.go/bin"))))
    (add-hook 'go-mode-hook
              (lambda ()
                (setq tab-width 4)))
    :hook
    (go-mode . lsp))

Haskell

Functionally Scottish.

  (use-package haskell-mode
    :config
    (setenv "PATH" (concat (concat
                            (concat (expand-file-name "~/.ghcup/bin") ":")
                            (getenv "PATH"))))

    (setq exec-path (append exec-path (list (expand-file-name "~/.ghcup/bin")))))

  (use-package lsp-haskell
    :hook
    (haskell-mode . lsp))

LISP

Programming for Programmers.

  (use-package sly
    :config
    (setq inferior-lisp-program "ros -Q run"
          browse-url-browser-function '(("hyperspec" . eww-browse-url) ("." . browse-url-default-browser)))
    (add-hook 'sly-mrepl-mode-hook 'smartparens-mode)
    (add-hook 'sly-mrepl-mode-hook 'company-mode)
    (add-hook 'lisp-mode-hook
              (lambda ()
                (define-key lisp-mode-map (kbd "C-c d") 'sly-documentation))))

Lua

A simple Python-like language with many implementations.

  (straight-use-package
   '(lua-mode :type git :host github :repo "immerrr/lua-mode"))
  (add-hook 'lua-mode-hook #'lsp)

  (setq lua-indent-nested-block-content-align nil)
  (setq lua-indent-close-paren-align nil)

  (defun lua-at-most-one-indent (old-function &rest arguments)
    (let ((old-res (apply old-function arguments)))
      (if (> old-res lua-indent-level) lua-indent-level old-res)))

  (advice-add #'lua-calculate-indentation-block-modifier
              :around #'lua-at-most-one-indent)

  (setq lua-indent-level 2)
  (setq lua-electric-flag nil)
  (defun lua-abbrev-mode-off () (abbrev-mode 0))
  (add-hook 'lua-mode-hook 'lua-abbrev-mode-off)
  (setq save-abbrevs nil)   ;; is this still needed?

OCaml

Installed with OPAM

  (setenv "PATH" (concat (concat
                          (concat (expand-file-name "~/.opam/default/bin") ":")
                            (getenv "PATH"))))

  (setq exec-path (append exec-path (list (expand-file-name "~/.opam/default/bin"))))

  (use-package tuareg
    :hook
    (tuareg-mode . lsp))

  (add-to-list 'load-path "/home/venomade/.opam/default/share/emacs/site-lisp")
  (require 'ocp-indent)
  (require 'dune)
  (require 'utop)

Typescript

  (setenv "PATH" (concat (concat
                          (concat (expand-file-name "~/.bun/bin") ":")
                            (getenv "PATH"))))

  (setq exec-path (append exec-path (list (expand-file-name "~/.bun/bin"))))

  (add-hook 'typescript-ts-mode-hook 'lsp)
  (add-hook 'tsx-ts-mode-hook 'lsp)

  (add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-ts-mode))
  (add-to-list 'auto-mode-alist '("\\.tsx\\'" . tsx-ts-mode))
  (add-to-list 'auto-mode-alist '("\\.json\\'" . json-ts-mode))

  ;; Tailwind
  (use-package lsp-tailwindcss
    :straight '(lsp-tailwindcss :type git :host github :repo "merrickluo/lsp-tailwindcss")
    :init (setq lsp-tailwindcss-add-on-mode t)
    :config
    (dolist (tw-major-mode
             '(css-mode
               css-ts-mode
               typescript-mode
               typescript-ts-mode
               tsx-ts-mode
               js2-mode
               js-ts-mode))
      (add-to-list 'lsp-tailwindcss-major-modes tw-major-mode)))

Zig

A systems language with emphasis on metaprogramming.

  (use-package zig-mode
    :hook (zig-mode . lsp))

Company

A powerful auto-completion utility.

  (use-package company
    :config
    (define-key company-active-map (kbd "RET") nil)
    (define-key company-active-map (kbd "<return>") nil)
    (define-key company-active-map (kbd "C-m") nil)

    ;; TAB indents or selects a completion
    (define-key company-active-map (kbd "TAB") #'company-complete-selection)
    (define-key company-active-map (kbd "<tab>") #'company-complete-selection)

    ;; Use C-p and C-n to navigate the completion list
    (define-key company-active-map (kbd "C-p") #'company-select-previous)
    (define-key company-active-map (kbd "C-n") #'company-select-next)

    ;; Restore normal tab behavior outside company-mode completions
    (define-key company-mode-map (kbd "TAB") #'indent-for-tab-command)
    (define-key company-mode-map (kbd "<tab>") #'indent-for-tab-command)

    ;; Disable Autocomplete
    (setq company-idle-delay 0)

    ;; Disable Case Sensitivity
    (setq completion-ignore-case t)

    ;; Bind M-/ to company-complete
    (define-key global-map (kbd "M-/") 'company-complete)
    (define-key global-map (kbd "M-?") 'dabbrev-expand)  
    :hook
    (prog-mode . company-mode))

  (use-package company-quickhelp
    :config
    (company-quickhelp-mode 1)
    (setq company-quickhelp-delay 0.5)
    (setq company-quickhelp-color-background (face-attribute 'company-tooltip :background))
    (add-hook 'enable-theme-functions
              (lambda (_)
                (setq company-quickhelp-color-background (face-attribute 'company-tooltip :background)))))

Cape

Add non-lsp completions to the capf.

  (use-package cape
    :ensure t
    :config
    (keymap-global-set "C-c c" 'cape-prefix-map)
    (setq dabbrev-case-fold-search t)
    ;;(add-hook 'completion-at-point-functions #'cape-dabbrev) ;; TODO: Check if this is slowing down comments?
    (add-hook 'completion-at-point-functions #'cape-file)
    (add-hook 'completion-at-point-functions #'cape-elisp-block)
    (add-hook 'completion-at-point-functions #'yasnippet-capf))

Codeium

  (straight-use-package
   '(codeium :type git :host github :repo "Exafunction/codeium.el"))

  (setq codeium-api-enabled
        (lambda (api)
          (memq api '(GetCompletions Heartbeat CancelRequest GetAuthToken RegisterUser auth-redirect AcceptCompletion))))

  (defun my-codeium/document/text ()
    (buffer-substring-no-properties (max (- (point) 3000) (point-min)) (min (+ (point) 1000) (point-max))))
  (defun my-codeium/document/cursor_offset ()
    (codeium-utf8-byte-length
     (buffer-substring-no-properties (max (- (point) 3000) (point-min)) (point))))
  (setq codeium/document/text 'my-codeium/document/text)
  (setq codeium/document/cursor_offset 'my-codeium/document/cursor_offset)

  (defun my/codeium/completion ()
    "Decouple codeium from other completions"
    (interactive)
    (cape-interactive #'codeium-completion-at-point))

  (keymap-global-set "C-c a" 'my/codeium/completion)

Utilities

Flycheck

Syntax checking for Emacs.

  (use-package flycheck
    :defer t
    :init (global-flycheck-mode 1)
    :config
    (setq-default flycheck-disabled-checkers '(emacs-lisp-checkdoc))
    (setq-default flycheck-cppcheck-args "--enable=all")
    (flycheck-add-next-checker 'c/c++-gcc 'c/c++-cppcheck 'append)
    (setq flycheck-indication-mode nil)
    (set-face-attribute 'flycheck-warning nil :underline nil)) ;; Disable Warning Underline (NOT WORKING)

  (add-to-list 'display-buffer-alist
               '("\\*Flycheck errors\\*"
                 (display-buffer-reuse-window
                  display-buffer-in-side-window)
                 (side . bottom)
                 (window-height . 0.2))) ;; Set flycheck error list to only take up the bottom 20% of the window

User Interface

Add Nerd Icons

Use Icons from Nerd Font to add a little modern spice to Emacs.

  (use-package nerd-icons
    :if (display-graphic-p))

  (use-package nerd-icons-dired
    :hook
    (dired-mode . nerd-icons-dired-mode))

  (use-package nerd-icons-ivy-rich
      :init (nerd-icons-ivy-rich-mode 1))

  (setq welcome-dashboard-use-nerd-icons t)

Fonts

Set Font

Set font for both Monospace and Proportional text.

  (defvar fontconf
      '((font . "Aporetic Sans Mono")
        (size . 11)))

    (set-face-attribute 'variable-pitch nil
            :font (cdr (assoc 'font fontconf))
            :height (* (cdr (assoc 'size fontconf)) 10)
            :weight 'regular)

    (set-face-attribute 'fixed-pitch nil
            :font (cdr (assoc 'font fontconf))
            :height (* (cdr (assoc 'size fontconf)) 10)
            :weight 'regular)

    (set-face-attribute 'default nil
            :font (cdr (assoc 'font fontconf))
            :height (* (cdr (assoc 'size fontconf)) 10)
            :weight 'regular)


    (add-to-list 'default-frame-alist
           `(font . ,(concat (cdr (assoc 'font fontconf)) "-" (number-to-string (cdr (assoc 'size fontconf))))))

    (set-face-attribute 'font-lock-comment-face nil
            :slant 'italic)

    (set-face-attribute 'font-lock-keyword-face nil
            :slant 'italic)

  ;;(set-frame-font "-misc-fixed-medium-r-normal--18-120-100-100-c-90-iso10646-1" t t)

Ligatures

Make ligature symbols out of common function symbols.

    (dolist (char/ligature-re
	     `((?-  . ,(rx (or (or "-->" "-<<" "->>" "-|" "-~" "-<" "->") (+ "-"))))
	       (?/  . ,(rx (or (or "/==" "/=" "/>" "/**" "/*") (+ "/"))))
	       (?*  . ,(rx (or (or "*>" "*/") (+ "*"))))
	       (?<  . ,(rx (or (or "<<=" "<<-" "<|||" "<==>" "<!--" "<=>" "<||" "<|>" "<-<"
				   "<==" "<=<" "<-|" "<~>" "<=|" "<~~" "<$>" "<+>" "</>"
				   "<*>" "<->" "<=" "<|" "<:" "<>"  "<$" "<-" "<~" "<+"
				   "</" "<*")
			       (+ "<"))))
	       (?:  . ,(rx (or (or ":?>" "::=" ":>" ":<" ":?" ":=") (+ ":"))))
	       (?=  . ,(rx (or (or "=>>" "==>" "=/=" "=!=" "=>" "=:=") (+ "="))))
	       (?!  . ,(rx (or (or "!==" "!=") (+ "!"))))
	       (?>  . ,(rx (or (or ">>-" ">>=" ">=>" ">]" ">:" ">-" ">=") (+ ">"))))
	       (?&  . ,(rx (+ "&")))
	       (?|  . ,(rx (or (or "|->" "|||>" "||>" "|=>" "||-" "||=" "|-" "|>"
				   "|]" "|}" "|=")
			       (+ "|"))))
	       (?.  . ,(rx (or (or ".?" ".=" ".-" "..<") (+ "."))))
	       (?+  . ,(rx (or "+>" (+ "+"))))
	       (?\[ . ,(rx (or "[<" "[|")))
	       (?\{ . ,(rx "{|"))
	       (?\? . ,(rx (or (or "?." "?=" "?:") (+ "?"))))
	       (?#  . ,(rx (or (or "#_(" "#[" "#{" "#=" "#!" "#:" "#_" "#?" "#(")
			       (+ "#"))))
	       (?\; . ,(rx (+ ";")))
	       (?_  . ,(rx (or "_|_" "__")))
	       (?~  . ,(rx (or "~~>" "~~" "~>" "~-" "~@")))
	       (?$  . ,(rx "$>"))
	       (?^  . ,(rx "^="))
	       (?\] . ,(rx "]#"))))
      (let ((char (car char/ligature-re))
	    (ligature-re (cdr char/ligature-re)))
	(set-char-table-range composition-function-table char
			      `([,ligature-re 0 font-shape-gstring]))))

Font Zooming

Bind both scroling and +/- to zooming.

  (global-set-key (kbd "C-=") 'text-scale-increase)
  (global-set-key (kbd "C--") 'text-scale-decrease)
  (global-set-key (kbd "<C-wheel-up>") 'text-scale-increase)
  (global-set-key (kbd "<C-wheel-down>") 'text-scale-decrease)

Theme

Set the theme to a nice dark one.

  (use-package kaolin-themes
    :config
    (set-window-margins nil 0))

  (add-hook 'emacs-startup-hook
            (lambda ()
              (load-theme 'kaolin-light t)))

  (defun theme-toggle ()
    "Toggle between two Emacs themes: doom-one and doom-dracula."
    (interactive)
    (let ((theme-a 'kaolin-light)
          (theme-b 'kaolin-dark))
      (cond
       ((member theme-a custom-enabled-themes)
        (disable-theme theme-a)
        (load-theme theme-b t))
       ((member theme-b custom-enabled-themes)
        (disable-theme theme-b)
        (load-theme theme-a t))
       (t
        (load-theme theme-a t)))))

Niceties

Disable Extra GUI Features

Disable GUI features to simplify frames.

  (menu-bar-mode -1)
  (tool-bar-mode -1)
  (scroll-bar-mode -1)
  (setq inhibit-startup-screen t)

No Scroll Jump

Scroll line by line instead of jumping multiple.

  (setq scroll-conservatively 100)
  (setq scroll-step 1)

Small Fringes

Reduce fringe size to 1px.

  (set-fringe-mode 1)

Zen Mode

Center the edtior with Olivetti for distraction-free editing.

  (use-package olivetti
    :config
    (keymap-global-set "C-c z" 'olivetti-mode))