3

I have this code to play mp3 files instead of opening them:

(defun ora-mp3 ()
  (interactive)
  (let ((file (buffer-file-name)))
    (kill-buffer (current-buffer))
    (ora-dired-start-process (format "rhythmbox \"%s\"" file))))
(add-to-list 'auto-mode-alist '("\\.mp3\\'" . ora-mp3))

Here ora-dired-start-process is just a variant of shell-command.

I kind of like using auto-mode-alist, since it's very granular: only mp3 files are affected. But the problem is that my window configuration gets messed up each time ora-mp3 is called.

How could I avoid the problem with the current approach? Or maybe there's another approach that doesn't have this problem?

abo-abo
  • 13,943
  • 1
  • 29
  • 43
  • Looks like you want to play the mp3 files from dired? If so you can advice the "dired find file" function (I am not at a computer, so don't know the exact name) with before-until modifier. If the dired-copy-filename-as-kill returns an mp3 file, run ora-dired-start-process, else do the default dired find file. – Kaushal Modi Jun 30 '15 at 10:45
  • This is a situation where using find-file is not really the right approach, use a new command with `(interactive "f")` to grab a file name and just work with that. – Jordon Biondo Jun 30 '15 at 12:44
  • @JordonBiondo, I know that it's iffy. But it works (except the window configuration thing). There are zero instances of when I would want to edit an mp3 file, but some advantages: e.g. I could browse playlists with `find-file-in-project` or something. – abo-abo Jun 30 '15 at 12:53
  • This should be closed as essentially a duplicate of http://emacs.stackexchange.com/q/3105/105. OP: in any case, see also that question for answers. – Drew Jun 30 '15 at 14:13

2 Answers2

1

I use ! open RET in dired buffer which DTRT for all files like mp3, odt &c.

sds
  • 5,928
  • 20
  • 39
  • Thanks, I already bound "r" in `dired` to do that years ago. My interest is in making it work everywhere, for example from a `locate` or a `find-file` completion session, or from a bookmark etc. – abo-abo Jun 30 '15 at 11:37
1

I used to use the openwith package which provides a global minor mode that installs a file name handler that consults a customizable list of program associations. Even if you don't like openwith, I think the idea of using file name handlers is a good one.

openwith has the advantage of working everywhere you open a file, but also has the disadvantage of working everywhere Emacs implicitly opens a file for you! In particular I remember being annoyed that grepping across a directory in dired would open my PDFs in an external viewer and play my videos! I remember whipping up something to put in my init.el to counteract that, maybe advising some dired function to deactivate openwith-mode first, something like that. At any rate it was easy to workaround.

I stopped using openwith when I started using Helm, as I find typing C-c C-x from helm-find-files convenient enough. (Also nowadays, I only unconditionally open audio and video files outside of Emacs, so most of the time I like being able to either open a file in Emacs or in an external program.)

Omar
  • 4,732
  • 1
  • 17
  • 32