Bad-ptr / persp-mode.el

named perspectives(set of buffers/window configs) for emacs
401 stars 44 forks source link

Getting perspective name in activation hooks #23

Closed jluttine closed 8 years ago

jluttine commented 8 years ago

How can I get the name of the activated perspective? I'd like to add some code which depends on the name of the perspective. For debugging, I've added the following hook:

  (add-hook 'persp-activated-hook (message (safe-persp-name (get-frame-persp))))

But it always prints "Default". Actually, I get run-hooks: Invalid function: "Default". But it's always "Default" and not the name of the layout I'm switching to (or from, for that matter). Am I doing something wrong? If I just run M-: and (message (safe-persp-name (get-frame-persp))), it prints the currently active perspective name correctly. I'm new to Lisp (and configuring Emacs), so I may have totally misunderstood something.

Bad-ptr commented 8 years ago

I may have totally misunderstood something.

Yes, you are.

The hooks is a list of functions. And add-hook is like add-to-list.

when you run this code

(add-hook 'persp-activated-hook (message (safe-persp-name (get-frame-persp))))

the (message (safe-persp-name (get-frame-persp))) expression is evaluated inplace and it's value then added to the persp-activated-hook list. To do what you want in a right way you must write a function and pass it's name to the add-hook. Or you can use lambdas(anonymous functions):

(add-hook 'persp-activated-hook (lambda () (message (safe-persp-name (get-frame-persp)))))
jluttine commented 8 years ago

Yep, now I understand. Thanks a lot. :)