From 04e0b9a5d0b400870e6ca39f454e7ed388ffe0af Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Tue, 27 Feb 2024 14:53:14 +0200 Subject: nyxt: Rewrite modules --- .config/nyxt/handle-url.lisp | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 .config/nyxt/handle-url.lisp (limited to '.config/nyxt/handle-url.lisp') diff --git a/.config/nyxt/handle-url.lisp b/.config/nyxt/handle-url.lisp new file mode 100644 index 0000000..1739793 --- /dev/null +++ b/.config/nyxt/handle-url.lisp @@ -0,0 +1,63 @@ +(in-package #:nyxt-user) + +(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))) -- cgit v1.2.3