* emacs and xdg-open @ 2012-01-09 10:13 Eric Abrahamsen 2012-01-09 22:26 ` Philipp Haselwarter 0 siblings, 1 reply; 5+ messages in thread From: Eric Abrahamsen @ 2012-01-09 10:13 UTC (permalink / raw) To: help-gnu-emacs I'm running archlinux, stumpwm and emacs, and have had pretty resounding success using dired as my only file manager: in dired I've bound 'e' to a command that passes files to xdg-open, and by dint of copying /usr/share/applications/mimeinfo.cache into ~/.local/share/applications/default.list, the right external applications open files out of dired. The one iffy thing is getting other applications to pass directory references to emacs (ie clicking the dropbox applet icon in the system tray and having the dropbox directory open in dired). Pointing the inode/directory MIME type at emacs works, but starts a new emacs instance. I created a "emacsclient.desktop" file: [Desktop Entry] Name=EmacsClient MimeType=inode/directory;x-directory/normal Exec=emacsclient -n %F Type=Application Terminal=false And this nearly works, except that the '-n' flag to the emacsclient exec doesn't appear to be respected: emacs still tells me "Press C-x # when you're done editing this file" (this being a dired buffer), and if I kill the buffer it asks: "Buffer `Dropbox' still has clients; kill it?" I guess this is a long shot, but does anyone have a guess as to why the '-n' flag is ignored, and how to fix it? If not, please consider this a (nearly-functional) tutorial on using emacs as your only file manager on linux systems without a proper desktop environment. :) Yrs, Eric -- GNU Emacs 24.0.92.2 (i686-pc-linux-gnu, GTK+ Version 2.24.8) of 2012-01-04 on pellet ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: emacs and xdg-open 2012-01-09 10:13 emacs and xdg-open Eric Abrahamsen @ 2012-01-09 22:26 ` Philipp Haselwarter 2012-01-10 3:07 ` Eric Abrahamsen 0 siblings, 1 reply; 5+ messages in thread From: Philipp Haselwarter @ 2012-01-09 22:26 UTC (permalink / raw) To: Eric Abrahamsen; +Cc: help-gnu-emacs Eric Abrahamsen <eric@ericabrahamsen.net> writes: > I'm running archlinux, stumpwm and emacs, and have had pretty resounding > success using dired as my only file manager: in dired I've bound 'e' to > a command that passes files to xdg-open, and by dint of copying > /usr/share/applications/mimeinfo.cache into > ~/.local/share/applications/default.list, the right external > applications open files out of dired. Thanks for sharing, fits my setup quite well! What command you use to call `xdg-open'? Some programs (eg. mplayer) exit uncleanly when their stderr and stdout are closed, which causes xdg-open to reopen the file with the default application for unknown mime types, as it appears. And allocating a buffer to every started program.... > The one iffy thing is getting other applications to pass directory > references to emacs (ie clicking the dropbox applet icon in the system > tray and having the dropbox directory open in dired). Pointing > the inode/directory MIME type at emacs works, but starts a new emacs > instance. I created a "emacsclient.desktop" file: > > [Desktop Entry] > Name=EmacsClient > MimeType=inode/directory;x-directory/normal > Exec=emacsclient -n %F > Type=Application > Terminal=false > > And this nearly works, except that the '-n' flag to the emacsclient exec > doesn't appear to be respected: emacs still tells me "Press C-x # when > you're done editing this file" (this being a dired buffer), and if I > kill the buffer it asks: "Buffer `Dropbox' still has clients; kill it?" > > I guess this is a long shot, but does anyone have a guess as to why the > '-n' flag is ignored, and how to fix it? If not, please consider this a > (nearly-functional) tutorial on using emacs as your only file manager on > linux systems without a proper desktop environment. :) > > Yrs, > Eric I wrote a quick test script, and it looks like it's not Emacs' fault, arguments are just not passed on. Actually, it the Exec line seems not to be honored; instead simply the basename of the .desktop gets executed (ie. just emacsclient). So in order to make everything work, you need to create a script with a new name (and also beware of applications that don't call emacsclient with an argument, it'll fail): emacsclient_wrapper #!/bin/sh [ "$@" ] && emacsclient -n $@ || emacsclient -n ~/ Leave out the -n in the desktop file and rename it accordingly: emacsclient_wrapper.desktop -- Philipp Haselwarter ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: emacs and xdg-open 2012-01-09 22:26 ` Philipp Haselwarter @ 2012-01-10 3:07 ` Eric Abrahamsen 2012-01-10 19:29 ` Philipp Haselwarter 0 siblings, 1 reply; 5+ messages in thread From: Eric Abrahamsen @ 2012-01-10 3:07 UTC (permalink / raw) To: help-gnu-emacs On Tue, Jan 10 2012, Philipp Haselwarter wrote: > Eric Abrahamsen <eric@ericabrahamsen.net> writes: > >> I'm running archlinux, stumpwm and emacs, and have had pretty resounding >> success using dired as my only file manager: in dired I've bound 'e' to >> a command that passes files to xdg-open, and by dint of copying >> /usr/share/applications/mimeinfo.cache into >> ~/.local/share/applications/default.list, the right external >> applications open files out of dired. > > Thanks for sharing, fits my setup quite well! There may also be mimeinfo.cache files in /usr/local/share/applications/, for packages you've installed yourself. I could probably figure out why xdg-mime doesn't just read those files directly, but life is short. > What command you use to call `xdg-open'? I modified something I found on the internet that originally used gnome-open: --8<---------------cut here---------------start------------->8--- (defun dired-xdg-open-file () "Opens the current file in a Dired buffer." (interactive) (xdg-open-file (dired-get-file-for-visit))) (defun xdg-open-file (filename) "xdg-opens the specified file." (interactive "fFile to open: ") (let ((process-connection-type nil)) (start-process "" nil "/usr/bin/xdg-open" filename))) ;;'e' usually does 'dired-find-file, same as RET, rebinding it here (add-hook 'dired-mode-hook (lambda () (define-key dired-mode-map (kbd "e") 'dired-xdg-open-file))) (setq image-dired-external-viewer "/usr/bin/xdg-open") --8<---------------cut here---------------end--------------->8--- > Some programs (eg. mplayer) exit uncleanly when their stderr and stdout > are closed, which causes xdg-open to reopen the file with the default > application for unknown mime types, as it appears. > And allocating a buffer to every started program.... > >> The one iffy thing is getting other applications to pass directory >> references to emacs (ie clicking the dropbox applet icon in the system >> tray and having the dropbox directory open in dired). Pointing >> the inode/directory MIME type at emacs works, but starts a new emacs >> instance. I created a "emacsclient.desktop" file: >> >> [Desktop Entry] >> Name=EmacsClient >> MimeType=inode/directory;x-directory/normal >> Exec=emacsclient -n %F >> Type=Application >> Terminal=false >> >> And this nearly works, except that the '-n' flag to the emacsclient exec >> doesn't appear to be respected: emacs still tells me "Press C-x # when >> you're done editing this file" (this being a dired buffer), and if I >> kill the buffer it asks: "Buffer `Dropbox' still has clients; kill it?" >> >> I guess this is a long shot, but does anyone have a guess as to why the >> '-n' flag is ignored, and how to fix it? If not, please consider this a >> (nearly-functional) tutorial on using emacs as your only file manager on >> linux systems without a proper desktop environment. :) >> >> Yrs, >> Eric > > I wrote a quick test script, and it looks like it's not Emacs' fault, > arguments are just not passed on. > > Actually, it the Exec line seems not to be honored; instead simply the > basename of the .desktop gets executed (ie. just emacsclient). > > So in order to make everything work, you need to create a script with a > new name (and also beware of applications that don't call emacsclient > with an argument, it'll fail): > > emacsclient_wrapper > #!/bin/sh > [ "$@" ] && emacsclient -n $@ || emacsclient -n ~/ > > Leave out the -n in the desktop file and rename it accordingly: > > emacsclient_wrapper.desktop This worked, by god, thank you! Just for the record, it seems the "Exec" line in the desktop file *is* honored, it just drops command-line arguments (perhaps the whole line could be wrapped in quotes or something?). Just naming the desktop file the same as my emacsclient-wrapper script wasn't enough, I had to point the Exec line at the script itself (I know because when I misspelled that line the whole thing stopped working). And a good thing too—how bizarre would it be to have an Exec line that did nothing? Dropping command line parameters seems weird enough, though… Anyway, thanks for getting me the rest of the way there! Eric -- GNU Emacs 24.0.92.2 (i686-pc-linux-gnu, GTK+ Version 2.24.8) of 2012-01-04 on pellet ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: emacs and xdg-open 2012-01-10 3:07 ` Eric Abrahamsen @ 2012-01-10 19:29 ` Philipp Haselwarter 2012-01-11 5:00 ` Eric Abrahamsen 0 siblings, 1 reply; 5+ messages in thread From: Philipp Haselwarter @ 2012-01-10 19:29 UTC (permalink / raw) To: Eric Abrahamsen; +Cc: help-gnu-emacs Eric Abrahamsen <eric@ericabrahamsen.net> writes: > On Tue, Jan 10 2012, Philipp Haselwarter wrote: > >> Eric Abrahamsen <eric@ericabrahamsen.net> writes: >> ---8<---[snipped 13 lines]---8<--- > >> What command you use to call `xdg-open'? > > I modified something I found on the internet that originally used > gnome-open: > > (defun dired-xdg-open-file () > "Opens the current file in a Dired buffer." > (interactive) > (xdg-open-file (dired-get-file-for-visit))) > > (defun xdg-open-file (filename) > "xdg-opens the specified file." > (interactive "fFile to open: ") > (let ((process-connection-type nil)) > (start-process "" nil "/usr/bin/xdg-open" filename))) > > ;;'e' usually does 'dired-find-file, same as RET, rebinding it here > (add-hook 'dired-mode-hook > (lambda () > (define-key dired-mode-map (kbd "e") 'dired-xdg-open-file))) > > (setq image-dired-external-viewer "/usr/bin/xdg-open") > > >> Some programs (eg. mplayer) exit uncleanly when their stderr and stdout >> are closed, which causes xdg-open to reopen the file with the default >> application for unknown mime types, as it appears. >> And allocating a buffer to every started program.... >> remains unsolved --8<---------------cut here---------------start------------->8--- (eval-after-load 'dired '(progn (define-key dired-mode-map "E" 'my-dired-xdg-open) (define-key dired-mode-map [tool-bar my-dired-xdg-open] `(menu-item "ext" my-dired-xdg-open :help "Open file(s) with xdg-open" :image ,(find-image '((:type xpm :file "right-arrow.xpm"))))))) (defun my-dired-xdg-open (arg) "Open files from `dired-get-marked-files' with `my-xdg-open-file'" (interactive "P") (mapcar 'my-xdg-open-file (dired-get-marked-files nil arg))) (defun dired-xdg-open-file () "Opens the current file in a Dired buffer." (interactive) (xdg-open-file (dired-get-file-for-visit))) --8<---------------cut here---------------end--------------->8--- The only annoyance left is that opening multiple files calls the program several times as xdg-open accepts only one file. I'd glad to hear ideas how to solve this! ---8<---[snipped 53 lines]---8<--- > > Anyway, thanks for getting me the rest of the way there! > > Eric Glad it works; usually I have {tool,menu,scroll}-bar-mode turned off but I kind of like them for a file manager. Here's what I ended up with: --8<---------------cut here---------------start------------->8--- #!/bin/sh [ "$1" ] || set "`pwd`" OPT='((tool-bar-lines . 1) (menu-bar-lines . 1) (vertical-scroll-bars . right))' emacsclient -n -c -F "$OPT" "$@" --8<---------------cut here---------------end--------------->8--- -- Philipp Haselwarter ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: emacs and xdg-open 2012-01-10 19:29 ` Philipp Haselwarter @ 2012-01-11 5:00 ` Eric Abrahamsen 0 siblings, 0 replies; 5+ messages in thread From: Eric Abrahamsen @ 2012-01-11 5:00 UTC (permalink / raw) To: help-gnu-emacs On Wed, Jan 11 2012, Philipp Haselwarter wrote: > Eric Abrahamsen <eric@ericabrahamsen.net> writes: > >> On Tue, Jan 10 2012, Philipp Haselwarter wrote: >> >>> Eric Abrahamsen <eric@ericabrahamsen.net> writes: >>> > ---8<---[snipped 13 lines]---8<--- >> [...] >>> Some programs (eg. mplayer) exit uncleanly when their stderr and stdout >>> are closed, which causes xdg-open to reopen the file with the default >>> application for unknown mime types, as it appears. >>> And allocating a buffer to every started program.... >>> > remains unsolved I've never really seen this problem, so I'm not sure how to address it… [...] > > The only annoyance left is that opening multiple files calls the program > several times as xdg-open accepts only one file. I'd glad to hear ideas > how to solve this! I think the only solution here is to look inside xdg-open and see what it uses to determine the proper default applications. Then, inside emacsclient-wrapper, don't use xdg-open itself, but do what it does to figure out the proper application, and then pass multiple files directly to that application. I'll admit it never even occurred to me to open multiple files this way (though it should have!), so I hadn't thought about it. Assuming you have no desktop environment, the relevant function inside xdg-open is open_generic_xdg_mime, which starts at line 408 in my installation. The following lines make it work: # get the mime type, and the .desktop file of the program that # should open it --8<---------------cut here---------------start------------->8--- filetype=`xdg-mime query filetype "$1" | sed "s/;.*//"` default=`xdg-mime query default "$filetype"` --8<---------------cut here---------------end--------------->8--- # then it finds the .desktop file, puts it in $file, and gets the actual # executable from it, this is why the command line flags are dropped! --8<---------------cut here---------------start------------->8--- command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`" command_exec=`which $command 2>/dev/null` --8<---------------cut here---------------end--------------->8--- # execute command --8<---------------cut here---------------start------------->8--- $command_exec "$1" --8<---------------cut here---------------end--------------->8--- So you could use any or all of the above bits to make a shell script which does basically what xdg-open does, except which can also handle multiple files (and can also preserve command line flags for the executable). Hope that's useful, Eric -- GNU Emacs 24.0.92.2 (i686-pc-linux-gnu, GTK+ Version 2.24.8) of 2012-01-04 on pellet ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2012-01-11 5:00 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2012-01-09 10:13 emacs and xdg-open Eric Abrahamsen 2012-01-09 22:26 ` Philipp Haselwarter 2012-01-10 3:07 ` Eric Abrahamsen 2012-01-10 19:29 ` Philipp Haselwarter 2012-01-11 5:00 ` Eric Abrahamsen
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).