-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixed-scale.el
executable file
·127 lines (101 loc) · 4.5 KB
/
fixed-scale.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
;;; fixed-scale.el --- Make text scale stickier -*- lexical-binding: t; -*-
;; Copyright (C) 2016 Aaron Harris
;; Author: Aaron Harris <[email protected]>
;; Keywords: convenience
;; Dependencies: `seq'
;; This program is free software; 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 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This module defines `fixed-scale-mode', a global minor mode that
;; attempts to preserve the current text scaling when certain commands
;; change it.
;;
;; To use the mode, just enable `fixed-scale-mode' in the usual way
;; and add commands that you wish preserved text scaling to the
;; variable `fixed-scale-command-list'.
;;
;; If you use a replacement for `execute-extended-command' (M-x), you
;; may need to add it to the list `fixed-scale-extended-command-list'
;; so that `fixed-scale-mode' knows that it should check
;; `extended-command-history' for the actual command being executed.
;;; Code:
(require 'seq)
;;;; User Options
;;===============
(defgroup fixed-scale nil
"Make text scale stickier."
:prefix "fixed-scale-"
:link '(emacs-commentary-link "fixed-scale")
:group 'display)
(define-widget 'fixed-scale-command 'lazy
"A command (interactive function)."
:offset 4
:tag "Command"
:type '(restricted-sexp :match-alternatives commandp))
(defcustom fixed-scale-command-list nil
"A list of commands that should not change text scaling."
:type '(repeat fixed-scale-command))
(defcustom fixed-scale-extended-command-list
'(execute-extended-command helm-M-x)
"A list of commands that execute other commands.
When one of these commands is executed, `fixed-scale-mode'
consults `extended-command-history' to determine whether the
command executed is in `fixed-scale-command-list'."
:type '(repeat fixed-scale-command))
;;;; State Variables
;;==================
(defvar fixed-scale 0
"Text scale remembered by `fixed-scale-mode'.")
;;;; Implementation
;;=================
(defun fixed-scale-remember ()
"Save current text scaling, if necessary.
Intended for use in `pre-command-hook'. If `this-command' is in
`fixed-scale-command-list', remember the current text scale (as
`fixed-scale') and add `fixed-scale-reset' to `post-command-hook'
so it can be restored."
(when (require 'validate nil :noerror)
(validate-variable 'fixed-scale-command-list))
(when (bound-and-true-p text-scale-mode)
(setq fixed-scale text-scale-mode-amount)
(add-hook 'post-command-hook #'fixed-scale-reset)))
(defun fixed-scale-reset ()
"Restore text scaling from last `fixed-scale-remember'.
Intended for use in `post-command-hook'. Set the current text
scale to whatever `fixed-scale-remember' remembered (as
`fixed-scale'), and remove self from `post-command-hook'."
(remove-hook 'post-command-hook #'fixed-scale-reset)
(when (or (seq-contains fixed-scale-command-list this-command)
(and (seq-contains fixed-scale-extended-command-list
this-command)
(seq-contains fixed-scale-command-list
(intern-soft (car extended-command-history)))))
(text-scale-set fixed-scale)))
;;;###autoload
(define-minor-mode fixed-scale-mode
"Global minor mode to make `text-scale-mode' stickier.
Sometimes a command will remove text scaling as a byproduct of
its function. This mode attempts to rectify that. To use it,
you must add commands that reset the text scale to the variable
`fixed-scale-command-list'. Then, whenever one of those commands
is executed while `fixed-scale-mode' is enabled (it is enabled by
default, if the module `fixed-scale' has been loaded),
`fixed-scale-mode' will remember the proper text scaling and
restore it after the command is finished."
:global t
:init-value t
:require 'fixed-scale
(if fixed-scale-mode
(add-hook 'pre-command-hook #'fixed-scale-remember)
(remove-hook 'pre-command-hook #'fixed-scale-remember)
(remove-hook 'post-command-hook #'fixed-scale-reset)))
(provide 'fixed-scale)
;;; fixed-scale.el ends here