FG42/lisp/fg42/cubes/meissa.el

81 lines
3.1 KiB
EmacsLisp

;;; MeissaCube --- Meissa client for FG42 -*- lexical-binding: t; -*-
;;
;; Copyright (c) 2010-2024 Sameer Rahmani & Contributors
;;
;; Author: Sameer Rahmani <lxsameer@gnu.org>
;; URL: https://ziglab.com/FG42/FG42
;; Version: 3.0.0
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;; Code:
(require 'fpkg)
(require 'fg42/cube)
(require 'fg42/utils)
(require 'fg42/x)
(defcube fg42/meissa-cube
"Integrate FG42 with Meissa"
:title "Meissa cube"
:flag-default t
:flag meissa
(fpkg/use msgpack)
(autoload 'msgpack-encode "msgpack")
(let ((host (or (plist-get fg42/meissa-cube-params :host) "127.0.0.1"))
(port (or (plist-get fg42/meissa-cube-params :port) "6666")))
(defun fg42/meissa-sentinel (process event)
(message "[Meissa] %s: %s" process event))
(defun fg42/meissa-send-command (command payload cb)
(let* ((encoded-command (msgpack-encode `((command . ,command) (payload . ,payload))))
(decoded-response nil)
(process (make-network-process :name "meissa-client"
:buffer "*Meissa*"
:host host
:service port
:family 'ipv4
:coding 'binary
:filter (lambda (_process data)
(setq decoded-response
(msgpack-read-from-string data)))
;;(lambda (_process _event) (delete-process process))
:sentinel #'fg42/meissa-sentinel)))
(process-send-string process encoded-command)
(setq command-response-timer
(run-with-timer 1 nil
(lambda (cb)
(while (not decoded-response)
(accept-process-output process))
(funcall cb decoded-response))
cb))))
(defun fg42/meissa-process-response (response)
(message "[Meissa]: %s" response))
(defun fg42/meissa-speech (txt)
(fg42/meissa-send-command "enqueue" `((text . ,txt)) #'fg42/meissa-process-response))
(defun read-this-for-me ()
(interactive)
(fg42/meissa-speech (fg42/get-x11-selection-text)))))
(provide 'fg42/cubes/meissa)
;;; meissa.el ends here