xwidget-plus-common.el 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ;;; xwidget-plus-common.el --- Helper functions for xwidget-plus. -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2020 Damien Merenne <dam@cosinux.org>
  3. ;; This file is NOT part of GNU Emacs.
  4. ;;; Commentary:
  5. ;; Shared functions for the xwidget-plus package.
  6. ;;; License:
  7. ;; This program is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; This program is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;
  19. ;;; Code:
  20. (defgroup xwidget-plus nil
  21. "Augment the xwidget webkit browser."
  22. :group 'convenience)
  23. (defcustom xwidget-plus-completion-system 'default
  24. "The completion system to be used by xwidget plus.
  25. Custom function should be a function that takes no arguments and
  26. returns an instance of an eieio class extending
  27. `xwidget-plus-completion-backend'."
  28. :group 'xwidget-plus
  29. :type '(radio
  30. (const :tag "Ido" ido)
  31. (const :tag "Helm" helm)
  32. (const :tag "Ivy" ivy)
  33. (const :tag "Default" default)
  34. (function :tag "Custom function")))
  35. (require 'json)
  36. (require 'subr-x)
  37. (require 'xwidget)
  38. (defun xwidget-plus-make-class (class style)
  39. "Generate a css CLASS definition from the STYLE alist."
  40. (format ".%s { %s }\\n" class (mapconcat (lambda (v) (format "%s: %s;" (car v) (cdr v))) style " ")))
  41. (defmacro xwidget-plus--js (js _ &rest replacements)
  42. "Apply `format' on JS with REPLACEMENTS providing MMM mode delimiters.
  43. This file has basic support for javascript using MMM mode and
  44. local variables (see at the end of the file)."
  45. (declare (indent 2))
  46. `(format ,js ,@replacements))
  47. (defun xwidget-plus-js-string-escape (string)
  48. "Escape STRING for injection."
  49. (replace-regexp-in-string "\n" "\\\\n" (replace-regexp-in-string "'" "\\\\'" string)))
  50. (defun xwidget-plus-inject-head-element (xwidget tag id type content)
  51. "Insert TAG element under XWIDGET head with ID TYPE and CONTENT."
  52. (let* ((id (xwidget-plus-js-string-escape id))
  53. (tag (xwidget-plus-js-string-escape tag))
  54. (type (xwidget-plus-js-string-escape type))
  55. (content (xwidget-plus-js-string-escape content))
  56. (script (xwidget-plus--js "
  57. __xwidget_id = '%s';
  58. if (!document.getElementById(__xwidget_id)) {
  59. var e = document.createElement('%s');
  60. e.type = '%s';
  61. e.id = __xwidget_id;
  62. e.innerHTML = '%s';
  63. document.getElementsByTagName('head')[0].appendChild(e);
  64. };
  65. null;
  66. " js-- id tag type content)))
  67. (xwidget-webkit-execute-script xwidget script)))
  68. (defun xwidget-plus-inject-script (xwidget id script)
  69. "Inject javascript SCRIPT in XWIDGET session using a script element with ID."
  70. (xwidget-plus-inject-head-element xwidget "script" id "text/javascript" script))
  71. (defun xwidget-plus-inject-style (xwidget id style)
  72. "Inject css STYLE in XWIDGET session using a style element with ID."
  73. (xwidget-plus-inject-head-element xwidget "style" id "text/css" style))
  74. (defun xwidget-plus-lisp-to-js (identifier)
  75. "Convert IDENTIFIER from Lisp style to javascript style."
  76. (replace-regexp-in-string "-" "_" (if (symbolp identifier) (symbol-name identifier) identifier)))
  77. (defvar xwidget-plus-js-scripts '() "An alist of list of javascript function.")
  78. (defun xwidget-plus-js-register-function (ns-name name js-script)
  79. "Register javascript function NAME in namespace NS-NAME with body JS-SCRIPT."
  80. (let* ((namespace (assoc ns-name xwidget-plus-js-scripts))
  81. (fun (when namespace (assoc name (cdr namespace)))))
  82. (cond (fun
  83. (delete fun namespace)
  84. (xwidget-plus-js-register-function ns-name name js-script))
  85. ((not namespace)
  86. (push (cons ns-name '()) xwidget-plus-js-scripts)
  87. (xwidget-plus-js-register-function ns-name name js-script))
  88. (t
  89. (push (cons name js-script) (cdr namespace))))
  90. (cons ns-name name)))
  91. (defun xwidget-plus-js-funcall (xwidget namespace name &rest arguments)
  92. "Invoke javascript function NAME in XWIDGET instance passing ARGUMENTS witch CALLBACK in NAMESPACE."
  93. ;;; Try to be smart
  94. (let* ((callback (car (last arguments)))
  95. (arguments (if (functionp callback) (reverse (cdr (reverse arguments))) arguments))
  96. (json-args (seq-map #'json-encode arguments))
  97. (arg-string (string-join json-args ", "))
  98. (namespace (xwidget-plus-lisp-to-js namespace))
  99. (name (xwidget-plus-lisp-to-js name))
  100. (script (format "__xwidget_plus_%s_%s(%s)" namespace name arg-string)))
  101. (xwidget-webkit-execute-script xwidget script (and (functionp callback) callback))))
  102. (defmacro xwidget-plus-js-def (namespace name arguments docstring js-body)
  103. "Create a function NAME with ARGUMENTS, DOCSTRING and JS-BODY.
  104. This will define a javascript function in the namespace NAMESPACE
  105. and a Lisp function to call it."
  106. (declare (indent 3) (doc-string 4))
  107. (let* ((js-arguments (seq-map #'xwidget-plus-lisp-to-js arguments))
  108. (js-name (xwidget-plus-lisp-to-js name))
  109. (js-namespace (xwidget-plus-lisp-to-js namespace))
  110. (lisp-arguments (append '(xwidget) arguments '(&optional callback)))
  111. (script (xwidget-plus--js "function __xwidget_plus_%s_%s(%s) {%s};" js--
  112. js-namespace js-name (string-join js-arguments ", ") (eval js-body)))
  113. (lisp-def `(defun ,(intern (format "xwidget-plus-%s-%s" namespace name)) ,lisp-arguments
  114. ,docstring
  115. (xwidget-plus-js-funcall xwidget (quote ,namespace) (quote ,name) ,@arguments callback)))
  116. (lisp-store `(xwidget-plus-js-register-function (quote ,namespace) (quote ,name) ,script)))
  117. `(progn ,lisp-def ,lisp-store)))
  118. (defun xwidget-plus-js-inject (xwidget ns-name)
  119. "Inject the functions defined in NS-NAME into XWIDGET session."
  120. (let* ((namespace (assoc ns-name xwidget-plus-js-scripts))
  121. (script (mapconcat #'cdr (cdr namespace) "\n")))
  122. (xwidget-plus-inject-script xwidget (format "--xwidget-plus-%s" (symbol-name ns-name)) script)))
  123. ;; Local Variables:
  124. ;; eval: (mmm-mode)
  125. ;; eval: (mmm-add-group 'elisp-js '((elisp-rawjs :submode js-mode
  126. ;; :face mmm-code-submode-face
  127. ;; :delimiter-mode nil
  128. ;; :front "xwidget-plus--js \"" :back "\" js--")
  129. ;; (elisp-defjs :submode js-mode
  130. ;; :face mmm-code-submode-face
  131. ;; :delimiter-mode nil
  132. ;; :front "xwidget-plus-defjs .*\n.*\"\"\n" :back "\")\n")))
  133. ;; mmm-classes: elisp-js
  134. ;; End:
  135. (provide 'xwidget-plus-common)
  136. ;;; xwidget-plus-common.el ends here