-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen-capture.el
127 lines (108 loc) · 4.79 KB
/
screen-capture.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
;; The MIT License (MIT)
;; Copyright (c) 2021 B.V. Raghav
;; Permission is hereby granted, free of charge, to any
;; person obtaining a copy of this software and associated
;; documentation files (the ``Software''), to deal in the
;; Software without restriction, including without
;; limitation the rights to use, copy, modify, merge,
;; publish, distribute, sublicense, and/or sell copies of
;; the Software, and to permit persons to whom the
;; Software is furnished to do so, subject to the
;; following conditions:
;; The above copyright notice and this permission notice
;; shall be included in all copies or substantial portions
;; of the Software.
;; THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF
;; ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
;; TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
;; PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
;; SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
;; ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
;; OTHER DEALINGS IN THE SOFTWARE.
(require 'seq)
(defgroup screen-capture nil "Screen Capture Group")
(defcustom screen-capture-binary "~/.local/bin/screen-capture"
"Location of screen capture binary."
:type '(file :must-match t)
:group 'screen-capture)
(defcustom screen-capture-destination "~/.screen-capture/dest"
"Path to screen capture destination folder."
:type 'directory
:group 'screen-capture)
(defcustom screen-capture-destinations
;; '(("www" . "~/public_html/images")
;; ("org" . "~/org/images")
;; ("roam" . "~/org-roam/images")
;; ("tmp" . "~/tmp/screen-capture"))
nil
"List of screen capture destinations."
:type '(alist :key-type string
:value-type directory)
:group 'screen-capture)
(defcustom screen-capture-styles
'("complete" "active" "window" "patch")
"List of capture styles"
:type '(list string))
(defun screen-capture-binary ()
(expand-file-name screen-capture-binary))
(defun screen-capture (style-or-dest &optional minimize-frame)
(interactive
(list (completing-read "Screen Capture: "
(screen-capture--options)
nil t)
(prefix-numeric-value current-prefix-arg)))
(if (member style-or-dest (screen-capture--destination-names))
(progn (screen-capture--set-dest style-or-dest)
(call-interactively #'screen-capture))
(and (message "Calling process: %s %s" (screen-capture-binary) style-or-dest)
(call-process (screen-capture-binary) nil nil nil style-or-dest)
(call-process "i3-msg" nil nil nil "floating enable")
(call-process "i3-msg" nil nil nil "resize set 300 100"))))
(defun screen-captured-last ()
(interactive)
(let* ((asc-modif #'screen-capture--time-ascending)
(default-directory
(file-name-as-directory screen-capture-destination))
(last-capture
(caar (reverse (sort (screen-captured-all) asc-modif)))))
(file-truename last-capture)))
(defun screen-captured-all ()
(interactive)
(directory-files-and-attributes default-directory))
(defun screen-capture--destination-names ()
(mapcar #'car (screen-capture--get-destinations)))
(defun screen-capture--get-destinations ()
(or screen-capture-destinations
(let* ((default-directory (file-name-directory screen-capture-destination))
(dest (file-truename screen-capture-destination))
(get-dest (lambda (x) (file-truename (car x))))
(dest-filter (lambda (x)
(and (stringp (nth 1 x))
(not (string= dest (funcall get-dest x))))))
(dest-siblings (directory-files-and-attributes default-directory))
(dest-candidates (seq-filter dest-filter dest-siblings))
(candidates-to-alist (lambda (x) (cons (car x) (cadr x))))
(dest-alist (mapcar candidates-to-alist dest-candidates)))
dest-alist)))
(defun screen-capture--options ()
(concatenate 'list
(screen-capture--destination-names)
screen-capture-styles))
(defun screen-capture--set-dest (location)
(let* ((dest screen-capture-destination)
(true-loc
(expand-file-name location
(file-name-directory dest)))
(true-dest
(file-truename
(cdr (assoc location (screen-capture--get-destinations))))))
(make-directory true-dest t)
(make-symbolic-link true-dest true-loc t)
(message "screen-capture--set-dest: %s -> %s"
screen-capture-destination location)
(make-symbolic-link location screen-capture-destination t)))
(defun screen-capture--time-ascending (x y)
(time-less-p (nth 6 x) (nth 6 y)))
(provide 'screen-capture)