Thursday, March 24, 2011

Hacking Lisp in Emacs...

After having used Emacs for years, I finally started to check out Lisp. The tutorial is excellent, especially when you use the Texinfo version inside Emacs, which allows you to directly execute all the examples from the text :-)

These are my first functions:

(defun cxxcomment (pos0 pos1)
  "Alternative commenting function for c++ code"
  (interactive "r")
  (let ((line0 (line-number-at-pos pos0))
        (line1 (line-number-at-pos pos1)))
    (save-excursion
      (while (<= line0 line1)
        (goto-line line0)
        (insert "//")
        (setq line0 (+ line0 1 ))
        )
      )
    )
  )

(defun cxxuncomment (pos0 pos1)
  "Alternative uncommenting function for c++ code"
  (interactive "r")
  (let ((line0 (line-number-at-pos pos0))
        (line1 (line-number-at-pos pos1)))
    (save-excursion
      (while (<= line0 line1)
        (goto-line line0)
        (if (re-search-forward "^\\([ \t]*\\)//" (line-end-position) t)
            (replace-match "\\1"))
        (setq line0 (+ line0 1 ))
        )
      )
    )
  )

Very basic stuff, but I already like this one better than the one built into c++-mode ;-)

0 comments: