dimanche 16 août 2015

Number line in region using custom printf-like format

By default starts at zero, but you can use C-u 50 M-x my-number-line to make it start at 50...
The commands asks for a printf format that will be inserted at the start of each line, so you can do right alignment, hex, whatever.

Example region:
Pellentesque tristique imperdiet tortor. Cras placerat accumsan
nulla. Donec hendrerit tempor tellus. Nam a sapien. Nam vestibulum
accumsan nisl. Donec at pede. Pellentesque dapibus suscipit
ligula. Nunc porta vulputate tellus. Nunc aliquet, augue nec
adipiscing interdum, lacus tellus malesuada massa, quis varius mi
purus non odio. Nunc rutrum turpis sed pede. Etiam vel tortor sodales
tellus ultricies commodo.
Mark all lines, M-x my-number-line RET 0x%03x SPC RET and BAM, lines are prefixed with hex line numbers:
0x000 Pellentesque tristique imperdiet tortor. Cras placerat accumsan
0x001 nulla. Donec hendrerit tempor tellus. Nam a sapien. Nam vestibulum
0x002 accumsan nisl. Donec at pede. Pellentesque dapibus suscipit
0x003 ligula. Nunc porta vulputate tellus. Nunc aliquet, augue nec
0x004 adipiscing interdum, lacus tellus malesuada massa, quis varius mi
0x005 purus non odio. Nunc rutrum turpis sed pede. Etiam vel tortor sodales
0x006 tellus ultricies commodo.
(defun my-number-line (arg beg end fmt)
  (interactive "P\nr\nsformat: ")
  (let ((n (if (numberp arg) arg 0)))
    (save-excursion
      (goto-char beg)
      (beginning-of-line)
      (while (< (point) end)
        (beginning-of-line)
        (let ((s (format fmt n)))
          (insert s)
          (incf end (length s)))
        (incf n)
        (forward-line)
        (beginning-of-line)))))

Aucun commentaire:

Enregistrer un commentaire