Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory structure where I have <project>/worksheet.tex. When I open each worksheet, I get an undescriptive worksheet<n>as the name for the buffer.

I'd like to save the names of buffers as file local variables. Something perhaps like:

;; (to be safe if "specific project" already exists as a buffer)
buffer-name: (generate-new-buffer-name "specific project")

How can this be implemented without using the eval file-local 'variable'?

share|improve this question
    
I'm pretty sure it can't, but if you answer ! to the "potentially unsafe file-local variables" prompt, Emacs will note in your custom.el file that it should not prompt about that file's local variables in future. – Aaron Miller Dec 13 '13 at 21:03

(update) here's a solution with a file-local variable:

in your files:

;; -*- preferred-buffer-name: "purtsl"; -*-

in .emacs:

(defun change-buffer-name ()
  (if (boundp 'preferred-buffer-name)
      (rename-buffer preferred-buffer-name)))

(add-hook 'find-file-hook 'change-buffer-name t)

you might have to customize safe-local-variable-values and/or enable-local-variables, depending on how many projects you have, safety considerations and how annoying it is for you to answer the security question..


and here's a different approach. it checks for the name of the containing directory and then changes the buffer's name to /WS:

(defun find-file-custom (filename)
  "open filename and set buffer name to <project>/WS"
  (interactive "F")
  (find-file filename)
  (rename-buffer (concat "<" (file-name-nondirectory
                       (directory-file-name
                        (file-name-directory (buffer-file-name)))) ">/WS")))
share|improve this answer

As far as I know, you can't do what you want using file-local variables without the eval: pseudo-variable.

However, the uniquify library (which is standard), should cover your needs. Just put something like the following in your init file:

(require 'uniquify)

You can also customize the uniquify-buffer-name-style and uniquify-separator variables to tailor uniquify to your needs. I personally like these settings more than the defaults:

(setq
 uniquify-buffer-name-style 'post-forward
 uniquify-separator         ":")
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .