首页 / 知识

关于elisp:如何设置Emacs窗口的大小?

2023-04-17 04:11:00

关于elisp:如何设置Emacs窗口的大小?

How do I set the size of Emacs' window?

我正在尝试检测要在其上启动emacs的屏幕的大小,并相应地调整其大小和位置(在emacs中,这就是emacs的框架)。 我正在尝试设置.emacs,以便始终获得一个"合理大"的窗口,该窗口的左上角靠近屏幕的左上角。

我想这对一般情况来说是一个很大的要求,因此要缩小范围,我对Windows和(Debian)Linux上的GNU Emacs 22最感兴趣。


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(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不推荐使用window-system。合适的替代品是(display-graphic-p)。看到这个问题的答案如何检测emacs是否处于终端模式?多一点背景。


我的.emacs中包含以下内容:

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

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

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


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

1
2
3
4
5
6
7
8
9
10
(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的原始框架。


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

1
2
3
(add-to-list 'default-frame-alist '(height . 24))

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

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

1
Emacs.geometry: 80x70

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

根据手册,该技术也适用于MS Windows,将资源作为键/值对存储在注册表中。我从来没有测试过。与仅编辑文件相比,这可能很棒,可能带来更多的不便。


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


在ubuntu上执行:

1
2
3
4
5
6
7
8
(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)

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

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

1
2
3
4
5
6
7
(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))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(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的设置。但是,在我的GNU Emacs 24.1.1中,"高度不能正常工作"。


屏幕检测调整框架

最新内容

相关内容

猜你喜欢