emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: new auto-version.el package [sneak peek]


From: Emanuel Berg
Subject: Re: new auto-version.el package [sneak peek]
Date: Sun, 04 Aug 2024 00:39:43 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

This is also close do done now, possibly.

BTW version 1.9.5 is 585 updates :)

;;; auto-version.el --- automatic x.y.z version -*- lexical-binding: t -*-
;;
;; Author: Emanuel Berg <incal@dataswamp.org>
;; Created: 2024-08-01
;; Keywords: convenience, lisp, tools
;; License: GPL3+
;; URL: https://dataswamp.org/~incal/elpa/auto-version.el
;; Version: 1.9.5
;;
;;; Commentary:
;;
;; __________________________________________________________
;; ^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`
;;
;;         auto-version.el -- automatic x.y.z version
;;
;; __________________________________________________________
;; `^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^`^
;;
;; Automatic version number uptick.
;;
;; The version number must look like this:
;;
;;                           x.y.z
;;
;; To enable in Emacs Lisp buffers:
;;
;; (keymap-set emacs-lisp-mode-map "C-x C-s" #'auto-version-save)
;;
;; See the function `auto-version'.
;;
;; More:
;;
;; `auto-version-save'  automate version update on save
;; `aversion--updates'  how many updates is a version number?
;;
;; ----------------------------------------------------------
;;
;;; Code:

(require 'cl-lib)

(defun auto-version-save (&rest arg)
  "Uptick version and save. See `auto-version'.
ARG are passed on to `save-buffer'."
  (interactive "p")
  (let ((ups (auto-version)))
    (save-buffer arg)
    (when ups
      (message "Updates: %s" (aversion--updates ups)))))

(let ((yz-max-def 20))

  (defun auto-version (&optional yz-max label)
    "Uptick the x.y.z version number if present and in a modified buffer.
\ny and z overtick at YZ-MAX, by default 20.
\nLABEL is prepended to the version search string, it defaults
to \"^;; Version: \" in Elisp buffers and otherwise to
\"\\\\W\".
\nAlso see: `auto-version-save'"
    (interactive)
    (or yz-max (setq yz-max yz-max-def))
    (or label  (setq label (if (eq major-mode 'emacs-lisp-mode)
                               "^;; Version: "
                             "\\W")))
    (when (buffer-modified-p)
      (save-mark-and-excursion
        (goto-char (point-min))
        (when (re-search-forward
                (format "%s\\([0-9]+.[0-9]+.[0-9]+\\)\\b" label)
                nil t)
          (pcase-let ((`(,x ,y ,z) (version-to-list (match-string 1))))
            (when (zerop (setq z (mod (cl-incf z) yz-max)))
              (when (zerop (setq y (mod (cl-incf y) yz-max)))
                (cl-incf x)))
            (let ((ver (format "%d.%d.%d" x y z)))
              (replace-match ver nil nil nil 1)
              ver))))))

  (defun aversion--updates (ver &optional overtick)
    "Translate VER into the number of updates it represents.
Overtick is OVERTICK for y and z. [default 20]
\nFor example, version 0.0.1 is the first update or update number 1;
meanwhile version 7.4.19 is already at update number 2899!
\n(+ (* 7 (expt 20 2)) (* 4 (expt 20 1)) (* 19 (expt 20 0)))"
    (interactive (list
                   (read-string "Version: ")
                   (read-number "Overtick: " yz-max-def)))
    (or overtick (setq overtick yz-max-def))
    (let* ((vel (version-to-list ver)))
      (cl-loop
        with ups = 0
        for i downfrom (1- (length vel))
        for d in vel do
          (cl-incf ups (* d (expt overtick i)))
        finally return ups)))

  (declare-function auto-version nil)
  (declare-function aversion--updates nil))

(provide 'auto-version)
;;; auto-version.el ends here

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]