I don't have the requisite libraries installed, or know how the TeX functions work, but something like this might do the trick.
(eval-after-load "latex"
'(progn
(defun my-insert-latex-command-macro (beginning end)
(interactive "r")
(goto-char beginning)
(when (not (looking-at "(\\(.+?\\))"))
(error "Region does not match expected pattern."))
(push-mark)
(replace-match "eq:\\1")
(exchange-point-and-mark)
(TeX-insert-macro "command")
(goto-char (mark))
(pop-mark))
(define-key LaTeX-mode-map (kbd "C-c (") 'my-insert-latex-command-macro)))
Using the "r"
argument for interactive conveniently gives you the region boundaries in ascending order, regardless of which direction you marked it in.
To avoid greedy matching in a regexp, use the non-greedy operators (*?
, +?
, ??
).
Note that you do not have sufficient backslash escapes in your regexps. In order to get a backslash into a regexp string in the first place, you first need to account for string escaping. Basically, double the backslashes. e.g.: This:
(query-replace-regexp "\(([0-9]+)\)" "\1")
needs to be this:
(query-replace-regexp "\\(([0-9]+)\\)" "\\1")