-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxframe.el
164 lines (142 loc) · 5.54 KB
/
maxframe.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
;;; $Id: maxframe.el 386 2007-04-11 15:43:12Z ryan $
;; maximize the emacs frame based on display size
;; Copyright (C) 2007 Ryan McGeary
;; Author: Ryan McGeary
;; Version: 0.3
;; Keywords: display frame window maximize
;; This code is free; you can redistribute it and/or modify it under the
;; terms of the GNU General Public License as published by the Free
;; Software Foundation; either version 2, or (at your option) any later
;; version.
;;; Commentary:
;;
;; Purpose
;; -------
;; maxframe provides the ability to maximize the emacs frame and stay within
;; the display resolution.
;;
;; Usage
;; -----
;; Example of lines to be added to your .emacs:
;;
;; (require 'maxframe)
;; (add-hook 'window-setup-hook 'maximize-frame t)
;;
;; If using two framebuffers (monitors), it might be necesssary to specify a
;; mf-max-width value set to the pixel width of main framebuffer. This is
;; necessary because emacs does not yet support sniffing different
;; framebuffers. Example:
;;
;; (require 'maxframe)
;; (setq mf-max-width 1600) ;; Pixel width of main monitor.
;; (add-hook 'window-setup-hook 'maximize-frame t)
;;
;; How it works
;; ------------
;; puts the emacs frame in the top left corner of the display and calculates
;; the maximum number of columns and rows that can fit in the display
;;
;; Limitations
;; -----------
;; Requires Emacs 22 (for fringe support), but maximize-frame still works
;; under Emacs 21 on Windows.
;;
;; Emacs does not recognize when the display's resolution is changed. This is
;; a problem because I would like to be able to re-maximize the frame after
;; connecting to a display with different resolution. Unfortunately,
;; display-pixel-width and display-pixel-height yield the display resolution
;; values from when emacs was started instead of the current display
;; values. Perhaps there's a way to have emacs re-sniff these values, but I'm
;; not yet sure how.
;;
;; Credits
;; -------
;; The w32 specific functions were borrowed from the Emacs Manual:
;; http://www.gnu.org/software/emacs/windows/big.html#windows-like-window-ops
(defgroup maxframe nil "Handle maximizing frames.")
(defcustom mf-display-padding-width 0
"*Any extra display padding that you want to account for while
determining the maximize number of columns to fit on a display"
:type 'integer
:group 'maxframe)
;; The default accounts for a Mac OS X display with a menubar
;; height of 22 pixels, a titlebar of 23 pixels, and no dock.
(defcustom mf-display-padding-height (+ 22 23)
"*Any extra display padding that you want to account for while
determining the maximize number of rows to fit on a display"
:type 'integer
:group 'maxframe)
(defcustom mf-offset-x 0
"*The x coordinate of the upper left corner of the frame.
Negative values are interpreted relative to the rightmost
position. See `set-frame-position'."
:type 'integer
:group 'maxframe)
(defcustom mf-offset-y 0
"*The y coordinate of the upper left corner of the frame.
Negative values are interpreted relative to the bottommost
position. See `set-frame-position'."
:type 'integer
:group 'maxframe)
(defcustom mf-max-width nil
"*The maximum display width to support. This helps better
support the true nature of display-pixel-width. Since multiple
monitors will result in a very large display pixel width, this
value is used to set the stop point for maximizing the frame.
This could also be used to set a fixed frame size without going
over the display dimensions."
:type 'integer
:group 'maxframe)
(defcustom mf-max-height nil
"*The maximum display height to support. This helps better
support the true nature of display-pixel-height. See
`mf-max-width'."
:type 'integer
:group 'maxframe)
(defun w32-maximize-frame ()
"Maximize the current frame (windows only)"
(interactive)
(w32-send-sys-command 61488))
(defun w32-restore-frame ()
"Restore a minimized/maximized frame (windows only)"
(interactive)
(w32-send-sys-command 61728))
(defun mf-max-columns (width)
"Calculates the maximum number of columns that can fit in
pixels specified by WIDTH."
(let ((scroll-bar (or (frame-parameter nil 'scroll-bar-width) 0))
(left-fringe (or left-fringe-width (nth 0 (window-fringes)) 0))
(right-fringe (or right-fringe-width (nth 1 (window-fringes)) 0)))
(/ (- width scroll-bar left-fringe right-fringe
mf-display-padding-width)
(frame-char-width))))
(defun mf-max-rows (height)
"Calculates the maximum number of rows that can fit in pixels
specified by HEIGHT."
(/ (- height
mf-display-padding-height)
(frame-char-height)))
(defun mf-set-frame-pixel-size (frame width height)
"Sets size of FRAME to WIDTH by HEIGHT, measured in pixels."
(set-frame-size frame (mf-max-columns width) (mf-max-rows height)))
(defun mf-max-display-pixel-width ()
(min (display-pixel-width)
(or mf-max-width (display-pixel-width))))
(defun mf-max-display-pixel-height ()
(min (display-pixel-height)
(or mf-max-height (display-pixel-height))))
(defun x-maximize-frame ()
"Maximize the current frame (x or mac only)"
(interactive)
(mf-set-frame-pixel-size (selected-frame)
(mf-max-display-pixel-width)
(mf-max-display-pixel-height))
(set-frame-position (selected-frame) mf-offset-x mf-offset-y))
(defun maximize-frame ()
"Maximizes the frame to fit the display if under a windowing
system."
(interactive)
(cond ((eq window-system 'w32) (w32-maximize-frame))
((memq window-system '(x mac)) (x-maximize-frame))))
(defalias 'mf 'maximize-frame)
(provide 'maxframe)