commit:     4ebe3143212afa53ffc2f88156cc69a4f383375b
Author:     Ulrich Müller <ulm <AT> gentoo <DOT> org>
AuthorDate: Mon Jun 12 15:48:17 2023 +0000
Commit:     Ulrich Müller <ulm <AT> gentoo <DOT> org>
CommitDate: Mon Jun 12 15:48:17 2023 +0000
URL:        https://gitweb.gentoo.org/proj/eselect.git/commit/?id=4ebe3143

eselect-mode: Update copyright years before writing a file

* misc/eselect-mode.el (eselect, eselect-mode-fix-whitespace)
(eselect-mode-update-copyright): New custom group and variables.
(eselect-mode-copyright-regexp): New variable.
(eselect-mode-update-copyright): New function, mostly copied from
ebuild-mode-update-copyright in ebuild-mode.el.
(eselect-mode-before-save): Make fixing of whitespace conditional.
Update copyright years when customised to do so.

Signed-off-by: Ulrich Müller <ulm <AT> gentoo.org>

 ChangeLog            | 10 ++++++++++
 misc/eselect-mode.el | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 87efe93..fd75fe3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2023-06-12  Ulrich Müller  <u...@gentoo.org>
+
+       * misc/eselect-mode.el (eselect, eselect-mode-fix-whitespace)
+       (eselect-mode-update-copyright): New custom group and variables.
+       (eselect-mode-copyright-regexp): New variable.
+       (eselect-mode-update-copyright): New function, mostly copied from
+       ebuild-mode-update-copyright in ebuild-mode.el.
+       (eselect-mode-before-save): Make fixing of whitespace conditional.
+       Update copyright years when customised to do so.
+
 2023-06-07  Ulrich Müller  <u...@gentoo.org>
 
        * bin/eselect.in (PATH): Use printf instead of echo.

diff --git a/misc/eselect-mode.el b/misc/eselect-mode.el
index d7ea649..d2df858 100644
--- a/misc/eselect-mode.el
+++ b/misc/eselect-mode.el
@@ -1,6 +1,6 @@
 ;;; eselect-mode.el --- edit eselect files
 
-;; Copyright 2006-2022 Gentoo Authors
+;; Copyright 2006-2023 Gentoo Authors
 
 ;; Author: Matthew Kennedy <mkenn...@gentoo.org>
 ;;     Diego Pettenò <flamee...@gentoo.org>
@@ -31,6 +31,26 @@
 (require 'sh-script)
 (require 'font-lock)
 
+;;; Variables.
+
+(defgroup eselect nil
+  "Major mode for Gentoo eselect modules."
+  :group 'languages)
+
+(defcustom eselect-mode-fix-whitespace t
+  "If non-nil, delete trailing whitespace before writing a file."
+  :type 'boolean
+  :group 'eselect)
+
+(defcustom eselect-mode-update-copyright t
+  "If non-nil, update copyright years before writing a file."
+  :type 'boolean
+  :group 'eselect)
+
+(defvar eselect-mode-copyright-regexp
+  "^#[ \t]*Copyright[ \t]+\\([1-9][0-9]+\\)\\(?:-\\([1-9][0-9]+\\)\\)?\
+[ \t]+\\(.*\\<Gentoo Authors\\>.*\\)")
+
 ;;; Font-lock.
 
 (defvar eselect-mode-keywords-warn
@@ -86,8 +106,37 @@
 
 ;;; Mode definitions.
 
+(defun eselect-mode-update-copyright ()
+  "Update the copyright notice in the file's header."
+  (save-excursion
+    (goto-char (point-min))
+    (let ((case-fold-search nil))
+      (when (re-search-forward eselect-mode-copyright-regexp 400 t)
+       (let* ((y1 (string-to-number (match-string 1)))
+              (y2 (and (match-string 2)
+                       (string-to-number (match-string 2))))
+              (year (save-match-data (format-time-string "%Y" nil t)))
+              (y (string-to-number year)))
+         (if y2
+             ;; Update range of years
+             (if (or (> 2005 y1) (>= y1 y2) (> y2 y))
+                 (lwarn 'eselect :warning
+                        "Suspicious range of copyright years: %d-%d" y1 y2)
+               (if (/= y2 y)
+                   (replace-match year t t nil 2)))
+           ;; Update single year and convert to range if necessary
+           (if (or (> 2005 y1) (> y1 y))
+               (lwarn 'eselect :warning "Suspicious copyright year: %d" y1)
+             (if (/= y1 y)
+                 (replace-match (concat "\\1-" year) t nil nil 1)))))))))
+
 (defun eselect-mode-before-save ()
-  (delete-trailing-whitespace)
+  (when eselect-mode-fix-whitespace
+    (delete-trailing-whitespace))
+  (when eselect-mode-update-copyright
+    (eselect-mode-update-copyright)
+    ;; call it only once per buffer
+    (set (make-local-variable 'eselect-mode-update-copyright) nil))
   ;; return nil, otherwise the file is presumed to be written
   nil)
 

Reply via email to