Christophe Weblog Wiki Code Publications Music
handle the case when there are zero connected players
[squeeze-el.git] / squeeze.el
index 977813b28fc24c118fc0dafd600d7c7901250723..cfbca135f33a6ea7cd560bcca0592ebe22b0ec5d 100644 (file)
     (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.\\<squeeze-mode-map>"
   (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)
+    (when (> count 0)
+      (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))