-
Notifications
You must be signed in to change notification settings - Fork 1
/
escr.el
216 lines (175 loc) · 7.54 KB
/
escr.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
;;; escr.el --- Tool for taking a screen shot of the Emacs' frames, windows and
;;; regions.
;; Copyright (C) 2014 Andrey Tykhonov <[email protected]>
;; Author: Andrey Tykhonov <[email protected]>
;; Maintainer: Andrey Tykhonov <[email protected]>
;; URL: https://github.com/atykhonov/escr
;; Version: 0.1.0
;; Keywords: convenience
;; This file is NOT part of GNU Emacs.
;; This 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 2, or (at your option)
;; any later version.
;; This file 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This tool allows to take a screen shot of the Emacs' frames, windows and regions.
;;
;; Usage:
;;
;; - Use `escr-frame-screenshot' command to take a screen shot of the current frame.
;;
;; - Use `escr-window-screenshot' command to take a screen shot of the current
;; window.
;;
;; - Use `escr-region-screenshot' command to take a screen shot of the current
;; region.
;;
;; Customization:
;;
;; Set `escr-exclude-fringes' to `t' if you want to exclude fringes from a screen
;; shot. This variable does work only for `escr-region-screenshot'.
;;
;; Set `escr-screenshot-width' to nil if you want to take a screen shot of current
;; window width. And set to integer value if you want limit screen shot to the number
;; of maximum column. This variable does work only for `escr-region-screenshot'.
;;
;; Set `escr-screenshot-directory' to any directory where you would like screen shots
;; to be stored. Default directory is "~/.emacs.d/screenshots/". If directory doesn't
;; exist you'll be prompted to create that directory.
;;
;; Change `escr-screenshot-format' to the desired filename format. Default is
;; `%Y-%m-%d-%H%M%S.png' (Read `format-time-string' documentation for details).
;;
;; Set `escr-screenshot-quality' to desired image quality. Default is 100 (maximum
;; image quality).
;; Installation:
;; Assuming that the file `escr.el' is somewhere on the load path, add the following
;; lines to your `.emacs' file:
;; (require 'escr)
;; (global-set-key (kbd "C-c C-e r") 'escr-region-screenshot)
;; (global-set-key (kbd "C-c C-e f") 'escr-frame-screenshot)
;; (global-set-key (kbd "C-c C-e w") 'escr-window-screenshot)
;;
;; Change the key bindings to your liking.
;;
;;; Code:
(defvar escr-exclude-fringes t
"Exclude fringes from the screenshot. It does work only for
`escr-region-screenshot'.")
(defvar escr-screenshot-directory (concat
user-emacs-directory
"screenshots")
"Path to directory where screenshots are going to be stored.")
(defvar escr-filename-format "%Y-%m-%d-%H%M%S.png"
"Screenshots filename format.")
(defvar escr-screenshot-width 80
"Screenshot width. It does work only for `escr-region-screenshot'.
If nil, then screenshot will be of current window width.
If integer, then that value means how many columns screenshot will contain.")
(defvar escr-screenshot-quality "100"
"Screenshot image quality.")
(defun escr-region-screenshot ()
"Make screenshot from the current region. Please note that it
is possible to make screenshot only from the text which is
visible on the screen."
(interactive)
(let ((window-id (frame-parameter (selected-frame) 'window-id))
(char-height (frame-char-height))
(char-width (frame-char-width))
(filename (expand-file-name
(concat escr-screenshot-directory
"/"
(format-time-string escr-filename-format
(current-time)))))
(window-start-line nil)
(window-region-beginning-line nil)
(window-region-end-line nil)
(screenshot-height 0)
(screenshot-width 0)
(screenshot-x (nth 0 (window-pixel-edges)))
(screenshot-y (nth 1 (window-pixel-edges)))
(selection-start (region-beginning))
(selection-end (region-end))
(current-point (point))
(crop ""))
(escr--check-directory)
(setq window-start-line (line-number-at-pos (window-start)))
(goto-char selection-start)
(setq window-region-beginning-line (line-number-at-pos))
(goto-char selection-end)
(setq window-region-end-line (line-number-at-pos))
(goto-char current-point)
(if (integerp escr-screenshot-width)
(setq screenshot-width (* char-width escr-screenshot-width))
(setq screenshot-width (nth 2 (window-pixel-edges))))
(when escr-exclude-fringes
(setq screenshot-width (- screenshot-width
(nth 0 (window-fringes))))
(when (null escr-screenshot-width)
(setq screenshot-width (- screenshot-width
(nth 1 (window-fringes))))))
(setq screenshot-height (* (+ (- window-region-end-line
window-region-beginning-line)
1)
char-height))
(when escr-exclude-fringes
(setq screenshot-x (+ screenshot-x
(nth 0 (window-fringes)))))
(setq screenshot-y (+ screenshot-y
(* (- window-region-beginning-line
window-start-line)
char-height)))
(deactivate-mark t)
(redisplay t)
(escr--screenshot screenshot-x
screenshot-y
screenshot-width
screenshot-height)))
(defun escr-window-screenshot ()
"Make screenshot from the current window."
(interactive)
(escr--check-directory)
(escr--screenshot (nth 0 (window-pixel-edges))
(nth 1 (window-pixel-edges))
(nth 2 (window-pixel-edges))
(nth 3 (window-pixel-edges))))
(defun escr-frame-screenshot ()
"Make screenshot from the current frame."
(interactive)
(escr--check-directory)
(escr--screenshot 1 1
(frame-pixel-width)
(frame-pixel-height)))
(defun escr--screenshot (screenshot-x screenshot-y
screenshot-width
screenshot-height)
(let ((window-id (frame-parameter (selected-frame) 'window-id))
(crop (format "%sx%s+%s+%s"
screenshot-width
screenshot-height
screenshot-x
screenshot-y))
(filename (expand-file-name
(concat escr-screenshot-directory
"/"
(format-time-string escr-filename-format
(current-time))))))
(call-process "import" nil nil nil
"-window" window-id
"-crop" crop
"-quality" escr-screenshot-quality
filename)))
(defun escr--check-directory ()
(when (not (file-exists-p escr-screenshot-directory))
(if (y-or-n-p (format "Directory %s does not exist. Create it?"
escr-screenshot-directory))
(make-directory escr-screenshot-directory)
(error "Please create the directory first."))))
(provide 'escr)
;;; escr.el ends here