All pastes #155991 Raw Edit

murf

public text v1 · immutable
#155991 ·published 2006-08-31 14:05 UTC
rendered paste body
Emacs users: Ever create a switch with consecutive numbers? Set up a table or list with consecutive
numbers? Here's a handy pair of macros. I don't use the char-repseq as much as I use the num-repseq 
routine-- but every time I use either of them, I am very thankful they are around! I usually start
by setting up prototype lines, where something like 'ZZZZ' is where I want the numbers:
  case ZZZZ:
  case ZZZZ:
  case ZZZZ:
  case ZZZZ:
  case ZZZZ:

now, put a region around this text, and go to the top of it. alt-x num-repseq and it will prompt
you for the start number, the increment, and the number in a set. I use %d most often as the format,
and if you hit return, it will be the default format. Otherwise, you could use %x, %o, or %05d for that matter.
if I supply start num: 1005, increment 5, in groups of 1, format %d, and I'll get:
  case 1005:
  case 1010:
  case 1015:
  case 1020:
  case 1025:

Paste these in your .emacs file:

(defun char-repseq (repl start inc grp) "looks for repl, replaces with char
start, then start+inc, in groups of grp
etc. until repl not found"
  (interactive "sReplace? \ncStart Char? \nnIncrement? \nnIn Groups of: ")
  (let ((count 0)(ok-flag t))
    (while ok-flag
      (while (and (< count grp) (setq ok-flag (re-search-forward repl nil t)))
        (replace-match (char-to-string start) t t)
        (setq count (1+ count)))
      (setq count 0)
      (setq start (+ start inc)))
    ))

(defun num-repseq (repl start inc grp format) "looks for repl, replaces with char
start, then start+inc, in groups of grp
etc. until repl not found"
  (interactive "sReplace? \nnStart Num? \nnIncrement? \nnIn Groups of: \nsFormat: ")
  (if (not format)
      (setq format "%d"))
  (let ((count 0)(ok-flag t))
    (while ok-flag
      (while (and (< count grp) (setq ok-flag (re-search-forward repl nil t)))
        (replace-match (format format start) t t)
        (setq count (1+ count)))
      (setq count 0)
      (setq start (+ start inc)))
    ))