reddit / emacs

Emacs init NOT LOADED "Please save the current buffer to a file" but load-file works after C-xC-f...

ImportanceFit1412 · 2022-12-13 17:00
(I was so proud working up an org init file to share with everyone, I've been starting at the beginning and everything was going great, and this morning I wake up to a strange glitch after working on the plane...) ​ Warning (initialization): An error occurred while loading ‘/Users/me/.emacs.d/init.el’: : Please save the current buffer to a file And if I do a load-file manually then the same error occurs. But if I load the init.el into a buffer, do nothing else but load-file then it all works. As does eval-buffer and the other manual ways to run it. So load-file init.el nothing, C-x C-f init.el, load-file init.el works. It works both from \~/ and from /Users/... --debug-init does nothing, and my (message "entering init.el") at the top doesn't get hit when it fails. This started when somehow my init.el seemed to be gone (no clue how) -- so I did a revert-file in the buffer to get a copy back (per the message, save the buffer). It's as if emacs is stuck in some state of not knowing the init.el file exists -- but that seems like nonsense. ​ Thanks for any help or troubleshooting. **The fact that the opening message and debug-init do nothing makes me think the file isn't even getting seen at startup.**
lstrang · 2022-12-13 22:46
You shouldn't have to load-file init.el, emacs does that already. Perhaps your org file is getting generated in a buffer and not being saved. In any event, I use the following to generate my config (init-ded.org) file every time I save it, so init-ded.el is there already when I restart: ``` #+begin_src emacs-lisp (defun ded:tangle-literate-config () (when (and (eq major-mode 'org-mode) (string-prefix-p (expand-file-name user-emacs-directory) (buffer-file-name))) (let ((org-confirm-babel-evaluate nil)) (org-babel-tangle nil nil "emacs-lisp")))) (add-hook 'after-save-hook #'ded:tangle-literate-config) #+end_src ``` Note that user-emacs-directory is where my personal config is located: ~/.emacs.d/ded in my case. My config is in ~/.emacs.d/ded/init-ded.org. The above hook tangles any org file in that directory that I edit when it is saved, including init-ded.org. My init.el is not "literate," it just makes sure org and use-package are setup, then loads my personal config file, init-ded.el. Here is an abbreviated version: ``` ;; Define init files for user and system (setq emacs-d-dir (expand-file-name "~/.emacs.d/") emacs-init-file (concat emacs-d-dir "init.el") user-org-init-dir (concat emacs-d-dir user-login-name "/") user-org-init-file (concat user-org-init-dir "init-" user-login-name ".org") system-org-dir (concat emacs-d-dir system-name "/") system-org-init-file (concat system-org-dir "init-" system-name ".org") user-data-dir (concat user-org-init-dir "data/")) ;; Defer native compilation (setq comp-deferred-compilation t) (load-file "/home/ded/.emacs.d/ded/init-ded.el") ``` Hope that helps.