Skip to content

Commit

Permalink
Initial version of minor mode for auto exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
edkolev authored and kaushalmodi committed Jan 3, 2019
1 parent 88af494 commit ba944c6
Showing 1 changed file with 38 additions and 83 deletions.
121 changes: 38 additions & 83 deletions ox-hugo-auto-export-mode.el
@@ -1,121 +1,76 @@
;;; ox-hugo-auto-export.el --- Auto export using ox-hugo -*- lexical-binding: t -*-
;;; ox-hugo-auto-export-mode.el --- Minor mode for auto-exporting using ox-hugo -*- lexical-binding: t -*-

;; Authors: Kaushal Modi <kaushal.mod@gmail.com>
;; Authors: Kaushal Modi <kaushal.mod@gmail.com>, Evgeni Kolev <evgenysw@gmail.com>
;; URL: https://ox-hugo.scripter.co

;;; Commentary:

;; *This is NOT a stand-alone package.*
;;
;; It calls `org-hugo-export-wim-to-md' which auto-loads ox-hugo.
;; This is a minor mode for enabling auto-exporting of Org files via
;; ox-hugo.
;;
;; What this file does when `require'd:
;; - Sets `org-hugo-auto-export-on-save' as safe if boolean.
;; - Updates `org-hugo--org-capture-active-flag'
;; in `org-capture-before-finalize-hook' and
;; `org-capture-after-finalize-hook' hooks.
;; - Adds `org-hugo-auto-export-hook-fn' to `org-mode-hook'.
;; *It is NOT a stand-alone package.*

;;; Usage:
;; With `ox-hugo' installed,
;;
;; 1. Add below to your Emacs config:
;;
;; (require 'ox-hugo-auto-export)
;;
;; ** Do NOT autoload any function from this package
;; -- Just `require' it as shown above. **
;;
;; 2. Add below to your project's .dir-locals.el file (assuming that
;; the Org files in the "content-org" directory are meant to be
;; auto-exported using `ox-hugo':
;;
;; (("content-org"
;; . ((org-mode . ((org-hugo-auto-export-on-save . t))))))
;; To enable this minor mode for a "content-org" directory, add below
;; to the .dir-locals.el:
;;
;; (("content-org/"
;; . ((org-mode . ((eval . (org-hugo-auto-export-mode)))))))

;;; Code:

(defvar-local org-hugo-auto-export-on-save nil
"Enable flag for `org-hugo-export-wim-to-md-after-save'.
This is a buffer local variable to enable auto-exporting using
`ox-hugo' on file saving. It is intended to be set in
.dir-locals.el files.
For example, put the below in your project's .dir-locals.el if
the Org files in the \"content-org\" directory are meant to be
auto-exported using `ox-hugo':
\((\"content-org\"
. ((org-mode . ((org-hugo-auto-export-on-save . t)))))) ")
;; Declare this variable as safe if its value is boolean (t or nil),
;; so that adding it to .dir-locals.el does not prompt users.
(put 'org-hugo-auto-export-on-save 'safe-local-variable 'booleanp)

(defvar org-hugo--org-capture-active-flag nil
"Non-nil means that an Org Capture is in progress.
This variable is used to prevent auto-exports when saving Org
Captures (applicable if creating new posts using Org Capture
using the per-subtree export flow).
This is an internal flag; not to be modified by the user.")
using the per-subtree export flow).")

(defun org-hugo-export-wim-to-md-after-save ()
"Function for `after-save-hook' to run `org-hugo-export-wim-to-md'.
The exporting happens only if `org-hugo-auto-export-on-save' is
non-nil and `org-hugo--org-capture-active-flag' is nil (i.e. Org
Capture is not is progress)."
;; (message "[ox-hugo export after save DBG] after-save-hook = %S"
;; after-save-hook)
;; (message "[ox-hugo export after save DBG] org-hugo-auto-export-on-save = %S"
;; org-hugo-auto-export-on-save)
;; (when (boundp 'org-capture-before-finalize-hook)
;; (message "[ox-hugo export after save DBG] org-capture-before-finalize-hook = %S"
;; org-capture-before-finalize-hook))
;; (when (boundp 'org-capture-after-finalize-hook)
;; (message "[ox-hugo export after save DBG] org-capture-after-finalize-hook = %S"
;; org-capture-after-finalize-hook))
;; (message "[ox-hugo export after save DBG] org-hugo--org-capture-active-flag = %S"
;; org-hugo--org-capture-active-flag)
(save-excursion
(when (and org-hugo-auto-export-on-save
(not org-hugo--org-capture-active-flag))
The exporting happens only if `org-hugo--org-capture-active-flag'
is nil (i.e. Org Capture is not is progress)."
(unless org-hugo--org-capture-active-flag
(save-excursion
(org-hugo-export-wim-to-md))))

(defun org-hugo-auto-export-hook-fn ()
"Hook function to enable/disable auto-export of Org file/subtree on file save."
(add-hook 'after-save-hook #'org-hugo-export-wim-to-md-after-save :append :local))

;;; Org Capture
;;; Org Capture Hook Functions
;; Disallow auto-exporting when saving Org Captures.
(defun org-hugo--disallow-auto-export-during-capture ()
"Function for `org-capture-before-finalize-hook'.
Set `org-hugo--org-capture-active-flag' to t to prevent the
`ox-hugo' auto-exports from happening when saving captures."
;; (message "BEFORE capture")
(setq org-hugo--org-capture-active-flag t))

(defun org-hugo--allow-auto-export-after-capture ()
"Function for `org-capture-after-finalize-hook'.
Set `org-hugo--org-capture-active-flag' back to nil so that the
auto-export on save can resume working (if
`org-hugo-auto-export-on-save' is non-nil)."
;; (message "AFTER capture")
auto-export on save can resume working"
(setq org-hugo--org-capture-active-flag nil))

(with-eval-after-load 'org-capture
(add-hook 'org-capture-before-finalize-hook #'org-hugo--disallow-auto-export-during-capture)
(add-hook 'org-capture-after-finalize-hook #'org-hugo--allow-auto-export-after-capture :append))

;;; Org Mode Hook
(add-hook 'org-mode-hook #'org-hugo-auto-export-hook-fn)


(provide 'ox-hugo-auto-export)

;;; ox-hugo-auto-export.el ends here
;;;###autoload
(define-minor-mode org-hugo-auto-export-mode
"Toggle auto exporting the Org file using `ox-hugo'."
:global nil
:lighter ""
(if org-hugo-auto-export-mode
;; When the mode is enabled
(progn
(add-hook 'after-save-hook #'org-hugo-export-wim-to-md-after-save :append :local)
(with-eval-after-load 'org-capture
(add-hook 'org-capture-before-finalize-hook #'org-hugo--disallow-auto-export-during-capture)
(add-hook 'org-capture-after-finalize-hook #'org-hugo--allow-auto-export-after-capture :append)))
;; When the mode is disabled
(remove-hook 'after-save-hook #'org-hugo-export-wim-to-md-after-save :local)
(when (featurep 'org-capture)
(remove-hook 'org-capture-before-finalize-hook #'org-hugo--disallow-auto-export-during-capture)
(remove-hook 'org-capture-after-finalize-hook #'org-hugo--allow-auto-export-after-capture))))


(provide 'ox-hugo-auto-export-mode)

;;; ox-hugo-auto-export-mode.el ends here

0 comments on commit ba944c6

Please sign in to comment.