I do not know whether the following does resolve your problem. Your code has some flaws. Maybe, these cause the dysfunction.
The last sentence of the following section of the frame parameter documentation says that the associated value of
alpha can also be a number or
nil (when the window manager should handle transparency).
This parameter specifies the opacity of the frame, on graphical
displays that support variable opacity. It should be an integer
between 0 and 100, where 0 means completely transparent and 100
means completely opaque. It can also have a ‘nil’ value, which
tells Emacs not to set the frame opacity (leaving it to the window
Your code will error-out in those cases since
cadr expects a list and
\= expects numbers as arguments. It handles only the case of the following subsequent section in the doc:
The alpha frame parameter can also be a cons cell(‘active’ . ‘inactive’),
where ‘active’ is the opacity of the frame when it is selected,
and ‘inactive’ is the opacity when it is not selected.
There follows a modified function
toggle-transparency that regards the doc.
(defun toggle-transparency ()
(let ((alpha (frame-parameter nil 'alpha)))
(if (numberp alpha)
(cdr alpha)) ; may also be nil
(set-frame-parameter nil 'alpha '(85 . 50))
(set-frame-parameter nil 'alpha '(100 . 100)))))