1

I am looking to create some customized settings for a specific file. I think I want to add a hook for that file, or something that will create a few settings when that file loads.

For example, I have a file "really-long-lines.org", where every time I open it, I run M-x toggle-truncate-lines. By default, I have all files under org-mode open with (toggle-truncate-lines 0) set. But for this one file, I want it to be set to (toggle-truncate-lines 1). My first approach would be to create a mode for this one file, really-long-lines-mode, and run something like this:

(add-hook 'really-long-lines-mode
          (lambda()
            (toggle-truncate-lines 1)))

But is there a way to add a hook for a specific file? Or whatever solution works best.

2

I usually do it by adding a commented heading to a file:

# -*- truncate-lines:1 -*-

But this can also be done as file-name specific hook:

(defun really-long-lines.org_file_hook ()
  (when (string= (file-name-nondirectory (buffer-file-name)) "really-long-lines.org")
    (toggle-truncate-lines 1)
    )
)

(add-hook 'find-file-hook 'really-long-lines.org_file_hook)
  • 1
    Using a variable is indeed better than calling a function; but the variable name is just truncate-lines. – phils Aug 15 '15 at 8:38
  • Strange, neither of these solutions is working for me. Perhaps there is a conflict with this setting in my .emacs: (add-hook 'org-mode-hook (lambda() (toggle-truncate-lines 0)? – modulitos Aug 15 '15 at 8:48
  • Great, I have it working, although I get a scary warning that it is "not safe", but I loaded it anyway. – modulitos Aug 15 '15 at 9:03
  • @Lucas: about "not safe" -- next time it will ask you -- just press "!" and it will remember your choise. Hook should be working: I tested it with (message "hi"). – Adobe Aug 15 '15 at 11:32
4

A simple answer is to use the eval pseudo-variable in a file-local variables block. See the following answer on S.O. for details:

https://stackoverflow.com/a/4356877/324105

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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