aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'misc/eselect-mode.el')
-rw-r--r--misc/eselect-mode.el53
1 files changed, 51 insertions, 2 deletions
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 <mkennedy@gentoo.org>
;; Diego Pettenò <flameeyes@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)