FG42/core/cubes/rigel/core.el

106 lines
3.4 KiB
EmacsLisp

;;; RigelCube --- Rigel client for FG42 -*- lexical-binding: t; -*-
;;
;; Copyright (c) 2010-2023 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 'bindat)
(eval-when-compile
(defvar fg42/rigel-cube-params))
(defconst fg42/rigel/protocol
(bindat-type (len uint 32) (payload str (+ 32 len))))
(defvar fg42/rigel/client-process nil
"The process for the client.")
(defvar fg42/rigel/host (or (plist-get fg42/rigel-cube-params :host) "127.0.0.1")
"The hostname or ip address of the Rigel server.")
(defvar fg42/rigel/port (or (plist-get fg42/rigel-cube-params :port) "6666")
"The port number of the target Rigel server.")
(defun fg42/rigel/-pack-struct (str)
"Pack the given STR in an alist representing a message."
(list (cons 'len (length (eval str))) (cons 'payload (eval str))))
(defun fg42/rigel/-pack (str)
"Pack the give STR into binary format according to the `fg42/rigel/protocol'
protocol."
(bindat-pack fg42/rigel/protocol (fg42/rigel/-pack-struct str)))
(defun fg42/rigel/-sentinel (process event)
(message "[Rigel] %s: %s" process event))
(defun fg42/rigel/-connect ()
(when (null fg42/rigel/client-process)
(message "[Rigel] Connecting to server at %s:%s" fg42/rigel/host fg42/rigel/port)
(setq fg42/rigel/client-process
(make-network-process
:name "rigel-client"
:type nil
:server nil
:host fg42/rigel/host
:service fg42/rigel/port
:nowait t
:buffer (get-buffer-create "*Rigel*")
:sentinel 'fg42/rigel/-sentinel))))
(defun fg42/rigel/close ()
"Shutdown the Rigel client"
(interactive)
(message "[Rigel] Status: %s" (process-status "rigel-client"))
(when (process-status "rigel-client")
(process-send-string fg42/rigel/client-process (fg42/rigel/-pack "//close"))
(stop-process fg42/rigel/client-process))
(setq fg42/rigel/client-process nil)
(kill-buffer "*Rigel*"))
(defun fg42/rigel/read (txt)
"Send the given text to the Rigel server to read it out load"
(interactive "sRead: ")
(fg42/rigel/-connect)
(process-send-string fg42/rigel/client-process (fg42/rigel/-pack txt)))
(defun fg42/rigel/read-region (beg end)
"Send the text of selected region to the Rigel server to read it out load"
(interactive (if (use-region-p)
(list (region-beginning) (region-end))
(list nil nil)))
(fg42/rigel/read
(if (and beg end) (buffer-substring-no-properties beg end) "")))
(defun fg42/rigel/read-clipboard ()
"Send the text from the clipboadr to the Rigel server to read it out load"
(interactive)
(fg42/rigel/read
(or (current-kill 0) "")))
(provide 'cubes/rigel/core)
;;; core.el ends here