如何在 Emacs Lisp 中访问当前区域的内容?
我想在函数中以字符串形式访问当前区域的内容。 例如
(concat "stringa" (get-region-as-string) "stringb")
谢谢埃德
:
I want to access the contents of the current region as a string within a function. For example:
(concat "stringa" (get-region-as-string) "stringb")
Thanks
Ed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 starblue 所说,如果设置了标记,
(buffer-substring (mark) (point))
返回该区域的内容。 如果您不需要字符串属性,您可以使用'buffer-substring-no-properties
变体。但是,如果您正在编写交互式命令,则有更好的方法来获取该区域的端点,即使用
(interactive "r")
形式。 下面是来自simple.el
的示例:当从 Lisp 代码中调用时,
(interactive ...)
形式会被忽略,因此您可以使用此函数来计算行数缓冲区的任何部分,而不仅仅是区域,通过传递适当的参数:例如,(count-lines-region (point-min) (point-max))
计算缓冲区缩小部分的行数。 但是,当以交互方式调用时,将计算(interactive ...)
形式,并且"r"
代码提供点和标记,作为两个数字参数,最小的在前。请参阅 Emacs Lisp 手册的 21.2 部分。 1 使用交互式和21.2 .2 用于交互的代码字符。
As starblue says,
(buffer-substring (mark) (point))
returns the contents of the region, if the mark is set. If you do not want the string properties, you can use the'buffer-substring-no-properties
variant.However, if you're writing an interactive command, there's a better way to get the endpoints of the region, using the form
(interactive "r")
. Here's an example fromsimple.el
:When called from Lisp code, the
(interactive ...)
form is ignored, so you can use this function to count the lines in any part of the buffer, not just the region, by passing the appropriate arguments: for example,(count-lines-region (point-min) (point-max))
to count the lines in the narrowed part of the buffer. But when called interactively, the(interactive ...)
form is evaluated, and the"r"
code supplies the point and the mark, as two numeric arguments, smallest first.See the Emacs Lisp Manual, sections 21.2.1 Using Interactive and 21.2.2 Code Characters for interactive.
缓冲区子字符串
与region-beginning
和region-end
一起可以做到这一点。buffer-substring
together withregion-beginning
andregion-end
can do that.如果您想将 Lisp 代码中的区域内容复制到用户可访问的数据结构(如kill-ring、X 剪贴板或寄存器),Emacs Lisp 手册建议使用
filter-buffer-substring
而不是简单的buffer -子字符串
。 在复制之前,该函数会应用名为过滤缓冲区子字符串函数
。 该函数在版本 22.3 中添加。If you want to copy region content in a Lisp code to a user-accessible data structure like kill-ring, X clipboard or register, Emacs Lisp manual recommends to use
filter-buffer-substring
instead of simplybuffer-substring
. Before copying, the function applies filter functions from a list variable calledfilter-buffer-substring-functions
. The function was added in version 22.3.