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.

1405 lines
59 KiB

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