0

Files can have the same type (the same extension) and still have different kinds of contents for which I have different preferences. For instance, some text files are in English and some in Swedish, and they need to have the Flyspell dictionary set differently. Mostly, I want auto-fill-mode to be on, but for some files I always want to have manual control over where line breaks happen. For some files I want visual-line-mode to be on, but for others I want to truncate-files. Hence, I find myself issuing commands for this and other configurations every time I edit a file for which my preferences differ from my default settings.

What I would like is to have Emacs save these properties (and possibly others that I specify) on a file basis, and set them automatically when I next open the file, a bit similarly to how save-place-mode saves the position in each file, and goes to that position the next time the file is opened. I don't want to put any magic file variable lines in the files (because I want them “clean” when others look at the files, perhaps not using Emacs).

I'm fine with implementing this myself, but before I do, I'd like to check: is there already something that I can use to accomplish what I want in a simple way that I haven't found or thought of? I've googled for it but haven't found anything.

1
  • 1
    It might help readers to delete all such now-useless comments.
    – Drew
    Commented Jun 3, 2023 at 17:21

1 Answer 1

1

You can create an association list that links file names to setup functions.

(defun english-setup ()
  (message "English setup."))

(defun swedish-latex-setup ()
  ;; (flyspell-prog-mode)
  (message "Swedish setup"))

(setq setup-functions-alist '(("english.txt" . english-setup)
                              ("swedish.tex" . swedish-latex-setup)))

Then create a function that you will call after visiting a file that sets up the buffer.

(defun language-setup ()
  (let ((list  (copy-alist setup-functions-alist)))
    (while list
      (when (string-equal (file-name-nondirectory (buffer-file-name))
                          (car (car list)))
        (funcall (cdr (car list))))
      (setq list (cdr list)))))

(add-hook 'find-file-hook 'language-setup)

Note that language-setup only compares filenames without the directory, you may want to change file-name-nondirectory to another file name function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.