[Top][All Lists]

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

Re: enum.el

From: Jean Louis
Subject: Re: enum.el
Date: Sun, 15 Jan 2023 21:52:24 +0300
User-agent: Mutt/2.2.9+54 (af2080d) (2022-11-21)

* Emanuel Berg <> [2023-01-15 15:42]:
> ;;; -*- lexical-binding: t -*-
> ;;
> ;; this file:
> ;;
> (require 'cl-lib)
> (require 'subr-x)
> (defun enum (&optional beg end suf)
>   "Enumerate each line from BEG to END, counting from one.
> Use SUF as a suffix to the digits inserted.
> BEG defaults to the beginning of the buffer,
> END defaults to the end of the buffer,
> SUF defaults to \". \""
>   (interactive
>    `(,@(if (use-region-p)
>            (list (region-beginning) (region-end))
>          (list nil nil) )
>      ,(when current-prefix-arg
>         (read-string "suffix: ") )))
>   (or beg (setq beg (point-min)))
>   (or end (setq end (point-max)))
>   (or suf (setq suf ". "))
>   (goto-char beg)
>   (let*((lines   (count-lines beg end))
>         (pad-len (length (number-to-string lines))) )
>     (cl-loop
>       for line from 1 to lines
>       do (goto-char (line-beginning-position))
>          (insert
>            (format "%s%s"
>                    (string-pad (number-to-string line) pad-len nil t) suf) )
>          (forward-line) )))

Looks sophisticated.

When there is bunch of pictures in Dired, freshly downloaded, then I
sometimes need to enumerate such files.

(defun renumber-files (&optional files)
  "This function works within Dired or Directory Editor in GNU
  Emacs. It will rename bunch of files and renumber them
  automatically by date and number of the file. It is useful when
  you are renaming less important images or bunch of files with
  irrelevant file names."
  (let* ((files (or files (dired-get-marked-files)))
         (count 1))
    (dolist (file files)
      (let* ((extension (file-name-extension file))
             (filename (format "%s-%06.f.%s" (format-time-string "%F") count 
        (rename-file file filename)
        (setq count (+ count 1))))))


Take action in Free Software Foundation campaigns:

In support of Richard M. Stallman

reply via email to

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