forked from canatella/xwwp
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathxwwp-yank.el
72 lines (60 loc) · 2.81 KB
/
xwwp-yank.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
;;; xwwp-yank.el --- Better yanking in `xwidget-webkit' sessions -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Q. Hong <[email protected]>
;; 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 of the License, 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. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Add support for yanking in input/test areas in `xwidget-webkit' sessions.
;;; Code:
(require 'xwwp)
(defgroup xwwp-yank nil
"`xwidget-webkit' xwwp-yank customizations."
:group 'xwwp)
(defcustom xwwp-yank-enabled (memq window-system '(mac ns))
"Enable yanking workaround for NS webkit implementation."
:group 'xwwp-yank)
(defun xwwp-yank-inject (xwidget)
(xwidget-webkit-execute-script xwidget (format "
window.__xwidget_plus_yank_cache = '%s';
if(!window.__xwidget_plus_yank_handler){
window.__xwidget_plus_yank_handler = function (ev) {
if ( ev.ctrlKey && ev.code == 'KeyY' ){
let ae = document.activeElement;
if( ae && ( ae.nodeName == 'INPUT' || ae.nodeName == 'TEXTAREA' )){
let pos = ae.selectionStart;
let endpos = ae.selectionEnd;
ae.value = ae.value.substring(0, pos) + window.__xwidget_plus_yank_cache + ae.value.substring(endpos);
}
ev.preventDefault();
}
}
}
document.removeEventListener('keydown', window.__xwidget_plus_yank_handler);
document.addEventListener('keydown', window.__xwidget_plus_yank_handler);
null;" (xwwp-js-string-escape (with-temp-buffer (yank) (buffer-string))))))
(defun xwwp-yank-select-window-advice (window &optional norecord)
"If WINDOW contains an xwidget-webkit session, pass top of yank ring
into its window.__xwidget_plus_yank_current attribute."
(when xwwp-yank-enabled
(with-current-buffer (window-buffer window)
(when (eq major-mode 'xwidget-webkit-mode)
(let ((xwidget (xwidget-webkit-current-session)))
(xwwp-yank-inject xwidget))))))
(defun xwwp-yank-xwidget-event-advice ()
"Pass top of yank ring into current xwidget-webkit session's
window.__xwidget_plus_yank_current attribute."
(when xwwp-yank-enabled
(xwwp-yank-inject (xwidget-webkit-current-session))))
(advice-add 'select-window :after #'xwwp-yank-select-window-advice)
(advice-add 'xwidget-event-handler :after #'xwwp-yank-xwidget-event-advice)
(provide 'xwwp-yank)
;;; xwwp-yank.el ends here