很多時候我需要使用正則表達式捕獲某個區域中的某些段落 - 然後在每個段落上執行操作。Emacs:捕獲段落並對每個段落執行
例如,考慮恢復一個數列清單的問題:
1. Some text with a blank
line. I want not to have that line break
2. Some more text. Also - with
a line break.
3. I want to have a defun which
will capture each numbered entry
and then join it
我想寫一個defun定義,這將使以前的文字像:
1. Some text with a blank line. I want not to have that line break
2. Some more text. Also - with a line break.
3. I want to have a defun which will capture each numbered entry and then join it
下面是現在我的最好的嘗試:
(defun joining-lines (start end)
(interactive "r")
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(while (search-forward-regexp "\\([[:digit:]]\\. \\)\\(\\[^[:digit:]\\].*?\\)" nil t)
(replace-match "\\1\\,(replace-regexp-in-string " ;; here's a line break
" " " (match-string 2))" t nil))
)
)
它既不工作也不報錯。
其實最好有一個獨立的defun來處理字符串。通過這種方式,可以輕鬆地將代碼擴展爲在replace-match
上有多個替換。
關於'\,()'只能以交互方式提供,您可以在使用該語法運行交互式替換之後使用'C-x M-:'(repeat-complex-command),以獲得在函數中使用的等效elisp。 – phils
Virtuosic代碼... – Adobe