unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* enum.el
@ 2023-01-14  9:13 Emanuel Berg
  2023-01-15 18:52 ` enum.el Jean Louis
  0 siblings, 1 reply; 2+ messages in thread
From: Emanuel Berg @ 2023-01-14  9:13 UTC (permalink / raw)
  To: help-gnu-emacs

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/enum.el

(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) )))

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




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-01-15 18:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-01-14  9:13 enum.el Emanuel Berg
2023-01-15 18:52 ` enum.el Jean Louis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).