Search code examples
emacstransparencyelispkey-bindings

How to set a key binding to make Emacs as transparent/opaque as I want?


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?


Solution

  • (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.