mpc.el 109 KB

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