2017-04-11 11 views
1

Emacsに関数(para2lines)を追加して、現在の段落をその文に分割し、別のバッファに1行ずつ出力することができます。Emacsの現在の段落の分割線

(define (p2l paraString) 
    (define lst (string-split paraString ". ")) 
    (for ((i lst)) 
    (displayln i))) 

テスト:

(p2l "This is a test. For checking only. Only three lines.") 

出力:

(defun pl (ss) 
    (interactive) 
    (let ((lst (split-string (ss)))) 
    (while lst 
     (print (pop lst))))) 

This is a test 
For checking only 
Only three lines. 

のEmacs Lispでは、私は次のコード管理できるラケット/スキーム内のコードで、次のしかし、私はどのようにテキストを取得するのか分からない現在の位置でグラフを作成する。どうすればこの機能を修正できますか?

編集:基本的には、別々の行として読みたいが、段落として保存したい。

+0

「段落を保存する」とはどういう意味ですか? – ashawley

+1

これは元の文書の段落とみなされます。したがって、paraを行に分割するのは読み込み専用です。 – rnso

答えて

1

私は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)))))) 
+0

はい、私はそれを別々の行として読みたいが、段落として保存したい。 – rnso

+0

コードが機能していません。あなたはそれを自分で試しましたか?新しいバッファを開きますが、行を区切りません。 @gilezのコードは動作しています(ただし、同じバッファ内にあります)。おそらく文字列 "。"をチェックし、それらを "\ n"で置き換える必要があります。 – rnso

+0

あなたの文章は1つのスペースで終わるように見えます。あなたは 'M-x customize-variable RET文末二重スペースRET nil RET'ですか? – ashawley

2

あなたの手助けをしてくれる例です。これは、新しいバッファではなく、現在の段落(カーソルが配置されている場所)への変換を行います。あなたが必要とするものなら、これを修正して関数に文字列を渡すことができます。

(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)))) 
+1

また、あらゆる種類のコーナーケースを扱う 'forward-sentence'を調べて、いくつかの変数を使って好みの振る舞いを得ることができます。 – jpkotta

+0

出力はどのように新しいバッファに入れられますか? – rnso

+0

"; \ n"と置き換えられる別の正規表現( ";?")を追加するには – rnso

関連する問題