X-Git-Url: http://christophe.rhodes.io/gitweb/?p=squeeze-el.git;a=blobdiff_plain;f=squeeze.el;h=25491596d5633e9b41ab235b8b1135376b9011b5;hp=cdfb86d946b900ca24736058948df3cbd7e88d4a;hb=0e4ee58cdc46656c8ffaa2e9e0b642c88036b980;hpb=74f3e40d940c863a26407a0cd954a11d84bc86f8 diff --git a/squeeze.el b/squeeze.el index cdfb86d..2549159 100644 --- a/squeeze.el +++ b/squeeze.el @@ -15,8 +15,23 @@ (define-key map (kbd "TAB") 'completion-at-point) map)) +(defun squeeze-unhex-string (string) + (with-temp-buffer + (let ((case-fold-search t) + (start 0)) + (while (string-match "%[0-9a-f][0-9a-f]" string start) + (let* ((s (match-beginning 0)) + (ch1 (url-unhex (elt string (+ s 1)))) + (code (+ (* 16 ch1) + (url-unhex (elt string (+ s 2)))))) + (insert (substring string start s) + (byte-to-string code)) + (setq start (match-end 0)))) + (insert (substring string start))) + (buffer-string))) + (defun squeeze-unhex-and-decode-utf8-string (string) - (decode-coding-string (url-unhex-string string) 'utf-8)) + (decode-coding-string (squeeze-unhex-string string) 'utf-8)) (define-derived-mode squeeze-mode comint-mode "Squeeze" "Major mode for interacting with the Squeezebox Server CLI.\\" @@ -39,15 +54,25 @@ (defvar squeeze-control-inhibit-display nil) -(defun squeeze-update-state (string) - (let (done-something) - (dolist (line (split-string string "\n")) - (when (squeeze-update-state-from-line line) - (setq done-something t))) - (when done-something - (unless squeeze-control-inhibit-display - (squeeze-control-display-players)))) - string) +(lexical-let ((buffer (get-buffer-create " *squeeze-update-state*"))) + (defun squeeze-update-state (string) + ;; FIXME: we could make this a lot more elegant by using the + ;; buffer abstraction more + (if (cl-position ?\n string) + (let (done-something) + (with-current-buffer buffer + (insert string) + (setq string (buffer-string)) + (erase-buffer)) + (dolist (line (split-string string "\n")) + (when (squeeze-update-state-from-line line) + (setq done-something t))) + (when done-something + (unless squeeze-control-inhibit-display + (squeeze-control-display-players)))) + (with-current-buffer buffer + (insert string))) + string)) (defconst squeeze-player-line-regexp "^\\(\\(?:[0-9a-f]\\{2\\}%3A\\)\\{5\\}[0-9a-f]\\{2\\}\\) ") @@ -329,15 +354,20 @@ playerindex playerid uuid ip name model isplayer displaytype canpoweroff connected power volume) (defun squeeze-string-plistify (string start end) + (unless end + (setq end (length string))) (save-match-data (let (result) (loop - (if (string-match "\\([a-z]+\\)%3A\\([^ \n]+\\)" string start) + (if (string-match "\\([a-z_]+\\)%3A\\([^ \n]+\\)" string start) (let ((mend (match-end 0))) (when (> mend end) (return)) (push (intern (format ":%s" (substring string (match-beginning 1) (match-end 1)))) result) - (push (url-unhex-string (substring string (match-beginning 2) (match-end 2))) result) + (push (decode-coding-string + (url-unhex-string (substring string (match-beginning 2) (match-end 2))) + 'utf-8) + result) (setq start mend)) (return))) (nreverse result)))) @@ -348,20 +378,30 @@ (when startpos (squeeze-string-plistify string startpos (length string))))) +(defun squeeze-parse-count (string) + (save-match-data + (let ((countpos (string-match "count%3A\\([0-9]*\\)\\>" string))) + (if countpos + (string-to-number + (substring string (match-beginning 1) (match-end 1))) + (let ((kind + (progn (string-match "^\\([a-z]*\\) " string) + (substring string (match-beginning 1) (match-end 1))))) + (message "no count found in %s line" kind) + nil))))) + (defun squeeze-parse-players-line (string) - (let ((countpos (string-match " count%3A\\([0-9]\\) " string)) - (startpos (match-end 0))) - (unless countpos - (message "no count found in players line")) - (let ((count (string-to-number (substring string (match-beginning 1) (match-end 1)))) - result endpos) - (dotimes (i (1- count)) - (setq endpos (progn (string-match " connected%3A[0-1] " string startpos) - (match-end 0))) - (push (apply 'squeeze-make-player (squeeze-string-plistify string startpos endpos)) result) - (setq startpos endpos)) - (push (apply 'squeeze-make-player (squeeze-string-plistify string startpos (length string))) result) - result))) + (let ((count (squeeze-parse-count string)) + (startpos (string-match "playerindex" string)) + result endpos) + (dotimes (i (1- count)) + (setq endpos (progn (string-match " connected%3A[0-1] " string startpos) + (match-end 0))) + (push (apply 'squeeze-make-player (squeeze-string-plistify string startpos endpos)) result) + (setq startpos endpos)) + (push (apply 'squeeze-make-player (squeeze-string-plistify string startpos (length string))) result) + result)) + (defun squeeze-complete-command-at-point () (save-excursion @@ -399,7 +439,7 @@ "serverstatus" "status" "displaystatus" "readdirectory" ;; Notifications - + ;; Alarm commands and queries "alarm" "alarms"