xwidget-plus-common.el 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. (defun xwidget-plus-make-class (class style)
  21. "Generate a css CLASS definition from the STYLE alist."
  22. (format ".%s { %s }\\n" class (mapconcat (lambda (v) (format "%s: %s;" (car v) (cdr v))) style " ")))
  23. (defmacro --js (js _ &rest replacements)
  24. "Apply `format' on JS with REPLACEMENTS providing MMM mode delimiters.
  25. This file has basic support for javascript using MMM mode and
  26. local variables (see at the end of the file)."
  27. (declare (indent 3))
  28. `(format ,js ,@replacements))
  29. (defun xwidget-plus-js-string-escape (string)
  30. "Escape STRING for injection."
  31. (replace-regexp-in-string "\n" "\\\\n" (replace-regexp-in-string "'" "\\\\'" string)))
  32. (defun xwidget-plus-inject-head-element (xwidget tag id type content)
  33. "Insert TAG element under XWIDGET head with ID TYPE and CONTENT."
  34. (let* ((id (xwidget-plus-js-string-escape id))
  35. (tag (xwidget-plus-js-string-escape tag))
  36. (type (xwidget-plus-js-string-escape type))
  37. (content (xwidget-plus-js-string-escape content))
  38. (script (--js "
  39. __xwidget_id = '%s';
  40. if (!document.getElementById(__xwidget_id)) {
  41. var e = document.createElement('%s');
  42. e.type = '%s';
  43. e.id = __xwidget_id;
  44. e.innerHTML = '%s';
  45. document.getElementsByTagName('head')[0].appendChild(e);
  46. };
  47. null;
  48. " js-- id tag type content)))
  49. (xwidget-webkit-execute-script xwidget script)))
  50. (defun xwidget-plus-inject-script (xwidget id script)
  51. "Inject javascript SCRIPT in XWIDGET session using a script element with ID."
  52. (xwidget-plus-inject-head-element xwidget "script" id "text/javascript" script))
  53. (defun xwidget-plus-inject-style (xwidget id style)
  54. "Inject css STYLE in XWIDGET session using a style element with ID."
  55. (xwidget-plus-inject-head-element xwidget "style" id "text/css" style))
  56. ;; Local Variables:
  57. ;; eval: (mmm-mode)
  58. ;; eval: (mmm-add-classes '((elisp-js :submode js-mode :face mmm-code-submode-face :delimiter-mode nil :front "--js \"" :back "\" js--")))
  59. ;; mmm-classes: elisp-js
  60. ;; End:
  61. (provide 'xwidget-plus-common)
  62. ;;; xwidget-plus-common.el ends here