Foad S. Farimani
Foad S. Farimani

Reputation: 14026

Creating a portable Emacs Org Mode setup with automatic HTML preview using `.dir-locals.el`

I'm trying to create a portable Emacs Org Mode setup/template that I can store on GitHub and clone to any machine with a fresh Emacs installation. My goal is to replicate a workflow similar to what I used in Visual Studio Code with Markdown files:

Essentially, I'd like to automate everything so that as soon as I clone the repository and start Emacs, I can begin working right away without manual setup each time.

Here's what I have so far in my .dir-locals.el:

((org-mode . ((eval . (progn
                        ;; Configure package archives
                        (require 'package)
                        (setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
                                                 ("melpa" . "http://melpa.org/packages/")))
                        (package-initialize)
                        ;; Function to ensure a package is installed
                        (defun ensure-package-installed (package)
                          (unless (package-installed-p package)
                            (unless package-archive-contents
                              (package-refresh-contents))
                            (package-install package)))
                        ;; Ensure required packages are installed
                        (dolist (pkg '(org impatient-mode))
                          (ensure-package-installed pkg))
                        ;; Load impatient-mode
                        (require 'impatient-mode)
                        ;; Function to set up the layout
                        (defun setup-org-preview ()
                          (interactive)
                          (delete-other-windows)
                          (split-window-right)
                          (other-window 1)
                          (let ((html-file (concat (file-name-sans-extension (buffer-file-name)) ".html")))
                            ;; Export to HTML if the file doesn't exist
                            (unless (file-exists-p html-file)
                              (with-current-buffer (find-file-noselect (buffer-file-name))
                                (org-html-export-to-html)))
                            ;; Open the HTML file with eww
                            (eww-open-file html-file))
                          (other-window 1))
                        ;; Configure org-mode for HTML export
                        (setq org-html-doctype "html5"
                              org-html-html5-fancy t
                              org-html-head-include-default-style nil)
                        ;; Set up impatient-mode for live preview
                        (defun org-html-live-preview ()
                          (interactive)
                          (impatient-mode)
                          (httpd-start)
                          (imp-set-user-filter
                           (lambda (buffer)
                             (org-export-as 'html nil nil t nil))))
                        ;; Add hooks to run setup and preview
                        (add-hook 'org-mode-hook (lambda ()
                                                   (org-html-live-preview)
                                                   (setup-org-preview)))
                        ;; Auto-export to HTML on save
                        (add-hook 'after-save-hook
                                  (lambda ()
                                    (when (eq major-mode 'org-mode)
                                      (org-html-export-to-html))))
                        )))))

However, when I run emacs test.org in the command prompt, this is what I see:

enter image description here

The .org file is visible on the left, but on the right, I see the Emacs welcome page instead of the HTML preview. How can I modify this setup to achieve the desired layout and functionality?

P.S. For those who end-up here. I kinda solved my problem and wrote about it here.

Upvotes: 0

Views: 72

Answers (0)

Related Questions