Wednesday, July 07, 2004

defmacro insert-pair

Would a function with a optional parameter be better?

(defmacro insert-pair (name char1 char2)
  "Creates function name which is Like insert-parenthesis, but inserts char char2
   instead of parenthesis "
  `(defun ,name (arg)
     (interactive "P")
     (if arg (setq arg (prefix-numeric-value arg))
       (setq arg 0))
     (cond ((> arg 0) (skip-chars-forward " \t"))
           ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
     (insert ,char1)
     (save-excursion
       (or (eq arg 0) (forward-word arg))
       (insert ,char2))))

(insert-pair insert-brackets "[" "]")
(global-set-key "\M-[" 'insert-brackets)

No comments: