summaryrefslogtreecommitdiffhomepage
path: root/attic/misc.el
blob: 7fb45c92208d318cfed2e8a491175239e816bc94 (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
;;; ace window
(use-package ace-window
  :ensure t
  :demand t
  :init (setq aw-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l)
              aw-char-position 'top-left
              aw-ignore-current nil
              aw-dispatch-when-more-than 2
              aw-leading-char-style 'path
              aw-display-mode-overlay t
              aw-scope 'frame)
  :config

  (defun jao-ace-consult-buffer-other-window (w)
    (interactive)
    (aw-switch-to-window w)
    (consult-buffer))

  (setf (alist-get ?b aw-dispatch-alist)
        '(jao-ace-consult-buffer-other-window "Consult buffer"))

  (setf (alist-get ?B aw-dispatch-alist)
        (alist-get ?u aw-dispatch-alist))


  :bind (("M-o" . ace-window)
         ("M-O" . ace-swap-window)
         ("C-x 4 t" . ace-swap-window)))

;;; sway
(defun jao-swaymsg (msg)
  (shell-command (format "swaymsg '%s' >/dev/null" msg)))

(defmacro jao-def-swaymsg (name msg)
  `(defun ,(intern (format "jao-sway-%s" name)) ()
     (interactive)
     (jao-swaymsg ,msg)))
(jao-def-swaymsg firefox "[app_id=firefox] focus")

(defvar jao-sway-enabled
  (and (eq window-system 'pgtk) (not jao-xmonad-enabled)))

(defun jao-sway-set-wallpaper (f)
  (jao-swaymsg (format "output * bg %s fill" f))
  (make-symbolic-link f "~/.wallpaper.sway" t))

(defun jao-sway-run-or-focus (cmd &optional ws)
  (if (jao-shell-running-p "firefox")
      (jao-swaymsg (format "[app_id=%s] focus" cmd))
    (jao-swaymsg (format "workspace %s" (or ws 2)))
    (start-process-shell-command cmd nil cmd)))

(defun jao-sway-run-or-focus-tidal ()
  (interactive)
  (if (jao-shell-running-p "tidal-hifi")
      (jao-swaymsg "[app_id=tidal-hifi] scratchpad show")
    (start-process-shell-command "tidal-hifi" nil "tidal-hifi &")
    (jao-sway-run-or-focus-tidal)))

(defun jao-sway-run-or-focus-firefox ()
  (interactive)
  (jao-sway-run-or-focus "firefox"))

(defun jao-sway-enable ()
  (setq jao-browse-doc-use-emacs-p t)
  (setq jao-wallpaper-random-wake nil)
  (jao-trisect)
  (jao-set-transparency 85)
  (jao-themes-setup)
  ;; (display-time-mode 1)
  (global-set-key (kbd "s-f") #'jao-sway-run-or-focus-firefox)
  (defalias 'jao-streaming-list #'jao-sway-run-or-focus-tidal)
  (message "Welcome to sway"))

(when jao-sway-enabled
  (defalias 'x-change-window-property #'ignore)
  (add-hook 'after-init-hook #'jao-sway-enable))

;;; time display
(setq display-time-world-list
      '(("Europe/Paris" "Barcelona")
        ("America/Los_Angeles" "Los Angeles")
        ("America/New_York" "New York")
        ("Europe/London" "London")
        ("Asia/Calcutta" "Bangalore")
        ("Asia/Tokyo" "Tokyo")))

(defun jao-time--pdt-hour ()
  (jao-time-at-zone "%H" "America/Los_Angeles"))

(defun jao-time--chicago-hour ()
  (jao-time-at-zone "%H" "America/Chicago"))

(defun jao-time-at-zone (format zone)
  (set-time-zone-rule zone)
  (prog1 (format-time-string format)
    (set-time-zone-rule nil)))

(defun jao-time-echo-la-time ()
  (interactive)
  (message (jao-time-at-zone "LA %H:%M" "America/Los_Angeles")))

(defun jao-time-echo-times ()
  (interactive)
  (let ((msg (format "%s (%s)"
                     (format-time-string "%a, %e %B - %H:%M")
                     (jao-time-at-zone "%H:%M" "America/Los_Angeles"))))
    (jao-notify msg "" (jao-data-file "clock-world-icon.png"))))

(defun jao-time-to-epoch (&optional s)
  "Transform a time string to an epoch integer in milliseconds."
  (interactive)
  (let ((s (or s (read-string "Time string: " (thing-at-point 'string)))))
    (message "%s = %s"
             s
             (round (* 1000 (time-to-seconds (parse-time-string s)))))))

(defun jao-epoch-to-time (&optional v)
  "Transform an epoch, given in milliseconds, to a time string."
  (interactive)
  (let ((v (or v (read-number "Milliseconds: " (thing-at-point 'number)))))
    (message "%s = %s" v
             (format-time-string "%Y-%m-%d %H:%M:%S"
                                 (seconds-to-time (/ v 1000.0))))))
;;; twtxt
(use-package twtxt
  :ensure t
  :init (setq twtxt-file (expand-file-name "~/doc/jao.io/twtxt")
              twtxt-following
              '(("yarn" "https://twtxt.net/user/news/twtxt.txt"))))
;;; corfu bits
(defun jao-corfu-enable-no-auto ()
  (setq-local corfu-auto nil)
  (corfu-mode 1))

(defmacro jao-corfu-no-auto (mode)
  (let ((mode-name (intern (format "%s-mode" mode)))
        (hook-name (intern (format "%s-mode-hook" mode))))
    `(with-eval-after-load ',mode
       (add-to-list 'corfu-excluded-modes ',mode-name)
       (add-hook ',hook-name #'jao-corfu-enable-no-auto))))

(jao-corfu-no-auto eshell)

;;; gnus bits

(jao-transient-major-mode gnus-group
  ["Search"
   ("zc" "consult search" consult-notmuch)
   ("zf" "consult folder search" jao-consult-notmuch-folder)
   ("g" "gnus search" gnus-group-read-ephemeral-search-group)])

(defun jao-gnus-restart-servers ()
  (interactive)
  (message "Restarting all servers...")
  (gnus-group-enter-server-mode)
  (gnus-server-close-all-servers)
  (gnus-server-open-all-servers)
  (gnus-server-exit)
  (message "Restarting all servers... done"))

;;;; delayed expiry
(defvar jao-gnus--expire-every 50)
(defvar jao-gnus--get-count (1+ jao-gnus--expire-every))

(defun jao-gnus-get-new-news (&optional arg)
  (interactive "p")
  (when (and jao-gnus--expire-every
             (> jao-gnus--get-count jao-gnus--expire-every))
    (when jao-gnus-use-pm-imap (gnus-group-catchup "nnimap:pm/spam" t))
    (gnus-group-expire-all-groups)
    (setq jao-gnus--get-count 0))
  (setq jao-gnus--get-count (1+ jao-gnus--get-count))
  (gnus-group-get-new-news (max (if (= 1 jao-gnus--get-count) 4 3)
                                (or arg 0))))

(define-key gnus-group-mode-map "g" 'jao-gnus-get-new-news)
(define-key gnus-group-mode-map "\C-x\C-s" #'gnus-group-save-newsrc)

(defun jao-gnus--first-group ()
  (when (derived-mode-p 'gnus-group-mode)
    (gnus-group-first-unread-group)))

(with-eval-after-load "jao-afio"
  (add-hook 'jao-afio-switch-hook #'jao-gnus--first-group))

;;;; remove HTML from From contents (arxiv with r2e)
(require 'shr)
(defvar jao-gnus--from-rx
  (concat "From: \\\"?\\(  " jao-gnus--news-rx "\\)"))

(defun jao-gnus-remove-anchors ()
  (save-excursion
    (goto-char (point-min))
    (cond ((re-search-forward jao-gnus--from-rx nil t)
           (replace-match "" nil nil nil 1))
          ((re-search-forward "[gq].+ updates on arXiv.org: " nil t)
           (replace-match "")
           (let ((begin (point)))
             (when (re-search-forward "^\\(To\\|Subject\\):" nil t)
               (beginning-of-line)
               (let ((shr-width 10000))
                 (shr-render-region begin (1- (point))))))))))

(add-hook 'gnus-part-display-hook 'jao-gnus-remove-anchors)