From 21c725dfe0c8fc3d4df32edb4995346df1ea9b97 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sun, 4 Sep 2022 16:20:15 -0700 Subject: Font lock long Git commit summary lines * 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 | 3 ++- etc/NEWS | 6 ++++++ lisp/vc/vc-git.el | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/.dir-locals.el b/.dir-locals.el index 7812beb001c..1c90ddcf567 100644 --- a/.dir-locals.el +++ b/.dir-locals.el @@ -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))) diff --git a/etc/NEWS b/etc/NEWS index 476cd7ba6c1..1ee5958bcee 100644 --- 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 --- diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el index 7395253745e..573622b71e1 100644 --- a/lisp/vc/vc-git.el +++ b/lisp/vc/vc-git.el @@ -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) -- cgit v1.2.3