xwidget-plus-common.el 6.2 KB

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