【问题标题】:How do I set the size of Emacs' window?如何设置 Emacs 窗口的大小?
【发布时间】:2010-09-10 17:09:20
【问题描述】:

我正在尝试检测我正在启动 emacs 的屏幕的大小,并相应地调整它开始的窗口的大小和位置(我猜那是 emacs-speak 中的框架)。我正在尝试设置我的 .emacs,以便始终获得一个“相当大”的窗口,它位于屏幕左上角附近的左上角。

我想这是对一般情况的要求,所以为了缩小范围,我对 Windows 和 (Debian) Linux 上的 GNU Emacs 22 最感兴趣。

【问题讨论】:

    标签: emacs elisp


    【解决方案1】:

    如果您想根据分辨率更改大小,可以执行以下操作(根据您的具体需要调整首选宽度和分辨率):

    (defun set-frame-size-according-to-resolution ()
      (interactive)
      (if window-system
      (progn
        ;; use 120 char wide window for largeish displays
        ;; and smaller 80 column windows for smaller displays
        ;; pick whatever numbers make sense for you
        (if (> (x-display-pixel-width) 1280)
               (add-to-list 'default-frame-alist (cons 'width 120))
               (add-to-list 'default-frame-alist (cons 'width 80)))
        ;; for the height, subtract a couple hundred pixels
        ;; from the screen height (for panels, menubars and
        ;; whatnot), then divide by the height of a char to
        ;; get the height we want
        (add-to-list 'default-frame-alist 
             (cons 'height (/ (- (x-display-pixel-height) 200)
                                 (frame-char-height)))))))
    
    (set-frame-size-according-to-resolution)
    

    请注意,在较新版本的 emacs 中不推荐使用窗口系统。一个合适的替代品是(display-graphic-p)。有关更多背景信息,请参阅this answer 问题How to detect that emacs is in terminal-mode?

    【讨论】:

    • 我喜欢不同的数字,所以(按照你的建议)我调整了我的数字。不过,这很好用;谢谢!
    • 再一次,我实际上更喜欢this version——更简单(易于阅读和调整),并且非常适合我的需要。不过,两者都是很好的答案! +1
    • 注意高度,至少在 OS X 上的 Emacs 24.2 上。如果将它设置得太高,窗口将卡在缓冲区的第一行不可见的状态-- 点可以向上导航,但窗口不会滚动显示它。
    • 此方法(以及此处列出的其他方法)有一个小瑕疵。 Emacs 将以一定的大小打开,一秒钟后将调整为我们设置的 w/e 大小。但过渡是可见的。有没有办法告诉 emacs 从 t=0 秒开始以所需的大小绘制自己?
    【解决方案2】:

    我的.emacs 中有以下内容:

    (if (window-system)
      (set-frame-height (selected-frame) 60))
    

    您还可以查看函数set-frame-sizeset-frame-positionset-frame-width。使用C-h f(又名M-x describe-function)调出详细文档。

    我不确定是否有办法在当前窗口环境中计算框架的最大高度/宽度。

    【讨论】:

    • 耶! (if (window-system) (set-frame-size (selected-frame) 124 40)) 赢得胜利 - 如此简洁明了,让我得到终端默认的大小,这很好而且熟悉。 :) (当然,根据偏好进行调整。)谢谢!
    • 只是为了澄清:如果你把它放在init.el 它只适用于初始帧。如果之后再创建一个新框架,上述功能将不起作用;新框架将在default-frame-alist 中设置设置。
    • 这对我有用,只是 (set-frame-size (selected-frame) 80 24) 给了我一个 161 x 48 的帧;我在 4K hidpi 屏幕上,但我不认为这会影响字符数。
    【解决方案3】:

    取自:http://www.gnu.org/software/emacs/windows/old/faq4.html

    (setq default-frame-alist
          '((top . 200) (left . 400)
            (width . 80) (height . 40)
            (cursor-color . "white")
            (cursor-type . box)
            (foreground-color . "yellow")
            (background-color . "black")
            (font . "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1")))
    
    (setq initial-frame-alist '((top . 10) (left . 30)))
    

    第一个设置适用于所有 emacs 框架,包括启动时弹出的第一个框架。第二个设置将附加属性添加到第一帧。这是因为有时很高兴知道您启动 emacs 的原始框架。

    【讨论】:

    • 我已经使用该方法很长一段时间来设置框架的高度和宽度 ((width . 80) (height . 40)),但由于某种原因,在我将 Xubuntu 版本从 12.04 升级到 13.10 后,它停止工作:启动时,帧大小是默认值。然后我改用克里斯康威的答案,这对我有用。
    【解决方案4】:

    我发现在 X-Window 环境中执行此操作的最简单方法是通过 X resources。我的 .Xdefaults 的相关部分如下所示:

    Emacs.geometry: 80x70
    

    您应该能够使用 +0+0 位置坐标作为后缀,以将其强制显示在显示屏的左上角。 (我不这样做的原因是我偶尔会产生新的帧,如果它们出现在与前一个完全相同的位置会让人感到困惑)

    根据手册this technique works on MS Windows too,将资源作为键/值对存储在注册表中。我从来没有测试过。这可能很棒,与简单地编辑文件相比,它可能会带来更多的不便。

    【讨论】:

    【解决方案5】:

    尝试将以下代码添加到.emacs

    (add-to-list 'default-frame-alist '(height . 24))
    
    (add-to-list 'default-frame-alist '(width . 80)) 
    

    【讨论】:

      【解决方案6】:

      您也可以在启动 emacs 时使用 -geometry 参数:emacs -geometry 80x60+20+30 将为您提供一个 80 个字符宽、60 行高的窗口,左上角向右 20 像素,左上角向下 30 像素背景。

      【讨论】:

        【解决方案7】:

        在 ubuntu 上做:

        (defun toggle-fullscreen ()
          (interactive)
          (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
                         '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0))
          (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
                         '(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0))
        )
        (toggle-fullscreen)
        

        【讨论】:

          【解决方案8】:

          在 windows 上,您可以使用此功能使 emacs 框架最大化:

          (defun w32-maximize-frame ()
            "Maximize the current frame"
            (interactive)
            (w32-send-sys-command 61488))
          

          【讨论】:

            【解决方案9】:
            (setq initial-frame-alist
                    (append '((width . 263) (height . 112) (top . -5) (left . 5) (font . "4.System VIO"))
                            initial-frame-alist))
            
            (setq default-frame-alist
                    (append '((width . 263) (height . 112) (top . -5) (left . 5) (font . "4.System VIO"))
                            default-frame-alist))
            

            【讨论】:

              【解决方案10】:
              (defun set-frame-size-according-to-resolution ()
                (interactive)
                (if window-system
                (progn
                  ;; use 120 char wide window for largeish displays
                  ;; and smaller 80 column windows for smaller displays
                  ;; pick whatever numbers make sense for you
                  (if (> (x-display-pixel-width) 1280)
                         (add-to-list 'default-frame-alist (cons 'width 120))
                         (add-to-list 'default-frame-alist (cons 'width 80)))
                  ;; for the height, subtract a couple hundred pixels
                  ;; from the screen height (for panels, menubars and
                  ;; whatnot), then divide by the height of a char to
                  ;; get the height we want
                  (add-to-list 'default-frame-alist 
                       (cons 'height (/ (- (x-display-pixel-height) 200)
                                           (frame-char-height)))))))
              
              (set-frame-size-according-to-resolution)
              

              我更喜欢 Bryan Oakley 的设置。但是 'height 在我的 GNU Emacs 24.1.1 中无法正常工作。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2023-04-11
                • 2022-06-18
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多