search - emacs:在区域上搜索和替换
问题描述
所以,我有这个出色的功能(有人为我制作的)用于在整个缓冲区上进行多次搜索和替换。
(defun accent-replace-whole-buffer ()
"Corrects macrons from badly scanned latin"
(interactive "*")
(dolist (ele (list ?â ?ä ?ê ?ë ?î ?ô ?ü ?ï))
(setq elt (char-to-string ele))
(goto-char (point-min))
(while (search-forward elt nil t 1)
(replace-match
(char-to-string
(pcase ele
(`?â ?ā)
(`?ä ?ā)
(`?ê ?ē)
(`?ë ?ē)
(`?î ?ī)
(`?ô ?ō)
(`?ü ?ū)
(`?ï ?ī)))))))
我想制作另一个功能,它只在选定的区域上执行此操作。
我该怎么办?哪里有不错的教程?
解决方案
使用narrow-to-region
,里面save-restriction
:
(defun accent-replace-in-region (begin end)
"Corrects macrons in active region from badly scanned latin"
(interactive "*r")
(save-restriction
(narrow-to-region begin end)
(dolist (ele (list ?ā ?ā ?ē ?ē ?ī ?ō ?ū ?ī))
(setq elt (char-to-string ele))
(goto-char (point-min))
(while (search-forward elt nil t 1)
(replace-match
(char-to-string
(pcase ele
(`?â ?ā)
(`?ä ?ā)
(`?ê ?ē)
(`?ë ?ē)
(`?î ?ī)
(`?ô ?ō)
(`?ü ?ū)
(`?ï ?ī))))))))
推荐阅读
- rxjs - RxJS toArray() 等效
- python - 如何创建具有容差的while循环
- ffmpeg - 使用 ffmpeg 从 mpeg-ts 到 dash 时 A/V 不同步
- java - 从端点获取 indexOutOfBoundException
- android - Flutter APK 内置在手机中,但打不开?
- javascript - 如果他们之间有保留,如何禁止选择日期
- python - 为什么我在 Jupyter 上的代码不产生输出
- knockout.js - KnockoutJs:我的计算函数没有动态改变我视图中的元素
- solaris - Solaris 11 上的 net-snmp 构建问题
- javascript - JavaScript Promise 解决了 Chrome / Edge / 等中的“未定义”,但在 Firefox 中运行良好