Smart Bracket Count (Elisp) Search

I have the following function that removes the LaTeX command surrounding the current cursor position:

(defun remove-tex-cmd () (interactive) (save-excursion (let (cur-point beg-point end-point) (setq cur-point (point)) (catch 'notexcmd (if (not (re-search-backward "\\.*?{" nil t)) ; now the point is at the { (throw 'notexcmd nil)) (search-backward "\\" nil t) (setq beg-point (point)) (re-search-forward "}") (setq end-point (point)) (if (> end-point cur-point) (kill-region beg-point end-point)) (throw 'notexcmd nil))) (if 'notexcmd (message "no tex command at point")))) 

It works well, except in the following situation, because it just matches the following closure } :

 \test{a<cursor here>sdf ${bla}+1$} 

leads to

 +1$} 

I could, of course, count the opening and closing brackets. However, since this problem often arises, I wonder if there is an even more intelligent search function, or am I missing a completely different point?

+1
source share
1 answer

Use list or gender based operations:

 (defun remove-tex-cmd () (interactive) (backward-up-list 1) (backward-sexp 1) (kill-sexp 2)) 

To handle a scan error in external brackets:

 (defun remove-tex-cmd () (interactive) (condition-case nil (progn (backward-up-list 1) (backward-sexp 1) (kill-sexp 2)) (scan-error (message "Outside parentheses.")))) 
+2
source

All Articles