]> git.rkrishnan.org Git - .emacs.d.git/blob - emacs/maxframe.el
submodulized .emacs.d setup
[.emacs.d.git] / emacs / maxframe.el
1 ;;; $Id: maxframe.el 331 2007-02-20 16:58:21Z ryan $
2 ;; maximize the emacs frame based on display size
3
4 ;; Copyright (C) 2007 Ryan McGeary
5 ;; Author: Ryan McGeary
6 ;; Keywords: display frame window maximize
7
8 ;; This code is free; you can redistribute it and/or modify it under the
9 ;; terms of the GNU General Public License as published by the Free
10 ;; Software Foundation; either version 2, or (at your option) any later
11 ;; version.
12
13 ;;; Commentary:
14 ;;
15 ;; Purpose
16 ;; -------
17 ;; maxframe provides the ability to maximize the emacs frame and stay within
18 ;; the display resolution.
19 ;;
20 ;; Usage
21 ;; -----
22 ;; Example of lines to be added to your .emacs:
23 ;;
24 ;;     (require 'maxframe)
25 ;;     (add-hook 'window-setup-hook 'maximize-frame t)
26 ;;
27 ;; How it works
28 ;; ------------
29 ;; puts the emacs frame in the top left corner of the display and calculates
30 ;; the maximum number of columns and rows that can fit in the display
31 ;;
32 ;; Limitations
33 ;; -----------
34 ;; Requires Emacs 22 (for fringe support), but maximize-frame still works
35 ;; under Emacs 21 on Windows.
36 ;;
37 ;; Emacs does not recognize when the display's resolution is changed. This is
38 ;; a problem because I would like to be able to re-maximize the frame after
39 ;; connecting to a display with different resolution. Unfortunately,
40 ;; display-pixel-width and display-pixel-height yield the display resolution
41 ;; values from when emacs was started instead of the current display
42 ;; values. Perhaps there's a way to have emacs re-sniff these values, but I'm
43 ;; not yet sure how.
44
45
46 (defgroup maxframe nil "Handle maximizing frames.")
47
48 (defcustom mf-display-padding-width 0
49   "*Any extra display padding that you want to account for while
50 determining the maximize number of columns to fit on a display"
51   :type 'integer
52   :group 'maxframe)
53
54 ;; The default accounts for a Mac OS X display with a menubar 
55 ;; height of 22 pixels, a titlebar of 23 pixels, and no dock.
56 (defcustom mf-display-padding-height (+ 22 23)
57   "*Any extra display padding that you want to account for while
58 determining the maximize number of rows to fit on a display"
59   :type 'integer
60   :group 'maxframe)
61
62 (defun w32-maximize-frame ()
63   "Maximize the current frame (windows only)"
64   (interactive)
65   (w32-send-sys-command 61488))
66
67 (defun w32-restore-frame ()
68   "Restore a minimized/maximized frame (windows only)"
69   (interactive)
70   (w32-send-sys-command 61728))
71
72 (defun mf-max-columns (width)
73   "Calculates the maximum number of columns that can fit in
74 pixels specified by WIDTH."
75   (let ((scroll-bar (or (frame-parameter nil 'scroll-bar-width) 0))
76         (left-fringe (or left-fringe-width (nth 0 (window-fringes)) 0))
77         (right-fringe (or right-fringe-width (nth 1 (window-fringes)) 0)))
78     (/ (- width scroll-bar left-fringe right-fringe
79           mf-display-padding-width)
80        (frame-char-width))))
81
82 (defun mf-max-rows (height)
83   "Calculates the maximum number of rows that can fit in pixels
84 specified by HEIGHT."
85   (/ (- height
86         mf-display-padding-height)
87      (frame-char-height)))
88
89 (defun mf-set-frame-pixel-size (frame width height)
90   "Sets size of FRAME to WIDTH by HEIGHT, measured in pixels."
91   (set-frame-size frame (mf-max-columns width) (mf-max-rows height)))
92
93 (defun x-maximize-frame ()
94   "Maximize the current frame (x or mac only)"
95   (interactive)
96   (mf-set-frame-pixel-size (selected-frame)
97                            (display-pixel-width)
98                            (display-pixel-height))
99   (set-frame-position (selected-frame) 0 0))
100
101 (defun maximize-frame ()
102   "Maximizes the frame to fit the display if under a windowing
103 system."
104   (interactive)
105   (cond ((eq window-system 'w32) (w32-maximize-frame))
106         ((memq window-system '(x mac)) (x-maximize-frame))))
107
108 (defalias 'mf 'maximize-frame)
109
110 (provide 'maxframe)