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.
This commit is contained in:
Sean Whitton 2022-09-04 16:20:15 -07:00
parent 3f1efe33d6
commit 21c725dfe0
3 changed files with 71 additions and 2 deletions

View 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)))

View file

@ -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
---

View 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)