lang/org: refactor org init process

after! org is more reliable than org-load-hook.
This commit is contained in:
Henrik Lissner 2017-12-09 14:42:42 -05:00
parent 9d3155892a
commit cd7bd7628f
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
7 changed files with 52 additions and 46 deletions

View file

@ -461,10 +461,10 @@ you came from."
(advice-add #'plantuml-preview-string
:around #'doom*plantuml-preview-in-popup-window))
;; Ensure these settings are attached to org-load-hook as late as possible,
;; giving other modules a chance to add their own hooks.
;; Ensure these settings are loaded as late as possible, giving other modules a
;; chance to reconfigure org popup settings before the defaults kick in.
(defun doom|init-org-popups ()
(add-hook! 'org-load-hook
(after! org
(set! :popup
'("*Calendar*" :size 0.4 :noselect t)
'(" *Org todo*" :size 5 :noselect t)

View file

@ -1,7 +1,5 @@
;;; lang/org/+attach.el -*- lexical-binding: t; -*-
(add-hook 'org-load-hook #'+org-attach|init)
;; I believe Org's native attachment system is over-complicated and litters
;; files with metadata I don't want. So I wrote my own, which:
;;
@ -62,7 +60,7 @@
:filter-return #'+org-attach*download-fullname))
;;
(defun +org-attach|init ()
(after! org
(setq org-attach-directory +org-attach-dir)
(push (car (last (split-string +org-attach-dir "/" t)))

View file

@ -1,7 +1,5 @@
;;; lang/org/+babel.el -*- lexical-binding: t; -*-
(add-hook 'org-load-hook #'+org-babel|init)
(defvar +org-babel-languages
'(calc
css
@ -25,7 +23,7 @@
"A list of org-babel languages to load.")
(defun +org-babel|init ()
(after! org
(setq org-src-fontify-natively t ; make code pretty
org-src-preserve-indentation t ; use native major-mode indentation
org-src-tab-acts-natively t

View file

@ -1,7 +1,5 @@
;;; lang/org/+capture.el -*- lexical-binding: t; -*-
(add-hook 'org-load-hook #'+org-capture|init)
;; Sets up two `org-capture' workflows that I like:
;;
;; 1. The traditional way: invoking `org-capture' directly (or through a
@ -24,7 +22,7 @@
(file+headline org-default-notes-file "Inbox")
"* %u %?\n%i" :prepend t :kill-buffer t)))
(defun +org-capture|init ()
(after! org
(defvaralias 'org-default-notes-file '+org-default-notes-file)
(setq org-default-notes-file (expand-file-name +org-default-notes-file +org-dir))

View file

@ -1,7 +1,5 @@
;;; lang/org/+export.el -*- lexical-binding: t; -*-
(add-hook 'org-load-hook #'+org-export|init)
;; I don't have any beef with org's built-in export system, but I do wish it
;; would export to a central directory, rather than `default-directory'. This is
;; because all my org files are usually in one place, and I want to be able to
@ -18,7 +16,7 @@
(parse-raw . t))))
;;
(defun +org-export|init ()
(after! org
(setq org-export-directory (expand-file-name ".export" +org-dir)
org-export-backends '(ascii html latex md pandoc)
org-export-with-toc t

View file

@ -3,8 +3,6 @@
(defvar +org-present-text-scale 7
"The `text-scale-amount' for `org-tree-slide-mode'.")
(add-hook 'org-load-hook #'+org-present|init)
;;
;; Plugins
@ -45,7 +43,7 @@
;; Bootstrap
;;
(defun +org-present|init ()
(after! org
(require 'ox-reveal)
(map! :map org-mode-map "<f8>" #'+org-present/start))

View file

@ -16,71 +16,75 @@
(if (featurep! +present) (load! +present))
;; TODO (if (featurep! +publish) (load! +publish))
(after! org (+org|init))
(add-hook 'org-mode-hook #'+org|hook)
;;
;; Plugins
;;
(def-package! toc-org
:hook (org-mode . toc-org-enable))
:commands toc-org-enable)
(def-package! org-crypt ; built-in
:hook (org-load . org-crypt-use-before-save-magic)
:commands org-crypt-use-before-save-magic
:config
(setq org-tags-exclude-from-inheritance '("crypt")
org-crypt-key user-mail-address
epa-file-encrypt-to user-mail-address))
(def-package! org-bullets
:hook (org-mode . org-bullets-mode))
:commands org-bullets-mode)
;;
;; Hooks & bootstraps
;; Bootstrap
;;
(defun +org|init ()
"Run once, when org is first loaded."
(defvaralias 'org-directory '+org-dir)
(after! org
;; Occasionally, Emacs encounters an error loading the built-in org, aborting
;; the load. This results in a broken, partially loaded state. This require
;; tries to set it straight.
(require 'org)
(defvaralias 'org-directory '+org-dir)
(org-crypt-use-before-save-magic)
(+org-init-ui)
(+org-init-keybinds)
(+org-hacks))
(defun +org|hook ()
"Run everytime `org-mode' is enabled."
(when (featurep! :feature evil)
(add-hook 'evil-insert-state-exit-hook #'+org|realign-table-maybe nil t)
(add-hook 'evil-insert-state-exit-hook #'+org|update-cookies nil t))
(add-hook! org-mode
#'(doom|disable-line-numbers ; no line numbers
org-bullets-mode ; "prettier" bullets
org-indent-mode ; margin-based indentation
toc-org-enable ; auto-table of contents
visual-line-mode ; line wrapping
(add-hook 'before-save-hook #'+org|update-cookies nil t)
+org|enable-auto-reformat-tables
+org|enable-auto-update-cookies
+org|smartparens-compatibility-config
+org|unfold-to-2nd-level-or-point
))
;;
(setq line-spacing 1)
(visual-line-mode +1)
(org-indent-mode +1)
(doom|disable-line-numbers)
;; show-paren-mode causes problems for org-indent-mode, so disable it
(set (make-local-variable 'show-paren-mode) nil)
;;
;; Config hooks
;;
(defun +org|unfold-to-2nd-level-or-point ()
"My version of the 'overview' #+STARTUP option: expand first-level headings.
Expands the first level, but no further. If point was left somewhere deeper,
unfold to point on startup."
(unless org-agenda-inhibit-startup
;; My version of the 'overview' #+STARTUP option: expand first-level
;; headings. Expands the first level, but no further.
(when (eq org-startup-folded t)
(outline-hide-sublevels 2))
;; If saveplace places the point in a folded position, unfold it on load
(when (outline-invisible-p)
(ignore-errors
(save-excursion
(outline-previous-visible-heading 1)
(org-show-subtree)))))
(org-show-subtree))))))
(defun +org|smartparens-compatibility-config ()
"Instruct `smartparens' not to impose itself in org-mode."
(defun +org-sp-point-in-checkbox-p (_id action _context)
(when (eq action 'insert)
(sp--looking-at-p "\\s-*]")))
@ -93,6 +97,18 @@
(sp-local-pair "~" nil :unless '(sp-point-after-word-p sp-point-before-word-p))
(sp-local-pair "=" nil :unless '(sp-point-after-word-p sp-point-before-word-p))))
(defun +org|enable-auto-reformat-tables ()
"Realign tables exiting insert mode (`evil-mode')."
(when (featurep 'evil)
(add-hook 'evil-insert-state-exit-hook #'+org|realign-table-maybe nil t)))
(defun +org|enable-auto-update-cookies ()
"Update statistics cookies when saving or exiting insert mode (`evil-mode')."
(when (featurep 'evil)
(add-hook 'evil-insert-state-exit-hook #'+org|update-cookies nil t))
(add-hook 'before-save-hook #'+org|update-cookies nil t))
;;
(defun +org-init-ui ()
"Configures the UI for `org-mode'."