使用智能支架计数搜索(Elisp)

时间:2009-06-29 09:04:34

标签: search emacs elisp

我有以下函数删除围绕当前光标位置的LaTeX命令:

(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$}
当然,我可以计算开始和结束括号。但是,由于这个问题经常发生,我想知道是否存在一些更智能的搜索功能,还是我错过了一个完全不同的观点?

1 个答案:

答案 0 :(得分:2)

使用基于list或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."))))