mpc.el 109 KB

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