You are not logged in.

#1 2024-02-12 23:29:08

lenhuppe
Member
From: New Hampshire USA
Registered: 2018-12-10
Posts: 308
Website

SOLVED Emacs config question

I am learning how to setup Emacs with Org and I ran into a configuration issue.
Like most Emacs noobs my first Org document is my Emacs configuration file.
The problem is that  Org is tangling files when I am not in Org mode.
I am utilizing use-package as follows:

  (use-package org
    :init
    (defun auto-tangle-file() (org-babel-load-file buffer-file-name))
    (setq org-ellipsis " ▾")
    (setq org-confirm-babel-evaluate nil)
    :config
    (add-hook 'org-mode-hook #'visual-line-mode)
    (add-hook 'org-mode-hook #'org-indent-mode)
    (add-hook 'org-mode-hook #'variable-pitch-mode)
    (add-hook 'org-mode-hook (lambda () (add-hook 'after-save-hook #'auto-tangle-file)))
    (global-set-key (kbd "C-c l") #'org-store-link)
    (global-set-key (kbd "C-c a") #'org-agenda)
    (global-set-key (kbd "C-c c") #'org-capture))

The #'auto-tangle-file function that I created is working great in Org mode.
What I don't understand is why it is not limited in scope to Org mode.
I thought that 'org-mode-hook would limit the scope of my function to Org.
My function is based upon the code below which I found here : https://github.com/daviwil/emacs-from-s … /Emacs.org

;; Automatically tangle our Emacs.org config file when we save it
(defun efs/org-babel-tangle-config ()
  (when (string-equal (buffer-file-name)
                      (expand-file-name "~/Projects/Code/emacs-from-scratch/Emacs.org"))
    ;; 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)))

I have been trying various changes but I keep getting 'function void' errors.

Why is my #'auto-tangle-file being called when I am in shell or python mode?

Better yet can any of this code be adapted to work on any Org file but only in Org mode?

Last edited by lenhuppe (2024-02-15 21:01:30)

Offline

#2 2024-02-13 07:22:37

Stefan Husmann
Member
From: Germany
Registered: 2007-08-07
Posts: 1,391

Re: SOLVED Emacs config question

According to the org mode manual in the section "Activation":

Files with the ‘.org’ extension use Org mode by default.  To turn on
Org mode in a file that does not have the extension ‘.org’, make the
first line of a file look like this:

     MY PROJECTS    -*- mode: org; -*-

Do you have such lines in some of your files?

Offline

#3 2024-02-13 07:34:20

swsnr
Member
Registered: 2024-01-21
Posts: 26

Re: SOLVED Emacs config question

It's a long time since I used Emacs but if I remember correctly hook variables aren't special.

"after-save-hook" is probably not declared as buffer local variable, so your "add-hook" call changes the global variable every time.

You'll need to make it buffer local first, with e.g. "(setq-local after-save-hook t)". Then a subsequent "add-hook" call changes the buffer-local value and your function no longer runs in modes other than org.

Offline

#4 2024-02-13 12:48:37

lenhuppe
Member
From: New Hampshire USA
Registered: 2018-12-10
Posts: 308
Website

Re: SOLVED Emacs config question

Stefan Husmann wrote:

According to the org mode manual in the section "Activation":

Files with the ‘.org’ extension use Org mode by default.  To turn on
Org mode in a file that does not have the extension ‘.org’, make the
first line of a file look like this:

     MY PROJECTS    -*- mode: org; -*-

Do you have such lines in some of your files?

I am aware of that directive but I have not used it in any of my files.
My issue is turning off Org mode when I working in other major modes.

Offline

#5 2024-02-14 14:22:22

Stefan Husmann
Member
From: Germany
Registered: 2007-08-07
Posts: 1,391

Re: SOLVED Emacs config question

I just found this:

https://github.com/daviwil/emacs-from-scratch/issues/38

whcih makes me think, this could be considered a feature rather than a bug by the "emacs-from-scratch" people. In genaral, I think you would be much better off asking there.

Offline

#6 2024-02-15 21:01:04

lenhuppe
Member
From: New Hampshire USA
Registered: 2018-12-10
Posts: 308
Website

Re: SOLVED Emacs config question

Solution:

I found the answer here: https://github.com/jamcha-aa/auto-org-m … -org-md.el

From what I can see so far it works as intended and does not impact modes other than Org.
I may change  org-babel-load() to just tangle the file and not load it, similar to org-md-export().
In the end I can just run 'M-x restart-emacs' for testing and not have to use org-babel-load().
That and I can also evaluate a block of source code by running 'C-c C-c' right in the Org document.

	(use-package org
		:init
		(setq org-ellipsis " ▾")
		:hook (after-save . org-md-export)
		:hook (after-save . org-babel-load))

	(defun org-md-export ()
		(when (derived-mode-p 'org-mode)
			(org-md-export-to-markdown)))

	(defun org-babel-load ()
		(when (derived-mode-p 'org-mode)
			(org-babel-load-file buffer-file-name)))

Update:

	(use-package org-auto-tangle
  	  :hook (org-mode . org-auto-tangle-mode)
  	  :config (setq org-auto-tangle-default t))

Last edited by lenhuppe (2024-02-16 02:26:04)

Offline

Board footer

Powered by FluxBB