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.

1491 lines
62 KiB

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