~azzamsa/scripts.el

ref: fe7fd65808a145e21979f2dfcbb4332cbdeb0d8f scripts.el/aza-scripts.el -rw-r--r-- 6.4 KiB
fe7fd658 — azzamsa remove `get-pass` 5 months ago
                                                                                
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
(require 'cl-lib)
(require 's)
(require 'f)
(require 'ts)
(require 'request)
(require 'aza-secrets)

(defun save-all-buffers-silently ()
  (save-some-buffers t))

;;;###autoload
(defun aza-kill-other-buffers ()
  "Kill all buffers but current buffer and special buffers.
(Buffer that start with '*' and white space ignored)"
  (interactive)
  (when (y-or-n-p "Save and kill all other buffers ? ")
    (save-all-buffers-silently)
    (let ((killed-bufs 0))
      (dolist (buffer (delq (current-buffer) (buffer-list)))
        (let ((name (buffer-name buffer)))
          (when (and name (not (string-equal name ""))
                     (/= (aref name 0) ?\s)
                     (string-match "^[^\*]" name))
            (cl-incf killed-bufs)
            (funcall 'kill-buffer buffer))))
      (message "Saved & killed %d buffer(s)" killed-bufs))))

(defun aza-today (&optional arg)
  "Insert today's date.

A prefix ARG specifies how many days to move;
negative means previous day.

If region selected, parse region as today's date pivot."
  (interactive "P")
  (let ((date (if (use-region-p)
                  (ts-parse (buffer-substring-no-properties (region-beginning) (region-end)))
                (ts-now)))
        (arg (or arg 0)))
    (if (use-region-p)
        (delete-region (region-beginning) (region-end)))
    (insert (ts-format "%A, %B %e, %Y" (ts-adjust 'day arg date)))))

(defun insert-filename-as-heading ()
  "Take current filename (word separated by dash) as heading."
  (interactive)
  (insert
   (capitalize
    (s-join " " (s-split-words (file-name-sans-extension (buffer-name)))))))

(defun now ()
  (interactive)
  (insert (format-time-string "%F %H:%M")))

(defun hour ()
  (interactive)
  (insert (format-time-string "%H:%M")))

(defun rm-mysecrets ()
  "Remove all confidential information."
  (interactive)
  (dolist (pair (list-my-secrets))
    (save-excursion
      (replace-string (car pair) (cdr pair)))))

(defun light-set-value ()
  "Set light value directly inside Emacs"
  (interactive)
  (let* ((current-value (s-trim (shell-command-to-string "light -G")))
         (light-value (read-string "Set Value: " current-value)))
    (start-process "" nil "light" "-S" light-value)))

(defun pyqt-compile-ui ()
  "Compile Qt5 user interfaces to Python code directly from
Emacs"
  (interactive)
  (let ((inputfile (dired-get-filename))
        (outputfile
         (file-name-sans-extension
          (file-name-nondirectory (dired-get-filename)))))
    (start-process "" nil "pyuic5" inputfile
                   (concat "--output=" default-directory outputfile "_ui.py"))))

(defun mpv-here ()
  "Play all multimedia files in current directory"
  (interactive)
  (start-process "" nil "mpv" "--force-window" "--loop" default-directory))

(defun trash-empty ()
  "Empty trash using trash-cli. It's safer and painless"
  (interactive)
  (start-process "" nil "rm" "-rf" trash-dir1 trash-dir2 trash-dir3))

(defun anacron-show-last-backup ()
  "Show last backup of my anacron status."
  (interactive)
  (let ((contents (f-read "~/cron-report/anacron-status.txt" 'utf-8)))
    (message (car (last (delete "" (split-string contents "\n")))))))

(defun what-day ()
  "Show day name from spesific time"
  (interactive)
  (let ((date (read-string "Date: "))
        (month (read-string "Month: "))
        (year (read-string "Year: " (number-to-string (ts-year (ts-now))))))
    (message (ts-day-name (ts-parse (s-join " " (list date month year)))))))

(defun ask-github ()
  "GET Github notification API."
  (let* ((github-pass (password-store-get-field "code/github" "token"))
         (archive-response (request "https://api.github.com/notifications?all"
                             :parser 'json-read
                             :headers `(("Authorization" . ,(concat "token" " " github-pass))
                                        ("Content-Type" . "application/json"))
                             :sync t))
         (data (request-response-data archive-response))
         (status (request-response-status-code archive-response)))
    (if (eq status 200)
        data
      404)))

(defun github-show-notification ()
  "Check if Github notification exist without opening browser
Reduce Distraction."
  (interactive)
  (let ((result (ask-github)))
    (if (not (equal result 404))
        (if (equal result '[])
            (message "No notification.")
          (message "Yey, You have notification!"))
      (message "Request failed"))))

(defun ask-archive (url)
  "Get request to Wayback API"
  (let* ((archive-response (request (concat "http://archive.org/wayback/available?url=" url)
                             :parser 'json-read
                             :sync t))
         (data (request-response-data archive-response))
         (status (request-response-status-code archive-response)))
    (if (eq status 200)
        data
      404)))

(defun wayback-save-page ()
  "Archive page to Wayback"
  (interactive)
  (let* ((url (read-string "Url: "))
         (response (nth 1 (nth 1 (ask-archive url)))))
    (if response
        (message "Webpage had an archive")
      (message "Webpage archived"))))

(defun play-reminder-sound ()
  (start-process "" nil "mpv" coin-work-medium-sound))

(defun remind-me ()
  "Notify with a sound after certain time"
  (interactive)
  (let ((time (read-string "Time (min|sec): " "10 min")))
    (message "I will remind you after %s" time)
    (run-at-time time nil #'play-reminder-sound)))

(defun to-snake-case (start end)
  "Change selected text to snake case format"
  (interactive "r")
  (if (use-region-p)
      (let ((camel-case-str (buffer-substring start end)))
        (delete-region start end)
        (insert (s-snake-case camel-case-str)))
    (message "No region selected")))

(defun connect-remote ()
  "Open dired buffer in selected remote machine"
  (interactive)
  (let* ((remote-source `((name . "")
                          (candidates . ,(mapcar 'car remote-machines))
                          (action . (lambda (candidate)
                                      candidate))))
         (selected-machine (helm :sources '(remote-source)))
         (machine-data (cdr (assoc selected-machine remote-machines)))
         (username (plist-get machine-data :username))
         (ip-address (plist-get machine-data :ip)))
    (if (string= username "root")
        (dired (concat "/ssh:" username "@" ip-address ":/"))
      (dired (concat "/ssh:" username "@" ip-address ":/home/" username "/")))
    (message "Connected")))

(provide 'aza-scripts)