From cd7bd7628f1a72b495320049bb8b6bf44fe16ed7 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Sat, 9 Dec 2017 14:42:42 -0500 Subject: [PATCH] lang/org: refactor org init process after! org is more reliable than org-load-hook. --- core/core-popups.el | 6 +-- modules/lang/org/+attach.el | 4 +- modules/lang/org/+babel.el | 4 +- modules/lang/org/+capture.el | 4 +- modules/lang/org/+export.el | 4 +- modules/lang/org/+present.el | 4 +- modules/lang/org/config.el | 72 ++++++++++++++++++++++-------------- 7 files changed, 52 insertions(+), 46 deletions(-) diff --git a/core/core-popups.el b/core/core-popups.el index ccd43765e..6ab219b13 100644 --- a/core/core-popups.el +++ b/core/core-popups.el @@ -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) diff --git a/modules/lang/org/+attach.el b/modules/lang/org/+attach.el index 945d6d113..94988d99e 100644 --- a/modules/lang/org/+attach.el +++ b/modules/lang/org/+attach.el @@ -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))) diff --git a/modules/lang/org/+babel.el b/modules/lang/org/+babel.el index f128c547e..73a7dba78 100644 --- a/modules/lang/org/+babel.el +++ b/modules/lang/org/+babel.el @@ -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 diff --git a/modules/lang/org/+capture.el b/modules/lang/org/+capture.el index f39e8077d..130d3ffdf 100644 --- a/modules/lang/org/+capture.el +++ b/modules/lang/org/+capture.el @@ -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)) diff --git a/modules/lang/org/+export.el b/modules/lang/org/+export.el index 0f70ed9cf..c4cd8911a 100644 --- a/modules/lang/org/+export.el +++ b/modules/lang/org/+export.el @@ -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 diff --git a/modules/lang/org/+present.el b/modules/lang/org/+present.el index c25d36134..daec968aa 100644 --- a/modules/lang/org/+present.el +++ b/modules/lang/org/+present.el @@ -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 "" #'+org-present/start)) diff --git a/modules/lang/org/config.el b/modules/lang/org/config.el index e52e9a97f..95227a749 100644 --- a/modules/lang/org/config.el +++ b/modules/lang/org/config.el @@ -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'."