Skip to content

Instantly share code, notes, and snippets.

@jbaznik
Forked from DinoChiesa/dired-additions.el
Created June 5, 2025 05:21
Show Gist options
  • Save jbaznik/f77a5b75b71deb36d9c1f7a8e712f8c2 to your computer and use it in GitHub Desktop.
Save jbaznik/f77a5b75b71deb36d9c1f7a8e712f8c2 to your computer and use it in GitHub Desktop.

Revisions

  1. @DinoChiesa DinoChiesa created this gist May 8, 2025.
    74 changes: 74 additions & 0 deletions dired-additions.el
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    (defun mode-for-buffer (&optional buffer-or-string)
    "Returns the major mode associated with a buffer."
    (with-current-buffer (or buffer-or-string (current-buffer))
    major-mode))

    (defun my-dired-copy-or-move-other-window (operation)
    "Copy or move the marked files to another directory.
    OPERATION is a symbol, either `COPY' or `MOVE' .
    This works with files or directories."
    (unless (eq major-mode 'dired-mode)
    (error "works only when current-buffer is in dired-mode"))
    (let ((other-visible-dired-buffers
    (delq nil (mapcar #'(lambda (w)
    (let* ((b (window-buffer w))
    (m (mode-for-buffer b)))
    (and (eq m 'dired-mode)
    (not (eq b (current-buffer)))
    b)))
    (window-list)))))

    (unless (= (length other-visible-dired-buffers) 1)
    (error "Can copy only if exactly 2 dired windows are visible"))

    (let ((fns (cond
    ((eq operation 'COPY)
    '(copy-file copy-directory))
    ((eq operation 'MOVE)
    '(rename-file rename-file))
    (t
    (error "unsupported operation")
    )))
    (dst-dir (expand-file-name (with-current-buffer (car other-visible-dired-buffers)
    default-directory))))
    (mapc #'(lambda (f)
    (let ((fn
    (cond
    ((file-directory-p f)
    (cadr fns))
    (t
    (car fns)))))
    (funcall fn f dst-dir 1)))
    (dired-get-marked-files nil))
    (with-current-buffer (car other-visible-dired-buffers)
    (revert-buffer))
    (revert-buffer))))


    (defun my-dired-move-file-to-dir-in-other-window (&optional arg)
    "If there are two or more windows, and the current one is in
    dired-mode, and one of the others is also dired-mode, then move
    the file under cursor or the marked files to the directory shown
    in the other dired window. If the current buffer is not in
    dired-mode, or if not exactly 2 windows show dired, then message
    and quit.
    "
    (interactive "P")
    (my-dired-copy-or-move-other-window 'MOVE))

    (defun my-dired-copy-file-to-dir-in-other-window (&optional arg)
    "If there are two or more windows, and the current one is in
    dired-mode, and one of the others is also dired-mode, then copy
    the file under cursor or the marked files to the directory shown
    in the other dired window. If the current buffer is not in
    dired-mode, or if not exactly 2 windows show dired, then message
    and quit.
    "
    (interactive "P")
    (my-dired-copy-or-move-other-window 'COPY))


    (defun my-dired-mode-hook-fn ()
    (keymap-local-set "C-c C-c" #'my-dired-copy-file-to-dir-in-other-window)
    (keymap-local-set "C-c C-m" #'my-dired-move-file-to-dir-in-other-window))