首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Emacs中当前段落的分隔行

Emacs中当前段落的分隔行
EN

Stack Overflow用户
提问于 2017-04-11 16:56:03
回答 2查看 777关注 0票数 1

我想在Emacs中添加一个函数(para2lines),通过它我可以将当前的段落分割成句子,并在一个单独的缓冲区中逐行打印它们。以下为球拍/计划代码:

代码语言:javascript
复制
(define (p2l paraString)
  (define lst (string-split paraString ". "))
  (for ((i lst))
    (displayln i)))

测试:

代码语言:javascript
复制
(p2l "This is a test. For checking only. Only three lines.")

输出:

代码语言:javascript
复制
This is a test
For checking only
Only three lines.

在Emacs中,我可以管理以下代码:

代码语言:javascript
复制
(defun pl (ss)
  (interactive)
  (let ((lst (split-string (ss))))
    (while lst
      (print (pop lst)))))

但我不知道如何从目前立场的段落中得到案文。我如何纠正这个函数?

编辑:基本上,我想把它读成单独的行,但是想把它保存成段落。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-12 04:12:20

我只需运行Emacs命令或编写宏,将段落转换为单句行,但也许您只是想将包装好的段落作为行来阅读,因此需要有Emacs命令。

下面的内容将抓住当前段落,插入一个新的缓冲区*Lines*,然后将句子转换为行。

代码语言:javascript
复制
(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。例如,匹配分号和句点。

代码语言:javascript
复制
(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))))))
票数 1
EN

Stack Overflow用户

发布于 2017-04-11 21:03:24

这里有一个例子,可以帮助你在路上。它将完成对当前段落的转换(即光标所在的位置),而不是一个新的缓冲区。如果需要的话,可以修改它以将字符串传递给函数。

代码语言:javascript
复制
(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))))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43352006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档