summaryrefslogtreecommitdiff
path: root/.emacs.d/modules/thanos-commands.el
blob: 83691515cc94cd7b6418a1565b7112a8fb907426 (about) (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
;;; thanos-commands.el --- custom commands/functions  -*- lexical-binding: t; -*-

;; Copyright (C) 2023  Thanos Apollo

;; Author: Thanos Apollo <[email protected]>
;; Keywords: 

;; 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 <https://www.gnu.org/licenses/>.

;;; Commentary:
;; ╭━━━━┳╮╱╱╱╱╱╱╱╱╱╱╱╱╱╱╭━━━╮╱╱╱╱╱╭╮╭╮╱╱╱╱╱╱╱╱╭━━━╮
;; ┃╭╮╭╮┃┃╱╱╱╱╱╱╱╱╱╱╱╱╱╱┃╭━╮┃╱╱╱╱╱┃┃┃┃╱╱╱╱╱╱╱╱┃╭━━╯
;; ╰╯┃┃╰┫╰━┳━━┳━╮╭━━┳━━╮┃┃╱┃┣━━┳━━┫┃┃┃╭━━╮╱╱╱╱┃╰━━┳╮╭┳━━┳━━┳━━╮
;; ╱╱┃┃╱┃╭╮┃╭╮┃╭╮┫╭╮┃━━┫┃╰━╯┃╭╮┃╭╮┃┃┃┃┃╭╮┃╭━━╮┃╭━━┫╰╯┃╭╮┃╭━┫━━┫
;; ╱╱┃┃╱┃┃┃┃╭╮┃┃┃┃╰╯┣━━┃┃╭━╮┃╰╯┃╰╯┃╰┫╰┫╰╯┃╰━━╯┃╰━━┫┃┃┃╭╮┃╰━╋━━┃
;; ╱╱╰╯╱╰╯╰┻╯╰┻╯╰┻━━┻━━╯╰╯╱╰┫╭━┻━━┻━┻━┻━━╯╱╱╱╱╰━━━┻┻┻┻╯╰┻━━┻━━╯
;; ╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱┃┃
;; ╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╰╯
;;; Code:

;; VM
(defvar vm-directory "~/Virtual/")

(defun vm-create-image ()
  "Create qcow2 image."
  (interactive)
  (let ((name (format "%s%s.qcow2" vm-directory (read-string "Name: ")))
	(size (format "%s" (read-string "Size(G): "))))
    (shell-command
     (format "qemu-img create -f qcow2 %s %sG" name size))))


(defun vm-run ()
  "Spawn Virtual Machine."
  (interactive)
  (let ((memory (format "%sG" (read-string "Memory(G): ")))
	(cores (read-string "Cores: "))
	(image (read-file-name "Image: " vm-directory))
	(iso (if (y-or-n-p "Load iso? ")
		 (read-file-name "ISO: ")
	       nil)))
    (call-process-shell-command
     (format "qemu-system-x86_64 -enable-kvm -m %s -smp %s -hda %s -vga virtio -device virtio-serial-pci %s"
	     memory cores image (when iso (concat "-cdrom " iso))))))

;; MISC

(defun thanos/run-in-background (command)
  "Run COMMAND in the background."
  (let ((command-parts (split-string command "[ ]+")))
    (apply #'call-process `(,(car command-parts) nil 0 nil ,@(cdr command-parts)))))

(defun thanos/emacs-keys ()
  "Swap caps with ctrl."
  (interactive)
  (start-process-shell-command
   "setxkbmap" nil "setxkbmap us -option ctrl:swapcaps"))

(defun thanos/center-buffer ()
  (interactive)
  (set-fringe-mode
   (/ (- (frame-pixel-width)
	 (* 150 (frame-char-width)))
      2)))

(defun thanos/git-clone (repository)
  "Clone git REPOSITORY."
  (interactive (list (read-string "repo: ")))
  (call-process-shell-command
   (concat "git clone " (shell-quote-argument repository)) nil 0))

;; CREATE

(defun create-text-scratch ()
  "Create a scratch buffer."
  (interactive)
  (switch-to-buffer (get-buffer-create "*Text Scratch*"))
  (org-mode))

(defun create-scratch ()
  "Create scratch buffer."
  (interactive)
  (switch-to-buffer (get-buffer-create "*scratch*"))
  (emacs-lisp-mode))

(defvar-keymap thanos/create-map
  :doc "Create custom buffers"
  "t" #'create-text-scratch
  "e" #'create-scratch)

;; THEMING
(defvar wallpapers-dir "~/wallpapers/")

(defun thanos/load-theme ()
  "Disable current theme and load a new one."
  (interactive)
  (let ((theme (intern (completing-read "Theme: " (custom-available-themes)))))
    (disable-theme (car custom-enabled-themes))
    (load-theme theme t)))

(defun thanos/wallpaper-set (image)
  "Set IMAGE as wallpaper, using feh."
  (call-process-shell-command
   (concat "feh --bg-scale " wallpapers-dir image) nil 0))

(defun thanos/wallpaper-random ()
  "Set random wallpaper."
  (interactive)
  (let ((wallpapers (directory-files "~/wallpapers" nil "^[^.].*")))
    (thanos/wallpaper-set (nth (random (length wallpapers)) wallpapers))))

(defun thanos/wallpaper-select ()
  "Set wallpaper."
  (interactive)
  (let ((wallpaper (completing-read "Choose wallpaper: " (directory-files wallpapers-dir nil "^[^.].*"))))
    (thanos/wallpaper-set wallpaper)))

(defvar-keymap thanos/applications-map
  :doc "Thanos commonly used programs"
  "t" #'thanos/load-theme
  "w" #'thanos/wallpaper-select
  "C-c" thanos/create-map)

(define-key global-map (kbd "C-c a") thanos/applications-map)


(defun dired-watch-video ()
  "Watch play file with mpv."
  (interactive)
  (call-process-shell-command
   (format "mpv \"%s\"" (dired-get-filename)) nil 0))

(defun dired-set-wallpaper ()
  "Set NAME as wallpaper using feh."
  (interactive)
  (call-process-shell-command
   (format "feh --bg-scale %s" (dired-get-filename)) nil 0))

(defun dired-delete-files-except ()
  "Delete all files inside directory except match."
  (interactive)
  (let* ((directory (read-directory-name "Select directory: "))
	 (files (directory-files directory t))
	 (except-match (read-string "Except the ones that have: ")))
    (dolist (file files)
      (unless (or (string= "." (substring file -1))
		  (string= ".." (substring file -2))
		  (string-match except-match file))
	(dired-delete-file file t)))))

(defun dired-delete-file-match ()
  "Delete all files inside directory except match."
  (interactive)
  (let* ((directory (read-directory-name "Select directory: "))
	 (files (directory-files directory t))
	 (match (read-string "Delete files that match: ")))
    (dolist (file files)
      (when (string-match-p match file)
	(dired-delete-file file t)))))

(defun dired-rename-capitalize-file ()
  "Capitalize the base name of the file at point in a Dired buffer."
  (interactive)
  (let* ((file (dired-get-file-for-visit))
	 (new-file (capitalize (file-name-nondirectory file))))
    (if (string-prefix-p "." file)
	(message "Skipping file starting with '.'")
      (progn
	(rename-file file (concat (file-name-directory file) new-file))
	(revert-buffer)
	(message "Renamed %s to %s" file new-file)))))

(defun thanos/0x0-upload-file ()
  "Update file to 0x0.st"
  (interactive)
  (async-shell-command (format "curl -F'file=@%s' https://0x0.st" (dired-get-filename))))

;; yeetube
(defun yeetube-download-videos-ffmpeg ()
    "Download videos using ffmpeg."
    (interactive)
    (let ((url "")
	  (name "")
	  (download-counter 1)
	  (stored-contents nil))
      ;; Read links and names until "q" is entered
      (while (not (string= url "q"))
	(setf url (read-string "Enter URL (q to quit): "))
	(unless (string= url "q")
	  (setf name (read-string (format "Custom name (download counter: %d) " download-counter)))
	  (push (cons url name) stored-contents)
	  (setf download-counter (1+ download-counter))))
      ;; Process the collected links and names
      (dolist (pair stored-contents)
	(let ((url (car pair))
	      (name (cdr pair)))
	  (async-shell-command
	   (format
	    "ffmpeg -protocol_whitelist file,crypto,data,https,tls,tcp -stats -i '%s' -codec copy '%s.mp4'"
	    url name))))))

(defun yeetube-download-vimeo-videos ()
    "Download videos from vimeo services."
    (interactive)
    (let ((url "")
	  (name "")
	  (download-counter 1))
      (while (not (string= url "q"))
	(setf url (read-string "Enter URL (q to quit): "))
	(unless (string= url "q")
	  (setf name (read-string (format "Custom name (download counter: %d) " download-counter)))
	  (setf download-counter (1+ download-counter))
	  (call-process-shell-command
	   (format
	    "yt-dlp '%s' -o '%s'"
	    (replace-regexp-in-string "\\.json" ".m3u8" url) name)
	   nil 0)))))

;; Input methods
(defvar thanos/input-methods
  '("greek" "bulgarian-phonetic" "nil")
  "List of input methods to toggle.")

(defvar thanos/input-methods-index 0
  "Index of the next input method in `thanos/input-methods' to use.")

(defun thanos/toggle-input-method ()
  "Switch to the next input method in `thanos/input-methods'."
  (interactive)
  (setf thanos/input-methods-index
        (mod (1+ thanos/input-methods-index) (length thanos/input-methods)))
  (let ((input-method (nth thanos/input-methods-index thanos/input-methods)))
    (set-input-method (if (string= "nil" input-method) nil input-method))))

(defun thanos/toggle-input-method ()
  "Switch to the next input method in `thanos/input-methods'."
  (interactive)
  (setf thanos/input-methods-index
        (mod (1+ thanos/input-methods-index) (length thanos/input-methods)))
  (let ((input-method (nth thanos/input-methods-index thanos/input-methods)))
    (set-input-method (if (string= "nil" input-method) nil input-method))))

(defun erc-libera ()
  "Login to liberachat with erc."
  (interactive)
  (erc-tls :server "irc.libera.chat" :port 6697
	   :nick "thanosapollo"
	   :user "thanosapollo"
	   :password (password-store-get "liberachat/thanos_apollo")))

(provide 'thanos-commands)
;;; thanos-commands.el ends here