为什么这个elisp功能从缓冲区中抓取文本不起作用 [英] Why does this elisp function to grab text from buffer not work

查看:100
本文介绍了为什么这个elisp功能从缓冲区中抓取文本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些这样的文字:

qwerty\tberty 
merty\tserty 

我想抓住标签字符和行尾之间的文字。然后显示它的工作,直到缓冲区的结尾插入文本。

I want to grab the text berty between the tab character and end of line. Then to show it worked goto the end of the buffer and insert the text there.

但是这个代码不行。任何想法为什么不呢?

But this code doesn't work. Any ideas why not?

(defun do-test ()
  "tester"
  (interactive)
  (goto-char (point-min))
  (if (search-forward "qwerty" nil t)
     (delete-char 1) ;;delete tab
     (setq myStr (buffer-substring point end-of-line)) ;add text to variable
     ;goto end of buffer and insert text as demonstration it works
     (goto-char (point-max))
     (insert(myStr))
  )
)

编辑:

抱歉剪切和粘贴错误。应该是:

Sorry cut and paste error. Should have been:

(if(search-forwardqwertynil t)

(if (search-forward "qwerty" nil t)

推荐答案

您的代码中有一些问题:

There are a number of problems in your code:


  1. code>采用第一个参数,如果测试为正,则执行该参数;否则将执行所有剩余的参数(如大多数$ b中将会和 else 子句) $ b命令式语言)您可以将所有表达式分组到 progn 表单中,或者在表单中使用它需要任意数量的表达式来评估条件是否为真,而没有 else 子句。

  1. if takes a first argument which is executed in case the test is positive; all remaining args are executed otherwise (like what would be and else clause in most imperative languages). You can either group all your expressions in a progn form, or use the when form, which takes an arbitrary number of expressions to evaluate if the condition is true, and no else clause.

line-end-position 是函数,这意味着你必须调用它们来获取值:eg (point)

point and line-end-position are functions, which means you have to call them to get values: e.g (point).

另一方面, myStr 是一个变量,这意味着你可以直接插入它的值(你不能像函数一样调用它)

On the other hand, myStr is a variable, which means you can directly insert its value (and you cannot call it like a function)

为了保持原始缓冲区的完整性,您应该向前移动1个字符,而不是删除TAB。这可以使用 forward-char 命令完成。

If you want to keep your original buffer intact, you should move point forward by 1 character instead of deleting the TAB. This can be done using the forward-char command.

这是您的功能的修改版本,考虑到所有这一切。此外,我将选项名称更改为qwerty以匹配您的示例。

Here is a modified version of your function, taking all this into account. Also, I changed "Option Name" to "qwerty" to match your example.

(defun do-test ()
  "tester"
  (interactive)
  (goto-char (point-min))
  (when (search-forward "qwerty" nil t)
      (forward-char 1) ;;skip tab
      ;store text in variable `myStr'
      (setq myStr (buffer-substring (point) (line-end-position)))
      ;goto end of buffer and insert text as demonstration it works
      (goto-char (point-max))
      (insert myStr)))






请注意,使用正则表达式可能是实现您的目标的更好方法:


Please note that using regular expressions could be a better way of achieving your goal:

(defun do-test ()
  (interactive)
  (goto-char (point-min))
  (when (re-search-forward "qwerty\t\\(.*\\)")
    (goto-char (point-max))
    (insert (match-string 1))))

这篇关于为什么这个elisp功能从缓冲区中抓取文本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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