Leo Ufimtsev
Leo Ufimtsev

Reputation: 6512

Emacs and eww, open links in new window?

I configured Emacs to open new links in eww like so:

 (setq browse-url-browser-function 'eww-browse-url)

Now when I click on a link, it opens in the same buffer.

I would like it to open a new window (i.e split vertically like C - x 3) and open the page in the newly created frame on the right. So that I still have the original org-mode notes on the left.

[Edit]

I hacked together something. But it only works when I activate the hotkey, not when another function opens a link.

Ideally, I want something like the below, but for whenever I open a link (e.g in helm-google).

(defun my/open-in-right-window ()
  "Open the selected link on the right window plane"
  (interactive)
  (delete-other-windows nil)
  (split-window-right nil)
  (other-window 1)
  (org-return nil)
)

(defun my/eww-quitAndSingleWin ()
  "Quit the current browser session and activate single window mode."
  (interactive)
  (quit-window nil)
  (delete-other-windows nil)
)

(defun my/eww-split-right ()
  "Splits the Window. Moves eww to the right and underlying content on the left."
  (interactive)
  (split-window-right nil)
  (quit-window nil)
  (other-window 1)
)

(global-set-key (kbd "H-r") 'my/open-in-right-window)    

(add-hook 'eww-mode-hook   ;no impact.
      (lambda ()
         (local-set-key (kbd "s") 'my/eww-split-right)
         (local-set-key (kbd "Q") 'my/eww-quitAndSingleWin)
   ))

It kills other windows, opens new window, switches to new window, then presses return [which is configured to open links in my config].
Further then in eww-mode a 'Q' (capital) quits the session and kills the other window so as to avoid too many open windows.

It's not the most elegant solution. I'm open to better ideas?

Upvotes: 2

Views: 4045

Answers (2)

Jostein Kjønigsen
Jostein Kjønigsen

Reputation: 321

While russel's answer may have been correct in the past, eww-current-title and eww-current-url has been obsoleted in favour of a buffer-local plist called eww-data.

Current eww-implementation also includes the hooks we need to plug in after a buffer is rendered, thus avoiding the need to do "messy" things like defadvice.

As per August 2015 and this Git-commit, the following elisp works for me:

(defun my-set-eww-buffer-title ()
  (let* ((title  (plist-get eww-data :title))
         (url    (plist-get eww-data :url))
         (result (concat "*eww-" (or title
                              (if (string-match "://" url)
                                  (substring url (match-beginning 0))
                                url)) "*")))
    (rename-buffer result t)))

(add-hook 'eww-after-render-hook 'my-set-eww-buffer-title)

You can probably use this hook to add your desired keybindings too.

Upvotes: 2

russell
russell

Reputation: 708

I had the similar issue of wanting to have multiple eww buffers open and did it by advising eww-render. You could probably put your code in there to make it always run.

(defadvice eww-render (after set-eww-buffer-name activate)
  (rename-buffer (concat "*eww-" (or eww-current-title
                                     (if (string-match "://" eww-current-url)
                                         (substring eww-current-url (match-beginning 0))
                                       eww-current-url)) "*") t))

Upvotes: 3

Related Questions