如何使用Emacs作为默认选项切换全屏?

14
我正在使用来自http://emacsformacosx.com/的Mac Emacs,并且需要在启动Emacs时点击最大化图标。
我该如何将最大化的Emacs窗口设置为默认窗口?

1
你尝试过查看Emacs Wiki吗?例如http://www.emacswiki.org/emacs/FullScreen#toc18和http://www.emacswiki.org/emacs/EmacsForMacOS#toc27。它们可能已经过时,我没有Mac,所以无法测试。 - Luke Girvin
我在使用emacsforosx.com提供的Emacs发行版时遇到了很大的麻烦。今天,我从这个版本中构建了一个全新的Emacs,该版本已经彻底修补了OS X的问题,并且取得了更好的结果,包括正确的OS X全屏支持。希望这可以帮到你! - Aaron Miller
@Eric Fail -- 添加OSX标签的问题在于那些人对Emacs一无所知,往往会因为不是他们的目标受众而进行负面投票和关闭投票。 - lawlist
@lawlist,我不确定我完全理解这个问题。你能详细说明一下吗? - Eric Fail
@Eric Fail -- 我曾经有过关于广泛主题标签的不好经历,论坛参与者很快就会投反对票并关闭帖子。许多人要么订阅感兴趣的特定标签,要么收藏一个喜欢的标签并监控新活动。我个人认为,许多监视OSX标签的人可能根本不在乎如何最大化Emacs框架。大多数声望高的论坛参与者将简单地删除标签而不说任何话,所以如果他们对此感到强烈,我会让他们自己决定。这对我来说并不是什么大问题,我只是想让你知道。 - lawlist
@lawlist,感谢您抽出时间解释。我也没有特别强烈的感觉。我只是想了解根本原因。 - Eric Fail
5个回答

9

按照以下方式启动emacs

emacs -mm

8

你是否能够像非emacs窗口一样获得完全的最大化覆盖?我只能让窗口扩展到我的默认等宽字体的整数倍宽度和高度。在我的(Ubuntu/GNU Emacs 23.1.1)系统上,这会在右侧和底部留下大约半个字符宽的边框。唯一的方法是通过窗口管理器的系统菜单中的最大化选项(位于左上角)来完全扩展窗口。 - Peter.O
PS:我找到了另一个SO答案,其中有一种方法适用于我的Linux X-Windows系统:maximize emacs on start up?(not the fullscreen) .... 取消!它不起作用...它使垂直方向过多最大化... 但是,两种方法结合起来确实有效(奇怪...也许有一天我会弄清楚为什么?...但现在它可以正常工作了(经过几个月的困扰!)...感谢Jon O.的maxframe建议。 - Peter.O
1
你可以通过Emacs软件包管理器和Melpa获取maxframe。 - Jonathan Arkell
@Arkell 抱歉,但你不能在emacs 24(emacsformacos)上这样做...maxframe和Melpa(不管那是什么)都不会出现在emacs包管理器中。 - thefonso
@Peter.O 你好,我有类似的问题:http://superuser.com/questions/584755/emacs-doesnt-fully-maximize-in-linux-slackware 你能分享一下你的可行代码吗? - user4035
@user4035:我已经在paste.ubuntu.com上发布了.emacs代码片段。 - Peter.O

8

这是我编写并使用的一个函数。当你连续按下 F11 键时,emacs 会切换到 4 种模式:

(defun switch-fullscreen nil
  (interactive)
  (let* ((modes '(nil fullboth fullwidth fullheight))
         (cm (cdr (assoc 'fullscreen (frame-parameters) ) ) )
         (next (cadr (member cm modes) ) ) )
    (modify-frame-parameters
     (selected-frame)
     (list (cons 'fullscreen next)))))

(define-key global-map [f11] 'switch-fullscreen)

这对我来说很好用,除了一个烦恼。当我到达 fullboth 模式时,框架会占据屏幕上的所有内容;也就是说,WM 窗口标题栏消失了,我的 XFCE 面板也消失在窗口后面(我正在使用 Xubuntu)。fullheight 模式正常工作:框架仅使用 XFCE 面板留给它的垂直空间。我希望 fullboth 模式能够像按下标题栏中的 + 按钮一样正常工作。有什么想法可以实现吗? - Teemu Leisti
这是gtk中的一个bug,而不是emacs的问题。请向gtk开发人员报告此问题。 - alinsoar
fullboth 是一个误称。在普通的 Mac 术语中,它使 Emacs 进入全屏模式。而 maximized 才是真正的全屏! - trss
当我在2006年或2007年编写这段代码时,它是_fullboth_:请在此处阅读 http://www.emacswiki.org/emacs/FullScreen#toc16 - alinsoar
事实上,我只在X服务器上进行了测试。 - alinsoar

6
简短回答是将以下代码添加到您的custom-set-variables中:
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 ...
 '(initial-frame-alist (quote ((fullscreen . maximized))))
 ...
 )

以下是我对同样问题的解决方案。简而言之,我在所有应用程序中都遇到了这个问题,而不仅仅是在Emacs中。为此,我已经将快捷键cmd-m全局绑定到我的Mac上的缩放菜单选项,通常是绿色最大化按钮的菜单选项。然而,Emacs没有提供缩放菜单选项,通常位于窗口菜单项下面。所以我最终得到了以下结果。昨晚我刚编写了以下内容。
;; This defines cmd-m to do the same as clicking the green titlebar button
;; usually meant for the "Window -> Zoom" menu option in Mac apps
(defun zoom () "zoom, same as clicking the green titlebar button in Mac app windows"
  (interactive)
  (set-frame-parameter
   nil 'fullscreen
   (pcase (frame-parameter nil 'fullscreen)
     (`nil 'fullheight)
     (`fullheight 'maximized)
     (`fullboth (ding) 'fullboth)
     (`fullscreen (ding) 'fullscreen)
     (_ nil))))
(global-set-key (kbd "s-m") 'zoom)

这段代码的最后一行是一个键盘快捷键,与我最初描述的全局Mac cmd+m键绑定很配。您可以根据自己的需要进行自定义。我习惯于在启动大多数应用程序时按下cmd-m,直到它适合屏幕大小,对于Emacs也是如此。所以我不会烦恼initial-frame-alist设置。
今晚我继续完善了我想要的功能集,添加了以下代码。
;; This defines ctrl-cmd-f to do the same as clicking the toggle-fullscreen titlebar
;; icon usually meant for the "View -> Enter/Exit Full Screen" menu option in
;; Mac apps
(defun toggle-fullscreen() "toggle-fullscreen, same as clicking the
 corresponding titlebar icon in the right hand corner of Mac app windows"
  (interactive)
  (set-frame-parameter
   nil 'fullscreen
   (pcase (frame-parameter nil 'fullscreen)
     (`fullboth nil)
     (`fullscreen nil)
     (_ 'fullscreen))))
(global-set-key (kbd "C-s-f") 'toggle-fullscreen)
; For some weird reason C-s-f only means right cmd key!
(global-set-key (kbd "<C-s-268632070>") 'toggle-fullscreen)

一些注意事项:

  1. 如果您从这段代码中学习使用pcase,请小心不要像我一样在文档中将反引号误读为引号。
  2. fullscreenfullboth的别名,并不像后者那样名称与意义不符。因此,我不仅将其作为(frame-parameter nil 'fullscreen)的值进行处理,而且每当我想要将set-frame-parameter设置为fullboth时都会使用它。

希望对您有所帮助。


1

这个https://dev59.com/qnRA5IYBdhLWcg3wzhbZ#1029065 给出的答案对我有用(使用GNU Emacs v24.2.1)。简而言之,在您的.emacs文件中定义以下函数:

(defun x11-maximize-frame ()
  "Maximize the current frame (to full screen)"
  (interactive)
  (x-send-client-message nil 0 nil "_NET_WM_STATE" 32 '(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0))
  (x-send-client-message nil 0 nil "_NET_WM_STATE" 32 '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0)))

为了方便起见,您可以将该命令绑定到一个键上。我使用C-z键,否则它会最小化框架,而我不需要它,但总是在我意外按下它时感到烦恼:

(global-set-key (kbd "C-z") 'x11-maximize-frame)

正如我在那个答案的评论中所指出的,重复使用此命令会在普通框架状态和最大化状态之间循环,但有一个小烦恼:在这两者之间,有一种奇怪的状态,框架几乎但不完全垂直最大化。但这只是一个小问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接