2009-06-29 35 views
1

我有以下功能删除围绕当前光标位置的乳胶命令:与智能支架计数(elisp的)搜索

(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")))) 

它运作良好,除了以下情况,因为它简单地将下一个闭合匹配}

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

结果

+1$} 

当然,我可以计算开始和结束括号。然而,由于这个问题应该经常发生,我想知道是否存在一些更智能的搜索功能,或者我错过了一个完全不同的点?

回答

2

使用列表 - 或者基于sexp-操作:

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

为了处理扫描错误时,括号外:

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

正是我一直在寻找。我怎样才能抓住'扫描错误:“不平衡的括号”,57,1',当外部不合格时会发生? – 2009-06-29 10:44:50