Skip to content

Instantly share code, notes, and snippets.

@BilalQadri
Last active June 26, 2022 12:58
Show Gist options
  • Save BilalQadri/d8bc6272dafbcb7c4c363e42b5854b28 to your computer and use it in GitHub Desktop.
Save BilalQadri/d8bc6272dafbcb7c4c363e42b5854b28 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
(defun load-ws ()
(interactive)
(load-file "~/.emacs.d/websocket.el")
(start-ws)
(start-repl))
(defun hov ()
(interactive)
(buffer-substring-no-properties (mark) (point)))
(defun selct ()
(interactive)
(backward-up-list)
(mark-sexp)
(send-text (hov)))
;(define-key lisp-mode-map [remap isearch-forward] 'selct)
(define-key scheme-mode-map "\C-c\C-s" #'selct)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; web socket
;;;;;;;;;;;;;;;;
(defvar global-proc nil)
(defvar ws-conn nil)
(defvar client nil)
(defun send-text (msg)
(websocket-send-text client msg))
(defun ws-close ()
(interactive)
(websocket-server-close ws-conn))
(defun start-ws ()
(interactive)
(setq tls-checktrust nil)
(when (>= (string-to-number (substring emacs-version 0 2)) 24)
(message "Testing with wss://192.168.100.8/")
(when (eq system-type 'windows-nt)
(message "Windows users must have gnutls DLLs in the emacs bin directory."))
(setq ws-conn (websocket-server
8001
:host "192.168.100.5"
:on-message (lambda (_ws frame)
(setf client _ws)
(comint-output-filter global-proc (format " %s\n" (websocket-frame-text frame)))
(comint-output-filter global-proc biwa-prompt)
(message "ws frame: %S" (websocket-frame-text frame)))
:on-open (lambda (_websocket) (message "Client connection opened!"))
:on-close (lambda (_websocket)
(message "Websocket closed"))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; derive comint
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun 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" #'ccb)
(defconst biwa-prompt "Biwa> ")
(defun biwa-input-sender (proc input)
(setf global-proc proc)
(message input)
(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 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