-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebkit.el
384 lines (324 loc) · 11.8 KB
/
webkit.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
;;; webkit.el --- Running WebKit browser in Emacs
;; Filename: webkit.el
;; Description: Running WebKit browser in Emacs
;; Author: Andy Stewart <lazycat.manatee@gmail.com>
;; Maintainer: Andy Stewart <lazycat.manatee@gmail.com>
;; Copyright (C) 2014, Andy Stewart, all rights reserved.
;; Created: 2014-01-02 21:51:17
;; Version: 0.1
;; Last-Updated: 2014-01-02 21:51:17
;; By: Andy Stewart
;; URL: http://www.emacswiki.org/emacs/download/webkit.el
;; Keywords:
;; Compatibility: GNU Emacs 24.3.50.1
;;
;; Features that might be required by this library:
;;
;;
;;
;;; This file is NOT part of GNU Emacs
;;; License
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; Running WebKit browser in Emacs
;;
;;; Installation:
;;
;; NOTE: just webkit.el can't work, you need install below depends first:
;; * PyQt5: http://www.riverbankcomputing.co.uk/software/pyqt/intro
;; * Python-Xlib: https://pypi.python.org/pypi/python-xlib
;; * Python-EPC: https://github.com/tkf/python-epc
;;
;; Detail description please look: http://www.emacswiki.org/emacs/WebKit
;;
;; Then put webkit.el to your load-path.
;; The load-path is usually ~/elisp/.
;; It's set in your ~/.emacs like this:
;; (add-to-list 'load-path (expand-file-name "~/elisp"))
;;
;; And the following to your ~/.emacs startup file.
;;
;; (require 'webkit)
;;
;; Quick start:
;;
;; M-x webkit-open-url
;;
;;; Customize:
;;
;;
;;
;; All of the above can customize by:
;; M-x customize-group RET webkit RET
;;
;;; Change log:
;;
;; 2014/01/02
;; * First released.
;;
;;; Acknowledgements:
;;
;;
;;
;;; TODO
;;
;;
;;
;;; Require
(require 'epc)
(when noninteractive
(load "subr")
(load "byte-run"))
(eval-when-compile (require 'cl))
;;; Code:
(defcustom webkit-mode-hook '()
"WebKit mode hook."
:type 'hook
:group 'webkit-mode)
(defvar webkit-mode-map
(let ((map (make-sparse-keymap)))
map)
"Keymap used by `webkit-mode'.")
(define-derived-mode webkit-mode text-mode "WebKit"
(interactive)
(kill-all-local-variables)
(setq major-mode 'webkit-mode)
(setq mode-name "WebKit")
(set (make-local-variable 'buffer-id) (webkit-generate-id))
(use-local-map webkit-mode-map)
(run-hooks 'webkit-mode-hook))
(defun webkit-get-window-allocation (&optional window)
(let* ((window-edges (window-inside-pixel-edges window))
(x (nth 0 window-edges))
(y (nth 1 window-edges))
(w (- (nth 2 window-edges) x))
(h (- (nth 3 window-edges) y))
)
(list x y w h)))
(defun webkit-get-emacs-xid ()
(frame-parameter nil 'window-id))
(random t)
(defun webkit-generate-id ()
(format "%04x%04x-%04x-%04x-%04x-%06x%06x"
(random (expt 16 4))
(random (expt 16 4))
(random (expt 16 4))
(random (expt 16 4))
(random (expt 16 4))
(random (expt 16 6))
(random (expt 16 6)) ))
(defvar webkit-enable-proxy-p nil)
(defvar webkit-proxy-config-path "~/.emacs.d/deepin-emacs/webkit-proxy")
(defun webkit-save-proxy-config ()
(with-current-buffer (find-file-noselect webkit-proxy-config-path)
(erase-buffer)
(insert (prin1-to-string webkit-enable-proxy-p))
(let ((delete-old-versions nil))
(save-buffer 0))))
(defun webkit-restore-proxy-config ()
(if (file-exists-p webkit-proxy-config-path)
(setq webkit-enable-proxy-p
(read
(with-temp-buffer
(insert-file-contents webkit-proxy-config-path)
(buffer-string))))))
(webkit-restore-proxy-config)
(defvar pyepc-file (expand-file-name "browser.py" (file-name-directory load-file-name)))
(defvar pyepc-browser
(let* ((browser
(epc:start-epc (or (getenv "PYTHON") "python")
(list pyepc-file
(if webkit-enable-proxy-p "--enable-proxy" "--disable-proxy")))))
(epc:call-deferred browser 'init (list (webkit-get-emacs-xid)))
browser))
(defvar webkit-buffer-dict (make-hash-table :test 'equal))
(defvar webkit-history-urls-path "~/.emacs.d/deepin-emacs/webkit-history")
(defvar webkit-history-urls (make-hash-table :test 'equal))
(defvar webkit-title-length 30)
(defvar webkit-tab-index 0)
(defun webkit-create-buffer (url)
(setq webkit-tab-index (+ webkit-tab-index 1))
(let ((webkit-buffer (generate-new-buffer (concat (truncate-string-to-width url webkit-title-length)))))
(with-current-buffer webkit-buffer
(webkit-mode)
(set (make-local-variable 'buffer-url) url)
(puthash buffer-id webkit-buffer webkit-buffer-dict)
)
webkit-buffer))
(defun webkit-get-url-name (url)
(car (last (split-string url "://")))
)
(defun webkit-get-url-history (url-name)
(if webkit-history-urls
(gethash url-name webkit-history-urls)
nil)
)
(defun webkit-change-buffer-title (id title)
(let* ((buffer (gethash id webkit-buffer-dict)))
(with-current-buffer buffer
;; Rename buffer title.
(rename-buffer (truncate-string-to-width title webkit-title-length))
;; Record url title in history.
(let* ((url-name (webkit-get-url-name buffer-url))
(url-history (webkit-get-url-history url-name)))
(if url-history
(let ((url-number (car url-history))
(url-title (cdr url-history)))
(puthash url-name (list url-number title) webkit-history-urls)
(webkit-save-history-urls))))
)
)
)
(defun webkit-delete-history-url (url-name)
(let ((url-history (webkit-get-url-history url-name)))
(when url-history
(remhash url-name webkit-history-urls)
(webkit-save-history-urls))))
(defun webkit-clean-history ()
(setq webkit-history-urls nil)
(webkit-save-history-urls))
(defun webkit-save-history-urls ()
(with-current-buffer (find-file-noselect webkit-history-urls-path)
(erase-buffer)
(insert (prin1-to-string webkit-history-urls))
(let ((delete-old-versions nil))
(save-buffer 0))))
(defun webkit-restore-history-urls ()
(if (file-exists-p webkit-history-urls-path)
(setq webkit-history-urls
(read
(with-temp-buffer
(insert-file-contents webkit-history-urls-path)
(buffer-string)))))
;; Init hash table if `webkit-history-urls' is nil.
(unless webkit-history-urls
(setq webkit-history-urls (make-hash-table :test 'equal)))
)
(webkit-restore-history-urls)
(defun webkit-open-url (url)
(interactive "sURL: ")
(let* ((buffer (webkit-create-buffer url))
(url-parts (split-string url "://"))
)
(unless (member (nth 0 url-parts) (list "http" "https" "ftp" "file"))
(if (= (length url-parts) 1)
(setq url (concat "http://" (nth 0 url-parts))))
)
(with-current-buffer buffer
(let* ((window-allocation (webkit-get-window-allocation))
(x (nth 0 window-allocation))
(y (nth 1 window-allocation))
(w (nth 2 window-allocation))
(h (nth 3 window-allocation))
(view-id (webkit-generate-id)))
(epc:call-deferred pyepc-browser 'create_buffer (list buffer-id url w h))
))
(switch-to-buffer buffer)
;; Record browse history.
(let* ((url-name (webkit-get-url-name url))
(url-history (webkit-get-url-history url-name)))
(if url-history
(let ((url-number (car url-history))
(url-title (cdr url-history)))
(puthash url-name (list (+ url-number 1) url-title) webkit-history-urls))
(puthash url-name (list 1 url) webkit-history-urls))
(webkit-save-history-urls))
))
(defun webkit-monitor-window-change (&rest _)
(let ((view-infos)
(selected-buffer (window-buffer (selected-window))))
(dolist (window (window-list))
(let ((buffer (window-buffer window)))
(with-current-buffer buffer
(if (string= "webkit-mode" (format "%s" major-mode))
(let* ((window-allocation (webkit-get-window-allocation window))
(x (nth 0 window-allocation))
(y (nth 1 window-allocation))
(w (nth 2 window-allocation))
(h (nth 3 window-allocation))
)
(add-to-list 'view-infos (list buffer-id x y w h))
)))))
(epc:call-deferred pyepc-browser 'update_views (list view-infos))
(with-current-buffer selected-buffer
(if (string= "webkit-mode" (format "%s" major-mode))
(let* ((window-allocation (webkit-get-window-allocation (selected-window)))
(w (nth 2 window-allocation))
(h (nth 3 window-allocation))
)
(epc:call-deferred pyepc-browser 'adjust_size (list buffer-id w h))
)))
))
(defun webkit-monitor-buffer-kill ()
(with-current-buffer (buffer-name)
(if (string= "webkit-mode" (format "%s" major-mode))
(progn
(epc:call-deferred pyepc-browser 'remove_buffer (list buffer-id))
(remhash buffer-id webkit-buffer-dict)))))
(defun webkit-focus-browser-view ()
(interactive)
(with-current-buffer (current-buffer)
(if (string= "webkit-mode" (format "%s" major-mode))
(let* ((window-allocation (webkit-get-window-allocation (get-buffer-window (current-buffer))))
(x (nth 0 window-allocation))
(y (nth 1 window-allocation))
(w (nth 2 window-allocation))
(h (nth 3 window-allocation))
)
(epc:call-deferred pyepc-browser 'focus_view (list buffer-id x y w h))
(message "Focus view: %S" buffer-id)
)
)))
(defun webkit-toggle-console-info ()
(interactive)
(epc:call-deferred pyepc-browser 'toggle_console_info ()))
(defun webkit-enable-proxy ()
(interactive)
(setq webkit-enable-proxy-p t)
(webkit-save-proxy-config)
(message "Enable webkit proxy, reboot emacs effective."))
(defun webkit-disable-proxy ()
(interactive)
(setq webkit-enable-proxy-p nil)
(webkit-save-proxy-config)
(message "Disable webkit proxy, reboot emacs effective."))
(defadvice switch-to-buffer (after webkit-switch-to-buffer-advice activate)
(webkit-focus-browser-view))
(defadvice other-window (after webkit-other-window-advice activate)
(webkit-focus-browser-view))
(add-hook 'window-configuration-change-hook #'webkit-monitor-window-change)
(add-hook 'kill-buffer-hook #'webkit-monitor-buffer-kill)
(epc:define-method pyepc-browser
'message
(lambda (&rest args) (message "%S" args)))
(epc:define-method pyepc-browser
'open-url
(lambda (&rest args)
(webkit-open-url (nth 0 args))
))
(epc:define-method pyepc-browser
'change-buffer-title
(lambda (&rest args)
(webkit-change-buffer-title (nth 0 args) (nth 1 args))
))
(epc:define-method pyepc-browser
'focus-browser-view
(lambda (&rest args)
(webkit-focus-browser-view)
))
(setq browse-url-browser-function (lambda (url flag) (webkit-open-url url)))
(provide 'webkit)
;;; webkit.el ends here