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.

1400 lines
53 KiB

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