Skip to content

Latest commit

 

History

History
2038 lines (1742 loc) · 69.9 KB

config.org

File metadata and controls

2038 lines (1742 loc) · 69.9 KB

Havner’s Emacs configuration

Prelude

Configurability

(defun +open-config-org (&optional arg)
  (interactive "P")
  (let ((config "~/.emacs.d/config.org"))
    (if arg
        (find-file-other-window config)
      (find-file config))))
(global-set-key (kbd "<f12>") #'+open-config-org)

(defun +set-ubuntu-font-size (&optional arg)
  (interactive "NFont size: ")
  (let ((font-to-set (concat "Ubuntu Mono-" (number-to-string arg))))
    (set-face-attribute 'default nil :font font-to-set)))
(global-set-key (kbd "M-<f12>") #'+set-ubuntu-font-size)

(defun +set-font-size (&optional arg)
  (interactive "NFont size: ")
  (set-face-attribute 'default nil :height (* arg 10)))
(global-set-key (kbd "C-<f12>") #'+set-font-size)

Mac / OSX bootstrap

;;; get rid of the travesty set in term/ns-win
(when (eq window-system 'ns)
  (define-key global-map (kbd "<home>") 'move-beginning-of-line)
  (define-key global-map (kbd "<end>")  'move-end-of-line))

;;; unify mac distributions with other OS'es
(pcase window-system
  ('mac
   (setq mac-option-modifier '(:function super :mouse super))
   (setq mac-command-modifier 'meta))
  ('ns
   (setq ns-option-modifier '(:function super :mouse super))
   (setq ns-command-modifier 'meta)))

(when (eq window-system 'mac)
  (setq mac-mouse-wheel-smooth-scroll nil))

;;; specific paths
(if (eq system-type 'darwin)
    (setq insert-directory-program "gls"))

;;; Handle mac keyboard without insert
;;; <help> -> insert on regular PC keyboard
;;; <f13> -> above "insert" on full mac keyboard
;;; <clear> -> on numpad on full mac keyboard
(when (or (eq window-system 'mac)
          (eq window-system 'ns))
  (define-key function-key-map (kbd "<help>") (kbd "<insert>"))
  (define-key function-key-map (kbd "<M-help>") (kbd "<M-insert>"))
  (define-key function-key-map (kbd "<S-help>") (kbd "<S-insert>"))
  (define-key function-key-map (kbd "<C-help>") (kbd "<C-insert>"))
  (define-key function-key-map (kbd "<f13>") (kbd "<insert>"))
  (define-key function-key-map (kbd "<M-f13>") (kbd "<M-insert>"))
  (define-key function-key-map (kbd "<S-f13>") (kbd "<S-insert>"))
  (define-key function-key-map (kbd "<C-f13>") (kbd "<C-insert>"))
  (define-key function-key-map (kbd "<clear>") (kbd "<insert>"))
  (define-key function-key-map (kbd "<M-clear>") (kbd "<M-insert>"))
  (define-key function-key-map (kbd "<S-clear>") (kbd "<S-insert>"))
  (define-key function-key-map (kbd "<C-clear>") (kbd "<C-insert>")))

(defun +mac-browse-url-open (url &optional ignored)
  "Pass the specified URL to the \"open\" command.
open is a OSX desktop utility that calls your preferred web browser.
The optional argument IGNORED is not used."
  (interactive (browse-url-interactive-arg "URL: "))
  (call-process "open" nil 0 nil url))

(defun +mac-toggle-frame-fullscreen ()
  "Toggle fullscreen state of selected frame."
  (interactive)
  (let ((fullscreen (frame-parameter nil 'fullscreen)))
    (if (memq fullscreen '(fullscreen fullboth))
        (let ((fullscreen-restore (frame-parameter nil 'fullscreen-restore)))
          (if (memq fullscreen-restore '(maximized fullheight fullwidth))
              (set-frame-parameter nil 'fullscreen fullscreen-restore)
            (set-frame-parameter nil 'fullscreen nil)))
      (set-frame-parameter nil `fullscreen 'fullscreen))))

;;; Use the F11 as the true macos fullscreen
(when (or (eq window-system 'mac)
          (eq window-system 'ns))
  (global-set-key (kbd "<f11>") #'+mac-toggle-frame-fullscreen))

Windows bootstrap

;;; specific paths
(when (eq system-type 'windows-nt)
  (setq ls-lisp-use-insert-directory-program t)
  (setq magit-git-executable "git.exe")
  (with-eval-after-load 'consult
	(setq consult-find-args (concat "c:/Users/Havner/Downloads/emacs/bin/" consult-find-args))))

Elisp

(defun +add-to-list-global (list elem &optional app)
  (if app
      (set-default list (append (eval list) `(,elem)))
    (set-default list (append `(,elem) (eval list)))))

(defun +delete-from-list (list elem)
  (set list (delete elem (eval list))))

(defun +list-ends-with (sublist list)
  "Return t if LIST end is equal to SUBLIST."
  (equal (nthcdr (- (length list) (length sublist)) list)
         sublist))

Packages

(let ((dirs '("~/.emacs.d/helpa/zenburn-emacs"
              "~/.emacs.d/helpa/spacemacs-theme")))
  (dolist (dir dirs)
    (when (file-directory-p dir)
      (add-to-list 'load-path dir))))

To be used directly (interactive)

(defun +kill-buffers-prefix (arg)
  (interactive "MPrefix: ")
  (mapc (lambda (buffer)
          (if (string-prefix-p arg (buffer-name buffer) t)
              (kill-buffer buffer)))
        (buffer-list)))

(defun +kill-buffers-mode (mode)
  (mapc (lambda (buffer)
          (when (eq mode (buffer-local-value 'major-mode buffer))
            (kill-buffer buffer)))
        (buffer-list)))

(defun +kill-buffers-magit ()
  (interactive)
  (+kill-buffers-prefix "magit"))

(defun +kill-buffers-dired ()
  (interactive)
  (+kill-buffers-mode 'dired-mode))

(defun +sudo-file-path (file)
  (concat "/sudo::" (expand-file-name file)))

(defun +sudo-find-file (file)
  (interactive "FOpen file as root")
  (find-file (+sudo-file-path file)))

(defun +sudo-this-file ()
  (interactive)
  (let ((file-name (buffer-file-name)))
    (if file-name
        (find-alternate-file (concat "/sudo::" file-name))
      (message "Current buffer not a file"))))

(defvar +lang-ring
  "List of languages the `+cycle-ispell-languages' will cycle through.")
(let ((langs '("polish" "english")))
  (setq +lang-ring (make-ring (length langs)))
  (dolist (elem langs) (ring-insert +lang-ring elem))
  (ispell-change-dictionary (ring-ref +lang-ring -1)))

(defun +cycle-ispell-languages ()
  "Cycle currently used Ispell language from `+lang-ring'."
  (interactive)
  (let ((lang (ring-ref +lang-ring -1)))
    (ring-insert +lang-ring lang)
    (ispell-change-dictionary lang)))

(defun +display-prefix (arg)
  "Display the value of the raw prefix ARG."
  (interactive "P")
  (message "%s" arg))

(defun +de-unicode ()
  "Tidy up a buffer by replacing all special Unicode characters.
Replaces things like smart quotes with their more sane cousins."
  (interactive)
  (let ((unicode-map '(("[\u2018\|\u2019\|\u201A\|\uFFFD]" . "'")
                       ("[\u201c\|\u201d\|\u201e]" . "\"")
                       ("\u2013" . "--")
                       ("\u2014" . "---")
                       ("\u2026" . "...")
                       ("\u00A9" . "(c)")
                       ("\u00AE" . "(r)")
                       ("\u2122" . "TM")
                       ("[\u02DC\|\u00A0]" . " "))))
    (save-excursion
      (cl-loop for (key . value) in unicode-map
               do
               (goto-char (point-min))
               (replace-regexp key value)))))

(defun +eval-and-replace ()
  "Replace the preceding sexp with its value."
  (interactive)
  (backward-kill-sexp)
  (condition-case nil
      (prin1 (eval (read (current-kill 0)))
             (current-buffer))
    (error (message "Invalid expression")
           (insert (current-kill 0)))))

(defun +scroll-right-small ()
  (interactive)
  (scroll-left 5))

(defun +scroll-left-small ()
  (interactive)
  (scroll-right 5))

(defun +create-scratch-buffer ()
  (with-current-buffer (get-buffer-create "*scratch*")
    (set-buffer-major-mode (current-buffer))
    (when (and (zerop (buffer-size))
               initial-scratch-message)
      (insert (substitute-command-keys initial-scratch-message))
      (set-buffer-modified-p nil))
    (current-buffer)))

(defun +switch-to-scratch ()
  (interactive)
  (let ((buffer (or (get-buffer "*scratch*")
                    (+create-scratch-buffer))))
    (switch-to-buffer buffer)))

CMD Line

(defun +command-line-ediff (switch)
  "EDiff two files from command line"
  (let ((file1 (pop command-line-args-left))
        (file2 (pop command-line-args-left)))
    (ediff file1 file2)))
(add-to-list 'command-switch-alist '("ediff" . +command-line-ediff))

(defun +command-line-vdiff (switch)
  "VDiff two files from command line"
  (let ((file1 (pop command-line-args-left))
        (file2 (pop command-line-args-left)))
    (vdiff-files file1 file2)))
(add-to-list 'command-switch-alist '("vdiff" . +command-line-vdiff))

Themes

(defun +disable-themes ()
  (interactive)
  (dolist (theme custom-enabled-themes)
    (if theme (disable-theme theme))))

(defmacro +def-theme-function (theme desc &optional module &rest body)
  (let ((fun-name (format "+%s" theme)))
    `(defun ,(intern fun-name) ()
       ,desc
       (interactive)
       (when ,module
         (require ,module))
       (+disable-themes)
       ,@body
       (load-theme ',theme t))))

;;;                  THEME-NAME           FUNCTION-DESC          FILE-NAME

(+def-theme-function spacemacs-dark       "Spacemacs Dark"       'spacemacs-common)
(+def-theme-function spacemacs-light      "Spacemacs Light"      'spacemacs-common)

(+def-theme-function zenburn              "Zenburn"              'zenburn-theme)

(+def-theme-function doom-Iosvkem         "DOOM Iosvkem"         'doom-themes)
(+def-theme-function doom-badger          "DOOM Badger"          'doom-themes)
(+def-theme-function doom-city-lights     "DOOM City Lights"     'doom-themes)
(+def-theme-function doom-challenger-deep "DOOM Challenger Deer" 'doom-themes)
(+def-theme-function doom-dark+           "DOOM Dark+"           'doom-themes)
(+def-theme-function doom-dracula         "DOOM Dracula"         'doom-themes)
(+def-theme-function doom-henna           "DOOM Henna"           'doom-themes)
(+def-theme-function doom-lantern         "DOOM Lantern"         'doom-themes)
(+def-theme-function doom-material        "DOOM Material"        'doom-themes)
(+def-theme-function doom-miramare        "DOOM Miramare"        'doom-themes)
(+def-theme-function doom-molokai         "DOOM Molokai"         'doom-themes)
(+def-theme-function doom-monokai-machine "DOOM Monokai Machine" 'doom-themes)
(+def-theme-function doom-moonlight       "DOOM Moonlight"       'doom-themes)
(+def-theme-function doom-nord-aurora     "DOOM Nord Aurora"     'doom-themes)
(+def-theme-function doom-nord-light      "DOOM Nord Light"      'doom-themes)
(+def-theme-function doom-nord            "DOOM Nord"            'doom-themes)
(+def-theme-function doom-nova            "DOOM Nova"            'doom-themes)
(+def-theme-function doom-oceanic-next    "DOOM Oceanic Next"    'doom-themes)
(+def-theme-function doom-one-light       "DOOM One Light"       'doom-themes)
(+def-theme-function doom-one             "DOOM One"             'doom-themes)
(+def-theme-function doom-opera-light     "DOOM Opera Light"     'doom-themes)
(+def-theme-function doom-opera           "DOOM Opera"           'doom-themes)
(+def-theme-function doom-outrun-electric "DOOM Outrun Electric" 'doom-themes)
(+def-theme-function doom-palenight       "DOOM Palenight"       'doom-themes)
(+def-theme-function doom-sourcerer       "DOOM Sourcerer"       'doom-themes)
(+def-theme-function doom-spacegrey       "DOOM Spacegrey"       'doom-themes)
(+def-theme-function doom-vibrant         "DOOM Vibrant"         'doom-themes)
(+def-theme-function doom-zenburn         "DOOM Zenburn"         'doom-themes)

Additional basic window/frame functions

(defun +kill-current-buffer ()
  "Kill the current buffer without prompting."
  (interactive)
  (kill-buffer (current-buffer)))

(defun +kill-buffer-and-window-and-balance ()
  "Kill buffer and window and balance"
  (interactive)
  (kill-buffer-and-window)
  (balance-windows))

(defun +delete-window-and-balance ()
  "Delete current windowKill the current buffer without prompting."
  (interactive)
  (delete-window)
  (balance-windows))

(defun +split-window-below-switch-and-balance ()
  "Split the window horizontally, then switch to the new pane."
  (interactive)
  (split-window-below)
  (other-window 1)
  (balance-windows))

(defun +split-window-right-switch-and-balance ()
  "Split the window vertically, then switch to the new pane."
  (interactive)
  (split-window-right)
  (other-window 1)
  (balance-windows))

switch-window variants

(autoload 'switch-window--then "switch-window" "" t)

(defun +switch-window-then-kill-current-buffer ()
  (interactive)
  (switch-window--then
   "Buffer to kill: "
   #'+kill-current-buffer
   #'+kill-current-buffer t))

(defun +switch-window-then-kill-buffer-and-window-and-balance ()
  (interactive)
  (switch-window--then
   "Window to kill: "
   #'+kill-buffer-and-window-and-balance
   #'+kill-buffer-and-window-and-balance t))

(defun +switch-window-then-delete-window-and-balance ()
  (interactive)
  (switch-window--then
   "Delete window: "
   #'+delete-window-and-balance
   #'+delete-window-and-balance t))

(defun +switch-window-then-split-below-switch-and-balance (arg)
  (interactive "P")
  (switch-window--then
   "Below-split window: "
   #'+split-window-below-switch-and-balance
   #'+split-window-below-switch-and-balance arg 1))

(defun +switch-window-then-split-right-switch-and-balance (arg)
  (interactive "P")
  (switch-window--then
   "Right-split window: "
   #'+split-window-right-switch-and-balance
   #'+split-window-right-switch-and-balance arg 1))

<escape> terminal hack

(defun +tty-esc-filter (map)
  (if (and (+list-ends-with '(?\e) (append (this-single-command-keys) nil))
           (sit-for 0.05))
      [escape] map))

(defun +lookup-key (map key)
  (catch 'found
    (map-keymap (lambda (k b) (if (equal key k) (throw 'found b))) map)))

(defun +catch-tty-esc ()
  "Setup key mappings of current terminal to turn a tty's ESC into `escape'."
  (when (memq (terminal-live-p (frame-terminal)) '(t pc))
    (let ((esc-binding (+lookup-key input-decode-map ?\e)))
      (define-key input-decode-map
        [?\e] `(menu-item "" ,esc-binding :filter +tty-esc-filter)))))

(+catch-tty-esc)

Option to delete trailing whitespace on file save

(defcustom +delete-trailing-whitespace-on-save nil
  "Whether to call `delete-trailing-whitespace' on file save."
  :type 'boolean
  :group 'havner)

(defun +maybe-delete-trailing-whitespace ()
  (when (and +delete-trailing-whitespace-on-save
             (or (derived-mode-p 'prog-mode)
                 (derived-mode-p 'text-mode)))
    (delete-trailing-whitespace)))

(with-eval-after-load 'files
  (add-hook 'before-save-hook #'+maybe-delete-trailing-whitespace))

Option to restore EDiff state on exit

(defcustom +ediff-restore-winconfig-state-on-exit nil
  "Whether to restore a previous winconfig state after quitting EDiff."
  :type 'boolean
  :group 'havner)

(defvar +ediff-last-winconfig nil)
(defun +ediff-maybe-save-winconfig-state ()
  (when +ediff-restore-winconfig-state-on-exit
    (setq +ediff-last-winconfig (current-window-configuration))))
(defun +ediff-maybe-restore-winconfig-state ()
  (when +ediff-restore-winconfig-state-on-exit
    (set-window-configuration +ediff-last-winconfig)))

(with-eval-after-load 'ediff-init
  (add-hook 'ediff-before-setup-hook #'+ediff-maybe-save-winconfig-state)
  (add-hook 'ediff-quit-hook #'+ediff-maybe-restore-winconfig-state))

Option to replace completion at point with company

(defcustom +company-replace-completion nil
  "Whether to use company-complete every time completion-at-point is called."
  :type 'boolean
  :group 'havner)

(defun +company-maybe-replace-completion (orig-fun &rest args)
  (if (or (not +company-replace-completion)
          (eq (active-minibuffer-window)
              (selected-window)))
      (apply orig-fun args)
    (company-complete)))

(advice-add #'completion-at-point
            :around #'+company-maybe-replace-completion)

Option for magit not to restore window configuration

(defcustom +magit-dont-restore-window-configuration nil
  "Whether not to restore windows configuration on magit quit."
  :type 'boolean
  :group 'havner)

(defun +magit-maybe-dont-restore-window-configuration (orig-fun &rest args)
  "Bury or kill the current buffer and DON'T restore previous window configuration."
  (if +magit-dont-restore-window-configuration
      (quit-window (car args) (selected-window))
    (apply orig-fun args)))

(advice-add #'magit-restore-window-configuration
            :around #'+magit-maybe-dont-restore-window-configuration)

Option to show file name in consult-async searches

(defcustom +consult-async-show-filename nil
  "Whether to show filename in result lines."
  :type 'boolean
  :group 'havner)

(defun +consult-async-grep-group-maybe-show-filename (orig-fun &rest args)
  (if (and +consult-async-show-filename
           (nth 1 args))
      (car args)
    (apply orig-fun args)))

(advice-add #'consult--grep-group
            :around #'+consult-async-grep-group-maybe-show-filename)

Option to show [CRM] in completing-read-multiple prompt

(defcustom +crm-indicator nil
  "Whether to show [CRM] indicator in completing-read-multiple."
  :type 'boolean
  :group 'havner)

(defun +crm-maybe-show-indicator (args)
  (if +crm-indicator
      (cons (format "[CRM%s] %s"
                    (replace-regexp-in-string
                     "\\`\\[.*?]\\*\\|\\[.*?]\\*\\'" ""
                     crm-separator)
                    (car args))
            (cdr args))
    args))

(advice-add #'completing-read-multiple
            :filter-args #'+crm-maybe-show-indicator)

Option for company to ignore orderless

(defcustom +company-ignore-orderless nil
  "Ignore orderless completion-style when using company"
  :type 'boolean
  :group 'havner)

(defun +company-capf-maybe-ignore-orderless (fn &rest args)
  (if +company-ignore-orderless
      (let ((completion-styles '(basic partial-completion)))
        (apply fn args))
    (apply fn args)))

(advice-add #'company-capf--candidates
            :around #'+company-capf-maybe-ignore-orderless)

Option for parinfer-rust to use rainbow-delimiters

(defcustom +parinfer-rust-use-rainbow-delimiters nil
  "Whether to use rainbow-delimiters in paren mode."
  :type 'boolean
  :group 'havner)

(defun +parinfer-rust-maybe-use-rainbow-delimiters (fun &rest args)
  (let ((rainbow-disable (and (not (string-equal parinfer-rust--mode "paren"))
                              parinfer-rust-dim-parens)))
    (when (and rainbow-disable
               +parinfer-rust-use-rainbow-delimiters
               (bound-and-true-p rainbow-delimiters-mode))
      (rainbow-delimiters-mode-disable))
    (apply fun args)
    (when (and (not rainbow-disable)
               +parinfer-rust-use-rainbow-delimiters
               (fboundp 'rainbow-delimiters-mode))
      (rainbow-delimiters-mode-enable))))

(advice-add #'parinfer-rust--dim-parens
            :around #'+parinfer-rust-maybe-use-rainbow-delimiters)

For god-mode

(defun +god-mode-enable-or-keyboard-quit ()
  "Turn on `god-local-mode' in all buffers or `keyboard-quit'.
`keyboard-quit' happens when `god-local-mode' was already on in
the current buffer, or it's impossible to turn it on."
  (interactive)
  (let ((old-status (bound-and-true-p god-local-mode)))
    (god-mode-all 1)
    (when (or old-status
              (not god-local-mode))
      (keyboard-quit))))

;;; Function for <escape> in company-mode so <escape> still
;;; works transparently with company-mode active
(defun +company-abort-and-god-mode-enable ()
  (interactive)
  (company-abort)
  (+god-mode-enable-or-keyboard-quit))

;;; Additional editing shortcuts

(defun +god-edit-open-line ()
  "New line and disable god mode."
  (interactive)
  (end-of-line)
  (newline 1 t)
  (god-mode-all -1))

(defun +god-edit-open-line-prev ()
  "New line and disable god mode."
  (interactive)
  (beginning-of-line)
  (newline 1 t)
  (forward-line -1)
  (god-mode-all -1))

(defun +god-edit-beginning-of-line ()
  "Beginning of line and disable god mode."
  (interactive)
  (beginning-of-line)
  (god-mode-all -1))

(defun +god-edit-end-of-line ()
  "End of line and disable god mode."
  (interactive)
  (end-of-line)
  (god-mode-all -1))

(defun +god-edit-change-word (arg)
  (interactive "p")
  (kill-word arg)
  (god-mode-all -1))

(defun +god-edit-match-paren ()
  (interactive)
  (cond ((looking-at "\\s(") (forward-list 1))
        ((looking-back "\\s)" 1) (backward-list 1))))

For orderless

(defun +vertico-orderless-dispatch (pattern _index _total)
  (cond
   ;;; Ensure $ works with Consult commands, which add disambiguation suffixes
   ((string-suffix-p "$" pattern)
    `(orderless-regexp . ,(concat (substring pattern 0 -1) "[\x200000-\x300000]*$")))
   ;;; Ignore single !
   ((string= "!" pattern) `(orderless-literal . ""))
   ;;; Without literal
   ((string-prefix-p "!" pattern) `(orderless-without-literal . ,(substring pattern 1)))
   ;;; Character folding
   ((string-prefix-p "%" pattern) `(char-fold-to-regexp . ,(substring pattern 1)))
   ((string-suffix-p "%" pattern) `(char-fold-to-regexp . ,(substring pattern 0 -1)))
   ;;; Initialism matching
   ((string-prefix-p "`" pattern) `(orderless-initialism . ,(substring pattern 1)))
   ((string-suffix-p "`" pattern) `(orderless-initialism . ,(substring pattern 0 -1)))
   ;;; Literal matching
   ((string-prefix-p "=" pattern) `(orderless-literal . ,(substring pattern 1)))
   ((string-suffix-p "=" pattern) `(orderless-literal . ,(substring pattern 0 -1)))
   ;;; Flex matching
   ((string-prefix-p "~" pattern) `(orderless-flex . ,(substring pattern 1)))
   ((string-suffix-p "~" pattern) `(orderless-flex . ,(substring pattern 0 -1)))))

For consult

(defmacro +consult-make-find-function (fun-name fun &optional ask)
  `(defun ,fun-name (arg)
     (interactive "p")
     (let ((initial (thing-at-point 'symbol)))
       (if initial (setq initial (concat initial "#")))
       (,fun ,ask initial))))

(+consult-make-find-function +consult-find consult-find t)
(+consult-make-find-function +consult-grep consult-grep t)
(+consult-make-find-function +consult-ripgrep consult-ripgrep t)
(+consult-make-find-function +consult-projectile-grep consult-grep)
(+consult-make-find-function +consult-projectile-ripgrep consult-ripgrep)
(+consult-make-find-function +consult-git-grep consult-git-grep)

(defun +consult-line ()
  (interactive)
  (consult-line (thing-at-point 'symbol)))

For projects

(defmacro +with-real-projectile-file (require-writeable &rest body)
  "Call BODY only if the current buffer is a real file inside a projectile project.
If REQUIRE-WRITEABLE is non-nil the file has to be writeable."
  (declare (debug t))
  `(when (and buffer-file-name
              (projectile-project-root)
              (or (not ,require-writeable)
                  (file-writable-p (buffer-file-name))))
     ,@body))

For helpful

(autoload 'helpful--read-symbol "helpful")

(defun +helpful-keymap (symbol)
  "Show help for keymap named SYMBOL."
  (interactive
   (list (helpful--read-symbol
          "Keymap: "
          (help-fns--most-relevant-active-keymap)
          (lambda (m) (and (boundp m) (keymapp (symbol-value m)))))))
  (helpful--update-and-switch-buffer symbol nil))

Configuration

Themes

Zenburn

Spacemacs

(setq spacemacs-theme-comment-bg nil)
(setq spacemacs-theme-comment-italic nil)
(setq spacemacs-theme-underline-parens nil)
(setq spacemacs-theme-org-height nil)

Doom

;; (doom-themes-visual-bell-config)
(setq doom-themes-enable-bold t)     ; if nil, bold is universally disabled
(setq doom-themes-enable-italic nil) ; if nil, italics is universally disabled

LOAD

(defvar +system-color-depth)

(cond (window-system
       (setq +system-color-depth '24bit))
      ((equal (getenv "TERM") "xterm-24bit")
       (setq +system-color-depth '24bit))
      ((equal (getenv "TERM") "xterm-256color")
       (setq +system-color-depth '256color))
      ((equal (getenv "TERM") "xterm-16color")
       (setq +system-color-depth '16color))
      ((equal (getenv "TERM") "xterm")
       (setq +system-color-depth '8color))
      ((equal (getenv "TERM") "linux")
       (setq +system-color-depth '8color))
      (_
       (setq +system-color-depth 'headless)))

(pcase +system-color-depth
  ('24bit    (+doom-moonlight))
  ('256color (+zenburn)))

Misc options

(defalias 'yes-or-no-p 'y-or-n-p)  ; Treat 'y' as yes, 'n' as no.
(define-key query-replace-map (kbd "<return>") 'act)
(define-key query-replace-map (kbd "RET") 'act)
(setq y-or-n-p-use-read-key t)

(setq load-prefer-newer t)
(setq inhibit-startup-screen t)
(setq scroll-conservatively 101)
(setq scroll-error-top-bottom t)
(setq require-final-newline t)
(setq gc-cons-threshold (* 10 1024 1024))
(setq-default truncate-lines t)
(setq bookmark-default-file "~/.emacs-bookmarks.el")
(setq recentf-save-file "~/.emacs-recentf.el")
(setq recentf-max-saved-items 500)
(setq create-lockfiles nil)       ; lockfiles breaks python completion
(setq find-file-visit-truename t) ; doom-modeline likes that
(setq view-mode-map (make-sparse-keymap))
(setq +delete-trailing-whitespace-on-save t)

(when window-system
  (setq confirm-kill-emacs 'yes-or-no-p))

;;; minor modes
(setq show-paren-delay 0.0)
(setq display-time-24hr-format t)
(setq display-time-day-and-date nil)
(setq display-time-default-load-average nil)

;;; hooks
(add-hook 'text-mode-hook #'turn-on-auto-fill)
(add-hook 'after-save-hook #'executable-make-buffer-file-executable-if-script-p)

Minor modes

;;; GUI
(menu-bar-mode 0)
(tool-bar-mode 0)
(tooltip-mode 0)
(when window-system
  (set-scroll-bar-mode nil))

(defun +disable-scroll-bars (frame)
  (modify-frame-parameters frame
                           '((vertical-scroll-bars . nil)
                             (horizontal-scroll-bars . nil))))
(add-hook 'after-make-frame-functions '+disable-scroll-bars)

;;; modeline
(column-number-mode t)
(line-number-mode t)
(size-indication-mode t)
(display-time-mode t)

;;; misc / buffer
(show-paren-mode t)
(delete-selection-mode t)
(transient-mark-mode t)
(global-auto-revert-mode t)
(recentf-mode t)

;;; external, too short for their own section
(global-page-break-lines-mode t)
(beginend-global-mode t)
(setq anzu-cons-mode-line-p nil)
(global-anzu-mode t)

GUI options

(setq use-dialog-box t)
(setq default-frame-alist
      '((width . 120)
        (height . 40)))
(pcase window-system
  ('w32 (set-face-attribute 'default nil :font "Ubuntu Mono-10"))
  ('x   (set-face-attribute 'default nil :font "Ubuntu Mono-12"))
  ('mac (set-face-attribute 'default nil :height 120))
  ('ns  (set-face-attribute 'default nil :family "Monaco" :height 120)))

;;; stop beeping FFS
(setq ring-bell-function 'ignore)

Mouse options

(setq mouse-yank-at-point t)
(setq mouse-wheel-scroll-amount '(1 ((shift) . 5) ((control))))

(xterm-mouse-mode t)

Backups

(setq temporary-file-directory "~/tmp")
(unless (file-directory-p temporary-file-directory)
  (mkdir temporary-file-directory))

(setq backup-directory-alist
      `((".*" . ,temporary-file-directory)))
(setq auto-save-list-file-prefix
      (concat temporary-file-directory "/auto-save-list/.saves-"))
;; (setq auto-save-file-name-transforms
;;       `((".*" ,temporary-file-directory t)))

Tab related

(setq tab-always-indent 'complete)
(setq backward-delete-char-untabify-method nil)
(setq-default indent-tabs-mode t)
(setq-default tab-width 4)
(defvaralias 'standard-indent 'tab-width)

paradox

(setq paradox-column-width-package 30)
(setq paradox-column-width-version 14)
(setq paradox-spinner-type 'progress-bar-filled)
(setq paradox-automatically-star nil)
(setq paradox-execute-asynchronously nil)
(setq package-native-compile t)

shackle

;;; Finally, don't create/switch/delete windows uncontrollably because
;;; every plugin author has a different view on how your workflow
;;; should look like. With few small exceptions (popup windows) don't
;;; create any windows unless I do that explicitely.

;;; windows that are allowed to popup
(setq shackle-rules '(("\*Marked\ Files\*" :regexp t :align t)     ;; dired
                      ("\*Deletions\*"     :regexp t :align t)     ;; dired
                      ("\*NeoTree\*"       :regexp t :align nil)   ;; neotree
                      ("\*transient\*"     :regexp t :align t)))   ;; magit
;;; everything else reuse current window and don't close it on quit
(setq shackle-default-rule '(:same t :inhibit-window-quit t :select t))

(shackle-mode t)

switch-window

(setq switch-window-minibuffer-shortcut ?x)
(setq switch-window-background t)
(setq switch-window-mvborder-increment 2)
(setq switch-window-multiple-frames t)

window-jump

(setq wj-jump-frames switch-window-multiple-frames)

tab-bar

(setq tab-bar-show 1)
(setq tab-bar-new-tab-choice "*scratch*")
(setq tab-bar-tab-hints t)
(setq tab-bar-select-tab-modifiers '(meta))
(setq tab-bar-close-button-show nil)
(tab-bar-mode t)
(tab-rename "main ")

(defmacro +tab-bar-select-function (number)
  (let ((fun-name (format "+tab-select-%d" number)))
    `(defun ,(intern fun-name) ()
       (interactive)
       (tab-select ,number))))
(dotimes (count 10)
  (eval `(+tab-bar-select-function ,count)))

vundo

(setq vundo-compact-display t)
(setq vundo-window-max-height 15)

dired

(setq dired-dwim-target t)
(setq dired-auto-revert-buffer t)
(setq dired-listing-switches "-alhB --group-directories-first")

(with-eval-after-load 'dired
  ;; (add-hook 'dired-mode-hook #'all-the-icons-dired-mode)
  (add-hook 'dired-mode-hook #'diredfl-mode))

ibuffer

(setq ibuffer-default-sorting-mode 'alphabetic)
(setq ibuffer-expert t)

(defun +ibuffer-kill ()
  (interactive)
  (kill-buffer "*Ibuffer*"))
(with-eval-after-load 'ibuffer
  ;; (add-hook 'ibuffer-mode-hook #'all-the-icons-ibuffer-mode)
  (define-key ibuffer-mode-map [remap quit-window] #'+ibuffer-kill))

whitespace-mode

(pcase +system-color-depth
  ('24bit    (setq whitespace-style '(face trailing space-mark tab-mark spaces tabs)))
  ('256color (setq whitespace-style '(face trailing space-mark tab-mark spaces tabs)))
  (_         (setq whitespace-style '(face trailing space-mark tab-mark))))

parinfer-rust-mode

(setq parinfer-rust-mode-map (make-sparse-keymap))
(setq parinfer-rust-preferred-mode "indent")
(setq +parinfer-rust-use-rainbow-delimiters t)

which-key

(setq which-key-idle-secondary-delay 0.5)

(which-key-mode t)
(which-key-enable-god-mode-support)

calendar

(setq calendar-week-start-day 1)
(setq calendar-mark-holidays-flag t)
(with-eval-after-load 'calendar
  (calendar-set-date-style 'european)
  (add-hook 'calendar-today-visible-hook 'calendar-mark-today))

(setq holiday-hebrew-holidays nil)
(setq holiday-islamic-holidays nil)
(setq holiday-bahai-holidays nil)
(setq holiday-oriental-holidays nil)
(setq holiday-christian-holidays nil)
(setq holiday-general-holidays
      `((holiday-fixed 1 1 "Nowy Rok")
        (holiday-fixed 1 6 "Trzech Króli")
        (holiday-easter-etc 0 "Wielkanoc")
        (holiday-easter-etc 1 "Poniedziałek Wielkanocny")
        (holiday-fixed 5 1 "Święto Pracy")
        (holiday-fixed 5 3 "Święto Konstytucji 3 Maja")
        (holiday-easter-etc 49 "Zielone świątki")
        (holiday-easter-etc 60 "Boże Ciało")
        (holiday-fixed 8 15 "Wniebowzięcie Najświętrzej Maryi Panny")
        (holiday-fixed 11 1 "Wszystkich Świętych")
        (holiday-fixed 11 11 "Święto Niepodległości")
        (holiday-fixed 12 25 "Pierwszy dzień Bożego Narodzenia")
        (holiday-fixed 12 26 "Drugi dzień Bożego Narodzenia")))

vterm

(autoload 'multi-vterm-next "multi-vterm" "" t)

avy

(setq avy-keys (append (number-sequence ?a ?z) (number-sequence ?A ?Z)))
(setq avy-background t)

(autoload 'avy-pop-mark "avy" "" t)

server

(defun +maybe-server-start ()
  (when (eq window-system 'x)
    (server-start)))

(add-hook 'after-init-hook #'+maybe-server-start)

ediff

(setq ediff-split-window-function 'split-window-horizontally)
(setq ediff-window-setup-function 'ediff-setup-windows-plain)
(setq +ediff-restore-winconfig-state-on-exit t)

(with-eval-after-load 'ediff-init
  (autoload 'outline-show-all "outline" "" t)
  (add-hook 'ediff-prepare-buffer-hook #'outline-show-all))

vdiff “SPC major”

(with-eval-after-load 'vdiff
  (autoload 'outline-show-all "outline" "" t)
  (add-hook 'vdiff-mode-hook #'outline-show-all))

(leader-key-major-mode-map 'vdiff-mode-map 'vdiff-mode-prefix-map 'vdiff)

xref

(setq xref-prompt-for-identifier nil)

company

(setq company-backends
      '(company-capf
        company-files
        company-ispell))

(setq company-idle-delay 0)
(setq company-minimum-prefix-length 3)
(setq company-require-match nil)
(setq company-tooltip-align-annotations t)
(setq +company-replace-completion t)
(setq +company-ignore-orderless t)

(global-company-mode t)

yasnippet

(setq yas-minor-mode-map (make-sparse-keymap))
(setq yas-alias-to-yas/prefix-p nil)

compile

(setq compilation-read-command nil)
(setq compilation-scroll-output t)

doom-modeline

(setq doom-modeline-height 24)
(setq doom-modeline-buffer-file-name-style 'truncate-with-project)
(setq doom-modeline-icon (if window-system t nil))

(doom-modeline-mode t)

writeroom-mode

(setq writeroom-width 120)
(setq writeroom-restore-window-config t)
(setq writeroom-mode-line t)

(with-eval-after-load 'writeroom-mode
  (+delete-from-list 'writeroom-global-effects 'writeroom-set-fullscreen))

minimap

(setq minimap-window-location 'right)
(setq minimap-minimum-width 20)
(with-eval-after-load 'minimap
  (add-to-list 'minimap-major-modes 'text-mode))

org-mode

(setq org-directory "~/pCloud/Documents/emacs/org")
(defun +org-file-path (filename)
  "Return the absolute address of an org file, given its relative name."
  (concat (file-name-as-directory org-directory) filename))
(setq org-index-file (+org-file-path "index.org"))

(when (file-exists-p org-index-file)
  (setq org-default-notes-file org-index-file)
  (setq org-agenda-files (list org-index-file))
  (setq org-archive-location (concat (+org-file-path "archive.org") "::* From %s")))

(setq org-log-done 'time)
(setq org-edit-src-content-indentation 0)
(setq org-src-fontify-natively t)
(setq org-src-tab-acts-natively t)
(setq org-src-window-setup 'current-window)
(setq org-startup-indented t)
(setq org-support-shift-select t)
(setq org-babel-python-command "python3")
(setq org-confirm-babel-evaluate nil)
(setq org-beamer-theme "Warsaw")
(setq org-highlight-latex-and-related '(latex))
(setq org-export-with-sub-superscripts '{})

(setq org-latex-listings 'minted)
(setq org-latex-packages-alist '(("" "minted")))
(setq org-latex-compiler "xelatex")
(setq org-latex-pdf-process
      '("%latex -shell-escape -interaction nonstopmode -output-directory %o %f"
        "%latex -shell-escape -interaction nonstopmode -output-directory %o %f"
        "%latex -shell-escape -interaction nonstopmode -output-directory %o %f"))

(with-eval-after-load 'org
  (org-babel-do-load-languages 'org-babel-load-languages '((emacs-lisp . t) (python . t) (C . t) (shell . t)))
  (require 'ob-rust)

  (add-hook 'org-mode-hook #'turn-on-auto-fill)
  (add-hook 'org-mode-hook #'org-bullets-mode)

  ;;; was (org-cycle-agenda-files), allow avy
  (define-key org-mode-map (kbd "C-'") nil))

(with-eval-after-load 'ox
  (require 'ox-twbs)
  (require 'ox-beamer))

magit

(setq magit-define-global-key-bindings nil)
(setq magit-repository-directories '(("~/devel/" . 2) ("~/.emacs.d/" . 1) ("~/Documents/" . 1)))
(setq magit-bury-buffer-function 'quit-window)
(setq +magit-dont-restore-window-configuration t)

rust-mode “SPC major”

(setq rust-mode-map (make-sparse-keymap))

(defvar rust-command-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "d") #'rust-dbg-wrap-or-unwrap)
    (define-key map (kbd "c") #'rust-compile)
    (define-key map (kbd "k") #'rust-check)
    (define-key map (kbd "t") #'rust-test)
    (define-key map (kbd "r") #'rust-run)
    (define-key map (kbd "l") #'rust-run-clippy)
    (define-key map (kbd "f") #'rust-format-buffer)
    (define-key map (kbd "n") #'rust-goto-format-problem)
    map))

(leader-key-major-mode-map 'rust-mode-map 'rust-command-map 'rust-mode)

symbol-overlay “SPC o”

(defvar symbol-overlay-command-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "o") #'symbol-overlay-put)
    (define-key map (kbd "r") #'symbol-overlay-remove-all)
    (define-key map (kbd "c") #'symbol-overlay-count)
    (define-key map (kbd "m") #'symbol-overlay-mode)
    (define-key map (kbd "f") #'symbol-overlay-switch-forward)
    (define-key map (kbd "b") #'symbol-overlay-switch-backward)
    (define-key map (kbd "n") #'symbol-overlay-jump-next)
    (define-key map (kbd "p") #'symbol-overlay-jump-prev)
    map))

projectile “SPC p”

(setq projectile-keymap-prefix nil)
(setq projectile-known-projects-file "~/.emacs-projectile.el")
(setq projectile-cache-file "~/.emacs-projectile-cache.el")
(setq projectile-dynamic-mode-line nil)
(setq frame-title-format '((:eval (projectile-project-name))))
(setq project-list-file "~/.emacs-project.el")

(with-eval-after-load 'projectile
  (add-to-list 'projectile-globally-ignored-directories "build")
  (add-to-list 'projectile-globally-ignored-directories "out")
  (add-to-list 'projectile-globally-ignored-directories ".ccls-cache")
  (add-to-list 'projectile-project-root-files-top-down-recurring "compile_commands.json")
  (add-to-list 'projectile-project-root-files-top-down-recurring ".ccls"))

(projectile-mode t)

flycheck “SPC f”

(setq flycheck-mode-map (make-sparse-keymap))
(setq flycheck-mode-line nil)
(setq flycheck-flake8-maximum-line-length 100)
(setq flycheck-idle-change-delay 3)
(setq flycheck-check-syntax-automatically '(save new-line mode-enabled))

(autoload 'flycheck-select-checker "flycheck" "" t)

;;; enable everywhere excluding elisp, it always reports shitload of errors for snippets
(setq-default flycheck-disabled-checkers '(emacs-lisp-checkdoc emacs-lisp))

lsp-mode “SPC l”

(setq lsp-keymap-prefix nil)
(setq lsp-session-file "~/.emacs-lsp-session-v1")
(setq lsp-restart 'ignore)
(setq lsp-enable-symbol-highlighting nil)
(setq lsp-enable-on-type-formatting nil)
(setq lsp-headerline-arrow "/")
(setq lsp-file-watch-threshold 8000)
(setq lsp-lens-enable nil)

(setq lsp-ui-doc-enable nil)
(setq lsp-ui-doc-delay 1)
(setq lsp-ui-doc-alignment 'window)
(setq lsp-ui-doc-show-with-cursor t)
(setq lsp-ui-doc-show-with-mouse nil)

(setq lsp-ui-sideline-delay 1)

(with-eval-after-load 'lsp-mode
  (add-to-list 'lsp-file-watch-ignored-directories "[/\\\\]out\\'")
  (add-to-list 'lsp-file-watch-ignored-directories "[/\\\\]build\\'")
  (add-to-list 'lsp-file-watch-ignored-directories "[/\\\\]\\.ccls-cache\\'"))

;;; Reimplement the original map to something more compact
(setq lsp-command-map (make-sparse-keymap))
(with-eval-after-load 'lsp-mode
  (define-key lsp-command-map (kbd "a") #'lsp-execute-code-action)
  (define-key lsp-command-map (kbd "d") #'lsp-describe-thing-at-point)
  (define-key lsp-command-map (kbd "f") #'lsp-format-buffer)
  (define-key lsp-command-map (kbd "h") #'lsp-document-highlight)
  (define-key lsp-command-map (kbd "i") #'lsp-ui-imenu)
  (define-key lsp-command-map (kbd "l") #'consult-lsp-diagnostics)   ;; lsp-ui-flycheck-list
  (define-key lsp-command-map (kbd "r") #'lsp-rename))

god-mode

(setq god-mode-enable-function-key-translation nil)

(defun +god-mode-update ()
  (setq cursor-type (if (or god-local-mode buffer-read-only) 'box 'bar))
  (hl-line-mode (if (or god-local-mode buffer-read-only) 0 t))
  (when (and overwrite-mode god-local-mode) (overwrite-mode 0)))
(add-hook 'post-command-hook #'+god-mode-update)

;;; enablers and disablers
(with-eval-after-load 'god-mode
  (add-to-list 'god-exempt-major-modes 'vterm-mode t)
  (add-to-list 'god-exempt-major-modes 'finder-mode t)
  (+delete-from-list 'god-exempt-predicates #'god-view-mode-p)

  (global-set-key (kbd "<escape>") #'+god-mode-enable-or-keyboard-quit)
  (define-key god-local-mode-map (kbd "i") #'god-mode-all)
  ;;; broken for now
  (define-key god-local-mode-map (kbd "C-h k") nil))

(god-mode-all 1)

leader-key

(with-eval-after-load 'god-mode
  (define-key god-local-mode-map (kbd leader-key-root-key) nil))

(defun +leader-key-pred ()
  (or buffer-read-only
      god-local-mode))
(setq leader-key-pred #'+leader-key-pred)

(leader-key-mode t)

;;; optional configuration for modes that don't play well
(add-to-list 'leader-key-exempt-major-modes 'vterm-mode t)
(leader-key-do-map 'custom-mode-map 'cus-edit)
(leader-key-do-map 'magit-blame-read-only-mode-map 'magit-blame)
(leader-key-do-map 'compilation-mode-map 'compile)
(leader-key-do-map 'tar-mode-map 'tar-mode)

dashboard

(setq dashboard-items '((recents  . 5)
                        (projects . 5)
                        (bookmarks . 5)))
(setq dashboard-set-heading-icons (if window-system t nil))
(setq dashboard-set-file-icons (if window-system t nil))
(setq dashboard-set-footer nil)
(setq dashboard-startup-banner 'logo)
(setq dashboard-center-content t)

(dashboard-setup-startup-hook)

minibuffer [vertico]

;; (setq enable-recursive-minibuffers t)
(setq read-file-name-completion-ignore-case t)
(setq read-buffer-completion-ignore-case t)
(setq completion-ignore-case t)
(setq +crm-indicator t)

orderless [vertico]

(setq orderless-style-dispatchers '(+vertico-orderless-dispatch))
(setq completion-styles '(orderless basic))
(setq completion-category-defaults nil)
(setq completion-category-overrides '((file (styles orderless partial-completion))))
(with-eval-after-load 'orderless
  (add-to-list 'orderless-matching-styles 'orderless-prefixes t))

vertico

(setq vertico-count 20)
(setq vertico-resize nil)

(add-hook 'minibuffer-setup-hook #'vertico-repeat-save)
(with-eval-after-load 'rfn-eshadow
  (add-hook 'rfn-eshadow-update-overlay-hook #'vertico-directory-tidy))

(vertico-mode t)

consult [vertico]

(setq xref-show-xrefs-function #'consult-xref)
(setq xref-show-definitions-function #'consult-xref)

(setq completion-in-region-function #'consult-completion-in-region)

(setq consult-narrow-key "C-c")
(setq consult-async-min-input 2)
(setq consult-async-refresh-delay  0.1)
(setq consult-async-input-throttle 0.2)
(setq consult-async-input-debounce 0.1)
(setq consult-line-start-from-top t)
(setq +consult-async-show-filename t)

marginalia [vertico]

(with-eval-after-load 'marginalia
  ;; (add-hook 'marginalia-mode-hook #'all-the-icons-completion-marginalia-setup)
  (add-to-list 'marginalia-command-categories '(projectile-find-file . project-file))
  (add-to-list 'marginalia-command-categories '(projectile-recentf . project-file))
  (add-to-list 'marginalia-command-categories '(projectile-switch-to-buffer . buffer))
  (add-to-list 'marginalia-command-categories '(projectile-switch-project . project-file)))

(marginalia-mode t)

embark [vertico]

(setq which-key-use-C-h-commands nil)
(setq prefix-help-command #'embark-prefix-help-command)
(setq embark-quit-after-action '((kill-buffer . nil)
                                 (t . t)))

(with-eval-after-load 'embark
  (add-hook 'embark-collect-mode-hook #'consult-preview-at-point-mode)
  (+delete-from-list 'embark-pre-action-hooks '(kill-buffer embark--confirm)))

wgrep

(setq wgrep-enable-key (kbd "C-x C-q"))
(with-eval-after-load 'wgrep
  (define-key wgrep-mode-map (kbd "C-x C-s") nil)
  (define-key wgrep-mode-map (kbd "C-c C-e") nil))
(with-eval-after-load 'grep
  (define-key grep-mode-map (kbd "e") #'wgrep-change-to-wgrep-mode))

NeoTre

(setq neo-smart-open t)
(setq neo-window-position 'right)

(defun +neo-enter-file-hook-f (type path arg)
  (if (eq type 'file)
      (neotree-hide)))
(add-hook 'neo-enter-hook #'+neo-enter-file-hook-f)

Programming

Text

(defun +text-mode-hook-f ()
  (setq show-trailing-whitespace t)
  (setq fill-column 80))
(add-hook 'text-mode-hook #'+text-mode-hook-f t)

Prog

(defun +prog-devel-hook-f ()
  (+text-mode-hook-f)
  (display-fill-column-indicator-mode t)
  (display-line-numbers-mode t)
  (subword-mode t)
  (rainbow-delimiters-mode t)
  (yas-minor-mode t)
  (flycheck-mode t))
(add-hook 'prog-mode-hook #'+prog-devel-hook-f t)

;;; Should be default but for some reason some package breaks it
(add-to-list 'compilation-environment "TERM=dumb")

CC

(setq ccls-executable "~/Documents/ccls/Release/ccls")
(with-eval-after-load 'cc-mode
  (require 'ccls))

(setq c-basic-offset tab-width)
(defvaralias 'c-basic-offset 'tab-width)
(smart-tabs-insinuate 'c 'c++)
(setq c-tab-always-indent nil)
(setq c-insert-tab-function 'completion-at-point)
(setq c-default-style
      '((c-mode . "linux")
        (c++-mode . "stroustrup")
        (java-mode . "java")
        (awk-mode . "awk")
        (other . "gnu")))

;; (add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))      ; *.h in c++-mode

(defun +cc-devel-hook-f ()
  (c-set-offset 'innamespace 0)
  (c-set-offset 'inextern-lang 0)
  (c-set-offset 'inline-open 0)
  (c-set-offset 'inlambda '+)
  (+with-real-projectile-file t (lsp)))
(with-eval-after-load 'cc-vars
  (add-hook 'c-mode-common-hook #'+cc-devel-hook-f t))

Rust

(with-eval-after-load 'rust-mode
  (require 'lsp-rust))

(defvaralias 'rust-indent-offset 'tab-width)

(defun +rust-devel-f ()
  (setq tab-width 4)
  (setq indent-tabs-mode nil)
  (+with-real-projectile-file t (lsp)))
(with-eval-after-load 'rust-mode
  (add-hook 'rust-mode-hook #'+rust-devel-f t))

Python

(setq lsp-pyls-plugins-pylint-enabled nil) ; it's too noisy
(with-eval-after-load 'python
  (require 'lsp-pyls))

(defvaralias 'python-indent-offset 'tab-width)

(defun +python-devel-hook-f ()
  (setq tab-width 4)
  (setq indent-tabs-mode nil)
  (+with-real-projectile-file t (lsp)))
(with-eval-after-load 'python
  (add-hook 'python-mode-hook #'+python-devel-hook-f t))

LUA

(defvaralias 'lua-indent-level 'tab-width)

(defun +lua-devel-hook-f ()
  (setq require-final-newline nil)
  (setq +delete-trailing-whitespace-on-save nil)
  (setq tab-width 4)
  (setq-local company-backends
              '(company-dabbrev-code
                company-files
                company-ispell)))
(with-eval-after-load 'lua-mode
  (add-hook 'lua-mode-hook #'+lua-devel-hook-f t))

JS

(defvaralias 'js-indent-level 'tab-width)

(defun +js-devel-f ()
  (setq tab-width 4))
(with-eval-after-load 'js
  (add-hook 'js-mode-hook #'+js-devel-f t))

Lisps

;;; repls
(defalias 'run-elisp 'ielm)             ; run-elisp
(setq inferior-lisp-program "sbcl")     ; run-lisp
(setq scheme-program-name "scheme")     ; run-scheme

(defun +lisps-devel-hook-f ()
  (setq indent-tabs-mode nil)
  (when (or (not buffer-file-name)
            (not (equal "init.el" (file-name-nondirectory buffer-file-name))))
    (parinfer-rust-mode t)))
(setq lisps-mode-hooks
      '(emacs-lisp-mode-hook
        lisp-mode-hook
        scheme-mode-hook))
        ;; ielm-mode-hook
        ;; inferior-lisp-mode-hook
        ;; inferior-scheme-mode-hook
(dolist (hook lisps-mode-hooks)
  (add-hook hook #'+lisps-devel-hook-f t))

shell

(defvaralias 'sh-indentation 'tab-width)
(defvaralias 'sh-basic-offset 'tab-width)

(add-to-list 'auto-mode-alist '("bashrc\\." . shell-script-mode))
(add-to-list 'auto-mode-alist '("profile\\'" . shell-script-mode))

;; (add-to-list 'smart-tabs-insinuate-alist
;;              '(sh lambda nil
;;                   (add-hook 'sh-mode-hook
;;                             (lambda nil
;;                               (smart-tabs-mode-enable)
;;                               (smart-tabs-advice sh-basic-indent-line sh-basic-offset)))))

;; (smart-tabs-insinuate 'sh)

(defun +sh-devel-hook-f ()
  (setq tab-width 4)
  (setq-local company-backends
              '(company-dabbrev-code
                company-files
                company-ispell)))
(with-eval-after-load 'sh-script
  (add-hook 'sh-mode-hook #'+sh-devel-hook-f t))

NXML

(defvaralias 'nxml-child-indent 'tab-width)

(defun +nxml-devel-hook-f ()
  (setq tab-width 2))
(with-eval-after-load 'nxml-mode
  (add-hook 'nxml-mode-hook #'+nxml-devel-hook-f t))

Diff

;;; diff mode resets whitespace-style, my styles include face and trailing
(defun +diff-devel-hook-f ()
  (setq-local whitespace-style '(face trailing spaces tabs space-mark tab-mark)))
(with-eval-after-load 'diff-mode
  (add-hook 'diff-mode-hook #'+diff-devel-hook-f t))

Make

(with-eval-after-load 'company-dabbrev-code
  (add-to-list 'company-dabbrev-code-modes 'makefile-mode))

(defun +makefile-devel-hook-f ()
  (setq-local company-backends
              '(company-dabbrev-code
                company-files
                company-ispell)))
(with-eval-after-load 'make-mode
  (add-hook 'makefile-mode-hook #'+makefile-devel-hook-f t))

CMake

(defvaralias 'cmake-tab-width 'tab-width)

(with-eval-after-load 'company-dabbrev-code
  (add-to-list 'company-dabbrev-code-modes 'cmake-mode))

(defun +cmake-devel-hook-f ()
  (setq-local company-backends
              '((company-dabbrev-code company-cmake)
                company-keywords
                company-files
                company-ispell)))
(with-eval-after-load 'cmake-mode
  (add-hook 'cmake-mode-hook #'+cmake-devel-hook-f t))

eshell

(defun +esh-devel-hook-f ()
  (setq-local company-backends
              '(company-capf)))
(with-eval-after-load 'esh-mode
  (add-hook 'eshell-mode-hook #'+esh-devel-hook-f))

LaTeX

(defun +latex-devel-hook-f ()
  (setq-local company-backends
              '(company-files
                company-ispell))
  (company-auctex-init))
(with-eval-after-load 'tex-mode
  (add-hook 'LaTeX-mode-hook #'+latex-devel-hook-f))

Shortcuts

Escape!

(with-eval-after-load 'minibuffer
  (define-key minibuffer-mode-map (kbd "<escape>") #'minibuffer-keyboard-quit))
(with-eval-after-load 'company
  (define-key company-active-map (kbd "<escape>") #'+company-abort-and-god-mode-enable))
(with-eval-after-load 'transient
  (define-key transient-map (kbd "<escape>") #'transient-quit-one))
(with-eval-after-load 'lsp-ui-imenu
  (define-key lsp-ui-imenu-mode-map (kbd "<escape>") #'quit-window))
(with-eval-after-load 'lsp-ui-flycheck
  (define-key lsp-ui-flycheck-list-mode-map (kbd "<escape>") #'quit-window))
(with-eval-after-load 'vundo
  (define-key vundo-mode-map (kbd "<escape>") #'vundo-quit))
(with-eval-after-load 'esh-mode
  (define-key eshell-mode-map (kbd "<escape>") #'quit-window))

Global shortcuts

(global-set-key (kbd "<M-up>") nil)
(global-set-key (kbd "<M-down>") nil)
(global-set-key (kbd "<M-left>") nil)
(global-set-key (kbd "<M-right>") nil)

(global-set-key (kbd "<C-M-up>") #'scroll-down-line)
(global-set-key (kbd "<C-M-down>") #'scroll-up-line)
(global-set-key (kbd "<C-M-left>") #'+scroll-left-small)
(global-set-key (kbd "<C-M-right>") #'+scroll-right-small)

(global-set-key (kbd "<s-up>") #'scroll-down-line)
(global-set-key (kbd "<s-down>") #'scroll-up-line)
(global-set-key (kbd "<s-left>") #'+scroll-left-small)
(global-set-key (kbd "<s-right>") #'+scroll-right-small)

(global-set-key (kbd "<f1>") #'vertico-repeat)                    ;; help prefix

(global-set-key (kbd "M-y") #'consult-yank-from-kill-ring)        ;; yank-pop

(global-set-key (kbd "M-n") #'forward-paragraph)
(global-set-key (kbd "M-p") #'backward-paragraph)

(global-set-key (kbd "M-q") #'unfill-toggle)                      ;; fill-paragraph
(global-set-key (kbd "M-z") #'zap-up-to-char)                     ;; zap-to-char

(global-set-key (kbd "M-/") #'xref-find-references)               ;; dabbrev-expand

(global-set-key (kbd "C-a") #'mwim-beginning-of-line-or-code)     ;; move-end-of-line
(global-set-key (kbd "C-e") #'mwim-end-of-line-or-code)           ;; move-beggining-of-line

(global-set-key (kbd "C-s") #'isearch-forward-regexp)             ;; isearch-forward
(global-set-key (kbd "C-r") #'isearch-backward-regexp)            ;; isearch-backward
(global-set-key (kbd "C-M-s") #'isearch-forward)                  ;; isearch-forward-regexp
(global-set-key (kbd "C-M-r") #'isearch-backward)                 ;; isearch-backward-regexp
(define-key isearch-mode-map (kbd "C-l") #'recenter-top-bottom)
(define-key isearch-mode-map (kbd "C-o") #'consult-line)

(global-set-key (kbd "C-z") #'undo)                               ;; suspend-frame
(global-set-key (kbd "C-S-z") #'undo-redo)
(global-set-key [remap undo] #'undo-only)                         ;; undo

(global-set-key (kbd "C-\\") #'consult-mark)                      ;; toggle-input-method
(global-set-key (kbd "C-|") #'consult-global-mark)

(global-set-key (kbd "C-.") #'consult-imenu)
(global-set-key (kbd "C-,") #'consult-imenu-multi)

(global-set-key (kbd "C-'") #'avy-goto-word-1)
(global-set-key (kbd "C-;") #'avy-pop-mark)
(define-key isearch-mode-map (kbd "C-'") #'avy-isearch)

(pcase system-type
  ('windows-nt
   (global-set-key (kbd "C-`") #'eshell))
  (_
   (global-set-key (kbd "C-`") #'multi-vterm-next)
   (global-set-key (kbd "C-~") #'multi-vterm)))

(global-set-key (kbd "C-o") #'+god-edit-open-line)                ;; open-line
(global-set-key (kbd "C-S-o") #'+god-edit-open-line-prev)
(global-set-key (kbd "C-S-a") #'+god-edit-beginning-of-line)
(global-set-key (kbd "C-S-e") #'+god-edit-end-of-line)
(global-set-key (kbd "C-S-d") #'+god-edit-change-word)
(global-set-key (kbd "C-%") #'+god-edit-match-paren)

(global-set-key (kbd "C-h M") #'describe-minor-mode)                                ;; nil

(global-set-key (kbd "C-x b") #'consult-buffer)                                     ;; switch-to-buffer
(global-set-key (kbd "C-x o") #'switch-window)                                      ;; other-window
(global-set-key (kbd "C-x k") #'+switch-window-then-kill-current-buffer)            ;; kill-buffer
(global-set-key (kbd "C-x 0") #'+switch-window-then-delete-window-and-balance)      ;; delete-window
(global-set-key (kbd "C-x 1") #'switch-window-then-maximize)                        ;; delete-other-windows
(global-set-key (kbd "C-x 2") #'+switch-window-then-split-below-switch-and-balance) ;; split-window-below
(global-set-key (kbd "C-x 3") #'+switch-window-then-split-right-switch-and-balance) ;; split-window-right

Local map shortcuts

(define-key minibuffer-local-map (kbd "C-z") #'embark-act)

(with-eval-after-load 'vertico
  (define-key vertico-map (kbd "<next>") #'vertico-scroll-up)
  (define-key vertico-map (kbd "<prior>") #'vertico-scroll-down)
  (define-key vertico-map (kbd "C-<tab>") #'vertico-next-group)
  (define-key vertico-map (kbd "<backtab>") #'vertico-directory-up)
  ;; (define-key vertico-map (kbd "<backspace>") #'vertico-directory-delete-char)
  (define-key vertico-map (kbd "M-<backspace>") #'vertico-directory-delete-word))

(with-eval-after-load 'org
  (define-key org-mode-map (kbd "C-.") #'consult-org-heading)
  (define-key org-mode-map (kbd "C-c C-'") #'org-edit-special))
(with-eval-after-load 'org-src
  (define-key org-src-mode-map (kbd "C-c C-'") #'org-edit-src-exit))
(with-eval-after-load 'org-keys
  (define-key org-babel-map (kbd "C-k") #'org-babel-remove-result-one-or-many))

(with-eval-after-load 'dired
  (define-key dired-mode-map (kbd "<tab>") #'switch-window)
  (define-key dired-mode-map (kbd "TAB") #'switch-window)
  (define-key dired-mode-map (kbd "<backtab>") #'dired-up-directory))

(with-eval-after-load 'esh-mode
  (define-key eshell-mode-map (kbd "<tab>") #'completion-at-point)
  (define-key eshell-mode-map (kbd "TAB") #'completion-at-point))

(with-eval-after-load 'parinfer-rust-mode
  (define-key parinfer-rust-mode-map (kbd "M-'") #'parinfer-rust-toggle-paren-mode))

(with-eval-after-load 'company
  (define-key company-active-map (kbd "<return>") nil)
  (define-key company-active-map (kbd "RET") nil)
  (define-key company-active-map (kbd "<tab>") #'company-complete-selection)
  (define-key company-active-map (kbd "TAB") #'company-complete-selection)
  (define-key company-active-map (kbd "<backtab>") #'consult-company)
  (define-key company-active-map (kbd "S-<tab>") #'consult-company))

(with-eval-after-load 'company-template
  (define-key company-template-nav-map (kbd "<tab>") nil)
  (define-key company-template-nav-map (kbd "TAB") nil)
  (define-key company-template-nav-map (kbd "C-<tab>") #'company-template-forward-field))

(with-eval-after-load 'lsp
  (define-key lsp-mode-map (kbd "C-.") #'consult-lsp-file-symbols))

(with-eval-after-load 'lsp-ui
  (define-key lsp-ui-mode-map (kbd "C-M-,") #'lsp-ui-find-workspace-symbol)
  (define-key lsp-ui-mode-map (kbd "C-M-.") #'lsp-ui-peek-find-definitions)
  (define-key lsp-ui-mode-map (kbd "C-M-/") #'lsp-ui-peek-find-references))

(with-eval-after-load 'yasnippet
  (define-key yas-keymap (kbd "<tab>") nil)
  (define-key yas-keymap (kbd "TAB") nil)
  (define-key yas-keymap (kbd "<backtab>") 'yas-next-field-or-maybe-expand)
  (define-key yas-keymap (kbd "S-<tab>") 'yas-next-field-or-maybe-expand))

(with-eval-after-load 'projectile
  (define-key projectile-command-map (kbd "SPC") #'consult-projectile))

(with-eval-after-load 'flycheck
  (define-key flycheck-command-map (kbd "SPC") #'consult-flycheck))

Leader shortcuts

;;; command maps defined in this file
(leader-key-define-key "o" symbol-overlay-command-map "symbol-overlay")

;;; command maps defined in packages
(with-eval-after-load 'projectile
  (leader-key-define-key "p" projectile-command-map "projectile"))
(with-eval-after-load 'flycheck
  (leader-key-define-key "f" flycheck-command-map "flycheck"))
(with-eval-after-load 'lsp-mode
  (leader-key-define-key "l" lsp-command-map "lsp"))

;;; transient command maps
(leader-key-define-key "m" #'magit-status)
(leader-key-define-key "g" #'magit-file-dispatch)

(leader-key-define-key "n" #'neotree-show)

(leader-key-define-key "u" #'universal-argument)
(leader-key-define-key "SPC" #'set-mark-command)

(leader-key-define-key "<left>" #'window-jump-left)
(leader-key-define-key "<right>" #'window-jump-right)
(leader-key-define-key "<up>" #'window-jump-up)
(leader-key-define-key "<down>" #'window-jump-down)
(leader-key-define-key "0" #'+switch-window-then-delete-window-and-balance "delete-window")
(leader-key-define-key "1" #'switch-window-then-maximize "maximize")
(leader-key-define-key "2" #'+switch-window-then-split-below-switch-and-balance "split-below")
(leader-key-define-key "3" #'+switch-window-then-split-right-switch-and-balance "split-right")

(leader-key-describe-key "x" "emacs")
(leader-key-define-key "x SPC" #'rectangle-mark-mode)
(leader-key-define-key "x ;" #'eval-expression)
(leader-key-define-key "x e" #'eval-last-sexp)
(leader-key-define-key "x p" #'paradox-list-packages)
(leader-key-define-key "x q" #'save-buffers-kill-terminal)
(leader-key-define-key "x r" #'+eval-and-replace)
(leader-key-define-key "x s" #'save-some-buffers)
(leader-key-define-key "x w" #'write-file)
(leader-key-define-key "x x" #'execute-extended-command)

(leader-key-describe-key "r" "run")
(leader-key-define-key "r t" #'multi-vterm-next)
(leader-key-define-key "r e" #'eshell)

(leader-key-describe-key "t" "toggle")
(leader-key-define-key "t c" #'display-fill-column-indicator-mode)
(leader-key-define-key "t f" #'toggle-frame-fullscreen)
(leader-key-define-key "t F" #'+mac-toggle-frame-fullscreen)
(leader-key-define-key "t l" #'hl-line-mode)
(leader-key-define-key "t m" #'minimap-mode)
(leader-key-define-key "t n" #'display-line-numbers-mode)
(leader-key-define-key "t t" #'toggle-truncate-lines)
(leader-key-define-key "t w" #'whitespace-mode)
(leader-key-define-key "t z" #'writeroom-mode)

(leader-key-describe-key "w" "windows")
(leader-key-define-key "w <left>" #'window-jump-left)
(leader-key-define-key "w <right>" #'window-jump-right)
(leader-key-define-key "w <up>" #'window-jump-up)
(leader-key-define-key "w <down>" #'window-jump-down)
(leader-key-define-key "w 0" #'+switch-window-then-delete-window-and-balance "delete-window")
(leader-key-define-key "w 1" #'switch-window-then-maximize "maximize")
(leader-key-define-key "w 2" #'+switch-window-then-split-below-switch-and-balance "split-below")
(leader-key-define-key "w 3" #'+switch-window-then-split-right-switch-and-balance "split-right")
(leader-key-define-key "w =" #'balance-windows)
(leader-key-define-key "w k" #'+switch-window-then-kill-buffer-and-window-and-balance "kill-buffer-and-window")(leader-key-define-key "w m" #'switch-window-then-maximize "maximize")
(leader-key-define-key "w o" #'switch-window "other-window")
(leader-key-define-key "w r" #'windresize)
(leader-key-define-key "w s" #'switch-window-then-swap-buffer "swap-buffers")

(leader-key-describe-key "d" "diff")
(leader-key-define-key "d e" #'ediff-buffers)
(leader-key-define-key "d E" #'ediff-files)
(leader-key-define-key "d v" #'vdiff-buffers)
(leader-key-define-key "d V" #'vdiff-files)

(leader-key-describe-key "b" "buffers/files")
(leader-key-define-key "b b" #'consult-buffer)
(leader-key-define-key "b d" #'dired)
(leader-key-define-key "b f" #'find-file)
(leader-key-define-key "b g" #'consult-goto-line)
(leader-key-define-key "b h" #'mark-whole-buffer)
(leader-key-define-key "b i" #'ibuffer)
(leader-key-define-key "b j" #'dired-jump)
(leader-key-define-key "b k" #'+switch-window-then-kill-current-buffer "kill-current-buffer")
(leader-key-define-key "b m" #'consult-bookmark)
(leader-key-define-key "b M" #'bookmark-delete)
(leader-key-define-key "b p" #'consult-project-buffer)
(leader-key-define-key "b r" #'consult-recent-file)
(leader-key-define-key "b s" #'+sudo-find-file)
(leader-key-define-key "b S" #'+sudo-this-file)
(leader-key-define-key "b u" #'vundo)
(leader-key-define-key "b v" #'find-alternate-file)
(leader-key-define-key "b x" #'+switch-to-scratch)

(leader-key-describe-key "j" "json?")
(leader-key-define-key "j p" #'json-pretty-print-buffer)

(leader-key-describe-key "c" "spellcheck")
(leader-key-define-key "c b" #'flyspell-buffer)
(leader-key-define-key "c c" #'ispell-word)
(leader-key-define-key "c f" #'flyspell-mode)
(leader-key-define-key "c l" #'+cycle-ispell-languages)
(leader-key-define-key "c p" #'flyspell-prog-mode)

(leader-key-describe-key "h" "help/desc")
(leader-key-define-key "h a" #'helpful-symbol)
(leader-key-define-key "h b" #'embark-bindings)
(leader-key-define-key "h c" #'helpful-callable)
(leader-key-define-key "h e" #'about-emacs)
(leader-key-define-key "h f" #'helpful-function)
(leader-key-define-key "h g" #'customize-group)
(leader-key-define-key "h h" #'helpful-at-point)
(leader-key-define-key "h k" #'helpful-key)
(leader-key-define-key "h K" #'+helpful-keymap)
(leader-key-define-key "h m" #'describe-mode)
(leader-key-define-key "h M" #'describe-minor-mode)
(leader-key-define-key "h p" #'describe-package)
(leader-key-define-key "h w" #'consult-man)
(leader-key-define-key "h W" #'woman)
(leader-key-define-key "h v" #'helpful-variable)
;; (which-key-show-[full-]keymap)
;; (which-key-show-[full-]major-mode)
;; (which-key-show-[full-]minor-mode-keymap)

(leader-key-describe-key "s" "search")
(leader-key-define-key "s r" #'+consult-ripgrep)
(leader-key-define-key "s f" #'+consult-find)
(leader-key-define-key "s g" #'+consult-git-grep)
(leader-key-define-key "s l" #'consult-focus-lines)
(leader-key-define-key "s p" #'+consult-projectile-ripgrep)
(leader-key-define-key "s s" #'+consult-line)

(leader-key-describe-key "TAB" "tabs")
(leader-key-define-key "TAB 0" #'+tab-select-0)
(leader-key-define-key "TAB 1" #'+tab-select-1)
(leader-key-define-key "TAB 2" #'+tab-select-2)
(leader-key-define-key "TAB 3" #'+tab-select-3)
(leader-key-define-key "TAB 4" #'+tab-select-4)
(leader-key-define-key "TAB 5" #'+tab-select-5)
(leader-key-define-key "TAB 6" #'+tab-select-6)
(leader-key-define-key "TAB 7" #'+tab-select-7)
(leader-key-define-key "TAB 8" #'+tab-select-8)
(leader-key-define-key "TAB 9" #'+tab-select-9)
(leader-key-define-key "TAB TAB" #'tab-switch)
(leader-key-define-key "TAB c" #'tab-close)
(leader-key-define-key "TAB C" #'tab-close-other)
(leader-key-define-key "TAB d" #'dired-other-tab)      ;; TODO: doesn't work
(leader-key-define-key "TAB f" #'find-file-other-tab)  ;; TODO: doesn't work
(leader-key-define-key "TAB n" #'tab-new)
(leader-key-define-key "TAB o" #'tab-next)
(leader-key-define-key "TAB r" #'tab-rename)
(leader-key-define-key "TAB <right>" #'tab-next)
(leader-key-define-key "TAB <left>" #'tab-previous)

(leader-key-describe-key "i" "insert")
(leader-key-define-key "i e" #'emojify-insert-emoji)
(leader-key-define-key "i u" #'insert-char)

Old bindings, fallback

(global-set-key (kbd "<f5>") #'projectile-compile-project)
;; (global-set-key (kbd "<f6>") #'+consult-find)
;; (global-set-key (kbd "<f7>") #'+consult-ripgrep)
;; (global-set-key (kbd "<f8>") #'+consult-line)
(global-set-key (kbd "<f9>") #'consult-projectile)
;; (global-set-key (kbd "<f10>") #'consult-flycheck)

Notes

C-x keys:

keyorignewremove?
C-0
C-1
C-2
C-3
C-4
C-5
C-6
C-7
C-8
C-9
C-asave-some-buffers
C-g
C-y
0delete-windowswitch-window-then-delete-window-and-balance
1delete-other-windowsswitch-window-then-maximize
2split-window-belowswitch-window-then-split-below-switch-and-balance
3split-window-rightswitch-window-then-split-right-switch-and-balance
7
9
c
g
j
kkill-bufferswitch-window-then-kill-current-buffer
oother-windowswitch-window
w
y

Navigation

left/right:

  • char
  • word

next/previous:

  • line

forward/backward:

  • char
  • word
  • line
  • sentence
  • paragraph
CtrlMeta
fforw charforw word
bback charback word
nnext lineforw paragraph*
pprev lineback paragraph*
eline endforw sentence
aline beginback sentence
(none)Ctrl
rightright charright word
leftleft charleft word
downnext lineforw paragraph
upprev lineback paragraph