简体   繁体   English

失去焦点时更改Emacs窗口外观

[英]Change Emacs window appearance when it loses focus

When I program I use two screens with Emacs on both with two buffers split in each window totaling 4 open source files on screen at any one time. 当我编程时,我使用带有Emacs的两个屏幕,两个缓冲区在每个窗口中分开,在任何时候在屏幕上共计4个开源文件。

I switch between buffers with Cx b and between Windows with Alt-TAB . 我在具有Cx b缓冲区之间以及在使用Alt-TAB Windows之间切换。 I change the appearance of buffers when I switch between them by defining different faces for mode-line and mode-line-inactive . 当我通过为mode-linemode-line-inactive定义不同的面来切换它们时,我改变了缓冲区的外观。 But how do I inactivate a buffer when I switch from the Emacs window completely to another Emacs window via Alt-TAB ? 但是当我通过Alt-TAB从Emacs窗口完全切换到另一个Emacs窗口时,如何禁用缓冲区?

It's probably also relevant that I'm using Emacs 23.2.1 on Ubuntu 11.04 with Gnome 2.32.1. 我在Ubuntu 11.04和Gnome 2.32.1上使用Emacs 23.2.1可能也是相关的。

PS: The question How to automatically save files on lose focus in Emacs is after a different goal but with the same original event of "window losing focus". PS: 如何在Emacs中自动保存失去焦点的文件的问题是在一个不同的目标之后,但是具有相同的原始事件“窗口失去焦点”。

It may depend on your window manager and how it manages multiple windows, or frames, in emacs parlance. 它可能取决于您的窗口管理器以及它如何管理多个窗口或框架,用emacs的说法。 The code below works like a champ in fvwm but not always in gnome. 下面的代码就像fvwm中的冠军一样,但并不总是在gnome中。

I map a keystroke, Co, to go between frames, this helps when you want to go to the other frame but an alt-tab would take you through a number of superfluous apps on the way. 我将键击Co映射到帧之间,这有助于你想要转到另一帧但是alt-tab会带你通过一些多余的应用程序。

If you're running a single instance of emacs with two frames you could use something like the following: 如果您正在运行具有两个框架的单个emacs实例,则可以使用以下内容:

(defun pgr-previous-frame ()
  "go to the previous frame"
  (interactive)
  (pgr-switch-frame (previous-frame)))

(defun pgr-next-frame ()
  "go to the next frame"
  (interactive)
  (pgr-switch-frame (next-frame)))

(defun pgr-switch-frame (frame)
  "go to the specified frame and raise it"
  ;; reset the frame properties here
  (select-frame frame)     
  (raise-frame frame)
  ;;change the display in some manner here
  )

You could also try adding some advice to raise-frame and lower-frame haven't tried it but it's worth a try. 您还可以尝试添加一些建议来提升帧和低帧没有尝试过,但它值得一试。

In Emacs 24.4 and later, you can use focus-in-hook and focus-out-hook . 在Emacs 24.4及更高版本中,您可以使用focus-in-hookfocus-out-hook This piece of code seems to work, such that the active window of an inactive frame has the same colour as an inactive window: 这段代码似乎有用,这样非活动帧的活动窗口与非活动窗口的颜色相同:

(defvar my-mode-line-active-background "gray75")
(defvar my-mode-line-inactive-background "gray40")

(defun my-unhighlight-mode-line ()
  (set-face-attribute 'mode-line nil
                      :background my-mode-line-inactive-background))

(add-hook 'focus-out-hook 'my-unhighlight-mode-line)

(defun my-highlight-mode-line ()
  (set-face-attribute 'mode-line nil
                      :background my-mode-line-active-background))

(add-hook 'focus-in-hook 'my-highlight-mode-line)

I really liked @logoscia answer, which allowed me to do this more generic version. 我真的很喜欢@logoscia的答案,这让我可以做更通用的版本。 It uses the mode-line-inactive face when no focus. 它在没有焦点时使用mode-line-inactive面。

(add-hook 'focus-out-hook
      (lambda ()
        (copy-face 'mode-line '--mode-line-backup)
        (copy-face 'mode-line-inactive 'mode-line)))
(add-hook 'focus-in-hook
      (lambda ()
        (copy-face '--mode-line-backup 'mode-line)))

I don't know if it can be done only with Emacs, but a possible alternative is running wmctrl in a shell script which perodically checks which window has the focus and if there is a change then it lets Emacs know via emacsclient which can send lisp code to a running Emacs for evaluation: 我不知道它是否只能用Emacs完成,但是一个可能的替代方法是在shell脚本中运行wmctrl ,它可以永久地检查哪个窗口具有焦点,如果有更改,那么它可以通过emacsclient知道Emacs可以发送lisp用于评估的正在运行的Emacs的代码:

-e' --eval' Tell Emacs to evaluate some Emacs Lisp code, instead of visiting some files. -e' eval'告诉Emacs评估一些Emacs Lisp代码,而不是访问某些文件。 When this option is given, the arguments to `emacsclient' are interpreted as a list of expressions to evaluate, not as a list of files to visit. 当给出这个选项时,`emacsclient'的参数被解释为要评估的表达式列表, 而不是要访问的文件列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM