2009-06-29 17 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