我想在Emacs中添加一个函数(para2lines),通过它我可以将当前的段落分割成句子,并在一个单独的缓冲区中逐行打印它们。以下为球拍/计划代码:
(define (p2l paraString)
(define lst (string-split paraString ". "))
(for ((i lst))
(displayln i)))测试:
(p2l "This is a test. For checking only. Only three lines.")输出:
This is a test
For checking only
Only three lines.在Emacs中,我可以管理以下代码:
(defun pl (ss)
(interactive)
(let ((lst (split-string (ss))))
(while lst
(print (pop lst)))))但我不知道如何从目前立场的段落中得到案文。我如何纠正这个函数?
编辑:基本上,我想把它读成单独的行,但是想把它保存成段落。
发布于 2017-04-12 04:12:20
我只需运行Emacs命令或编写宏,将段落转换为单句行,但也许您只是想将包装好的段落作为行来阅读,因此需要有Emacs命令。
下面的内容将抓住当前段落,插入一个新的缓冲区*Lines*,然后将句子转换为行。
(defun para-lines ()
"Split sentences of paragraph to lines in new buffer."
(interactive)
;; Move the paragraph to a new buffer.
(let ((b (generate-new-buffer "*Lines*")))
(with-output-to-temp-buffer b
(let ((beg (save-excursion (forward-paragraph -1) (point)))
(end (save-excursion (forward-paragraph +1) (point))))
(princ (buffer-substring-no-properties beg end))))
;; Switch to new buffer
(with-current-buffer b
;; Since the name starts with "*", shut off Help Mode
(fundamental-mode)
;; Make sure buffer is writable
(setq buffer-read-only nil)
;; From the start of the buffer
(goto-char (point-min))
;; While not at the end of the buffer
(while (< (point) (point-max))
(forward-sentence 1)
;; Delete spaces between sentences before making new new line
(delete-horizontal-space)
;; Don't add a new line, if already at the end of the line
(unless (= (line-end-position) (point))
(newline))))))要避免使用forward-sentence,只需使用正则表达式,请使用re-search-forward。例如,匹配分号和句点。
(defun para-lines ()
"Split sentences of paragraph to lines in new buffer."
(interactive)
;; Move the paragraph to a new buffer.
(let ((b (generate-new-buffer "*Lines*")))
(with-output-to-temp-buffer b
(let ((beg (save-excursion (forward-paragraph -1) (point)))
(end (save-excursion (forward-paragraph +1) (point))))
(princ (buffer-substring-no-properties beg end))))
;; Switch to new buffer
(with-current-buffer b
;; Since the name starts with "*", shut off Help Mode
(fundamental-mode)
;; Make sure buffer is writable
(setq buffer-read-only nil)
;; From the start of the buffer
(goto-char (point-min))
;; While not at the end of the buffer
(while (< (point) (point-max))
(re-search-forward "[.;]\\s-+" nil t)
;; Delete spaces between sentences before making new new line
(delete-horizontal-space)
;; Don't add a new line, if already at the end of the line
(unless (= (line-end-position) (point))
(newline))))))发布于 2017-04-11 21:03:24
这里有一个例子,可以帮助你在路上。它将完成对当前段落的转换(即光标所在的位置),而不是一个新的缓冲区。如果需要的话,可以修改它以将字符串传递给函数。
(defun p2l ()
"Format current paragraph into single lines."
(interactive "*")
(save-excursion
(forward-paragraph)
(let ((foo (point)))
(backward-paragraph)
(replace-regexp "\n" " " nil (1+ (point)) foo)
(backward-paragraph)
(replace-regexp "\\. ?" ".\n" nil (point) foo))))https://stackoverflow.com/questions/43352006
复制相似问题