Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ability to automatically org-babel based on file category #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions Emacs.org
Original file line number Diff line number Diff line change
Expand Up @@ -565,16 +565,36 @@ You can add more =src= block templates below by copying one of the lines and cha
This snippet adds a hook to =org-mode= buffers so that =efs/org-babel-tangle-config= gets executed each time such a buffer gets saved. This function checks to see if the file being saved is the Emacs.org file you're looking at right now, and if so, automatically exports the configuration here to the associated output files.

#+begin_src emacs-lisp

;; Automatically tangle our Emacs.org config file when we save it
(defun efs/org-babel-tangle-config ()
(when (string-equal (file-name-directory (buffer-file-name))
(expand-file-name user-emacs-directory))
;; Dynamic scoping to the rescue
(let ((org-confirm-babel-evaluate nil))
(org-babel-tangle))))

(add-hook 'org-mode-hook (lambda () (add-hook 'after-save-hook #'efs/org-babel-tangle-config)))
;; This allows you to tell org-babel to tangle you org files after
;; save by placing:
;; #+CATEGORY: Configuration
;; in the head of the org file.
;; Change this search key/term to what best suits the individual
(setq efs/tangle-search-key "CATEGORY")
(setq efs/tangle-search-term "Configuration")

(defun efs/org-global-props (&optional efs/property)
"Get the plists of global org properties of current buffer."
(interactive)
(unless efs/property (setq efs/property "PROPERTY"))
(org-element-map (org-element-parse-buffer)
'keyword (lambda (el) (when (string-match efs/property (org-element-property :key el)) el))))

(defun efs/org-global-prop-value (key)
"Get global org property KEY of current buffer."
(org-element-property :value (car (efs/org-global-props key))))

;; Automatically tangle our Emacs.org config file when we save it
(defun efs/org-babel-tangle-config ()
"Used to tangle org file when it change."
(when (or (string-equal (file-name-directory (buffer-file-name))
(expand-file-name user-emacs-directory))
(string-equal (efs/org-global-prop-value efs/tangle-search-key) efs/tangle-search-term))
;; Dynamic scoping to the rescue
(let ((org-confirm-babel-evaluate nil))
(org-babel-tangle))))

(add-hook 'org-mode-hook (lambda () (add-hook 'after-save-hook #'efs/org-babel-tangle-config)))

#+end_src

Expand Down Expand Up @@ -995,7 +1015,7 @@ Dired is a built-in file manager for Emacs that does some pretty amazing things!
- =*= - Lots of other auto-marking functions
- =k= / =K= - "Kill" marked items (refresh buffer with =g= / =g r= to get them back)
- Many operations can be done on a single file if there are no active marks!

**** Copying and Renaming files

- =C= - Copy marked files (or if no files are marked, the current file)
Expand Down
29 changes: 27 additions & 2 deletions init.el
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,35 @@
(add-to-list 'org-structure-template-alist '("el" . "src emacs-lisp"))
(add-to-list 'org-structure-template-alist '("py" . "src python"))

;; This allows you to tell org-babel to tangle you org files after
;; save by placing:
;; #+CATEGORY: Configuration
;; in the head of the org file.
;; Change this search key/term to what best suits the individual
(setq efs/tangle-search-key "CATEGORY")
(setq efs/tangle-search-term "Configuration")

(defun efs/org-global-props (&optional efs/property)
"Get the plists of global org properties of current buffer.
Takes as argument EFS/PROPERTY"
(interactive)
(unless efs/property (setq efs/property "PROPERTY"))
(org-element-map (org-element-parse-buffer)
'keyword (lambda (el)
(when (string-match efs/property (org-element-property :key el))
el))))

(defun efs/org-global-prop-value (key)
"Get global org property KEY of current buffer."
(org-element-property :value (car (efs/org-global-props key))))

;; Automatically tangle our Emacs.org config file when we save it
(defun efs/org-babel-tangle-config ()
(when (string-equal (file-name-directory (buffer-file-name))
(expand-file-name user-emacs-directory))
"Used to tangle org file when it change."
(when (or (string-equal (file-name-directory (buffer-file-name))
(expand-file-name user-emacs-directory))
(string-equal (efs/org-global-prop-value efs/tangle-search-key)
efs/tangle-search-term))
;; Dynamic scoping to the rescue
(let ((org-confirm-babel-evaluate nil))
(org-babel-tangle))))
Expand Down