From d1dedee56688a622a2c3961debfa5dd91e23b013 Mon Sep 17 00:00:00 2001 From: Tim Vaughan Date: Tue, 15 Jun 2021 18:10:48 +0200 Subject: [PATCH] Better message handling. --- lirc.el | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/lirc.el b/lirc.el index c9dc91b..db78af8 100644 --- a/lirc.el +++ b/lirc.el @@ -51,6 +51,15 @@ (defcustom lirc-prompt "> " "Prompt.") +;;; Faces +;; + +;;; Global variables +;; + +(defvar lirc-current-channel nil) +(defvar lirc-channel-list nil) + (defvar lirc-response "") (defun lirc-filter (proc string) @@ -79,12 +88,19 @@ (list (lirc-as-string tags) (lirc-as-string src) (upcase (lirc-as-string cmd)) - (mapcar #'lirc-as-string params))) + (mapcar #'lirc-as-string + (if (and params (listp (elt params 0))) + (elt params 0) + params)))) (defun lirc-msg-tags (msg) (elt msg 0)) (defun lirc-msg-src (msg) (elt msg 1)) (defun lirc-msg-cmd (msg) (elt msg 2)) (defun lirc-msg-params (msg) (elt msg 3)) +(defun lirc-msg-trail (msg) + (let ((params (lirc-msg-params msg))) + (if params + (elt params (- (length params) 1))))) (defvar lirc-msg-regex (rx @@ -112,34 +128,40 @@ (apply #'lirc-msg (append (list tags src cmd) params))) (error "Failed to parse string " string))) +(defun lirc--filtered-join (&rest args) + (string-join (seq-filter (lambda (el) el) args) " ")) + (defun lirc-msg->string (msg) (let ((tags (lirc-msg-tags msg)) (src (lirc-msg-src msg)) (cmd (lirc-msg-cmd msg)) (params (lirc-msg-params msg))) - (concat - (if tags (concat "@" tags " ") "") - (if src (concat ":" src " ") "") - cmd " " + (lirc--filtered-join + (if tags (concat "@" tags) nil) + (if src (concat ":" src) nil) + cmd (if (> (length params) 1) (string-join (seq-take params (- (length params) 1)) " ") - "") + nil) (if (> (length params) 0) - (concat " :" (elt params (- (length params) 1))))))) + (concat ":" (elt params (- (length params) 1))) + nil)))) (defun lirc-display-string (string) (with-current-buffer "*lirc*" (let ((inhibit-read-only t)) (save-excursion (goto-char (point-max)) - (insert string "\n"))))) + (insert (propertize (concat string "\n") 'read-only t)))))) (defun lirc-process-msg-string (string) - (let ((msg (lirc-string->msg string))) + (let* ((msg (lirc-string->msg string))) (cond ((equal (lirc-msg-cmd msg) "PING") (lirc-send-msg (lirc-msg nil nil "PONG" (lirc-msg-params msg)))) + ((string-match (rx (= 3 digit)) (lirc-msg-cmd msg)) + (lirc-display-string (string-join (cdr (lirc-msg-params msg)) " "))) (t (lirc-display-string (lirc-msg->string msg)))))) -- 2.20.1