2011-08-08 10:41:33 +02:00
|
|
|
;;; xwidget.el --- api functions for xwidgets
|
2010-06-18 15:32:32 +02:00
|
|
|
;; see xwidget.c for more api functions
|
|
|
|
|
2011-08-08 10:41:33 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;
|
|
|
|
|
2010-06-18 15:32:32 +02:00
|
|
|
(require 'xwidget-internal)
|
|
|
|
|
2011-07-12 23:06:27 +02:00
|
|
|
;;TODO model after make-text-button instead!
|
2011-08-08 10:41:33 +02:00
|
|
|
;;; Code:
|
|
|
|
|
2011-07-14 23:16:18 +02:00
|
|
|
(defun xwidget-insert (pos type title width height)
|
2011-08-02 11:42:58 +02:00
|
|
|
"Insert an xwidget at POS, given ID, TYPE, TITLE WIDTH and HEIGHT.
|
2010-06-18 15:32:32 +02:00
|
|
|
Return ID
|
|
|
|
|
2011-08-08 10:41:33 +02:00
|
|
|
see xwidget.c for types suitable for TYPE."
|
2010-06-18 15:32:32 +02:00
|
|
|
(goto-char pos)
|
2011-07-14 23:16:18 +02:00
|
|
|
(let ((id (make-xwidget (point) (point) type title width height nil)))
|
2011-07-18 22:45:22 +02:00
|
|
|
(put-text-property (point)
|
|
|
|
(+ 1 (point)) 'display (list 'xwidget ':xwidget id))
|
|
|
|
|
2011-07-14 23:16:18 +02:00
|
|
|
id))
|
2010-06-18 15:32:32 +02:00
|
|
|
|
2011-07-18 22:45:22 +02:00
|
|
|
|
2011-07-17 23:53:27 +02:00
|
|
|
(defun xwidget-at (pos)
|
2011-08-08 10:41:33 +02:00
|
|
|
"Return xwidget at POS."
|
|
|
|
;;TODO this function is a bit tedious because the C layer isnt well protected yet and
|
2011-08-01 16:40:12 +02:00
|
|
|
;;xwidgetp aparently doesnt work yet
|
|
|
|
(let* ((disp (get-text-property pos 'display))
|
|
|
|
(xw (car (cdr (cdr disp)))))
|
|
|
|
;;(if ( xwidgetp xw) xw nil)
|
|
|
|
(if (equal 'xwidget (car disp)) xw)
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun xwidget-socket-handler ()
|
2011-08-08 10:41:33 +02:00
|
|
|
"Create plug for socket. TODO."
|
2011-08-01 16:40:12 +02:00
|
|
|
(interactive)
|
|
|
|
(message "socket handler xwidget %S" last-input-event)
|
|
|
|
(let*
|
|
|
|
((xwidget-event-type (nth 2 last-input-event))
|
|
|
|
(xwidget-id (nth 1 last-input-event)))
|
|
|
|
(cond ( (eq xwidget-event-type 'xembed-ready)
|
|
|
|
(let*
|
|
|
|
((xembed-id (nth 3 last-input-event)))
|
|
|
|
(message "xembed ready event: %S xw-id:%s" xembed-id xwidget-id)
|
|
|
|
;;TODO fetch process data from the xwidget. create it, store process info
|
|
|
|
;;will start emacs/uzbl in a xembed socket when its ready
|
|
|
|
;; (cond
|
|
|
|
;; ((eq 3 xwidget-id)
|
|
|
|
;; (start-process "xembed" "*xembed*" (format "%ssrc/emacs" default-directory) "-q" "--parent-id" (number-to-string xembed-id) ) )
|
|
|
|
;; ((eq 5 xwidget-id)
|
|
|
|
;; (start-process "xembed2" "*xembed2*" "uzbl-core" "-s" (number-to-string xembed-id) "http://www.fsf.org" ) )
|
|
|
|
)))))
|
2011-07-18 22:45:22 +02:00
|
|
|
|
|
|
|
|
2011-06-25 22:53:39 +02:00
|
|
|
|
|
|
|
|
2011-07-12 23:06:27 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; webkit support
|
2011-07-14 23:16:18 +02:00
|
|
|
(require 'browse-url)
|
2011-08-06 18:36:06 +02:00
|
|
|
(require 'image-mode);;for some image-mode alike functinoality
|
|
|
|
(require 'cl);;for flet
|
|
|
|
|
2011-07-12 23:06:27 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun xwidget-webkit-browse-url (url &optional new-session)
|
|
|
|
"Ask xwidget-webkit to browse URL.
|
|
|
|
NEW-SESSION specifies whether to create a new xwidget-webkit session. URL
|
|
|
|
defaults to the string looking like a url around the cursor position."
|
|
|
|
(interactive (progn
|
|
|
|
(require 'browse-url)
|
|
|
|
(browse-url-interactive-arg "xwidget-webkit URL: ")))
|
|
|
|
(when (stringp url)
|
|
|
|
(if new-session
|
|
|
|
(xwidget-webkit-new-session url)
|
|
|
|
(xwidget-webkit-goto-url url))))
|
|
|
|
|
|
|
|
|
2011-08-02 11:42:58 +02:00
|
|
|
;;shims for adapting image mode code to the webkit browser window
|
|
|
|
(defun xwidget-image-display-size (spec &optional pixels frame)
|
2011-08-08 10:41:33 +02:00
|
|
|
"Image code adaptor. SPEC PIXELS FRAME like the corresponding `image-mode' fn."
|
2011-08-02 11:42:58 +02:00
|
|
|
(let ((xwi (xwidget-info (xwidget-at 1))))
|
|
|
|
(cons (aref xwi 2)
|
|
|
|
(aref xwi 3))))
|
|
|
|
|
|
|
|
(defmacro xwidget-image-mode-navigation-adaptor (fn)
|
2011-08-08 10:41:33 +02:00
|
|
|
"Image code adaptor. `image-mode' FN is called."
|
2011-08-02 11:42:58 +02:00
|
|
|
`(lambda () (interactive)
|
2011-08-04 00:08:40 +02:00
|
|
|
(flet ((image-display-size (spec) (xwidget-image-display-size spec)))
|
2011-08-07 14:24:24 +02:00
|
|
|
(funcall ,fn ))))
|
|
|
|
|
|
|
|
(defmacro xwidget-image-mode-navigation-adaptor-p (fn)
|
2011-08-08 10:41:33 +02:00
|
|
|
"Image code adaptor. `image-mode' FN is called with interactive arg."
|
2011-08-07 14:24:24 +02:00
|
|
|
`(lambda (n) (interactive "p")
|
|
|
|
(flet ((image-display-size (spec) (xwidget-image-display-size spec)))
|
|
|
|
(funcall ,fn n))))
|
2011-08-02 11:42:58 +02:00
|
|
|
|
2011-07-18 22:45:22 +02:00
|
|
|
|
2011-07-12 23:06:27 +02:00
|
|
|
;;todo.
|
|
|
|
;; - check that the webkit support is compiled in
|
2011-07-18 14:37:45 +02:00
|
|
|
(defvar xwidget-webkit-mode-map
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(define-key map "g" 'xwidget-webkit-browse-url)
|
|
|
|
(define-key map "a" 'xwidget-webkit-adjust-size-to-content)
|
2011-07-19 11:07:49 +02:00
|
|
|
(define-key map "b" 'xwidget-webkit-back )
|
2011-08-06 18:36:06 +02:00
|
|
|
(define-key map "r" 'xwidget-webkit-reload )
|
2011-08-08 10:41:33 +02:00
|
|
|
(define-key map "t" (lambda () (interactive) (message "o")) )
|
2011-07-18 14:37:45 +02:00
|
|
|
(define-key map "\C-m" 'xwidget-webkit-insert-string)
|
2011-08-08 10:41:33 +02:00
|
|
|
(define-key map [xwidget-event] 'xwidget-webkit-event-handler);;TODO needs to go into a higher level handler
|
2011-08-02 00:32:00 +02:00
|
|
|
|
|
|
|
;;similar to image mode bindings
|
2011-08-06 18:36:06 +02:00
|
|
|
;;TODO theres something wrong with the macro
|
2011-08-02 11:42:58 +02:00
|
|
|
(define-key map (kbd "SPC") (xwidget-image-mode-navigation-adaptor 'image-scroll-up))
|
|
|
|
(define-key map (kbd "DEL") (xwidget-image-mode-navigation-adaptor 'image-scroll-down))
|
2011-08-07 00:21:42 +02:00
|
|
|
|
|
|
|
(define-key map [remap scroll-up] (xwidget-image-mode-navigation-adaptor 'image-scroll-up))
|
|
|
|
(define-key map [remap scroll-up-command] (xwidget-image-mode-navigation-adaptor 'image-scroll-up))
|
|
|
|
|
|
|
|
(define-key map [remap scroll-down] (xwidget-image-mode-navigation-adaptor 'image-scroll-down))
|
|
|
|
(define-key map [remap scroll-down-command] (xwidget-image-mode-navigation-adaptor 'image-scroll-down))
|
|
|
|
|
2011-08-02 00:32:00 +02:00
|
|
|
|
2011-08-07 14:24:24 +02:00
|
|
|
(define-key map [remap forward-char] (xwidget-image-mode-navigation-adaptor-p 'image-forward-hscroll))
|
|
|
|
(define-key map [remap backward-char] (xwidget-image-mode-navigation-adaptor-p 'image-backward-hscroll))
|
|
|
|
(define-key map [remap right-char] (xwidget-image-mode-navigation-adaptor-p 'image-forward-hscroll))
|
|
|
|
(define-key map [remap left-char] (xwidget-image-mode-navigation-adaptor-p 'image-backward-hscroll))
|
|
|
|
(define-key map [remap previous-line] (xwidget-image-mode-navigation-adaptor-p 'image-previous-line))
|
|
|
|
(define-key map [remap next-line] (xwidget-image-mode-navigation-adaptor-p 'image-next-line))
|
2011-08-02 00:32:00 +02:00
|
|
|
|
2011-08-07 00:21:42 +02:00
|
|
|
|
2011-08-02 00:32:00 +02:00
|
|
|
(define-key map [remap move-beginning-of-line] (xwidget-image-mode-navigation-adaptor 'image-bol))
|
|
|
|
(define-key map [remap move-end-of-line] (xwidget-image-mode-navigation-adaptor 'image-eol))
|
|
|
|
(define-key map [remap beginning-of-buffer] (xwidget-image-mode-navigation-adaptor 'image-bob))
|
|
|
|
(define-key map [remap end-of-buffer] (xwidget-image-mode-navigation-adaptor 'image-eob))
|
|
|
|
|
|
|
|
|
2011-07-18 14:37:45 +02:00
|
|
|
map)
|
2011-07-18 22:45:22 +02:00
|
|
|
|
2011-07-18 14:37:45 +02:00
|
|
|
"Keymap for `xwidget-webkit-mode'.")
|
2011-07-17 23:53:27 +02:00
|
|
|
|
2011-08-02 00:32:00 +02:00
|
|
|
|
2011-07-31 23:55:11 +02:00
|
|
|
(defun xwidget-webkit-event-handler ()
|
2011-08-08 10:41:33 +02:00
|
|
|
"Receive webkit event."
|
2011-07-31 23:55:11 +02:00
|
|
|
(interactive)
|
|
|
|
(message "stuff happened to webkit xwidget %S" last-input-event)
|
|
|
|
(let*
|
|
|
|
((xwidget-event-type (nth 2 last-input-event))
|
|
|
|
(xwidget (nth 1 last-input-event)))
|
2011-08-02 00:32:00 +02:00
|
|
|
(cond ((eq xwidget-event-type 'document-load-finished)
|
|
|
|
(message "webkit loaded %s" xwidget)
|
|
|
|
(xwidget-webkit-adjust-size-to-content))
|
2011-07-31 23:55:11 +02:00
|
|
|
)))
|
2011-07-18 22:45:22 +02:00
|
|
|
|
2011-07-31 23:57:51 +02:00
|
|
|
(define-derived-mode xwidget-webkit-mode
|
|
|
|
special-mode "xwidget-webkit" "xwidget webkit view mode"
|
2011-08-02 00:32:00 +02:00
|
|
|
(setq buffer-read-only t)
|
|
|
|
;; Keep track of [vh]scroll when switching buffers
|
|
|
|
(image-mode-setup-winprops)
|
|
|
|
|
|
|
|
)
|
2011-07-18 22:45:22 +02:00
|
|
|
|
2011-07-17 23:53:27 +02:00
|
|
|
(defvar xwidget-webkit-last-session-buffer nil)
|
|
|
|
|
|
|
|
(defun xwidget-webkit-last-session ()
|
2011-08-08 10:41:33 +02:00
|
|
|
"Last active webkit, or a new one."
|
2011-07-17 23:53:27 +02:00
|
|
|
(if (buffer-live-p xwidget-webkit-last-session-buffer)
|
|
|
|
(save-excursion
|
2011-07-19 17:43:28 +02:00
|
|
|
(set-buffer xwidget-webkit-last-session-buffer)
|
2011-07-17 23:53:27 +02:00
|
|
|
(xwidget-at 1))
|
|
|
|
nil))
|
|
|
|
|
2011-07-30 02:13:59 +02:00
|
|
|
(defun xwidget-webkit-current-session ()
|
2011-08-08 10:41:33 +02:00
|
|
|
"Either the webkit in the current buffer, or the last one used, which might be nil."
|
2011-07-30 02:13:59 +02:00
|
|
|
(if (xwidget-at 1)
|
|
|
|
(xwidget-at 1)
|
|
|
|
(xwidget-webkit-last-session)))
|
|
|
|
|
2011-07-18 14:37:45 +02:00
|
|
|
(defun xwidget-adjust-size-to-content (xw)
|
2011-08-08 10:41:33 +02:00
|
|
|
"Resize XW to content."
|
|
|
|
;;xwidgets doesnt support widgets that have their own opinions about size well yet
|
|
|
|
;;this reads the desired size and resizes the emacs allocated area accordingly
|
2011-07-18 14:37:45 +02:00
|
|
|
(let ((size (xwidget-size-request xw)))
|
2011-07-18 22:45:22 +02:00
|
|
|
(xwidget-resize xw (car size) (cadr size))))
|
|
|
|
|
2011-07-18 14:37:45 +02:00
|
|
|
|
|
|
|
(defun xwidget-webkit-insert-string (xw str)
|
2011-08-08 10:41:33 +02:00
|
|
|
"Insert string in the active field in the webkit.
|
|
|
|
Argument XW webkit.
|
|
|
|
Argument STR string."
|
|
|
|
;;TODO read out the string in the field first and provide for edit
|
2011-07-30 02:13:59 +02:00
|
|
|
(interactive (list (xwidget-webkit-current-session)
|
2011-07-18 14:37:45 +02:00
|
|
|
(read-string "string:")))
|
|
|
|
(xwidget-webkit-execute-script xw (format "document.activeElement.value='%s'" str)))
|
|
|
|
|
|
|
|
(defun xwidget-webkit-adjust-size-to-content ()
|
2011-08-08 10:41:33 +02:00
|
|
|
"Adjust webkit to content size."
|
2011-07-18 14:37:45 +02:00
|
|
|
(interactive)
|
2011-07-30 02:13:59 +02:00
|
|
|
( xwidget-adjust-size-to-content ( xwidget-webkit-current-session)))
|
2011-07-18 22:45:22 +02:00
|
|
|
|
2011-07-23 09:34:37 +02:00
|
|
|
(defun xwidget-webkit-adjust-size (w h)
|
2011-08-08 10:41:33 +02:00
|
|
|
"Manualy set webkit size.
|
|
|
|
Argument W width.
|
|
|
|
Argument H height."
|
|
|
|
;;TODO shouldnt be tied to the webkit xwidget
|
2011-07-23 09:34:37 +02:00
|
|
|
(interactive "nWidth:\nnHeight:\n")
|
2011-07-30 02:13:59 +02:00
|
|
|
( xwidget-resize ( xwidget-webkit-current-session) w h))
|
2011-07-23 09:34:37 +02:00
|
|
|
|
2011-07-18 14:37:45 +02:00
|
|
|
|
2011-07-12 23:06:27 +02:00
|
|
|
(defun xwidget-webkit-new-session (url)
|
2011-08-08 10:41:33 +02:00
|
|
|
"Create a new webkit session buffer with URL."
|
2011-07-17 23:53:27 +02:00
|
|
|
(let*
|
|
|
|
((bufname (generate-new-buffer-name "*xwidget-webkit*"))
|
|
|
|
)
|
|
|
|
(setq xwidget-webkit-last-session-buffer (switch-to-buffer (get-buffer-create bufname)))
|
|
|
|
(insert " ")
|
|
|
|
(xwidget-insert 1 'webkit-osr bufname 1000 1000)
|
|
|
|
(xwidget-webkit-mode)
|
2011-07-18 22:45:22 +02:00
|
|
|
(xwidget-webkit-goto-uri ( xwidget-webkit-last-session) url )))
|
2011-07-12 23:06:27 +02:00
|
|
|
|
|
|
|
|
2011-07-15 05:27:27 +02:00
|
|
|
(defun xwidget-webkit-goto-url (url)
|
2011-08-08 10:41:33 +02:00
|
|
|
"Goto URL."
|
2011-07-30 02:13:59 +02:00
|
|
|
(if ( xwidget-webkit-current-session)
|
2011-07-19 17:43:28 +02:00
|
|
|
(progn
|
2011-07-30 02:13:59 +02:00
|
|
|
(xwidget-webkit-goto-uri ( xwidget-webkit-current-session) url))
|
2011-07-15 05:27:27 +02:00
|
|
|
( xwidget-webkit-new-session url)))
|
2011-07-12 23:06:27 +02:00
|
|
|
|
2011-07-19 11:07:49 +02:00
|
|
|
(defun xwidget-webkit-back ()
|
2011-08-08 10:41:33 +02:00
|
|
|
"Back in history."
|
2011-07-19 11:07:49 +02:00
|
|
|
(interactive)
|
2011-07-30 02:13:59 +02:00
|
|
|
(xwidget-webkit-execute-script ( xwidget-webkit-current-session) "history.go(-1);"))
|
2011-07-20 12:50:40 +02:00
|
|
|
|
|
|
|
(defun xwidget-webkit-reload ()
|
2011-08-08 10:41:33 +02:00
|
|
|
"Reload current url."
|
2011-07-20 12:50:40 +02:00
|
|
|
(interactive)
|
2011-07-30 02:13:59 +02:00
|
|
|
(xwidget-webkit-execute-script ( xwidget-webkit-current-session) "history.go(0);"))
|
2011-07-19 11:07:49 +02:00
|
|
|
|
2011-07-19 17:43:28 +02:00
|
|
|
(defun xwidget-current-url ()
|
2011-08-08 10:41:33 +02:00
|
|
|
"Get the webkit url."
|
2011-07-19 17:43:28 +02:00
|
|
|
;;notice the fugly "title" hack. it is needed because the webkit api doesnt support returning values.
|
|
|
|
;;TODO make a wrapper for the title hack so its easy to remove should webkit someday support JS return values
|
2011-08-08 10:41:33 +02:00
|
|
|
;;or we find some other way to access the DOM
|
2011-07-30 02:13:59 +02:00
|
|
|
(xwidget-webkit-execute-script (xwidget-webkit-current-session) "document.title=document.URL;")
|
|
|
|
(xwidget-webkit-get-title (xwidget-webkit-current-session)))
|
2011-07-19 17:43:28 +02:00
|
|
|
|
|
|
|
|
2011-06-25 22:53:39 +02:00
|
|
|
|
2010-06-18 15:32:32 +02:00
|
|
|
;; use declare here?
|
|
|
|
;; (declare-function xwidget-resize-internal "xwidget.c" )
|
|
|
|
;; check-declare-function?
|
|
|
|
|
2011-07-18 01:26:27 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
(defun xwidget-cleanup ()
|
2011-08-08 10:41:33 +02:00
|
|
|
"Delete zombie xwidgets."
|
2011-07-18 01:26:27 +02:00
|
|
|
;;its still pretty easy to trigger bugs with xwidgets.
|
|
|
|
;;this function tries to implement a workaround
|
|
|
|
(interactive)
|
|
|
|
(xwidget-delete-zombies) ;;kill xviews who should have been deleted but stull linger
|
|
|
|
(redraw-display);;redraw display otherwise ghost of zombies will remain to haunt the screen
|
|
|
|
)
|
|
|
|
|
2011-07-18 22:45:22 +02:00
|
|
|
|
|
|
|
|
2011-07-18 14:37:45 +02:00
|
|
|
;;this is a workaround because I cant find the right place to put it in C
|
2011-08-08 10:41:33 +02:00
|
|
|
;;seems to work well in practice though
|
2011-07-18 14:37:45 +02:00
|
|
|
(add-hook 'window-configuration-change-hook 'xwidget-cleanup)
|
|
|
|
|
2011-08-08 10:41:33 +02:00
|
|
|
;;killflash is sadly not reliable yet.
|
2011-07-31 23:55:11 +02:00
|
|
|
(defvar xwidget-webkit-kill-flash-oneshot t)
|
2011-07-18 22:45:22 +02:00
|
|
|
(defun xwidget-webkit-kill-flash ()
|
2011-08-08 10:41:33 +02:00
|
|
|
"Disable the flash plugin in webkit.
|
|
|
|
This is needed because Flash is non-free and doesnt work reliably
|
|
|
|
on 64 bit systems and offscreen rendering. Sadly not reliable
|
|
|
|
yet, so deinstall Flash instead for now."
|
2011-07-18 22:45:22 +02:00
|
|
|
;;you can only call this once or webkit crashes and takes emacs with it. odd.
|
|
|
|
(unless xwidget-webkit-kill-flash-oneshot
|
|
|
|
(xwidget-disable-plugin-for-mime "application/x-shockwave-flash")
|
|
|
|
(setq xwidget-webkit-kill-flash-oneshot t)))
|
|
|
|
|
|
|
|
(xwidget-webkit-kill-flash)
|
|
|
|
|
2010-06-18 15:32:32 +02:00
|
|
|
(provide 'xwidget)
|
2011-08-08 10:41:33 +02:00
|
|
|
|
|
|
|
(provide 'xwidget)
|
|
|
|
|
|
|
|
;;; xwidget.el ends here
|