如何在 Emacs Lisp 中访问当前区域的内容?

发布于 2024-07-14 01:20:13 字数 135 浏览 6 评论 0原文

我想在函数中以字符串形式访问当前区域的内容。 例如

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天冷不及心凉 2024-07-21 01:20:13

正如 starblue 所说,如果设置了标记,(buffer-substring (mark) (point)) 返回该区域的内容。 如果您不需要字符串属性,您可以使用 'buffer-substring-no-properties 变体。

但是,如果您正在编写交互式命令,则有更好的方法来获取该区域的端点,即使用(interactive "r") 形式。 下面是来自 simple.el 的示例:

(defun count-lines-region (start end)
  "Print number of lines and characters in the region."
  (interactive "r")
  (message "Region has %d lines, %d characters"
       (count-lines start end) (- end start)))

当从 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 from simple.el:

(defun count-lines-region (start end)
  "Print number of lines and characters in the region."
  (interactive "r")
  (message "Region has %d lines, %d characters"
       (count-lines start end) (- end start)))

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.

好久不见√ 2024-07-21 01:20:13

缓冲区子字符串region-beginningregion-end 一起可以做到这一点。

buffer-substring together with region-beginning and region-end can do that.

丑疤怪 2024-07-21 01:20:13

如果您想将 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 simply buffer-substring. Before copying, the function applies filter functions from a list variable called filter-buffer-substring-functions. The function was added in version 22.3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文