Browse Source

Optimized init.el for speed

master
Josh Wolfe 2 years ago
parent
commit
f34f29638d
1 changed files with 75 additions and 62 deletions
  1. 75
    62
      emacs/.emacs.d/init.el

+ 75
- 62
emacs/.emacs.d/init.el View File

@@ -4,75 +4,88 @@
;; \ V V / (_) | | _| __/ \__ \ | | | | | | |_ | __/ |
;; \_/\_/ \___/|_|_| \___| |___/ |_|_| |_|_|\__(_)___|_|

;; Set garbage collect high to speed up startup
(let ((gc-cons-threshold most-positive-fixnum)
(ad-redefinition-action 'accept)) ;; Ignore advice warnings
(require 'package)
;; Setup package sources
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
("melpa" . "http://melpa.org/packages/")
("org" . "http://orgmode.org/elpa/")))
(setq package-enable-at-startup nil)
(setq package-pinned-packages '((use-package . "melpa")))
(package-initialize)

;; Bootstrap use-package
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
(require 'use-package)
(setq use-package-always-ensure t)
;; Disable file name handler for performance during startup
(defvar wolfe/file-name-handler-alist file-name-handler-alist)
(setq file-name-handler-alist nil
;; Set garbage collect high to speed up startup
gc-cons-threshold most-positive-fixnum
gc-cons-percentage 0.6
;; Ignore advice warnings
ad-redefinition-action 'accept)

;; Use latest org before calling babel
(use-package-pin-package 'org "org")
(use-package org
:ensure org-plus-contrib)
(add-hook 'emacs-startup-hook
(lambda () (setq gc-cons-threshold 16777216
gc-cons-percentage 0.1
file-name-handler-alist wolfe/file-name-handler-alist
ad-redefinition-action 'warn)))

(defun init/tangle-section-canceled ()
"Return t if the current section header was CANCELED, else nil."
(save-excursion
(if (re-search-backward "^\\*+\\s-+\\(.*?\\)?\\s-*$" nil t)
(string-prefix-p "CANCELED" (match-string 1))
nil)))
(require 'package)
;; Setup package sources
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
("melpa" . "http://melpa.org/packages/")
("org" . "http://orgmode.org/elpa/")))
(setq package-enable-at-startup nil)
(setq package-pinned-packages '((use-package . "melpa")))
(package-initialize)

(defun init/tangle-config-org (orgfile elfile)
"This function will write all source blocks from =config.org= into
;; Bootstrap use-package
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
(require 'use-package)
(setq use-package-always-ensure t)

;; Use latest org before calling babel
(use-package-pin-package 'org "org")
(use-package org
:ensure org-plus-contrib)

(defun init/tangle-section-canceled ()
"Return t if the current section header was CANCELED, else nil."
(save-excursion
(if (re-search-backward "^\\*+\\s-+\\(.*?\\)?\\s-*$" nil t)
(string-prefix-p "CANCELED" (match-string 1))
nil)))

(defun init/tangle-config-org (orgfile elfile)
"This function will write all source blocks from =config.org= into
=config.el= that are ...

- not marked as :tangle no
- have a source-code of =emacs-lisp=
- doesn't have the todo-marker CANCELED"
(let* ((body-list ())
(gc-cons-threshold most-positive-fixnum)
(org-babel-src-block-regexp (concat
;; (1) indentation (2) lang
"^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*"
;; (3) switches
"\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)"
;; (4) header arguments
"\\([^\n]*\\)\n"
;; (5) body
"\\([^\000]*?\n\\)??[ \t]*#\\+end_src")))
(with-temp-buffer
(insert-file-contents orgfile)
(goto-char (point-min))
(while (re-search-forward org-babel-src-block-regexp nil t)
(let ((lang (match-string 2))
(args (match-string 4))
(body (match-string 5))
(canc (init/tangle-section-canceled)))
(when (and (string= lang "emacs-lisp")
(not (string-match-p ":tangle\\s-+no" args))
(not canc))
(add-to-list 'body-list body)))))
(with-temp-file elfile
(insert (format ";; Don't edit this file, edit %s instead ...\n\n" orgfile))
(apply 'insert (reverse body-list)))
(message "Wrote %s ..." elfile)))
(let* ((body-list ())
(gc-cons-threshold most-positive-fixnum)
(org-babel-src-block-regexp (concat
;; (1) indentation (2) lang
"^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*"
;; (3) switches
"\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)"
;; (4) header arguments
"\\([^\n]*\\)\n"
;; (5) body
"\\([^\000]*?\n\\)??[ \t]*#\\+end_src")))
(with-temp-buffer
(insert-file-contents orgfile)
(goto-char (point-min))
(while (re-search-forward org-babel-src-block-regexp nil t)
(let ((lang (match-string 2))
(args (match-string 4))
(body (match-string 5))
(canc (init/tangle-section-canceled)))
(when (and (string= lang "emacs-lisp")
(not (string-match-p ":tangle\\s-+no" args))
(not canc))
(add-to-list 'body-list body)))))
(with-temp-file elfile
(insert (format ";; Don't edit this file, edit %s instead ...\n\n" orgfile))
(apply 'insert (reverse body-list)))
(message "Wrote %s ..." elfile)))

(let ((orgfile (concat user-emacs-directory "README.org"))
(elfile (concat user-emacs-directory "README.el")))
(when (or (not (file-exists-p elfile))
(file-newer-than-file-p orgfile elfile))
(init/tangle-config-org orgfile elfile))
(load-file elfile)))
(let ((orgfile (concat user-emacs-directory "README.org"))
(elfile (concat user-emacs-directory "README.el")))
(when (or (not (file-exists-p elfile))
(file-newer-than-file-p orgfile elfile))
(init/tangle-config-org orgfile elfile))
(load-file elfile))

Loading…
Cancel
Save