diff options
author | Thanos Apollo <[email protected]> | 2024-02-02 10:17:39 +0200 |
---|---|---|
committer | Thanos Apollo <[email protected]> | 2024-02-02 10:18:26 +0200 |
commit | cb1988923e03876f37f3007ce8d4693b73a018fd (patch) | |
tree | 3dc938d0b79dee9ac7527d8b6f81a9e369ad164a /.config/nyxt | |
parent | 10c184e03f66f8c87687a818955ddecb412088dc (diff) |
nyxt:Refactor search commands
Diffstat (limited to '.config/nyxt')
-rw-r--r-- | .config/nyxt/commands.lisp | 92 |
1 files changed, 44 insertions, 48 deletions
diff --git a/.config/nyxt/commands.lisp b/.config/nyxt/commands.lisp index 4c762f8..e89bea0 100644 --- a/.config/nyxt/commands.lisp +++ b/.config/nyxt/commands.lisp @@ -3,63 +3,59 @@ '("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.") + "List of search engines. +Last entry is the default.") -(define-command-global thanos/set-url () - "Lean & Mean version of thanos/set-url +(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)) -Default set-url does way too much for my liking and occasionally -hangs, this is just a minimal alternative" +(define-command-global thanos/set-url () (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)))))))) - + (handle-url prompt-result 'buffer-load))) (define-command-global thanos/set-url-new-buffer () - "Lean & Mean version of thanos/set-url-new-buffer" (let* ((prompt-result (first (prompt - :prompt "Search" + :prompt "Search [New Buffer]" :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)))))))) + (handle-url prompt-result 'thanos/make-buffer-focus-url))) |