├── README.md
└── default-text-scale.el
/README.md:
--------------------------------------------------------------------------------
1 | [](http://melpa.org/#/default-text-scale)
2 | [](http://stable.melpa.org/#/default-text-scale)
3 |
4 |
5 | # Easily adjust the font size in all Emacs frames
6 |
7 | This package provides commands for increasing or decreasing the
8 | default font size in all GUI Emacs frames -- it is like an Emacs-wide
9 | version of `text-scale-mode`.
10 |
11 | It works by adjusting the height of the `default` face in the
12 | `user` theme, which is always combined with any other loaded
13 | themes.
14 |
15 | It's handy for quickly adjusting the font size for readability or
16 | impromptu screen-sharing.
17 |
18 | **Note that as of Emacs 29, many users might prefer the built-in `global-text-scale-adjust` system.**
19 |
20 | ## Installation
21 |
22 | Install `default-text-scale` from the [MELPA](http://melpa.org)
23 | repository. The version of `default-text-scale` there will always be
24 | up-to-date.
25 |
26 | ## Usage
27 |
28 | You can then customize/enable the global minor mode
29 | `default-text-scale-mode`, which binds C-M-= and
30 | C-M-- by default. Alternatively, bind the corresponding
31 | commands yourself as desired.
32 |
33 | ## About
34 |
35 | Author: Steve Purcell
36 |
37 | Homepage: https://github.com/purcell/default-text-scale
38 |
39 | This little library was extracted from the author's
40 | [full Emacs configuration](https://github.com/purcell/emacs.d), which
41 | readers might find of interest.
42 |
43 |
44 |
45 | [💝 Support this project and my other Open Source work](https://www.patreon.com/sanityinc)
46 |
47 | [💼 LinkedIn profile](https://uk.linkedin.com/in/stevepurcell)
48 |
49 | [✍ sanityinc.com](http://www.sanityinc.com/)
50 |
--------------------------------------------------------------------------------
/default-text-scale.el:
--------------------------------------------------------------------------------
1 | ;;; default-text-scale.el --- Easily adjust the font size in all frames -*- lexical-binding: t -*-
2 |
3 | ;; Copyright (C) 2015 Steve Purcell
4 |
5 | ;; Author: Steve Purcell
6 | ;; URL: https://github.com/purcell/default-text-scale
7 | ;; Keywords: frames, faces
8 | ;; Package-Version: 0
9 | ;; Package-Requires: ((emacs "24"))
10 |
11 | ;; This program is free software; you can redistribute it and/or modify
12 | ;; it under the terms of the GNU General Public License as published by
13 | ;; the Free Software Foundation, either version 3 of the License, or
14 | ;; (at your option) any later version.
15 |
16 | ;; This program is distributed in the hope that it will be useful,
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 | ;; GNU General Public License for more details.
20 |
21 | ;; You should have received a copy of the GNU General Public License
22 | ;; along with this program. If not, see .
23 |
24 | ;;; Commentary:
25 |
26 | ;; This package provides commands for increasing or decreasing the
27 | ;; default font size in all GUI Emacs frames -- it is like an
28 | ;; Emacs-wide version of `text-scale-mode'.
29 |
30 | ;; Usage:
31 |
32 | ;; Enable the global minor mode `default-text-scale-mode' to bind
33 | ;; C-M-= and C-M-- to `default-text-scale-increase' and
34 | ;; `default-text-scale-decrease' respectively. Alternatively, bind
35 | ;; those commands directly in another map.
36 |
37 | ;;; Code:
38 |
39 | (defgroup default-text-scale nil
40 | "Adjusting the default font size across all frames."
41 | :group 'faces
42 | :prefix "default-text-scale-")
43 |
44 | (defcustom default-text-scale-amount 10
45 | "Increment by which to adjust the :height of the default face."
46 | :type 'integer
47 | :group 'default-text-scale)
48 |
49 | (defvar default-text-scale--complement 0
50 | "Stores the delta needed to get back to the original default face height.")
51 |
52 | (defun default-text-scale-increment (delta)
53 | "Adjust the default font height by DELTA on every graphical frame.
54 | The pixel size of the frame will be kept approximately the same,
55 | to the extent possible, as with the function `set-frame-font'.
56 | DELTA should be a multiple of 10, to match the units used by
57 | the :height face attribute."
58 | (interactive "nIncrement (e.g. 10, -5)? ")
59 | (unless (display-multi-font-p (selected-frame))
60 | (error "Cannot adjust default text scale from a non-graphical frame"))
61 | (let* ((cur-height (face-attribute 'default :height))
62 | (new-height (+ cur-height delta))
63 | (initial-char-width (frame-char-width (selected-frame)))
64 | (initial-char-height (frame-char-height (selected-frame)))
65 | frame-sizes)
66 | (dolist (f (frame-list))
67 | (when (display-multi-font-p f)
68 | (unless (frame-parameter f 'fullscreen)
69 | (push (list f (frame-parameter f 'height) (frame-parameter f 'width)) frame-sizes))))
70 | (face-spec-set 'default `((t (:height ,new-height))))
71 | (dolist (entry frame-sizes)
72 | (let ((f (car entry))
73 | (orig-height (nth 1 entry))
74 | (orig-width (nth 2 entry)))
75 | (face-spec-recalc 'default f)
76 | (modify-frame-parameters
77 | f
78 | `((height . ,(round (* initial-char-height orig-height)
79 | (frame-char-height f)))
80 | (width . ,(round (* initial-char-width orig-width)
81 | (frame-char-width f)))))
82 | (with-selected-frame f
83 | (run-hooks 'after-setting-font-hook)))) ;; This line is apparently necessary for Emacs to properly
84 | ;; recalculate the face attributes in order for the
85 | ;; actually-applied height to be correctly returned
86 | ;; below. Evidently some visible text must be displayed (however
87 | ;; briefly) for this to occur: a temp buffer is insufficient.
88 | (message "Stale font size: %d" (face-attribute 'default :height))
89 | (let* ((actual-new-height (face-attribute 'default :height))
90 | (actual-delta (- actual-new-height cur-height)))
91 | (setq default-text-scale--complement (- default-text-scale--complement actual-delta))
92 | (message "Default font size is now %d" actual-new-height))))
93 |
94 | ;;;###autoload
95 | (defun default-text-scale-increase ()
96 | "Increase the height of the default face by `default-text-scale-amount'."
97 | (interactive)
98 | (default-text-scale-increment default-text-scale-amount))
99 |
100 | ;;;###autoload
101 | (defun default-text-scale-decrease ()
102 | "Decrease the height of the default face by `default-text-scale-amount'."
103 | (interactive)
104 | (default-text-scale-increment (- default-text-scale-amount)))
105 |
106 | ;;;###autoload
107 | (defun default-text-scale-reset (&optional set-current)
108 | "Reset the height of the default face.
109 | With prefix argument SET-CURRENT, set the current size as the
110 | default to which subsequent sizes would be reset."
111 | (interactive "P")
112 | (if set-current
113 | (message "Default font size set to current size.")
114 | (default-text-scale-increment default-text-scale--complement))
115 | (setq default-text-scale--complement 0))
116 |
117 | (defun default-text-scale--update-for-new-frame (f)
118 | "Recalculate the font size in new frame F.
119 | This ensures new frames have the correct font size after the font
120 | has been set with `set-face-attribute'."
121 | (when (display-multi-font-p f)
122 | (face-spec-recalc 'default f)))
123 |
124 | ;;;###autoload
125 | (define-minor-mode default-text-scale-mode
126 | "Change the size of the \"default\" face in every frame."
127 | :global t
128 | :require 'default-text-scale
129 | :keymap (let ((map (make-sparse-keymap)))
130 | (define-key map (kbd "C-M-=") 'default-text-scale-increase)
131 | (define-key map (kbd "C-M--") 'default-text-scale-decrease)
132 | (define-key map (kbd "C-M-0") 'default-text-scale-reset)
133 | map)
134 | (if default-text-scale-mode
135 | (progn
136 | (add-hook 'after-make-frame-functions #'default-text-scale--update-for-new-frame)
137 | (setq default-text-scale--complement 0))
138 | (remove-hook 'after-make-frame-functions #'default-text-scale--update-for-new-frame)
139 | (default-text-scale-reset)))
140 |
141 |
142 | (provide 'default-text-scale)
143 | ;;; default-text-scale.el ends here
144 |
--------------------------------------------------------------------------------