summaryrefslogtreecommitdiff
path: root/.config/nyxt/commands.lisp
blob: 4c762f8cc9ae761f4ddcf220c617c7a05142e6b2 (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
(defvar *thanos/search-engines*
  (list
   '("google" "https://google.com/search?q=~a")
   '("aa" "https://annas-archive.org/search?q=~a")
   '("v" "https://yewtu.be/search?q=~a")
   '("duck" "https://duckduckgo.com/?q=~a"))
  "List of search engines.")

(define-command-global thanos/set-url ()
  "Lean & Mean version of thanos/set-url

Default set-url does way too much for my liking and occasionally
hangs, this is just a minimal alternative"
  (let* ((prompt-result (first
                         (prompt
                          :prompt "Search"
                          :sources (list (make-instance 'prompter:raw-source))))))
    (cond 
      ((and (or (str:ends-with-p ".org" prompt-result)
                (str:ends-with-p ".com" prompt-result)
                (str:ends-with-p ".net" prompt-result)
                (str:ends-with-p ".engineer" prompt-result))
            (not (find #\Space prompt-result)))
       (buffer-load (str:concat "https://" prompt-result)))
      ((str:starts-with-p "localhost" prompt-result)
       (buffer-load (str:concat "http://" prompt-result)))
      ((let ((split-result (str:split #\Space prompt-result)))
         (find (first split-result) *thanos/search-engines* :key #'first :test #'string=))
       (let* ((engine-tag (subseq prompt-result 0 (position #\Space prompt-result)))
              (query (subseq prompt-result (+ 1 (position #\Space prompt-result))))
              (engine (find engine-tag *thanos/search-engines* :key #'first :test #'string=))
              (search-url (second engine)))
         (buffer-load (cl-ppcre:regex-replace "~a" search-url (quri:url-encode query)))))
     (t
      (let* ((engine (first (last *thanos/search-engines*)))
             (search-url (second engine)))
        (buffer-load (cl-ppcre:regex-replace "~a" search-url (quri:url-encode prompt-result))))))))


(define-command-global thanos/set-url-new-buffer ()
  "Lean & Mean version of thanos/set-url-new-buffer"
  (let* ((prompt-result (first
                         (prompt
                          :prompt "Search"
                          :sources (list (make-instance 'prompter:raw-source))))))
    (cond
      ((and (or (str:ends-with-p ".org" prompt-result)
                (str:ends-with-p ".com" prompt-result)
                (str:ends-with-p ".net" prompt-result)
                (str:ends-with-p ".engineer" prompt-result))
            (not (find #\Space prompt-result)))
       (make-buffer-focus :url (str:concat "https://" prompt-result)))
      ((str:starts-with-p "localhost" prompt-result)
       (make-buffer-focus :url (str:concat "http://" prompt-result)))
      ((let ((split-result (str:split #\Space prompt-result)))
         (find (first split-result) *thanos/search-engines* :key #'first :test #'string=))
       (let* ((engine-tag (subseq prompt-result 0 (position #\Space prompt-result)))
              (query (subseq prompt-result (+ 1 (position #\Space prompt-result))))
              (engine (find engine-tag *thanos/search-engines* :key #'first :test #'string=))
              (search-url (second engine)))
         (make-buffer-focus :url (cl-ppcre:regex-replace "~a" search-url (quri:url-encode query)))))
      (t
       (let* ((engine (first (last *thanos/search-engines*)))
              (search-url (second engine)))
         (make-buffer-focus :url (cl-ppcre:regex-replace "~a" search-url (quri:url-encode prompt-result))))))))