当前位置:  开发笔记 > 编程语言 > 正文

帮助使用交互式Emacs Lisp函数替换文本

如何解决《帮助使用交互式EmacsLisp函数替换文本》经验,为你挑选了1个好方法。

我已经使用Emacs几个月了,我想开始使用elisp编程.具体来说,我想写自己的interactive功能.但是,我有点失落.(interactive ...)有很多选择,我不确定我想要哪一个.然后,我真的不知道我需要的功能的名称.如果有人能帮我把我的伪代码变成真正的代码,我会非常感激!(和往常一样,任何提供信息的地方的链接都会很好.现在我刚读过这篇文章.)

这是我想要做的伪代码:

(defun my-func (buffer) ; I think I need the buffer as an arg?
  "does some replacements"
  (interactive ???) ; ?
  (let (replacements (list
   '("a-regexp-string" . "a-replacement-string-with-backreferences")
   ...)) ; more of the above
    (while replacements
      (let (current (car replacements)) ; get a regexp-replacement pair
        (some-regexp-replace-func buffer (car current) (cdr current)) ; do the replacement
        (setq replacements (cdr replacements))))))

scottfrazer.. 5

首先,从函数的外观来看,你可能会在当前缓冲区中执行它,所以不,你不需要有一个'buffer'参数.如果这是一个不好的假设,我可以更改代码.接下来,在'let'中,如果要分配变量,则需要在每对var/value周围设置另一组parens.最后,当循环遍历列表时,我更喜欢使用类似函数式编程的函数(mapcar,mapc等).我会尝试在这里内置一些评论:

(defun my-func ()
  "Do some replacements"
  (interactive)
  (let ((replacements (list '("foo" . "bar")
                            '("baz" . "quux"))))
    (save-excursion ; So point isn't moved after this function
      (mapc (lambda (x) ; Go through the list, with this 'inline' function
                        ; being called with each element as the variable 'x'
              (goto-char (point-min)) ; Start at the beginning of the buffer
              (while (re-search-forward (car x) nil t) ; Search for the car of the replacement
                (replace-match (cdr x)))) ; And replace it with the cdr
            replacements)))) ; The list we're mapc'ing through

至于要阅读的内容,我建议使用Emacs附带的Elisp手册.



1> scottfrazer..:

首先,从函数的外观来看,你可能会在当前缓冲区中执行它,所以不,你不需要有一个'buffer'参数.如果这是一个不好的假设,我可以更改代码.接下来,在'let'中,如果要分配变量,则需要在每对var/value周围设置另一组parens.最后,当循环遍历列表时,我更喜欢使用类似函数式编程的函数(mapcar,mapc等).我会尝试在这里内置一些评论:

(defun my-func ()
  "Do some replacements"
  (interactive)
  (let ((replacements (list '("foo" . "bar")
                            '("baz" . "quux"))))
    (save-excursion ; So point isn't moved after this function
      (mapc (lambda (x) ; Go through the list, with this 'inline' function
                        ; being called with each element as the variable 'x'
              (goto-char (point-min)) ; Start at the beginning of the buffer
              (while (re-search-forward (car x) nil t) ; Search for the car of the replacement
                (replace-match (cdr x)))) ; And replace it with the cdr
            replacements)))) ; The list we're mapc'ing through

至于要阅读的内容,我建议使用Emacs附带的Elisp手册.

推荐阅读
农大军乐团_697
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有