Update.
[elisp.git] / media.el
index 79313f2..c9d640d 100644 (file)
--- a/media.el
+++ b/media.el
@@ -1,4 +1,4 @@
-;; -*-Emacs-Lisp-*-
+;; -*- mode: emacs-lisp -*-
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; This program is free software; you can redistribute it and/or         ;;
@@ -53,7 +53,7 @@
 
 (defgroup media ()
   "Major mode to control media players"
-  :version "1.2.1")
+  :version "1.2.2")
 
 (defcustom media/player-api "media-mplayer"
   "The file to load for the abstract layer with the media player."
@@ -79,17 +79,17 @@ title to display in the list (convenient for internet radios)."
   :group 'media)
 
 (defcustom media/playlist-at-top nil
-  "Should the playlists be created at the top of the media buffer ?"
+  "Should the playlists be created at the top of the media buffer?"
   :type 'bool
   :group 'media)
 
 (defcustom media/add-current-song-to-interrupted-when-killing nil
-  "Should we save the current song with time in the Interrupted playlist ?"
+  "Should we save the current song with time in the Interrupted playlist?"
   :type 'bool
   :group 'media)
 
 (defcustom media/do-not-remove-nonexisting-entries nil
-  "Should we remove the entries corresponding to a non-existing file when saving the playlists ?"
+  "Should we remove the entries corresponding to a non-existing file when saving the playlists?"
   :type 'bool
   :group 'media)
 
@@ -99,12 +99,17 @@ title to display in the list (convenient for internet radios)."
   :group 'media)
 
 (defcustom media/continue-mode nil
-  "Should the player start the next song in the buffer when the current terminates ?"
+  "Should the player start the next song in the buffer when the current terminates?"
   :type 'boolean
   :group 'media)
 
+(defcustom media/continue-mode-hint "*"
+  "What to append to the MPlayer string when in repeat mode"
+  :type 'string
+  :group 'media)
+
 (defcustom media/expert nil
-  "Should the keymap help be shown ?"
+  "Should we bypass the keymap help when starting"
   :type 'boolean
   :group 'media)
 
@@ -112,6 +117,10 @@ title to display in the list (convenient for internet radios)."
   "Contains the name of the current file playing, the frequency in Hz
 and the bitrate. Should be nil if no information is available.")
 
+(defvar media/current-song-in-stream nil
+  "Contains the title of the current song playing, as it may be
+parsed from the stream.")
+
 (defvar media/buffer nil
   "The main buffer for the media player mode.")
 
@@ -243,6 +252,10 @@ and the bitrate. Should be nil if no information is available.")
 ;; Finding and playing URLs ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
+(defun media/reset-current-information ()
+    (setq media/current-information nil
+          media/current-song-in-stream nil))
+
 (defun media/format-url (url)
   (if (string-match "^file:.*/\\([^/]+\\)$" url)
       (match-string 1 url)
@@ -254,6 +267,7 @@ and the bitrate. Should be nil if no information is available.")
         (time (get-text-property position 'time)))
     (if (not url) (media/remove-highlight)
       (run-hook-with-args 'media/before-play-hook url)
+      (media/reset-current-information)
       (media/api/play url)
       ;; We keep the information of the url and the title
       (setq media/played-information (cons url (get-text-property position 'title)))
@@ -611,9 +625,11 @@ returns nil if no id3 tags could be found."
                  (title (or (and (consp c) (cdr c)) url)))
             (if (string-match "^\\(http\\|mms\\)://" url)
                 (media/insert-url (cons url title) 0)
-              (if (file-regular-p url) (media/insert-file url 0)
-                (if (file-directory-p url) (media/insert-dir url 0)
-                  (error "Unknown type `%s'" url))))))
+              (if (file-exists-p url)
+                  (if (file-regular-p url) (media/insert-file url 0)
+                    (if (file-directory-p url) (media/insert-dir url 0)
+                      (error "Unknown type `%s'" url))))
+              )))
         list))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -650,8 +666,7 @@ returns nil if no id3 tags could be found."
               (push (cons url (cons title time)) (cdr (assoc playlist list)))
               )))))
 
-    (save-excursion
-      (set-buffer (find-file-noselect media/playlist-file))
+    (with-current-buffer (find-file-noselect media/playlist-file)
       (erase-buffer)
       (mapc (lambda (x)
               (insert "PLAYLIST:" (car x) "\n")
@@ -721,7 +736,8 @@ returns nil if no id3 tags could be found."
             (propertize "\n" 'playlist name)
             )
     (setq media/active-playlist name)
-    (message "Playlist `%s' created" name)))
+    ;; (message "Playlist `%s' created" name)
+    ))
 
 (defun media/playlist-position (name)
   "Returns the position where the given playlist starts."
@@ -821,6 +837,7 @@ returns nil if no id3 tags could be found."
 
 (defun media/stop () (interactive)
   (message "Stop")
+  (media/reset-current-information)
   (media/api/stop))
 
 (defun media/queue-song-at-point ()
@@ -868,6 +885,19 @@ the 'Queue' playlist, and plays it if no song is currently playing."
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
+(defun media/player-error ()
+  (message "Player error")
+  (media/reset-current-information)
+  (media/remove-highlight))
+
+(defun media/song-terminates ()
+  (with-current-buffer media/buffer
+    (if media/continue-mode (media/play-next t)
+      (media/reset-current-information)
+      (media/remove-highlight))))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
 (defun media/switch-continue-mode ()
   "Switches between a mode which automatically chains files and a mode
 which stops when the songs ends."
@@ -878,15 +908,6 @@ which stops when the songs ends."
     (message "Continue mode switched off."))
   )
 
-(defun media/player-error ()
-  (message "Player error")
-  (media/remove-highlight))
-
-(defun media/song-terminates ()
-  (with-current-buffer media/buffer
-    (if media/continue-mode (media/play-next t)
-      (media/remove-highlight))))
-
 (defun media/duration-to-string (duration)
   (let ((sec (mod duration 60))
         (min (/ duration 60)))
@@ -900,7 +921,7 @@ which stops when the songs ends."
    (concat
     " "
     media/player-id
-    (if media/continue-mode "*")
+    (if media/continue-mode media/continue-mode-hint)
     " "
 
     (if media/current-information
@@ -921,10 +942,12 @@ which stops when the songs ends."
   "Print a message with informations about the song currently playing"
   (interactive)
   (if media/current-information
-      (message "Now playing %s (%dHz, %s, %dkbit/s)"
+      (message "Now playing %s %s(%dHz, %s, %dkbit/s)"
                (or (and (string= (car media/played-information) (nth 0 media/current-information))
                         (cdr media/played-information))
                    (replace-regexp-in-string "^.*/\\([^/]*\\)$" "\\1" (nth 0 media/current-information)))
+               ;; (if media/current-song-in-stream (concat "[" media/current-song-in-stream "] ") "")
+               (if media/current-song-in-stream (concat "| " media/current-song-in-stream " ") "")
                (nth 1 media/current-information)
                (if (= 2 (nth 2 media/current-information)) "stereo" "mono")
                (nth 3 media/current-information))
@@ -943,7 +966,7 @@ which stops when the songs ends."
 
   (unless (condition-case nil
               (media/save-playlists)
-            (error (not (y-or-n-p "Can not save the playlists. Still kill the media buffer ? "))))
+            (error (not (y-or-n-p "Can not save the playlists. Still kill the media buffer? "))))
     (kill-buffer media/buffer))
   )