]> git.eshelyaron.com Git - emacs.git/commitdiff
Font lock long Git commit summary lines
authorSean Whitton <spwhitton@spwhitton.name>
Sun, 4 Sep 2022 23:20:15 +0000 (16:20 -0700)
committerSean Whitton <spwhitton@spwhitton.name>
Mon, 5 Sep 2022 18:12:23 +0000 (11:12 -0700)
* lisp/vc/vc-git.el (vc-git-log-edit-summary-target-len)
(vc-git-log-edit-summary-max-len): New defcustoms.
(vc-git-log-edit-summary-target-warning)
(vc-git-log-edit-summary-max-warning): New faces.
(vc-git--log-edit-summary-check): New function.
(vc-git-log-edit-mode): Add vc-git--log-edit-summary-check to
log-edit-font-lock-keywords to font lock long Git commit summary
lines.
* etc/NEWS (VC): Document the change.
* .dir-locals.el: Set vc-git-log-edit-summary-target-len.

.dir-locals.el
etc/NEWS
lisp/vc/vc-git.el

index 7812beb001c8d0d09ccc691916fcdc578e2f3c19..1c90ddcf567ab97e270910bc4dee91d08dac5571 100644 (file)
@@ -17,7 +17,8 @@
                (electric-quote-string . nil)
               (mode . bug-reference-prog)))
  (log-edit-mode . ((log-edit-font-lock-gnu-style . t)
-                   (log-edit-setup-add-author . t)))
+                   (log-edit-setup-add-author . t)
+                  (vc-git-log-edit-summary-target-len . 50)))
  (change-log-mode . ((add-log-time-zone-rule . t)
                     (fill-column . 74)
                     (mode . bug-reference)))
index 476cd7ba6c1cf5d186e1b9e813d2ee254110a0f2..1ee5958bcee9e4e99dd8894e997afab6c70fa9e6 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1644,6 +1644,12 @@ directory in "~/foo/bar", using 'C-x v v' on a new, unregistered file
 in the Git repository in "~/foo/bar".  This makes this command
 consistent with 'vc-responsible-backend'.
 
+---
+*** Log Edit now font locks long Git commit summary lines.
+Writing shorter summary lines avoids truncation in contexts in which
+Git commands display summary lines.  See the two new variables
+'vc-git-log-edit-summary-target-len' and 'vc-git-log-edit-summary-max-len'.
+
 ** Message
 
 ---
index 7395253745efa556ddc99bcd85a5ea49f8dc532d..573622b71e1524976c5ab840f718be24b4043d15 100644 (file)
@@ -858,6 +858,45 @@ The car of the list is the current branch."
 
 ;;; STATE-CHANGING FUNCTIONS
 
+(defcustom vc-git-log-edit-summary-target-len nil
+  "Target length for Git commit summary lines.
+If a number, characters in Summary: lines beyond this length are
+displayed in the `vc-git-log-edit-summary-target-warning' face.
+
+By setting this to an integer around 50, you can improve the
+compatibility of your commit messages with Git commands that
+print the summary line in width-constrained contexts.  However,
+many commit summaries will need to exceed this length.
+
+See also `vc-git-log-edit-summary-max-len'."
+  :type '(choice (const :tag "No target" nil)
+                 (natnum :tag "Target length"))
+  :safe (lambda (x) (or (not x) (natnump x))))
+
+(defface vc-git-log-edit-summary-target-warning
+  '((t :inherit warning))
+  "Face for Git commit summary lines beyond the target length.
+See `vc-git-log-edit-summary-target-len'.")
+
+(defcustom vc-git-log-edit-summary-max-len 68
+  "Maximum length for Git commit summary lines.
+If a number, characters in summary lines beyond this length are
+displayed in the `vc-git-log-edit-summary-max-warning' face.
+
+It is good practice to avoid writing summary lines longer than
+this because otherwise the summary line will be truncated in many
+contexts in which Git commands display summary lines.
+
+See also `vc-git-log-edit-summary-target-len'."
+  :type '(choice (const :tag "No target" nil)
+                 (natnum :tag "Target length"))
+  :safe (lambda (x) (or (not x) (natnump x))))
+
+(defface vc-git-log-edit-summary-max-warning
+  '((t :inherit error))
+  "Face for Git commit summary lines beyond the maximum length.
+See `vc-git-log-edit-summary-max-len'.")
+
 (defun vc-git-create-repo ()
   "Create a new Git repository."
   (vc-git-command nil 0 nil "init"))
@@ -911,9 +950,32 @@ If toggling on, also insert its message into the buffer."
   "C-c C-n" #'vc-git-log-edit-toggle-no-verify
   "C-c C-e" #'vc-git-log-edit-toggle-amend)
 
+(defun vc-git--log-edit-summary-check (limit)
+  (and (re-search-forward "^Summary: " limit t)
+       (when-let ((regex
+                   (cond ((and (natnump vc-git-log-edit-summary-max-len)
+                               (natnump vc-git-log-edit-summary-target-len))
+                          (format ".\\{,%d\\}\\(.\\{,%d\\}\\)\\(.*\\)"
+                                  vc-git-log-edit-summary-target-len
+                                  (- vc-git-log-edit-summary-max-len
+                                     vc-git-log-edit-summary-target-len)))
+                         ((natnump vc-git-log-edit-summary-max-len)
+                          (format ".\\{,%d\\}\\(?2:.*\\)"
+                                  vc-git-log-edit-summary-max-len))
+                         ((natnump vc-git-log-edit-summary-target-len)
+                          (format ".\\{,%d\\}\\(.*\\)"
+                                  vc-git-log-edit-summary-target-len)))))
+         (re-search-forward regex limit t))))
+
 (define-derived-mode vc-git-log-edit-mode log-edit-mode "Log-Edit/git"
   "Major mode for editing Git log messages.
-It is based on `log-edit-mode', and has Git-specific extensions.")
+It is based on `log-edit-mode', and has Git-specific extensions."
+  (setq-local
+   log-edit-font-lock-keywords
+   (append log-edit-font-lock-keywords
+           '((vc-git--log-edit-summary-check
+             (1 'vc-git-log-edit-summary-target-warning prepend t)
+              (2 'vc-git-log-edit-summary-max-warning prepend t))))))
 
 (defvar vc-git-patch-string nil)