PascalVKooten
PascalVKooten

Reputation: 21451

creating a toggle function in emacs

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?

Upvotes: 2

Views: 331

Answers (2)

thisirs
thisirs

Reputation: 788

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)))

Upvotes: 2

Oleg Pavliv
Oleg Pavliv

Reputation: 21182

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)))))

Upvotes: 2

Related Questions