Commit 0832d54e5e03

Vincent Demeester <vincent@sbr.pm>
2024-12-13 14:13:08
tools/emacs: add consult-vc-modified-fields from chmouel
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent 32575e7
Changed files (2)
tools/emacs/config/config-vcs.el
@@ -190,5 +190,9 @@
   (define-fringe-bitmap 'git-gutter-fr:modified [224] nil nil '(center repeated))
   (define-fringe-bitmap 'git-gutter-fr:deleted [128 192 224 240] nil nil 'bottom))
 
+(use-package consult-vc-modified-files
+  :bind
+  ("C-x v /" . consult-vc-modified-files))
+
 (provide 'config-vcs)
 ;;; config-vcs.el ends here
tools/emacs/lisp/consult-vc-modified-files.el
@@ -0,0 +1,100 @@
+;;; consult-vc-modified-files.el --- Show git modified files in a vc project with consult  -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2023  Chmouel Boudjnah
+
+;; Author: Chmouel Boudjnah <chmouel@chmouel.com>
+;; Keywords: vc, convenience
+;; Created: 2023
+;; Version: 0.0.2
+;; Package-Requires: ((emacs "28.1") (consult "0.9"))
+;; Keywords: convenience
+;; Homepage: https://github.com/chmouel/consult-vc-modified-files
+;;
+;; This file is not part of GNU Emacs.
+;;
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Show modified and added files from a Git repository and show the files from
+;; the HEAD commits as another sources with consult open the file with
+;; find-files
+
+;;; Code:
+(require 'consult)
+(require 'project)
+(require 'vc-git)
+
+(defcustom consult-vc-modified-files-sources
+  '(consult-vc-modified-files-source-files
+    consult-vc-modified-files-source-head-files)
+  "Sources for modified and HEAD files in the current Git project.
+
+This variable defines the file sources used by `consult-vc-modified-files`.
+You can customize this list to add or remove sources as needed."
+  :type '(repeat (choice (const :tag "Modified locally" consult-vc-modified-files-source-files)
+                         (const :tag "Modified in HEAD" consult-vc-modified-files-source-head-files)))
+  :group 'consult-vc-modified)
+
+(defface consult-vc-modified-files-head-files-face
+  '((t :inherit shadow))
+  "Face for files modified in HEAD.")
+
+(defface consult-vc-modified-files-face
+  '((t :inherit default))
+  "Face for locally modified files.")
+
+(defvar consult-vc-modified-files-history nil
+  "History for `consult-vc-modified-files`.")
+
+(defvar consult-vc-modified-files-source-head-files
+  `(:name "Modified in HEAD"
+          :category vc
+          :face consult-vc-modified-files-head-files-face
+          :history consult-vc-modified-files-history
+          :items (lambda () (consult-vc-modified-files-get-files "diff-tree" "-z" "--no-commit-id" "--name-only" "-r" "HEAD"))))
+
+(defvar consult-vc-modified-files-source-files
+  `(:name "Modified locally"
+          :category vc
+          :face consult-vc-modified-files-face
+          :history consult-vc-modified-files-history
+          :items (lambda () (consult-vc-modified-files-get-files "ls-files" "-z" "-m" "-o" "--exclude-standard"))))
+
+(defun consult-vc-modified-files-get-files (&rest args)
+  "Run a Git command with ARGS and return the output as a list of files."
+  (let ((default-directory (project-root (project-current t))))
+    (if (vc-git-root default-directory)
+        (split-string
+         (apply #'vc-git--run-command-string "" args) "\0" t)
+      '())))
+
+(defun consult-vc-modified-files (&optional sources)
+  "Prompt user to select a modified file from the project and open it.
+SOURCES defaults to `consult-vc-modified-files-sources`."
+  (interactive)
+  (let ((project (project-current t)))
+    (if project
+        (let* ((default-directory (project-root project))
+               (selected (consult--multi (or sources consult-vc-modified-files-sources)
+                                         :prompt "Choose a file: "
+                                         :history 'consult-vc-modified-files-history
+                                         :sort nil)))
+          (if selected
+              (find-file (expand-file-name (car selected) default-directory))
+            (message "No files selected or no modifications found.")))
+      (error "No project found"))))
+
+(provide 'consult-vc-modified-files)
+;;; consult-vc-modified-files.el ends here