2

In the depth of the internet I found this function which I need foremost but you could see it as an example for any function that can be applied on a buffer.

(defun open-in-desktop ()
  "Open the containing directory of the current file in desktop. Works in Microsoft Windows and Mac OS X."
  (interactive)
  (cond
   ((string-equal system-type "windows-nt")
    (w32-shell-execute "explore" (replace-regexp-in-string "/" "\\" default-directory t t)))
   ((string-equal system-type "darwin") (shell-command "open ."))))

Now I use ibuffer and I would like to execute that function on the buffer where the cursor is currently located (using a keybinding).

Currently I mark the buffer (m),execute ibuffer-do-eval (E) and then type (open-in-desktop). This works but is quite tedious.

I have no coding skills therefore, I would appreciate any help on that.

  • 1
    That function's documentation string string says it opens the current file, but it actually opens the current directory. – Omar Oct 4 '17 at 23:08
  • Omar, I have neither written the function, nor its description. I have edited the description a bit I hope its now nearer to the truth. – Jens Lange Oct 5 '17 at 2:03
2

Following the example of ibuffer-do-eval:

(with-eval-after-load "ibuffer"
  ;; M-x ibuffer-do-open-in-desktop
  (define-ibuffer-op open-in-desktop ()
    "Call `open-in-desktop' on current or marked buffers."
    (:opstring "opened"
     :modifier-p :maybe)
    (open-in-desktop))

  (define-key ibuffer-mode-map (kbd "C-c o") 'ibuffer-do-open-in-desktop))

Edit:

Is there any way to remove the marking directly in that function without pressing u?

I don't think define-ibuffer-op facilitates that; but we could instead do something like:

(with-eval-after-load "ibuffer"
  ;; M-x ibuffer-do-open-in-desktop
  (define-ibuffer-op open-in-desktop ()
    "Call `open-in-desktop' on current or marked buffers."
    (:opstring "opened"
     :modifier-p :maybe)
    (open-in-desktop))

  (defun my-ibuffer-open-in-desktop ()
    "Call `open-in-desktop' on current or marked buffers."
    (interactive)
    (if (ibuffer-marked-buffer-names)
        (ibuffer-do-open-in-desktop)
      (with-current-buffer (ibuffer-current-buffer :must-be-live)
        (open-in-desktop))))

  (define-key ibuffer-mode-map (kbd "C-c o") 'my-ibuffer-open-in-desktop))
  • Thanks, that works. I can definitivly go from here, there is just some minor caviat. When I hit "C-c o" the buffer is left marked. Is there any way to remove the marking directly in that function without pressing u? – Jens Lange Oct 5 '17 at 1:56
  • I don't think it's a good idea to unmark the marked buffers, as we can't differentiate between a pre-marked buffer and a buffer which was marked by our command; however we can work around that by preventing an unmarked buffer from being marked on this account in the first place. I've updated the answer. – phils Oct 5 '17 at 4:13
  • Obviously I haven't thought this far. Thanks for pointing that out and writing this extensive solution. – Jens Lange Oct 5 '17 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.