1992-05-30 23:54:21 +00:00
|
|
|
|
;;; fill.el --- fill commands for Emacs
|
|
|
|
|
|
2002-04-10 21:21:12 +00:00
|
|
|
|
;; Copyright (C) 1985,86,92,94,95,96,97,1999,2001,2002
|
|
|
|
|
;; Free Software Foundation, Inc.
|
1990-07-27 04:20:16 +00:00
|
|
|
|
|
2001-08-06 10:12:17 +00:00
|
|
|
|
;; Maintainer: FSF
|
1992-07-22 04:22:42 +00:00
|
|
|
|
;; Keywords: wp
|
|
|
|
|
|
1990-07-27 04:20:16 +00:00
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
1992-07-16 21:47:34 +00:00
|
|
|
|
;; the Free Software Foundation; either version 2, or (at your option)
|
1990-07-27 04:20:16 +00:00
|
|
|
|
;; any later version.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
1996-01-14 07:34:30 +00:00
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
|
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
|
;; Boston, MA 02111-1307, USA.
|
1990-07-27 04:20:16 +00:00
|
|
|
|
|
1993-03-22 03:27:18 +00:00
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;; All the commands for filling text. These are documented in the Emacs
|
|
|
|
|
;; manual.
|
|
|
|
|
|
1992-07-16 21:47:34 +00:00
|
|
|
|
;;; Code:
|
1990-07-27 04:20:16 +00:00
|
|
|
|
|
1997-05-05 15:00:53 +00:00
|
|
|
|
(defcustom fill-individual-varying-indent nil
|
1992-05-10 18:15:10 +00:00
|
|
|
|
"*Controls criterion for a new paragraph in `fill-individual-paragraphs'.
|
|
|
|
|
Non-nil means changing indent doesn't end a paragraph.
|
|
|
|
|
That mode can handle paragraphs with extra indentation on the first line,
|
|
|
|
|
but it requires separator lines between paragraphs.
|
1997-05-05 15:00:53 +00:00
|
|
|
|
A value of nil means that any change in indentation starts a new paragraph."
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'fill)
|
1994-02-04 04:45:52 +00:00
|
|
|
|
|
1997-05-05 15:00:53 +00:00
|
|
|
|
(defcustom colon-double-space nil
|
|
|
|
|
"*Non-nil means put two spaces after a colon when filling."
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'fill)
|
1995-08-02 18:36:20 +00:00
|
|
|
|
|
1995-02-02 09:50:23 +00:00
|
|
|
|
(defvar fill-paragraph-function nil
|
1995-12-24 23:30:18 +00:00
|
|
|
|
"Mode-specific function to fill a paragraph, or nil if there is none.
|
|
|
|
|
If the function returns nil, then `fill-paragraph' does its normal work.")
|
1995-02-02 09:50:23 +00:00
|
|
|
|
|
2002-11-03 03:41:11 +00:00
|
|
|
|
(defvar fill-paragraph-handle-comment t
|
|
|
|
|
"If non-nil, paragraph filling will try to pay attention to comments.")
|
|
|
|
|
|
1997-05-17 02:01:42 +00:00
|
|
|
|
(defvar enable-kinsoku t
|
1997-08-09 03:41:42 +00:00
|
|
|
|
"*Non-nil means enable \"kinsoku\" processing on filling paragraph.
|
|
|
|
|
Kinsoku processing is designed to prevent certain characters from being
|
|
|
|
|
placed at the beginning or end of a line by filling.
|
|
|
|
|
See the documentation of `kinsoku' for more information.")
|
1997-02-20 05:33:51 +00:00
|
|
|
|
|
1990-07-27 04:20:16 +00:00
|
|
|
|
(defun set-fill-prefix ()
|
1994-02-04 04:45:52 +00:00
|
|
|
|
"Set the fill prefix to the current line up to point.
|
1991-03-20 04:10:45 +00:00
|
|
|
|
Filling expects lines to start with the fill prefix and
|
|
|
|
|
reinserts the fill prefix in each resulting line."
|
1990-07-27 04:20:16 +00:00
|
|
|
|
(interactive)
|
2001-01-31 16:12:41 +00:00
|
|
|
|
(let ((left-margin-pos (save-excursion (move-to-left-margin) (point))))
|
|
|
|
|
(if (> (point) left-margin-pos)
|
|
|
|
|
(progn
|
|
|
|
|
(setq fill-prefix (buffer-substring left-margin-pos (point)))
|
|
|
|
|
(if (equal fill-prefix "")
|
|
|
|
|
(setq fill-prefix nil)))
|
|
|
|
|
(setq fill-prefix nil)))
|
1990-07-27 04:20:16 +00:00
|
|
|
|
(if fill-prefix
|
|
|
|
|
(message "fill-prefix: \"%s\"" fill-prefix)
|
|
|
|
|
(message "fill-prefix cancelled")))
|
|
|
|
|
|
1997-05-05 15:00:53 +00:00
|
|
|
|
(defcustom adaptive-fill-mode t
|
|
|
|
|
"*Non-nil means determine a paragraph's fill prefix from its text."
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'fill)
|
1991-03-20 04:10:45 +00:00
|
|
|
|
|
2000-10-06 19:50:40 +00:00
|
|
|
|
(defcustom adaptive-fill-regexp
|
2002-04-10 21:21:12 +00:00
|
|
|
|
;; Added `!' for doxygen comments starting with `//!' or `/*!'.
|
|
|
|
|
;; Added `%' for TeX comments.
|
|
|
|
|
(purecopy "[ \t]*\\([-!|#%;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*")
|
1991-03-20 04:10:45 +00:00
|
|
|
|
"*Regexp to match text at start of line that constitutes indentation.
|
1997-06-11 06:51:29 +00:00
|
|
|
|
If Adaptive Fill mode is enabled, a prefix matching this pattern
|
|
|
|
|
on the first and second lines of a paragraph is used as the
|
|
|
|
|
standard indentation for the whole paragraph.
|
|
|
|
|
|
|
|
|
|
If the paragraph has just one line, the indentation is taken from that
|
|
|
|
|
line, but in that case `adaptive-fill-first-line-regexp' also plays
|
|
|
|
|
a role."
|
|
|
|
|
:type 'regexp
|
|
|
|
|
:group 'fill)
|
|
|
|
|
|
1997-08-01 19:20:29 +00:00
|
|
|
|
(defcustom adaptive-fill-first-line-regexp "\\`[ \t]*\\'"
|
1997-06-11 06:51:29 +00:00
|
|
|
|
"*Regexp specifying whether to set fill prefix from a one-line paragraph.
|
|
|
|
|
When a paragraph has just one line, then after `adaptive-fill-regexp'
|
|
|
|
|
finds the prefix at the beginning of the line, if it doesn't
|
|
|
|
|
match this regexp, it is replaced with whitespace.
|
|
|
|
|
|
|
|
|
|
By default, this regexp matches sequences of just spaces and tabs.
|
|
|
|
|
|
|
|
|
|
However, we never use a prefix from a one-line paragraph
|
|
|
|
|
if it would act as a paragraph-starter on the second line."
|
1997-05-05 15:00:53 +00:00
|
|
|
|
:type 'regexp
|
|
|
|
|
:group 'fill)
|
1991-03-20 04:10:45 +00:00
|
|
|
|
|
1997-05-05 15:00:53 +00:00
|
|
|
|
(defcustom adaptive-fill-function nil
|
2001-02-07 23:31:57 +00:00
|
|
|
|
"*Function to call to choose a fill prefix for a paragraph, or nil.
|
1997-05-05 15:00:53 +00:00
|
|
|
|
This function is used when `adaptive-fill-regexp' does not match."
|
2001-02-07 23:31:57 +00:00
|
|
|
|
:type '(choice (const nil) function)
|
1997-05-05 15:00:53 +00:00
|
|
|
|
:group 'fill)
|
1995-08-02 18:36:20 +00:00
|
|
|
|
|
2001-11-25 22:37:00 +00:00
|
|
|
|
(defvar fill-indent-according-to-mode nil ;Screws up CC-mode's filling tricks.
|
2000-11-10 11:43:55 +00:00
|
|
|
|
"Whether or not filling should try to use the major mode's indentation.")
|
|
|
|
|
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
(defun current-fill-column ()
|
|
|
|
|
"Return the fill-column to use for this line.
|
|
|
|
|
The fill-column to use for a buffer is stored in the variable `fill-column',
|
|
|
|
|
but can be locally modified by the `right-margin' text property, which is
|
|
|
|
|
subtracted from `fill-column'.
|
|
|
|
|
|
|
|
|
|
The fill column to use for a line is the first column at which the column
|
|
|
|
|
number equals or exceeds the local fill-column - right-margin difference."
|
|
|
|
|
(save-excursion
|
1995-06-29 03:21:26 +00:00
|
|
|
|
(if fill-column
|
|
|
|
|
(let* ((here (progn (beginning-of-line) (point)))
|
|
|
|
|
(here-col 0)
|
|
|
|
|
(eol (progn (end-of-line) (point)))
|
|
|
|
|
margin fill-col change col)
|
2000-10-06 19:50:40 +00:00
|
|
|
|
;; Look separately at each region of line with a different
|
|
|
|
|
;; right-margin.
|
1995-06-29 03:21:26 +00:00
|
|
|
|
(while (and (setq margin (get-text-property here 'right-margin)
|
|
|
|
|
fill-col (- fill-column (or margin 0))
|
|
|
|
|
change (text-property-not-all
|
|
|
|
|
here eol 'right-margin margin))
|
|
|
|
|
(progn (goto-char (1- change))
|
|
|
|
|
(setq col (current-column))
|
|
|
|
|
(< col fill-col)))
|
|
|
|
|
(setq here change
|
|
|
|
|
here-col col))
|
|
|
|
|
(max here-col fill-col)))))
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
|
|
|
|
|
(defun canonically-space-region (beg end)
|
|
|
|
|
"Remove extra spaces between words in region.
|
1996-02-21 21:20:28 +00:00
|
|
|
|
Leave one space between words, two at end of sentences or after colons
|
1998-10-21 11:50:56 +00:00
|
|
|
|
\(depending on values of `sentence-end-double-space', `colon-double-space',
|
|
|
|
|
and `sentence-end-without-period').
|
1995-07-17 22:50:30 +00:00
|
|
|
|
Remove indentation from each line."
|
1999-07-20 16:02:27 +00:00
|
|
|
|
(interactive "*r")
|
2002-04-11 22:00:07 +00:00
|
|
|
|
(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 ?\ )
|
1999-09-08 07:50:19 +00:00
|
|
|
|
(while (and (< (point) end)
|
2002-04-11 22:00:07 +00:00
|
|
|
|
(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))))))
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
|
2000-01-11 21:42:12 +00:00
|
|
|
|
(defun fill-common-string-prefix (s1 s2)
|
|
|
|
|
"Return the longest common prefix of strings S1 and S2, or nil if none."
|
|
|
|
|
(let ((cmp (compare-strings s1 nil nil s2 nil nil)))
|
2000-11-10 11:43:55 +00:00
|
|
|
|
(if (eq cmp t)
|
2000-01-11 21:42:12 +00:00
|
|
|
|
s1
|
|
|
|
|
(setq cmp (1- (abs cmp)))
|
|
|
|
|
(unless (zerop cmp)
|
|
|
|
|
(substring s1 0 cmp)))))
|
2003-02-04 13:30:45 +00:00
|
|
|
|
|
1995-11-18 16:42:57 +00:00
|
|
|
|
(defun fill-context-prefix (from to &optional first-line-regexp)
|
1995-11-10 17:13:56 +00:00
|
|
|
|
"Compute a fill prefix from the text between FROM and TO.
|
1999-07-30 14:31:57 +00:00
|
|
|
|
This uses the variables `adaptive-fill-regexp' and `adaptive-fill-function'
|
1997-06-11 06:51:29 +00:00
|
|
|
|
and `adaptive-fill-first-line-regexp'. `paragraph-start' also plays a role;
|
|
|
|
|
we reject a prefix based on a one-line paragraph if that prefix would
|
|
|
|
|
act as a paragraph-separator."
|
|
|
|
|
(or first-line-regexp
|
|
|
|
|
(setq first-line-regexp adaptive-fill-first-line-regexp))
|
1995-11-10 17:13:56 +00:00
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char from)
|
|
|
|
|
(if (eolp) (forward-line 1))
|
|
|
|
|
;; Move to the second line unless there is just one.
|
2001-11-03 00:55:10 +00:00
|
|
|
|
(move-to-left-margin)
|
1995-11-18 16:42:57 +00:00
|
|
|
|
(let ((firstline (point))
|
1997-06-17 21:46:40 +00:00
|
|
|
|
first-line-prefix
|
1995-11-18 16:42:57 +00:00
|
|
|
|
;; Non-nil if we are on the second line.
|
1997-06-17 21:46:40 +00:00
|
|
|
|
second-line-prefix
|
|
|
|
|
start)
|
|
|
|
|
(setq start (point))
|
|
|
|
|
(setq first-line-prefix
|
2001-03-07 00:12:59 +00:00
|
|
|
|
;; We don't need to consider `paragraph-start' here since it
|
|
|
|
|
;; will be explicitly checked later on.
|
|
|
|
|
;; Also setting first-line-prefix to nil prevents
|
|
|
|
|
;; second-line-prefix from being used.
|
|
|
|
|
(cond ;; ((looking-at paragraph-start) nil)
|
1997-06-17 21:46:40 +00:00
|
|
|
|
((and adaptive-fill-regexp (looking-at adaptive-fill-regexp))
|
2001-10-30 08:08:12 +00:00
|
|
|
|
(match-string-no-properties 0))
|
1997-06-17 21:46:40 +00:00
|
|
|
|
(adaptive-fill-function (funcall adaptive-fill-function))))
|
1995-11-10 17:13:56 +00:00
|
|
|
|
(forward-line 1)
|
2001-10-30 08:08:12 +00:00
|
|
|
|
(if (< (point) to)
|
|
|
|
|
(progn
|
2002-04-29 04:02:22 +00:00
|
|
|
|
(move-to-left-margin)
|
|
|
|
|
(setq start (point))
|
|
|
|
|
(setq second-line-prefix
|
|
|
|
|
(cond ((looking-at paragraph-start) nil) ;Can it happen ? -stef
|
|
|
|
|
((and adaptive-fill-regexp
|
|
|
|
|
(looking-at adaptive-fill-regexp))
|
|
|
|
|
(buffer-substring-no-properties start (match-end 0)))
|
|
|
|
|
(adaptive-fill-function
|
|
|
|
|
(funcall adaptive-fill-function))))
|
1997-06-17 21:46:40 +00:00
|
|
|
|
;; If we get a fill prefix from the second line,
|
|
|
|
|
;; make sure it or something compatible is on the first line too.
|
2001-10-30 08:08:12 +00:00
|
|
|
|
(when second-line-prefix
|
|
|
|
|
(unless first-line-prefix (setq first-line-prefix ""))
|
2002-04-29 04:02:22 +00:00
|
|
|
|
;; If the non-whitespace chars match the first line,
|
|
|
|
|
;; just use it (this subsumes the 2 checks used previously).
|
|
|
|
|
;; Used when first line is `/* ...' and second-line is
|
|
|
|
|
;; ` * ...'.
|
|
|
|
|
(let ((tmp second-line-prefix)
|
|
|
|
|
(re "\\`"))
|
|
|
|
|
(while (string-match "\\`[ \t]*\\([^ \t]+\\)" tmp)
|
|
|
|
|
(setq re (concat re ".*" (regexp-quote (match-string 1 tmp))))
|
|
|
|
|
(setq tmp (substring tmp (match-end 0))))
|
|
|
|
|
;; (assert (string-match "\\`[ \t]*\\'" tmp))
|
2001-10-30 08:08:12 +00:00
|
|
|
|
|
2002-04-29 04:02:22 +00:00
|
|
|
|
(if (string-match re first-line-prefix)
|
|
|
|
|
second-line-prefix
|
2000-01-11 21:42:12 +00:00
|
|
|
|
|
2002-04-29 04:02:22 +00:00
|
|
|
|
;; Use the longest common substring of both prefixes,
|
|
|
|
|
;; if there is one.
|
|
|
|
|
(fill-common-string-prefix first-line-prefix
|
|
|
|
|
second-line-prefix)))))
|
1997-06-17 21:46:40 +00:00
|
|
|
|
;; If we get a fill prefix from a one-line paragraph,
|
|
|
|
|
;; maybe change it to whitespace,
|
|
|
|
|
;; and check that it isn't a paragraph starter.
|
|
|
|
|
(if first-line-prefix
|
|
|
|
|
(let ((result
|
|
|
|
|
;; If first-line-prefix comes from the first line,
|
|
|
|
|
;; see if it seems reasonable to use for all lines.
|
|
|
|
|
;; If not, replace it with whitespace.
|
|
|
|
|
(if (or (and first-line-regexp
|
|
|
|
|
(string-match first-line-regexp
|
|
|
|
|
first-line-prefix))
|
|
|
|
|
(and comment-start-skip
|
|
|
|
|
(string-match comment-start-skip
|
|
|
|
|
first-line-prefix)))
|
|
|
|
|
first-line-prefix
|
|
|
|
|
(make-string (string-width first-line-prefix) ?\ ))))
|
|
|
|
|
;; But either way, reject it if it indicates the start
|
|
|
|
|
;; of a paragraph when text follows it.
|
|
|
|
|
(if (not (eq 0 (string-match paragraph-start
|
|
|
|
|
(concat result "a"))))
|
1997-06-17 21:47:30 +00:00
|
|
|
|
result)))))))
|
1995-11-10 17:13:56 +00:00
|
|
|
|
|
2001-10-30 08:08:12 +00:00
|
|
|
|
(defun fill-single-word-nobreak-p ()
|
|
|
|
|
"Don't break a line after the first or before the last word of a sentence."
|
|
|
|
|
(or (looking-at "[ \t]*\\sw+[ \t]*[.?!:][ \t]*$")
|
|
|
|
|
(save-excursion
|
|
|
|
|
(skip-chars-backward " \t")
|
|
|
|
|
(and (/= (skip-syntax-backward "w") 0)
|
|
|
|
|
(/= (skip-chars-backward " \t") 0)
|
|
|
|
|
(/= (skip-chars-backward ".?!:") 0)))))
|
|
|
|
|
|
|
|
|
|
(defun fill-french-nobreak-p ()
|
|
|
|
|
"Return nil if French style allows breaking the line at point.
|
|
|
|
|
This is used in `fill-nobreak-predicate' to prevent breaking lines just
|
|
|
|
|
after an opening paren or just before a closing paren or a punctuation
|
|
|
|
|
mark such as `?' or `:'. It is common in French writing to put a space
|
|
|
|
|
at such places, which would normally allow breaking the line at those
|
|
|
|
|
places."
|
|
|
|
|
(or (looking-at "[ \t]*[])}<7D>?!;:-]")
|
|
|
|
|
(save-excursion
|
|
|
|
|
(skip-chars-backward " \t")
|
|
|
|
|
(unless (bolp)
|
|
|
|
|
(backward-char 1)
|
|
|
|
|
(or (looking-at "[([{<7B>]")
|
|
|
|
|
;; Don't cut right after a single-letter word.
|
|
|
|
|
(and (memq (preceding-char) '(?\t ?\ ))
|
|
|
|
|
(eq (char-syntax (following-char)) ?w)))))))
|
|
|
|
|
|
|
|
|
|
(defcustom fill-nobreak-predicate nil
|
|
|
|
|
"List of predicates for recognizing places not to break a line.
|
|
|
|
|
The predicates are called with no arguments, with point at the place to
|
|
|
|
|
be tested. If it returns t, fill commands do not break the line there."
|
|
|
|
|
:group 'fill
|
|
|
|
|
:type 'hook
|
|
|
|
|
:options '(fill-french-nobreak-p fill-single-word-nobreak-p))
|
|
|
|
|
|
2002-11-13 23:32:21 +00:00
|
|
|
|
(defcustom fill-nobreak-invisible nil
|
|
|
|
|
"Non-nil means that fill command do not break lines in invisible text."
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'fill)
|
|
|
|
|
|
2001-10-30 08:08:12 +00:00
|
|
|
|
(defun fill-nobreak-p ()
|
|
|
|
|
"Return nil if breaking the line at point is allowed.
|
2002-11-13 23:32:21 +00:00
|
|
|
|
Can be customized with the variables `fill-nobreak-predicate'
|
|
|
|
|
and `fill-nobreak-invisible'."
|
|
|
|
|
(or
|
|
|
|
|
(and fill-nobreak-invisible (line-move-invisible (point)))
|
|
|
|
|
(unless (bolp)
|
2001-10-30 08:08:12 +00:00
|
|
|
|
(or
|
|
|
|
|
;; Don't break after a period followed by just one space.
|
|
|
|
|
;; Move back to the previous place to break.
|
|
|
|
|
;; The reason is that if a period ends up at the end of a
|
|
|
|
|
;; line, further fills will assume it ends a sentence.
|
|
|
|
|
;; If we now know it does not end a sentence, avoid putting
|
2002-04-10 21:21:12 +00:00
|
|
|
|
;; it at the end of the line.
|
2001-10-30 08:08:12 +00:00
|
|
|
|
(and sentence-end-double-space
|
|
|
|
|
(save-excursion
|
|
|
|
|
(skip-chars-backward ". ")
|
|
|
|
|
(looking-at "\\. \\([^ ]\\|$\\)")))
|
|
|
|
|
;; Another approach to the same problem.
|
|
|
|
|
(save-excursion
|
|
|
|
|
(skip-chars-backward ". ")
|
|
|
|
|
(and (looking-at "\\.")
|
|
|
|
|
(not (looking-at sentence-end))))
|
|
|
|
|
;; Don't split a line if the rest would look like a new paragraph.
|
|
|
|
|
(unless use-hard-newlines
|
|
|
|
|
(save-excursion
|
|
|
|
|
(skip-chars-forward " \t") (looking-at paragraph-start)))
|
2002-11-13 23:32:21 +00:00
|
|
|
|
(run-hook-with-args-until-success 'fill-nobreak-predicate)))))
|
1998-03-09 06:37:20 +00:00
|
|
|
|
|
1998-10-21 11:50:56 +00:00
|
|
|
|
;; Put `fill-find-break-point-function' property to charsets which
|
|
|
|
|
;; require special functions to find line breaking point.
|
2001-10-30 08:08:12 +00:00
|
|
|
|
(dolist (pair '((katakana-jisx0201 . kinsoku)
|
2001-11-19 23:51:03 +00:00
|
|
|
|
(chinese-gb2312 . kinsoku)
|
|
|
|
|
(japanese-jisx0208 . kinsoku)
|
|
|
|
|
(japanese-jisx0212 . kinsoku)
|
|
|
|
|
(chinese-big5-1 . kinsoku)
|
2001-10-30 08:08:12 +00:00
|
|
|
|
(chinese-big5-2 . kinsoku)))
|
|
|
|
|
(put-charset-property (car pair) 'fill-find-break-point-function (cdr pair)))
|
1998-10-19 00:40:10 +00:00
|
|
|
|
|
1998-10-21 11:50:56 +00:00
|
|
|
|
(defun fill-find-break-point (limit)
|
|
|
|
|
"Move point to a proper line breaking position of the current line.
|
1998-10-19 00:40:10 +00:00
|
|
|
|
Don't move back past the buffer position LIMIT.
|
|
|
|
|
|
1998-10-21 11:50:56 +00:00
|
|
|
|
This function is called when we are going to break the current line
|
|
|
|
|
after or before a non-ascii character. If the charset of the
|
|
|
|
|
character has the property `fill-find-break-point-function', this
|
1998-10-19 00:40:10 +00:00
|
|
|
|
function calls the property value as a function with one arg LINEBEG.
|
1998-10-21 11:50:56 +00:00
|
|
|
|
If the charset has no such property, do nothing."
|
|
|
|
|
(let* ((ch (following-char))
|
|
|
|
|
(charset (char-charset ch))
|
|
|
|
|
func)
|
|
|
|
|
(if (eq charset 'ascii)
|
|
|
|
|
(setq ch (preceding-char)
|
|
|
|
|
charset (char-charset ch)))
|
2000-06-07 12:00:23 +00:00
|
|
|
|
(if (charsetp charset)
|
|
|
|
|
(setq func
|
|
|
|
|
(get-charset-property charset 'fill-find-break-point-function)))
|
1999-12-15 00:52:08 +00:00
|
|
|
|
(if (and func (fboundp func))
|
|
|
|
|
(funcall func limit))))
|
1998-10-19 00:40:10 +00:00
|
|
|
|
|
2001-11-19 23:51:03 +00:00
|
|
|
|
(defun fill-delete-prefix (from to prefix)
|
|
|
|
|
"Delete the fill prefix from every line except the first.
|
|
|
|
|
The first line may not even have a fill prefix.
|
|
|
|
|
Point is moved to just past the fill prefix on the first line."
|
2002-04-10 21:21:12 +00:00
|
|
|
|
(let ((fpre (if (and prefix (not (string-match "\\`[ \t]*\\'" prefix)))
|
|
|
|
|
(concat "[ \t]*\\("
|
|
|
|
|
(replace-regexp-in-string
|
|
|
|
|
"[ \t]+" "[ \t]*"
|
|
|
|
|
(regexp-quote prefix))
|
|
|
|
|
"\\)?[ \t]*")
|
|
|
|
|
"[ \t]*")))
|
|
|
|
|
(goto-char from)
|
|
|
|
|
(if (>= (+ (current-left-margin) (length prefix))
|
|
|
|
|
(current-fill-column))
|
|
|
|
|
(error "fill-prefix too long for specified width"))
|
|
|
|
|
(forward-line 1)
|
|
|
|
|
(while (< (point) to)
|
2001-11-19 23:51:03 +00:00
|
|
|
|
(if (looking-at fpre)
|
2002-04-10 21:21:12 +00:00
|
|
|
|
(delete-region (point) (match-end 0)))
|
|
|
|
|
(forward-line 1))
|
|
|
|
|
(goto-char from)
|
|
|
|
|
(if (looking-at fpre)
|
|
|
|
|
(goto-char (match-end 0)))
|
|
|
|
|
(setq from (point))))
|
2001-11-19 23:51:03 +00:00
|
|
|
|
|
|
|
|
|
(defun fill-delete-newlines (from to justify nosqueeze squeeze-after)
|
|
|
|
|
(goto-char from)
|
|
|
|
|
;; Make sure sentences ending at end of line get an extra space.
|
|
|
|
|
;; loses on split abbrevs ("Mr.\nSmith")
|
2002-04-10 21:21:12 +00:00
|
|
|
|
(let ((eol-double-space-re
|
|
|
|
|
(cond
|
|
|
|
|
((not colon-double-space) (concat sentence-end "$"))
|
|
|
|
|
;; Try to add the : inside the `sentence-end' regexp.
|
|
|
|
|
((string-match "\\[[^][]*\\(\\.\\)[^][]*\\]" sentence-end)
|
|
|
|
|
(concat (replace-match ".:" nil nil sentence-end 1) "$"))
|
|
|
|
|
;; Can't find the right spot to insert the colon.
|
|
|
|
|
(t "[.?!:][])}\"']*$"))))
|
2001-11-19 23:51:03 +00:00
|
|
|
|
(while (re-search-forward eol-double-space-re to t)
|
2002-04-10 21:21:12 +00:00
|
|
|
|
(or (>= (point) to) (memq (char-before) '(?\t ?\ ))
|
|
|
|
|
(insert-and-inherit ?\ ))))
|
2001-11-19 23:51:03 +00:00
|
|
|
|
|
|
|
|
|
(goto-char from)
|
|
|
|
|
(if enable-multibyte-characters
|
|
|
|
|
;; Delete unnecessay newlines surrounded by words. The
|
|
|
|
|
;; character category `|' means that we can break a line
|
|
|
|
|
;; at the character. And, charset property
|
|
|
|
|
;; `nospace-between-words' tells how to concatenate
|
|
|
|
|
;; words. If the value is non-nil, never put spaces
|
|
|
|
|
;; between words, thus delete a newline between them.
|
|
|
|
|
;; If the value is nil, delete a newline only when a
|
|
|
|
|
;; character preceding a newline has text property
|
|
|
|
|
;; `nospace-between-words'.
|
|
|
|
|
(while (search-forward "\n" to t)
|
|
|
|
|
(let ((prev (char-before (match-beginning 0)))
|
|
|
|
|
(next (following-char)))
|
|
|
|
|
(if (and (or (aref (char-category-set next) ?|)
|
|
|
|
|
(aref (char-category-set prev) ?|))
|
|
|
|
|
(or (get-charset-property (char-charset prev)
|
|
|
|
|
'nospace-between-words)
|
|
|
|
|
(get-text-property (1- (match-beginning 0))
|
|
|
|
|
'nospace-between-words)))
|
|
|
|
|
(delete-char -1)))))
|
|
|
|
|
|
|
|
|
|
(goto-char from)
|
|
|
|
|
(skip-chars-forward " \t")
|
|
|
|
|
;; Then change all newlines to spaces.
|
|
|
|
|
(subst-char-in-region from to ?\n ?\ )
|
|
|
|
|
(if (and nosqueeze (not (eq justify 'full)))
|
|
|
|
|
nil
|
2003-04-02 23:40:36 +00:00
|
|
|
|
(canonically-space-region (or squeeze-after (point)) to))
|
2001-11-19 23:51:03 +00:00
|
|
|
|
(goto-char from))
|
|
|
|
|
|
2002-04-10 21:21:12 +00:00
|
|
|
|
(defun fill-move-to-break-point (linebeg)
|
|
|
|
|
"Move to the position where the line should be broken.
|
2002-04-11 22:00:07 +00:00
|
|
|
|
The break position will be always after LINEBEG and generally before point."
|
|
|
|
|
;; If the fill column is before linebeg, move to linebeg.
|
|
|
|
|
(if (> linebeg (point)) (goto-char linebeg))
|
2002-04-10 21:21:12 +00:00
|
|
|
|
;; Move back to the point where we can break the line
|
|
|
|
|
;; at. We break the line between word or after/before
|
|
|
|
|
;; the character which has character category `|'. We
|
|
|
|
|
;; search space, \c| followed by a character, or \c|
|
|
|
|
|
;; following a character. If not found, place
|
|
|
|
|
;; the point at linebeg.
|
|
|
|
|
(while
|
|
|
|
|
(when (re-search-backward "[ \t]\\|\\c|.\\|.\\c|" linebeg 0)
|
|
|
|
|
;; In case of space, we place the point at next to
|
|
|
|
|
;; the point where the break occurs actually,
|
|
|
|
|
;; because we don't want to change the following
|
|
|
|
|
;; logic of original Emacs. In case of \c|, the
|
|
|
|
|
;; point is at the place where the break occurs.
|
|
|
|
|
(forward-char 1)
|
|
|
|
|
(when (fill-nobreak-p) (skip-chars-backward " \t" linebeg))))
|
2002-10-31 00:30:36 +00:00
|
|
|
|
|
|
|
|
|
;; Move back over the single space between the words.
|
|
|
|
|
(skip-chars-backward " \t")
|
|
|
|
|
|
2002-04-10 21:21:12 +00:00
|
|
|
|
;; If the left margin and fill prefix by themselves
|
|
|
|
|
;; pass the fill-column. or if they are zero
|
|
|
|
|
;; but we have no room for even one word,
|
|
|
|
|
;; keep at least one word or a character which has
|
|
|
|
|
;; category `|' anyway.
|
|
|
|
|
(if (>= linebeg (point))
|
|
|
|
|
;; Ok, skip at least one word or one \c| character.
|
|
|
|
|
;; Meanwhile, don't stop at a period followed by one space.
|
|
|
|
|
(let ((to (line-end-position))
|
|
|
|
|
(fill-nobreak-predicate nil) ;to break sooner.
|
|
|
|
|
(first t))
|
|
|
|
|
(goto-char linebeg)
|
|
|
|
|
(while (and (< (point) to) (or first (fill-nobreak-p)))
|
|
|
|
|
;; Find a breakable point while ignoring the
|
|
|
|
|
;; following spaces.
|
|
|
|
|
(skip-chars-forward " \t")
|
|
|
|
|
(if (looking-at "\\c|")
|
|
|
|
|
(forward-char 1)
|
|
|
|
|
(let ((pos (save-excursion
|
|
|
|
|
(skip-chars-forward "^ \n\t")
|
|
|
|
|
(point))))
|
|
|
|
|
(if (re-search-forward "\\c|" pos t)
|
|
|
|
|
(forward-char -1)
|
|
|
|
|
(goto-char pos))))
|
|
|
|
|
(setq first nil)))
|
|
|
|
|
|
|
|
|
|
(if enable-multibyte-characters
|
|
|
|
|
;; If we are going to break the line after or
|
|
|
|
|
;; before a non-ascii character, we may have to
|
|
|
|
|
;; run a special function for the charset of the
|
|
|
|
|
;; character to find the correct break point.
|
|
|
|
|
(if (not (and (eq (charset-after (1- (point))) 'ascii)
|
|
|
|
|
(eq (charset-after (point)) 'ascii)))
|
|
|
|
|
;; Make sure we take SOMETHING after the fill prefix if any.
|
|
|
|
|
(fill-find-break-point linebeg)))))
|
|
|
|
|
|
|
|
|
|
(defun fill-newline ()
|
|
|
|
|
;; Replace whitespace here with one newline, then
|
|
|
|
|
;; indent to left margin.
|
|
|
|
|
(skip-chars-backward " \t")
|
|
|
|
|
(if (and (= (following-char) ?\ )
|
|
|
|
|
(or (aref (char-category-set (preceding-char)) ?|)
|
|
|
|
|
(looking-at "[ \t]+\\c|")))
|
|
|
|
|
;; We need one space at end of line so that
|
|
|
|
|
;; further filling won't delete it. NOTE: We
|
|
|
|
|
;; intentionally leave this one space to
|
|
|
|
|
;; distingush the case that user wants to put
|
|
|
|
|
;; space between \c| characters.
|
|
|
|
|
(forward-char 1))
|
|
|
|
|
(insert ?\n)
|
|
|
|
|
;; Give newline the properties of the space(s) it replaces
|
|
|
|
|
(set-text-properties (1- (point)) (point)
|
|
|
|
|
(text-properties-at (point)))
|
2002-11-13 23:32:21 +00:00
|
|
|
|
;; If we don't want breaks in invisible text, don't insert
|
|
|
|
|
;; an invisible newline.
|
|
|
|
|
(if fill-nobreak-invisible
|
|
|
|
|
(remove-text-properties (1- (point)) (point)
|
|
|
|
|
'(invisible t)))
|
2002-04-10 21:21:12 +00:00
|
|
|
|
(if (or fill-prefix
|
|
|
|
|
(not fill-indent-according-to-mode))
|
2002-12-02 16:19:52 +00:00
|
|
|
|
(fill-indent-to-left-margin)
|
2002-04-10 21:21:12 +00:00
|
|
|
|
(indent-according-to-mode))
|
|
|
|
|
;; Insert the fill prefix after indentation.
|
|
|
|
|
;; Set prefixcol so whitespace in the prefix won't get lost.
|
|
|
|
|
(and fill-prefix (not (equal fill-prefix ""))
|
|
|
|
|
(insert-and-inherit fill-prefix)))
|
|
|
|
|
|
2002-12-02 16:19:52 +00:00
|
|
|
|
(defun fill-indent-to-left-margin ()
|
|
|
|
|
"Indent current line to the column given by `current-left-margin'."
|
|
|
|
|
(let ((beg (point)))
|
|
|
|
|
(indent-line-to (current-left-margin))
|
|
|
|
|
(put-text-property beg (point) 'face 'default)))
|
|
|
|
|
|
1996-02-21 21:20:28 +00:00
|
|
|
|
(defun fill-region-as-paragraph (from to &optional justify
|
|
|
|
|
nosqueeze squeeze-after)
|
1995-02-23 18:22:04 +00:00
|
|
|
|
"Fill the region as one paragraph.
|
1995-11-17 23:32:15 +00:00
|
|
|
|
It removes any paragraph breaks in the region and extra newlines at the end,
|
1995-02-23 18:22:04 +00:00
|
|
|
|
indents and fills lines between the margins given by the
|
|
|
|
|
`current-left-margin' and `current-fill-column' functions.
|
1997-07-07 18:27:33 +00:00
|
|
|
|
\(In most cases, the variable `fill-column' controls the width.)
|
1995-11-17 23:32:15 +00:00
|
|
|
|
It leaves point at the beginning of the line following the paragraph.
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
|
1995-02-23 18:22:04 +00:00
|
|
|
|
Normally performs justification according to the `current-justification'
|
|
|
|
|
function, but with a prefix arg, does full justification instead.
|
|
|
|
|
|
|
|
|
|
From a program, optional third arg JUSTIFY can specify any type of
|
1996-02-21 21:20:28 +00:00
|
|
|
|
justification. Fourth arg NOSQUEEZE non-nil means not to make spaces
|
1996-02-22 02:57:50 +00:00
|
|
|
|
between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil,
|
1996-02-21 21:20:28 +00:00
|
|
|
|
means don't canonicalize spaces before that position.
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
|
2002-10-31 00:30:36 +00:00
|
|
|
|
Return the `fill-prefix' used for filling.
|
2000-11-13 12:11:04 +00:00
|
|
|
|
|
1994-02-04 04:45:52 +00:00
|
|
|
|
If `sentence-end-double-space' is non-nil, then period followed by one
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
space does not end a sentence, so don't break a line there."
|
1999-07-20 16:02:27 +00:00
|
|
|
|
(interactive (progn
|
|
|
|
|
(barf-if-buffer-read-only)
|
|
|
|
|
(list (region-beginning) (region-end)
|
|
|
|
|
(if current-prefix-arg 'full))))
|
1998-03-14 03:05:28 +00:00
|
|
|
|
(unless (memq justify '(t nil none full center left right))
|
1998-03-08 00:27:23 +00:00
|
|
|
|
(setq justify 'full))
|
1995-02-23 18:22:04 +00:00
|
|
|
|
|
1995-11-17 23:32:15 +00:00
|
|
|
|
;; Make sure "to" is the endpoint.
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(goto-char (min from to))
|
|
|
|
|
(setq to (max from to))
|
1995-11-17 23:32:15 +00:00
|
|
|
|
;; Ignore blank lines at beginning of region.
|
|
|
|
|
(skip-chars-forward " \t\n")
|
1995-02-23 18:22:04 +00:00
|
|
|
|
|
1995-11-17 23:32:15 +00:00
|
|
|
|
(let ((from-plus-indent (point))
|
|
|
|
|
(oneleft nil))
|
|
|
|
|
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(setq from (point))
|
2003-02-04 13:30:45 +00:00
|
|
|
|
|
1995-11-17 23:32:15 +00:00
|
|
|
|
;; Delete all but one soft newline at end of region.
|
1996-01-09 23:18:07 +00:00
|
|
|
|
;; And leave TO before that one.
|
1995-11-17 23:32:15 +00:00
|
|
|
|
(goto-char to)
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
|
|
|
|
|
(if (and oneleft
|
|
|
|
|
(not (and use-hard-newlines
|
|
|
|
|
(get-text-property (1- (point)) 'hard))))
|
|
|
|
|
(delete-backward-char 1)
|
|
|
|
|
(backward-char 1)
|
|
|
|
|
(setq oneleft t)))
|
2001-11-19 23:51:03 +00:00
|
|
|
|
(setq to (copy-marker (point) t))
|
2000-11-10 11:43:55 +00:00
|
|
|
|
;; ;; If there was no newline, and there is text in the paragraph, then
|
|
|
|
|
;; ;; create a newline.
|
|
|
|
|
;; (if (and (not oneleft) (> to from-plus-indent))
|
|
|
|
|
;; (newline))
|
1995-11-17 23:32:15 +00:00
|
|
|
|
(goto-char from-plus-indent))
|
|
|
|
|
|
|
|
|
|
(if (not (> to (point)))
|
2002-04-10 21:21:12 +00:00
|
|
|
|
nil ;; There is no paragraph, only whitespace: exit now.
|
1995-02-23 18:22:04 +00:00
|
|
|
|
|
|
|
|
|
(or justify (setq justify (current-justification)))
|
|
|
|
|
|
|
|
|
|
;; Don't let Adaptive Fill mode alter the fill prefix permanently.
|
|
|
|
|
(let ((fill-prefix fill-prefix))
|
|
|
|
|
;; Figure out how this paragraph is indented, if desired.
|
2001-11-19 23:51:03 +00:00
|
|
|
|
(when (and adaptive-fill-mode
|
|
|
|
|
(or (null fill-prefix) (string= fill-prefix "")))
|
|
|
|
|
(setq fill-prefix (fill-context-prefix from to))
|
|
|
|
|
;; Ignore a white-space only fill-prefix
|
|
|
|
|
;; if we indent-according-to-mode.
|
|
|
|
|
(when (and fill-prefix fill-indent-according-to-mode
|
|
|
|
|
(string-match "\\`[ \t]*\\'" fill-prefix))
|
|
|
|
|
(setq fill-prefix nil)))
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
|
2003-04-02 23:04:09 +00:00
|
|
|
|
(goto-char from)
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
|
|
|
|
|
(if (not justify) ; filling disabled: just check indentation
|
|
|
|
|
(progn
|
|
|
|
|
(goto-char from)
|
2002-04-10 21:21:12 +00:00
|
|
|
|
(while (< (point) to)
|
2003-04-02 23:04:09 +00:00
|
|
|
|
(if (and (not (eolp))
|
|
|
|
|
(< (current-indentation) (current-left-margin)))
|
|
|
|
|
(fill-indent-to-left-margin))
|
|
|
|
|
(forward-line 1)))
|
|
|
|
|
|
|
|
|
|
(if use-hard-newlines
|
|
|
|
|
(remove-list-of-text-properties from to '(hard)))
|
|
|
|
|
;; Make sure first line is indented (at least) to left margin...
|
|
|
|
|
(if (or (memq justify '(right center))
|
|
|
|
|
(< (current-indentation) (current-left-margin)))
|
|
|
|
|
(fill-indent-to-left-margin))
|
|
|
|
|
;; Delete the fill-prefix from every line.
|
|
|
|
|
(fill-delete-prefix from to fill-prefix)
|
|
|
|
|
(setq from (point))
|
|
|
|
|
|
|
|
|
|
;; FROM, and point, are now before the text to fill,
|
|
|
|
|
;; but after any fill prefix on the first line.
|
|
|
|
|
|
|
|
|
|
(fill-delete-newlines from to justify nosqueeze squeeze-after)
|
|
|
|
|
|
|
|
|
|
;; This is the actual filling loop.
|
|
|
|
|
(goto-char from)
|
|
|
|
|
(let (linebeg)
|
|
|
|
|
(while (< (point) to)
|
|
|
|
|
(setq linebeg (point))
|
|
|
|
|
(move-to-column (1+ (current-fill-column)))
|
|
|
|
|
(if (when (< (point) to)
|
|
|
|
|
;; Find the position where we'll break the line.
|
|
|
|
|
(fill-move-to-break-point linebeg)
|
|
|
|
|
;; Check again to see if we got to the end of
|
|
|
|
|
;; the paragraph.
|
|
|
|
|
(skip-chars-forward " \t")
|
|
|
|
|
(< (point) to))
|
|
|
|
|
;; Found a place to cut.
|
|
|
|
|
(progn
|
|
|
|
|
(fill-newline)
|
|
|
|
|
(when justify
|
|
|
|
|
;; Justify the line just ended, if desired.
|
|
|
|
|
(save-excursion
|
|
|
|
|
(forward-line -1)
|
|
|
|
|
(justify-current-line justify nil t))))
|
|
|
|
|
|
|
|
|
|
(goto-char to)
|
|
|
|
|
;; Justify this last line, if desired.
|
|
|
|
|
(if justify (justify-current-line justify t t))))))
|
|
|
|
|
;; Leave point after final newline.
|
|
|
|
|
(goto-char to)
|
|
|
|
|
(unless (eobp) (forward-char 1))
|
2000-11-13 12:11:04 +00:00
|
|
|
|
;; Return the fill-prefix we used
|
|
|
|
|
fill-prefix)))
|
|
|
|
|
|
|
|
|
|
(defsubst skip-line-prefix (prefix)
|
|
|
|
|
"If point is inside the string PREFIX at the beginning of line, move past it."
|
|
|
|
|
(when (and prefix
|
|
|
|
|
(< (- (point) (line-beginning-position)) (length prefix))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(looking-at (regexp-quote prefix))))
|
|
|
|
|
(goto-char (match-end 0))))
|
1990-07-27 04:20:16 +00:00
|
|
|
|
|
|
|
|
|
(defun fill-paragraph (arg)
|
2001-02-22 20:09:13 +00:00
|
|
|
|
"Fill paragraph at or after point. Prefix ARG means justify as well.
|
1994-02-04 04:45:52 +00:00
|
|
|
|
If `sentence-end-double-space' is non-nil, then period followed by one
|
1995-02-02 09:50:23 +00:00
|
|
|
|
space does not end a sentence, so don't break a line there.
|
1997-07-07 18:27:33 +00:00
|
|
|
|
the variable `fill-column' controls the width for filling.
|
1995-02-02 09:50:23 +00:00
|
|
|
|
|
|
|
|
|
If `fill-paragraph-function' is non-nil, we call it (passing our
|
2000-11-13 12:11:04 +00:00
|
|
|
|
argument to it), and if it returns non-nil, we simply return its value.
|
|
|
|
|
|
2001-02-22 20:09:13 +00:00
|
|
|
|
If `fill-paragraph-function' is nil, return the `fill-prefix' used for filling."
|
1999-07-20 16:02:27 +00:00
|
|
|
|
(interactive (progn
|
|
|
|
|
(barf-if-buffer-read-only)
|
|
|
|
|
(list (if current-prefix-arg 'full))))
|
2002-11-03 03:41:11 +00:00
|
|
|
|
;; First try fill-paragraph-function.
|
1995-02-02 09:50:23 +00:00
|
|
|
|
(or (and fill-paragraph-function
|
1995-02-02 19:28:31 +00:00
|
|
|
|
(let ((function fill-paragraph-function)
|
2002-11-03 03:41:11 +00:00
|
|
|
|
;; If fill-paragraph-function is set, it probably takes care
|
|
|
|
|
;; of comments and stuff. If not, it will have to set
|
|
|
|
|
;; fill-paragraph-handle-comment back to t explicitly or
|
|
|
|
|
;; return nil.
|
|
|
|
|
(fill-paragraph-handle-comment nil)
|
1995-02-02 19:28:31 +00:00
|
|
|
|
fill-paragraph-function)
|
|
|
|
|
(funcall function arg)))
|
2002-11-03 03:41:11 +00:00
|
|
|
|
;; Then try our syntax-aware filling code.
|
|
|
|
|
(and fill-paragraph-handle-comment
|
2002-11-07 22:51:28 +00:00
|
|
|
|
;; Our code only handles \n-terminated comments right now.
|
2002-11-03 03:41:11 +00:00
|
|
|
|
comment-start comment-start-skip (equal comment-end "")
|
|
|
|
|
(let ((fill-paragraph-handle-comment nil))
|
|
|
|
|
(fill-comment-paragraph arg)))
|
|
|
|
|
;; If it all fails, default to the good ol' text paragraph filling.
|
1998-06-05 22:27:42 +00:00
|
|
|
|
(let ((before (point))
|
2002-11-07 22:51:28 +00:00
|
|
|
|
(paragraph-start paragraph-start)
|
2002-04-11 22:00:07 +00:00
|
|
|
|
;; Fill prefix used for filling the paragraph.
|
|
|
|
|
fill-pfx)
|
2002-11-07 22:51:28 +00:00
|
|
|
|
;; Try to prevent code sections and comment sections from being
|
|
|
|
|
;; filled together.
|
|
|
|
|
(when (and fill-paragraph-handle-comment comment-start-skip)
|
|
|
|
|
(setq paragraph-start
|
|
|
|
|
(concat paragraph-start "\\|[ \t]*\\(?:"
|
|
|
|
|
comment-start-skip "\\)")))
|
1995-02-02 09:50:23 +00:00
|
|
|
|
(save-excursion
|
2002-10-07 13:54:44 +00:00
|
|
|
|
;; To make sure the return value of forward-paragraph is meaningful,
|
|
|
|
|
;; we have to start from the beginning of line, otherwise skipping
|
|
|
|
|
;; past the last few chars of a paragraph-separator would count as
|
|
|
|
|
;; a paragraph (and not skipping any chars at EOB would not count
|
|
|
|
|
;; as a paragraph even if it is).
|
|
|
|
|
(move-to-left-margin)
|
2002-10-03 23:01:51 +00:00
|
|
|
|
(if (not (zerop (forward-paragraph)))
|
|
|
|
|
;; There's no paragraph at or after point: give up.
|
|
|
|
|
(setq fill-pfx "")
|
|
|
|
|
(let ((end (point))
|
|
|
|
|
(beg (progn (backward-paragraph) (point))))
|
|
|
|
|
(goto-char before)
|
|
|
|
|
(setq fill-pfx
|
|
|
|
|
(if use-hard-newlines
|
|
|
|
|
;; Can't use fill-region-as-paragraph, since this
|
|
|
|
|
;; paragraph may still contain hard newlines. See
|
|
|
|
|
;; fill-region.
|
|
|
|
|
(fill-region beg end arg)
|
|
|
|
|
(fill-region-as-paragraph beg end arg))))))
|
2000-11-13 12:11:04 +00:00
|
|
|
|
;; See if point ended up inside the fill-prefix, and if so, move
|
|
|
|
|
;; past it.
|
|
|
|
|
(skip-line-prefix fill-pfx)
|
|
|
|
|
fill-pfx)))
|
1990-07-27 04:20:16 +00:00
|
|
|
|
|
2002-11-03 03:41:11 +00:00
|
|
|
|
(defun fill-comment-paragraph (&optional justify)
|
|
|
|
|
"Fill current comment.
|
|
|
|
|
If we're not in a comment, just return nil so that the caller
|
2002-11-07 22:51:28 +00:00
|
|
|
|
can take care of filling. JUSTIFY is used as in `fill-paragraph'."
|
|
|
|
|
(comment-normalize-vars)
|
|
|
|
|
(let (has-code-and-comment ; Non-nil if it contains code and a comment.
|
|
|
|
|
comin comstart)
|
2002-11-03 03:41:11 +00:00
|
|
|
|
;; Figure out what kind of comment we are looking at.
|
|
|
|
|
(save-excursion
|
|
|
|
|
(beginning-of-line)
|
2002-11-07 22:51:28 +00:00
|
|
|
|
(when (setq comstart (comment-search-forward (line-end-position) t))
|
|
|
|
|
(setq comin (point))
|
|
|
|
|
(goto-char comstart) (skip-chars-backward " \t")
|
|
|
|
|
(setq has-code-and-comment (not (bolp)))))
|
2003-02-04 13:30:45 +00:00
|
|
|
|
|
2002-11-07 22:51:28 +00:00
|
|
|
|
(if (not comstart)
|
2002-11-03 03:41:11 +00:00
|
|
|
|
;; Return nil, so the normal filling will take place.
|
|
|
|
|
nil
|
|
|
|
|
|
|
|
|
|
;; Narrow to include only the comment, and then fill the region.
|
2002-11-07 22:51:28 +00:00
|
|
|
|
(let* ((fill-prefix fill-prefix)
|
|
|
|
|
(comment-re (concat "[ \t]*\\(?:" comment-start-skip "\\)"))
|
|
|
|
|
(comment-fill-prefix ; Compute a fill prefix.
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char comstart)
|
|
|
|
|
(if has-code-and-comment
|
|
|
|
|
(concat (make-string (/ (current-column) tab-width) ?\t)
|
|
|
|
|
(make-string (% (current-column) tab-width) ?\ )
|
|
|
|
|
(buffer-substring (point) comin))
|
|
|
|
|
(buffer-substring (line-beginning-position) comin))))
|
|
|
|
|
beg end)
|
|
|
|
|
(save-excursion
|
|
|
|
|
(save-restriction
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(narrow-to-region
|
|
|
|
|
;; Find the first line we should include in the region to fill.
|
|
|
|
|
(if has-code-and-comment
|
|
|
|
|
(line-beginning-position)
|
|
|
|
|
(save-excursion
|
|
|
|
|
(while (and (zerop (forward-line -1))
|
|
|
|
|
(looking-at comment-re)))
|
|
|
|
|
;; We may have gone too far. Go forward again.
|
|
|
|
|
(line-beginning-position
|
2003-01-19 00:19:44 +00:00
|
|
|
|
(if (looking-at (concat ".*\\(?:" comment-start-skip "\\)"))
|
2002-11-07 22:51:28 +00:00
|
|
|
|
1 2))))
|
2002-11-03 03:41:11 +00:00
|
|
|
|
;; Find the beginning of the first line past the region to fill.
|
|
|
|
|
(save-excursion
|
|
|
|
|
(while (progn (forward-line 1)
|
|
|
|
|
(looking-at comment-re)))
|
|
|
|
|
(point)))
|
2002-11-07 22:51:28 +00:00
|
|
|
|
;; Obey paragraph starters and boundaries within comments.
|
|
|
|
|
(let* ((paragraph-separate
|
|
|
|
|
;; Use the default values since they correspond to
|
|
|
|
|
;; the values to use for plain text.
|
|
|
|
|
(concat paragraph-separate "\\|[ \t]*\\(?:"
|
|
|
|
|
comment-start-skip "\\)\\(?:"
|
|
|
|
|
(default-value 'paragraph-separate) "\\)"))
|
|
|
|
|
(paragraph-start
|
|
|
|
|
(concat paragraph-start "\\|[ \t]*\\(?:"
|
|
|
|
|
comment-start-skip "\\)\\(?:"
|
|
|
|
|
(default-value 'paragraph-start) "\\)"))
|
|
|
|
|
(paragraph-ignore-fill-prefix nil)
|
|
|
|
|
(fill-prefix comment-fill-prefix)
|
|
|
|
|
(after-line (if has-code-and-comment
|
2003-04-02 23:04:09 +00:00
|
|
|
|
(line-beginning-position 2))))
|
|
|
|
|
(setq end (progn (forward-paragraph) (point)))
|
2002-11-07 22:51:28 +00:00
|
|
|
|
;; If this comment starts on a line with code,
|
|
|
|
|
;; include that line in the filling.
|
|
|
|
|
(setq beg (progn (backward-paragraph)
|
|
|
|
|
(if (eq (point) after-line)
|
|
|
|
|
(forward-line -1))
|
2003-04-02 23:04:09 +00:00
|
|
|
|
(point)))))
|
2002-11-07 22:51:28 +00:00
|
|
|
|
|
|
|
|
|
;; Find the fill-prefix to use.
|
|
|
|
|
(cond
|
|
|
|
|
(fill-prefix) ; Use the user-provided fill prefix.
|
|
|
|
|
((and adaptive-fill-mode ; Try adaptive fill mode.
|
|
|
|
|
(setq fill-prefix (fill-context-prefix beg end))
|
|
|
|
|
(string-match comment-start-skip fill-prefix)))
|
|
|
|
|
(t
|
|
|
|
|
(setq fill-prefix comment-fill-prefix)))
|
|
|
|
|
|
|
|
|
|
;; Don't fill with narrowing.
|
|
|
|
|
(or
|
|
|
|
|
(fill-region-as-paragraph
|
|
|
|
|
beg end justify nil
|
|
|
|
|
;; Don't canonicalize spaces within the code just before
|
|
|
|
|
;; the comment.
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char beg)
|
|
|
|
|
(if (looking-at fill-prefix)
|
|
|
|
|
nil
|
|
|
|
|
(re-search-forward comment-start-skip))))
|
|
|
|
|
;; Make sure we don't return nil.
|
|
|
|
|
t))))))
|
2002-11-03 03:41:11 +00:00
|
|
|
|
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
(defun fill-region (from to &optional justify nosqueeze to-eop)
|
1990-07-27 04:20:16 +00:00
|
|
|
|
"Fill each of the paragraphs in the region.
|
1998-03-08 00:27:23 +00:00
|
|
|
|
A prefix arg means justify as well.
|
1997-07-07 18:27:33 +00:00
|
|
|
|
Ordinarily the variable `fill-column' controls the width.
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
|
1998-03-08 00:27:23 +00:00
|
|
|
|
Noninteractively, the third argument JUSTIFY specifies which
|
|
|
|
|
kind of justification to do: `full', `left', `right', `center',
|
1998-03-14 03:05:28 +00:00
|
|
|
|
or `none' (equivalent to nil). t means handle each paragraph
|
|
|
|
|
as specified by its text properties.
|
|
|
|
|
|
1998-03-08 00:27:23 +00:00
|
|
|
|
The fourth arg NOSQUEEZE non-nil means to leave
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
whitespace other than line breaks untouched, and fifth arg TO-EOP
|
|
|
|
|
non-nil means to keep filling to the end of the paragraph (or next
|
|
|
|
|
hard newline, if `use-hard-newlines' is on).
|
|
|
|
|
|
2000-11-13 12:11:04 +00:00
|
|
|
|
Return the fill-prefix used for filling the last paragraph.
|
|
|
|
|
|
1994-02-04 04:45:52 +00:00
|
|
|
|
If `sentence-end-double-space' is non-nil, then period followed by one
|
|
|
|
|
space does not end a sentence, so don't break a line there."
|
1999-07-20 16:02:27 +00:00
|
|
|
|
(interactive (progn
|
|
|
|
|
(barf-if-buffer-read-only)
|
|
|
|
|
(list (region-beginning) (region-end)
|
|
|
|
|
(if current-prefix-arg 'full))))
|
1998-03-14 03:05:28 +00:00
|
|
|
|
(unless (memq justify '(t nil none full center left right))
|
1998-03-08 00:27:23 +00:00
|
|
|
|
(setq justify 'full))
|
2002-10-31 00:30:36 +00:00
|
|
|
|
(let (max beg fill-pfx)
|
|
|
|
|
(goto-char (max from to))
|
|
|
|
|
(when to-eop
|
|
|
|
|
(skip-chars-backward "\n")
|
|
|
|
|
(forward-paragraph))
|
|
|
|
|
(setq max (copy-marker (point) t))
|
|
|
|
|
(goto-char (setq beg (min from to)))
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(while (< (point) max)
|
|
|
|
|
(let ((initial (point))
|
|
|
|
|
end)
|
|
|
|
|
;; If using hard newlines, break at every one for filling
|
|
|
|
|
;; purposes rather than using paragraph breaks.
|
|
|
|
|
(if use-hard-newlines
|
|
|
|
|
(progn
|
|
|
|
|
(while (and (setq end (text-property-any (point) max
|
|
|
|
|
'hard t))
|
|
|
|
|
(not (= ?\n (char-after end)))
|
|
|
|
|
(not (>= end max)))
|
|
|
|
|
(goto-char (1+ end)))
|
|
|
|
|
(setq end (if end (min max (1+ end)) max))
|
|
|
|
|
(goto-char initial))
|
|
|
|
|
(forward-paragraph 1)
|
|
|
|
|
(setq end (min max (point)))
|
|
|
|
|
(forward-paragraph -1))
|
|
|
|
|
(if (< (point) beg)
|
|
|
|
|
(goto-char beg))
|
|
|
|
|
(if (>= (point) initial)
|
|
|
|
|
(setq fill-pfx
|
|
|
|
|
(fill-region-as-paragraph (point) end justify nosqueeze))
|
|
|
|
|
(goto-char end))))
|
|
|
|
|
fill-pfx))
|
1990-07-27 04:20:16 +00:00
|
|
|
|
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
|
1997-05-05 15:00:53 +00:00
|
|
|
|
(defcustom default-justification 'left
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
"*Method of justifying text not otherwise specified.
|
|
|
|
|
Possible values are `left', `right', `full', `center', or `none'.
|
|
|
|
|
The requested kind of justification is done whenever lines are filled.
|
2001-10-30 08:08:12 +00:00
|
|
|
|
The `justification' text-property can locally override this variable."
|
1997-05-05 15:00:53 +00:00
|
|
|
|
:type '(choice (const left)
|
|
|
|
|
(const right)
|
|
|
|
|
(const full)
|
|
|
|
|
(const center)
|
|
|
|
|
(const none))
|
|
|
|
|
:group 'fill)
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
(make-variable-buffer-local 'default-justification)
|
|
|
|
|
|
1995-01-19 04:27:15 +00:00
|
|
|
|
(defun current-justification ()
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
"How should we justify this line?
|
|
|
|
|
This returns the value of the text-property `justification',
|
|
|
|
|
or the variable `default-justification' if there is no text-property.
|
|
|
|
|
However, it returns nil rather than `none' to mean \"don't justify\"."
|
2003-02-04 13:30:45 +00:00
|
|
|
|
(let ((j (or (get-text-property
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
;; Make sure we're looking at paragraph body.
|
2003-02-04 13:30:45 +00:00
|
|
|
|
(save-excursion (skip-chars-forward " \t")
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(if (and (eobp) (not (bobp)))
|
|
|
|
|
(1- (point)) (point)))
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
'justification)
|
|
|
|
|
default-justification)))
|
|
|
|
|
(if (eq 'none j)
|
|
|
|
|
nil
|
|
|
|
|
j)))
|
|
|
|
|
|
2001-12-07 03:34:59 +00:00
|
|
|
|
(defun set-justification (begin end style &optional whole-par)
|
|
|
|
|
"Set the region's justification style to STYLE.
|
|
|
|
|
This commands prompts for the kind of justification to use.
|
1995-02-23 18:22:04 +00:00
|
|
|
|
If the mark is not active, this command operates on the current paragraph.
|
2001-12-07 03:34:59 +00:00
|
|
|
|
If the mark is active, it operates on the region. However, if the
|
|
|
|
|
beginning and end of the region are not at paragraph breaks, they are
|
|
|
|
|
moved to the beginning and end \(respectively) of the paragraphs they
|
|
|
|
|
are in.
|
|
|
|
|
|
1995-02-23 18:22:04 +00:00
|
|
|
|
If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
|
|
|
|
|
breaks.
|
|
|
|
|
|
|
|
|
|
When calling from a program, operates just on region between BEGIN and END,
|
|
|
|
|
unless optional fourth arg WHOLE-PAR is non-nil. In that case bounds are
|
|
|
|
|
extended to include entire paragraphs as in the interactive command."
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
(interactive (list (if mark-active (region-beginning) (point))
|
|
|
|
|
(if mark-active (region-end) (point))
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(let ((s (completing-read
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
"Set justification to: "
|
1995-02-23 18:22:04 +00:00
|
|
|
|
'(("left") ("right") ("full")
|
|
|
|
|
("center") ("none"))
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
nil t)))
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(if (equal s "") (error ""))
|
|
|
|
|
(intern s))
|
|
|
|
|
t))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(save-restriction
|
|
|
|
|
(if whole-par
|
|
|
|
|
(let ((paragraph-start (if use-hard-newlines "." paragraph-start))
|
2003-02-04 13:30:45 +00:00
|
|
|
|
(paragraph-ignore-fill-prefix (if use-hard-newlines t
|
1995-02-23 18:22:04 +00:00
|
|
|
|
paragraph-ignore-fill-prefix)))
|
|
|
|
|
(goto-char begin)
|
|
|
|
|
(while (and (bolp) (not (eobp))) (forward-char 1))
|
|
|
|
|
(backward-paragraph)
|
|
|
|
|
(setq begin (point))
|
|
|
|
|
(goto-char end)
|
|
|
|
|
(skip-chars-backward " \t\n" begin)
|
|
|
|
|
(forward-paragraph)
|
|
|
|
|
(setq end (point))))
|
|
|
|
|
|
|
|
|
|
(narrow-to-region (point-min) end)
|
|
|
|
|
(unjustify-region begin (point-max))
|
2001-12-07 03:34:59 +00:00
|
|
|
|
(put-text-property begin (point-max) 'justification style)
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(fill-region begin (point-max) nil t))))
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
|
|
|
|
|
(defun set-justification-none (b e)
|
|
|
|
|
"Disable automatic filling for paragraphs in the region.
|
|
|
|
|
If the mark is not active, this applies to the current paragraph."
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(interactive (list (if mark-active (region-beginning) (point))
|
|
|
|
|
(if mark-active (region-end) (point))))
|
|
|
|
|
(set-justification b e 'none t))
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
|
|
|
|
|
(defun set-justification-left (b e)
|
|
|
|
|
"Make paragraphs in the region left-justified.
|
2001-12-07 03:34:59 +00:00
|
|
|
|
This means they are flush at the left margin and ragged on the right.
|
1995-02-23 18:22:04 +00:00
|
|
|
|
This is usually the default, but see the variable `default-justification'.
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
If the mark is not active, this applies to the current paragraph."
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(interactive (list (if mark-active (region-beginning) (point))
|
|
|
|
|
(if mark-active (region-end) (point))))
|
|
|
|
|
(set-justification b e 'left t))
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
|
|
|
|
|
(defun set-justification-right (b e)
|
2001-12-03 06:11:37 +00:00
|
|
|
|
"Make paragraphs in the region right-justified.
|
2001-12-07 03:34:59 +00:00
|
|
|
|
This means they are flush at the right margin and ragged on the left.
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
If the mark is not active, this applies to the current paragraph."
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(interactive (list (if mark-active (region-beginning) (point))
|
|
|
|
|
(if mark-active (region-end) (point))))
|
|
|
|
|
(set-justification b e 'right t))
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
|
|
|
|
|
(defun set-justification-full (b e)
|
2001-12-03 06:11:37 +00:00
|
|
|
|
"Make paragraphs in the region fully justified.
|
1995-02-23 18:22:04 +00:00
|
|
|
|
This makes lines flush on both margins by inserting spaces between words.
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
If the mark is not active, this applies to the current paragraph."
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(interactive (list (if mark-active (region-beginning) (point))
|
|
|
|
|
(if mark-active (region-end) (point))))
|
|
|
|
|
(set-justification b e 'full t))
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
|
|
|
|
|
(defun set-justification-center (b e)
|
|
|
|
|
"Make paragraphs in the region centered.
|
|
|
|
|
If the mark is not active, this applies to the current paragraph."
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(interactive (list (if mark-active (region-beginning) (point))
|
|
|
|
|
(if mark-active (region-end) (point))))
|
|
|
|
|
(set-justification b e 'center t))
|
|
|
|
|
|
|
|
|
|
;; A line has up to six parts:
|
|
|
|
|
;;
|
2003-02-04 13:30:45 +00:00
|
|
|
|
;; >>> hello.
|
1995-02-23 18:22:04 +00:00
|
|
|
|
;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
|
|
|
|
|
;;
|
|
|
|
|
;; "Indent-1" is the left-margin indentation; normally it ends at column
|
|
|
|
|
;; given by the `current-left-margin' function.
|
|
|
|
|
;; "FP" is the fill-prefix. It can be any string, including whitespace.
|
|
|
|
|
;; "Indent-2" is added to justify a line if the `current-justification' is
|
|
|
|
|
;; `center' or `right'. In `left' and `full' justification regions, any
|
|
|
|
|
;; whitespace there is part of the line's text, and should not be changed.
|
|
|
|
|
;; Trailing whitespace is not counted as part of the line length when
|
|
|
|
|
;; center- or right-justifying.
|
|
|
|
|
;;
|
2003-02-04 13:30:45 +00:00
|
|
|
|
;; All parts of the line are optional, although the final newline can
|
1995-02-23 18:22:04 +00:00
|
|
|
|
;; only be missing on the last line of the buffer.
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
|
|
|
|
|
(defun justify-current-line (&optional how eop nosqueeze)
|
1995-02-23 18:22:04 +00:00
|
|
|
|
"Do some kind of justification on this line.
|
|
|
|
|
Normally does full justification: adds spaces to the line to make it end at
|
|
|
|
|
the column given by `current-fill-column'.
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
Optional first argument HOW specifies alternate type of justification:
|
2003-02-04 13:30:45 +00:00
|
|
|
|
it can be `left', `right', `full', `center', or `none'.
|
1995-02-23 18:22:04 +00:00
|
|
|
|
If HOW is t, will justify however the `current-justification' function says to.
|
|
|
|
|
If HOW is nil or missing, full justification is done by default.
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
Second arg EOP non-nil means that this is the last line of the paragraph, so
|
|
|
|
|
it will not be stretched by full justification.
|
|
|
|
|
Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
|
|
|
|
|
otherwise it is made canonical."
|
1999-07-20 16:02:27 +00:00
|
|
|
|
(interactive "*")
|
1995-11-10 17:13:56 +00:00
|
|
|
|
(if (eq t how) (setq how (or (current-justification) 'none))
|
|
|
|
|
(if (null how) (setq how 'full)
|
|
|
|
|
(or (memq how '(none left right center))
|
|
|
|
|
(setq how 'full))))
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(or (memq how '(none left)) ; No action required for these.
|
|
|
|
|
(let ((fc (current-fill-column))
|
|
|
|
|
(pos (point-marker))
|
|
|
|
|
fp-end ; point at end of fill prefix
|
|
|
|
|
beg ; point at beginning of line's text
|
|
|
|
|
end ; point at end of line's text
|
|
|
|
|
indent ; column of `beg'
|
|
|
|
|
endcol ; column of `end'
|
1998-04-23 21:55:15 +00:00
|
|
|
|
ncols ; new indent point or offset
|
|
|
|
|
(nspaces 0) ; number of spaces between words
|
|
|
|
|
; in line (not space characters)
|
|
|
|
|
fracspace ; fractional amount of space to be
|
|
|
|
|
; added between each words
|
|
|
|
|
(curr-fracspace 0) ; current fractional space amount
|
|
|
|
|
count)
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(end-of-line)
|
|
|
|
|
;; Check if this is the last line of the paragraph.
|
2003-02-04 13:30:45 +00:00
|
|
|
|
(if (and use-hard-newlines (null eop)
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(get-text-property (point) 'hard))
|
|
|
|
|
(setq eop t))
|
|
|
|
|
(skip-chars-backward " \t")
|
|
|
|
|
;; Quick exit if it appears to be properly justified already
|
|
|
|
|
;; or there is no text.
|
|
|
|
|
(if (or (bolp)
|
|
|
|
|
(and (memq how '(full right))
|
|
|
|
|
(= (current-column) fc)))
|
|
|
|
|
nil
|
|
|
|
|
(setq end (point))
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(skip-chars-forward " \t")
|
|
|
|
|
;; Skip over fill-prefix.
|
2003-02-04 13:30:45 +00:00
|
|
|
|
(if (and fill-prefix
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(not (string-equal fill-prefix ""))
|
|
|
|
|
(equal fill-prefix
|
2003-02-04 13:30:45 +00:00
|
|
|
|
(buffer-substring
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(point) (min (point-max) (+ (length fill-prefix)
|
|
|
|
|
(point))))))
|
|
|
|
|
(forward-char (length fill-prefix))
|
2003-02-04 13:30:45 +00:00
|
|
|
|
(if (and adaptive-fill-mode
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(looking-at adaptive-fill-regexp))
|
|
|
|
|
(goto-char (match-end 0))))
|
|
|
|
|
(setq fp-end (point))
|
|
|
|
|
(skip-chars-forward " \t")
|
|
|
|
|
;; This is beginning of the line's text.
|
|
|
|
|
(setq indent (current-column))
|
|
|
|
|
(setq beg (point))
|
|
|
|
|
(goto-char end)
|
|
|
|
|
(setq endcol (current-column))
|
|
|
|
|
|
|
|
|
|
;; HOW can't be null or left--we would have exited already
|
2003-02-04 13:30:45 +00:00
|
|
|
|
(cond ((eq 'right how)
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(setq ncols (- fc endcol))
|
|
|
|
|
(if (< ncols 0)
|
|
|
|
|
;; Need to remove some indentation
|
2003-02-04 13:30:45 +00:00
|
|
|
|
(delete-region
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(progn (goto-char fp-end)
|
|
|
|
|
(if (< (current-column) (+ indent ncols))
|
|
|
|
|
(move-to-column (+ indent ncols) t))
|
|
|
|
|
(point))
|
|
|
|
|
(progn (move-to-column indent) (point)))
|
|
|
|
|
;; Need to add some
|
|
|
|
|
(goto-char beg)
|
|
|
|
|
(indent-to (+ indent ncols))
|
|
|
|
|
;; If point was at beginning of text, keep it there.
|
2003-02-04 13:30:45 +00:00
|
|
|
|
(if (= beg pos)
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(move-marker pos (point)))))
|
|
|
|
|
|
|
|
|
|
((eq 'center how)
|
|
|
|
|
;; Figure out how much indentation is needed
|
|
|
|
|
(setq ncols (+ (current-left-margin)
|
|
|
|
|
(/ (- fc (current-left-margin) ;avail. space
|
|
|
|
|
(- endcol indent)) ;text width
|
|
|
|
|
2)))
|
|
|
|
|
(if (< ncols indent)
|
|
|
|
|
;; Have too much indentation - remove some
|
|
|
|
|
(delete-region
|
|
|
|
|
(progn (goto-char fp-end)
|
|
|
|
|
(if (< (current-column) ncols)
|
|
|
|
|
(move-to-column ncols t))
|
|
|
|
|
(point))
|
|
|
|
|
(progn (move-to-column indent) (point)))
|
|
|
|
|
;; Have too little - add some
|
|
|
|
|
(goto-char beg)
|
|
|
|
|
(indent-to ncols)
|
|
|
|
|
;; If point was at beginning of text, keep it there.
|
|
|
|
|
(if (= beg pos)
|
|
|
|
|
(move-marker pos (point)))))
|
|
|
|
|
|
|
|
|
|
((eq 'full how)
|
|
|
|
|
;; Insert extra spaces between words to justify line
|
|
|
|
|
(save-restriction
|
1995-01-25 00:05:59 +00:00
|
|
|
|
(narrow-to-region beg end)
|
|
|
|
|
(or nosqueeze
|
|
|
|
|
(canonically-space-region beg end))
|
|
|
|
|
(goto-char (point-max))
|
1998-04-23 21:55:15 +00:00
|
|
|
|
;; count word spaces in line
|
|
|
|
|
(while (search-backward " " nil t)
|
|
|
|
|
(setq nspaces (1+ nspaces))
|
|
|
|
|
(skip-chars-backward " "))
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(setq ncols (- fc endcol))
|
1998-04-23 21:55:15 +00:00
|
|
|
|
;; Ncols is number of additional space chars needed
|
|
|
|
|
(if (and (> ncols 0) (> nspaces 0) (not eop))
|
|
|
|
|
(progn
|
|
|
|
|
(setq curr-fracspace (+ ncols (/ (1+ nspaces) 2))
|
|
|
|
|
count nspaces)
|
|
|
|
|
(while (> count 0)
|
|
|
|
|
(skip-chars-forward " ")
|
|
|
|
|
(insert-and-inherit
|
|
|
|
|
(make-string (/ curr-fracspace nspaces) ?\ ))
|
|
|
|
|
(search-forward " " nil t)
|
|
|
|
|
(setq count (1- count)
|
|
|
|
|
curr-fracspace
|
|
|
|
|
(+ (% curr-fracspace nspaces) ncols)))))))
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(t (error "Unknown justification value"))))
|
|
|
|
|
(goto-char pos)
|
|
|
|
|
(move-marker pos nil)))
|
1993-03-11 07:05:23 +00:00
|
|
|
|
nil)
|
1994-09-23 20:46:35 +00:00
|
|
|
|
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(defun unjustify-current-line ()
|
|
|
|
|
"Remove justification whitespace from current line.
|
|
|
|
|
If the line is centered or right-justified, this function removes any
|
1996-01-04 23:35:23 +00:00
|
|
|
|
indentation past the left margin. If the line is full-justified, it removes
|
1995-02-23 18:22:04 +00:00
|
|
|
|
extra spaces between words. It does nothing in other justification modes."
|
|
|
|
|
(let ((justify (current-justification)))
|
|
|
|
|
(cond ((eq 'left justify) nil)
|
|
|
|
|
((eq nil justify) nil)
|
|
|
|
|
((eq 'full justify) ; full justify: remove extra spaces
|
|
|
|
|
(beginning-of-line-text)
|
2000-10-06 19:50:40 +00:00
|
|
|
|
(canonically-space-region (point) (line-end-position)))
|
1995-02-23 18:22:04 +00:00
|
|
|
|
((memq justify '(center right))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(move-to-left-margin nil t)
|
|
|
|
|
;; Position ourselves after any fill-prefix.
|
2003-02-04 13:30:45 +00:00
|
|
|
|
(if (and fill-prefix
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(not (string-equal fill-prefix ""))
|
|
|
|
|
(equal fill-prefix
|
2003-02-04 13:30:45 +00:00
|
|
|
|
(buffer-substring
|
1995-02-23 18:22:04 +00:00
|
|
|
|
(point) (min (point-max) (+ (length fill-prefix)
|
|
|
|
|
(point))))))
|
|
|
|
|
(forward-char (length fill-prefix)))
|
|
|
|
|
(delete-region (point) (progn (skip-chars-forward " \t")
|
|
|
|
|
(point))))))))
|
|
|
|
|
|
|
|
|
|
(defun unjustify-region (&optional begin end)
|
|
|
|
|
"Remove justification whitespace from region.
|
|
|
|
|
For centered or right-justified regions, this function removes any indentation
|
2003-02-04 13:30:45 +00:00
|
|
|
|
past the left margin from each line. For full-justified lines, it removes
|
1995-02-23 18:22:04 +00:00
|
|
|
|
extra spaces between words. It does nothing in other justification modes.
|
|
|
|
|
Arguments BEGIN and END are optional; default is the whole buffer."
|
|
|
|
|
(save-excursion
|
|
|
|
|
(save-restriction
|
|
|
|
|
(if end (narrow-to-region (point-min) end))
|
|
|
|
|
(goto-char (or begin (point-min)))
|
|
|
|
|
(while (not (eobp))
|
|
|
|
|
(unjustify-current-line)
|
|
|
|
|
(forward-line 1)))))
|
|
|
|
|
|
1990-07-27 04:20:16 +00:00
|
|
|
|
|
1998-07-13 02:11:16 +00:00
|
|
|
|
(defun fill-nonuniform-paragraphs (min max &optional justifyp citation-regexp)
|
1993-04-11 04:02:23 +00:00
|
|
|
|
"Fill paragraphs within the region, allowing varying indentation within each.
|
|
|
|
|
This command divides the region into \"paragraphs\",
|
|
|
|
|
only at paragraph-separator lines, then fills each paragraph
|
|
|
|
|
using as the fill prefix the smallest indentation of any line
|
|
|
|
|
in the paragraph.
|
|
|
|
|
|
|
|
|
|
When calling from a program, pass range to fill as first two arguments.
|
1992-05-10 18:15:10 +00:00
|
|
|
|
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
|
|
|
|
|
JUSTIFY to justify paragraphs (prefix arg),
|
1998-07-13 02:11:16 +00:00
|
|
|
|
When filling a mail message, pass a regexp for CITATION-REGEXP
|
|
|
|
|
which will match the prefix of a line which is a citation marker
|
|
|
|
|
plus whitespace, but no other kind of prefix.
|
2002-09-06 07:18:44 +00:00
|
|
|
|
Also, if CITATION-REGEXP is non-nil, don't fill header lines."
|
1999-07-20 16:02:27 +00:00
|
|
|
|
(interactive (progn
|
|
|
|
|
(barf-if-buffer-read-only)
|
|
|
|
|
(list (region-beginning) (region-end)
|
|
|
|
|
(if current-prefix-arg 'full))))
|
1993-04-11 04:02:23 +00:00
|
|
|
|
(let ((fill-individual-varying-indent t))
|
1998-07-13 02:11:16 +00:00
|
|
|
|
(fill-individual-paragraphs min max justifyp citation-regexp)))
|
1993-04-11 04:02:23 +00:00
|
|
|
|
|
1998-07-13 02:11:16 +00:00
|
|
|
|
(defun fill-individual-paragraphs (min max &optional justify citation-regexp)
|
1993-04-11 04:02:23 +00:00
|
|
|
|
"Fill paragraphs of uniform indentation within the region.
|
2003-02-04 13:30:45 +00:00
|
|
|
|
This command divides the region into \"paragraphs\",
|
1997-09-14 20:27:34 +00:00
|
|
|
|
treating every change in indentation level or prefix as a paragraph boundary,
|
1993-04-11 04:02:23 +00:00
|
|
|
|
then fills each paragraph using its indentation level as the fill prefix.
|
1992-05-10 18:15:10 +00:00
|
|
|
|
|
1997-09-14 20:27:34 +00:00
|
|
|
|
There is one special case where a change in indentation does not start
|
|
|
|
|
a new paragraph. This is for text of this form:
|
|
|
|
|
|
|
|
|
|
foo> This line with extra indentation starts
|
|
|
|
|
foo> a paragraph that continues on more lines.
|
|
|
|
|
|
|
|
|
|
These lines are filled together.
|
|
|
|
|
|
|
|
|
|
When calling from a program, pass the range to fill
|
|
|
|
|
as the first two arguments.
|
1992-05-10 18:15:10 +00:00
|
|
|
|
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
|
|
|
|
|
JUSTIFY to justify paragraphs (prefix arg),
|
1998-07-13 02:11:16 +00:00
|
|
|
|
When filling a mail message, pass a regexp for CITATION-REGEXP
|
|
|
|
|
which will match the prefix of a line which is a citation marker
|
|
|
|
|
plus whitespace, but no other kind of prefix.
|
2002-09-06 07:18:44 +00:00
|
|
|
|
Also, if CITATION-REGEXP is non-nil, don't fill header lines."
|
1999-07-20 16:02:27 +00:00
|
|
|
|
(interactive (progn
|
|
|
|
|
(barf-if-buffer-read-only)
|
|
|
|
|
(list (region-beginning) (region-end)
|
|
|
|
|
(if current-prefix-arg 'full))))
|
1991-12-21 09:14:03 +00:00
|
|
|
|
(save-restriction
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char min)
|
|
|
|
|
(beginning-of-line)
|
1994-10-26 09:32:04 +00:00
|
|
|
|
(narrow-to-region (point) max)
|
1998-07-13 02:11:16 +00:00
|
|
|
|
(if citation-regexp
|
1994-10-26 09:32:04 +00:00
|
|
|
|
(while (and (not (eobp))
|
1995-11-10 17:13:56 +00:00
|
|
|
|
(or (looking-at "[ \t]*[^ \t\n]+:")
|
1994-10-26 09:32:04 +00:00
|
|
|
|
(looking-at "[ \t]*$")))
|
1995-11-10 17:13:56 +00:00
|
|
|
|
(if (looking-at "[ \t]*[^ \t\n]+:")
|
1993-11-12 23:35:39 +00:00
|
|
|
|
(search-forward "\n\n" nil 'move)
|
|
|
|
|
(forward-line 1))))
|
1991-12-21 09:14:03 +00:00
|
|
|
|
(narrow-to-region (point) max)
|
|
|
|
|
;; Loop over paragraphs.
|
1997-11-15 20:58:28 +00:00
|
|
|
|
(while (let ((here (point)))
|
1997-09-08 06:47:05 +00:00
|
|
|
|
;; Skip over all paragraph-separating lines
|
|
|
|
|
;; so as to not include them in any paragraph.
|
1997-11-15 20:58:28 +00:00
|
|
|
|
(while (and (not (eobp))
|
|
|
|
|
(progn (move-to-left-margin)
|
|
|
|
|
(and (not (eobp))
|
|
|
|
|
(looking-at paragraph-separate))))
|
1997-09-08 06:47:05 +00:00
|
|
|
|
(forward-line 1))
|
|
|
|
|
(skip-chars-forward " \t\n") (not (eobp)))
|
1995-11-11 05:30:49 +00:00
|
|
|
|
(move-to-left-margin)
|
1991-12-21 09:14:03 +00:00
|
|
|
|
(let ((start (point))
|
|
|
|
|
fill-prefix fill-prefix-regexp)
|
|
|
|
|
;; Find end of paragraph, and compute the smallest fill-prefix
|
|
|
|
|
;; that fits all the lines in this paragraph.
|
|
|
|
|
(while (progn
|
|
|
|
|
;; Update the fill-prefix on the first line
|
|
|
|
|
;; and whenever the prefix good so far is too long.
|
|
|
|
|
(if (not (and fill-prefix
|
|
|
|
|
(looking-at fill-prefix-regexp)))
|
|
|
|
|
(setq fill-prefix
|
2000-10-06 19:50:40 +00:00
|
|
|
|
(fill-individual-paragraphs-prefix
|
|
|
|
|
citation-regexp)
|
1995-11-11 05:30:49 +00:00
|
|
|
|
fill-prefix-regexp (regexp-quote fill-prefix)))
|
1995-11-14 23:28:24 +00:00
|
|
|
|
(forward-line 1)
|
1996-05-31 04:17:02 +00:00
|
|
|
|
(if (bolp)
|
|
|
|
|
;; If forward-line went past a newline,
|
|
|
|
|
;; move further to the left margin.
|
|
|
|
|
(move-to-left-margin))
|
1991-12-21 09:14:03 +00:00
|
|
|
|
;; Now stop the loop if end of paragraph.
|
|
|
|
|
(and (not (eobp))
|
1992-05-10 18:15:10 +00:00
|
|
|
|
(if fill-individual-varying-indent
|
|
|
|
|
;; If this line is a separator line, with or
|
|
|
|
|
;; without prefix, end the paragraph.
|
2003-02-04 13:30:45 +00:00
|
|
|
|
(and
|
1995-11-14 23:28:24 +00:00
|
|
|
|
(not (looking-at paragraph-separate))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(not (and (looking-at fill-prefix-regexp)
|
2000-10-06 19:50:40 +00:00
|
|
|
|
(progn (forward-char
|
|
|
|
|
(length fill-prefix))
|
|
|
|
|
(looking-at
|
|
|
|
|
paragraph-separate))))))
|
1992-05-10 18:15:10 +00:00
|
|
|
|
;; If this line has more or less indent
|
|
|
|
|
;; than the fill prefix wants, end the paragraph.
|
|
|
|
|
(and (looking-at fill-prefix-regexp)
|
1999-07-30 14:34:28 +00:00
|
|
|
|
;; If fill prefix is shorter than a new
|
|
|
|
|
;; fill prefix computed here, end paragraph.
|
|
|
|
|
(let ((this-line-fill-prefix
|
2003-02-04 13:30:45 +00:00
|
|
|
|
(fill-individual-paragraphs-prefix
|
1999-07-30 14:34:28 +00:00
|
|
|
|
citation-regexp)))
|
2003-02-04 13:30:45 +00:00
|
|
|
|
(>= (length fill-prefix)
|
1999-07-30 14:34:28 +00:00
|
|
|
|
(length this-line-fill-prefix)))
|
1992-05-10 18:15:10 +00:00
|
|
|
|
(save-excursion
|
2000-10-06 19:50:40 +00:00
|
|
|
|
(not (progn (forward-char
|
|
|
|
|
(length fill-prefix))
|
1997-09-14 20:27:34 +00:00
|
|
|
|
(or (looking-at "[ \t]")
|
|
|
|
|
(looking-at paragraph-separate)
|
2000-04-03 19:28:16 +00:00
|
|
|
|
(looking-at paragraph-start)))))
|
|
|
|
|
(not (and (equal fill-prefix "")
|
|
|
|
|
citation-regexp
|
|
|
|
|
(looking-at citation-regexp))))))))
|
1991-12-21 09:14:03 +00:00
|
|
|
|
;; Fill this paragraph, but don't add a newline at the end.
|
|
|
|
|
(let ((had-newline (bolp)))
|
(set-justification): New function.
(set-justification-{none,left,right,full,center}): New functions.
(fill-region-as-paragraph, fill-region, justify-current-line): New
arg NOSQUEEZE defeats normal removal of extra whitespace.
(fill-region-as-paragraph, fill-region)
(fill-nonuniform-paragraphs, fill-individual-paragraphs):
Arg JUSTIFY-FLAG (JUSTIFYP) renamed to JUSTIFY.
(fill-region-as-paragraph): Obey left-margin; fill-prefix starts
after left-margin. Disable filling if JUSTIFY == none, but indent to
margin anyway.
Adaptive-fill removes text-props from fill-prefixes it finds.
Adaptive-fill no longer has to notice left-margin: std fill does that.
Use fill-column and canonically-space-region functions.
(canonically-space-region): New fn split from fill-region-as-paragraph.
(fill-region): New args NOSQUEEZE (as above) and TO-EOP.
(default-justification): New variable.
(current-left-margin, fill-column, justification): New functions.
(fill-paragraph): Use fill-region-as-paragraph when possible.
(justify-current-line): New arguments; different kinds of
justification handled. Uses left-margin and fill-column functions.
1995-01-19 04:20:52 +00:00
|
|
|
|
(fill-region-as-paragraph start (point) justify)
|
1998-09-04 20:37:22 +00:00
|
|
|
|
(if (and (bolp) (not had-newline))
|
|
|
|
|
(delete-char -1))))))))
|
1998-07-13 02:11:16 +00:00
|
|
|
|
(defun fill-individual-paragraphs-prefix (citation-regexp)
|
2001-10-30 08:08:12 +00:00
|
|
|
|
(let* ((adaptive-fill-first-line-regexp ".*")
|
|
|
|
|
(just-one-line-prefix
|
|
|
|
|
;; Accept any prefix rather than just the ones matched by
|
|
|
|
|
;; adaptive-fill-first-line-regexp.
|
|
|
|
|
(fill-context-prefix (point) (line-beginning-position 2)))
|
|
|
|
|
(two-lines-prefix
|
|
|
|
|
(fill-context-prefix (point) (line-beginning-position 3))))
|
|
|
|
|
(if (not just-one-line-prefix)
|
|
|
|
|
(buffer-substring
|
|
|
|
|
(point) (save-excursion (skip-chars-forward " \t") (point)))
|
1998-07-13 02:11:16 +00:00
|
|
|
|
;; See if the citation part of JUST-ONE-LINE-PREFIX
|
|
|
|
|
;; is the same as that of TWO-LINES-PREFIX,
|
|
|
|
|
;; except perhaps with longer whitespace.
|
2001-10-30 08:08:12 +00:00
|
|
|
|
(if (and just-one-line-prefix two-lines-prefix
|
|
|
|
|
(let* ((one-line-citation-part
|
|
|
|
|
(fill-individual-paragraphs-citation
|
|
|
|
|
just-one-line-prefix citation-regexp))
|
|
|
|
|
(two-lines-citation-part
|
|
|
|
|
(fill-individual-paragraphs-citation
|
|
|
|
|
two-lines-prefix citation-regexp))
|
|
|
|
|
(adjusted-two-lines-citation-part
|
|
|
|
|
(substring two-lines-citation-part 0
|
|
|
|
|
(string-match "[ \t]*\\'"
|
|
|
|
|
two-lines-citation-part))))
|
|
|
|
|
(and
|
1998-07-13 02:11:16 +00:00
|
|
|
|
(string-match (concat "\\`"
|
2000-10-06 19:50:40 +00:00
|
|
|
|
(regexp-quote
|
|
|
|
|
adjusted-two-lines-citation-part)
|
1998-07-13 02:11:16 +00:00
|
|
|
|
"[ \t]*\\'")
|
|
|
|
|
one-line-citation-part)
|
|
|
|
|
(>= (string-width one-line-citation-part)
|
2001-10-30 08:08:12 +00:00
|
|
|
|
(string-width two-lines-citation-part)))))
|
1998-07-13 02:11:16 +00:00
|
|
|
|
two-lines-prefix
|
2001-10-30 08:08:12 +00:00
|
|
|
|
just-one-line-prefix))))
|
1998-07-13 02:11:16 +00:00
|
|
|
|
|
|
|
|
|
(defun fill-individual-paragraphs-citation (string citation-regexp)
|
2001-10-30 08:08:12 +00:00
|
|
|
|
(if citation-regexp
|
|
|
|
|
(if (string-match citation-regexp string)
|
|
|
|
|
(match-string 0 string)
|
|
|
|
|
"")
|
|
|
|
|
string))
|
1998-07-13 02:11:16 +00:00
|
|
|
|
|
1992-06-30 13:54:21 +00:00
|
|
|
|
;;; fill.el ends here
|