185 lines
6.7 KiB
EmacsLisp
185 lines
6.7 KiB
EmacsLisp
;;; qwan-dictation.el --- Format dictated text -*- lexical-binding: t; -*-
|
|
|
|
;;; Commentary:
|
|
;; Join wrapped prose and put each sentence on its own line. Sentence
|
|
;; boundaries are deliberately simple: a full stop followed by whitespace.
|
|
;; Blank lines separate paragraphs; Org structure and spoken commands are
|
|
;; not interpreted. Select prose rather than an entire structured Org file.
|
|
|
|
;;; Code:
|
|
|
|
(require 'ert)
|
|
|
|
(defun qwan/one-line-per-sentence (begin end)
|
|
"Format dictated prose in the region between BEGIN and END.
|
|
Join single newlines within paragraphs, then split after full stops
|
|
followed by spaces or tabs. Preserve full stops inside filenames,
|
|
blank lines between paragraphs, and any final newline. Remove leading
|
|
and trailing spaces and tabs on each line.
|
|
|
|
Interactively, require an active region. This command formats prose;
|
|
it does not recognize Org headings, source blocks, or spoken commands."
|
|
(interactive
|
|
(if (use-region-p)
|
|
(list (region-beginning) (region-end))
|
|
(user-error "Select the dictated text to format")))
|
|
(let* ((input (buffer-substring-no-properties begin end))
|
|
(trimmed (replace-regexp-in-string "^[ \t]+\\|[ \t]+$" "" input))
|
|
(output
|
|
(replace-regexp-in-string
|
|
"[^\n]+\\(?:\n[^\n]+\\)*"
|
|
(lambda (paragraph)
|
|
(replace-regexp-in-string
|
|
"\\.[ \t]+" ".\n"
|
|
(replace-regexp-in-string "\n" " " paragraph t t)
|
|
t t))
|
|
trimmed t t)))
|
|
(unless (equal input output)
|
|
(save-excursion
|
|
(atomic-change-group
|
|
(delete-region begin end)
|
|
(goto-char begin)
|
|
(insert output))))))
|
|
|
|
;;; Acceptance tests:
|
|
|
|
(defun qwan/one-line-per-sentence-test--apply (input)
|
|
"Run `qwan/one-line-per-sentence' interactively on all of INPUT."
|
|
(require 'org)
|
|
(with-temp-buffer
|
|
(org-mode)
|
|
(insert input)
|
|
(let ((transient-mark-mode t))
|
|
(goto-char (point-max))
|
|
(push-mark (point-min) t t)
|
|
(call-interactively #'qwan/one-line-per-sentence))
|
|
(buffer-string)))
|
|
|
|
(ert-deftest qwan/one-line-per-sentence-joins-and-splits ()
|
|
"Given wrapped, indented prose, join wraps and split sentences."
|
|
;; Given wrapped sentences, indentation, and multiple sentences per line.
|
|
;; When formatting the selected text, then produce unindented sentence lines.
|
|
(should
|
|
(equal
|
|
(qwan/one-line-per-sentence-test--apply
|
|
(concat
|
|
" First sentence wraps\n"
|
|
"\tonto another line. Second sentence\n"
|
|
" also wraps. third sentence.\n"))
|
|
(concat
|
|
"First sentence wraps onto another line.\n"
|
|
"Second sentence also wraps.\n"
|
|
"third sentence.\n"))))
|
|
|
|
(ert-deftest qwan/one-line-per-sentence-preserves-filenames ()
|
|
"Given filenames, preserve their dots while splitting sentences."
|
|
;; Given full stops inside filenames and between sentences.
|
|
;; When formatting, then split only at sentence-separating whitespace.
|
|
(should
|
|
(equal
|
|
(qwan/one-line-per-sentence-test--apply
|
|
"Sasha Chua in her .emex has a example. Read notes.org next.\n")
|
|
(concat
|
|
"Sasha Chua in her .emex has a example.\n"
|
|
"Read notes.org next.\n"))))
|
|
|
|
(ert-deftest qwan/one-line-per-sentence-preserves-paragraph-breaks ()
|
|
"Given wrapped paragraphs, preserve the blank line between them."
|
|
;; Given two paragraphs containing wrapped sentences.
|
|
;; When formatting, then join within paragraphs, never across the blank line.
|
|
(should
|
|
(equal
|
|
(qwan/one-line-per-sentence-test--apply
|
|
(concat
|
|
"First sentence wraps\n"
|
|
"onto another line. Second sentence.\n"
|
|
"\n"
|
|
"Another paragraph also\n"
|
|
"wraps. Its second sentence.\n"))
|
|
(concat
|
|
"First sentence wraps onto another line.\n"
|
|
"Second sentence.\n"
|
|
"\n"
|
|
"Another paragraph also wraps.\n"
|
|
"Its second sentence.\n"))))
|
|
|
|
(ert-deftest qwan/one-line-per-sentence-preserves-multiple-blank-lines ()
|
|
"Given whitespace-only blank lines, preserve their number."
|
|
;; Given two blank lines, one containing indentation.
|
|
;; When formatting, then retain both blank lines without their whitespace.
|
|
(should
|
|
(equal
|
|
(qwan/one-line-per-sentence-test--apply
|
|
" First paragraph. \n \t\n\n Second\nparagraph.\n")
|
|
"First paragraph.\n\n\nSecond paragraph.\n")))
|
|
|
|
(ert-deftest qwan/one-line-per-sentence-only-changes-region ()
|
|
"Given surrounding text, change only the selected block."
|
|
(require 'org)
|
|
(with-temp-buffer
|
|
(org-mode)
|
|
;; Given a selected block with unselected text on both sides.
|
|
(insert "Before. Leave this alone.\n\n")
|
|
(let ((start (point))
|
|
(transient-mark-mode t))
|
|
(insert " First sentence\ncontinues. Second sentence.\n")
|
|
(let ((end (point)))
|
|
(insert "\nAfter. Leave this alone.\n")
|
|
;; When formatting just the middle block.
|
|
(goto-char end)
|
|
(push-mark start t t)
|
|
(call-interactively #'qwan/one-line-per-sentence)))
|
|
;; Then preserve the surrounding text exactly.
|
|
(should
|
|
(equal
|
|
(buffer-string)
|
|
(concat
|
|
"Before. Leave this alone.\n\n"
|
|
"First sentence continues.\n"
|
|
"Second sentence.\n"
|
|
"\nAfter. Leave this alone.\n")))))
|
|
|
|
(ert-deftest qwan/one-line-per-sentence-is-idempotent ()
|
|
"Given formatted paragraphs, formatting again changes nothing."
|
|
(let ((formatted
|
|
(concat
|
|
"First sentence.\n"
|
|
"Second sentence.\n"
|
|
"\n"
|
|
"third sentence.\n")))
|
|
;; Given already formatted text, when formatting, then leave it unchanged.
|
|
(should
|
|
(equal
|
|
(qwan/one-line-per-sentence-test--apply formatted)
|
|
formatted))))
|
|
|
|
(ert-deftest qwan/one-line-per-sentence-splits-before-tabs ()
|
|
"Given a tab after a full stop, treat it as separating whitespace."
|
|
;; Given a tab-separated sentence, when formatting, then replace the tab.
|
|
(should
|
|
(equal
|
|
(qwan/one-line-per-sentence-test--apply "First.\tSecond.\n")
|
|
"First.\nSecond.\n")))
|
|
|
|
(ert-deftest qwan/one-line-per-sentence-preserves-missing-final-newline ()
|
|
"Given text without a final newline, do not add one."
|
|
;; Given no final newline, when formatting, then preserve that boundary.
|
|
(should
|
|
(equal
|
|
(qwan/one-line-per-sentence-test--apply "First. Second.")
|
|
"First.\nSecond.")))
|
|
|
|
(ert-deftest qwan/one-line-per-sentence-requires-active-region ()
|
|
"Given no active region, refuse to modify the buffer."
|
|
(with-temp-buffer
|
|
(insert "First. Second.\n")
|
|
(let ((transient-mark-mode t)
|
|
(mark-active nil))
|
|
;; Given no selection, when invoked, then signal a helpful user error.
|
|
(should-error (call-interactively #'qwan/one-line-per-sentence)
|
|
:type 'user-error))
|
|
(should (equal (buffer-string) "First. Second.\n"))))
|
|
|
|
(provide 'qwan-dictation)
|
|
;;; qwan-dictation.el ends here
|