Emacs 设置透明度

scinart posted @ 2013年6月27日 00:43 in elisp , 1648 阅读

很多Emacs的配置是小而杂的,今天发一个关于透明度的几个函数,能让你方便的使用它们。

你真正用到的方法,见最后。

 

;;; my-alpha.el ---

;; Copyright 2013 Scinart Ouyang
;;
;; Author: akubeej@gmail.com
;; Version: $Id: my-alpha.el,v 0.0 2013/06/03 12:22:42 scinart Exp $
;; Keywords:
;; X-URL: not distributed yet

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Code:

;; emacs的透明度有两个,一个是激活状态下的透明度,一个是非激活状态下的透明度。

;; 默认透明度增减幅度为2或1,初始为激活不透明,非激活状态透明度50%
(defvar frame-alpha-active-walk-step 2)
(defvar frame-alpha-inactive-walk-step 1)
(defvar frame-alpha-default (list 100 50))

(defconst frame-alpha-original-lower-limit 20)

;; 透明度下限
(setq frame-alpha-lower-limit 10)
(defvar frame-alpha-max 100)
(defvar frame-alpha-min frame-alpha-lower-limit)


(defun frame-alpha-initialize ()
  (set-frame-parameter nil 'alpha (or (frame-parameter nil 'alpha) frame-alpha-default)))
;; important
(frame-alpha-initialize)

(defun frame-alpha-set-active (arg)
  (cond ((> arg frame-alpha-max)
     (dolist (f (frame-list))
       (set-frame-parameter f 'alpha (list frame-alpha-max (second (frame-parameter nil 'alpha))))))
    ((< arg frame-alpha-min)
     (dolist (f (frame-list))
       (set-frame-parameter f 'alpha (list frame-alpha-min (second (frame-parameter nil 'alpha))))))
    (t
     (dolist (f (frame-list))
       (set-frame-parameter f 'alpha (list arg (second (frame-parameter nil 'alpha))))))))
(defun frame-alpha-get-active ()
  (first (frame-parameter nil 'alpha)))

(defun frame-alpha-set-inactive (arg)
  (cond ((> arg frame-alpha-max)
     (dolist (f (frame-list))
       (set-frame-parameter f 'alpha (list (first (frame-parameter nil 'alpha)) frame-alpha-max))))
    ((< arg frame-alpha-min)
     (dolist (f (frame-list))
       (set-frame-parameter f 'alpha (list (first (frame-parameter nil 'alpha)) frame-alpha-min))))
    (t
     (dolist (f (frame-list))
       (set-frame-parameter f 'alpha (list (first (frame-parameter nil 'alpha)) arg))))))
(defun frame-alpha-get-inactive ()
  (second (frame-parameter nil 'alpha)))

(defun frame-alpha-set-all (arg)
  (if (and (list arg)
       (list (cdr arg))
       (null (cddr arg))
       (numberp (first arg))
       (numberp (second arg)))
      (progn
    (frame-alpha-set-active (first arg))
    (frame-alpha-set-inactive (second arg)))
    (message "%s in not a valid parameter like (100 80)" arg)))
(defun frame-alpha-get-all ()
  (frame-parameter nil 'alpha))

(defun frame-alpha-active-increase ()
  (interactive)
  (frame-alpha-set-active (+ (frame-alpha-get-active) frame-alpha-active-walk-step)))
(defun frame-alpha-active-decrease ()
  (interactive)
  (frame-alpha-set-active (- (frame-alpha-get-active) frame-alpha-active-walk-step)))
(defun frame-alpha-inactive-increase ()
  (interactive)
  (frame-alpha-set-inactive (+ (frame-alpha-get-inactive) frame-alpha-inactive-walk-step)))
(defun frame-alpha-inactive-decrease ()
  (interactive)
  (frame-alpha-set-inactive (- (frame-alpha-get-inactive) frame-alpha-inactive-walk-step)))
(defun frame-alpha-all-increase ()
  (frame-alpha-active-increase)
  (frame-alpha-inactive-increase))
(defun frame-alpha-all-decrease ()
  (frame-alpha-active-decrease)
  (frame-alpha-inactive-decrease))

(defun frame-alpha-get ()
  (interactive)
  (message "%s" (frame-parameter (selected-frame) 'alpha)))
(defun frame-alpha-reset ()
  (interactive)
  (set-frame-parameter (selected-frame) 'alpha frame-alpha-default))

(provide 'my-alpha)



;;;;  User Options, Variables
;;;;##########################################################################

;; 这是一个模仿C-x e e e e 这样的宏,感觉很神奇。当需要经常重复一些函数里很有用。
(defmacro continuous-do-things (name round-expression)
  "continuous-do-things, no arg now, please.
example: (continuous-do-things continuous-downcase-word (downcase-word 1))
2013-05-30 Thursday 09:43:50 by Scinart.
so great"
  `(defun ,name ()
     (interactive)
     ,round-expression
     (let ((repeat-key (and (> (length (this-single-command-keys)) 1)
                last-input-event))
       repeat-key-str)
       (setq repeat-key-str (format-kbd-macro (vector repeat-key) nil))
       (while repeat-key
     (unless (current-message)
       (message "(Type %s to repeat)"
            repeat-key-str))
     (if (equal repeat-key (read-event))
         (progn
           (clear-this-command-keys t)
           ,round-expression
           (setq last-input-event nil))
       (setq repeat-key nil)))
       (when last-input-event
     (clear-this-command-keys t)
     (setq unread-command-events (list last-input-event))))))

(continuous-do-things continuous-frame-alpha-active-increase (frame-alpha-active-increase))
(continuous-do-things continuous-frame-alpha-active-decrease (frame-alpha-active-decrease))
(continuous-do-things continuous-frame-alpha-inactive-increase (frame-alpha-inactive-increase))
(continuous-do-things continuous-frame-alpha-inactive-decrease (frame-alpha-inactive-decrease))
(continuous-do-things continuous-frame-alpha-all-increase (frame-alpha-all-increase))
(continuous-do-things continuous-frame-alpha-all-decrease (frame-alpha-all-decrease))

;; 这六个是你需要用的函数。自行绑定你喜欢的快捷键。

;; peek 就是暂时透明1.5秒。C-u peek 就是2秒多,C-u C-u peek 就是三秒多。依此类推。

(defun peek (arg)
  "temporary set transparent to arg seconds
2013-06-06 Thursday 22:45:34 by Scinart"
  (interactive "p")
  (let ((alpha (car (frame-alpha-get-all)))
    (time (1+ (log (+ 1 (abs arg)) 4))))
    (when (< 50 alpha)
      (frame-alpha-set-active 33)
      (message "%d" time)
      (sleep-for (abs time) 500)
      (frame-alpha-set-active alpha))))

;; 保持透明,直到按任意键,不过不可以按C-g,否则会保持透明状态的透明度,还没想出来怎么解决。
(defun perspective ()
  "be transparent until a key stroke
2013-06-06 Thursday 22:45:58 by Scinart"
  (interactive)
  (let ((active-alpha (frame-alpha-get-active))
    (inactive-alpha (frame-alpha-get-inactive)))
    (frame-alpha-set-active frame-alpha-min)
    (frame-alpha-set-inactive frame-alpha-min)
    (read-event)
    (when last-input-event
      (frame-alpha-set-active active-alpha)
      (frame-alpha-set-inactive inactive-alpha)
      (setq unread-command-events (list last-input-event)))))

;; 重新定义了make-frame-command,即C-x 5 2,让新窗口具有与原来窗口一样的透明度。

(defun make-frame-command ()
  "Make a new frame, on the same terminal as the selected frame.
If the terminal is a text-only terminal, this also selects the
new frame.
modified 2013-06-04 Tuesday 00:23:47)"
  (interactive)
  (if (display-graphic-p)
          (make-frame (list (cons 'alpha (frame-parameter (selected-frame) 'alpha))))
    (select-frame (make-frame))))


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter