views:

212

answers:

1

I want to have a command in Emacs to make it as opaque/transparent as I want (refer to the fabulous question that pointed out that transparency is possible in Emacs, and the EmacsWiki page linked there which has the code I am using below).

The EmacsWiki code sets "C-c t" to toggle the previously set transparency on and off:

;;(set-frame-parameter (selected-frame) 'alpha '(<active> [<inactive>]))  
(set-frame-parameter (selected-frame) 'alpha '(85 50))  
(add-to-list 'default-frame-alist '(alpha 85 50))

enter code here(eval-when-compile (require 'cl))  
(defun toggle-transparency ()  
(interactive)  
(if (/= 
    (cadr (find 'alpha (frame-parameters nil) :key #'car)) 
    100)  
   (set-frame-parameter nil 'alpha '(100 100))
 (set-frame-parameter nil 'alpha '(85 60))))  
 (global-set-key (kbd "C-c t") 'toggle-transparency)

What I would like to do is to be able to choose the % transparency when I am in Emacs.

If possible, I would like a command where I type for example "C-c t N" (where N is the % opaqueness) for the active frame, and then "M-c t N" for the inactive window.

If that can't be done like that, then maybe a command where if I type "C-c t" it asks me for the number which gives the opaqueness of the active window (and the same for the inactive window using "M-c t").

Thanks in advance for your time :)

Below are just some comments that are not important to answer the question if you are not interested:

I really want this because when I told my supervisor I was learning Emacs he said TexShop is much better and that I am using software from the 80's. I told him about the wonders of Emacs and he said TexShop has all of it and more. I matched everything he showed me except for the transparency (though he couldn't match the preview inside Emacs from preview-latex). I found the transparency thing by chance, and now I want to show him Emacs rules!

I imagine this will be a piece of cake for some of you, and even though I could get it done if I spent enough time trying to learn lisp or reading around, I am not a programmer and I have only been using Emacs and a mac for a week. I am lost already as it is! So thanks in advance for your time and help - I will learn lisp eventually!

Edit: My supervisor uses TextMate, not TeXShop. Does it make more sense now?

+3  A: 
(defun set-frame-alpha (arg &optional active)
  (interactive "nEnter alpha value (1-100): \np")
  (let* ((elt (assoc 'alpha default-frame-alist))
         (old (frame-parameter nil 'alpha))
         (new (cond ((atom old)     `(,arg ,arg))
                    ((eql 1 active) `(,arg ,(cadr old)))
                    (t              `(,(car old) ,arg)))))
    (if elt (setcdr elt new) (push `(alpha ,@new) default-frame-alist))
    (set-frame-parameter nil 'alpha new)))
(global-set-key (kbd "C-c t") 'set-frame-alpha)

Use C-c t for the active frame, and C-u C-c t for the non-active frame.

Edit:

One way to make the effect persists across sessions is to enable desktop-save-mode and add default-frame-alist to desktop-globals-to-save, through the customization interface: M-x customize-group RET desktop.

huaiyuan
Thank you for that! :)
Vivi
Sorry, I do have one question still... Can I just substitute the whole code above by this or do I need to leave part of that code in my .emacs file?
Vivi
I tested it without anything else, then with the first three lines of the code I included in the question, then with the first four lines (the last three not counting the line which has ;;). In all those options, the problem is that whatever setting I make will disappear when I close Emacs and go back to either fully opaque (first option) or to 85 50 as defined. And I did save options, adopt frame parameters as default, but still, the setting is lost when I close the program. Is there a way to make the setting permanent or am I doing something wrong?
Vivi
I hope I answered your question in the edit. Notice I also made some changes to the code.
huaiyuan
I will try again and get back to you. I downloaded the lisp manual and was trying to figure out how to change the code, but I guess this isn't a half-an-hour thing....
Vivi
OK, so I used the code you wrote and then selected the frame as default and saved options, using the menu options rather than what you wrote on the bottom, just because I am still not good with the shortcuts and I wanted to try with the menu click options in case it worked. Well, it did work! It is perfect now, thank you very much! I will use what you said in the edit. I am also happy that there were two codes so I can compare the two and start learning lisp. I am really grateful for your help, thank you :)
Vivi
Happy to help :) I made one correction: the last line, the group name should be "desktop" instead of "desktop-save-mode"; the latter is an option to be set in the "desktop" group.
huaiyuan
I haven't tried that bit yet because I suspect if I enable desktop-save-mode then my changes will be saved automatically, is that correct? If it is, I am not ready to do it yet, as I have only been using Emacs for a week, and I have been playing a bit with face customisation (and other stuff) and I am afraid I won't be able to undo what I did if I regret later, so I mostly choose not to save my options. I will check what those variables do now. Thanks again!
Vivi
Thanks for updating the code again :)Most of it was just to make the code more elegant, right?
Vivi