2

How to get a function that toggles transparency for instance? Possible candidates for the toggle could be, 0%, 85%, 100% or maybe just 2 of those...

    (defun transparency-toggle ()
    "Toggle transparency."
    (interactive)
    (add-to-list 'default-frame-alist '(alpha 85 50))
    )

What is missing?

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160

2 Answers2

2

The following code is taken from Emacs wiki

(eval-when-compile (require 'cl))
(set-frame-parameter nil 'alpha '(100 100))

(defun toggle-transparency (unused)
  (interactive "P")
  (if (/=
       (cadr (frame-parameter nil 'alpha))
       100)
      (set-frame-parameter nil 'alpha '(100 100))
    (set-frame-parameter nil 'alpha '(85 50))))


(global-set-key (kbd "C-c t") 'toggle-transparency)

EDIT: answering to your question. If you don't initialize a frame parameter alpha it will be nill. So, if you don't want to have the code:

(set-frame-parameter nil 'alpha '(100 100))

you can check it in the function toggle-transparency

(defun toggle-transparency (unused)
  (interactive "P")
  (let ((curr-alpha (frame-parameter nil 'alpha)))
    (if (or (null curr-alpha) (/= (cadr curr-alpha) 100))
        (set-frame-parameter nil 'alpha '(100 100))
      (set-frame-parameter nil 'alpha '(50 50)))))
Oleg Pavliv
  • 20,462
  • 7
  • 59
  • 75
  • Somehow it didn't work with that "set frame parameter nil". What I had to do to make it work is use the code in my answer below. – PascalVKooten Sep 12 '12 at 10:03
2

Here is a ring version that lets you loop through various alpha, and jumps directly to the last or first alpha when called with a prefix argument:

(defun ring-transparency (arg)
  (interactive "P")
  (let* ((ring '(100 50 25 0))
         (current (frame-parameter nil 'alpha))
         (last (car (last ring)))
         (next (if arg
                   (if (equal current (car ring)) last (car ring))
                 (or (cadr (member current ring)) (car ring)))))
    (set-frame-parameter nil 'alpha next)))
thisirs
  • 790
  • 6
  • 6