Human Readable Emacs Configuration using Org mode
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1404 lines
59 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #+TITLE: Emacs Configuration
  2. #+AUTHOR: Levi Olson
  3. #+EMAIL: olson.levi@gmail.com
  4. #+DATE: <2019-01-30 Wed>
  5. #+LANGUAGE: en
  6. #+BABEL: :cache yes
  7. #+HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://thomasf.github.io/solarized-css/solarized-light.min.css" />
  8. #+PROPERTY: header-args :tangle yes
  9. #+OPTIONS: num:10 whn:nil toc:10 H:10
  10. #+STARTUP: content
  11. * Summary
  12. I've really been wanting to have a nicely formatted emacs config file and this is my attempt at it.
  13. * Required Magic
  14. ** Lexical Binding
  15. #+BEGIN_SRC emacs-lisp :results silent
  16. ;;; -*- lexical-binding: t -*-
  17. ;;; DO NOT EDIT THIS FILE DIRECTLY
  18. ;;; EDIT ~init.org~ instead
  19. #+END_SRC
  20. ** The Magical Glue
  21. The following auto compiles the emacs-lisp within the =init.org= file.
  22. Simply run `org-babel-tangle` to make it RAIN!
  23. #+BEGIN_SRC emacs-lisp :results silent
  24. ;; (setq byte-compile-warnings nil)
  25. (defun tangle-init ()
  26. "If the current buffer is 'init.org' the code-blocks are tangled, and the tangled file is compiled."
  27. (when (equal (buffer-file-name)
  28. (expand-file-name (concat user-emacs-directory "init.org")))
  29. ;; Avoid running hooks when tangling.
  30. (let ((prog-mode-hook nil))
  31. (org-babel-tangle)
  32. (byte-compile-file (concat user-emacs-directory "init.el")))))
  33. (add-hook 'after-save-hook 'tangle-init)
  34. #+END_SRC
  35. * Config
  36. ** Packages
  37. #+BEGIN_SRC emacs-lisp :results silent
  38. (require 'package)
  39. (defvar my-packages
  40. '(all-the-icons
  41. anzu
  42. base16-theme
  43. better-defaults
  44. company
  45. company-go
  46. counsel
  47. counsel-projectile
  48. diminish
  49. dockerfile-mode
  50. doom-themes
  51. ein
  52. eldoc-eval
  53. elpy
  54. expand-region
  55. gitignore-mode
  56. go-mode
  57. go-playground
  58. gorepl-mode
  59. flycheck
  60. iedit
  61. ivy
  62. ivy-hydra
  63. json-mode
  64. magit
  65. material-theme
  66. multiple-cursors
  67. projectile
  68. py-autopep8
  69. rainbow-delimiters
  70. shrink-path
  71. tide
  72. typescript-mode
  73. use-package
  74. web-mode
  75. which-key))
  76. (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
  77. (add-to-list 'package-archives '("melpa-stable" . "http://stable.melpa.org/packages/"))
  78. (when (not package-archive-contents)
  79. (package-refresh-contents))
  80. (package-initialize)
  81. (dolist (p my-packages)
  82. (when (not (package-installed-p p))
  83. (package-install p)))
  84. #+END_SRC
  85. ** Better Defaults
  86. #+BEGIN_SRC emacs-lisp :results silent
  87. (require 'better-defaults)
  88. ;; Instead of the annoying giant warning icon, just flash the modeline.
  89. ;; (this happens when you do something like C-g)
  90. (setq ring-bell-function
  91. (lambda ()
  92. (let ((orig-fg (face-foreground 'mode-line)))
  93. (set-face-foreground 'mode-line "#F2804F")
  94. (run-with-idle-timer 0.1 nil
  95. (lambda (fg) (set-face-foreground 'mode-line fg))
  96. orig-fg))))
  97. #+END_SRC
  98. ** Basic Customization
  99. #+BEGIN_SRC emacs-lisp :results silent
  100. (defvar backup-dir (expand-file-name "~/.emacs.d/backup/"))
  101. (defvar autosave-dir (expand-file-name "~/.emacs.d/autosave/"))
  102. (setq inhibit-startup-message t
  103. initial-scratch-message nil
  104. backup-directory-alist (list (cons ".*" backup-dir))
  105. auto-save-list-file-prefix autosave-dir
  106. auto-save-file-name-transforms `((".*" ,autosave-dir t)))
  107. (menu-bar-mode 0)
  108. (scroll-bar-mode 0)
  109. (tool-bar-mode 0)
  110. ;; (load-theme 'doom-city-lights t)
  111. ;; (load-theme 'doom-dracula t)
  112. ;; (load-theme 'doom-nord t)
  113. (load-theme 'doom-one t)
  114. ;; (load-theme 'doom-spacegrey t)
  115. ;; (load-theme 'base16-ocean t)
  116. (load-theme 'base16-onedark t)
  117. (global-linum-mode t)
  118. (global-auto-revert-mode t)
  119. (defalias 'yes-or-no-p 'y-or-n-p)
  120. #+END_SRC
  121. ** Tools
  122. *** General
  123. #+BEGIN_SRC emacs-lisp :results silent
  124. (require 'which-key)
  125. (which-key-setup-minibuffer)
  126. (which-key-mode)
  127. #+END_SRC
  128. *** Company
  129. #+BEGIN_SRC emacs-lisp :results silent
  130. (require 'company)
  131. (add-hook 'after-init-hook 'global-company-mode)
  132. #+END_SRC
  133. *** Diminish
  134. #+BEGIN_SRC emacs-lisp :results silent
  135. (require 'diminish)
  136. (diminish 'auto-revert-mode)
  137. (eval-after-load "company" '(diminish 'company-mode))
  138. (eval-after-load "counsel" '(diminish 'counsel-mode))
  139. (eval-after-load "elpy" '(diminish 'elpy-mode))
  140. (eval-after-load "go-mode" '(diminish 'go-mode))
  141. (eval-after-load "go-playground" '(diminish 'go-playground-mode))
  142. (eval-after-load "gorepl-mode" '(diminish 'gorepl-mode))
  143. (eval-after-load "flycheck" '(diminish 'flycheck-mode))
  144. (eval-after-load "ivy" '(diminish 'ivy-mode))
  145. (eval-after-load "projectile" '(diminish 'projectile-mode))
  146. (eval-after-load "which-key" '(diminish 'which-key-mode))
  147. #+END_SRC
  148. *** Dired
  149. #+BEGIN_SRC emacs-lisp :results silent
  150. (defun dired-mode-setup ()
  151. "Will run as hook for `dired-mode'."
  152. (dired-hide-details-mode 1))
  153. (add-hook 'dired-mode-hook 'dired-mode-setup)
  154. #+END_SRC
  155. *** Ivy
  156. #+BEGIN_SRC emacs-lisp :results silent
  157. (require 'ivy-hydra)
  158. (require 'ivy)
  159. (require 'swiper)
  160. (ivy-mode 1)
  161. (counsel-mode)
  162. (setq ivy-use-virtual-buffers t
  163. enable-recursive-minibuffers t
  164. ivy-height 25
  165. ivy-initial-inputs-alist nil
  166. ivy-extra-directories nil)
  167. (global-set-key (kbd "C-s") 'swiper)
  168. (global-set-key (kbd "C-c C-r") 'ivy-resume)
  169. (global-set-key (kbd "M-x") 'counsel-M-x)
  170. (global-set-key (kbd "C-x C-f") 'counsel-find-file)
  171. (global-set-key (kbd "C-c g") 'counsel-git)
  172. (global-set-key (kbd "C-c j") 'counsel-git-grep)
  173. (global-set-key (kbd "C-c k") 'counsel-ag)
  174. (define-key minibuffer-local-map (kbd "C-r") 'counsel-minibuffer-history)
  175. (defun ivy-open-current-typed-path ()
  176. (interactive)
  177. (when ivy--directory
  178. (let* ((dir ivy--directory)
  179. (text-typed ivy-text)
  180. (path (concat dir text-typed)))
  181. (delete-minibuffer-contents)
  182. (ivy--done path))))
  183. (define-key ivy-minibuffer-map (kbd "<return>") 'ivy-alt-done)
  184. (define-key ivy-minibuffer-map (kbd "C-f") 'ivy-open-current-typed-path)
  185. #+END_SRC
  186. *** Magit
  187. #+BEGIN_SRC emacs-lisp :results silent
  188. (require 'magit)
  189. (global-set-key (kbd "C-x g") 'magit-status)
  190. (global-set-key (kbd "C-c g") 'magit-status)
  191. (setq magit-completing-read-function 'ivy-completing-read)
  192. #+END_SRC
  193. *** Projectile
  194. #+BEGIN_SRC emacs-lisp :results silent
  195. (require 'projectile)
  196. (require 'counsel-projectile)
  197. (projectile-mode)
  198. (setq projectile-mode-line '(:eval (format " %s" (projectile-project-name)))
  199. projectile-remember-window-configs t
  200. projectile-completion-system 'ivy)
  201. (counsel-projectile-mode)
  202. #+END_SRC
  203. ** Development Specific
  204. *** General
  205. #+BEGIN_SRC emacs-lisp :results silent
  206. (require 'rainbow-delimiters)
  207. (global-flycheck-mode)
  208. (add-hook 'before-save-hook 'delete-trailing-whitespace)
  209. (add-hook 'prog-mode-hook 'rainbow-delimiters-mode)
  210. (setq-default indent-tabs-mode nil
  211. tab-width 4)
  212. (defvaralias 'c-basic-offset 'tab-width)
  213. (defvaralias 'cperl-indent-level 'tab-width)
  214. (electric-pair-mode 1)
  215. (show-paren-mode 1)
  216. (require 'dockerfile-mode)
  217. (add-to-list 'auto-mode-alist '("Dockerfile*\\'" . dockerfile-mode))
  218. (require 'gitignore-mode)
  219. (add-to-list 'auto-mode-alist '("gitignore\\'" . gitignore-mode))
  220. (require 'json-mode)
  221. (add-to-list 'auto-mode-alist '("\\.json\\'" . json-mode))
  222. (require 'web-mode)
  223. (add-to-list 'auto-mode-alist '("\\.html\\'" . web-mode))
  224. #+END_SRC
  225. *** Python
  226. #+BEGIN_SRC emacs-lisp :results silent
  227. (elpy-enable)
  228. (setq python-shell-interpreter "jupyter"
  229. python-shell-interpreter-args "console --simple-prompt")
  230. (when (require 'flycheck nil t)
  231. (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
  232. (add-hook 'elpy-mode-hook 'flycheck-mode))
  233. (require 'py-autopep8)
  234. (setq py-autopep8-options '("--ignore=E501"))
  235. (add-hook 'elpy-mode-hook 'py-autopep8-enable-on-save)
  236. #+END_SRC
  237. *** Go
  238. #+BEGIN_SRC emacs-lisp :results silent
  239. (require 'go-mode)
  240. (require 'go-playground)
  241. (require 'gorepl-mode)
  242. (require 'company-go)
  243. (add-to-list 'auto-mode-alist '("\\.go\\'" . go-mode))
  244. (add-hook 'go-mode-hook (lambda ()
  245. (add-hook 'before-save-hook 'gofmnt-before-save)
  246. (local-set-key (kbd "M-.") 'godef-jump)
  247. (local-set-key (kbd "M-,") 'pop-tag-mark)
  248. (set (make-local-variable 'company-backends) '(company-go))
  249. (setq company-tooltip-limit 20
  250. company-idle-delay .3
  251. company-echo-delay 0
  252. company-begin-commands '(self-insert-command))
  253. (gorepl-mode)))
  254. (defun set-exec-path-from-shell-PATH ()
  255. (let ((path-from-shell (replace-regexp-in-string
  256. "[ \t\n]*$"
  257. ""
  258. (shell-command-to-string "$SHELL --login -i -c 'echo $PATH'"))))
  259. (setenv "PATH" path-from-shell)
  260. (setq eshell-path-env path-from-shell)
  261. (setq exec-path (split-string path-from-shell path-separator))))
  262. (when window-system (set-exec-path-from-shell-PATH))
  263. (setenv "GOPATH" "/home/leviolson/go")
  264. (add-to-list 'exec-path "/home/leviolson/go/bin")
  265. #+END_SRC
  266. *** TypeScript
  267. #+BEGIN_SRC emacs-lisp :results silent
  268. (defun setup-tide-mode ()
  269. "Tide setup function."
  270. (interactive)
  271. (tide-setup)
  272. (flycheck-mode +1)
  273. (setq flycheck-check-syntax-automatically '(save mode-enabled))
  274. (eldoc-mode +1)
  275. (tide-hl-identifier-mode +1)
  276. (company-mode +1))
  277. ;; aligns annotation to the right hand side
  278. (setq company-tooltip-align-annotations t)
  279. ;; formats the buffer before saving
  280. (add-hook 'before-save-hook 'tide-format-before-save)
  281. (add-hook 'typescript-mode-hook #'setup-tide-mode)
  282. (require 'typescript-mode)
  283. (require 'tide)
  284. (add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-mode))
  285. (add-hook 'typescript-mode-hook
  286. '(lambda ()
  287. (set (make-local-variable 'company-backends) '(company-tide))
  288. (setq company-tooltip-limit 20
  289. company-idle-delay .3
  290. company-echo-delay 0
  291. company-begin-commands '(self-insert-command)
  292. tide-format-options '(:insertSpaceAfterFunctionKeywordForAnonymousFunctions t :placeOpenBraceOnNewLineForFunctions nil))
  293. (tide-setup)))
  294. #+END_SRC
  295. **** TSX
  296. #+BEGIN_SRC emacs-lisp :results silent
  297. (require 'web-mode)
  298. (add-to-list 'auto-mode-alist '("\\.tsx\\'" . web-mode))
  299. (add-hook 'web-mode-hook
  300. (lambda ()
  301. (when (string-equal "tsx" (file-name-extension buffer-file-name))
  302. (setup-tide-mode))))
  303. ;; enable typescript-tslint checker
  304. (flycheck-add-mode 'typescript-tslint 'web-mode)
  305. #+END_SRC
  306. **** JSX
  307. #+BEGIN_SRC emacs-lisp :results silent
  308. (require 'web-mode)
  309. (add-to-list 'auto-mode-alist '("\\.jsx\\'" . web-mode))
  310. (add-hook 'web-mode-hook
  311. (lambda ()
  312. (when (string-equal "jsx" (file-name-extension buffer-file-name))
  313. (setup-tide-mode))))
  314. ;; configure jsx-tide checker to run after your default jsx checker
  315. (flycheck-add-mode 'javascript-eslint 'web-mode)
  316. (flycheck-add-next-checker 'javascript-eslint 'jsx-tide 'append)
  317. #+END_SRC
  318. *** Org
  319. #+BEGIN_SRC emacs-lisp :results silent
  320. (org-babel-do-load-languages
  321. 'org-babel-load-languages
  322. '((js . t)
  323. (shell . t)
  324. (emacs-lisp . t)))
  325. (defvar org-src-tab-acts-natively)
  326. (setq org-src-tab-acts-natively t)
  327. ;; (setenv "NODE_PATH"
  328. ;; (getenv "NODE_PATH"))
  329. (defvar org-confirm-babel-evaluate)
  330. (defun my-org-confirm-babel-evaluate (lang body)
  331. "Execute certain languages without confirming.
  332. Takes LANG to allow and BODY to execute."
  333. (not (or (string= lang "js")
  334. (string= lang "restclient")
  335. (string= lang "emacs-lisp")
  336. (string= lang "shell"))))
  337. (setq org-confirm-babel-evaluate #'my-org-confirm-babel-evaluate)
  338. (add-to-list 'org-structure-template-alist
  339. (list "e" (concat "#+BEGIN_SRC emacs-lisp :results silent\n"
  340. "\n"
  341. "#+END_SRC")))
  342. (add-to-list 'org-structure-template-alist
  343. (list "j" (concat "#+BEGIN_SRC js :cmd \"babel-node\"\n"
  344. "\n"
  345. "#+END_SRC")))
  346. (add-to-list 'org-structure-template-alist
  347. (list "r" (concat "#+BEGIN_SRC restclient :results raw\n"
  348. "\n"
  349. "#+END_SRC")))
  350. #+END_SRC
  351. ** Functions
  352. #+BEGIN_SRC emacs-lisp :results silent
  353. (defun find-user-init-file ()
  354. "Edit the `~/.emacs.d/init.org' file."
  355. (interactive)
  356. (find-file "~/.emacs.d/init.org"))
  357. (defun load-user-init-file ()
  358. "Reload the `~/.emacs.d/init.elc' file."
  359. (interactive)
  360. (load-file "~/.emacs.d/init.elc"))
  361. (defun jump-to-symbol-internal (&optional backwardp)
  362. "Jumps to the next symbol near the point if such a symbol exists. If BACKWARDP is non-nil it jumps backward."
  363. (let* ((point (point))
  364. (bounds (find-tag-default-bounds))
  365. (beg (car bounds)) (end (cdr bounds))
  366. (str (isearch-symbol-regexp (find-tag-default)))
  367. (search (if backwardp 'search-backward-regexp
  368. 'search-forward-regexp)))
  369. (goto-char (if backwardp beg end))
  370. (funcall search str nil t)
  371. (cond ((<= beg (point) end) (goto-char point))
  372. (backwardp (forward-char (- point beg)))
  373. (t (backward-char (- end point))))))
  374. (defun jump-to-previous-like-this ()
  375. "Jumps to the previous occurrence of the symbol at point."
  376. (interactive)
  377. (jump-to-symbol-internal t))
  378. (defun jump-to-next-like-this ()
  379. "Jumps to the next occurrence of the symbol at point."
  380. (interactive)
  381. (jump-to-symbol-internal))
  382. (defun kill-this-buffer-unless-scratch ()
  383. "Works like `kill-this-buffer' unless the current buffer is the *scratch* buffer. In which case the buffer content is deleted and the buffer is buried."
  384. (interactive)
  385. (if (not (string= (buffer-name) "*scratch*"))
  386. (kill-this-buffer)
  387. (delete-region (point-min) (point-max))
  388. (switch-to-buffer (other-buffer))
  389. (bury-buffer "*scratch*")))
  390. (defun backward-delete-to-word (arg)
  391. "Delete words backward. With ARG, do this many times."
  392. (interactive "p")
  393. (delete-region (point) (progn (backward-to-word arg) (point))))
  394. (defun comment-or-uncomment-region-or-line ()
  395. "Comments or uncomments the region or the current line if there's no active region."
  396. (interactive)
  397. (let (beg end)
  398. (if (region-active-p)
  399. (setq beg (region-beginning) end (region-end))
  400. (setq beg (line-beginning-position) end (line-end-position)))
  401. (comment-or-uncomment-region beg end)))
  402. (defun new-line-below ()
  403. "Create a new line below current line."
  404. (interactive)
  405. (move-end-of-line 1)
  406. (newline-and-indent))
  407. (defun new-line-above ()
  408. "Create a new line above current line."
  409. (interactive)
  410. (move-beginning-of-line 1)
  411. (newline)
  412. (forward-line -1))
  413. (defun duplicate-thing (comment)
  414. "Duplicates the current line, or the region if active. If an argument (COMMENT) is given, the duplicated region will be commented out."
  415. (interactive "P")
  416. (save-excursion
  417. (let ((start (if (region-active-p) (region-beginning) (point-at-bol)))
  418. (end (if (region-active-p) (region-end) (point-at-eol))))
  419. (goto-char end)
  420. (unless (region-active-p)
  421. (newline))
  422. (insert (buffer-substring start end))
  423. (when comment (comment-region start end)))))
  424. (defun tidy ()
  425. "Ident, untabify and unwhitespacify current buffer, or region if active."
  426. (interactive)
  427. (let ((beg (if (region-active-p) (region-beginning) (point-min)))
  428. (end (if (region-active-p) (region-end) (point-max))))
  429. (if (region-active-p) (message "Indenting Region") (message "Indenting File"))
  430. (let ((inhibit-message t))
  431. (indent-region beg end))
  432. (whitespace-cleanup)
  433. (untabify beg (if (< end (point-max)) end (point-max)))))
  434. (defun phil-columns ()
  435. "Good 'ol Phil-Columns."
  436. (interactive)
  437. (message "Good 'ol fill-columns")
  438. (with-output-to-temp-buffer "*PHIL-COLUMN*"
  439. (shell-command "mpv --no-video 'https://www.youtube.com/watch?v=YkADj0TPrJA&t=3m16s' > /dev/null 2>&1 & sleep 7; pkill mpv"))
  440. (other-window 1)
  441. (delete-window))
  442. (declare-function first "Goto FIRST shell.")
  443. (declare-function goto-non-shell-buffer "Goto something other than a shell buffer.")
  444. (declare-function switch-shell "Switch shell.")
  445. (let ((last-shell ""))
  446. (defun toggle-shell ()
  447. (interactive)
  448. (cond ((string-match-p "^\\*shell<[1-9][0-9]*>\\*$" (buffer-name))
  449. (goto-non-shell-buffer))
  450. ((get-buffer last-shell) (switch-to-buffer last-shell))
  451. (t (shell (setq last-shell "*shell<1>*")))))
  452. (defun switch-shell (n)
  453. (let ((buffer-name (format "*shell<%d>*" n)))
  454. (setq last-shell buffer-name)
  455. (cond ((get-buffer buffer-name)
  456. (switch-to-buffer buffer-name))
  457. (t (shell buffer-name)
  458. (rename-buffer buffer-name)))))
  459. (defun goto-non-shell-buffer ()
  460. (let* ((r "^\\*shell<[1-9][0-9]*>\\*$")
  461. (shell-buffer-p (lambda (b) (string-match-p r (buffer-name b))))
  462. (non-shells (cl-remove-if shell-buffer-p (buffer-list))))
  463. (when non-shells
  464. (switch-to-buffer (first non-shells))))))
  465. (defadvice shell (after kill-with-no-query nil activate)
  466. "."
  467. (set-process-query-on-exit-flag (get-buffer-process ad-return-value) nil))
  468. (declare-function comint-truncate-buffer ".")
  469. (defun clear-comint ()
  470. "Run `comint-truncate-buffer' with the `comint-buffer-maximum-size' set to zero."
  471. (interactive)
  472. (let ((comint-buffer-maximum-size 0))
  473. (comint-truncate-buffer)))
  474. (defun c-setup ()
  475. "Compile."
  476. (local-set-key (kbd "C-c C-c") 'compile))
  477. #+END_SRC
  478. ** Bindings
  479. #+BEGIN_SRC emacs-lisp :results silent
  480. (require 'company)
  481. (add-hook 'comint-mode-hook (lambda () (local-set-key (kbd "C-l") 'clear-comint)))
  482. (add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
  483. (add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)
  484. (add-hook 'c-mode-common-hook 'c-setup)
  485. (add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))
  486. (defvar company-active-map (make-keymap)
  487. "Company Mode keymap.")
  488. (defvar custom-bindings (make-keymap)
  489. "A keymap of custom bindings.")
  490. (define-key global-map (kbd "M-p") 'jump-to-previous-like-this)
  491. (define-key global-map (kbd "M-n") 'jump-to-next-like-this)
  492. (define-key global-map (kbd "M-<tab>") 'switch-to-next-buffer)
  493. (define-key global-map (kbd "M-<backspace>")'backward-delete-to-word)
  494. (define-key global-map (kbd "C-<backspace>")'backward-delete-to-word)
  495. (global-set-key (kbd "C-S-<down>") 'mc/mark-next-like-this)
  496. (global-set-key (kbd "C->") 'mc/mark-next-like-this)
  497. (global-set-key (kbd "C-S-<up>") 'mc/mark-previous-like-this)
  498. (global-set-key (kbd "C-<") 'mc/mark-previous-like-this)
  499. (global-set-key (kbd "C-c C->") 'mc/mark-all-like-this)
  500. ;; (dolist (n (number-sequence 1 9))
  501. ;; (global-set-key (kbd (concat "M-" (int-to-string n)))
  502. ;; (lambda () (interactive) (switch-shell n))))
  503. (define-key company-active-map (kbd "C-d") 'company-show-doc-buffer)
  504. (define-key company-active-map (kbd "C-n") 'company-select-next)
  505. (define-key company-active-map (kbd "C-p") 'company-select-previous)
  506. (define-key company-active-map (kbd "<tab>") 'company-complete)
  507. (define-key custom-bindings (kbd "C-c p") 'counsel-projectile-switch-project)
  508. (define-key custom-bindings (kbd "C-c f") 'counsel-projectile-find-file)
  509. (define-key custom-bindings (kbd "C-c m") 'magit-status)
  510. (define-key custom-bindings (kbd "C-c D") 'define-word-at-point)
  511. (define-key custom-bindings (kbd "C-@") 'er/expand-region)
  512. (define-key custom-bindings (kbd "C-#") 'er/contract-region)
  513. (define-key custom-bindings (kbd "C-S-c C-S-c") 'mc/edit-lines)
  514. (define-key custom-bindings (kbd "C-c b") 'ivy-switch-buffer)
  515. (define-key custom-bindings (kbd "C-c l") 'org-store-link)
  516. (define-key custom-bindings (kbd "C-c t") 'org-set-tags)
  517. (define-key custom-bindings (kbd "M-u") 'upcase-dwim)
  518. (define-key custom-bindings (kbd "M-c") 'capitalize-dwim)
  519. (define-key custom-bindings (kbd "M-l") 'downcase-dwim)
  520. (define-key custom-bindings (kbd "M-o") 'other-window)
  521. (define-key custom-bindings (kbd "C-c s") 'ispell-word)
  522. (define-key custom-bindings (kbd "C-c C-d") 'org-capture)
  523. (define-key custom-bindings (kbd "C-c <up>") 'windmove-up)
  524. (define-key custom-bindings (kbd "C-c <down>") 'windmove-down)
  525. (define-key custom-bindings (kbd "C-c <left>") 'windmove-left)
  526. (define-key custom-bindings (kbd "C-c <right>") 'windmove-right)
  527. (define-key custom-bindings (kbd "C-c a") (lambda () (interactive) (org-agenda nil "n")))
  528. (define-key custom-bindings (kbd "C-c e") 'find-user-init-file)
  529. (define-key custom-bindings (kbd "C-x f") 'phil-columns)
  530. (define-key custom-bindings (kbd "C-x k") 'kill-this-buffer-unless-scratch)
  531. (define-key custom-bindings (kbd "C-c d") 'duplicate-thing)
  532. (define-key custom-bindings (kbd "C-c c") 'comment-or-uncomment-region-or-line)
  533. (define-key custom-bindings (kbd "C-;") 'comment-or-uncomment-region-or-line)
  534. (define-key custom-bindings (kbd "C-o") 'new-line-below)
  535. (define-key custom-bindings (kbd "C-S-o") 'new-line-above)
  536. (define-key custom-bindings (kbd "<C-tab>") 'tidy)
  537. (define-key custom-bindings (kbd "M-q") 'kill-this-buffer)
  538. (define-key custom-bindings (kbd "M-RET") '(lambda () (interactive) (term (getenv "SHELL"))))
  539. (define-minor-mode custom-bindings-mode
  540. "A mode that activates custom-bindings."
  541. t nil custom-bindings)
  542. #+END_SRC
  543. ** UI
  544. #+BEGIN_SRC emacs-lisp :results silent
  545. (cond ((member "PragmataPro" (font-family-list))
  546. (set-face-attribute 'default nil :font "PragmataPro-14")))
  547. #+END_SRC
  548. *** Modeline
  549. #+BEGIN_SRC emacs-lisp :results silent
  550. (require 'use-package)
  551. (require 'anzu)
  552. (require 'eldoc-eval)
  553. (require 'iedit)
  554. (require 'projectile)
  555. (require 'all-the-icons)
  556. (defsubst doom--prepare-modeline-segments (segments)
  557. (cl-loop for seg in segments
  558. if (stringp seg)
  559. collect seg
  560. else
  561. collect (list (intern (format "doom-modeline-segment--%s" (symbol-name seg))))))
  562. (defvar doom--transient-counter 0)
  563. (defmacro add-transient-hook! (hook &rest forms)
  564. "Attaches transient forms to a HOOK.
  565. HOOK can be a quoted hook or a sharp-quoted function (which will be advised).
  566. These forms will be evaluated once when that function/hook is first invoked,
  567. then it detaches itself."
  568. (declare (indent 1))
  569. (let ((append (eq (car forms) :after))
  570. (fn (intern (format "doom-transient-hook-%s" (cl-incf doom--transient-counter)))))
  571. `(when ,hook
  572. (fset ',fn
  573. (lambda (&rest _)
  574. ,@forms
  575. (cond ((functionp ,hook) (advice-remove ,hook #',fn))
  576. ((symbolp ,hook) (remove-hook ,hook #',fn)))
  577. (unintern ',fn nil)))
  578. (cond ((functionp ,hook)
  579. (advice-add ,hook ,(if append :after :before) #',fn))
  580. ((symbolp ,hook)
  581. (add-hook ,hook #',fn ,append))))))
  582. (defmacro add-hook! (&rest args)
  583. "A convenience macro for `add-hook'. Takes, in order:
  584. 1. Optional properties :local and/or :append, which will make the hook
  585. buffer-local or append to the list of hooks (respectively),
  586. 2. The hooks: either an unquoted major mode, an unquoted list of major-modes,
  587. a quoted hook variable or a quoted list of hook variables. If unquoted, the
  588. hooks will be resolved by appending -hook to each symbol.
  589. 3. A function, list of functions, or body forms to be wrapped in a lambda.
  590. Examples:
  591. (add-hook! 'some-mode-hook 'enable-something)
  592. (add-hook! some-mode '(enable-something and-another))
  593. (add-hook! '(one-mode-hook second-mode-hook) 'enable-something)
  594. (add-hook! (one-mode second-mode) 'enable-something)
  595. (add-hook! :append (one-mode second-mode) 'enable-something)
  596. (add-hook! :local (one-mode second-mode) 'enable-something)
  597. (add-hook! (one-mode second-mode) (setq v 5) (setq a 2))
  598. (add-hook! :append :local (one-mode second-mode) (setq v 5) (setq a 2))
  599. Body forms can access the hook's arguments through the let-bound variable
  600. `args'."
  601. (declare (indent defun) (debug t))
  602. (let ((hook-fn 'add-hook)
  603. append-p local-p)
  604. (while (keywordp (car args))
  605. (pcase (pop args)
  606. (:append (setq append-p t))
  607. (:local (setq local-p t))
  608. (:remove (setq hook-fn 'remove-hook))))
  609. (let ((hooks (doom--resolve-hook-forms (pop args)))
  610. (funcs
  611. (let ((val (car args)))
  612. (if (memq (car-safe val) '(quote function))
  613. (if (cdr-safe (cadr val))
  614. (cadr val)
  615. (list (cadr val)))
  616. (list args))))
  617. forms)
  618. (dolist (fn funcs)
  619. (setq fn (if (symbolp fn)
  620. `(function ,fn)
  621. `(lambda (&rest _) ,@args)))
  622. (dolist (hook hooks)
  623. (push (if (eq hook-fn 'remove-hook)
  624. `(remove-hook ',hook ,fn ,local-p)
  625. `(add-hook ',hook ,fn ,append-p ,local-p))
  626. forms)))
  627. `(progn ,@(nreverse forms)))))
  628. (defmacro def-modeline-segment! (name &rest forms)
  629. "Defines a modeline segment and byte compiles it."
  630. (declare (indent defun) (doc-string 2))
  631. (let ((sym (intern (format "doom-modeline-segment--%s" name))))
  632. `(progn
  633. (defun ,sym () ,@forms)
  634. ,(unless (bound-and-true-p byte-compile-current-file)
  635. `(let (byte-compile-warnings)
  636. (byte-compile #',sym))))))
  637. (defmacro def-modeline! (name lhs &optional rhs)
  638. "Defines a modeline format and byte-compiles it. NAME is a symbol to identify
  639. it (used by `doom-modeline' for retrieval). LHS and RHS are lists of symbols of
  640. modeline segments defined with `def-modeline-segment!'.
  641. Example:
  642. (def-modeline! minimal
  643. (bar matches \" \" buffer-info)
  644. (media-info major-mode))
  645. (doom-set-modeline 'minimal t)"
  646. (let ((sym (intern (format "doom-modeline-format--%s" name)))
  647. (lhs-forms (doom--prepare-modeline-segments lhs))
  648. (rhs-forms (doom--prepare-modeline-segments rhs)))
  649. `(progn
  650. (defun ,sym ()
  651. (let ((lhs (list ,@lhs-forms))
  652. (rhs (list ,@rhs-forms)))
  653. (let ((rhs-str (format-mode-line rhs)))
  654. (list lhs
  655. (propertize
  656. " " 'display
  657. `((space :align-to (- (+ right right-fringe right-margin)
  658. ,(+ 1 (string-width rhs-str))))))
  659. rhs-str))))
  660. ,(unless (bound-and-true-p byte-compile-current-file)
  661. `(let (byte-compile-warnings)
  662. (byte-compile #',sym))))))
  663. (defun doom-modeline (key)
  664. "Returns a mode-line configuration associated with KEY (a symbol). Throws an
  665. error if it doesn't exist."
  666. (let ((fn (intern (format "doom-modeline-format--%s" key))))
  667. (when (functionp fn)
  668. `(:eval (,fn)))))
  669. (defun doom-set-modeline (key &optional default)
  670. "Set the modeline format. Does nothing if the modeline KEY doesn't exist. If
  671. DEFAULT is non-nil, set the default mode-line for all buffers."
  672. (when-let ((modeline (doom-modeline key)))
  673. (setf (if default
  674. (default-value 'mode-line-format)
  675. (buffer-local-value 'mode-line-format (current-buffer)))
  676. modeline)))
  677. (use-package eldoc-eval
  678. :config
  679. (defun +doom-modeline-eldoc (text)
  680. (concat (when (display-graphic-p)
  681. (+doom-modeline--make-xpm
  682. (face-background 'doom-modeline-eldoc-bar nil t)
  683. +doom-modeline-height
  684. +doom-modeline-bar-width))
  685. text))
  686. ;; Show eldoc in the mode-line with `eval-expression'
  687. (defun +doom-modeline--show-eldoc (input)
  688. "Display string STR in the mode-line next to minibuffer."
  689. (with-current-buffer (eldoc-current-buffer)
  690. (let* ((str (and (stringp input) input))
  691. (mode-line-format (or (and str (or (+doom-modeline-eldoc str) str))
  692. mode-line-format))
  693. mode-line-in-non-selected-windows)
  694. (force-mode-line-update)
  695. (sit-for eldoc-show-in-mode-line-delay))))
  696. (setq eldoc-in-minibuffer-show-fn #'+doom-modeline--show-eldoc)
  697. (eldoc-in-minibuffer-mode +1))
  698. ;; anzu and evil-anzu expose current/total state that can be displayed in the
  699. ;; mode-line.
  700. (use-package anzu
  701. :init
  702. ;; (add-transient-hook! #'ex-start-search (require 'anzu))
  703. ;; (add-transient-hook! #'ex-start-word-search (require 'anzu))
  704. :config
  705. (setq anzu-cons-mode-line-p nil
  706. anzu-minimum-input-length 1
  707. anzu-search-threshold 250)
  708. ;; Avoid anzu conflicts across buffers
  709. (mapc #'make-variable-buffer-local
  710. '(anzu--total-matched anzu--current-position anzu--state
  711. anzu--cached-count anzu--cached-positions anzu--last-command
  712. anzu--last-isearch-string anzu--overflow-p))
  713. ;; Ensure anzu state is cleared when searches & iedit are done
  714. (add-hook 'isearch-mode-end-hook #'anzu--reset-status t)
  715. ;; (add-hook '+evil-esc-hook #'anzu--reset-status t)
  716. (add-hook 'iedit-mode-end-hook #'anzu--reset-status))
  717. ;; Keep `+doom-modeline-current-window' up-to-date
  718. (defvar +doom-modeline-current-window (frame-selected-window))
  719. (defun +doom-modeline|set-selected-window (&rest _)
  720. "Sets `+doom-modeline-current-window' appropriately"
  721. (when-let ((win (frame-selected-window)))
  722. (unless (minibuffer-window-active-p win)
  723. (setq +doom-modeline-current-window win))))
  724. (add-hook 'window-configuration-change-hook #'+doom-modeline|set-selected-window)
  725. (add-hook 'focus-in-hook #'+doom-modeline|set-selected-window)
  726. (advice-add #'handle-switch-frame :after #'+doom-modeline|set-selected-window)
  727. (advice-add #'select-window :after #'+doom-modeline|set-selected-window)
  728. ;; fish-style modeline
  729. (use-package shrink-path
  730. :commands (shrink-path-prompt shrink-path-file-mixed))
  731. ;;
  732. ;; Variables
  733. ;;
  734. (defvar +doom-modeline-height 29
  735. "How tall the mode-line should be (only respected in GUI emacs).")
  736. (defvar +doom-modeline-bar-width 3
  737. "How wide the mode-line bar should be (only respected in GUI emacs).")
  738. (defvar +doom-modeline-vspc
  739. (propertize " " 'face 'variable-pitch)
  740. "TODO")
  741. (defvar +doom-modeline-buffer-file-name-style 'truncate-upto-project
  742. "Determines the style used by `+doom-modeline-buffer-file-name'.
  743. Given ~/Projects/FOSS/emacs/lisp/comint.el
  744. truncate-upto-project => ~/P/F/emacs/lisp/comint.el
  745. truncate-upto-root => ~/P/F/e/lisp/comint.el
  746. truncate-all => ~/P/F/e/l/comint.el
  747. relative-from-project => emacs/lisp/comint.el
  748. relative-to-project => lisp/comint.el
  749. file-name => comint.el")
  750. ;; externs
  751. (defvar anzu--state nil)
  752. (defvar evil-mode nil)
  753. (defvar evil-state nil)
  754. (defvar evil-visual-selection nil)
  755. (defvar iedit-mode nil)
  756. (defvar all-the-icons-scale-factor)
  757. (defvar all-the-icons-default-adjust)
  758. ;;
  759. ;; Custom faces
  760. ;;
  761. (defgroup +doom-modeline nil
  762. ""
  763. :group 'doom)
  764. (defface doom-modeline-buffer-path
  765. '((t (:inherit (mode-line-emphasis bold))))
  766. "Face used for the dirname part of the buffer path."
  767. :group '+doom-modeline)
  768. (defface doom-modeline-buffer-file
  769. '((t (:inherit (mode-line-buffer-id bold))))
  770. "Face used for the filename part of the mode-line buffer path."
  771. :group '+doom-modeline)
  772. (defface doom-modeline-buffer-modified
  773. '((t (:inherit (error bold) :background nil)))
  774. "Face used for the 'unsaved' symbol in the mode-line."
  775. :group '+doom-modeline)
  776. (defface doom-modeline-buffer-major-mode
  777. '((t (:inherit (mode-line-emphasis bold))))
  778. "Face used for the major-mode segment in the mode-line."
  779. :group '+doom-modeline)
  780. (defface doom-modeline-highlight
  781. '((t (:inherit mode-line-emphasis)))
  782. "Face for bright segments of the mode-line."
  783. :group '+doom-modeline)
  784. (defface doom-modeline-panel
  785. '((t (:inherit mode-line-highlight)))
  786. "Face for 'X out of Y' segments, such as `+doom-modeline--anzu', `+doom-modeline--evil-substitute' and
  787. `iedit'"
  788. :group '+doom-modeline)
  789. (defface doom-modeline-info
  790. `((t (:inherit (success bold))))
  791. "Face for info-level messages in the modeline. Used by `*vc'."
  792. :group '+doom-modeline)
  793. (defface doom-modeline-warning
  794. `((t (:inherit (warning bold))))
  795. "Face for warnings in the modeline. Used by `*flycheck'"
  796. :group '+doom-modeline)
  797. (defface doom-modeline-urgent
  798. `((t (:inherit (error bold))))
  799. "Face for errors in the modeline. Used by `*flycheck'"
  800. :group '+doom-modeline)
  801. ;; Bar
  802. (defface doom-modeline-bar '((t (:inherit highlight)))
  803. "The face used for the left-most bar on the mode-line of an active window."
  804. :group '+doom-modeline)
  805. (defface doom-modeline-eldoc-bar '((t (:inherit shadow)))
  806. "The face used for the left-most bar on the mode-line when eldoc-eval is
  807. active."
  808. :group '+doom-modeline)
  809. (defface doom-modeline-inactive-bar '((t (:inherit warning :inverse-video t)))
  810. "The face used for the left-most bar on the mode-line of an inactive window."
  811. :group '+doom-modeline)
  812. ;;
  813. ;; Modeline helpers
  814. ;;
  815. (defsubst active ()
  816. (eq (selected-window) +doom-modeline-current-window))
  817. ;; Inspired from `powerline's `pl/make-xpm'.
  818. (defun +doom-modeline--make-xpm (color height width)
  819. "Create an XPM bitmap."
  820. (propertize
  821. " " 'display
  822. (let ((data (make-list height (make-list width 1)))
  823. (color (or color "None")))
  824. (create-image
  825. (concat
  826. (format "/* XPM */\nstatic char * percent[] = {\n\"%i %i 2 1\",\n\". c %s\",\n\" c %s\","
  827. (length (car data))
  828. (length data)
  829. color
  830. color)
  831. (apply #'concat
  832. (cl-loop with idx = 0
  833. with len = (length data)
  834. for dl in data
  835. do (cl-incf idx)
  836. collect
  837. (concat "\""
  838. (cl-loop for d in dl
  839. if (= d 0) collect (string-to-char " ")
  840. else collect (string-to-char "."))
  841. (if (eq idx len) "\"};" "\",\n")))))
  842. 'xpm t :ascent 'center))))
  843. (defun +doom-modeline-buffer-file-name ()
  844. "Propertized `buffer-file-name' based on `+doom-modeline-buffer-file-name-style'."
  845. (propertize
  846. (pcase +doom-modeline-buffer-file-name-style
  847. ('truncate-upto-project (+doom-modeline--buffer-file-name 'shrink))
  848. ('truncate-upto-root (+doom-modeline--buffer-file-name-truncate))
  849. ('truncate-all (+doom-modeline--buffer-file-name-truncate t))
  850. ('relative-to-project (+doom-modeline--buffer-file-name-relative))
  851. ('relative-from-project (+doom-modeline--buffer-file-name-relative 'include-project))
  852. ('file-name (propertize (file-name-nondirectory buffer-file-name)
  853. 'face
  854. (let ((face (or (and (buffer-modified-p)
  855. 'doom-modeline-buffer-modified)
  856. (and (active)
  857. 'doom-modeline-buffer-file))))
  858. (when face `(:inherit ,face))))))
  859. 'help-echo buffer-file-truename))
  860. (defun +doom-modeline--buffer-file-name-truncate (&optional truncate-tail)
  861. "Propertized `buffer-file-name' that truncates every dir along path.
  862. If TRUNCATE-TAIL is t also truncate the parent directory of the file."
  863. (let ((dirs (shrink-path-prompt (file-name-directory buffer-file-truename)))
  864. (active (active)))
  865. (if (null dirs)
  866. (propertize "%b" 'face (if active 'doom-modeline-buffer-file))
  867. (let ((modified-faces (if (buffer-modified-p) 'doom-modeline-buffer-modified)))
  868. (let ((dirname (car dirs))
  869. (basename (cdr dirs))
  870. (dir-faces (or modified-faces (if active 'doom-modeline-project-root-dir)))
  871. (file-faces (or modified-faces (if active 'doom-modeline-buffer-file))))
  872. (concat (propertize (concat dirname
  873. (if truncate-tail (substring basename 0 1) basename)
  874. "/")
  875. 'face (if dir-faces `(:inherit ,dir-faces)))
  876. (propertize (file-name-nondirectory buffer-file-name)
  877. 'face (if file-faces `(:inherit ,file-faces)))))))))
  878. (defun +doom-modeline--buffer-file-name-relative (&optional include-project)
  879. "Propertized `buffer-file-name' showing directories relative to project's root only."
  880. (let ((root (projectile-project-root))
  881. (active (active)))
  882. (if (null root)
  883. (propertize "%b" 'face (if active 'doom-modeline-buffer-file))
  884. (let* ((modified-faces (if (buffer-modified-p) 'doom-modeline-buffer-modified))
  885. (relative-dirs (file-relative-name (file-name-directory buffer-file-truename)
  886. (if include-project (concat root "../") root)))
  887. (relative-faces (or modified-faces (if active 'doom-modeline-buffer-path)))
  888. (file-faces (or modified-faces (if active 'doom-modeline-buffer-file))))
  889. (if (equal "./" relative-dirs) (setq relative-dirs ""))
  890. (concat (propertize relative-dirs 'face (if relative-faces `(:inherit ,relative-faces)))
  891. (propertize (file-name-nondirectory buffer-file-truename)
  892. 'face (if file-faces `(:inherit ,file-faces))))))))
  893. (defun +doom-modeline--buffer-file-name (truncate-project-root-parent)
  894. "Propertized `buffer-file-name'.
  895. If TRUNCATE-PROJECT-ROOT-PARENT is t space will be saved by truncating it down
  896. fish-shell style.
  897. Example:
  898. ~/Projects/FOSS/emacs/lisp/comint.el => ~/P/F/emacs/lisp/comint.el"
  899. (let* ((project-root (projectile-project-root))
  900. (file-name-split (shrink-path-file-mixed project-root
  901. (file-name-directory buffer-file-truename)
  902. buffer-file-truename))
  903. (active (active)))
  904. (if (null file-name-split)
  905. (propertize "%b" 'face (if active 'doom-modeline-buffer-file))
  906. (pcase-let ((`(,root-path-parent ,project ,relative-path ,filename) file-name-split))
  907. (let ((modified-faces (if (buffer-modified-p) 'doom-modeline-buffer-modified)))
  908. (let ((sp-faces (or modified-faces (if active 'font-lock-comment-face)))
  909. (project-faces (or modified-faces (if active 'font-lock-string-face)))
  910. (relative-faces (or modified-faces (if active 'doom-modeline-buffer-path)))
  911. (file-faces (or modified-faces (if active 'doom-modeline-buffer-file))))
  912. (let ((sp-props `(,@(if sp-faces `(:inherit ,sp-faces)) ,@(if active '(:weight bold))))
  913. (project-props `(,@(if project-faces `(:inherit ,project-faces)) ,@(if active '(:weight bold))))
  914. (relative-props `(,@(if relative-faces `(:inherit ,relative-faces))))
  915. (file-props `(,@(if file-faces `(:inherit ,file-faces)))))
  916. (concat (propertize (if truncate-project-root-parent
  917. root-path-parent
  918. (abbreviate-file-name project-root))
  919. 'face sp-props)
  920. (propertize (concat project "/") 'face project-props)
  921. (if relative-path (propertize relative-path 'face relative-props))
  922. (propertize filename 'face file-props)))))))))
  923. ;;
  924. ;; Segments
  925. ;;
  926. (def-modeline-segment! buffer-default-directory
  927. "Displays `default-directory'. This is for special buffers like the scratch
  928. buffer where knowing the current project directory is important."
  929. (let ((face (if (active) 'doom-modeline-buffer-path)))
  930. (concat (if (display-graphic-p) " ")
  931. (all-the-icons-octicon
  932. "file-directory"
  933. :face face
  934. :v-adjust -0.05
  935. :height 1.25)
  936. (propertize (concat " " (abbreviate-file-name default-directory))
  937. 'face face))))
  938. ;;
  939. (def-modeline-segment! buffer-info
  940. "Combined information about the current buffer, including the current working
  941. directory, the file name, and its state (modified, read-only or non-existent)."
  942. (concat (cond (buffer-read-only
  943. (concat (all-the-icons-octicon
  944. "lock"
  945. :face 'doom-modeline-warning
  946. :v-adjust -0.05)
  947. " "))
  948. ((buffer-modified-p)
  949. (concat (all-the-icons-faicon
  950. "floppy-o"
  951. :face 'doom-modeline-buffer-modified
  952. :v-adjust -0.0575)
  953. " "))
  954. ((and buffer-file-name
  955. (not (file-exists-p buffer-file-name)))
  956. (concat (all-the-icons-octicon
  957. "circle-slash"
  958. :face 'doom-modeline-urgent
  959. :v-adjust -0.05)
  960. " "))
  961. ((buffer-narrowed-p)
  962. (concat (all-the-icons-octicon
  963. "fold"
  964. :face 'doom-modeline-warning
  965. :v-adjust -0.05)
  966. " ")))
  967. (if buffer-file-name
  968. (+doom-modeline-buffer-file-name)
  969. "%b")))
  970. ;;
  971. (def-modeline-segment! buffer-info-simple
  972. "Display only the current buffer's name, but with fontification."
  973. (propertize
  974. "%b"
  975. 'face (cond ((and buffer-file-name (buffer-modified-p))
  976. 'doom-modeline-buffer-modified)
  977. ((active) 'doom-modeline-buffer-file))))
  978. ;;
  979. (def-modeline-segment! buffer-encoding
  980. "Displays the encoding and eol style of the buffer the same way Atom does."
  981. (concat (pcase (coding-system-eol-type buffer-file-coding-system)
  982. (0 "LF ")
  983. (1 "CRLF ")
  984. (2 "CR "))
  985. (let ((sys (coding-system-plist buffer-file-coding-system)))
  986. (cond ((memq (plist-get sys :category) '(coding-category-undecided coding-category-utf-8))
  987. "UTF-8")
  988. (t (upcase (symbol-name (plist-get sys :name))))))
  989. " "))
  990. ;;
  991. (def-modeline-segment! major-mode
  992. "The major mode, including process, environment and text-scale info."
  993. (propertize
  994. (concat (format-mode-line mode-name)
  995. (when (stringp mode-line-process)
  996. mode-line-process)
  997. (and (featurep 'face-remap)
  998. (/= text-scale-mode-amount 0)
  999. (format " (%+d)" text-scale-mode-amount)))
  1000. 'face (if (active) 'doom-modeline-buffer-major-mode)))
  1001. ;;
  1002. (def-modeline-segment! vcs
  1003. "Displays the current branch, colored based on its state."
  1004. (when (and vc-mode buffer-file-name)
  1005. (let* ((backend (vc-backend buffer-file-name))
  1006. (state (vc-state buffer-file-name backend)))
  1007. (let ((face 'mode-line-inactive)
  1008. (active (active))
  1009. (all-the-icons-default-adjust -0.1))
  1010. (concat " "
  1011. (cond ((memq state '(edited added))
  1012. (if active (setq face 'doom-modeline-info))
  1013. (all-the-icons-octicon
  1014. "git-compare"
  1015. :face face
  1016. :v-adjust -0.05))
  1017. ((eq state 'needs-merge)
  1018. (if active (setq face 'doom-modeline-info))
  1019. (all-the-icons-octicon "git-merge" :face face))
  1020. ((eq state 'needs-update)
  1021. (if active (setq face 'doom-modeline-warning))
  1022. (all-the-icons-octicon "arrow-down" :face face))
  1023. ((memq state '(removed conflict unregistered))
  1024. (if active (setq face 'doom-modeline-urgent))
  1025. (all-the-icons-octicon "alert" :face face))
  1026. (t
  1027. (if active (setq face 'font-lock-doc-face))
  1028. (all-the-icons-octicon
  1029. "git-compare"
  1030. :face face
  1031. :v-adjust -0.05)))
  1032. " "
  1033. (propertize (substring vc-mode (+ (if (eq backend 'Hg) 2 3) 2))
  1034. 'face (if active face))
  1035. " ")))))
  1036. ;;
  1037. (defun +doom-ml-icon (icon &optional text face voffset)
  1038. "Displays an octicon ICON with FACE, followed by TEXT. Uses
  1039. `all-the-icons-octicon' to fetch the icon."
  1040. (concat (if vc-mode " " " ")
  1041. (when icon
  1042. (concat
  1043. (all-the-icons-material icon :face face :height 1.1 :v-adjust (or voffset -0.2))
  1044. (if text +doom-modeline-vspc)))
  1045. (when text
  1046. (propertize text 'face face))
  1047. (if vc-mode " " " ")))
  1048. (def-modeline-segment! flycheck
  1049. "Displays color-coded flycheck error status in the current buffer with pretty
  1050. icons."
  1051. (when (boundp 'flycheck-last-status-change)
  1052. (pcase flycheck-last-status-change
  1053. ('finished (if flycheck-current-errors
  1054. (let-alist (flycheck-count-errors flycheck-current-errors)
  1055. (let ((sum (+ (or .error 0) (or .warning 0))))
  1056. (+doom-ml-icon "do_not_disturb_alt"
  1057. (number-to-string sum)
  1058. (if .error 'doom-modeline-urgent 'doom-modeline-warning)
  1059. -0.25)))
  1060. (+doom-ml-icon "check" nil 'doom-modeline-info)))
  1061. ('running (+doom-ml-icon "access_time" nil 'font-lock-doc-face -0.25))
  1062. ('no-checker (+doom-ml-icon "sim_card_alert" "-" 'font-lock-doc-face))
  1063. ('errored (+doom-ml-icon "sim_card_alert" "Error" 'doom-modeline-urgent))
  1064. ('interrupted (+doom-ml-icon "pause" "Interrupted" 'font-lock-doc-face)))))
  1065. ;; ('interrupted (+doom-ml-icon "x" "Interrupted" 'font-lock-doc-face)))))
  1066. ;;
  1067. (defsubst doom-column (pos)
  1068. (save-excursion (goto-char pos)
  1069. (current-column)))
  1070. (def-modeline-segment! selection-info
  1071. "Information about the current selection, such as how many characters and
  1072. lines are selected, or the NxM dimensions of a block selection."
  1073. (when (and (active) (or mark-active (eq evil-state 'visual)))
  1074. (let ((reg-beg (region-beginning))
  1075. (reg-end (region-end)))
  1076. (propertize
  1077. (let ((lines (count-lines reg-beg (min (1+ reg-end) (point-max)))))
  1078. (cond ((or (bound-and-true-p rectangle-mark-mode)
  1079. (eq 'block evil-visual-selection))
  1080. (let ((cols (abs (- (doom-column reg-end)
  1081. (doom-column reg-beg)))))
  1082. (format "%dx%dB" lines cols)))
  1083. ((eq 'line evil-visual-selection)
  1084. (format "%dL" lines))
  1085. ((> lines 1)
  1086. (format "%dC %dL" (- (1+ reg-end) reg-beg) lines))
  1087. (t
  1088. (format "%dC" (- (1+ reg-end) reg-beg)))))
  1089. 'face 'doom-modeline-highlight))))
  1090. ;;
  1091. (defun +doom-modeline--macro-recording ()
  1092. "Display current Emacs or evil macro being recorded."
  1093. (when (and (active) (or defining-kbd-macro executing-kbd-macro))
  1094. (let ((sep (propertize " " 'face 'doom-modeline-panel)))
  1095. (concat sep
  1096. (propertize (if (bound-and-true-p evil-this-macro)
  1097. (char-to-string evil-this-macro)
  1098. "Macro")
  1099. 'face 'doom-modeline-panel)
  1100. sep
  1101. (all-the-icons-octicon "triangle-right"
  1102. :face 'doom-modeline-panel
  1103. :v-adjust -0.05)
  1104. sep))))
  1105. (defsubst +doom-modeline--anzu ()
  1106. "Show the match index and total number thereof. Requires `anzu', also
  1107. `evil-anzu' if using `evil-mode' for compatibility with `evil-search'."
  1108. (when (and anzu--state (not iedit-mode))
  1109. (propertize
  1110. (let ((here anzu--current-position)
  1111. (total anzu--total-matched))
  1112. (cond ((eq anzu--state 'replace-query)
  1113. (format " %d replace " total))
  1114. ((eq anzu--state 'replace)
  1115. (format " %d/%d " here total))
  1116. (anzu--overflow-p
  1117. (format " %s+ " total))
  1118. (t
  1119. (format " %s/%d " here total))))
  1120. 'face (if (active) 'doom-modeline-panel))))
  1121. (defsubst +doom-modeline--evil-substitute ()
  1122. "Show number of matches for evil-ex substitutions and highlights in real time."
  1123. (when (and evil-mode
  1124. (or (assq 'evil-ex-substitute evil-ex-active-highlights-alist)
  1125. (assq 'evil-ex-global-match evil-ex-active-highlights-alist)
  1126. (assq 'evil-ex-buffer-match evil-ex-active-highlights-alist)))
  1127. (propertize
  1128. (let ((range (if evil-ex-range
  1129. (cons (car evil-ex-range) (cadr evil-ex-range))
  1130. (cons (line-beginning-position) (line-end-position))))
  1131. (pattern (car-safe (evil-delimited-arguments evil-ex-argument 2))))
  1132. (if pattern
  1133. (format " %s matches " (how-many pattern (car range) (cdr range)))
  1134. " - "))
  1135. 'face (if (active) 'doom-modeline-panel))))
  1136. (defun doom-themes--overlay-sort (a b)
  1137. (< (overlay-start a) (overlay-start b)))
  1138. (defsubst +doom-modeline--iedit ()
  1139. "Show the number of iedit regions matches + what match you're on."
  1140. (when (and iedit-mode iedit-occurrences-overlays)
  1141. (propertize
  1142. (let ((this-oc (or (let ((inhibit-message t))
  1143. (iedit-find-current-occurrence-overlay))
  1144. (progn (iedit-prev-occurrence)
  1145. (iedit-find-current-occurrence-overlay))))
  1146. (length (length iedit-occurrences-overlays)))
  1147. (format " %s/%d "
  1148. (if this-oc
  1149. (- length
  1150. (length (memq this-oc (sort (append iedit-occurrences-overlays nil)
  1151. #'doom-themes--overlay-sort)))
  1152. -1)
  1153. "-")
  1154. length))
  1155. 'face (if (active) 'doom-modeline-panel))))
  1156. (def-modeline-segment! matches
  1157. "Displays: 1. the currently recording macro, 2. A current/total for the
  1158. current search term (with anzu), 3. The number of substitutions being conducted
  1159. with `evil-ex-substitute', and/or 4. The number of active `iedit' regions."
  1160. (let ((meta (concat (+doom-modeline--macro-recording)
  1161. (+doom-modeline--anzu)
  1162. (+doom-modeline--evil-substitute)
  1163. (+doom-modeline--iedit))))
  1164. (or (and (not (equal meta "")) meta)
  1165. (if buffer-file-name " %I "))))
  1166. ;; TODO Include other information
  1167. (def-modeline-segment! media-info
  1168. "Metadata regarding the current file, such as dimensions for images."
  1169. (cond ((eq major-mode 'image-mode)
  1170. (cl-destructuring-bind (width . height)
  1171. (image-size (image-get-display-property) :pixels)
  1172. (format " %dx%d " width height)))))
  1173. (def-modeline-segment! bar
  1174. "The bar regulates the height of the mode-line in GUI Emacs.
  1175. Returns \"\" to not break --no-window-system."
  1176. (if (display-graphic-p)
  1177. (+doom-modeline--make-xpm
  1178. (face-background (if (active)
  1179. 'doom-modeline-bar
  1180. 'doom-modeline-inactive-bar)
  1181. nil t)
  1182. +doom-modeline-height
  1183. +doom-modeline-bar-width)
  1184. ""))
  1185. ;;
  1186. ;; Mode lines
  1187. ;;
  1188. (def-modeline! main
  1189. (bar matches " " buffer-info " %l:%c %p " selection-info)
  1190. (buffer-encoding major-mode vcs flycheck))
  1191. (def-modeline! minimal
  1192. (bar matches " " buffer-info)
  1193. (media-info major-mode))
  1194. (def-modeline! special
  1195. (bar matches " " buffer-info-simple " %l:%c %p " selection-info)
  1196. (buffer-encoding major-mode flycheck))
  1197. (def-modeline! project
  1198. (bar buffer-default-directory)
  1199. (major-mode))
  1200. (def-modeline! media
  1201. (bar " %b ")
  1202. (media-info major-mode))
  1203. ;;
  1204. ;; Hooks
  1205. ;;
  1206. (defun +doom-modeline|init ()
  1207. "Set the default modeline."
  1208. (doom-set-modeline 'main t)
  1209. ;; This scratch buffer is already created and doesn't get a modeline. For the
  1210. ;; love of Emacs, someone give the man a modeline!
  1211. (with-current-buffer "*scratch*"
  1212. (doom-set-modeline 'main)))
  1213. (defun +doom-modeline|set-special-modeline ()
  1214. (doom-set-modeline 'special))
  1215. (defun +doom-modeline|set-media-modeline ()
  1216. (doom-set-modeline 'media))
  1217. (defun +doom-modeline|set-project-modeline ()
  1218. (doom-set-modeline 'project))
  1219. ;;
  1220. ;; Bootstrap
  1221. ;;
  1222. (add-hook 'emacs-startup-hook #'+doom-modeline|init)
  1223. ;; (add-hook 'doom-scratch-buffer-hook #'+doom-modeline|set-special-modeline)
  1224. ;; (add-hook '+doom-dashboard-mode-hook #'+doom-modeline|set-project-modeline)
  1225. (add-hook 'image-mode-hook #'+doom-modeline|set-media-modeline)
  1226. (add-hook 'org-src-mode-hook #'+doom-modeline|set-special-modeline)
  1227. (add-hook 'circe-mode-hook #'+doom-modeline|set-special-modeline)
  1228. #+END_SRC