Skip to content

Commit

Permalink
buffer: Fix SET-URL
Browse files Browse the repository at this point in the history
Setting URL to something like "github.com/atlas-engineer" now opens
the desired web page instead of performing a search with the default
search engine.
  • Loading branch information
shamazmazum committed Apr 15, 2024
1 parent bb3aaca commit 7644c4a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
49 changes: 26 additions & 23 deletions source/buffer.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -1398,33 +1398,36 @@ Loads the entry with default `prompter:actions-on-return'."))
(:documentation "Structure that processes a new URL query from user input.
Checks whether a valid https or local file URL is requested, in a DWIM fashion."))

;; TODO: CHECK-DNS-P is not needed anywhere in this file and may be
;; removed in the future.
(defmethod initialize-instance :after ((query new-url-query)
&key check-dns-p &allow-other-keys)
(declare (ignore check-dns-p))
;; Trim whitespace, in particular to detect URL properly.
(setf (query query) (str:trim (query query)))
(cond
((engine query)
;; First check engine: if set, no need to change anything.
nil)
((valid-url-p (query query)
:check-dns-p nil)
;; Valid URLs should be passed forward.
nil)
((and check-dns-p
(valid-tld-p (query query)))
(setf (query query) (str:concat "https://" (query query))))
;; Rest is for invalid URLs:
((uiop:file-exists-p (query query))
(setf (query query)
(str:concat
"file://"
(uiop:native-namestring
(uiop:ensure-absolute-pathname
(query query) *default-pathname-defaults*)))))
(t
(setf (engine query)
(or (engine query)
(default-search-engine))))))
(let ((https+query (str:concat "https://" (query query))))
(cond
((engine query)
;; First check engine: if set, no need to change anything.
nil)
((valid-url-p (query query) :check-dns-p nil)
;; Valid URLs should be passed forward.
nil)
;; Rest is for invalid URLs:
((and (find #\. (query query) :test #'char=)
(valid-url-p https+query :check-dns-p nil))
(setf (query query) https+query))
((uiop:file-exists-p (query query))
(setf (query query)
(str:concat
"file://"
(uiop:native-namestring
(uiop:ensure-absolute-pathname
(query query) *default-pathname-defaults*)))))
(t
(setf (engine query)
(or (engine query)
(default-search-engine)))))))

(defun encode-url-char (c)
(if (find c '("+" "&" "%") :test #'string=)
Expand Down
6 changes: 5 additions & 1 deletion tests/offline/urls.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@
;; "valid syntax but unknown scheme"
(assert-equality #'quri:uri=
(quri:uri "https://search.atlas.engineer/searxng/search?q=foo:blank")
(url (first (nyxt::input->queries "foo:blank")))))
(url (first (nyxt::input->queries "foo:blank"))))
;; "looks like an URL without scheme"
(assert-equality #'quri:uri=
(quri:uri "https://github.com/atlas-engineer")
(url (first (nyxt::input->queries "github.com/atlas-engineer")))))

(define-test nyxt-urls ()
(assert-error 'simple-error
Expand Down

0 comments on commit 7644c4a

Please sign in to comment.