summaryrefslogtreecommitdiff
path: root/.config/nyxt/commands.lisp
blob: e89bea08893c9a3c529436e7b76b47a6795dad1b (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
(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")
   '("aw" "https://wiki.archlinux.org/index.php?search=~a")
   '("duck" "https://duckduckgo.com/?q=~a"))
  "List of search engines.
Last entry is the default.")

(defun direct-url-p (url)
  "Checks if `url` is a direct URL with specific extensions, returning
t if so and nil otherwise. The URL must not contain spaces."
  (not (null (or (and (null (cl-ppcre:scan "\\s" url))
                      (or (cl-ppcre:scan "^(https?://)?.*\\.org(\\/.*)?$" url)
                          (cl-ppcre:scan "^(https?://)?.*\\.com(\\/.*)?$" url)
                          (cl-ppcre:scan "^(https?://)?.*\\.net(\\/.*)?$" url)
                          (cl-ppcre:scan "^(https?://)?.*\\.gr(\\/.*)?$" url)
			  (cl-ppcre:scan "^(https?://)?.*\\.co(\\/.*)?$" url)
                          (cl-ppcre:scan "^(https?://)?.*\\.engineer(\\/.*)?$" url)))))))

(defun handle-url (query func)
  "Process `query` as a URL or search query.

Applies `func` to the query`.
Requires the variable `*thanos/search-engines*` for search queries."
  (cond
   ((or (str:starts-with-p "localhost" query))
    (funcall func (str:concat "http://" query)))
   ((direct-url-p query)
    (funcall func (str:concat "https://" query)))
   ((str:starts-with-p "http" query)
    (funcall func query))
   ((let ((split-result (str:split #\Space query)))
      (find (first split-result) *thanos/search-engines* :key #'first :test #'string=))
    (let* ((engine-tag (subseq query 0 (position #\Space query)))
           (query (subseq query (+ 1 (position #\Space query))))
           (engine (find engine-tag *thanos/search-engines* :key #'first :test #'string=))
           (search-url (second engine)))
      (funcall func (cl-ppcre:regex-replace "~a" search-url (quri:url-encode query)))))
   (t
    (let* ((engine (first (last *thanos/search-engines*)))
           (search-url (second engine)))
      (funcall func (cl-ppcre:regex-replace "~a" search-url (quri:url-encode query)))))))

(defun thanos/make-buffer-focus-url (url)
  (make-buffer-focus :url url))

(define-command-global thanos/set-url ()
  (let* ((prompt-result (first
                         (prompt
                          :prompt "Search"
                          :sources (list (make-instance 'prompter:raw-source))))))
    (handle-url prompt-result 'buffer-load)))

(define-command-global thanos/set-url-new-buffer ()
  (let* ((prompt-result (first
                         (prompt
                          :prompt "Search [New Buffer]"
                          :sources (list (make-instance 'prompter:raw-source))))))
    (handle-url prompt-result 'thanos/make-buffer-focus-url)))