Function: canonically-space-region
canonically-space-region is an interactive and byte-compiled function
defined in fill.el.gz.
Signature
(canonically-space-region BEG END)
Documentation
Remove extra spaces between words in region.
Leave one space between words, two at end of sentences or after colons
(depending on values of sentence-end-double-space, colon-double-space,
and sentence-end-without-period).
Remove indentation from each line.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/textmodes/fill.el.gz
(defun canonically-space-region (beg end)
"Remove extra spaces between words in region.
Leave one space between words, two at end of sentences or after colons
\(depending on values of `sentence-end-double-space', `colon-double-space',
and `sentence-end-without-period').
Remove indentation from each line."
(interactive "*r")
;; Ideally, we'd want to scan the text from the end, so that changes to
;; text don't affect the boundary, but the regexp we match against does
;; not match as eagerly when matching backward, so we instead use
;; a marker.
(unless (markerp end) (setq end (copy-marker end t)))
(let ((end-spc-re (concat "\\(" (sentence-end) "\\) *\\| +")))
(save-excursion
(goto-char beg)
;; Nuke tabs; they get screwed up in a fill.
;; This is quick, but loses when a tab follows the end of a sentence.
;; Actually, it is difficult to tell that from "Mr.\tSmith".
;; Blame the typist.
(subst-char-in-region beg end ?\t ?\s)
(while (and (< (point) end)
(re-search-forward end-spc-re end t))
(delete-region
(cond
;; `sentence-end' matched and did not match all spaces.
;; I.e. it only matched the number of spaces it needs: drop the rest.
((and (match-end 1) (> (match-end 0) (match-end 1))) (match-end 1))
;; `sentence-end' matched but with nothing left. Either that means
;; nothing should be removed, or it means it's the "old-style"
;; sentence-end which matches all it can. Keep only 2 spaces.
;; We probably don't even need to check `sentence-end-double-space'.
((match-end 1)
(min (match-end 0)
(+ (if sentence-end-double-space 2 1)
(save-excursion (goto-char (match-end 0))
(skip-chars-backward " ")
(point)))))
(t ;; It's not an end of sentence.
(+ (match-beginning 0)
;; Determine number of spaces to leave:
(save-excursion
(skip-chars-backward " ]})\"'")
(cond ((and sentence-end-double-space
(or (memq (preceding-char) '(?. ?? ?!))
(and sentence-end-without-period
(= (char-syntax (preceding-char)) ?w)))) 2)
((and colon-double-space
(= (preceding-char) ?:)) 2)
((char-equal (preceding-char) ?\n) 0)
(t 1))))))
(match-end 0))))))