| 1 | ;;; mpc.el --- A client for the Music Player Daemon -*- coding: utf-8; lexical-binding: t -*- |
| 2 | |
| 3 | ;; Copyright (C) 2006-2014 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Author: Stefan Monnier <monnier@iro.umontreal.ca> |
| 6 | ;; Keywords: multimedia |
| 7 | |
| 8 | ;; This file is part of GNU Emacs. |
| 9 | |
| 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 11 | ;; it under the terms of the GNU General Public License as published by |
| 12 | ;; the Free Software Foundation, either version 3 of the License, or |
| 13 | ;; (at your option) any later version. |
| 14 | |
| 15 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | ;; GNU General Public License for more details. |
| 19 | |
| 20 | ;; You should have received a copy of the GNU General Public License |
| 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | ;;; Commentary: |
| 24 | |
| 25 | ;; This is an Emacs front end to the Music Player Daemon. |
| 26 | |
| 27 | ;; It mostly provides a browser inspired from Rhythmbox for your music |
| 28 | ;; collection and also allows you to play the music you select. The basic |
| 29 | ;; interface is somewhat unusual in that it does not focus on the |
| 30 | ;; playlist as much as on the browser. |
| 31 | ;; I play albums rather than songs and thus don't have much need for |
| 32 | ;; playlists, and it shows. Playlist support exists, but is still limited. |
| 33 | |
| 34 | ;; Bugs: |
| 35 | |
| 36 | ;; - when reaching end/start of song while ffwd/rewind, it may get wedged, |
| 37 | ;; signal an error, ... or when mpc-next/prev is called while ffwd/rewind. |
| 38 | ;; - MPD errors are not reported to the user. |
| 39 | |
| 40 | ;; Todo: |
| 41 | |
| 42 | ;; - add bindings/buttons/menuentries for the various commands. |
| 43 | ;; - mpc-undo |
| 44 | ;; - visual feedback for drag'n'drop |
| 45 | ;; - display/set `repeat' and `random' state (and maybe also `crossfade'). |
| 46 | ;; - allow multiple *mpc* sessions in the same Emacs to control different mpds. |
| 47 | ;; - look for .folder.png (freedesktop) or folder.jpg (XP) as well. |
| 48 | ;; - fetch album covers and lyrics from the web? |
| 49 | ;; - improve MPC-Status: better volume control, add a way to show/hide the |
| 50 | ;; rest, plus add the buttons currently in the toolbar. |
| 51 | ;; - improve mpc-songs-mode's header-line column-headings so they can be |
| 52 | ;; dragged to resize. |
| 53 | ;; - allow selecting several entries by drag-mouse. |
| 54 | ;; - poll less often |
| 55 | ;; - use the `idle' command |
| 56 | ;; - do the time-ticking locally (and sync every once in a while) |
| 57 | ;; - look at the end of play time to make sure we notice the end |
| 58 | ;; as soon as possible |
| 59 | ;; - better volume widget. |
| 60 | ;; - add synthesized tags. |
| 61 | ;; e.g. pseudo-artist = artist + composer + performer. |
| 62 | ;; e.g. pseudo-performer = performer or artist |
| 63 | ;; e.g. rewrite artist "Foo bar & baz" to "Foo bar". |
| 64 | ;; e.g. filename regexp -> compilation flag |
| 65 | ;; - window/buffer management. |
| 66 | ;; - menubar, tooltips, ... |
| 67 | ;; - add mpc-describe-song, mpc-describe-album, ... |
| 68 | ;; - add import/export commands (especially export to an MP3 player). |
| 69 | ;; - add a real notion of album (as opposed to just album-name): |
| 70 | ;; if all songs with same album-name have same artist -> it's an album |
| 71 | ;; else it's either several albums or a compilation album (or both), |
| 72 | ;; in which case we could use heuristics or user provided info: |
| 73 | ;; - if the user followed the 1-album = 1-dir idea, then we can group songs |
| 74 | ;; by their directory to create albums. |
| 75 | ;; - if a `compilation' flag is available, and if <=1 of the songs have it |
| 76 | ;; set, then we can group songs by their artist to create albums. |
| 77 | ;; - if two songs have the same track-nb and disk-nb, they're not in the |
| 78 | ;; same album. So from the set of songs with identical album names, we |
| 79 | ;; can get a lower bound on the number of albums involved, and then see |
| 80 | ;; which of those may be non-compilations, etc... |
| 81 | ;; - use a special directory name for compilations. |
| 82 | ;; - ask the web ;-) |
| 83 | |
| 84 | ;;; Code: |
| 85 | |
| 86 | ;; Prefixes used in this code: |
| 87 | ;; mpc-proc : management of connection (in/out formatting, ...) |
| 88 | ;; mpc-status : auto-updated status info |
| 89 | ;; mpc-volume : stuff handling the volume widget |
| 90 | ;; mpc-cmd : mpdlib abstraction |
| 91 | |
| 92 | ;; UI-commands : mpc- |
| 93 | ;; internal : mpc-- |
| 94 | |
| 95 | (eval-when-compile (require 'cl-lib)) |
| 96 | |
| 97 | (defgroup mpc () |
| 98 | "Client for the Music Player Daemon (mpd)." |
| 99 | :prefix "mpc-" |
| 100 | :group 'multimedia |
| 101 | :group 'applications) |
| 102 | |
| 103 | (defcustom mpc-browser-tags '(Genre Artist|Composer|Performer |
| 104 | Album|Playlist) |
| 105 | "Tags for which a browser buffer should be created by default." |
| 106 | ;; FIXME: provide a list of tags, for completion. |
| 107 | :type '(repeat symbol)) |
| 108 | |
| 109 | ;;; Misc utils ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 110 | |
| 111 | (defun mpc-assq-all (key alist) |
| 112 | (let ((res ()) val) |
| 113 | (dolist (elem alist) |
| 114 | (if (and (eq (car elem) key) |
| 115 | (not (member (setq val (cdr elem)) res))) |
| 116 | (push val res))) |
| 117 | (nreverse res))) |
| 118 | |
| 119 | (defun mpc-union (&rest lists) |
| 120 | (let ((res (nreverse (pop lists)))) |
| 121 | (dolist (list lists) |
| 122 | (let ((seen res)) ;Don't remove duplicates within each list. |
| 123 | (dolist (elem list) |
| 124 | (unless (member elem seen) (push elem res))))) |
| 125 | (nreverse res))) |
| 126 | |
| 127 | (defun mpc-intersection (l1 l2 &optional selectfun) |
| 128 | "Return L1 after removing all elements not found in L2. |
| 129 | If SELECTFUN is non-nil, elements aren't compared directly, but instead |
| 130 | they are passed through SELECTFUN before comparison." |
| 131 | (let ((res ())) |
| 132 | (if selectfun (setq l2 (mapcar selectfun l2))) |
| 133 | (dolist (elem l1) |
| 134 | (when (member (if selectfun (funcall selectfun elem) elem) l2) |
| 135 | (push elem res))) |
| 136 | (nreverse res))) |
| 137 | |
| 138 | (defun mpc-event-set-point (event) |
| 139 | (condition-case nil (posn-set-point (event-end event)) |
| 140 | (error (condition-case nil (mouse-set-point event) |
| 141 | (error nil))))) |
| 142 | |
| 143 | (defun mpc-compare-strings (str1 str2 &optional ignore-case) |
| 144 | "Compare strings STR1 and STR2. |
| 145 | Contrary to `compare-strings', this tries to get numbers sorted |
| 146 | numerically rather than lexicographically." |
| 147 | (let ((res (compare-strings str1 nil nil str2 nil nil ignore-case))) |
| 148 | (if (not (integerp res)) res |
| 149 | (let ((index (1- (abs res)))) |
| 150 | (if (or (>= index (length str1)) (>= index (length str2))) |
| 151 | res |
| 152 | (let ((digit1 (memq (aref str1 index) |
| 153 | '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))) |
| 154 | (digit2 (memq (aref str2 index) |
| 155 | '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)))) |
| 156 | (if digit1 |
| 157 | (if digit2 |
| 158 | (let ((num1 (progn (string-match "[0-9]+" str1 index) |
| 159 | (match-string 0 str1))) |
| 160 | (num2 (progn (string-match "[0-9]+" str2 index) |
| 161 | (match-string 0 str2)))) |
| 162 | (cond |
| 163 | ;; Here we presume that leading zeroes are only used |
| 164 | ;; for same-length numbers. So we'll incorrectly |
| 165 | ;; consider that "000" comes after "01", but I don't |
| 166 | ;; think it matters. |
| 167 | ((< (length num1) (length num2)) (- (abs res))) |
| 168 | ((> (length num1) (length num2)) (abs res)) |
| 169 | ((< (string-to-number num1) (string-to-number num2)) |
| 170 | (- (abs res))) |
| 171 | (t (abs res)))) |
| 172 | ;; "1a" comes before "10", but "0" comes before "a". |
| 173 | (if (and (not (zerop index)) |
| 174 | (memq (aref str1 (1- index)) |
| 175 | '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))) |
| 176 | (abs res) |
| 177 | (- (abs res)))) |
| 178 | (if digit2 |
| 179 | ;; "1a" comes before "10", but "0" comes before "a". |
| 180 | (if (and (not (zerop index)) |
| 181 | (memq (aref str1 (1- index)) |
| 182 | '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))) |
| 183 | (- (abs res)) |
| 184 | (abs res)) |
| 185 | res)))))))) |
| 186 | |
| 187 | (define-obsolete-function-alias 'mpc-string-prefix-p 'string-prefix-p "24.3") |
| 188 | |
| 189 | ;; This can speed up mpc--song-search significantly. The table may grow |
| 190 | ;; very large, tho. It's only bounded by the fact that it gets flushed |
| 191 | ;; whenever the connection is established; which seems to work OK thanks |
| 192 | ;; to the fact that MPD tends to disconnect fairly often, although our |
| 193 | ;; constant polling often prevents disconnection. |
| 194 | (defvar mpc--find-memoize (make-hash-table :test 'equal)) ;; :weakness t |
| 195 | (defvar-local mpc-tag nil) |
| 196 | |
| 197 | ;;; Support for the actual connection and MPD command execution ;;;;;;;;;;;; |
| 198 | |
| 199 | (defcustom mpc-host |
| 200 | (concat (or (getenv "MPD_HOST") "localhost") |
| 201 | (if (getenv "MPD_PORT") (concat ":" (getenv "MPD_PORT")))) |
| 202 | "Host (and port) where the Music Player Daemon is running. The |
| 203 | format is \"HOST\", \"HOST:PORT\", \"PASSWORD@HOST\" or |
| 204 | \"PASSWORD@HOST:PORT\" where PASSWORD defaults to no password, PORT |
| 205 | defaults to 6600 and HOST defaults to localhost." |
| 206 | :type 'string) |
| 207 | |
| 208 | (defvar mpc-proc nil) |
| 209 | |
| 210 | (defconst mpc--proc-end-re "^\\(?:OK\\(?: MPD .*\\)?\\|ACK \\(.*\\)\\)\n") |
| 211 | |
| 212 | (define-error 'mpc-proc-error "MPD error") |
| 213 | |
| 214 | (defun mpc--debug (format &rest args) |
| 215 | (if (get-buffer "*MPC-debug*") |
| 216 | (with-current-buffer "*MPC-debug*" |
| 217 | (goto-char (point-max)) |
| 218 | (insert-before-markers ;So it scrolls. |
| 219 | (replace-regexp-in-string "\n" "\n " |
| 220 | (apply 'format format args)) |
| 221 | "\n")))) |
| 222 | |
| 223 | (defun mpc--proc-filter (proc string) |
| 224 | (mpc--debug "Receive \"%s\"" string) |
| 225 | (with-current-buffer (process-buffer proc) |
| 226 | (if (process-get proc 'ready) |
| 227 | (if nil ;; (string-match "\\`\\(OK\n\\)+\\'" string) |
| 228 | ;; I haven't figured out yet why I get those extraneous OKs, |
| 229 | ;; so I'll just ignore them for now. |
| 230 | nil |
| 231 | (delete-process proc) |
| 232 | (set-process-buffer proc nil) |
| 233 | (pop-to-buffer (clone-buffer)) |
| 234 | (error "MPD output while idle!?")) |
| 235 | (save-excursion |
| 236 | (let ((start (or (marker-position (process-mark proc)) (point-min)))) |
| 237 | (goto-char start) |
| 238 | (insert string) |
| 239 | (move-marker (process-mark proc) (point)) |
| 240 | (beginning-of-line) |
| 241 | (when (and (< start (point)) |
| 242 | (re-search-backward mpc--proc-end-re start t)) |
| 243 | (process-put proc 'ready t) |
| 244 | (unless (eq (match-end 0) (point-max)) |
| 245 | (error "Unexpected trailing text")) |
| 246 | (let ((error-text (match-string 1))) |
| 247 | (delete-region (point) (point-max)) |
| 248 | (let ((callback (process-get proc 'callback))) |
| 249 | (process-put proc 'callback nil) |
| 250 | (if error-text |
| 251 | (process-put proc 'mpc-proc-error error-text)) |
| 252 | (funcall callback))))))))) |
| 253 | |
| 254 | (defun mpc--proc-connect (host) |
| 255 | (let ((port 6600) |
| 256 | pass) |
| 257 | |
| 258 | (when (string-match "\\`\\(?:\\(.*\\)@\\)?\\(.*?\\)\\(?::\\(.*\\)\\)?\\'" |
| 259 | host) |
| 260 | (let ((v (match-string 1 host))) |
| 261 | (when (and (stringp v) (not (string= "" v))) |
| 262 | (setq pass v))) |
| 263 | (let ((v (match-string 3 host))) |
| 264 | (setq host (match-string 2 host)) |
| 265 | (when (and (stringp v) (not (string= "" v))) |
| 266 | (setq port |
| 267 | (if (string-match "[^[:digit:]]" v) |
| 268 | (string-to-number v) |
| 269 | v))))) |
| 270 | |
| 271 | (mpc--debug "Connecting to %s:%s..." host port) |
| 272 | (with-current-buffer (get-buffer-create (format " *mpc-%s:%s*" host port)) |
| 273 | ;; (pop-to-buffer (current-buffer)) |
| 274 | (let (proc) |
| 275 | (while (and (setq proc (get-buffer-process (current-buffer))) |
| 276 | (progn ;; (debug) |
| 277 | (delete-process proc))))) |
| 278 | (erase-buffer) |
| 279 | (let* ((coding-system-for-read 'utf-8-unix) |
| 280 | (coding-system-for-write 'utf-8-unix) |
| 281 | (proc (condition-case err |
| 282 | (open-network-stream "MPC" (current-buffer) host port) |
| 283 | (error (user-error (error-message-string err)))))) |
| 284 | (when (processp mpc-proc) |
| 285 | ;; Inherit the properties of the previous connection. |
| 286 | (let ((plist (process-plist mpc-proc))) |
| 287 | (while plist (process-put proc (pop plist) (pop plist))))) |
| 288 | (mpc-proc-buffer proc 'mpd-commands (current-buffer)) |
| 289 | (process-put proc 'callback 'ignore) |
| 290 | (process-put proc 'ready nil) |
| 291 | (clrhash mpc--find-memoize) |
| 292 | (set-process-filter proc 'mpc--proc-filter) |
| 293 | (set-process-sentinel proc 'ignore) |
| 294 | (set-process-query-on-exit-flag proc nil) |
| 295 | ;; This may be called within a process filter ;-( |
| 296 | (with-local-quit (mpc-proc-sync proc)) |
| 297 | (setq mpc-proc proc) |
| 298 | (when pass |
| 299 | (mpc-proc-cmd (list "password" pass) nil)))))) |
| 300 | |
| 301 | (defun mpc--proc-quote-string (s) |
| 302 | (if (numberp s) (number-to-string s) |
| 303 | (setq s (replace-regexp-in-string "[\"\\]" "\\\\\\&" s)) |
| 304 | (if (string-match " " s) (concat "\"" s "\"") s))) |
| 305 | |
| 306 | (defconst mpc--proc-alist-to-alists-starters '(file directory)) |
| 307 | |
| 308 | (defun mpc--proc-alist-to-alists (alist) |
| 309 | (cl-assert (or (null alist) |
| 310 | (memq (caar alist) mpc--proc-alist-to-alists-starters))) |
| 311 | (let ((starter (caar alist)) |
| 312 | (alists ()) |
| 313 | tmp) |
| 314 | (dolist (pair alist) |
| 315 | (when (eq (car pair) starter) |
| 316 | (if tmp (push (nreverse tmp) alists)) |
| 317 | (setq tmp ())) |
| 318 | (push pair tmp)) |
| 319 | (if tmp (push (nreverse tmp) alists)) |
| 320 | (nreverse alists))) |
| 321 | |
| 322 | (defun mpc-proc (&optional restart) |
| 323 | (unless (and mpc-proc |
| 324 | (buffer-live-p (process-buffer mpc-proc)) |
| 325 | (not (and restart |
| 326 | (memq (process-status mpc-proc) '(closed))))) |
| 327 | (mpc--proc-connect mpc-host)) |
| 328 | mpc-proc) |
| 329 | |
| 330 | (defun mpc-proc-check (proc) |
| 331 | (let ((error-text (process-get proc 'mpc-proc-error))) |
| 332 | (when error-text |
| 333 | (process-put proc 'mpc-proc-error nil) |
| 334 | (signal 'mpc-proc-error error-text)))) |
| 335 | |
| 336 | (defun mpc-proc-sync (&optional proc) |
| 337 | "Wait for MPC process until it is idle again. |
| 338 | Return the buffer in which the process is/was running." |
| 339 | (unless proc (setq proc (mpc-proc))) |
| 340 | (unwind-protect |
| 341 | (progn |
| 342 | (while (and (not (process-get proc 'ready)) |
| 343 | (accept-process-output proc))) |
| 344 | (mpc-proc-check proc) |
| 345 | (if (process-get proc 'ready) (process-buffer proc) |
| 346 | (error "No response from MPD"))) |
| 347 | (unless (process-get proc 'ready) |
| 348 | ;; (debug) |
| 349 | (message "Killing hung process") |
| 350 | (delete-process proc)))) |
| 351 | |
| 352 | (defun mpc-proc-cmd (cmd &optional callback) |
| 353 | "Send command CMD to the MPD server. |
| 354 | If CALLBACK is nil, wait for the command to finish before returning, |
| 355 | otherwise return immediately and call CALLBACK with no argument |
| 356 | when the command terminates. |
| 357 | CMD can be a string which is passed as-is to MPD or a list of strings |
| 358 | which will be concatenated with proper quoting before passing them to MPD." |
| 359 | (let ((proc (mpc-proc 'restart))) |
| 360 | (if (and callback (not (process-get proc 'ready))) |
| 361 | (let ((old (process-get proc 'callback))) |
| 362 | (process-put proc 'callback |
| 363 | (lambda () |
| 364 | (funcall old) |
| 365 | (mpc-proc-cmd cmd callback)))) |
| 366 | ;; Wait for any pending async command to terminate. |
| 367 | (mpc-proc-sync proc) |
| 368 | (process-put proc 'ready nil) |
| 369 | (with-current-buffer (process-buffer proc) |
| 370 | (erase-buffer) |
| 371 | (mpc--debug "Send \"%s\"" cmd) |
| 372 | (process-send-string |
| 373 | proc (concat (if (stringp cmd) cmd |
| 374 | (mapconcat 'mpc--proc-quote-string cmd " ")) |
| 375 | "\n"))) |
| 376 | (if callback |
| 377 | ;; (let ((buf (current-buffer))) |
| 378 | (process-put proc 'callback |
| 379 | callback |
| 380 | ;; (lambda () |
| 381 | ;; (funcall callback |
| 382 | ;; (prog1 (current-buffer) |
| 383 | ;; (set-buffer buf))))) |
| 384 | ) |
| 385 | ;; If `callback' is nil, we're executing synchronously. |
| 386 | (process-put proc 'callback 'ignore) |
| 387 | ;; This returns the process's buffer. |
| 388 | (mpc-proc-sync proc))))) |
| 389 | |
| 390 | ;; This function doesn't exist in Emacs-21. |
| 391 | ;; (put 'mpc-proc-cmd-list 'byte-optimizer 'byte-optimize-pure-func) |
| 392 | (defun mpc-proc-cmd-list (cmds) |
| 393 | (concat "command_list_begin\n" |
| 394 | (mapconcat (lambda (cmd) |
| 395 | (if (stringp cmd) cmd |
| 396 | (mapconcat 'mpc--proc-quote-string cmd " "))) |
| 397 | cmds |
| 398 | "\n") |
| 399 | "\ncommand_list_end")) |
| 400 | |
| 401 | (defun mpc-proc-cmd-list-ok () |
| 402 | ;; To implement this, we'll need to tweak the process filter since we'd |
| 403 | ;; then sometimes get "trailing" text after "OK\n". |
| 404 | (error "Not implemented yet")) |
| 405 | |
| 406 | (defun mpc-proc-buf-to-alist (&optional buf) |
| 407 | (with-current-buffer (or buf (current-buffer)) |
| 408 | (let ((res ())) |
| 409 | (goto-char (point-min)) |
| 410 | (while (re-search-forward "^\\([^:]+\\): \\(.*\\)\n" nil t) |
| 411 | (push (cons (intern (match-string 1)) (match-string 2)) res)) |
| 412 | (nreverse res)))) |
| 413 | |
| 414 | (defun mpc-proc-buf-to-alists (buf) |
| 415 | (mpc--proc-alist-to-alists (mpc-proc-buf-to-alist buf))) |
| 416 | |
| 417 | (defun mpc-proc-cmd-to-alist (cmd &optional callback) |
| 418 | (if callback |
| 419 | (let ((buf (current-buffer))) |
| 420 | (mpc-proc-cmd cmd (lambda () |
| 421 | (funcall callback (prog1 (mpc-proc-buf-to-alist |
| 422 | (current-buffer)) |
| 423 | (set-buffer buf)))))) |
| 424 | ;; (let ((res nil)) |
| 425 | ;; (mpc-proc-cmd-to-alist cmd (lambda (alist) (setq res alist))) |
| 426 | ;; (mpc-proc-sync) |
| 427 | ;; res) |
| 428 | (mpc-proc-buf-to-alist (mpc-proc-cmd cmd)))) |
| 429 | |
| 430 | (defun mpc-proc-tag-string-to-sym (tag) |
| 431 | (intern (capitalize tag))) |
| 432 | |
| 433 | (defun mpc-proc-buffer (proc use &optional buffer) |
| 434 | (let* ((bufs (process-get proc 'buffers)) |
| 435 | (buf (cdr (assoc use bufs)))) |
| 436 | (cond |
| 437 | ((and buffer (buffer-live-p buf) (not (eq buffer buf))) |
| 438 | (error "Duplicate MPC buffer for %s" use)) |
| 439 | (buffer |
| 440 | (if buf |
| 441 | (setcdr (assoc use bufs) buffer) |
| 442 | (process-put proc 'buffers (cons (cons use buffer) bufs)))) |
| 443 | (t buf)))) |
| 444 | |
| 445 | ;;; Support for regularly updated current status information ;;;;;;;;;;;;;;; |
| 446 | |
| 447 | ;; Exported elements: |
| 448 | ;; `mpc-status' holds the uptodate data. |
| 449 | ;; `mpc-status-callbacks' holds the registered callback functions. |
| 450 | ;; `mpc-status-refresh' forces a refresh of the data. |
| 451 | ;; `mpc-status-stop' stops the automatic updating. |
| 452 | |
| 453 | (defvar mpc-status nil) |
| 454 | (defvar mpc-status-callbacks |
| 455 | '((state . mpc--status-timers-refresh) |
| 456 | ;; (song . mpc--queue-refresh) |
| 457 | ;; (state . mpc--queue-refresh) ;To detect the end of the last song. |
| 458 | (state . mpc--faster-toggle-refresh) ;Only ffwd/rewind while play/pause. |
| 459 | (volume . mpc-volume-refresh) |
| 460 | (file . mpc-songpointer-refresh) |
| 461 | ;; The song pointer may need updating even if the file doesn't change, |
| 462 | ;; if the same song appears multiple times in a row. |
| 463 | (song . mpc-songpointer-refresh) |
| 464 | (updating_db . mpc-updated-db) |
| 465 | (updating_db . mpc--status-timers-refresh) |
| 466 | (t . mpc-current-refresh)) |
| 467 | "Alist associating properties to the functions that care about them. |
| 468 | Each entry has the form (PROP . FUN) where PROP can be t to mean |
| 469 | to call FUN for any change whatsoever.") |
| 470 | |
| 471 | (defun mpc--status-callback () |
| 472 | (let ((old-status mpc-status)) |
| 473 | ;; Update the alist. |
| 474 | (setq mpc-status (mpc-proc-buf-to-alist)) |
| 475 | (cl-assert mpc-status) |
| 476 | (unless (equal old-status mpc-status) |
| 477 | ;; Run the relevant refresher functions. |
| 478 | (dolist (pair mpc-status-callbacks) |
| 479 | (when (or (eq t (car pair)) |
| 480 | (not (equal (cdr (assq (car pair) old-status)) |
| 481 | (cdr (assq (car pair) mpc-status))))) |
| 482 | (funcall (cdr pair))))))) |
| 483 | |
| 484 | (defvar mpc--status-timer nil) |
| 485 | (defun mpc--status-timer-start () |
| 486 | (add-hook 'pre-command-hook 'mpc--status-timer-stop) |
| 487 | (unless mpc--status-timer |
| 488 | (setq mpc--status-timer (run-with-timer 1 1 'mpc--status-timer-run)))) |
| 489 | (defun mpc--status-timer-stop () |
| 490 | (when mpc--status-timer |
| 491 | (cancel-timer mpc--status-timer) |
| 492 | (setq mpc--status-timer nil))) |
| 493 | (defun mpc--status-timer-run () |
| 494 | (with-demoted-errors "MPC: %S" |
| 495 | (when (process-get (mpc-proc) 'ready) |
| 496 | (let* ((buf (mpc-proc-buffer (mpc-proc) 'status)) |
| 497 | (win (get-buffer-window buf t))) |
| 498 | (if (not win) |
| 499 | (mpc--status-timer-stop) |
| 500 | (with-local-quit (mpc-status-refresh))))))) |
| 501 | |
| 502 | (defvar mpc--status-idle-timer nil) |
| 503 | (defun mpc--status-idle-timer-start () |
| 504 | (when mpc--status-idle-timer |
| 505 | ;; Turn it off even if we'll start it again, in case it changes the delay. |
| 506 | (cancel-timer mpc--status-idle-timer)) |
| 507 | (setq mpc--status-idle-timer |
| 508 | (run-with-idle-timer 1 t 'mpc--status-idle-timer-run)) |
| 509 | ;; Typically, the idle timer is started from the mpc--status-callback, |
| 510 | ;; which is run asynchronously while we're already idle (we typically |
| 511 | ;; just started idling), so the timer itself will only be run the next |
| 512 | ;; time we idle :-( |
| 513 | ;; To work around that, we immediately start the repeat timer. |
| 514 | (mpc--status-timer-start)) |
| 515 | (defun mpc--status-idle-timer-stop (&optional really) |
| 516 | (when mpc--status-idle-timer |
| 517 | ;; Turn it off even if we'll start it again, in case it changes the delay. |
| 518 | (cancel-timer mpc--status-idle-timer)) |
| 519 | (setq mpc--status-idle-timer |
| 520 | (unless really |
| 521 | ;; We don't completely stop the timer, so that if some other MPD |
| 522 | ;; client starts playback, we may get a chance to notice it. |
| 523 | (run-with-idle-timer 10 t 'mpc--status-idle-timer-run)))) |
| 524 | (defun mpc--status-idle-timer-run () |
| 525 | (mpc--status-timer-start) |
| 526 | (mpc--status-timer-run)) |
| 527 | |
| 528 | (defun mpc--status-timers-refresh () |
| 529 | "Start/stop the timers according to whether a song is playing." |
| 530 | (if (or (member (cdr (assq 'state mpc-status)) '("play")) |
| 531 | (cdr (assq 'updating_db mpc-status))) |
| 532 | (mpc--status-idle-timer-start) |
| 533 | (mpc--status-idle-timer-stop) |
| 534 | (mpc--status-timer-stop))) |
| 535 | |
| 536 | (defun mpc-status-refresh (&optional callback) |
| 537 | "Refresh `mpc-status'." |
| 538 | (let ((cb callback)) |
| 539 | (mpc-proc-cmd (mpc-proc-cmd-list '("status" "currentsong")) |
| 540 | (lambda () |
| 541 | (mpc--status-callback) |
| 542 | (if cb (funcall cb)))))) |
| 543 | |
| 544 | (defun mpc-status-stop () |
| 545 | "Stop the autorefresh of `mpc-status'. |
| 546 | This is normally used only when quitting MPC. |
| 547 | Any call to `mpc-status-refresh' may cause it to be restarted." |
| 548 | (setq mpc-status nil) |
| 549 | (mpc--status-idle-timer-stop 'really) |
| 550 | (mpc--status-timer-stop)) |
| 551 | |
| 552 | ;;; A thin layer above the raw protocol commands ;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 553 | |
| 554 | ;; (defvar mpc-queue nil) |
| 555 | ;; (defvar mpc-queue-back nil) |
| 556 | |
| 557 | ;; (defun mpc--queue-head () |
| 558 | ;; (if (stringp (car mpc-queue)) (car mpc-queue) (cadar mpc-queue))) |
| 559 | ;; (defun mpc--queue-pop () |
| 560 | ;; (when mpc-queue ;Can be nil if out of sync. |
| 561 | ;; (let ((song (car mpc-queue))) |
| 562 | ;; (cl-assert song) |
| 563 | ;; (push (if (and (consp song) (cddr song)) |
| 564 | ;; ;; The queue's first element is itself a list of |
| 565 | ;; ;; songs, where the first element isn't itself a song |
| 566 | ;; ;; but a description of the list. |
| 567 | ;; (prog1 (cadr song) (setcdr song (cddr song))) |
| 568 | ;; (prog1 (if (consp song) (cadr song) song) |
| 569 | ;; (setq mpc-queue (cdr mpc-queue)))) |
| 570 | ;; mpc-queue-back) |
| 571 | ;; (cl-assert (stringp (car mpc-queue-back)))))) |
| 572 | |
| 573 | ;; (defun mpc--queue-refresh () |
| 574 | ;; ;; Maintain the queue. |
| 575 | ;; (mpc--debug "mpc--queue-refresh") |
| 576 | ;; (let ((pos (cdr (or (assq 'Pos mpc-status) (assq 'song mpc-status))))) |
| 577 | ;; (cond |
| 578 | ;; ((null pos) |
| 579 | ;; (mpc-cmd-clear 'ignore)) |
| 580 | ;; ((or (not (member pos '("0" nil))) |
| 581 | ;; ;; There's only one song in the playlist and we've stopped. |
| 582 | ;; ;; Maybe it's because of some external client that set the |
| 583 | ;; ;; playlist like that and/or manually stopped the playback, but |
| 584 | ;; ;; it's more likely that we've simply reached the end of |
| 585 | ;; ;; the song. So remove it. |
| 586 | ;; (and (equal (assq 'state mpc-status) "stop") |
| 587 | ;; (equal (assq 'playlistlength mpc-status) "1") |
| 588 | ;; (setq pos "1"))) |
| 589 | ;; ;; We're not playing the first song in the queue/playlist any |
| 590 | ;; ;; more, so update the queue. |
| 591 | ;; (dotimes (i (string-to-number pos)) (mpc--queue-pop)) |
| 592 | ;; (mpc-proc-cmd (mpc-proc-cmd-list |
| 593 | ;; (make-list (string-to-number pos) "delete 0")) |
| 594 | ;; 'ignore) |
| 595 | ;; (if (not (equal (cdr (assq 'file mpc-status)) |
| 596 | ;; (mpc--queue-head))) |
| 597 | ;; (message "MPC's queue is out of sync")))))) |
| 598 | |
| 599 | (defvar mpc--find-memoize-union-tags nil) |
| 600 | |
| 601 | (defun mpc-cmd-flush (tag value) |
| 602 | (puthash (cons tag value) nil mpc--find-memoize) |
| 603 | (dolist (uniontag mpc--find-memoize-union-tags) |
| 604 | (if (member (symbol-name tag) (split-string (symbol-name uniontag) "|")) |
| 605 | (puthash (cons uniontag value) nil mpc--find-memoize)))) |
| 606 | |
| 607 | |
| 608 | (defun mpc-cmd-special-tag-p (tag) |
| 609 | (or (memq tag '(Playlist Search Directory)) |
| 610 | (string-match "|" (symbol-name tag)))) |
| 611 | |
| 612 | (defun mpc-cmd-find (tag value) |
| 613 | "Return a list of all songs whose tag TAG has value VALUE. |
| 614 | The songs are returned as alists." |
| 615 | (or (gethash (cons tag value) mpc--find-memoize) |
| 616 | (puthash (cons tag value) |
| 617 | (cond |
| 618 | ((eq tag 'Playlist) |
| 619 | ;; Special case for pseudo-tag playlist. |
| 620 | (let ((l (condition-case nil |
| 621 | (mpc-proc-buf-to-alists |
| 622 | (mpc-proc-cmd (list "listplaylistinfo" value))) |
| 623 | (mpc-proc-error |
| 624 | ;; "[50@0] {listplaylistinfo} No such playlist" |
| 625 | nil))) |
| 626 | (i 0)) |
| 627 | (mapcar (lambda (s) |
| 628 | (prog1 (cons (cons 'Pos (number-to-string i)) s) |
| 629 | (cl-incf i))) |
| 630 | l))) |
| 631 | ((eq tag 'Search) |
| 632 | (mpc-proc-buf-to-alists |
| 633 | (mpc-proc-cmd (list "search" "any" value)))) |
| 634 | ((eq tag 'Directory) |
| 635 | (let ((pairs |
| 636 | (mpc-proc-buf-to-alist |
| 637 | (mpc-proc-cmd (list "listallinfo" value))))) |
| 638 | (mpc--proc-alist-to-alists |
| 639 | ;; Strip away the `directory' entries. |
| 640 | (delq nil (mapcar (lambda (pair) |
| 641 | (if (eq (car pair) 'directory) |
| 642 | nil pair)) |
| 643 | pairs))))) |
| 644 | ((string-match "|" (symbol-name tag)) |
| 645 | (add-to-list 'mpc--find-memoize-union-tags tag) |
| 646 | (let ((tag1 (intern (substring (symbol-name tag) |
| 647 | 0 (match-beginning 0)))) |
| 648 | (tag2 (intern (substring (symbol-name tag) |
| 649 | (match-end 0))))) |
| 650 | (mpc-union (mpc-cmd-find tag1 value) |
| 651 | (mpc-cmd-find tag2 value)))) |
| 652 | (t |
| 653 | (condition-case nil |
| 654 | (mpc-proc-buf-to-alists |
| 655 | (mpc-proc-cmd (list "find" (symbol-name tag) value))) |
| 656 | (mpc-proc-error |
| 657 | ;; If `tag' is not one of the expected tags, MPD burps |
| 658 | ;; about not having the relevant table. FIXME: check |
| 659 | ;; the kind of error. |
| 660 | (error "Unknown tag %s" tag) |
| 661 | (let ((res ())) |
| 662 | (setq value (cons tag value)) |
| 663 | (dolist (song (mpc-proc-buf-to-alists |
| 664 | (mpc-proc-cmd "listallinfo"))) |
| 665 | (if (member value song) (push song res))) |
| 666 | res))))) |
| 667 | mpc--find-memoize))) |
| 668 | |
| 669 | (defun mpc-cmd-list (tag &optional other-tag value) |
| 670 | ;; FIXME: we could also provide a `mpc-cmd-list' alternative which |
| 671 | ;; doesn't take an "other-tag value" constraint but a "song-list" instead. |
| 672 | ;; That might be more efficient in some cases. |
| 673 | (cond |
| 674 | ((eq tag 'Playlist) |
| 675 | (let ((pls (mpc-assq-all 'playlist (mpc-proc-cmd-to-alist "lsinfo")))) |
| 676 | (when other-tag |
| 677 | (dolist (pl (prog1 pls (setq pls nil))) |
| 678 | (let ((plsongs (mpc-cmd-find 'Playlist pl))) |
| 679 | (if (not (mpc-cmd-special-tag-p other-tag)) |
| 680 | (when (member (cons other-tag value) |
| 681 | (apply 'append plsongs)) |
| 682 | (push pl pls)) |
| 683 | ;; Problem N°2: we compute the intersection whereas all |
| 684 | ;; we care about is whether it's empty. So we could |
| 685 | ;; speed this up significantly. |
| 686 | ;; We only compare file names, because the full song-entries |
| 687 | ;; are slightly different (the ones in plsongs include |
| 688 | ;; position and id info specific to the playlist), and it's |
| 689 | ;; good enough because this is only used with "search", which |
| 690 | ;; doesn't pay attention to playlists and URLs anyway. |
| 691 | (let* ((osongs (mpc-cmd-find other-tag value)) |
| 692 | (ofiles (mpc-assq-all 'file (apply 'append osongs))) |
| 693 | (plfiles (mpc-assq-all 'file (apply 'append plsongs)))) |
| 694 | (when (mpc-intersection plfiles ofiles) |
| 695 | (push pl pls))))))) |
| 696 | pls)) |
| 697 | |
| 698 | ((eq tag 'Directory) |
| 699 | (if (null other-tag) |
| 700 | (apply 'nconc |
| 701 | (mpc-assq-all 'directory |
| 702 | (mpc-proc-buf-to-alist |
| 703 | (mpc-proc-cmd "lsinfo"))) |
| 704 | (mapcar (lambda (dir) |
| 705 | (let ((shortdir |
| 706 | (if (get-text-property 0 'display dir) |
| 707 | (concat " " |
| 708 | (get-text-property 0 'display dir)) |
| 709 | " ↪ ")) |
| 710 | (subdirs |
| 711 | (mpc-assq-all 'directory |
| 712 | (mpc-proc-buf-to-alist |
| 713 | (mpc-proc-cmd (list "lsinfo" dir)))))) |
| 714 | (dolist (subdir subdirs) |
| 715 | (put-text-property 0 (1+ (length dir)) |
| 716 | 'display shortdir |
| 717 | subdir)) |
| 718 | subdirs)) |
| 719 | (process-get (mpc-proc) 'Directory))) |
| 720 | ;; If there's an other-tag, then just extract the dir info from the |
| 721 | ;; list of other-tag's songs. |
| 722 | (let* ((other-songs (mpc-cmd-find other-tag value)) |
| 723 | (files (mpc-assq-all 'file (apply 'append other-songs))) |
| 724 | (dirs '())) |
| 725 | (dolist (file files) |
| 726 | (let ((dir (file-name-directory file))) |
| 727 | (if (and dir (setq dir (directory-file-name dir)) |
| 728 | (not (equal dir (car dirs)))) |
| 729 | (push dir dirs)))) |
| 730 | ;; Dirs might have duplicates still. |
| 731 | (setq dirs (delete-dups dirs)) |
| 732 | (let ((newdirs dirs)) |
| 733 | (while newdirs |
| 734 | (let ((dir (file-name-directory (pop newdirs)))) |
| 735 | (when (and dir (setq dir (directory-file-name dir)) |
| 736 | (not (member dir dirs))) |
| 737 | (push dir newdirs) |
| 738 | (push dir dirs))))) |
| 739 | dirs))) |
| 740 | |
| 741 | ;; The UI should not provide access to such a thing anyway currently. |
| 742 | ;; But I could imagine adding in the future a browser for the "search" |
| 743 | ;; tag, which would provide things like previous searches. Not sure how |
| 744 | ;; useful that would be tho. |
| 745 | ((eq tag 'Search) (error "Not supported")) |
| 746 | |
| 747 | ((string-match "|" (symbol-name tag)) |
| 748 | (let ((tag1 (intern (substring (symbol-name tag) |
| 749 | 0 (match-beginning 0)))) |
| 750 | (tag2 (intern (substring (symbol-name tag) |
| 751 | (match-end 0))))) |
| 752 | (mpc-union (mpc-cmd-list tag1 other-tag value) |
| 753 | (mpc-cmd-list tag2 other-tag value)))) |
| 754 | |
| 755 | ((null other-tag) |
| 756 | (condition-case nil |
| 757 | (mapcar 'cdr (mpc-proc-cmd-to-alist (list "list" (symbol-name tag)))) |
| 758 | (mpc-proc-error |
| 759 | ;; If `tag' is not one of the expected tags, MPD burps about not |
| 760 | ;; having the relevant table. |
| 761 | ;; FIXME: check the kind of error. |
| 762 | (error "MPD does not know this tag %s" tag) |
| 763 | (mpc-assq-all tag (mpc-proc-cmd-to-alist "listallinfo"))))) |
| 764 | (t |
| 765 | (condition-case nil |
| 766 | (if (mpc-cmd-special-tag-p other-tag) |
| 767 | (signal 'mpc-proc-error "Not implemented") |
| 768 | (mapcar 'cdr |
| 769 | (mpc-proc-cmd-to-alist |
| 770 | (list "list" (symbol-name tag) |
| 771 | (symbol-name other-tag) value)))) |
| 772 | (mpc-proc-error |
| 773 | ;; DAMN!! the 3-arg form of `list' is new in 0.12 !! |
| 774 | ;; FIXME: check the kind of error. |
| 775 | (let ((other-songs (mpc-cmd-find other-tag value))) |
| 776 | (mpc-assq-all tag |
| 777 | ;; Don't use `nconc' now that mpc-cmd-find may |
| 778 | ;; return a memoized result. |
| 779 | (apply 'append other-songs)))))))) |
| 780 | |
| 781 | (defun mpc-cmd-stop (&optional callback) |
| 782 | (mpc-proc-cmd "stop" callback)) |
| 783 | |
| 784 | (defun mpc-cmd-clear (&optional callback) |
| 785 | (mpc-proc-cmd "clear" callback) |
| 786 | ;; (setq mpc-queue-back nil mpc-queue nil) |
| 787 | ) |
| 788 | |
| 789 | (defun mpc-cmd-pause (&optional arg callback) |
| 790 | "Pause or resume playback of the queue of songs." |
| 791 | (let ((cb callback)) |
| 792 | (mpc-proc-cmd (list "pause" arg) |
| 793 | (lambda () (mpc-status-refresh) (if cb (funcall cb)))) |
| 794 | (unless callback (mpc-proc-sync)))) |
| 795 | |
| 796 | (defun mpc-cmd-status () |
| 797 | (mpc-proc-cmd-to-alist "status")) |
| 798 | |
| 799 | (defun mpc-cmd-play () |
| 800 | (mpc-proc-cmd "play") |
| 801 | (mpc-status-refresh)) |
| 802 | |
| 803 | (defun mpc-cmd-add (files &optional playlist) |
| 804 | "Add the songs FILES to PLAYLIST. |
| 805 | If PLAYLIST is t or nil or missing, use the main playlist." |
| 806 | (mpc-proc-cmd (mpc-proc-cmd-list |
| 807 | (mapcar (lambda (file) |
| 808 | (if (stringp playlist) |
| 809 | (list "playlistadd" playlist file) |
| 810 | (list "add" file))) |
| 811 | files))) |
| 812 | (if (stringp playlist) |
| 813 | (mpc-cmd-flush 'Playlist playlist))) |
| 814 | |
| 815 | (defun mpc-cmd-delete (song-poss &optional playlist) |
| 816 | "Delete the songs at positions SONG-POSS from PLAYLIST. |
| 817 | If PLAYLIST is t or nil or missing, use the main playlist." |
| 818 | (mpc-proc-cmd (mpc-proc-cmd-list |
| 819 | (mapcar (lambda (song-pos) |
| 820 | (if (stringp playlist) |
| 821 | (list "playlistdelete" playlist song-pos) |
| 822 | (list "delete" song-pos))) |
| 823 | ;; Sort them from last to first, so the renumbering |
| 824 | ;; caused by the earlier deletions don't affect |
| 825 | ;; later ones. |
| 826 | (sort song-poss '>)))) |
| 827 | (if (stringp playlist) |
| 828 | (puthash (cons 'Playlist playlist) nil mpc--find-memoize))) |
| 829 | |
| 830 | |
| 831 | (defun mpc-cmd-move (song-poss dest-pos &optional playlist) |
| 832 | (let ((i 0)) |
| 833 | (mpc-proc-cmd |
| 834 | (mpc-proc-cmd-list |
| 835 | (mapcar (lambda (song-pos) |
| 836 | (if (>= song-pos dest-pos) |
| 837 | ;; positions past dest-pos have been |
| 838 | ;; shifted by i. |
| 839 | (setq song-pos (+ song-pos i))) |
| 840 | (prog1 (if (stringp playlist) |
| 841 | (list "playlistmove" playlist song-pos dest-pos) |
| 842 | (list "move" song-pos dest-pos)) |
| 843 | (if (< song-pos dest-pos) |
| 844 | ;; This move has shifted dest-pos by 1. |
| 845 | (cl-decf dest-pos)) |
| 846 | (cl-incf i))) |
| 847 | ;; Sort them from last to first, so the renumbering |
| 848 | ;; caused by the earlier deletions affect |
| 849 | ;; later ones a bit less. |
| 850 | (sort song-poss '>)))) |
| 851 | (if (stringp playlist) |
| 852 | (puthash (cons 'Playlist playlist) nil mpc--find-memoize)))) |
| 853 | |
| 854 | (defun mpc-cmd-update (&optional arg callback) |
| 855 | (let ((cb callback)) |
| 856 | (mpc-proc-cmd (if arg (list "update" arg) "update") |
| 857 | (lambda () (mpc-status-refresh) (if cb (funcall cb)))) |
| 858 | (unless callback (mpc-proc-sync)))) |
| 859 | |
| 860 | (defun mpc-cmd-tagtypes () |
| 861 | (mapcar 'cdr (mpc-proc-cmd-to-alist "tagtypes"))) |
| 862 | |
| 863 | ;; This was never integrated into MPD. |
| 864 | ;; (defun mpc-cmd-download (file) |
| 865 | ;; (with-current-buffer (generate-new-buffer " *mpc download*") |
| 866 | ;; (set-buffer-multibyte nil) |
| 867 | ;; (let* ((proc (mpc-proc)) |
| 868 | ;; (stdbuf (process-buffer proc)) |
| 869 | ;; (markpos (marker-position (process-mark proc))) |
| 870 | ;; (stdcoding (process-coding-system proc))) |
| 871 | ;; (unwind-protect |
| 872 | ;; (progn |
| 873 | ;; (set-process-buffer proc (current-buffer)) |
| 874 | ;; (set-process-coding-system proc 'binary (cdr stdcoding)) |
| 875 | ;; (set-marker (process-mark proc) (point)) |
| 876 | ;; (mpc-proc-cmd (list "download" file))) |
| 877 | ;; (set-process-buffer proc stdbuf) |
| 878 | ;; (set-marker (process-mark proc) markpos stdbuf) |
| 879 | ;; (set-process-coding-system proc (car stdcoding) (cdr stdcoding))) |
| 880 | ;; ;; The command has completed, let's decode. |
| 881 | ;; (goto-char (point-max)) |
| 882 | ;; (delete-char -1) ;Delete final newline. |
| 883 | ;; (while (re-search-backward "^>" nil t) |
| 884 | ;; (delete-char 1)) |
| 885 | ;; (current-buffer)))) |
| 886 | |
| 887 | ;;; Misc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 888 | |
| 889 | (defcustom mpc-mpd-music-directory nil |
| 890 | "Location of MPD's music directory." |
| 891 | :type '(choice (const nil) directory)) |
| 892 | |
| 893 | (defcustom mpc-data-directory |
| 894 | (if (and (not (file-directory-p "~/.mpc")) |
| 895 | (file-directory-p "~/.emacs.d")) |
| 896 | "~/.emacs.d/mpc" "~/.mpc") |
| 897 | "Directory where MPC.el stores auxiliary data." |
| 898 | :type 'directory) |
| 899 | |
| 900 | (defun mpc-data-directory () |
| 901 | (unless (file-directory-p mpc-data-directory) |
| 902 | (make-directory mpc-data-directory)) |
| 903 | mpc-data-directory) |
| 904 | |
| 905 | (defun mpc-file-local-copy (file) |
| 906 | ;; Try to set mpc-mpd-music-directory. |
| 907 | (when (and (null mpc-mpd-music-directory) |
| 908 | (string-match "\\`localhost" mpc-host)) |
| 909 | (let ((files '("~/.mpdconf" "/etc/mpd.conf")) |
| 910 | file) |
| 911 | (while (and files (not file)) |
| 912 | (if (file-exists-p (car files)) (setq file (car files))) |
| 913 | (setq files (cdr files))) |
| 914 | (with-temp-buffer |
| 915 | (ignore-errors (insert-file-contents file)) |
| 916 | (goto-char (point-min)) |
| 917 | (if (re-search-forward "^music_directory[ ]+\"\\([^\"]+\\)\"") |
| 918 | (setq mpc-mpd-music-directory |
| 919 | (match-string 1)))))) |
| 920 | ;; Use mpc-mpd-music-directory if applicable, or else try to use the |
| 921 | ;; `download' command, although it's never been accepted in `mpd' :-( |
| 922 | (if (and mpc-mpd-music-directory |
| 923 | (file-exists-p (expand-file-name file mpc-mpd-music-directory))) |
| 924 | (expand-file-name file mpc-mpd-music-directory) |
| 925 | ;; (let ((aux (expand-file-name (replace-regexp-in-string "[/]" "|" file) |
| 926 | ;; (mpc-data-directory)))) |
| 927 | ;; (unless (file-exists-p aux) |
| 928 | ;; (condition-case err |
| 929 | ;; (with-local-quit |
| 930 | ;; (with-current-buffer (mpc-cmd-download file) |
| 931 | ;; (write-region (point-min) (point-max) aux) |
| 932 | ;; (kill-buffer (current-buffer)))) |
| 933 | ;; (mpc-proc-error (message "Download error: %s" err) (setq aux nil)))) |
| 934 | ;; aux) |
| 935 | )) |
| 936 | |
| 937 | ;;; Formatter ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 938 | |
| 939 | (defun mpc-secs-to-time (secs) |
| 940 | ;; We could use `format-seconds', but it doesn't seem worth the trouble |
| 941 | ;; because we'd still need to check (>= secs (* 60 100)) since the special |
| 942 | ;; %z only allows us to drop the large units for small values but |
| 943 | ;; not to drop the small units for large values. |
| 944 | (if (stringp secs) (setq secs (string-to-number secs))) |
| 945 | (if (>= secs (* 60 100)) ;More than 100 minutes. |
| 946 | (format "%dh%02d" ;"%d:%02d:%02d" |
| 947 | (/ secs 3600) (% (/ secs 60) 60)) ;; (% secs 60) |
| 948 | (format "%d:%02d" (/ secs 60) (% secs 60)))) |
| 949 | |
| 950 | (defvar mpc-tempfiles nil) |
| 951 | (defconst mpc-tempfiles-reftable (make-hash-table :weakness 'key)) |
| 952 | |
| 953 | (defun mpc-tempfiles-clean () |
| 954 | (let ((live ())) |
| 955 | (maphash (lambda (_k v) (push v live)) mpc-tempfiles-reftable) |
| 956 | (dolist (f mpc-tempfiles) |
| 957 | (unless (member f live) (ignore-errors (delete-file f)))) |
| 958 | (setq mpc-tempfiles live))) |
| 959 | |
| 960 | (defun mpc-tempfiles-add (key file) |
| 961 | (mpc-tempfiles-clean) |
| 962 | (puthash key file mpc-tempfiles-reftable) |
| 963 | (push file mpc-tempfiles)) |
| 964 | |
| 965 | (defun mpc-format (format-spec info &optional hscroll) |
| 966 | "Format the INFO according to FORMAT-SPEC, inserting the result at point." |
| 967 | (let* ((pos 0) |
| 968 | (start (point)) |
| 969 | (col (if hscroll (- hscroll) 0)) |
| 970 | (insert (lambda (str) |
| 971 | (cond |
| 972 | ((>= col 0) (insert str)) |
| 973 | (t (insert (substring str (min (length str) (- col)))))))) |
| 974 | (pred nil)) |
| 975 | (while (string-match "%\\(?:%\\|\\(-\\)?\\([0-9]+\\)?{\\([[:alpha:]][[:alnum:]]*\\)\\(?:-\\([^}]+\\)\\)?}\\)" format-spec pos) |
| 976 | (let ((pre-text (substring format-spec pos (match-beginning 0)))) |
| 977 | (funcall insert pre-text) |
| 978 | (setq col (+ col (string-width pre-text)))) |
| 979 | (setq pos (match-end 0)) |
| 980 | (if (null (match-end 3)) |
| 981 | (progn |
| 982 | (funcall insert "%") |
| 983 | (setq col (+ col 1))) |
| 984 | (let* ((size (match-string 2 format-spec)) |
| 985 | (tag (intern (match-string 3 format-spec))) |
| 986 | (post (match-string 4 format-spec)) |
| 987 | (right-align (match-end 1)) |
| 988 | (text |
| 989 | (if (eq info 'self) (symbol-name tag) |
| 990 | (pcase tag |
| 991 | ((or `Time `Duration) |
| 992 | (let ((time (cdr (or (assq 'time info) (assq 'Time info))))) |
| 993 | (setq pred (list nil)) ;Just assume it's never eq. |
| 994 | (when time |
| 995 | (mpc-secs-to-time (if (and (eq tag 'Duration) |
| 996 | (string-match ":" time)) |
| 997 | (substring time (match-end 0)) |
| 998 | time))))) |
| 999 | (`Cover |
| 1000 | (let* ((dir (file-name-directory (cdr (assq 'file info)))) |
| 1001 | (cover (concat dir "cover.jpg")) |
| 1002 | (file (with-demoted-errors "MPC: %s" |
| 1003 | (mpc-file-local-copy cover))) |
| 1004 | image) |
| 1005 | ;; (debug) |
| 1006 | (push `(equal ',dir (file-name-directory (cdr (assq 'file info)))) pred) |
| 1007 | (if (null file) |
| 1008 | ;; Make sure we return something on which we can |
| 1009 | ;; place the `mpc-pred' property, as |
| 1010 | ;; a negative-cache. We could also use |
| 1011 | ;; a default cover. |
| 1012 | (progn (setq size nil) " ") |
| 1013 | (if (null size) (setq image (create-image file)) |
| 1014 | (let ((tempfile (make-temp-file "mpc" nil ".jpg"))) |
| 1015 | (call-process "convert" nil nil nil |
| 1016 | "-scale" size file tempfile) |
| 1017 | (setq image (create-image tempfile)) |
| 1018 | (mpc-tempfiles-add image tempfile))) |
| 1019 | (setq size nil) |
| 1020 | (propertize dir 'display image)))) |
| 1021 | (_ (let ((val (cdr (assq tag info)))) |
| 1022 | ;; For Streaming URLs, there's no other info |
| 1023 | ;; than the URL in `file'. Pretend it's in `Title'. |
| 1024 | (when (and (null val) (eq tag 'Title)) |
| 1025 | (setq val (cdr (assq 'file info)))) |
| 1026 | (push `(equal ',val (cdr (assq ',tag info))) pred) |
| 1027 | (cond |
| 1028 | ((not (and (eq tag 'Date) (stringp val))) val) |
| 1029 | ;; For "date", only keep the year! |
| 1030 | ((string-match "[0-9]\\{4\\}" val) |
| 1031 | (match-string 0 val)) |
| 1032 | (t val))))))) |
| 1033 | (space (when size |
| 1034 | (setq size (string-to-number size)) |
| 1035 | (propertize " " 'display |
| 1036 | (list 'space :align-to (+ col size))))) |
| 1037 | (textwidth (if text (string-width text) 0)) |
| 1038 | (postwidth (if post (string-width post) 0))) |
| 1039 | (when text |
| 1040 | (let ((display |
| 1041 | (if (and size |
| 1042 | (> (+ postwidth textwidth) size)) |
| 1043 | (propertize |
| 1044 | (truncate-string-to-width text size nil nil "…") |
| 1045 | 'help-echo text) |
| 1046 | text))) |
| 1047 | (when (memq tag '(Artist Album Composer)) ;FIXME: wrong list. |
| 1048 | (setq display |
| 1049 | (propertize display |
| 1050 | 'mouse-face 'highlight |
| 1051 | 'follow-link t |
| 1052 | 'keymap `(keymap |
| 1053 | (mouse-2 |
| 1054 | . (lambda () |
| 1055 | (interactive) |
| 1056 | (mpc-constraints-push 'noerror) |
| 1057 | (mpc-constraints-restore |
| 1058 | ',(list (list tag text))))))))) |
| 1059 | (funcall insert |
| 1060 | (concat (when size |
| 1061 | (propertize " " 'display |
| 1062 | (list 'space :align-to |
| 1063 | (+ col |
| 1064 | (if (and size right-align) |
| 1065 | (- size postwidth textwidth) |
| 1066 | 0))))) |
| 1067 | display post)))) |
| 1068 | (if (null size) (setq col (+ col textwidth postwidth)) |
| 1069 | (insert space) |
| 1070 | (setq col (+ col size)))))) |
| 1071 | (put-text-property start (point) 'mpc-pred |
| 1072 | `(lambda (info) (and ,@(nreverse pred)))))) |
| 1073 | |
| 1074 | ;;; The actual UI code ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1075 | |
| 1076 | (defvar mpc-mode-map |
| 1077 | (let ((map (make-keymap))) |
| 1078 | (suppress-keymap map) |
| 1079 | ;; (define-key map "\e" 'mpc-stop) |
| 1080 | (define-key map "q" 'mpc-quit) |
| 1081 | (define-key map "\r" 'mpc-select) |
| 1082 | (define-key map [(shift return)] 'mpc-select-toggle) |
| 1083 | (define-key map [mouse-2] 'mpc-select) |
| 1084 | (define-key map [S-mouse-2] 'mpc-select-extend) |
| 1085 | (define-key map [C-mouse-2] 'mpc-select-toggle) |
| 1086 | (define-key map [drag-mouse-2] 'mpc-drag-n-drop) |
| 1087 | ;; We use `always' because a binding to t is like a binding to nil. |
| 1088 | (define-key map [follow-link] :always) |
| 1089 | ;; But follow-link doesn't apply blindly to header-line and |
| 1090 | ;; mode-line clicks. |
| 1091 | (define-key map [header-line follow-link] 'ignore) |
| 1092 | (define-key map [mode-line follow-link] 'ignore) |
| 1093 | ;; Doesn't work because the first click changes the buffer, so the second |
| 1094 | ;; is applied elsewhere :-( |
| 1095 | ;; (define-key map [(double mouse-2)] 'mpc-play-at-point) |
| 1096 | (define-key map "p" 'mpc-pause) |
| 1097 | map)) |
| 1098 | |
| 1099 | (easy-menu-define mpc-mode-menu mpc-mode-map |
| 1100 | "Menu for MPC.el." |
| 1101 | '("MPC.el" |
| 1102 | ["Add new browser" mpc-tagbrowser] |
| 1103 | ["Update DB" mpc-update] |
| 1104 | ["Quit" mpc-quit])) |
| 1105 | |
| 1106 | (defvar mpc-tool-bar-map |
| 1107 | (let ((map (make-sparse-keymap))) |
| 1108 | (tool-bar-local-item "mpc/prev" 'mpc-prev 'prev map |
| 1109 | :enable '(not (equal (cdr (assq 'state mpc-status)) "stop")) |
| 1110 | :label "Prev" :vert-only t) |
| 1111 | ;; FIXME: how can we bind it to the down-event? |
| 1112 | (tool-bar-local-item "mpc/rewind" 'mpc-rewind 'rewind map |
| 1113 | :enable '(not (equal (cdr (assq 'state mpc-status)) "stop")) |
| 1114 | :label "Rew" :vert-only t |
| 1115 | :button '(:toggle . (and mpc--faster-toggle-timer |
| 1116 | (not mpc--faster-toggle-forward)))) |
| 1117 | ;; We could use a single toggle command for pause/play, with 2 different |
| 1118 | ;; icons depending on whether or not it's selected, but then it'd have |
| 1119 | ;; to be a toggle-button, thus displayed depressed in one of the |
| 1120 | ;; two states :-( |
| 1121 | (tool-bar-local-item "mpc/pause" 'mpc-pause 'pause map |
| 1122 | :label "Pause" :vert-only t |
| 1123 | :visible '(equal (cdr (assq 'state mpc-status)) "play") |
| 1124 | :help "Pause/play") |
| 1125 | (tool-bar-local-item "mpc/play" 'mpc-play 'play map |
| 1126 | :label "Play" :vert-only t |
| 1127 | :visible '(not (equal (cdr (assq 'state mpc-status)) "play")) |
| 1128 | :help "Play/pause") |
| 1129 | ;; FIXME: how can we bind it to the down-event? |
| 1130 | (tool-bar-local-item "mpc/ffwd" 'mpc-ffwd 'ffwd map |
| 1131 | :enable '(not (equal (cdr (assq 'state mpc-status)) "stop")) |
| 1132 | :label "Ffwd" :vert-only t |
| 1133 | :button '(:toggle . (and mpc--faster-toggle-timer |
| 1134 | mpc--faster-toggle-forward))) |
| 1135 | (tool-bar-local-item "mpc/next" 'mpc-next 'next map |
| 1136 | :label "Next" :vert-only t |
| 1137 | :enable '(not (equal (cdr (assq 'state mpc-status)) "stop"))) |
| 1138 | (tool-bar-local-item "mpc/stop" 'mpc-stop 'stop map |
| 1139 | :label "Stop" :vert-only t) |
| 1140 | (tool-bar-local-item "mpc/add" 'mpc-playlist-add 'add map |
| 1141 | :label "Add" :vert-only t |
| 1142 | :help "Append to the playlist") |
| 1143 | map)) |
| 1144 | |
| 1145 | (define-derived-mode mpc-mode fundamental-mode "MPC" |
| 1146 | "Major mode for the features common to all buffers of MPC." |
| 1147 | (buffer-disable-undo) |
| 1148 | (setq buffer-read-only t) |
| 1149 | (if (boundp 'tool-bar-map) ; not if --without-x |
| 1150 | (setq-local tool-bar-map mpc-tool-bar-map)) |
| 1151 | (setq-local truncate-lines t)) |
| 1152 | |
| 1153 | ;;; The mpc-status-mode buffer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1154 | |
| 1155 | (define-derived-mode mpc-status-mode mpc-mode "MPC-Status" |
| 1156 | "Major mode to display MPC status info." |
| 1157 | (setq-local mode-line-format |
| 1158 | '("%e" mode-line-frame-identification |
| 1159 | mode-line-buffer-identification)) |
| 1160 | (setq-local window-area-factor 3) |
| 1161 | (setq-local header-line-format '("MPC " mpc-volume))) |
| 1162 | |
| 1163 | (defvar mpc-status-buffer-format |
| 1164 | '("%-5{Time} / %{Duration} %2{Disc--}%4{Track}" "%{Title}" "%{Album}" "%{Artist}" "%128{Cover}")) |
| 1165 | |
| 1166 | (defun mpc-status-buffer-refresh () |
| 1167 | (let ((buf (mpc-proc-buffer (mpc-proc) 'status))) |
| 1168 | (when (buffer-live-p buf) |
| 1169 | (with-current-buffer buf |
| 1170 | (save-excursion |
| 1171 | (goto-char (point-min)) |
| 1172 | (when (assq 'file mpc-status) |
| 1173 | (let ((inhibit-read-only t)) |
| 1174 | (dolist (spec mpc-status-buffer-format) |
| 1175 | (let ((pred (get-text-property (point) 'mpc-pred))) |
| 1176 | (if (and pred (funcall pred mpc-status)) |
| 1177 | (forward-line) |
| 1178 | (delete-region (point) (line-beginning-position 2)) |
| 1179 | (ignore-errors (mpc-format spec mpc-status)) |
| 1180 | (insert "\n")))) |
| 1181 | (unless (eobp) (delete-region (point) (point-max)))))))))) |
| 1182 | |
| 1183 | (defun mpc-status-buffer-show () |
| 1184 | (interactive) |
| 1185 | (let* ((proc (mpc-proc)) |
| 1186 | (buf (mpc-proc-buffer proc 'status)) |
| 1187 | (songs-buf (mpc-proc-buffer proc 'songs)) |
| 1188 | (songs-win (if songs-buf (get-buffer-window songs-buf 0)))) |
| 1189 | (unless (buffer-live-p buf) |
| 1190 | (setq buf (get-buffer-create "*MPC-Status*")) |
| 1191 | (with-current-buffer buf |
| 1192 | (mpc-status-mode)) |
| 1193 | (mpc-proc-buffer proc 'status buf)) |
| 1194 | (if (null songs-win) (pop-to-buffer buf) |
| 1195 | (let ((_win (split-window songs-win 20 t))) |
| 1196 | (set-window-dedicated-p songs-win nil) |
| 1197 | (set-window-buffer songs-win buf) |
| 1198 | (set-window-dedicated-p songs-win 'soft))))) |
| 1199 | |
| 1200 | ;;; Selection management;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1201 | |
| 1202 | (defvar mpc-separator-ol nil) |
| 1203 | |
| 1204 | (defvar-local mpc-select nil) |
| 1205 | |
| 1206 | (defmacro mpc-select-save (&rest body) |
| 1207 | "Execute BODY and restore the selection afterwards." |
| 1208 | (declare (indent 0) (debug t)) |
| 1209 | `(let ((selection (mpc-select-get-selection)) |
| 1210 | (position (cons (buffer-substring-no-properties |
| 1211 | (line-beginning-position) (line-end-position)) |
| 1212 | (current-column)))) |
| 1213 | ,@body |
| 1214 | (mpc-select-restore selection) |
| 1215 | (goto-char (point-min)) |
| 1216 | (if (re-search-forward |
| 1217 | (concat "^" (regexp-quote (car position)) "$") |
| 1218 | (if (overlayp mpc-separator-ol) |
| 1219 | (overlay-end mpc-separator-ol)) |
| 1220 | t) |
| 1221 | (move-to-column (cdr position))) |
| 1222 | (let ((win (get-buffer-window (current-buffer) 0))) |
| 1223 | (if win (set-window-point win (point)))))) |
| 1224 | |
| 1225 | (defun mpc-select-get-selection () |
| 1226 | (mapcar (lambda (ol) |
| 1227 | (buffer-substring-no-properties |
| 1228 | (overlay-start ol) (1- (overlay-end ol)))) |
| 1229 | mpc-select)) |
| 1230 | |
| 1231 | (defun mpc-select-restore (selection) |
| 1232 | ;; Restore the selection. I.e. move the overlays back to their |
| 1233 | ;; corresponding location. Actually which overlay is used for what |
| 1234 | ;; doesn't matter. |
| 1235 | (mapc 'delete-overlay mpc-select) |
| 1236 | (setq mpc-select nil) |
| 1237 | (dolist (elem selection) |
| 1238 | ;; After an update, some elements may have disappeared. |
| 1239 | (goto-char (point-min)) |
| 1240 | (when (re-search-forward |
| 1241 | (concat "^" (regexp-quote elem) "$") nil t) |
| 1242 | (mpc-select-make-overlay))) |
| 1243 | (when mpc-tag (mpc-tagbrowser-all-select)) |
| 1244 | (beginning-of-line)) |
| 1245 | |
| 1246 | (defun mpc-select-make-overlay () |
| 1247 | (cl-assert (not (get-char-property (point) 'mpc-select))) |
| 1248 | (let ((ol (make-overlay |
| 1249 | (line-beginning-position) (line-beginning-position 2)))) |
| 1250 | (overlay-put ol 'mpc-select t) |
| 1251 | (overlay-put ol 'face 'region) |
| 1252 | (overlay-put ol 'evaporate t) |
| 1253 | (push ol mpc-select))) |
| 1254 | |
| 1255 | (defun mpc-select (&optional event) |
| 1256 | "Select the tag value at point." |
| 1257 | (interactive (list last-nonmenu-event)) |
| 1258 | (mpc-event-set-point event) |
| 1259 | (if (and (bolp) (eobp)) (forward-line -1)) |
| 1260 | (mapc 'delete-overlay mpc-select) |
| 1261 | (setq mpc-select nil) |
| 1262 | (if (mpc-tagbrowser-all-p) |
| 1263 | nil |
| 1264 | (mpc-select-make-overlay)) |
| 1265 | (when mpc-tag |
| 1266 | (mpc-tagbrowser-all-select) |
| 1267 | (mpc-selection-refresh))) |
| 1268 | |
| 1269 | (defun mpc-select-toggle (&optional event) |
| 1270 | "Toggle the selection of the tag value at point." |
| 1271 | (interactive (list last-nonmenu-event)) |
| 1272 | (mpc-event-set-point event) |
| 1273 | (save-excursion |
| 1274 | (cond |
| 1275 | ;; The line is already selected: deselect it. |
| 1276 | ((get-char-property (point) 'mpc-select) |
| 1277 | (let ((ols nil)) |
| 1278 | (dolist (ol mpc-select) |
| 1279 | (if (and (<= (overlay-start ol) (point)) |
| 1280 | (> (overlay-end ol) (point))) |
| 1281 | (delete-overlay ol) |
| 1282 | (push ol ols))) |
| 1283 | (cl-assert (= (1+ (length ols)) (length mpc-select))) |
| 1284 | (setq mpc-select ols))) |
| 1285 | ;; We're trying to select *ALL* additionally to others. |
| 1286 | ((mpc-tagbrowser-all-p) nil) |
| 1287 | ;; Select the current line. |
| 1288 | (t (mpc-select-make-overlay)))) |
| 1289 | (when mpc-tag |
| 1290 | (mpc-tagbrowser-all-select) |
| 1291 | (mpc-selection-refresh))) |
| 1292 | |
| 1293 | (defun mpc-select-extend (&optional event) |
| 1294 | "Extend the selection up to point." |
| 1295 | (interactive (list last-nonmenu-event)) |
| 1296 | (mpc-event-set-point event) |
| 1297 | (if (null mpc-select) |
| 1298 | ;; If nothing's selected yet, fallback to selecting the elem at point. |
| 1299 | (mpc-select event) |
| 1300 | (save-excursion |
| 1301 | (cond |
| 1302 | ;; The line is already in a selected area; truncate the area. |
| 1303 | ((get-char-property (point) 'mpc-select) |
| 1304 | (let ((before 0) |
| 1305 | (after 0) |
| 1306 | (mid (line-beginning-position)) |
| 1307 | start end) |
| 1308 | (while (and (zerop (forward-line 1)) |
| 1309 | (get-char-property (point) 'mpc-select)) |
| 1310 | (setq end (1+ (point))) |
| 1311 | (cl-incf after)) |
| 1312 | (goto-char mid) |
| 1313 | (while (and (zerop (forward-line -1)) |
| 1314 | (get-char-property (point) 'mpc-select)) |
| 1315 | (setq start (point)) |
| 1316 | (cl-incf before)) |
| 1317 | (if (and (= after 0) (= before 0)) |
| 1318 | ;; Shortening an already minimum-size region: do nothing. |
| 1319 | nil |
| 1320 | (if (> after before) |
| 1321 | (setq end mid) |
| 1322 | (setq start (1+ mid))) |
| 1323 | (let ((ols '())) |
| 1324 | (dolist (ol mpc-select) |
| 1325 | (if (and (>= (overlay-start ol) start) |
| 1326 | (< (overlay-start ol) end)) |
| 1327 | (delete-overlay ol) |
| 1328 | (push ol ols))) |
| 1329 | (setq mpc-select (nreverse ols)))))) |
| 1330 | ;; Extending a prior area. Look for the closest selection. |
| 1331 | (t |
| 1332 | (when (mpc-tagbrowser-all-p) |
| 1333 | (forward-line 1)) |
| 1334 | (let ((before 0) |
| 1335 | (count 0) |
| 1336 | (dir 1) |
| 1337 | (start (line-beginning-position))) |
| 1338 | (while (and (zerop (forward-line 1)) |
| 1339 | (not (get-char-property (point) 'mpc-select))) |
| 1340 | (cl-incf count)) |
| 1341 | (unless (get-char-property (point) 'mpc-select) |
| 1342 | (setq count nil)) |
| 1343 | (goto-char start) |
| 1344 | (while (and (zerop (forward-line -1)) |
| 1345 | (not (get-char-property (point) 'mpc-select))) |
| 1346 | (cl-incf before)) |
| 1347 | (unless (get-char-property (point) 'mpc-select) |
| 1348 | (setq before nil)) |
| 1349 | (when (and before (or (null count) (< before count))) |
| 1350 | (setq count before) |
| 1351 | (setq dir -1)) |
| 1352 | (goto-char start) |
| 1353 | (dotimes (_i (1+ (or count 0))) |
| 1354 | (mpc-select-make-overlay) |
| 1355 | (forward-line dir)))))) |
| 1356 | (when mpc-tag |
| 1357 | (mpc-tagbrowser-all-select) |
| 1358 | (mpc-selection-refresh)))) |
| 1359 | |
| 1360 | ;;; Constraint sets ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1361 | |
| 1362 | (defvar mpc--song-search nil) |
| 1363 | |
| 1364 | (defun mpc-constraints-get-current (&optional avoid-buf) |
| 1365 | "Return currently selected set of constraints. |
| 1366 | If AVOID-BUF is non-nil, it specifies a buffer which should be ignored |
| 1367 | when constructing the set of constraints." |
| 1368 | (let ((constraints (if mpc--song-search `((Search ,mpc--song-search)))) |
| 1369 | tag select) |
| 1370 | (dolist (buf (process-get (mpc-proc) 'buffers)) |
| 1371 | (setq buf (cdr buf)) |
| 1372 | (when (and (setq tag (buffer-local-value 'mpc-tag buf)) |
| 1373 | (not (eq buf avoid-buf)) |
| 1374 | (setq select |
| 1375 | (with-current-buffer buf (mpc-select-get-selection)))) |
| 1376 | (push (cons tag select) constraints))) |
| 1377 | constraints)) |
| 1378 | |
| 1379 | (defun mpc-constraints-tag-lookup (buffer-tag constraints) |
| 1380 | (let (res) |
| 1381 | (dolist (constraint constraints) |
| 1382 | (when (or (eq (car constraint) buffer-tag) |
| 1383 | (and (string-match "|" (symbol-name buffer-tag)) |
| 1384 | (member (symbol-name (car constraint)) |
| 1385 | (split-string (symbol-name buffer-tag) "|")))) |
| 1386 | (setq res (cdr constraint)))) |
| 1387 | res)) |
| 1388 | |
| 1389 | (defun mpc-constraints-restore (constraints) |
| 1390 | (let ((search (assq 'Search constraints))) |
| 1391 | (setq mpc--song-search (cadr search)) |
| 1392 | (when search (setq constraints (delq search constraints)))) |
| 1393 | (dolist (buf (process-get (mpc-proc) 'buffers)) |
| 1394 | (setq buf (cdr buf)) |
| 1395 | (when (buffer-live-p buf) |
| 1396 | (let* ((tag (buffer-local-value 'mpc-tag buf)) |
| 1397 | (constraint (mpc-constraints-tag-lookup tag constraints))) |
| 1398 | (when tag |
| 1399 | (with-current-buffer buf |
| 1400 | (mpc-select-restore constraint)))))) |
| 1401 | (mpc-selection-refresh)) |
| 1402 | |
| 1403 | ;; I don't get the ring.el code. I think it doesn't do what I need, but |
| 1404 | ;; then I don't understand when what it does would be useful. |
| 1405 | (defun mpc-ring-make (size) (cons 0 (cons 0 (make-vector size nil)))) |
| 1406 | (defun mpc-ring-push (ring val) |
| 1407 | (aset (cddr ring) (car ring) val) |
| 1408 | (setcar (cdr ring) (max (cadr ring) (1+ (car ring)))) |
| 1409 | (setcar ring (mod (1+ (car ring)) (length (cddr ring))))) |
| 1410 | (defun mpc-ring-pop (ring) |
| 1411 | (setcar ring (mod (1- (car ring)) (cadr ring))) |
| 1412 | (aref (cddr ring) (car ring))) |
| 1413 | |
| 1414 | (defvar mpc-constraints-ring (mpc-ring-make 10)) |
| 1415 | |
| 1416 | (defun mpc-constraints-push (&optional noerror) |
| 1417 | "Push the current selection on the ring for later." |
| 1418 | (interactive) |
| 1419 | (let ((constraints (mpc-constraints-get-current))) |
| 1420 | (if (null constraints) |
| 1421 | (unless noerror (error "No selection to push")) |
| 1422 | (mpc-ring-push mpc-constraints-ring constraints)))) |
| 1423 | |
| 1424 | (defun mpc-constraints-pop () |
| 1425 | "Recall the most recently pushed selection." |
| 1426 | (interactive) |
| 1427 | (let ((constraints (mpc-ring-pop mpc-constraints-ring))) |
| 1428 | (if (null constraints) |
| 1429 | (error "No selection to return to") |
| 1430 | (mpc-constraints-restore constraints)))) |
| 1431 | |
| 1432 | ;;; The TagBrowser mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1433 | |
| 1434 | (defconst mpc-tagbrowser-all-name (propertize "*ALL*" 'face 'italic)) |
| 1435 | (defvar-local mpc-tagbrowser-all-ol nil) |
| 1436 | (defvar-local mpc-tag-name nil) |
| 1437 | (defun mpc-tagbrowser-all-p () |
| 1438 | (and (eq (point-min) (line-beginning-position)) |
| 1439 | (equal mpc-tagbrowser-all-name |
| 1440 | (buffer-substring (point-min) (line-end-position))))) |
| 1441 | |
| 1442 | (define-derived-mode mpc-tagbrowser-mode mpc-mode '("MPC-" mpc-tag-name) |
| 1443 | (setq-local mode-line-process '("" mpc-tag-name)) |
| 1444 | (setq-local mode-line-format nil) |
| 1445 | (setq-local header-line-format '("" mpc-tag-name)) ;; "s" |
| 1446 | (setq-local buffer-undo-list t) |
| 1447 | ) |
| 1448 | |
| 1449 | (defun mpc-tagbrowser-refresh () |
| 1450 | (mpc-select-save |
| 1451 | (widen) |
| 1452 | (goto-char (point-min)) |
| 1453 | (cl-assert (looking-at (regexp-quote mpc-tagbrowser-all-name))) |
| 1454 | (forward-line 1) |
| 1455 | (let ((inhibit-read-only t)) |
| 1456 | (delete-region (point) (point-max)) |
| 1457 | (dolist (val (mpc-cmd-list mpc-tag)) (insert val "\n"))) |
| 1458 | (set-buffer-modified-p nil)) |
| 1459 | (mpc-reorder)) |
| 1460 | |
| 1461 | (defun mpc-updated-db () |
| 1462 | ;; FIXME: This is not asynchronous, but is run from a process filter. |
| 1463 | (unless (assq 'updating_db mpc-status) |
| 1464 | (clrhash mpc--find-memoize) |
| 1465 | (dolist (buf (process-get (mpc-proc) 'buffers)) |
| 1466 | (setq buf (cdr buf)) |
| 1467 | (when (buffer-local-value 'mpc-tag buf) |
| 1468 | (with-current-buffer buf (with-local-quit (mpc-tagbrowser-refresh))))) |
| 1469 | (with-local-quit (mpc-songs-refresh)))) |
| 1470 | |
| 1471 | (defun mpc-tagbrowser-tag-name (tag) |
| 1472 | (cond |
| 1473 | ((string-match "|" (symbol-name tag)) |
| 1474 | (let ((tag1 (intern (substring (symbol-name tag) |
| 1475 | 0 (match-beginning 0)))) |
| 1476 | (tag2 (intern (substring (symbol-name tag) |
| 1477 | (match-end 0))))) |
| 1478 | (concat (mpc-tagbrowser-tag-name tag1) |
| 1479 | " | " |
| 1480 | (mpc-tagbrowser-tag-name tag2)))) |
| 1481 | ((string-match "y\\'" (symbol-name tag)) |
| 1482 | (concat (substring (symbol-name tag) 0 -1) "ies")) |
| 1483 | (t (concat (symbol-name tag) "s")))) |
| 1484 | |
| 1485 | (defun mpc-tagbrowser-buf (tag) |
| 1486 | (let ((buf (mpc-proc-buffer (mpc-proc) tag))) |
| 1487 | (if (buffer-live-p buf) buf |
| 1488 | (setq buf (get-buffer-create (format "*MPC %ss*" tag))) |
| 1489 | (mpc-proc-buffer (mpc-proc) tag buf) |
| 1490 | (with-current-buffer buf |
| 1491 | (let ((inhibit-read-only t)) |
| 1492 | (erase-buffer) |
| 1493 | (if (member tag '(Directory)) |
| 1494 | (mpc-tagbrowser-dir-mode) |
| 1495 | (mpc-tagbrowser-mode)) |
| 1496 | (insert mpc-tagbrowser-all-name "\n")) |
| 1497 | (forward-line -1) |
| 1498 | (setq mpc-tag tag) |
| 1499 | (setq mpc-tag-name (mpc-tagbrowser-tag-name tag)) |
| 1500 | (mpc-tagbrowser-all-select) |
| 1501 | (mpc-tagbrowser-refresh) |
| 1502 | buf)))) |
| 1503 | |
| 1504 | (defvar tag-browser-tagtypes |
| 1505 | (lazy-completion-table tag-browser-tagtypes |
| 1506 | (lambda () |
| 1507 | (append '("Playlist" "Directory") |
| 1508 | (mpc-cmd-tagtypes))))) |
| 1509 | |
| 1510 | (defun mpc-tagbrowser (tag) |
| 1511 | "Create a new browser for TAG." |
| 1512 | (interactive |
| 1513 | (list |
| 1514 | (let ((completion-ignore-case t)) |
| 1515 | (intern |
| 1516 | (completing-read "Tag: " tag-browser-tagtypes nil 'require-match))))) |
| 1517 | (let* ((newbuf (mpc-tagbrowser-buf tag)) |
| 1518 | (win (get-buffer-window newbuf 0))) |
| 1519 | (if win (select-window win) |
| 1520 | (if (with-current-buffer (window-buffer) |
| 1521 | (derived-mode-p 'mpc-tagbrowser-mode)) |
| 1522 | (setq win (selected-window)) |
| 1523 | ;; Find a tagbrowser-mode buffer. |
| 1524 | (let ((buffers (process-get (mpc-proc) 'buffers)) |
| 1525 | buffer) |
| 1526 | (while |
| 1527 | (and buffers |
| 1528 | (not (and (buffer-live-p (setq buffer (cdr (pop buffers)))) |
| 1529 | (with-current-buffer buffer |
| 1530 | (derived-mode-p 'mpc-tagbrowser-mode)) |
| 1531 | (setq win (get-buffer-window buffer 0)))))))) |
| 1532 | (if (not win) |
| 1533 | (pop-to-buffer newbuf) |
| 1534 | (setq win (split-window win nil 'horiz)) |
| 1535 | (set-window-buffer win newbuf) |
| 1536 | (set-window-dedicated-p win 'soft) |
| 1537 | (select-window win) |
| 1538 | (balance-windows-area))))) |
| 1539 | |
| 1540 | (defun mpc-tagbrowser-all-select () |
| 1541 | "Select the special *ALL* entry if no other is selected." |
| 1542 | (if mpc-select |
| 1543 | (delete-overlay mpc-tagbrowser-all-ol) |
| 1544 | (save-excursion |
| 1545 | (goto-char (point-min)) |
| 1546 | (if mpc-tagbrowser-all-ol |
| 1547 | (move-overlay mpc-tagbrowser-all-ol |
| 1548 | (point) (line-beginning-position 2)) |
| 1549 | (let ((ol (make-overlay (point) (line-beginning-position 2)))) |
| 1550 | (overlay-put ol 'face 'region) |
| 1551 | (overlay-put ol 'evaporate t) |
| 1552 | (setq-local mpc-tagbrowser-all-ol ol)))))) |
| 1553 | |
| 1554 | ;; (defvar mpc-constraints nil) |
| 1555 | (defun mpc-separator (active) |
| 1556 | ;; Place a separator mark. |
| 1557 | (unless mpc-separator-ol |
| 1558 | (setq-local mpc-separator-ol |
| 1559 | (make-overlay (point) (point))) |
| 1560 | (overlay-put mpc-separator-ol 'after-string |
| 1561 | (propertize "\n" |
| 1562 | 'face '(:height 0.05 :inverse-video t)))) |
| 1563 | (goto-char (point-min)) |
| 1564 | (forward-line 1) |
| 1565 | (while |
| 1566 | (and (member (buffer-substring-no-properties |
| 1567 | (line-beginning-position) (line-end-position)) |
| 1568 | active) |
| 1569 | (zerop (forward-line 1)))) |
| 1570 | (if (or (eobp) (null active)) |
| 1571 | (delete-overlay mpc-separator-ol) |
| 1572 | (move-overlay mpc-separator-ol (1- (point)) (point)))) |
| 1573 | |
| 1574 | (defun mpc-sort (active) |
| 1575 | ;; Sort the active elements at the front. |
| 1576 | (let ((inhibit-read-only t)) |
| 1577 | (goto-char (point-min)) |
| 1578 | (if (mpc-tagbrowser-all-p) (forward-line 1)) |
| 1579 | (condition-case nil |
| 1580 | (sort-subr nil 'forward-line 'end-of-line |
| 1581 | nil nil |
| 1582 | (lambda (s1 s2) |
| 1583 | (setq s1 (buffer-substring-no-properties |
| 1584 | (car s1) (cdr s1))) |
| 1585 | (setq s2 (buffer-substring-no-properties |
| 1586 | (car s2) (cdr s2))) |
| 1587 | (cond |
| 1588 | ((member s1 active) |
| 1589 | (if (member s2 active) |
| 1590 | (let ((cmp (mpc-compare-strings s1 s2 t))) |
| 1591 | (and (numberp cmp) (< cmp 0))) |
| 1592 | t)) |
| 1593 | ((member s2 active) nil) |
| 1594 | (t (let ((cmp (mpc-compare-strings s1 s2 t))) |
| 1595 | (and (numberp cmp) (< cmp 0))))))) |
| 1596 | ;; The comparison predicate arg is new in Emacs-22. |
| 1597 | (wrong-number-of-arguments |
| 1598 | (sort-subr nil 'forward-line 'end-of-line |
| 1599 | (lambda () |
| 1600 | (let ((name (buffer-substring-no-properties |
| 1601 | (point) (line-end-position)))) |
| 1602 | (cond |
| 1603 | ((member name active) (concat "1" name)) |
| 1604 | (t (concat "2" "name")))))))))) |
| 1605 | |
| 1606 | (defvar mpc--changed-selection) |
| 1607 | |
| 1608 | (defun mpc-reorder (&optional nodeactivate) |
| 1609 | "Reorder entries based on the currently active selections. |
| 1610 | I.e. split the current browser buffer into a first part containing the |
| 1611 | entries included in the selection, then a separator, and then the entries |
| 1612 | not included in the selection. |
| 1613 | Return non-nil if a selection was deactivated." |
| 1614 | (mpc-select-save |
| 1615 | (let ((constraints (mpc-constraints-get-current (current-buffer))) |
| 1616 | (active 'all)) |
| 1617 | ;; (unless (equal constraints mpc-constraints) |
| 1618 | ;; (setq-local mpc-constraints constraints) |
| 1619 | (dolist (cst constraints) |
| 1620 | (let ((vals (apply 'mpc-union |
| 1621 | (mapcar (lambda (val) |
| 1622 | (mpc-cmd-list mpc-tag (car cst) val)) |
| 1623 | (cdr cst))))) |
| 1624 | (setq active |
| 1625 | (if (listp active) (mpc-intersection active vals) vals)))) |
| 1626 | |
| 1627 | (when (and (listp active)) |
| 1628 | ;; Remove the selections if they are all in conflict with |
| 1629 | ;; other constraints. |
| 1630 | (let ((deactivate t)) |
| 1631 | (dolist (sel selection) |
| 1632 | (when (member sel active) (setq deactivate nil))) |
| 1633 | (when deactivate |
| 1634 | ;; Variable declared/used by `mpc-select-save'. |
| 1635 | (when selection |
| 1636 | (setq mpc--changed-selection t)) |
| 1637 | (unless nodeactivate |
| 1638 | (setq selection nil) |
| 1639 | (mapc 'delete-overlay mpc-select) |
| 1640 | (setq mpc-select nil) |
| 1641 | (mpc-tagbrowser-all-select))))) |
| 1642 | |
| 1643 | ;; FIXME: This `mpc-sort' takes a lot of time. Maybe we should |
| 1644 | ;; be more clever and presume the buffer is mostly sorted already. |
| 1645 | (mpc-sort (if (listp active) active)) |
| 1646 | (mpc-separator (if (listp active) active))))) |
| 1647 | |
| 1648 | (defun mpc-selection-refresh () |
| 1649 | (let ((mpc--changed-selection t)) |
| 1650 | (while mpc--changed-selection |
| 1651 | (setq mpc--changed-selection nil) |
| 1652 | (dolist (buf (process-get (mpc-proc) 'buffers)) |
| 1653 | (setq buf (cdr buf)) |
| 1654 | (when (and (buffer-local-value 'mpc-tag buf) |
| 1655 | (not (eq buf (current-buffer)))) |
| 1656 | (with-current-buffer buf (mpc-reorder))))) |
| 1657 | ;; FIXME: reorder the current buffer last and prevent deactivation, |
| 1658 | ;; since whatever selection we made here is the most recent one |
| 1659 | ;; and should hence take precedence. |
| 1660 | (when mpc-tag (mpc-reorder 'nodeactivate)) |
| 1661 | ;; FIXME: comment? |
| 1662 | (if (and mpc--song-search mpc--changed-selection) |
| 1663 | (progn |
| 1664 | (setq mpc--song-search nil) |
| 1665 | (mpc-selection-refresh)) |
| 1666 | (mpc-songs-refresh)))) |
| 1667 | |
| 1668 | ;;; Hierarchical tagbrowser ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1669 | ;; Todo: |
| 1670 | ;; - Add a button on each dir to open/close it (?) |
| 1671 | ;; - add the parent dir on the previous line, grayed-out, if it's not |
| 1672 | ;; present (because we're in the non-selected part and the parent is |
| 1673 | ;; in the selected part). |
| 1674 | |
| 1675 | (defvar mpc-tagbrowser-dir-mode-map |
| 1676 | (let ((map (make-sparse-keymap))) |
| 1677 | (set-keymap-parent map mpc-tagbrowser-mode-map) |
| 1678 | (define-key map [?\M-\C-m] 'mpc-tagbrowser-dir-toggle) |
| 1679 | map)) |
| 1680 | |
| 1681 | ;; (defvar mpc-tagbrowser-dir-keywords |
| 1682 | ;; '(mpc-tagbrowser-dir-hide-prefix)) |
| 1683 | |
| 1684 | (define-derived-mode mpc-tagbrowser-dir-mode mpc-tagbrowser-mode '("MPC-" mpc-tag-name) |
| 1685 | ;; (setq-local font-lock-defaults |
| 1686 | ;; '(mpc-tagbrowser-dir-keywords t)) |
| 1687 | ) |
| 1688 | |
| 1689 | ;; (defun mpc-tagbrowser-dir-hide-prefix (limit) |
| 1690 | ;; (while |
| 1691 | ;; (let ((prev (buffer-substring (line-beginning-position 0) |
| 1692 | ;; (line-end-position 0)))) |
| 1693 | ;; ( |
| 1694 | |
| 1695 | (defun mpc-tagbrowser-dir-toggle (event) |
| 1696 | "Open or close the element at point." |
| 1697 | (interactive (list last-nonmenu-event)) |
| 1698 | (mpc-event-set-point event) |
| 1699 | (let ((name (buffer-substring (line-beginning-position) |
| 1700 | (line-end-position))) |
| 1701 | (prop (intern mpc-tag)) |
| 1702 | (proc (mpc-proc))) |
| 1703 | (if (not (member name (process-get proc prop))) |
| 1704 | (process-put proc prop |
| 1705 | (cons name (process-get proc prop))) |
| 1706 | (let ((new (delete name (process-get proc prop)))) |
| 1707 | (setq name (concat name "/")) |
| 1708 | (process-put proc prop |
| 1709 | (delq nil |
| 1710 | (mapcar (lambda (x) |
| 1711 | (if (string-prefix-p name x) |
| 1712 | nil x)) |
| 1713 | new))))) |
| 1714 | (mpc-tagbrowser-refresh))) |
| 1715 | |
| 1716 | |
| 1717 | ;;; Playlist management ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1718 | |
| 1719 | (defvar-local mpc-songs-playlist nil |
| 1720 | "Name of the currently selected playlist, if any. |
| 1721 | A value of t means the main playlist.") |
| 1722 | |
| 1723 | (defun mpc-playlist-create (name) |
| 1724 | "Save current playlist under name NAME." |
| 1725 | (interactive "sPlaylist name: ") |
| 1726 | (mpc-proc-cmd (list "save" name)) |
| 1727 | (let ((buf (mpc-proc-buffer (mpc-proc) 'Playlist))) |
| 1728 | (when (buffer-live-p buf) |
| 1729 | (with-current-buffer buf (mpc-tagbrowser-refresh))))) |
| 1730 | |
| 1731 | (defun mpc-playlist-destroy (name) |
| 1732 | "Delete playlist named NAME." |
| 1733 | (interactive |
| 1734 | (list (completing-read "Delete playlist: " (mpc-cmd-list 'Playlist) |
| 1735 | nil 'require-match))) |
| 1736 | (mpc-proc-cmd (list "rm" name)) |
| 1737 | (let ((buf (mpc-proc-buffer (mpc-proc) 'Playlist))) |
| 1738 | (when (buffer-live-p buf) |
| 1739 | (with-current-buffer buf (mpc-tagbrowser-refresh))))) |
| 1740 | |
| 1741 | (defun mpc-playlist-rename (oldname newname) |
| 1742 | "Rename playlist OLDNAME to NEWNAME." |
| 1743 | (interactive |
| 1744 | (let* ((oldname (if (and (eq mpc-tag 'Playlist) (null current-prefix-arg)) |
| 1745 | (buffer-substring (line-beginning-position) |
| 1746 | (line-end-position)) |
| 1747 | (completing-read "Rename playlist: " |
| 1748 | (mpc-cmd-list 'Playlist) |
| 1749 | nil 'require-match))) |
| 1750 | (newname (read-string (format "Rename '%s' to: " oldname)))) |
| 1751 | (if (zerop (length newname)) |
| 1752 | (error "Aborted") |
| 1753 | (list oldname newname)))) |
| 1754 | (mpc-proc-cmd (list "rename" oldname newname)) |
| 1755 | (let ((buf (mpc-proc-buffer (mpc-proc) 'Playlist))) |
| 1756 | (if (buffer-live-p buf) |
| 1757 | (with-current-buffer buf (mpc-tagbrowser-refresh))))) |
| 1758 | |
| 1759 | (defun mpc-playlist () |
| 1760 | "Show the current playlist." |
| 1761 | (interactive) |
| 1762 | (mpc-constraints-push 'noerror) |
| 1763 | (mpc-constraints-restore '())) |
| 1764 | |
| 1765 | (defun mpc-playlist-add () |
| 1766 | "Add the selection to the playlist." |
| 1767 | (interactive) |
| 1768 | (let ((songs (mapcar #'car (mpc-songs-selection)))) |
| 1769 | (mpc-cmd-add songs) |
| 1770 | (message "Appended %d songs" (length songs)) |
| 1771 | ;; Return the songs added. Used in `mpc-play'. |
| 1772 | songs)) |
| 1773 | |
| 1774 | (defun mpc-playlist-delete () |
| 1775 | "Remove the selected songs from the playlist." |
| 1776 | (interactive) |
| 1777 | (unless mpc-songs-playlist |
| 1778 | (error "The selected songs aren't part of a playlist")) |
| 1779 | (let ((song-poss (mapcar #'cdr (mpc-songs-selection)))) |
| 1780 | (mpc-cmd-delete song-poss mpc-songs-playlist) |
| 1781 | (mpc-songs-refresh) |
| 1782 | (message "Deleted %d songs" (length song-poss)))) |
| 1783 | |
| 1784 | ;;; Volume management ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1785 | |
| 1786 | (defvar mpc-volume-map |
| 1787 | (let ((map (make-sparse-keymap))) |
| 1788 | ;; Bind the up-events rather than the down-event, so the |
| 1789 | ;; `message' isn't canceled by the subsequent up-event binding. |
| 1790 | (define-key map [down-mouse-1] 'ignore) |
| 1791 | (define-key map [mouse-1] 'mpc-volume-mouse-set) |
| 1792 | (define-key map [header-line mouse-1] 'mpc-volume-mouse-set) |
| 1793 | (define-key map [header-line down-mouse-1] 'ignore) |
| 1794 | (define-key map [mode-line mouse-1] 'mpc-volume-mouse-set) |
| 1795 | (define-key map [mode-line down-mouse-1] 'ignore) |
| 1796 | map)) |
| 1797 | |
| 1798 | (defvar mpc-volume nil) (put 'mpc-volume 'risky-local-variable t) |
| 1799 | |
| 1800 | (defun mpc-volume-refresh () |
| 1801 | ;; Maintain the volume. |
| 1802 | (setq mpc-volume |
| 1803 | (mpc-volume-widget |
| 1804 | (string-to-number (cdr (assq 'volume mpc-status)))))) |
| 1805 | |
| 1806 | (defvar mpc-volume-step 5) |
| 1807 | |
| 1808 | (defun mpc-volume-mouse-set (&optional event) |
| 1809 | "Change volume setting." |
| 1810 | (interactive (list last-nonmenu-event)) |
| 1811 | (let* ((posn (event-start event)) |
| 1812 | (diff |
| 1813 | (if (memq (if (stringp (car-safe (posn-object posn))) |
| 1814 | (aref (car (posn-object posn)) (cdr (posn-object posn))) |
| 1815 | (with-current-buffer (window-buffer (posn-window posn)) |
| 1816 | (char-after (posn-point posn)))) |
| 1817 | '(?◁ ?<)) |
| 1818 | (- mpc-volume-step) mpc-volume-step)) |
| 1819 | (curvol (string-to-number (cdr (assq 'volume mpc-status)))) |
| 1820 | (newvol (max 0 (min 100 (+ curvol diff))))) |
| 1821 | (if (= newvol curvol) |
| 1822 | (progn |
| 1823 | (message "MPD volume already at %s%%" newvol) |
| 1824 | (ding)) |
| 1825 | (mpc-proc-cmd (list "setvol" newvol) 'mpc-status-refresh) |
| 1826 | (message "Set MPD volume to %s%%" newvol)))) |
| 1827 | |
| 1828 | (defun mpc-volume-widget (vol &optional size) |
| 1829 | (unless size (setq size 12.5)) |
| 1830 | (let ((scaledvol (* (/ vol 100.0) size))) |
| 1831 | ;; (message "Volume sizes: %s - %s" (/ vol fact) (/ (- 100 vol) fact)) |
| 1832 | (list (propertize "<" ;; "◁" |
| 1833 | ;; 'face 'default |
| 1834 | 'keymap mpc-volume-map |
| 1835 | 'face '(:box (:line-width -2 :style pressed-button)) |
| 1836 | 'mouse-face '(:box (:line-width -2 :style released-button))) |
| 1837 | " " |
| 1838 | (propertize "a" |
| 1839 | 'display (list 'space :width scaledvol) |
| 1840 | 'face '(:inverse-video t |
| 1841 | :box (:line-width -2 :style released-button))) |
| 1842 | (propertize "a" |
| 1843 | 'display (list 'space :width (- size scaledvol)) |
| 1844 | 'face '(:box (:line-width -2 :style released-button))) |
| 1845 | " " |
| 1846 | (propertize ">" ;; "▷" |
| 1847 | ;; 'face 'default |
| 1848 | 'keymap mpc-volume-map |
| 1849 | 'face '(:box (:line-width -2 :style pressed-button)) |
| 1850 | 'mouse-face '(:box (:line-width -2 :style released-button)))))) |
| 1851 | |
| 1852 | ;;; MPC songs mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1853 | |
| 1854 | (defvar mpc-current-song nil) (put 'mpc-current-song 'risky-local-variable t) |
| 1855 | (defvar mpc-current-updating nil) (put 'mpc-current-updating 'risky-local-variable t) |
| 1856 | (defvar mpc-songs-format-description nil) (put 'mpc-songs-format-description 'risky-local-variable t) |
| 1857 | |
| 1858 | (defvar mpc-previous-window-config nil) |
| 1859 | |
| 1860 | (defvar mpc-songs-mode-map |
| 1861 | (let ((map (make-sparse-keymap))) |
| 1862 | (set-keymap-parent map mpc-mode-map) |
| 1863 | (define-key map [remap mpc-select] 'mpc-songs-jump-to) |
| 1864 | map)) |
| 1865 | |
| 1866 | (defvar mpc-songpointer-set-visible nil) |
| 1867 | |
| 1868 | (defvar mpc-songs-hashcons (make-hash-table :test 'equal :weakness t) |
| 1869 | "Make song file name objects unique via hash consing. |
| 1870 | This is used so that they can be compared with `eq', which is needed for |
| 1871 | `text-property-any'.") |
| 1872 | (defun mpc-songs-hashcons (name) |
| 1873 | (or (gethash name mpc-songs-hashcons) (puthash name name mpc-songs-hashcons))) |
| 1874 | (defcustom mpc-songs-format "%2{Disc--}%3{Track} %-5{Time} %25{Title} %20{Album} %20{Artist} %5{Date}" |
| 1875 | "Format used to display each song in the list of songs." |
| 1876 | :type 'string) |
| 1877 | |
| 1878 | (defvar mpc-songs-totaltime) |
| 1879 | |
| 1880 | (defun mpc-songs-refresh () |
| 1881 | (let ((buf (mpc-proc-buffer (mpc-proc) 'songs))) |
| 1882 | (when (buffer-live-p buf) |
| 1883 | (with-current-buffer buf |
| 1884 | (let ((constraints (mpc-constraints-get-current (current-buffer))) |
| 1885 | (dontsort nil) |
| 1886 | (inhibit-read-only t) |
| 1887 | (totaltime 0) |
| 1888 | (curline (cons (count-lines (point-min) |
| 1889 | (line-beginning-position)) |
| 1890 | (buffer-substring (line-beginning-position) |
| 1891 | (line-end-position)))) |
| 1892 | active) |
| 1893 | (setq mpc-songs-playlist nil) |
| 1894 | (if (null constraints) |
| 1895 | ;; When there are no constraints, rather than show the list of |
| 1896 | ;; all songs (which could take a while to download and |
| 1897 | ;; format), we show the current playlist. |
| 1898 | ;; FIXME: it would be good to be able to show the complete |
| 1899 | ;; list, but that would probably require us to format it |
| 1900 | ;; on-the-fly to make it bearable. |
| 1901 | (setq dontsort t |
| 1902 | mpc-songs-playlist t |
| 1903 | active (mpc-proc-buf-to-alists |
| 1904 | (mpc-proc-cmd "playlistinfo"))) |
| 1905 | (dolist (cst constraints) |
| 1906 | (if (and (eq (car cst) 'Playlist) |
| 1907 | (= 1 (length (cdr cst)))) |
| 1908 | (setq mpc-songs-playlist (cadr cst))) |
| 1909 | ;; We don't do anything really special here for playlists, |
| 1910 | ;; because it's unclear what's a correct "union" of playlists. |
| 1911 | (let ((vals (apply 'mpc-union |
| 1912 | (mapcar (lambda (val) |
| 1913 | (mpc-cmd-find (car cst) val)) |
| 1914 | (cdr cst))))) |
| 1915 | (setq active (cond |
| 1916 | ((null active) |
| 1917 | (if (eq (car cst) 'Playlist) |
| 1918 | (setq dontsort t)) |
| 1919 | vals) |
| 1920 | ((or dontsort |
| 1921 | ;; Try to preserve ordering and |
| 1922 | ;; repetitions from playlists. |
| 1923 | (not (eq (car cst) 'Playlist))) |
| 1924 | (mpc-intersection active vals |
| 1925 | (lambda (x) (assq 'file x)))) |
| 1926 | (t |
| 1927 | (setq dontsort t) |
| 1928 | (mpc-intersection vals active |
| 1929 | (lambda (x) |
| 1930 | (assq 'file x))))))))) |
| 1931 | (mpc-select-save |
| 1932 | (erase-buffer) |
| 1933 | ;; Sorting songs is surprisingly difficult: when comparing two |
| 1934 | ;; songs with the same album name but different artist name, you |
| 1935 | ;; have to know whether these are two different albums (with the |
| 1936 | ;; same name) or a single album (typically a compilation). |
| 1937 | ;; I punt on it and just use file-name sorting, which does the |
| 1938 | ;; right thing if your library is properly arranged. |
| 1939 | (dolist (song (if dontsort active |
| 1940 | (sort active |
| 1941 | (lambda (song1 song2) |
| 1942 | (let ((cmp (mpc-compare-strings |
| 1943 | (cdr (assq 'file song1)) |
| 1944 | (cdr (assq 'file song2))))) |
| 1945 | (and (integerp cmp) (< cmp 0))))))) |
| 1946 | (cl-incf totaltime (string-to-number (or (cdr (assq 'Time song)) "0"))) |
| 1947 | (mpc-format mpc-songs-format song) |
| 1948 | (delete-char (- (skip-chars-backward " "))) ;Remove trailing space. |
| 1949 | (insert "\n") |
| 1950 | (put-text-property |
| 1951 | (line-beginning-position 0) (line-beginning-position) |
| 1952 | 'mpc-file (mpc-songs-hashcons (cdr (assq 'file song)))) |
| 1953 | (let ((pos (assq 'Pos song))) |
| 1954 | (if pos |
| 1955 | (put-text-property |
| 1956 | (line-beginning-position 0) (line-beginning-position) |
| 1957 | 'mpc-file-pos (string-to-number (cdr pos))))) |
| 1958 | )) |
| 1959 | (goto-char (point-min)) |
| 1960 | (forward-line (car curline)) |
| 1961 | (if (or (search-forward (cdr curline) nil t) |
| 1962 | (search-backward (cdr curline) nil t)) |
| 1963 | (beginning-of-line) |
| 1964 | (goto-char (point-min))) |
| 1965 | (setq-local mpc-songs-totaltime |
| 1966 | (unless (zerop totaltime) |
| 1967 | (list " " (mpc-secs-to-time totaltime)))) |
| 1968 | )))) |
| 1969 | (let ((mpc-songpointer-set-visible t)) |
| 1970 | (mpc-songpointer-refresh))) |
| 1971 | |
| 1972 | (defun mpc-songs-search (string) |
| 1973 | "Filter songs to those who include STRING in their metadata." |
| 1974 | (interactive "sSearch for: ") |
| 1975 | (setq mpc--song-search |
| 1976 | (if (zerop (length string)) nil string)) |
| 1977 | (let ((mpc--changed-selection t)) |
| 1978 | (while mpc--changed-selection |
| 1979 | (setq mpc--changed-selection nil) |
| 1980 | (dolist (buf (process-get (mpc-proc) 'buffers)) |
| 1981 | (setq buf (cdr buf)) |
| 1982 | (when (buffer-local-value 'mpc-tag buf) |
| 1983 | (with-current-buffer buf (mpc-reorder)))) |
| 1984 | (mpc-songs-refresh)))) |
| 1985 | |
| 1986 | (defun mpc-songs-kill-search () |
| 1987 | "Turn off the current search restriction." |
| 1988 | (interactive) |
| 1989 | (mpc-songs-search nil)) |
| 1990 | |
| 1991 | (defun mpc-songs-selection () |
| 1992 | "Return the list of songs currently selected." |
| 1993 | (let ((buf (mpc-proc-buffer (mpc-proc) 'songs))) |
| 1994 | (when (buffer-live-p buf) |
| 1995 | (with-current-buffer buf |
| 1996 | (save-excursion |
| 1997 | (let ((files ())) |
| 1998 | (if mpc-select |
| 1999 | (dolist (ol mpc-select) |
| 2000 | (push (cons |
| 2001 | (get-text-property (overlay-start ol) 'mpc-file) |
| 2002 | (get-text-property (overlay-start ol) 'mpc-file-pos)) |
| 2003 | files)) |
| 2004 | (goto-char (point-min)) |
| 2005 | (while (not (eobp)) |
| 2006 | (push (cons |
| 2007 | (get-text-property (point) 'mpc-file) |
| 2008 | (get-text-property (point) 'mpc-file-pos)) |
| 2009 | files) |
| 2010 | (forward-line 1))) |
| 2011 | (nreverse files))))))) |
| 2012 | |
| 2013 | (defun mpc-songs-jump-to (song-file &optional posn) |
| 2014 | "Jump to song SONG-FILE; interactively, this is the song at point." |
| 2015 | (interactive |
| 2016 | (let* ((event last-nonmenu-event) |
| 2017 | (posn (event-end event))) |
| 2018 | (with-selected-window (posn-window posn) |
| 2019 | (goto-char (posn-point posn)) |
| 2020 | (list (get-text-property (point) 'mpc-file) |
| 2021 | posn)))) |
| 2022 | (let* ((plbuf (mpc-proc-cmd "playlist")) |
| 2023 | (re (if song-file |
| 2024 | ;; Newer MPCs apparently include "file: " in the buffer. |
| 2025 | (concat "^\\([0-9]+\\):\\(?:file: \\)?" |
| 2026 | (regexp-quote song-file) "$"))) |
| 2027 | (sn (with-current-buffer plbuf |
| 2028 | (goto-char (point-min)) |
| 2029 | (when (and re (re-search-forward re nil t)) |
| 2030 | (match-string 1))))) |
| 2031 | (cond |
| 2032 | ((null re) (posn-set-point posn)) |
| 2033 | ((null sn) (error "This song is not in the playlist")) |
| 2034 | ((null (with-current-buffer plbuf (re-search-forward re nil t))) |
| 2035 | ;; song-file only appears once in the playlist: no ambiguity, |
| 2036 | ;; we're good to go! |
| 2037 | (mpc-proc-cmd (list "play" sn))) |
| 2038 | (t |
| 2039 | ;; The song appears multiple times in the playlist. If the current |
| 2040 | ;; buffer holds not only the destination song but also the current |
| 2041 | ;; song, then we will move in the playlist to the same relative |
| 2042 | ;; position as in the buffer. Otherwise, we will simply choose the |
| 2043 | ;; song occurrence closest to the current song. |
| 2044 | (with-selected-window (posn-window posn) |
| 2045 | (let* ((cur (and (markerp overlay-arrow-position) |
| 2046 | (marker-position overlay-arrow-position))) |
| 2047 | (dest (save-excursion |
| 2048 | (goto-char (posn-point posn)) |
| 2049 | (line-beginning-position))) |
| 2050 | (lines (when cur (* (if (< cur dest) 1 -1) |
| 2051 | (count-lines cur dest))))) |
| 2052 | (with-current-buffer plbuf |
| 2053 | (goto-char (point-min)) |
| 2054 | ;; Start the search from the current song. |
| 2055 | (forward-line (string-to-number |
| 2056 | (or (cdr (assq 'song mpc-status)) "0"))) |
| 2057 | ;; If the current song is also displayed in the buffer, |
| 2058 | ;; then try to move to the same relative position. |
| 2059 | (if lines (forward-line lines)) |
| 2060 | ;; Now search the closest occurrence. |
| 2061 | (let* ((next (save-excursion |
| 2062 | (when (re-search-forward re nil t) |
| 2063 | (cons (point) (match-string 1))))) |
| 2064 | (prev (save-excursion |
| 2065 | (when (re-search-backward re nil t) |
| 2066 | (cons (point) (match-string 1))))) |
| 2067 | (sn (cdr (if (and next prev) |
| 2068 | (if (< (- (car next) (point)) |
| 2069 | (- (point) (car prev))) |
| 2070 | next prev) |
| 2071 | (or next prev))))) |
| 2072 | (cl-assert sn) |
| 2073 | (mpc-proc-cmd (concat "play " sn)))))))))) |
| 2074 | |
| 2075 | (define-derived-mode mpc-songs-mode mpc-mode "MPC-song" |
| 2076 | (setq mpc-songs-format-description |
| 2077 | (with-temp-buffer (mpc-format mpc-songs-format 'self) (buffer-string))) |
| 2078 | (setq-local header-line-format |
| 2079 | ;; '("MPC " mpc-volume " " mpc-current-song) |
| 2080 | (list (propertize " " 'display '(space :align-to 0)) |
| 2081 | ;; 'mpc-songs-format-description |
| 2082 | '(:eval |
| 2083 | (let ((hscroll (window-hscroll))) |
| 2084 | (with-temp-buffer |
| 2085 | (mpc-format mpc-songs-format 'self hscroll) |
| 2086 | ;; That would be simpler than the hscroll handling in |
| 2087 | ;; mpc-format, but currently move-to-column does not |
| 2088 | ;; recognize :space display properties. |
| 2089 | ;; (move-to-column hscroll) |
| 2090 | ;; (delete-region (point-min) (point)) |
| 2091 | (buffer-string)))))) |
| 2092 | (setq-local |
| 2093 | mode-line-format |
| 2094 | '("%e" mode-line-frame-identification mode-line-buffer-identification |
| 2095 | #(" " 0 3 |
| 2096 | (help-echo "mouse-1: Select (drag to resize)\nmouse-2: Make current window occupy the whole frame\nmouse-3: Remove current window from display")) |
| 2097 | mode-line-position |
| 2098 | #(" " 0 2 |
| 2099 | (help-echo "mouse-1: Select (drag to resize)\nmouse-2: Make current window occupy the whole frame\nmouse-3: Remove current window from display")) |
| 2100 | mpc-songs-totaltime |
| 2101 | mpc-current-updating |
| 2102 | #(" " 0 2 |
| 2103 | (help-echo "mouse-1: Select (drag to resize)\nmouse-2: Make current window occupy the whole frame\nmouse-3: Remove current window from display")) |
| 2104 | (mpc--song-search |
| 2105 | (:propertize |
| 2106 | ("Search=\"" mpc--song-search "\"") |
| 2107 | help-echo "mouse-2: kill this search" |
| 2108 | follow-link t |
| 2109 | mouse-face mode-line-highlight |
| 2110 | keymap (keymap (mode-line keymap |
| 2111 | (mouse-2 . mpc-songs-kill-search)))) |
| 2112 | (:propertize "NoSearch" |
| 2113 | help-echo "mouse-2: set a search restriction" |
| 2114 | follow-link t |
| 2115 | mouse-face mode-line-highlight |
| 2116 | keymap (keymap (mode-line keymap (mouse-2 . mpc-songs-search))))))) |
| 2117 | |
| 2118 | ;; (setq-local mode-line-process |
| 2119 | ;; '("" ;; mpc-volume " " |
| 2120 | ;; mpc-songs-totaltime |
| 2121 | ;; mpc-current-updating)) |
| 2122 | ) |
| 2123 | |
| 2124 | (defun mpc-songpointer-set (pos) |
| 2125 | (let* ((win (get-buffer-window (current-buffer) t)) |
| 2126 | (visible (when win |
| 2127 | (or mpc-songpointer-set-visible |
| 2128 | (and (markerp overlay-arrow-position) |
| 2129 | (eq (marker-buffer overlay-arrow-position) |
| 2130 | (current-buffer)) |
| 2131 | (<= (window-start win) overlay-arrow-position) |
| 2132 | (< overlay-arrow-position (window-end win))))))) |
| 2133 | (unless (local-variable-p 'overlay-arrow-position) |
| 2134 | (setq-local overlay-arrow-position (make-marker))) |
| 2135 | (move-marker overlay-arrow-position pos) |
| 2136 | ;; If the arrow was visible, try to keep it that way. |
| 2137 | (if (and visible pos |
| 2138 | (or (> (window-start win) pos) (>= pos (window-end win t)))) |
| 2139 | (set-window-point win pos)))) |
| 2140 | |
| 2141 | (defun mpc-songpointer-refresh () |
| 2142 | (let ((buf (mpc-proc-buffer (mpc-proc) 'songs))) |
| 2143 | (when (buffer-live-p buf) |
| 2144 | (with-current-buffer buf |
| 2145 | (let* ((pos (text-property-any |
| 2146 | (point-min) (point-max) |
| 2147 | 'mpc-file (mpc-songs-hashcons |
| 2148 | (cdr (assq 'file mpc-status))))) |
| 2149 | (other (when pos |
| 2150 | (save-excursion |
| 2151 | (goto-char pos) |
| 2152 | (text-property-any |
| 2153 | (line-beginning-position 2) (point-max) |
| 2154 | 'mpc-file (mpc-songs-hashcons |
| 2155 | (cdr (assq 'file mpc-status)))))))) |
| 2156 | (if other |
| 2157 | ;; The song appears multiple times in the buffer. |
| 2158 | ;; We need to be careful to choose the right occurrence. |
| 2159 | (mpc-proc-cmd "playlist" 'mpc-songpointer-refresh-hairy) |
| 2160 | (mpc-songpointer-set pos))))))) |
| 2161 | |
| 2162 | (defun mpc-songpointer-context (size plbuf) |
| 2163 | (with-current-buffer plbuf |
| 2164 | (goto-char (point-min)) |
| 2165 | (forward-line (string-to-number (or (cdr (assq 'song mpc-status)) "0"))) |
| 2166 | (let ((context-before '()) |
| 2167 | (context-after '())) |
| 2168 | (save-excursion |
| 2169 | (dotimes (_i size) |
| 2170 | (when (re-search-backward "^[0-9]+:\\(.*\\)" nil t) |
| 2171 | (push (mpc-songs-hashcons (match-string 1)) context-before)))) |
| 2172 | ;; Skip the actual current song. |
| 2173 | (forward-line 1) |
| 2174 | (dotimes (_i size) |
| 2175 | (when (re-search-forward "^[0-9]+:\\(.*\\)" nil t) |
| 2176 | (push (mpc-songs-hashcons (match-string 1)) context-after))) |
| 2177 | ;; If there isn't `size' context, then return nil. |
| 2178 | (unless (and (< (length context-before) size) |
| 2179 | (< (length context-after) size)) |
| 2180 | (cons (nreverse context-before) (nreverse context-after)))))) |
| 2181 | |
| 2182 | (defun mpc-songpointer-score (context pos) |
| 2183 | (let ((count 0)) |
| 2184 | (goto-char pos) |
| 2185 | (dolist (song (car context)) |
| 2186 | (and (zerop (forward-line -1)) |
| 2187 | (eq (get-text-property (point) 'mpc-file) song) |
| 2188 | (cl-incf count))) |
| 2189 | (goto-char pos) |
| 2190 | (dolist (song (cdr context)) |
| 2191 | (and (zerop (forward-line 1)) |
| 2192 | (eq (get-text-property (point) 'mpc-file) song) |
| 2193 | (cl-incf count))) |
| 2194 | count)) |
| 2195 | |
| 2196 | (defun mpc-songpointer-refresh-hairy () |
| 2197 | ;; Based on the complete playlist, we should figure out where in the |
| 2198 | ;; song buffer is the currently playing song. |
| 2199 | (let ((plbuf (current-buffer)) |
| 2200 | (buf (mpc-proc-buffer (mpc-proc) 'songs))) |
| 2201 | (when (buffer-live-p buf) |
| 2202 | (with-current-buffer buf |
| 2203 | (let* ((context-size 0) |
| 2204 | (context '(() . ())) |
| 2205 | (pos (text-property-any |
| 2206 | (point-min) (point-max) |
| 2207 | 'mpc-file (mpc-songs-hashcons |
| 2208 | (cdr (assq 'file mpc-status))))) |
| 2209 | (score 0) |
| 2210 | (other pos)) |
| 2211 | (while |
| 2212 | (setq other |
| 2213 | (save-excursion |
| 2214 | (goto-char other) |
| 2215 | (text-property-any |
| 2216 | (line-beginning-position 2) (point-max) |
| 2217 | 'mpc-file (mpc-songs-hashcons |
| 2218 | (cdr (assq 'file mpc-status)))))) |
| 2219 | ;; There is an `other' contestant. |
| 2220 | (let ((other-score (mpc-songpointer-score context other))) |
| 2221 | (cond |
| 2222 | ;; `other' is worse: try the next one. |
| 2223 | ((< other-score score) nil) |
| 2224 | ;; `other' is better: remember it and then search further. |
| 2225 | ((> other-score score) |
| 2226 | (setq pos other) |
| 2227 | (setq score other-score)) |
| 2228 | ;; Both are equal and increasing the context size won't help. |
| 2229 | ;; Arbitrarily choose one of the two and keep looking |
| 2230 | ;; for a better match. |
| 2231 | ((< score context-size) nil) |
| 2232 | (t |
| 2233 | ;; Score is equal and increasing context might help: try it. |
| 2234 | (cl-incf context-size) |
| 2235 | (let ((new-context |
| 2236 | (mpc-songpointer-context context-size plbuf))) |
| 2237 | (if (null new-context) |
| 2238 | ;; There isn't more context: choose one arbitrarily |
| 2239 | ;; and keep looking for a better match elsewhere. |
| 2240 | (cl-decf context-size) |
| 2241 | (setq context new-context) |
| 2242 | (setq score (mpc-songpointer-score context pos)) |
| 2243 | (save-excursion |
| 2244 | (goto-char other) |
| 2245 | ;; Go back one line so we find `other' again. |
| 2246 | (setq other (line-beginning-position 0))))))))) |
| 2247 | (mpc-songpointer-set pos)))))) |
| 2248 | |
| 2249 | (defun mpc-current-refresh () |
| 2250 | ;; Maintain the current data. |
| 2251 | (mpc-status-buffer-refresh) |
| 2252 | (setq mpc-current-updating |
| 2253 | (if (assq 'updating_db mpc-status) " Updating-DB")) |
| 2254 | (ignore-errors |
| 2255 | (setq mpc-current-song |
| 2256 | (when (assq 'file mpc-status) |
| 2257 | (concat " " |
| 2258 | (mpc-secs-to-time (cdr (assq 'time mpc-status))) |
| 2259 | " " |
| 2260 | (cdr (assq 'Title mpc-status)) |
| 2261 | " (" |
| 2262 | (cdr (assq 'Artist mpc-status)) |
| 2263 | " / " |
| 2264 | (cdr (assq 'Album mpc-status)) |
| 2265 | ")")))) |
| 2266 | (force-mode-line-update t)) |
| 2267 | |
| 2268 | (defun mpc-songs-buf () |
| 2269 | (let ((buf (mpc-proc-buffer (mpc-proc) 'songs))) |
| 2270 | (if (buffer-live-p buf) buf |
| 2271 | (with-current-buffer (setq buf (get-buffer-create "*MPC-Songs*")) |
| 2272 | (mpc-proc-buffer (mpc-proc) 'songs buf) |
| 2273 | (mpc-songs-mode) |
| 2274 | buf)))) |
| 2275 | |
| 2276 | (defun mpc-update () |
| 2277 | "Tell MPD to refresh its database." |
| 2278 | (interactive) |
| 2279 | (mpc-cmd-update)) |
| 2280 | |
| 2281 | (defun mpc-quit () |
| 2282 | "Quit Music Player Daemon." |
| 2283 | (interactive) |
| 2284 | (let* ((proc mpc-proc) |
| 2285 | (bufs (mapcar 'cdr (if proc (process-get proc 'buffers)))) |
| 2286 | (wins (mapcar (lambda (buf) (get-buffer-window buf 0)) bufs)) |
| 2287 | (song-buf (mpc-songs-buf)) |
| 2288 | frames) |
| 2289 | ;; Collect all the frames where MPC buffers appear. |
| 2290 | (dolist (win wins) |
| 2291 | (when (and win (not (memq (window-frame win) frames))) |
| 2292 | (push (window-frame win) frames))) |
| 2293 | (if (and frames song-buf |
| 2294 | (with-current-buffer song-buf mpc-previous-window-config)) |
| 2295 | (progn |
| 2296 | (select-frame (car frames)) |
| 2297 | (set-window-configuration |
| 2298 | (with-current-buffer song-buf mpc-previous-window-config))) |
| 2299 | ;; Now delete the ones that show nothing else than MPC buffers. |
| 2300 | (dolist (frame frames) |
| 2301 | (let ((delete t)) |
| 2302 | (dolist (win (window-list frame)) |
| 2303 | (unless (memq (window-buffer win) bufs) (setq delete nil))) |
| 2304 | (if delete (ignore-errors (delete-frame frame)))))) |
| 2305 | ;; Then kill the buffers. |
| 2306 | (mapc 'kill-buffer bufs) |
| 2307 | (mpc-status-stop) |
| 2308 | (if proc (delete-process proc)))) |
| 2309 | |
| 2310 | (defun mpc-stop () |
| 2311 | "Stop playing the current queue of songs." |
| 2312 | (interactive) |
| 2313 | (mpc-cmd-stop) |
| 2314 | (mpc-cmd-clear) |
| 2315 | (mpc-status-refresh)) |
| 2316 | |
| 2317 | (defun mpc-pause () |
| 2318 | "Pause playing." |
| 2319 | (interactive) |
| 2320 | (mpc-cmd-pause "1")) |
| 2321 | |
| 2322 | (defun mpc-resume () |
| 2323 | "Resume playing." |
| 2324 | (interactive) |
| 2325 | (mpc-cmd-pause "0")) |
| 2326 | |
| 2327 | (defun mpc-play () |
| 2328 | "Start playing whatever is selected." |
| 2329 | (interactive) |
| 2330 | (if (member (cdr (assq 'state (mpc-cmd-status))) '("pause")) |
| 2331 | (mpc-resume) |
| 2332 | ;; When playing the playlist ends, the playlist isn't cleared, but the |
| 2333 | ;; user probably doesn't want to re-listen to it before getting to |
| 2334 | ;; listen to what he just selected. |
| 2335 | ;; (if (member (cdr (assq 'state (mpc-cmd-status))) '("stop")) |
| 2336 | ;; (mpc-cmd-clear)) |
| 2337 | ;; Actually, we don't use mpc-play to append to the playlist any more, |
| 2338 | ;; so we can just always empty the playlist. |
| 2339 | (mpc-cmd-clear) |
| 2340 | (if (mpc-playlist-add) |
| 2341 | (if (member (cdr (assq 'state (mpc-cmd-status))) '("stop")) |
| 2342 | (mpc-cmd-play)) |
| 2343 | (error "Don't know what to play")))) |
| 2344 | |
| 2345 | (defun mpc-next () |
| 2346 | "Jump to the next song in the queue." |
| 2347 | (interactive) |
| 2348 | (mpc-proc-cmd "next") |
| 2349 | (mpc-status-refresh)) |
| 2350 | |
| 2351 | (defun mpc-prev () |
| 2352 | "Jump to the beginning of the current song, or to the previous song." |
| 2353 | (interactive) |
| 2354 | (let ((time (cdr (assq 'time mpc-status)))) |
| 2355 | ;; Here we rely on the fact that string-to-number silently ignores |
| 2356 | ;; everything after a non-digit char. |
| 2357 | (cond |
| 2358 | ;; Go back to the beginning of current song. |
| 2359 | ((and time (> (string-to-number time) 0)) |
| 2360 | (mpc-proc-cmd (list "seekid" (cdr (assq 'songid mpc-status)) 0))) |
| 2361 | ;; We're at the beginning of the first song of the playlist. |
| 2362 | ;; Fetch the previous one from `mpc-queue-back'. |
| 2363 | ;; ((and (zerop (string-to-number (cdr (assq 'song mpc-status)))) |
| 2364 | ;; mpc-queue-back) |
| 2365 | ;; ;; Because we use cmd-list rather than cmd-play, the queue is not |
| 2366 | ;; ;; automatically updated. |
| 2367 | ;; (let ((prev (pop mpc-queue-back))) |
| 2368 | ;; (push prev mpc-queue) |
| 2369 | ;; (mpc-proc-cmd |
| 2370 | ;; (mpc-proc-cmd-list |
| 2371 | ;; (list (list "add" prev) |
| 2372 | ;; (list "move" (cdr (assq 'playlistlength mpc-status)) "0") |
| 2373 | ;; "previous"))))) |
| 2374 | ;; We're at the beginning of a song, but not the first one. |
| 2375 | (t (mpc-proc-cmd "previous"))) |
| 2376 | (mpc-status-refresh))) |
| 2377 | |
| 2378 | (defvar mpc-last-seek-time '(0 . 0)) |
| 2379 | |
| 2380 | (defun mpc--faster (event speedup step) |
| 2381 | "Fast forward." |
| 2382 | (interactive (list last-nonmenu-event)) |
| 2383 | (let ((repeat-delay (/ (abs (float step)) speedup))) |
| 2384 | (if (not (memq 'down (event-modifiers event))) |
| 2385 | (let* ((currenttime (float-time)) |
| 2386 | (last-time (- currenttime (car mpc-last-seek-time)))) |
| 2387 | (if (< last-time (* 0.9 repeat-delay)) |
| 2388 | nil ;; Throttle |
| 2389 | (let* ((status (if (< last-time 1.0) |
| 2390 | mpc-status (mpc-cmd-status))) |
| 2391 | (songid (cdr (assq 'songid status))) |
| 2392 | (time (if songid |
| 2393 | (if (< last-time 1.0) |
| 2394 | (cdr mpc-last-seek-time) |
| 2395 | (string-to-number |
| 2396 | (cdr (assq 'time status))))))) |
| 2397 | (setq mpc-last-seek-time |
| 2398 | (cons currenttime (setq time (+ time step)))) |
| 2399 | (mpc-proc-cmd (list "seekid" songid time) |
| 2400 | 'mpc-status-refresh)))) |
| 2401 | (let ((status (mpc-cmd-status))) |
| 2402 | (let* ((songid (cdr (assq 'songid status))) |
| 2403 | (time (if songid (string-to-number |
| 2404 | (cdr (assq 'time status)))))) |
| 2405 | (let ((timer (run-with-timer |
| 2406 | t repeat-delay |
| 2407 | (lambda () |
| 2408 | (mpc-proc-cmd (list "seekid" songid |
| 2409 | (setq time (+ time step))) |
| 2410 | 'mpc-status-refresh))))) |
| 2411 | (while (mouse-movement-p |
| 2412 | (event-basic-type (setq event (read-event))))) |
| 2413 | (cancel-timer timer))))))) |
| 2414 | |
| 2415 | (defvar mpc--faster-toggle-timer nil) |
| 2416 | (defun mpc--faster-stop () |
| 2417 | (when mpc--faster-toggle-timer |
| 2418 | (cancel-timer mpc--faster-toggle-timer) |
| 2419 | (setq mpc--faster-toggle-timer nil))) |
| 2420 | |
| 2421 | (defun mpc--faster-toggle-refresh () |
| 2422 | (if (equal (cdr (assq 'state mpc-status)) "stop") |
| 2423 | (mpc--faster-stop))) |
| 2424 | |
| 2425 | (defun mpc--songduration () |
| 2426 | (string-to-number |
| 2427 | (let ((s (cdr (assq 'time mpc-status)))) |
| 2428 | (if (not (string-match ":" s)) |
| 2429 | (error "Unexpected time format %S" s) |
| 2430 | (substring s (match-end 0)))))) |
| 2431 | |
| 2432 | (defvar mpc--faster-toggle-forward nil) |
| 2433 | (defvar mpc--faster-acceleration 0.5) |
| 2434 | (defun mpc--faster-toggle (speedup step) |
| 2435 | (setq speedup (float speedup)) |
| 2436 | (if mpc--faster-toggle-timer |
| 2437 | (mpc--faster-stop) |
| 2438 | (mpc-status-refresh) (mpc-proc-sync) |
| 2439 | (let* (songid ;The ID of the currently ffwd/rewinding song. |
| 2440 | songduration ;The duration of that song. |
| 2441 | songtime ;The time of the song last time we ran. |
| 2442 | oldtime ;The time of day last time we ran. |
| 2443 | prevsongid) ;The song we're in the process leaving. |
| 2444 | (let ((fun |
| 2445 | (lambda () |
| 2446 | (let ((newsongid (cdr (assq 'songid mpc-status)))) |
| 2447 | |
| 2448 | (if (and (equal prevsongid newsongid) |
| 2449 | (not (equal prevsongid songid))) |
| 2450 | ;; We left prevsongid and came back to it. Pretend it |
| 2451 | ;; didn't happen. |
| 2452 | (setq newsongid songid)) |
| 2453 | |
| 2454 | (cond |
| 2455 | ((null newsongid) (mpc--faster-stop)) |
| 2456 | ((not (equal songid newsongid)) |
| 2457 | ;; We jumped to another song: reset. |
| 2458 | (setq songid newsongid) |
| 2459 | (setq songtime (string-to-number |
| 2460 | (cdr (assq 'time mpc-status)))) |
| 2461 | (setq songduration (mpc--songduration)) |
| 2462 | (setq oldtime (float-time))) |
| 2463 | ((and (>= songtime songduration) mpc--faster-toggle-forward) |
| 2464 | ;; Skip to the beginning of the next song. |
| 2465 | (if (not (equal (cdr (assq 'state mpc-status)) "play")) |
| 2466 | (mpc-proc-cmd "next" 'mpc-status-refresh) |
| 2467 | ;; If we're playing, this is done automatically, so we |
| 2468 | ;; don't need to do anything, or rather we *shouldn't* |
| 2469 | ;; do anything otherwise there's a race condition where |
| 2470 | ;; we could skip straight to the next next song. |
| 2471 | nil)) |
| 2472 | ((and (<= songtime 0) (not mpc--faster-toggle-forward)) |
| 2473 | ;; Skip to the end of the previous song. |
| 2474 | (setq prevsongid songid) |
| 2475 | (mpc-proc-cmd "previous" |
| 2476 | (lambda () |
| 2477 | (mpc-status-refresh |
| 2478 | (lambda () |
| 2479 | (setq songid (cdr (assq 'songid mpc-status))) |
| 2480 | (setq songtime (setq songduration (mpc--songduration))) |
| 2481 | (setq oldtime (float-time)) |
| 2482 | (mpc-proc-cmd (list "seekid" songid songtime))))))) |
| 2483 | (t |
| 2484 | (setq speedup (+ speedup mpc--faster-acceleration)) |
| 2485 | (let ((newstep |
| 2486 | (truncate (* speedup (- (float-time) oldtime))))) |
| 2487 | (if (<= newstep 1) (setq newstep 1)) |
| 2488 | (setq oldtime (+ oldtime (/ newstep speedup))) |
| 2489 | (if (not mpc--faster-toggle-forward) |
| 2490 | (setq newstep (- newstep))) |
| 2491 | (setq songtime (min songduration (+ songtime newstep))) |
| 2492 | (unless (>= songtime songduration) |
| 2493 | (condition-case nil |
| 2494 | (mpc-proc-cmd |
| 2495 | (list "seekid" songid songtime) |
| 2496 | 'mpc-status-refresh) |
| 2497 | (mpc-proc-error (mpc-status-refresh))))))))))) |
| 2498 | (setq mpc--faster-toggle-forward (> step 0)) |
| 2499 | (funcall fun) ;Initialize values. |
| 2500 | (setq mpc--faster-toggle-timer |
| 2501 | (run-with-timer t 0.3 fun)))))) |
| 2502 | |
| 2503 | |
| 2504 | |
| 2505 | (defvar mpc-faster-speedup 8) |
| 2506 | |
| 2507 | (defun mpc-ffwd (_event) |
| 2508 | "Fast forward." |
| 2509 | (interactive (list last-nonmenu-event)) |
| 2510 | ;; (mpc--faster event 4.0 1) |
| 2511 | (mpc--faster-toggle mpc-faster-speedup 1)) |
| 2512 | |
| 2513 | (defun mpc-rewind (_event) |
| 2514 | "Fast rewind." |
| 2515 | (interactive (list last-nonmenu-event)) |
| 2516 | ;; (mpc--faster event 4.0 -1) |
| 2517 | (mpc--faster-toggle mpc-faster-speedup -1)) |
| 2518 | |
| 2519 | |
| 2520 | (defun mpc-play-at-point (&optional event) |
| 2521 | (interactive (list last-nonmenu-event)) |
| 2522 | (mpc-select event) |
| 2523 | (mpc-play)) |
| 2524 | |
| 2525 | ;; (defun mpc-play-tagval () |
| 2526 | ;; "Play all the songs of the tag at point." |
| 2527 | ;; (interactive) |
| 2528 | ;; (let* ((val (buffer-substring (line-beginning-position) (line-end-position))) |
| 2529 | ;; (songs (mapcar 'cdar |
| 2530 | ;; (mpc-proc-buf-to-alists |
| 2531 | ;; (mpc-proc-cmd (list "find" mpc-tag val)))))) |
| 2532 | ;; (mpc-cmd-add songs) |
| 2533 | ;; (if (member (cdr (assq 'state (mpc-cmd-status))) '("stop")) |
| 2534 | ;; (mpc-cmd-play)))) |
| 2535 | |
| 2536 | ;;; Drag'n'drop support ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2537 | ;; Todo: |
| 2538 | ;; the main thing to do here, is to provide visual feedback during the drag: |
| 2539 | ;; - change the mouse-cursor. |
| 2540 | ;; - highlight/select the source and the current destination. |
| 2541 | |
| 2542 | (defun mpc-drag-n-drop (event) |
| 2543 | "DWIM for a drag EVENT." |
| 2544 | (interactive "e") |
| 2545 | (let* ((start (event-start event)) |
| 2546 | (end (event-end event)) |
| 2547 | (start-buf (window-buffer (posn-window start))) |
| 2548 | (end-buf (window-buffer (posn-window end))) |
| 2549 | (songs |
| 2550 | (with-current-buffer start-buf |
| 2551 | (goto-char (posn-point start)) |
| 2552 | (if (get-text-property (point) 'mpc-select) |
| 2553 | ;; FIXME: actually we should only consider the constraints |
| 2554 | ;; corresponding to the selection in this particular buffer. |
| 2555 | (mpc-songs-selection) |
| 2556 | (cond |
| 2557 | ((and (derived-mode-p 'mpc-songs-mode) |
| 2558 | (get-text-property (point) 'mpc-file)) |
| 2559 | (list (cons (get-text-property (point) 'mpc-file) |
| 2560 | (get-text-property (point) 'mpc-file-pos)))) |
| 2561 | ((and mpc-tag (not (mpc-tagbrowser-all-p))) |
| 2562 | (mapcar (lambda (song) |
| 2563 | (list (cdr (assq 'file song)))) |
| 2564 | (mpc-cmd-find |
| 2565 | mpc-tag |
| 2566 | (buffer-substring (line-beginning-position) |
| 2567 | (line-end-position))))) |
| 2568 | (t |
| 2569 | (error "Unsupported starting position for drag'n'drop gesture"))))))) |
| 2570 | (with-current-buffer end-buf |
| 2571 | (goto-char (posn-point end)) |
| 2572 | (cond |
| 2573 | ((eq mpc-tag 'Playlist) |
| 2574 | ;; Adding elements to a named playlist. |
| 2575 | (let ((playlist (if (or (mpc-tagbrowser-all-p) |
| 2576 | (and (bolp) (eolp))) |
| 2577 | (error "Not a playlist") |
| 2578 | (buffer-substring (line-beginning-position) |
| 2579 | (line-end-position))))) |
| 2580 | (mpc-cmd-add (mapcar 'car songs) playlist) |
| 2581 | (message "Added %d songs to %s" (length songs) playlist) |
| 2582 | (if (member playlist |
| 2583 | (cdr (assq 'Playlist (mpc-constraints-get-current)))) |
| 2584 | (mpc-songs-refresh)))) |
| 2585 | ((derived-mode-p 'mpc-songs-mode) |
| 2586 | (cond |
| 2587 | ((null mpc-songs-playlist) |
| 2588 | (error "The songs shown do not belong to a playlist")) |
| 2589 | ((eq start-buf end-buf) |
| 2590 | ;; Moving songs within the shown playlist. |
| 2591 | (let ((dest-pos (get-text-property (point) 'mpc-file-pos))) |
| 2592 | (mpc-cmd-move (mapcar 'cdr songs) dest-pos mpc-songs-playlist) |
| 2593 | (message "Moved %d songs" (length songs)))) |
| 2594 | (t |
| 2595 | ;; Adding songs to the shown playlist. |
| 2596 | (let ((dest-pos (get-text-property (point) 'mpc-file-pos)) |
| 2597 | (pl (if (stringp mpc-songs-playlist) |
| 2598 | (mpc-cmd-find 'Playlist mpc-songs-playlist) |
| 2599 | (mpc-proc-cmd-to-alist "playlist")))) |
| 2600 | ;; MPD's protocol does not let us add songs at a particular |
| 2601 | ;; position in a playlist, so we first have to add them to the |
| 2602 | ;; end, and then move them to their final destination. |
| 2603 | (mpc-cmd-add (mapcar 'car songs) mpc-songs-playlist) |
| 2604 | (mpc-cmd-move (let ((poss '())) |
| 2605 | (dotimes (i (length songs)) |
| 2606 | (push (+ i (length pl)) poss)) |
| 2607 | (nreverse poss)) dest-pos mpc-songs-playlist) |
| 2608 | (message "Added %d songs" (length songs))))) |
| 2609 | (mpc-songs-refresh)) |
| 2610 | (t |
| 2611 | (error "Unsupported drag'n'drop gesture")))))) |
| 2612 | |
| 2613 | ;;; Toplevel ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2614 | |
| 2615 | (defcustom mpc-frame-alist '((name . "MPC") (tool-bar-lines . 1) |
| 2616 | (font . "Sans")) |
| 2617 | "Alist of frame parameters for the MPC frame." |
| 2618 | :type 'alist) |
| 2619 | |
| 2620 | ;;;###autoload |
| 2621 | (defun mpc () |
| 2622 | "Main entry point for MPC." |
| 2623 | (interactive |
| 2624 | (progn |
| 2625 | (if current-prefix-arg |
| 2626 | (setq mpc-host (read-string "MPD host and port: " nil nil mpc-host))) |
| 2627 | nil)) |
| 2628 | (let* ((song-buf (mpc-songs-buf)) |
| 2629 | (song-win (get-buffer-window song-buf 0))) |
| 2630 | (if song-win |
| 2631 | (select-window song-win) |
| 2632 | (if (or (window-dedicated-p) (window-minibuffer-p)) |
| 2633 | (ignore-errors (select-frame (make-frame mpc-frame-alist))) |
| 2634 | (with-current-buffer song-buf |
| 2635 | (setq-local mpc-previous-window-config |
| 2636 | (current-window-configuration)))) |
| 2637 | (let* ((win1 (selected-window)) |
| 2638 | (win2 (split-window)) |
| 2639 | (tags mpc-browser-tags)) |
| 2640 | (unless tags (error "Need at least one entry in `mpc-browser-tags'")) |
| 2641 | (set-window-buffer win2 song-buf) |
| 2642 | (set-window-dedicated-p win2 'soft) |
| 2643 | (mpc-status-buffer-show) |
| 2644 | (while |
| 2645 | (progn |
| 2646 | (set-window-buffer win1 (mpc-tagbrowser-buf (pop tags))) |
| 2647 | (set-window-dedicated-p win1 'soft) |
| 2648 | tags) |
| 2649 | (setq win1 (split-window win1 nil 'horiz))))) |
| 2650 | (balance-windows-area)) |
| 2651 | (mpc-songs-refresh) |
| 2652 | (mpc-status-refresh)) |
| 2653 | |
| 2654 | (provide 'mpc) |
| 2655 | |
| 2656 | ;;; mpc.el ends here |