搜索智能括号计数(Elisp) [英] Searching with intelligent bracket counting (Elisp)

查看:121
本文介绍了搜索智能括号计数(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));现在点在{
(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 ))
(如果'notexcmd
(消息no tex command at point)))

除了以下情况之外,它的效果很好,因为它只是匹配下一个关闭}

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

p>

  + 1 $} 

当然,我可以计算开始和结束方括号。然而,由于这个问题应该频繁出现,我想知道是否存在一些更智能的搜索功能,或者我是否缺少完全不同的一点?

解决方案>

使用基于列表或基于sexp的操作:

 (defun remove-tex-cmd()
(交互式)
(后退列表1)
(backward-sexp 1)
(kill-sexp 2))

在外部括号中处理扫描错误:

 (defun remove -tex-cmd()
(interactive)
(condition-case nil
(progn
(后退列表1)
(backward-sexp 1)
(kill-sexp 2))
(扫描错误(消息外括号))))


I have the following function that deletes 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 for the following situation, because it simply matches the next closing }:

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

results in

+1$}

I could, of course, count the opening and closing brackets. However, as this problem should occur frequently, I wonder whether there exists some more intelligent search function, or am I missing a totally different point?

解决方案

Use list- or sexp- based operations:

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

To handle scan error when outside parentheses:

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

这篇关于搜索智能括号计数(Elisp)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆