我正在尝试检测我要在其中启动emacs的屏幕大小,并相应地调整窗口(在emacs中称为frame)的大小和位置。我试图设置我的.emacs文件,以便我始终获得一个“相当大”的窗口,其左上角靠近我的屏幕左上角。
我猜这对于一般情况来说是一个很大的要求,所以为了缩小范围,我最感兴趣的是Windows和Linux下的GNU Emacs 22。
如果您想根据分辨率更改大小,可以像这样操作(根据您的具体需求调整首选宽度和分辨率):
(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
文件中有以下内容:
(if (window-system)
(set-frame-height (selected-frame) 60))
您还可以查看函数set-frame-size
、set-frame-position
和set-frame-width
。使用C-h f
(也称为M-x describe-function
)以获取详细的文档。
我不确定是否有一种方法来计算当前窗口环境中框架的最大高度/宽度。
(if (window-system) (set-frame-size (selected-frame) 124 40))
太棒了,简洁明了,可以让我获得终端的默认大小,非常熟悉。 :) (当然可以根据个人偏好进行调整。)谢谢! - lindesinit.el
中,它只适用于初始框架。如果在此之后创建一个新的框架,上面的函数就没有效果了;新的框架将使用在 default-frame-alist
中设置的设置。 - David J.(set-frame-size (selected-frame) 80 24)
给了我一个161x48的框架;我在4K hidpi屏幕上,但我不希望它影响字符计数。 - charliegreen摘自: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后,出现了某些问题:启动时,框架大小是默认值。然后我转而使用Chris Conway的答案,这对我很有效。 - Teemu Leisti尝试将以下代码添加到 .emacs
文件中
(add-to-list 'default-frame-alist '(height . 24))
(add-to-list 'default-frame-alist '(width . 80))
在X-Window环境中,我发现最简单的方法是使用X资源。 我的.Xdefaults文件中相关部分如下:
Emacs.geometry: 80x70
您可以在末尾加上+0+0的坐标来将其强制放置在显示器的左上角。(我不这样做的原因是,我偶尔会生成新的窗口,如果它们出现在与先前窗口完全相同的位置,会令人困惑)
根据手册,这种技术在 MS Windows 上也适用,将资源存储为键/值对在注册表中。我从未测试过。它可能非常好用,但与编辑文件相比,也可能更加麻烦。
emacs -geometry 80x60+20+30
,这将给您一个80个字符宽、60行高的窗口,顶部左侧角落比背景图像向右移动20个像素并向下移动30个像素。(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)
(defun w32-maximize-frame ()
"Maximize the current frame"
(interactive)
(w32-send-sys-command 61488))
(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中,'height无法正常工作。
(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))