Browse Source

Rename project: exwm-browser -> exwm-wb

Peter J. Jones 1 year ago
Signed by: Peter Jones <> GPG Key ID: 9DAFAA8D01941E49
1 changed files with 71 additions and 70 deletions
  1. 71

exwm-browsers.el → exwm-wb.el View File

@@ -1,10 +1,10 @@
;;; exwm-browsers.el --- Control web browsers running under EXWM. -*- lexical-binding: t -*-
;;; exwm-wb.el --- Control web browsers running under EXWM. -*- lexical-binding: t -*-

;; Copyright (C) 2018 Peter Jones <>

;; Author: Peter Jones <>
;; Homepage:
;; Package-Requires: ((emacs "24.4") (async "1.9") (exwm "0.18"))
;; Homepage:
;; Package-Requires: ((emacs "25.1") (async "1.9") (exwm "0.18"))
;; Version: 0.1.0
;; This file is not part of GNU Emacs.
@@ -44,7 +44,7 @@
;;; Classes
(defclass exwm-browser ()
(defclass exwm-wb-browser ()
:initarg :name
:initform nil
@@ -103,7 +103,7 @@
" A function to move forwards in the location history.")))

(defmethod exwm-browser--open-url ((browser exwm-browser) url new-window)
(defmethod exwm-wb-browser--open-url ((browser exwm-wb-browser) url new-window)

If NEW-WINDOW is nil then use the set-url function slot to do
@@ -119,55 +119,56 @@ function slot."

;;; Predefined browsers:
(defconst exwm-browsers-surf
(exwm-browser :name "Surf"
:class-name "Surf"
:open-url (lambda (url) (list "surf" url))
:get-url (lambda () (exwm-browsers-get-prop "_SURF_URI"))
:set-url (lambda (url) (exwm-browsers-set-prop "_SURF_GO" url))
:history-back (lambda () (exwm-browsers-send-keys exwm-browsers-surf-back-key))
:history-forward (lambda () (exwm-browsers-send-keys exwm-browsers-surf-forward-key)))
(defconst exwm-wb-surf
:name "Surf"
:class-name "Surf"
:open-url (lambda (url) (list "surf" url))
:get-url (lambda () (exwm-wb-get-prop "_SURF_URI"))
:set-url (lambda (url) (exwm-wb-set-prop "_SURF_GO" url))
:history-back (lambda () (exwm-wb-send-keys exwm-wb-surf-back-key))
:history-forward (lambda () (exwm-wb-send-keys exwm-wb-surf-forward-key)))
"The Surf web browser.")

;;; Customize interface:
(defgroup exwm-browsers nil
(defgroup exwm-wb nil
"A minor mode and helpers for managing web browsers under EXWM."
:version "0.1.0"
:prefix "exwm-browsers-"
:prefix "exwm-wb-"
:group 'applications)

(defcustom exwm-browsers-list
(list exwm-browsers-surf)
(defcustom exwm-wb-list
(list exwm-wb-surf)
"List of supported browsers.")

(defcustom exwm-browsers-surf-back-key [?\C-b]
(defcustom exwm-wb-surf-back-key [?\C-b]
"The key to send to Surf to make it move back in its history."
:group 'exwm-browsers
:group 'exwm-wb
:type 'key-sequence)

(defcustom exwm-browsers-surf-forward-key [?\C-f]
(defcustom exwm-wb-surf-forward-key [?\C-f]
"The key to send to Surf to make it move forward in its history."
:group 'exwm-browsers
:group 'exwm-wb
:type 'key-sequence)

(defcustom exwm-browsers-prompt-for-browser nil
(defcustom exwm-wb-prompt-for-browser nil
"Whether opening a URL should prompt for which browser to use.

Setting this to non-nil means always prompt. The default setting
is to use the first configured browser instead of prompting.

See `exwm-browsers-list' for the configured browser list."
:group 'exwm-browsers
See `exwm-wb-list' for the configured browser list."
:group 'exwm-wb
:type '(repeat FIXME:))

(defcustom exwm-browsers-shortcuts
(defcustom exwm-wb-shortcuts
"URLs of frequently visited sites and search engines.

This list is used by the `exwm-browsers-open-shortcut' command to
This list is used by the `exwm-wb-open-shortcut' command to
build an interactive prompt for selecting a URL and jumping to it
in a new browser window/tab.

@@ -176,68 +177,68 @@ URL is selected from the interactive prompt a second prompt will
be show to read a search string. In this mode the %s in the URL
will be substituted for the input from the second prompt
before launching a browser."
:group 'exwm-browsers
:group 'exwm-wb
:type '(repeat (choice string)))

;;; Internal variables:
(defvar exwm-browser-mode-map
(defvar exwm-wb-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-f") #'exwm-browsers-history-forward)
(define-key map (kbd "C-c C-b") #'exwm-browsers-history-back)
(define-key map (kbd "C-c C-l") #'exwm-browsers-open-url)
(define-key map (kbd "C-c C-j") #'exwm-browsers-open-shortcut)
(define-key map (kbd "C-c C-f") #'exwm-wb-history-forward)
(define-key map (kbd "C-c C-b") #'exwm-wb-history-back)
(define-key map (kbd "C-c C-l") #'exwm-wb-open-url)
(define-key map (kbd "C-c C-j") #'exwm-wb-open-shortcut)
"Key map for `exwm-browser-mode'.")
"Key map for `exwm-wb-mode'.")

;;; Functions:
(defun exwm-browsers-winid ()
(defun exwm-wb-winid ()
"Get the X11 window ID for the current EXWM buffer."
(if (derived-mode-p 'exwm-mode)
(exwm--buffer->id (current-buffer))
(error "Not a EXWM window")))

(defun exwm-browsers-get-prop (name)
(defun exwm-wb-get-prop (name)
"Read window property NAME from the current EXWM buffer."
;; FIXME: This is a hack stolen from exwm-surf: Copyright (C) 2017
;; without permission.
(let* ((winid (exwm-browsers-winid))
(let* ((winid (exwm-wb-winid))
(text (shell-command-to-string
(format "xprop -notype -id %s %s" winid name))))
(string-match "\"\\(.*\\)\"" text)
(match-string 1 text)))

(defun exwm-browsers-set-prop (name value)
(defun exwm-wb-set-prop (name value)
"Set the X11 window property NAME to VALUE for the current EXWM buffer."
(let ((winid (exwm-browsers-winid)))
(let ((winid (exwm-wb-winid)))
"xprop" nil (format "xprop -id %s -f %s 8s -set %s \"%s\""
winid name name value))))

(defun exwm-browsers-send-keys (keys)
(defun exwm-wb-send-keys (keys)
"Send the key sequence KEYS to the current EXWM window."
(when (derived-mode-p 'exwm-mode)
(seq-doseq (key keys)
(exwm-input--fake-key key))))

(defun exwm-browsers-choose-browser ()
(defun exwm-wb-choose-browser ()
"Prompt the user to select a browser."

(defun exwm-browsers-current-browser ()
(defun exwm-wb-current-browser ()
"Return the browser instance for the current window or nil."
(when (derived-mode-p 'exwm-mode)
(object-assoc exwm-class-name 'class-name exwm-browsers-list)))
(object-assoc exwm-class-name 'class-name exwm-wb-list)))

(defun exwm-browsers-funcall (slot arguments)
(defun exwm-wb-funcall (slot arguments)
"Call a function at SLOT with ARGUMENTS for the current browser buffer."
(let* ((browser (exwm-browsers-current-browser))
(let* ((browser (exwm-wb-current-browser))
(func (and browser (slot-value browser slot))))
(when func (apply func arguments))))

(defun exwm-browsers-url-prompt ()
(defun exwm-wb-url-prompt ()
"Prompt for a URL.

If the current window is a browser, extract a URL from it and use
@@ -249,7 +250,7 @@ Return a list just like `browse-url-interactive-arg' does where
the CAR is the URL read from the user and the CADR is the
new-window flag."
(let ((prompt "URL: ")
(url (exwm-browsers-funcall 'get-url nil)))
(url (exwm-wb-funcall 'get-url nil)))
(if url (list (read-string prompt url)
(not (eq (null browse-url-new-window-flag)
(null current-prefix-arg))))
@@ -257,22 +258,22 @@ new-window flag."

;;; Commands:
(defun exwm-browsers-open-url (url &optional new-window)
(defun exwm-wb-open-url (url &optional new-window)
"Open URL in a web browser.

If the current buffer is already a browser window and NEW-WINDOW
is nil, redirect the browser to URL replacing the current
location. Otherwise, when NEW-WINDOW is non-nil, always open a
new browser window/tab."
(interactive (exwm-browsers-url-prompt))
(let ((browser (or (and (not new-window) (exwm-browsers-current-browser))
(interactive (exwm-wb-url-prompt))
(let ((browser (or (and (not new-window) (exwm-wb-current-browser))
(exwm-browser--open-url browser url
(or new-window
(not (exwm-browsers-current-browser))))))
(not (exwm-wb-current-browser))))))

(defun exwm-browsers-open-shortcut (shortcut &optional new-window)
(defun exwm-wb-open-shortcut (shortcut &optional new-window)
"Open a shortcut URL in a browser window.

When called interactively prompt for SHORTCUT. If SHORTCUT
@@ -280,34 +281,34 @@ contains a %s format placeholder also prompt for a search query.

If NEW-WINDOW is non-nil then display the URL in a new window."
(list (completing-read "Shortcut: " exwm-browsers-shortcuts)
(list (completing-read "Shortcut: " exwm-wb-shortcuts)
(let ((url (if (string-match-p "%s" shortcut)
(format shortcut (read-string "Search Query: "))
(exwm-browsers-open-url url new-window)))
(exwm-wb-open-url url new-window)))

(defun exwm-browsers-history-back ()
(defun exwm-wb-history-back ()
"Tell the current browser to go to the previous URL in the history."
(exwm-browsers-funcall 'history-back nil))
(exwm-wb-funcall 'history-back nil))

(defun exwm-browsers-history-forward ()
(defun exwm-wb-history-forward ()
"Tell the current browser to go to the next URL in the history."
(exwm-browsers-funcall 'history-forward nil))
(exwm-wb-funcall 'history-forward nil))

(define-minor-mode exwm-browser-mode
(define-minor-mode exwm-wb-mode
"Minor mode to interact with a web browser displayed by EXWM."
:group 'exwm-browsers
:keymap 'exwm-browser-mode-map)
:group 'exwm-wb
:keymap 'exwm-wb-mode-map)

(defun exwm-browser-mode-maybe-enable ()
"Activate `exwm-browser-mode' if the current buffer is a browser."
(if (exwm-browsers-current-browser) (exwm-browser-mode 1)))
(defun exwm-wb-mode-maybe-enable ()
"Activate `exwm-wb-mode' if the current buffer is a browser."
(if (exwm-wb-current-browser) (exwm-wb-mode 1)))

;; Try to automatically enable `exwm-browser-mode' when needed:
(add-hook 'exwm-manage-finish-hook #'exwm-browser-mode-maybe-enable)
;; Try to automatically enable `exwm-wb-mode' when needed:
(add-hook 'exwm-manage-finish-hook #'exwm-wb-mode-maybe-enable)

(provide 'exwm-browsers)
;;; exwm-browsers.el ends here
(provide 'exwm-wb)
;;; exwm-wb.el ends here