Explorar el Código

Add eww like command xwwp to search or browse

Provides a DWIM xwwp command that takes a string argument. It will display the corresponding web page if the argument is a URL. If not it will prefix the string with `xwwp-search-prefix` and visit that URL.
Qiantan Hong hace 5 años
padre
commit
06ae5a6ef7
Se han modificado 2 ficheros con 60 adiciones y 2 borrados
  1. 18 2
      README.md
  2. 42 0
      xwwp.el

+ 18 - 2
README.md

@@ -18,6 +18,15 @@ with your backend of choice and I'll have a look at what can be done. Or better,
 fork and create a pull request, most of the needed code is already there, it
 just needs to be hooked.
 
+## DWIM style `M-x xwwp`
+
+DWIM style command adapted from `eww`, smarter than the original `xwidget-webkit-browse-url`.
+Automatically prefixes and expands the url, and use `xwwp-search-prefix` to search for it
+if input doesn't look like an URL or a domain name.
+
+If called with a prefix ARG, create a new Webkit session instead of reusing
+the default Webkit session.
+
 ## Browse url in other window
 
 The `xwidget-webkit-browse-url` just update the current xwidget-webkit buffer
@@ -27,8 +36,7 @@ front using `swith-to-buffer-other-window`.
 
 ### How to install
 
-Sorry, no melpa as of now. Should be added quite soon.
-
+Clone the repo locally and
 ```
 (use-package xwwp-follow-link
   :load-path "~/.emacs.d/xwwp-follow-link"
@@ -38,8 +46,16 @@ Sorry, no melpa as of now. Should be added quite soon.
               ("v" . xwwp-follow-link)))
 ```
 
+Or via Melpa `M-x package-install xwwp`,
+and setup for your completion backend via variable `xwwp-follow-link-completion-system`
+
 ## Development
 
 The goal of this package is to enhance the `xwidget-webkit` browser. If you have
 any code or feature suggestion that you think should make it into this package,
 please open an issue or better, create a pull request!
+
+## Authors
+
+- Damien Merenne <dam@cosinux.org>
+- Q. Hong <qhong@mit.edu>

+ 42 - 0
xwwp.el

@@ -161,6 +161,48 @@ Interactively, URL defaults to the string looking like a url around point."
       (progn (xwidget-webkit-goto-url url)
              (switch-to-buffer-other-window (xwidget-buffer (xwidget-webkit-current-session)))))))
 
+;;; Adapted from EWW code to provide a DWIM style XWWP command
+(require 'eww)
+(require 'puny)
+
+(defcustom xwwp-search-prefix "https://google.com/search?q="
+  "Prefix URL to search engine."
+  :group 'xwwp
+  :type 'string)
+
+(defun xwwp (url &optional arg)
+  "Fetch URL and render the page.
+If the input doesn't look like an URL or a domain name, the
+word(s) will be searched for via `xwwp-search-prefix'.
+
+If called with a prefix ARG, create a new Webkit buffer instead of reusing
+the default Webkit buffer."
+  (interactive
+   (let* ((uris (eww-suggested-uris))
+	      (prompt (concat "Enter URL or keywords"
+			              (if uris (format " (default %s)" (car uris)) "")
+			              ": ")))
+     (list (read-string prompt nil 'eww-prompt-history uris)
+           (prefix-numeric-value current-prefix-arg))))
+  (setq url
+        (let ((eww-search-prefix xwwp-search-prefix))
+          (eww--dwim-expand-url url)))
+  ;; Check whether the domain only uses "Highly Restricted" Unicode
+  ;; IDNA characters.  If not, transform to punycode to indicate that
+  ;; there may be funny business going on.
+  (let ((parsed (url-generic-parse-url url)))
+    (when (url-host parsed)
+      (unless (puny-highly-restrictive-domain-p (url-host parsed))
+        (setf (url-host parsed) (puny-encode-domain (url-host parsed)))))
+    ;; When the URL is on the form "http://a/../../../g", chop off all
+    ;; the leading "/.."s.
+    (when (url-filename parsed)
+      (while (string-match "\\`/[.][.]/" (url-filename parsed))
+        (setf (url-filename parsed) (substring (url-filename parsed) 3))))
+    (setq url (url-recreate-url parsed)))
+  (xwwp-browse-url-other-window url (eq arg 4)))
+
+
 ;; Local Variables:
 ;; eval: (mmm-mode)
 ;; eval: (mmm-add-group 'elisp-js '((elisp-rawjs :submode js-mode