From 4ebe3143212afa53ffc2f88156cc69a4f383375b Mon Sep 17 00:00:00 2001 From: Ulrich Müller Date: Mon, 12 Jun 2023 17:48:17 +0200 Subject: eselect-mode: Update copyright years before writing a file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- 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 + + * 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 * 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 ;; Diego Pettenò @@ -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]+\\(.*\\.*\\)") + ;;; 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) -- cgit v1.2.3-65-gdbad