2

例如,如何获得一个切换透明度的功能?切换的可能候选者可能是 0%、85%、100% 或者可能只是其中的 2 个......

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

什么不见​​了?

4

2 回答 2

2

以下代码取自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)

编辑:回答你的问题。如果您不初始化帧参数alpha,它将为零。因此,如果您不想拥有代码:

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

你可以在函数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)))))
于 2012-09-12T08:20:46.730 回答
2

这是一个环形版本,可让您循环遍历各种 alpha,并在使用前缀参数调用时直接跳转到最后一个或第一个 alpha:

(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)))
于 2012-09-12T12:49:22.580 回答