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.

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