0

Suppose I have a variable newName which is bearing some mode name, e.g. "python-mode". How do I make current buffer of the mode specified by newName?

(progn (let (newName) (setq newName "python-mode") (newName) ;; doesn't work! It doesn't set current buffer's mode to be a python mode. ) ) 

This also doesn't work:

(set-variable 'major-mode "python-mode") 

This question is fundamental - since it is equal to "is it really possible to treat data as code in lisp?"

Edit

@phils

Your solution doesn't work for me. I copy a buffer - and I want the new one to have the same mode as the old one. So I store the mode of the original buffer in the variable. Then try to apply Your solution. It gives error (it's the essence - I omit here the buffer-copying stuff):

(let (sameMode) (setq sameMode major-mode) (funcall (intern sameMode)) ) 

sameMode stores here mode in the form of "python-mode" (example for python-mode).

4
  • 1
    phils has the answer for you, but as an aside, whatever would make you think you could use strings as functions, especially without funcall? I really recommend reading the documentation for things like this, since it's included with emacs and would have easily answered this question. Guessing how a language works without trying to understand it first is unlikely to give you much success! Commented Sep 24, 2011 at 15:08
  • 2
    Adobe: In Lisp, there is a difference of a variable has a string value or an atom value. The function intern converts a string to an atom. In Emacs, the value of major-mode is an atom an thus you should not use intern. Btw. This is exactly as @phils specified in his second example in his answer. Commented Sep 25, 2011 at 16:05
  • @ Lindydancer: You do make things clear! Commented Sep 25, 2011 at 16:29
  • 1
    I just got around to checking the elisp definition of an atom (as I would have called it a symbol myself), and I think that's incorrect terminology. intern returns a symbol. An atom is any object which is not a cons cell, and therefore a string is an atom (which you can confirm with the (atom) predicate function). Commented Sep 28, 2011 at 4:10

1 Answer 1

7
(let ((mode "python-mode")) (funcall (intern mode))) 

or

(let ((mode 'python-mode)) (funcall mode)) 
Sign up to request clarification or add additional context in comments.

3 Comments

The second is not good for me - for I don't know ahead which mode will be in a variable - so I have my mode in the form "python-mode" (that's just an example - could be some other mode instead). Your first answer solves the problem - thank You so much. So all in all it's true - lisp could treat data as code!
I've updated the question - Your solution doesn't work for me. Can You please improve it?
Nevermind - Lindydancer explained it for me. You were 100% right and made kind of an ideal answer!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.