Insert increasing sequence of numbers in Emacs

8월 26th, 2009 by Leave a reply »

When coding in Emacs, I needed to insert an increasing sequence of integers as follows.

  1.  pdriver_object->MajorFunction[0] = NULL;
  2.  pdriver_object->MajorFunction[1] = NULL;
  3.  pdriver_object->MajorFunction[2] = NULL;
  4.  pdriver_object->MajorFunction[3] = NULL;
  5.  pdriver_object->MajorFunction[4] = NULL;
  6.  pdriver_object->MajorFunction[5] = NULL;

It’s easy to copy and paste the lines. Inserting a sequence of integers was the problem and I wrote a small Emacs Lisp code for that.

  1. (defun f (n)
  2.  (let ((ns (number-to-string n)))
  3.   (if (<= n 30)
  4.     (progn
  5.      (insert ns)
  6.      (next-line)
  7.      (backward-char (length ns))
  8.      (f (1+ n))))))
  9. (f 30)

This was my first practical elisp code. It required more time than typing in numbers, it’s very satisfactory. :)

Advertisement

댓글 남기기