| 1 | ;;; magit-dired.el --- Dired support for Magit -*- lexical-binding:t -*- |
| 2 | |
| 3 | ;; Copyright (C) 2008-2026 The Magit Project Contributors |
| 4 | |
| 5 | ;; Author: Jonas Bernoulli <emacs.magit@jonas.bernoulli.dev> |
| 6 | ;; Maintainer: Jonas Bernoulli <emacs.magit@jonas.bernoulli.dev> |
| 7 | |
| 8 | ;; SPDX-License-Identifier: GPL-3.0-or-later |
| 9 | |
| 10 | ;; Magit is free software: you can redistribute it and/or modify it |
| 11 | ;; under the terms of the GNU General Public License as published by |
| 12 | ;; the Free Software Foundation, either version 3 of the License, or |
| 13 | ;; (at your option) any later version. |
| 14 | ;; |
| 15 | ;; Magit is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 17 | ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 18 | ;; License for more details. |
| 19 | ;; |
| 20 | ;; You should have received a copy of the GNU General Public License |
| 21 | ;; along with Magit. If not, see <https://www.gnu.org/licenses/>. |
| 22 | |
| 23 | ;;; Commentary: |
| 24 | |
| 25 | ;; Dired support for Magit. |
| 26 | |
| 27 | ;;; Code: |
| 28 | |
| 29 | (require 'magit) |
| 30 | |
| 31 | (require 'dired) |
| 32 | |
| 33 | ;; For `magit-do-async-shell-command'. |
| 34 | (declare-function dired-read-shell-command "dired-aux" (prompt arg files)) |
| 35 | |
| 36 | ;;; Open Dired from Magit |
| 37 | |
| 38 | ;;;###autoload |
| 39 | (defun magit-dired-jump (&optional other-window) |
| 40 | "Visit file at point using Dired. |
| 41 | With a prefix argument, visit in another window. If there |
| 42 | is no file at point, then instead visit `default-directory'." |
| 43 | (interactive "P") |
| 44 | (dired-jump other-window |
| 45 | (and-let ((file (if (derived-mode-p 'magit-repolist-mode) |
| 46 | (tabulated-list-get-id) |
| 47 | (magit-file-at-point)))) |
| 48 | (expand-file-name (if (file-directory-p file) |
| 49 | (file-name-as-directory file) |
| 50 | file))))) |
| 51 | |
| 52 | ;;; Commands for Dired Buffers |
| 53 | |
| 54 | ;;;###autoload |
| 55 | (defun magit-dired-stage () |
| 56 | "In Dired, staged all marked files or the file at point." |
| 57 | (interactive) |
| 58 | (magit-stage-files (dired-get-marked-files))) |
| 59 | |
| 60 | ;;;###autoload |
| 61 | (defun magit-dired-unstage () |
| 62 | "In Dired, unstaged all marked files or the file at point." |
| 63 | (interactive) |
| 64 | (magit-unstage-files (dired-get-marked-files))) |
| 65 | |
| 66 | ;;;###autoload |
| 67 | (defun magit-dired-log (&optional follow) |
| 68 | "In Dired, show log for all marked files or the directory if none are marked." |
| 69 | (interactive "P") |
| 70 | (if-let ((topdir (magit-toplevel default-directory))) |
| 71 | (let ((args (car (magit-log-arguments))) |
| 72 | (files (or (dired-get-marked-files nil 'marked) |
| 73 | (list default-directory)))) |
| 74 | (when (and follow |
| 75 | (not (member "--follow" args)) |
| 76 | (not (cdr files))) |
| 77 | (push "--follow" args)) |
| 78 | (magit-log-setup-buffer |
| 79 | (list (or (magit-get-current-branch) "HEAD")) |
| 80 | args |
| 81 | (let ((default-directory topdir)) |
| 82 | (mapcar #'file-relative-name files)) |
| 83 | magit-log-buffer-file-locked)) |
| 84 | (magit--not-inside-repository-error))) |
| 85 | |
| 86 | ;;;###autoload |
| 87 | (defun magit-dired-am-apply-patches (repo &optional arg) |
| 88 | "In Dired, apply the marked (or next ARG) files as patches. |
| 89 | If inside a repository, then apply in that. Otherwise prompt |
| 90 | for a repository." |
| 91 | (interactive (list (or (magit-toplevel) |
| 92 | (magit-read-repository t)) |
| 93 | current-prefix-arg)) |
| 94 | (let ((files (dired-get-marked-files nil arg nil nil t))) |
| 95 | (magit-status-setup-buffer repo) |
| 96 | (magit-am-apply-patches files))) |
| 97 | |
| 98 | ;;; Miscellaneous Commands |
| 99 | |
| 100 | ;;;###autoload |
| 101 | (defun magit-do-async-shell-command (file) |
| 102 | "Open FILE with `dired-do-async-shell-command'. |
| 103 | Interactively, open the file at point." |
| 104 | (interactive (list (or (magit-file-at-point) |
| 105 | (magit-read-file "Act on file" nil t)))) |
| 106 | (require 'dired-aux) |
| 107 | (dired-do-async-shell-command |
| 108 | (dired-read-shell-command "& on %s: " current-prefix-arg (list file)) |
| 109 | nil (list file))) |
| 110 | |
| 111 | ;;; _ |
| 112 | (provide 'magit-dired) |
| 113 | ;; Local Variables: |
| 114 | ;; read-symbol-shorthands: ( |
| 115 | ;; ("and$" . "cond-let--and$") |
| 116 | ;; ("thread$" . "cond-let--thread$") |
| 117 | ;; ("when$" . "cond-let--when$") |
| 118 | ;; ("and-let*" . "cond-let--and-let*") |
| 119 | ;; ("and-let" . "cond-let--and-let") |
| 120 | ;; ("if-let*" . "cond-let--if-let*") |
| 121 | ;; ("if-let" . "cond-let--if-let") |
| 122 | ;; ("when-let*" . "cond-let--when-let*") |
| 123 | ;; ("when-let" . "cond-let--when-let") |
| 124 | ;; ("while-let*" . "cond-let--while-let*") |
| 125 | ;; ("while-let" . "cond-let--while-let") |
| 126 | ;; ("match-string" . "match-string") |
| 127 | ;; ("match-str" . "match-string-no-properties")) |
| 128 | ;; End: |
| 129 | ;;; magit-dired.el ends here |