My attempt to optimize my emacs load time <1 second
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.

601 lines
19 KiB

  1. ;; -*- lexical-binding: t; -*-
  2. (defvar file-name-handler-alist-old file-name-handler-alist)
  3. (setq file-name-handler-alist nil
  4. message-log-max 16384
  5. gc-cons-threshold 402653184
  6. gc-cons-percentage 0.6
  7. auto-window-vscroll nil)
  8. (add-hook 'after-init-hook
  9. `(lambda ()
  10. (setq file-name-handler-alist file-name-handler-alist-old
  11. gc-cons-threshold 800000
  12. gc-cons-percentage 0.1)
  13. (garbage-collect)) t)
  14. ;; Configure Melpa
  15. ;; Package management
  16. ;; set load-path manually
  17. ;; don't call package-initialize
  18. (eval-and-compile
  19. (setq load-prefer-newer t
  20. package-user-dir "~/.emacs.d/elpa"
  21. package--init-file-ensured t ; so it doesn't call package initialize
  22. package-enable-at-startup nil) ; do not automatically load packages
  23. (unless (file-directory-p package-user-dir)
  24. (make-directory package-user-dir t)))
  25. (setq use-package-verbose t
  26. use-package-always-defer nil ;I'm not used to that
  27. use-package-minimum-reported-time 0.01)
  28. ;; Initialize package management
  29. (eval-when-compile ; when byte compiled skip this
  30. (require 'package)
  31. ;; add aditional package archives
  32. (setq package-archives
  33. `(("gnu" . "https://elpa.gnu.org/packages/")
  34. ("melpa" . "https://melpa.org/packages/")
  35. ("org" . "https://orgmode.org/elpa/")))
  36. ;; initialize packages and ensure that use-package is installed
  37. (package-initialize)
  38. (unless (package-installed-p 'use-package)
  39. (package-refresh-contents)
  40. (package-install 'use-package)) ; install if it's missing
  41. (require 'use-package)
  42. (setq use-package-always-ensure t))
  43. ;; Define custom keymap prefixes
  44. (eval-and-compile
  45. (mapc #'(lambda (entry)
  46. (define-prefix-command (cdr entry))
  47. (bind-key (car entry) (cdr entry)))
  48. '(
  49. ;; ("C-," . my-ctrl-comma-map)
  50. ;; ("<C-m>" . my-ctrl-m-map)
  51. ;; ("C-h e" . my-ctrl-h-e-map)
  52. ;; ("C-h x" . my-ctrl-h-x-map)
  53. ("C-c b" . my-ctrl-c-b-map)
  54. ;; ("C-c e" . my-ctrl-c-e-map)
  55. ;; ("C-c m" . my-ctrl-c-m-map)
  56. ;; ("C-c w" . my-ctrl-c-w-map)
  57. ;; ("C-c y" . my-ctrl-c-y-map)
  58. ;; ("C-c H" . my-ctrl-c-H-map)
  59. ;; ("C-c N" . my-ctrl-c-N-map)
  60. ;; ("C-c (" . my-ctrl-c-open-paren-map)
  61. ;; ("C-c -" . my-ctrl-c-minus-map)
  62. ;; ("C-c =" . my-ctrl-c-equals-map)
  63. ;; ("C-c ." . my-ctrl-c-r-map)
  64. )))
  65. ;; Compile function
  66. (defun compile-init ()
  67. "If the current buffer is 'init.el' tangled file is compiled."
  68. (when (equal (buffer-file-name)
  69. (expand-file-name (concat user-emacs-directory "init.el")))
  70. ;; Avoid running hooks when tangling.
  71. (let ((prog-mode-hook nil))
  72. (byte-compile-file (concat user-emacs-directory "init.el")))))
  73. ;; (add-hook 'after-save-hook 'compile-init)
  74. ;; Some Basic Init
  75. (defvar backup-dir (expand-file-name "~/.emacs.d/backup/"))
  76. (defvar autosave-dir (expand-file-name "~/.emacs.d/autosave/"))
  77. (setq initial-scratch-message nil
  78. backup-directory-alist (list (cons ".*" backup-dir))
  79. auto-save-list-file-prefix autosave-dir
  80. auto-save-file-name-transforms `((".*" ,autosave-dir t)))
  81. ;; Turn off the menus, scrollbars, and toolbars
  82. (menu-bar-mode 0)
  83. (scroll-bar-mode 0)
  84. (tool-bar-mode 0)
  85. (horizontal-scroll-bar-mode 0)
  86. ;; Revert to file on disk if it changes
  87. (global-auto-revert-mode t)
  88. ;; Highlight the current line
  89. (global-hl-line-mode t)
  90. ;; Show the column number after the line number (i.e. 50:57)
  91. (column-number-mode t)
  92. ;; C-c <left>|<right> to undo|redo window changes like adding buffers.
  93. (winner-mode t)
  94. ;; Show matching paren
  95. (show-paren-mode t)
  96. ;; Indent w/spaces only
  97. (setq indent-tabs-mode nil)
  98. ;; Prevent stale elisp bytecode
  99. (setq load-prefer-newer t)
  100. ;; Add newline on save
  101. (setq require-final-newline t)
  102. ;; C-c to copy in Linux can be pasted in emacs
  103. (setq select-enable-clipboard t)
  104. ;; after mouse selection can be pasted in emacs
  105. (setq select-enable-primary t)
  106. ;; Paste at current point, not mouse location when "middle-clicking"
  107. (setq mouse-yank-at-point t)
  108. (add-hook 'before-save-hook 'delete-trailing-whitespace)
  109. ;; Set auth source
  110. ;; (setq auth-sources '("~/.authinfo.gpg"))
  111. (defalias 'yes-or-no-p 'y-or-n-p)
  112. (setq browse-url-generic-program "brave"
  113. browse-url-browser-function 'browse-url-generic)
  114. (defun display-startup-echo-area-message ()
  115. "Display startup echo area message."
  116. (message "Initialized in %s" (emacs-init-time)))
  117. (defun leo--find-user-init-file ()
  118. "Edit the `~/.emacs.d/init.el' file."
  119. (interactive)
  120. (find-file "~/.emacs.d/init.el"))
  121. (defun leo--tidy ()
  122. "Indent, untabify and unwhitespacify current buffer, or region if active."
  123. (interactive)
  124. (let ((beg (if (region-active-p) (region-beginning) (point-min)))
  125. (end (if (region-active-p) (region-end) (point-max))))
  126. (let ((inhibit-message t))
  127. (indent-region beg end))
  128. (whitespace-cleanup)
  129. (untabify beg (if (< end (point-max)) end (point-max)))
  130. (if (region-active-p) (message "Indenting Region...Done") (message "Indenting File...Done"))))
  131. (defun leo--comment-or-uncomment-region-or-line ()
  132. "Comment or uncomment the region or the current line if there's no active region."
  133. (interactive)
  134. (let (beg end)
  135. (if (region-active-p)
  136. (setq beg (region-beginning) end (region-end))
  137. (setq beg (line-beginning-position) end (line-end-position)))
  138. (comment-or-uncomment-region beg end)))
  139. (defun leo--duplicate-thing (comment)
  140. "Duplicates the current line, or the region if active. If an argument (COMMENT) is given, the duplicated region will be commented out."
  141. (interactive "P")
  142. (save-excursion
  143. (let ((start (if (region-active-p) (region-beginning) (point-at-bol)))
  144. (end (if (region-active-p) (region-end) (point-at-eol))))
  145. (goto-char end)
  146. (unless (region-active-p)
  147. (newline))
  148. (insert (buffer-substring start end))
  149. (when comment (comment-region start end)))))
  150. (defun leo--kill-this-buffer-unless-scratch ()
  151. "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."
  152. (interactive)
  153. (if (or (string= (buffer-name) "*dashboard*") (string= (buffer-name) "*scratch*"))
  154. (progn
  155. (bury-buffer (buffer-name))
  156. (switch-to-buffer (other-buffer)))
  157. (kill-this-buffer)))
  158. (defun leo--jump-to-symbol (&optional backwardp)
  159. "Jumps to the next symbol near the point if such a symbol exists. If BACKWARDP is non-nil it jumps backward."
  160. (let* ((point (point))
  161. (bounds (find-tag-default-bounds))
  162. (beg (car bounds)) (end (cdr bounds))
  163. (str (isearch-symbol-regexp (find-tag-default)))
  164. (search (if backwardp 'search-backward-regexp
  165. 'search-forward-regexp)))
  166. (goto-char (if backwardp beg end))
  167. (funcall search str nil t)
  168. (cond ((<= beg (point) end) (goto-char point))
  169. (backwardp (forward-char (- point beg)))
  170. (t (backward-char (- end point))))))
  171. (defun leo--jump-to-prev-symbol ()
  172. "Jumps to the previous occurrence of the symbol at point."
  173. (interactive)
  174. (leo--jump-to-symbol t))
  175. (defun leo--jump-to-next-symbol ()
  176. "Jumps to the next occurrence of the symbol at point."
  177. (interactive)
  178. (leo--jump-to-symbol))
  179. ;; USE PACKAGE help
  180. ;; (use-package package-here
  181. ;; :commands (cmd cmd-all cmd-etc) ;; List commands used to ":defer" the package
  182. ;; :bind-keymap
  183. ;; ("M-q" . package-here-keymap) ;; Setup an entire keymap using Prefix "M-q"
  184. ;; :bind
  185. ;; (("M-s" . cmd) ;; Available Globally
  186. ;; :map here-mode-map
  187. ;; ("C-t" . cmd-all) ;; Available only in "here-mode"
  188. ;; ("C-e" . cmd-etc)) ;; Available only in "here-mode"
  189. ;; :init
  190. ;; (setq stuff t) ;; declar vars etc...
  191. ;; :config
  192. ;; (here-mode 1) ;; eval stuff here after the init
  193. ;;
  194. ;; (use-package ruby-mode
  195. ;; :mode "\\.rb\\'"
  196. ;; :interpreter "ruby"
  197. ;;
  198. ;; ;; OR when the package name isn't the same as the =mode=
  199. ;; (use-package python
  200. ;; :mode ("\\.py\\'" . python-mode)
  201. ;; :interpreter ("python" . python-mode))
  202. ;;
  203. ;; USE ":defer" when you aren't using either :commands, :bind, :bind*, :bind-keymap, :bind-keymap*, :mode, :interpreter, or :hook
  204. ;; (use-package deferred-package
  205. ;; :defer t)
  206. ;; https://github.com/jwiegley/use-package/blob/master/bind-key.el
  207. (use-package bind-key
  208. :ensure t
  209. :bind
  210. ("C-<tab>" . leo--tidy)
  211. ("C-;" . leo--comment-or-uncomment-region-or-line)
  212. ("C-c e" . leo--find-user-init-file)
  213. ("M-q" . leo--kill-this-buffer-unless-scratch)
  214. ("C-c d" . leo--duplicate-thing)
  215. ("M-n" . leo--jump-to-next-symbol)
  216. ("M-p" . leo--jump-to-prev-symbol)
  217. ("M-u" . upcase-dwim)
  218. ("M-c" . capitalize-dwim)
  219. ("M-l" . downcase-dwim)
  220. )
  221. ;; https://github.com/winterTTr/ace-jump-mode
  222. (use-package ace-jump-mode
  223. :bind
  224. (("C-c SPC" . just-one-space)
  225. ("M-SPC" . ace-jump-mode)))
  226. ;; https://github.com/abo-abo/ace-window
  227. (use-package ace-window
  228. :ensure t
  229. :bind
  230. ("M-o" . ace-window)
  231. :init
  232. (setq aw-keys '(?a ?s ?h ?t ?g ?y ?n ?e ?o ?i))
  233. )
  234. ;; https://github.com/hlissner/emacs-doom-themes
  235. (use-package doom-themes
  236. :ensure t
  237. :config
  238. (load-theme 'doom-one t))
  239. ;; https://github.com/belak/base16-emacs
  240. (use-package base16-theme
  241. :disabled
  242. :ensure t
  243. :config
  244. (load-theme 'base16-onedark t))
  245. ;; https://github.com/Malabarba/beacon
  246. (use-package beacon
  247. :diminish
  248. :commands beacon-mode
  249. :init
  250. (setq beacon-size 80))
  251. ;; https://github.com/coldnew/linum-relative
  252. (use-package linum-relative
  253. :ensure t
  254. :config
  255. (linum-relative-mode))
  256. ;; https://github.com/Fanael/rainbow-delimiters
  257. (use-package rainbow-delimiters
  258. :ensure t
  259. :hook (prog-mode . rainbow-delimiters-mode))
  260. ;; M-x all-the-icons-install-fonts
  261. (use-package all-the-icons
  262. :ensure t)
  263. (use-package doom-modeline
  264. :ensure t
  265. :after (all-the-icons)
  266. :hook (after-init . doom-modeline-mode))
  267. (use-package dashboard
  268. :ensure t
  269. :init
  270. (setq dashboard-items '((recents . 6)
  271. (bookmarks . 5)
  272. ;; (projects . 5)
  273. (agenda . 5)))
  274. (setq dashboard-center-content t)
  275. (setq dashboard-banner-logo-title "Let's begin...")
  276. (setq dashboard-startup-banner 1)
  277. (setq dashboard-show-shortcuts t)
  278. (setq show-week-agenda-p t)
  279. (setq dashboard-org-agenda-categories '("work" "tasks"))
  280. :config
  281. (dashboard-setup-startup-hook))
  282. (use-package helm
  283. :ensure t
  284. :demand t
  285. :bind
  286. ("M-x" . helm-M-x)
  287. ("C-c b" . helm-buffers-list)
  288. ("C-x C-f" . helm-find-files))
  289. ;; https://github.com/emacsorphanage/helm-swoop
  290. (use-package helm-swoop
  291. :ensure t
  292. :after (helm)
  293. :bind
  294. ("C-s" . helm-swoop)
  295. :config
  296. ;; If you prefer fuzzy matching
  297. (setq helm-swoop-use-fuzzy-match t))
  298. ;; https://github.com/syohex/emacs-helm-ag
  299. (use-package helm-ag
  300. :ensure t
  301. :after (helm)
  302. :bind
  303. ("C-c k" . helm-ag))
  304. ;; https://github.com/magnars/expand-region.el
  305. (use-package expand-region
  306. :ensure t
  307. :bind
  308. ("C-@" . er/expand-region)
  309. ("C-#" . er/contract-region))
  310. ;; https://github.com/magnars/multiple-cursors.el
  311. (use-package multiple-cursors
  312. :ensure t
  313. :bind
  314. ("C-}" . mc/mark-next-like-this)
  315. ("C-)" . mc/unmark-next-like-this)
  316. ("C-{" . mc/mark-previous-like-this)
  317. ("C-(" . mc/unmark-previous-like-this))
  318. ;; https://github.com/joodland/bm
  319. (use-package bm
  320. :bind (("C-c b b" . bm-toggle)
  321. ("C-c b n" . bm-next)
  322. ("C-c b p" . bm-previous))
  323. :commands (bm-repository-load
  324. bm-buffer-save
  325. bm-buffer-save-all
  326. bm-buffer-restore)
  327. :init
  328. (setq bm-restore-repository-on-load t)
  329. (setq bm-repository-file (concat user-emacs-directory "bookmark-repo"))
  330. (add-hook 'after-init-hook 'bm-repository-load)
  331. (add-hook 'find-file-hooks 'bm-buffer-restore)
  332. (add-hook 'after-revert-hook #'bm-buffer-restore)
  333. (add-hook 'kill-buffer-hook #'bm-buffer-save)
  334. (add-hook 'after-save-hook #'bm-buffer-save)
  335. (add-hook 'vc-before-checkin-hook #'bm-buffer-save)
  336. (add-hook 'kill-emacs-hook #'(lambda nil
  337. (bm-buffer-save-all)
  338. (bm-repository-save))))
  339. ;; https://github.com/rmuslimov/browse-at-remote
  340. (use-package browse-at-remote
  341. :bind ("C-c B" . browse-at-remote))
  342. ;; https://github.com/justbur/emacs-which-key
  343. (use-package which-key
  344. :ensure t
  345. :config
  346. (which-key-setup-minibuffer)
  347. (which-key-mode))
  348. ;; https://github.com/lewang/fic-mode
  349. (use-package fic-mode
  350. :ensure t
  351. :hook prog-mode)
  352. ;; https://github.com/joaotavora/yasnippet
  353. (use-package yasnippet
  354. :ensure t
  355. :hook (prog-mode . yas-minor-mode)
  356. :init
  357. (setq yas-snippet-dirs (list (concat user-emacs-directory "snippets")))
  358. :config
  359. (yas-reload-all))
  360. ;; https://company-mode.github.io/
  361. (use-package company
  362. :ensure t
  363. :hook (prog-mode . company-mode))
  364. ;; https://github.com/magit/magit
  365. (use-package magit
  366. :commands magit-status
  367. :ensure t
  368. :init
  369. (setq magit-completing-read-function 'helm-completing-read-default-handler)
  370. :bind
  371. ("C-x g" . magit-status)
  372. ("C-c g" . magit-status))
  373. ;; https://github.com/bbatsov/projectile
  374. (use-package projectile
  375. :ensure t
  376. :bind-keymap
  377. ("C-c p" . projectile-command-map)
  378. )
  379. ;; https://github.com/magit/git-modes
  380. (use-package gitignore-mode
  381. :ensure t)
  382. ;; https://github.com/magit/git-modes
  383. (use-package gitconfig-mode
  384. :ensure t)
  385. ;; http://web-mode.org/
  386. (use-package web-mode
  387. :ensure t
  388. :mode "\\.html?\\'")
  389. ;; https://github.com/joshwnj/json-mode
  390. (use-package json-mode
  391. :ensure t
  392. :mode "\\.json\\'")
  393. ;; https://github.com/antonj/scss-mode
  394. (use-package scss-mode
  395. :ensure t
  396. :mode ("\\.s?css\\'" . scss-mode))
  397. ;; https://elpa.gnu.org/packages/csv-mode.html
  398. (use-package csv-mode
  399. :disabled
  400. :ensure t
  401. :mode ("\\.csv\\'" . csv-mode))
  402. ;; https://orgmode.org/elpa.html
  403. (use-package org
  404. :commands (org-cycle-agenda-files org-capture)
  405. :ensure org-plus-contrib
  406. :mode ("\\.org\\'" . org-mode)
  407. :bind (
  408. ("C-," . org-cycle-agenda-files)
  409. ("C-c C-d" . org-capture)
  410. :map org-mode-map
  411. ("M-RET" . org-insert-todo-heading)
  412. )
  413. :init
  414. (setq org-agenda-files '("~/Dropbox/Org/todo.org"
  415. "~/Dropbox/Org/archive.org"
  416. "~/Dropbox/Org/diary/eaglecrk.org"))
  417. (setq org-todo-keywords '((sequence "TODO(t)" "|" "DONE(d)")
  418. (sequence "BUG(b)" "INPROGRESS(i)" "|" "FIXED(f)")
  419. (sequence "TEST(T)" "NOTEST(N)" "|" "COMPLETE(C)")
  420. (sequence "|" "CANCELED(c)")
  421. (sequence "|" "NEEDCLARIFICATION(n)")
  422. (sequence "|" "PROVIDEUPDATE(p)")
  423. (sequence "|" "WAITING(w)"))
  424. org-refile-targets '((nil :maxlevel . 3)
  425. (org-agenda-files :maxlevel . 3))
  426. org-directory "~/Dropbox/Org"
  427. org-default-notes-file (concat org-directory "/todo.org")
  428. org-startup-folded t
  429. org-startup-indented t
  430. org-startup-align-all-tables t
  431. org-startup-with-inline-images t
  432. org-startup-with-latex-preview t
  433. org-src-tab-acts-natively t
  434. org-confirm-babel-evaluate nil
  435. org-log-done t
  436. org-log-done-with-time t
  437. org-log-into-drawer t
  438. org-hide-leading-stars t
  439. org-pretty-entities t
  440. org-use-property-inheritance t
  441. org-html-validation-link nil
  442. org-html-text-markup-alist '((bold . "<b>%s</b>")
  443. (code . "<code>%s</code>")
  444. (italic . "<i>%s</i>")
  445. (strike-through . "<del>%s</del>")
  446. (underline . "<span class=\"underline\">%s</span>")
  447. (verbatim . "<code class=\"verbatim\">%s</code>"))
  448. )
  449. :config
  450. (org-babel-do-load-languages 'org-babel-load-languages '((js . t)
  451. (shell . t)
  452. (emacs-lisp . t)))
  453. (add-to-list 'org-structure-template-alist
  454. (list "e" (concat "#+BEGIN_SRC emacs-lisp :results silent\n"
  455. "\n"
  456. "#+END_SRC")))
  457. (add-to-list 'org-structure-template-alist
  458. (list "j" (concat "#+BEGIN_SRC js :cmd \"/usr/local/bin/babel-node\" :results output code\n"
  459. "\n"
  460. "#+END_SRC")))
  461. (add-to-list 'org-structure-template-alist
  462. (list "r" (concat "#+BEGIN_SRC restclient :results raw\n"
  463. "\n"
  464. "#+END_SRC")))
  465. )
  466. ;; https://github.com/sabof/org-bullets
  467. (use-package org-bullets
  468. :ensure t
  469. :after (org)
  470. :hook (org-mode . org-bullets-mode)
  471. :config
  472. (set-face-attribute 'org-level-1 nil :height 1.3)
  473. (set-face-attribute 'org-level-2 nil :height 1.1)
  474. (set-face-attribute 'org-level-3 nil :height 1.05)
  475. (set-face-attribute 'org-level-4 nil :height 1.05)
  476. (set-face-attribute 'org-scheduled-today nil :height 1.0)
  477. (set-face-attribute 'org-agenda-date-today nil :height 1.1))
  478. ;; https://orgmode.org/worg/org-contrib/org-protocol.html
  479. (use-package org-protocol
  480. :ensure org-plus-contrib
  481. :after (org)
  482. :init
  483. (setq org-capture-templates
  484. '(("t" "new task" entry (file+headline "~/Dropbox/Org/todo.org" "Tasks")
  485. "* TODO [#A] %?\nSCHEDULED: %(org-insert-time-stamp (org-read-date nil t \"+0d\"))\n%a\n")
  486. ("n" "new note" entry (file+headline org-default-notes-file "Notes")
  487. "* %?\n%i\n")
  488. ("l" "store link" entry (file+olp org-default-notes-file "Links" "Unfiled")
  489. "* %a\n%?\n")
  490. ("d" "store link w/drawer" entry (file+olp org-default-notes-file "Links" "Unfiled")
  491. "* %?\n%l\n:COPIED_TEXT:\n %i\n:END:\n")
  492. ("f" "dotfile" entry (file+headline "~/Dropbox/Org/dotfiles.org" "Other")
  493. "* %?\n:PROPERTIES:\n:CUSTOM_ID: %(org-id-get-create)\n:END:\n")
  494. ))
  495. )
  496. (cond ((member "PragmataPro Liga" (font-family-list))
  497. (set-face-attribute 'default nil :font "PragmataPro Liga-12")))
  498. (server-start)
  499. ;; Reset GC as late as possible
  500. ;; (add-hook 'emacs-startup-hook
  501. ;; (setq gc-cons-threshold 16777216
  502. ;; gc-cons-percentage 0.1))
  503. (custom-set-variables
  504. ;; custom-set-variables was added by Custom.
  505. ;; If you edit it by hand, you could mess it up, so be careful.
  506. ;; Your init file should contain only one such instance.
  507. ;; If there is more than one, they won't work right.
  508. '(package-selected-packages
  509. (quote
  510. (browse-at-remote bm yasnippet which-key web-mode use-package scss-mode rainbow-delimiters projectile org-plus-contrib org-bullets multiple-cursors magit linum-relative json-mode helm-swoop helm-ag gitignore-mode gitconfig-mode fic-mode expand-region doom-themes doom-modeline dashboard company beacon ace-window ace-jump-mode)))
  511. '(safe-local-variable-values
  512. (quote
  513. ((eval org-content 2)
  514. (whitespace-line-column . 120)))))
  515. (custom-set-faces
  516. ;; custom-set-faces was added by Custom.
  517. ;; If you edit it by hand, you could mess it up, so be careful.
  518. ;; Your init file should contain only one such instance.
  519. ;; If there is more than one, they won't work right.
  520. )