下面的函數應該做你想要什麼:
(defun apply-function-to-region-lines (fn)
(interactive "aFunction to apply to lines in region: ")
(save-excursion
(goto-char (region-end))
(let ((end-marker (copy-marker (point-marker)))
next-line-marker)
(goto-char (region-beginning))
(if (not (bolp))
(forward-line 1))
(setq next-line-marker (point-marker))
(while (< next-line-marker end-marker)
(let ((start nil)
(end nil))
(goto-char next-line-marker)
(save-excursion
(setq start (point))
(forward-line 1)
(set-marker next-line-marker (point))
(setq end (point)))
(save-excursion
(let ((mark-active nil))
(narrow-to-region start end)
(funcall fn)
(widen)))))
(set-marker end-marker nil)
(set-marker next-line-marker nil))))
所以,如果你有,你想申請對線在緩衝區以下功能:
(defun test()
(insert "> "))
而且,如果你的緩衝區包含以下內容:
Line 1: blah, blah
Line 2: blah, blah
Line 3: blah, blah
Line 4: blah, blah
如果您選擇的區域只包含行2 & 3,進入「M-X應用功能對區域的線」,並在提示時輸入「test」作爲函數名,你將得到下面的結果在您的緩衝區:
Line 1: blah, blah
> Line 2: blah, blah
> Line 3: blah, blah
Line 4: blah, blah
來源
2011-06-30 19:15:51
zev
感謝sanityinnc! – dola