如何有效地浏览 emacs 缓冲区修改行
How do I navigate efficiently through emacs buffer modifying lines
我是 elisp(但不是编程)初学者,对实现功能的最佳实践有一些疑问。我写了一个 elisp 函数,可以根据特定规则重新格式化汇编程序源代码;此功能目前适用于单行。它基本上使用行内导航、查找和替换匹配调用子表达式来实现目标。
现在我想将它应用到标记区域,逐行处理该区域。该行为类似于缩进区域函数。
推荐的(有效的)实施方法是什么?我考虑使用应用于 (region-beginning)
和 (region-end)
的 (line-number-at-pos ...)
来计算行号,然后从上到下移动,逐行处理缓冲区,修改这些。
另外,我需要通过这个操作保留什么?我虽然关于 (save-match-data ...) 但我不确定如何处理标记和点。我想它们将无用,因为文本范围已更改。
使用save-excursion
to save and restore point and mark and save-restriction
缩小到区域。
模板应该是这样的:
(defun my-process-region (beg end)
"Apply `my-process-line` to every line in region."
(interactive "r")
(save-restriction
(widen)
(save-excursion
(narrow-to-region beg end)
(goto-char (point-min))
(while (not (eobp))
(my-process-line)))))
我接受sds的回答。最后,我使用了下面的代码。原因是我希望整行都可用于重新格式化,而不仅仅是标记的区域。因此,仅 (narrow-to-region)
是无法完成这项工作的。
我很高兴了解更多信息,并感谢对 pros/cons 或遗漏内容的评论:
(defun x-mode-reformat-region (beg end)
"..."
(interactive "r")
(save-excursion
(let ((nlines (+ 1 (apply '- (mapcar 'line-number-at-pos `(,end ,beg)))))
bol
...)
(goto-char beg)
(dotimes (i nlines)
(setq bol (line-beginning-position))
(goto-char bol)
;; do reformatting for this line -- uses bol for calculations
(forward-line)))))
下次试试——根据评论修改。我没有找到一种更简单的方法来扩展选择以包括整行...不知道 setq
/ narrow-to-region
组合是否可以进一步简化(除了直接使用 (progn ...)
作为参数?
(defun x-mode-reformat-region (beg end)
"..."
(interactive "r")
(save-restriction
(widen)
(save-excursion
(setq beg (progn (goto-char beg) (line-beginning-position))
end (progn (goto-char end) (line-end-position)))
(narrow-to-region beg end)
(goto-char (point-min))
(while (not (eobp))
(insert "*") ;; placeholder for fancy reformatting
(forward-line)))))
我是 elisp(但不是编程)初学者,对实现功能的最佳实践有一些疑问。我写了一个 elisp 函数,可以根据特定规则重新格式化汇编程序源代码;此功能目前适用于单行。它基本上使用行内导航、查找和替换匹配调用子表达式来实现目标。
现在我想将它应用到标记区域,逐行处理该区域。该行为类似于缩进区域函数。
推荐的(有效的)实施方法是什么?我考虑使用应用于 (region-beginning)
和 (region-end)
的 (line-number-at-pos ...)
来计算行号,然后从上到下移动,逐行处理缓冲区,修改这些。
另外,我需要通过这个操作保留什么?我虽然关于 (save-match-data ...) 但我不确定如何处理标记和点。我想它们将无用,因为文本范围已更改。
使用save-excursion
to save and restore point and mark and save-restriction
缩小到区域。
模板应该是这样的:
(defun my-process-region (beg end)
"Apply `my-process-line` to every line in region."
(interactive "r")
(save-restriction
(widen)
(save-excursion
(narrow-to-region beg end)
(goto-char (point-min))
(while (not (eobp))
(my-process-line)))))
我接受sds的回答。最后,我使用了下面的代码。原因是我希望整行都可用于重新格式化,而不仅仅是标记的区域。因此,仅 (narrow-to-region)
是无法完成这项工作的。
我很高兴了解更多信息,并感谢对 pros/cons 或遗漏内容的评论:
(defun x-mode-reformat-region (beg end)
"..."
(interactive "r")
(save-excursion
(let ((nlines (+ 1 (apply '- (mapcar 'line-number-at-pos `(,end ,beg)))))
bol
...)
(goto-char beg)
(dotimes (i nlines)
(setq bol (line-beginning-position))
(goto-char bol)
;; do reformatting for this line -- uses bol for calculations
(forward-line)))))
下次试试——根据评论修改。我没有找到一种更简单的方法来扩展选择以包括整行...不知道 setq
/ narrow-to-region
组合是否可以进一步简化(除了直接使用 (progn ...)
作为参数?
(defun x-mode-reformat-region (beg end)
"..."
(interactive "r")
(save-restriction
(widen)
(save-excursion
(setq beg (progn (goto-char beg) (line-beginning-position))
end (progn (goto-char end) (line-end-position)))
(narrow-to-region beg end)
(goto-char (point-min))
(while (not (eobp))
(insert "*") ;; placeholder for fancy reformatting
(forward-line)))))