Skip to content

Instantly share code, notes, and snippets.

@ympbyc
Forked from BilalQadri/emacs-biwa.el
Last active May 18, 2020 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ympbyc/ae1b4cd95a1a3d0d2aae70b94b14d096 to your computer and use it in GitHub Desktop.
Save ympbyc/ae1b4cd95a1a3d0d2aae70b94b14d096 to your computer and use it in GitHub Desktop.
Biwascheme from Emacs (with repl)
;; Initialize Biwascheme interpreter on frontend(browser) then send and receive expressions with websocket client
;; INSTALL websocket.el, LOAD this file and RUN interactive function `load-ws` from Emacs
;; ws-close to close server
;; send expression (e.g functions) to browser with `C-c C-s` ... Tip# cursor should be on function name
(require 'websocket)
(require 'scheme)
(defun biwa-start ()
(interactive)
;(load-file "~/.emacs.d/websocket.el")
(biwa-start-ws)
(biwa-start-repl))
(defun biwa-hov ()
(interactive)
(buffer-substring-no-properties (mark) (point)))
(defun biwa-selct ()
(interactive)
(backward-up-list)
(mark-sexp)
(biwa-send-text (biwa-hov)))
(defun biwa-eval-last-sexp ()
(interactive)
(biwa-send-text (format "%S" (elisp--preceding-sexp))))
;(define-key lisp-mode-map [remap isearch-forward] 'selct)
(define-key scheme-mode-map "\C-c\C-s" #'biwa-selct)
(define-key scheme-mode-map "\C-c\C-e" #'biwa-eval-last-sexp)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; web socket
;;;;;;;;;;;;;;;;
(defvar biwa-global-proc nil)
(defvar biwa-ws-conn nil)
(defvar biwa-client nil)
(defun biwa-send-text (msg)
(websocket-send-text biwa-client msg))
(defun biwa-ws-close ()
(interactive)
(websocket-server-close biwa-ws-conn))
(defun biwa-start-ws ()
(interactive)
(setq tls-checktrust nil)
(when (>= (string-to-number (substring emacs-version 0 2)) 24)
(message "Testing with wss://127.0.0.1/")
(when (eq system-type 'windows-nt)
(message "Windows users must have gnutls DLLs in the emacs bin directory."))
(setq biwa-ws-conn (websocket-server
8001
:host "127.0.0.1"
:on-message (lambda (_ws frame)
(setf biwa-client _ws)
(comint-output-filter biwa-global-proc (format " %s\n" (websocket-frame-payload frame)))
(comint-output-filter biwa-global-proc biwa-prompt)
(message "ws frame: %S" (websocket-frame-payload frame)))
:on-open (lambda (_websocket) (message "Client connection opened!"))
:on-close (lambda (_websocket)
(message "Websocket closed"))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; derive comint
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun biwa-ccb ()
(interactive)
(let ((comint-buffer-maximum-size 0))
(comint-truncate-buffer)))
;; let's bind the new command to a keycombo
(define-key comint-mode-map "\C-c\M-o" #'biwa-ccb)
(defconst biwa-prompt "Biwa> ")
(defun biwa-input-sender (proc input)
(setf biwa-global-proc proc)
(message input)
(biwa-send-text input))
(define-derived-mode
biwa-mode comint-mode "biwa"
"Run a Biwa shell."
:syntax-table js2-mode-syntax-table
(setq comint-prompt-regexp (concat "^" (regexp-quote biwa-prompt)))
(setq comint-input-sender 'biwa-input-sender)
(unless (comint-check-proc (current-buffer))
;; Was cat, but on non-Unix platforms that might not exist, so
;; use hexl instead, Interactive Javascript Mode which is part of the Emacs distribution.
(let ((fake-proc
(condition-case nil
(start-process "biwa" (current-buffer) "hexl")
(file-error (start-process "biwa" (current-buffer) "cat")))))
(set-process-query-on-exit-flag fake-proc nil)
;; Add a silly header
(insert "Interactive Biwa Mode\n")
(set-marker
(process-mark fake-proc) (point))
(comint-output-filter fake-proc biwa-prompt))))
(defun biwa-start-repl ()
(interactive)
(let ((buffer (generate-new-buffer "*BIWA*")))
(with-current-buffer buffer
(funcall 'biwa-mode))
(switch-to-buffer buffer)))
;(display-buffer buffer '(display-buffer-pop-up-frame . nil)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment