48 |
"Mode-specific function to fill a paragraph, or nil if there is none. |
"Mode-specific function to fill a paragraph, or nil if there is none. |
49 |
If the function returns nil, then `fill-paragraph' does its normal work.") |
If the function returns nil, then `fill-paragraph' does its normal work.") |
50 |
|
|
51 |
|
(defvar fill-paragraph-handle-comment t |
52 |
|
"If non-nil, paragraph filling will try to pay attention to comments.") |
53 |
|
|
54 |
(defvar enable-kinsoku t |
(defvar enable-kinsoku t |
55 |
"*Non-nil means enable \"kinsoku\" processing on filling paragraph. |
"*Non-nil means enable \"kinsoku\" processing on filling paragraph. |
56 |
Kinsoku processing is designed to prevent certain characters from being |
Kinsoku processing is designed to prevent certain characters from being |
197 |
(setq cmp (1- (abs cmp))) |
(setq cmp (1- (abs cmp))) |
198 |
(unless (zerop cmp) |
(unless (zerop cmp) |
199 |
(substring s1 0 cmp))))) |
(substring s1 0 cmp))))) |
200 |
|
|
201 |
(defun fill-context-prefix (from to &optional first-line-regexp) |
(defun fill-context-prefix (from to &optional first-line-regexp) |
202 |
"Compute a fill prefix from the text between FROM and TO. |
"Compute a fill prefix from the text between FROM and TO. |
203 |
This uses the variables `adaptive-fill-regexp' and `adaptive-fill-function' |
This uses the variables `adaptive-fill-regexp' and `adaptive-fill-function' |
316 |
:type 'hook |
:type 'hook |
317 |
:options '(fill-french-nobreak-p fill-single-word-nobreak-p)) |
:options '(fill-french-nobreak-p fill-single-word-nobreak-p)) |
318 |
|
|
319 |
|
(defcustom fill-nobreak-invisible nil |
320 |
|
"Non-nil means that fill command do not break lines in invisible text." |
321 |
|
:type 'boolean |
322 |
|
:group 'fill) |
323 |
|
|
324 |
(defun fill-nobreak-p () |
(defun fill-nobreak-p () |
325 |
"Return nil if breaking the line at point is allowed. |
"Return nil if breaking the line at point is allowed. |
326 |
Can be customized with the variable `fill-nobreak-predicate'." |
Can be customized with the variables `fill-nobreak-predicate' |
327 |
(unless (bolp) |
and `fill-nobreak-invisible'." |
328 |
|
(or |
329 |
|
(and fill-nobreak-invisible (line-move-invisible (point))) |
330 |
|
(unless (bolp) |
331 |
(or |
(or |
332 |
;; Don't break after a period followed by just one space. |
;; Don't break after a period followed by just one space. |
333 |
;; Move back to the previous place to break. |
;; Move back to the previous place to break. |
348 |
(unless use-hard-newlines |
(unless use-hard-newlines |
349 |
(save-excursion |
(save-excursion |
350 |
(skip-chars-forward " \t") (looking-at paragraph-start))) |
(skip-chars-forward " \t") (looking-at paragraph-start))) |
351 |
(run-hook-with-args-until-success 'fill-nobreak-predicate)))) |
(run-hook-with-args-until-success 'fill-nobreak-predicate))))) |
352 |
|
|
353 |
;; Put `fill-find-break-point-function' property to charsets which |
;; Put `fill-find-break-point-function' property to charsets which |
354 |
;; require special functions to find line breaking point. |
;; require special functions to find line breaking point. |
450 |
(subst-char-in-region from to ?\n ?\ ) |
(subst-char-in-region from to ?\n ?\ ) |
451 |
(if (and nosqueeze (not (eq justify 'full))) |
(if (and nosqueeze (not (eq justify 'full))) |
452 |
nil |
nil |
453 |
(canonically-space-region (or squeeze-after (point)) to) |
(canonically-space-region (or squeeze-after (point)) to)) |
|
(goto-char to) |
|
|
(delete-horizontal-space) |
|
|
(insert-and-inherit " ")) |
|
454 |
(goto-char from)) |
(goto-char from)) |
455 |
|
|
456 |
(defun fill-move-to-break-point (linebeg) |
(defun fill-move-to-break-point (linebeg) |
473 |
;; point is at the place where the break occurs. |
;; point is at the place where the break occurs. |
474 |
(forward-char 1) |
(forward-char 1) |
475 |
(when (fill-nobreak-p) (skip-chars-backward " \t" linebeg)))) |
(when (fill-nobreak-p) (skip-chars-backward " \t" linebeg)))) |
476 |
|
|
477 |
|
;; Move back over the single space between the words. |
478 |
|
(skip-chars-backward " \t") |
479 |
|
|
480 |
;; If the left margin and fill prefix by themselves |
;; If the left margin and fill prefix by themselves |
481 |
;; pass the fill-column. or if they are zero |
;; pass the fill-column. or if they are zero |
482 |
;; but we have no room for even one word, |
;; but we have no room for even one word, |
502 |
(forward-char -1) |
(forward-char -1) |
503 |
(goto-char pos)))) |
(goto-char pos)))) |
504 |
(setq first nil))) |
(setq first nil))) |
|
;; Normally, move back over the single space between |
|
|
;; the words. |
|
|
(skip-chars-backward " \t") |
|
505 |
|
|
506 |
(if enable-multibyte-characters |
(if enable-multibyte-characters |
507 |
;; If we are going to break the line after or |
;; If we are going to break the line after or |
523 |
;; We need one space at end of line so that |
;; We need one space at end of line so that |
524 |
;; further filling won't delete it. NOTE: We |
;; further filling won't delete it. NOTE: We |
525 |
;; intentionally leave this one space to |
;; intentionally leave this one space to |
526 |
;; distingush the case that user wants to put |
;; distinguish the case that user wants to put |
527 |
;; space between \c| characters. |
;; space between \c| characters. |
528 |
(forward-char 1)) |
(forward-char 1)) |
529 |
(insert ?\n) |
(insert ?\n) |
530 |
;; Give newline the properties of the space(s) it replaces |
;; Give newline the properties of the space(s) it replaces |
531 |
(set-text-properties (1- (point)) (point) |
(set-text-properties (1- (point)) (point) |
532 |
(text-properties-at (point))) |
(text-properties-at (point))) |
533 |
|
;; If we don't want breaks in invisible text, don't insert |
534 |
|
;; an invisible newline. |
535 |
|
(if fill-nobreak-invisible |
536 |
|
(remove-text-properties (1- (point)) (point) |
537 |
|
'(invisible t))) |
538 |
(if (or fill-prefix |
(if (or fill-prefix |
539 |
(not fill-indent-according-to-mode)) |
(not fill-indent-according-to-mode)) |
540 |
(indent-to-left-margin) |
(fill-indent-to-left-margin) |
541 |
(indent-according-to-mode)) |
(indent-according-to-mode)) |
542 |
;; Insert the fill prefix after indentation. |
;; Insert the fill prefix after indentation. |
|
;; Set prefixcol so whitespace in the prefix won't get lost. |
|
543 |
(and fill-prefix (not (equal fill-prefix "")) |
(and fill-prefix (not (equal fill-prefix "")) |
544 |
(insert-and-inherit fill-prefix))) |
;; Markers that were after the whitespace are now at point: insert |
545 |
|
;; before them so they don't get stuck before the prefix. |
546 |
|
(insert-before-markers-and-inherit fill-prefix))) |
547 |
|
|
548 |
|
(defun fill-indent-to-left-margin () |
549 |
|
"Indent current line to the column given by `current-left-margin'." |
550 |
|
(let ((beg (point))) |
551 |
|
(indent-line-to (current-left-margin)) |
552 |
|
(put-text-property beg (point) 'face 'default))) |
553 |
|
|
554 |
(defun fill-region-as-paragraph (from to &optional justify |
(defun fill-region-as-paragraph (from to &optional justify |
555 |
nosqueeze squeeze-after) |
nosqueeze squeeze-after) |
568 |
between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil, |
between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil, |
569 |
means don't canonicalize spaces before that position. |
means don't canonicalize spaces before that position. |
570 |
|
|
571 |
Return the fill-prefix used for filling. |
Return the `fill-prefix' used for filling. |
572 |
|
|
573 |
If `sentence-end-double-space' is non-nil, then period followed by one |
If `sentence-end-double-space' is non-nil, then period followed by one |
574 |
space does not end a sentence, so don't break a line there." |
space does not end a sentence, so don't break a line there." |
578 |
(if current-prefix-arg 'full)))) |
(if current-prefix-arg 'full)))) |
579 |
(unless (memq justify '(t nil none full center left right)) |
(unless (memq justify '(t nil none full center left right)) |
580 |
(setq justify 'full)) |
(setq justify 'full)) |
|
;; Arrange for undoing the fill to restore point. |
|
|
(if (and buffer-undo-list (not (eq buffer-undo-list t))) |
|
|
(setq buffer-undo-list (cons (point) buffer-undo-list))) |
|
581 |
|
|
582 |
;; Make sure "to" is the endpoint. |
;; Make sure "to" is the endpoint. |
583 |
(goto-char (min from to)) |
(goto-char (min from to)) |
590 |
|
|
591 |
(beginning-of-line) |
(beginning-of-line) |
592 |
(setq from (point)) |
(setq from (point)) |
593 |
|
|
594 |
;; Delete all but one soft newline at end of region. |
;; Delete all but one soft newline at end of region. |
595 |
;; And leave TO before that one. |
;; And leave TO before that one. |
596 |
(goto-char to) |
(goto-char to) |
625 |
(string-match "\\`[ \t]*\\'" fill-prefix)) |
(string-match "\\`[ \t]*\\'" fill-prefix)) |
626 |
(setq fill-prefix nil))) |
(setq fill-prefix nil))) |
627 |
|
|
628 |
(save-restriction |
(goto-char from) |
629 |
(goto-char from) |
(beginning-of-line) |
|
(beginning-of-line) |
|
|
(narrow-to-region (point) to) |
|
630 |
|
|
631 |
(if (not justify) ; filling disabled: just check indentation |
(if (not justify) ; filling disabled: just check indentation |
632 |
(progn |
(progn |
633 |
(goto-char from) |
(goto-char from) |
|
(while (< (point) to) |
|
|
(if (and (not (eolp)) |
|
|
(< (current-indentation) (current-left-margin))) |
|
|
(indent-to-left-margin)) |
|
|
(forward-line 1))) |
|
|
|
|
|
(if use-hard-newlines |
|
|
(remove-text-properties from (point-max) '(hard nil))) |
|
|
;; Make sure first line is indented (at least) to left margin... |
|
|
(if (or (memq justify '(right center)) |
|
|
(< (current-indentation) (current-left-margin))) |
|
|
(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) |
|
634 |
(while (< (point) to) |
(while (< (point) to) |
635 |
(setq linebeg (point)) |
(if (and (not (eolp)) |
636 |
(move-to-column (1+ (current-fill-column))) |
(< (current-indentation) (current-left-margin))) |
637 |
(if (>= (point) to) |
(fill-indent-to-left-margin)) |
638 |
(or nosqueeze (delete-horizontal-space)) |
(forward-line 1))) |
639 |
;; Find the position where we'll break the line. |
|
640 |
(fill-move-to-break-point linebeg) |
(if use-hard-newlines |
641 |
|
(remove-list-of-text-properties from to '(hard))) |
642 |
;; Check again to see if we got to the end of the paragraph. |
;; Make sure first line is indented (at least) to left margin... |
643 |
(if (save-excursion (skip-chars-forward " \t") (>= (point) to)) |
(if (or (memq justify '(right center)) |
644 |
(or nosqueeze (delete-horizontal-space)) |
(< (current-indentation) (current-left-margin))) |
645 |
(fill-newline))) |
(fill-indent-to-left-margin)) |
646 |
;; Justify the line just ended, if desired. |
;; Delete the fill-prefix from every line. |
647 |
(if justify |
(fill-delete-prefix from to fill-prefix) |
648 |
(if (save-excursion (skip-chars-forward " \t") (>= (point) to)) |
(setq from (point)) |
649 |
(progn |
|
650 |
(delete-horizontal-space) |
;; FROM, and point, are now before the text to fill, |
651 |
(justify-current-line justify t t)) |
;; but after any fill prefix on the first line. |
652 |
|
|
653 |
|
(fill-delete-newlines from to justify nosqueeze squeeze-after) |
654 |
|
|
655 |
|
;; This is the actual filling loop. |
656 |
|
(goto-char from) |
657 |
|
(let (linebeg) |
658 |
|
(while (< (point) to) |
659 |
|
(setq linebeg (point)) |
660 |
|
(move-to-column (1+ (current-fill-column))) |
661 |
|
(if (when (< (point) to) |
662 |
|
;; Find the position where we'll break the line. |
663 |
|
(fill-move-to-break-point linebeg) |
664 |
|
;; Check again to see if we got to the end of |
665 |
|
;; the paragraph. |
666 |
|
(skip-chars-forward " \t") |
667 |
|
(< (point) to)) |
668 |
|
;; Found a place to cut. |
669 |
|
(progn |
670 |
|
(fill-newline) |
671 |
|
(when justify |
672 |
|
;; Justify the line just ended, if desired. |
673 |
(save-excursion |
(save-excursion |
674 |
(forward-line -1) |
(forward-line -1) |
675 |
(justify-current-line justify nil t))))))) |
(justify-current-line justify nil t)))) |
676 |
;; Leave point after final newline. |
|
677 |
(goto-char to)) |
(goto-char to) |
678 |
(unless (eobp) |
;; Justify this last line, if desired. |
679 |
(forward-char 1)) |
(if justify (justify-current-line justify t t)))))) |
680 |
|
;; Leave point after final newline. |
681 |
|
(goto-char to) |
682 |
|
(unless (eobp) (forward-char 1)) |
683 |
;; Return the fill-prefix we used |
;; Return the fill-prefix we used |
684 |
fill-prefix))) |
fill-prefix))) |
685 |
|
|
705 |
(interactive (progn |
(interactive (progn |
706 |
(barf-if-buffer-read-only) |
(barf-if-buffer-read-only) |
707 |
(list (if current-prefix-arg 'full)))) |
(list (if current-prefix-arg 'full)))) |
708 |
|
;; First try fill-paragraph-function. |
709 |
(or (and fill-paragraph-function |
(or (and fill-paragraph-function |
710 |
(let ((function fill-paragraph-function) |
(let ((function fill-paragraph-function) |
711 |
|
;; If fill-paragraph-function is set, it probably takes care |
712 |
|
;; of comments and stuff. If not, it will have to set |
713 |
|
;; fill-paragraph-handle-comment back to t explicitly or |
714 |
|
;; return nil. |
715 |
|
(fill-paragraph-handle-comment nil) |
716 |
fill-paragraph-function) |
fill-paragraph-function) |
717 |
(funcall function arg))) |
(funcall function arg))) |
718 |
|
;; Then try our syntax-aware filling code. |
719 |
|
(and fill-paragraph-handle-comment |
720 |
|
;; Our code only handles \n-terminated comments right now. |
721 |
|
comment-start comment-start-skip (equal comment-end "") |
722 |
|
(let ((fill-paragraph-handle-comment nil)) |
723 |
|
(fill-comment-paragraph arg))) |
724 |
|
;; If it all fails, default to the good ol' text paragraph filling. |
725 |
(let ((before (point)) |
(let ((before (point)) |
726 |
|
(paragraph-start paragraph-start) |
727 |
;; Fill prefix used for filling the paragraph. |
;; Fill prefix used for filling the paragraph. |
728 |
fill-pfx) |
fill-pfx) |
729 |
|
;; Try to prevent code sections and comment sections from being |
730 |
|
;; filled together. |
731 |
|
(when (and fill-paragraph-handle-comment comment-start-skip) |
732 |
|
(setq paragraph-start |
733 |
|
(concat paragraph-start "\\|[ \t]*\\(?:" |
734 |
|
comment-start-skip "\\)"))) |
735 |
(save-excursion |
(save-excursion |
736 |
(forward-paragraph) |
;; To make sure the return value of forward-paragraph is meaningful, |
737 |
(or (bolp) (newline 1)) |
;; we have to start from the beginning of line, otherwise skipping |
738 |
(let ((end (point)) |
;; past the last few chars of a paragraph-separator would count as |
739 |
(beg (progn (backward-paragraph) (point)))) |
;; a paragraph (and not skipping any chars at EOB would not count |
740 |
(goto-char before) |
;; as a paragraph even if it is). |
741 |
(setq fill-pfx |
(move-to-left-margin) |
742 |
(if use-hard-newlines |
(if (not (zerop (forward-paragraph))) |
743 |
;; Can't use fill-region-as-paragraph, since this |
;; There's no paragraph at or after point: give up. |
744 |
;; paragraph may still contain hard newlines. See |
(setq fill-pfx "") |
745 |
;; fill-region. |
(let ((end (point)) |
746 |
(fill-region beg end arg) |
(beg (progn (backward-paragraph) (point)))) |
747 |
(fill-region-as-paragraph beg end arg))))) |
(goto-char before) |
748 |
|
(setq fill-pfx |
749 |
|
(if use-hard-newlines |
750 |
|
;; Can't use fill-region-as-paragraph, since this |
751 |
|
;; paragraph may still contain hard newlines. See |
752 |
|
;; fill-region. |
753 |
|
(fill-region beg end arg) |
754 |
|
(fill-region-as-paragraph beg end arg)))))) |
755 |
;; See if point ended up inside the fill-prefix, and if so, move |
;; See if point ended up inside the fill-prefix, and if so, move |
756 |
;; past it. |
;; past it. |
757 |
(skip-line-prefix fill-pfx) |
(skip-line-prefix fill-pfx) |
758 |
fill-pfx))) |
fill-pfx))) |
759 |
|
|
760 |
|
(defun fill-comment-paragraph (&optional justify) |
761 |
|
"Fill current comment. |
762 |
|
If we're not in a comment, just return nil so that the caller |
763 |
|
can take care of filling. JUSTIFY is used as in `fill-paragraph'." |
764 |
|
(comment-normalize-vars) |
765 |
|
(let (has-code-and-comment ; Non-nil if it contains code and a comment. |
766 |
|
comin comstart) |
767 |
|
;; Figure out what kind of comment we are looking at. |
768 |
|
(save-excursion |
769 |
|
(beginning-of-line) |
770 |
|
(when (setq comstart (comment-search-forward (line-end-position) t)) |
771 |
|
(setq comin (point)) |
772 |
|
(goto-char comstart) (skip-chars-backward " \t") |
773 |
|
(setq has-code-and-comment (not (bolp))))) |
774 |
|
|
775 |
|
(if (not comstart) |
776 |
|
;; Return nil, so the normal filling will take place. |
777 |
|
nil |
778 |
|
|
779 |
|
;; Narrow to include only the comment, and then fill the region. |
780 |
|
(let* ((fill-prefix fill-prefix) |
781 |
|
(comment-re (concat "[ \t]*\\(?:" comment-start-skip "\\)")) |
782 |
|
(comment-fill-prefix ; Compute a fill prefix. |
783 |
|
(save-excursion |
784 |
|
(goto-char comstart) |
785 |
|
(if has-code-and-comment |
786 |
|
(concat (make-string (/ (current-column) tab-width) ?\t) |
787 |
|
(make-string (% (current-column) tab-width) ?\ ) |
788 |
|
(buffer-substring (point) comin)) |
789 |
|
(buffer-substring (line-beginning-position) comin)))) |
790 |
|
beg end) |
791 |
|
(save-excursion |
792 |
|
(save-restriction |
793 |
|
(beginning-of-line) |
794 |
|
(narrow-to-region |
795 |
|
;; Find the first line we should include in the region to fill. |
796 |
|
(if has-code-and-comment |
797 |
|
(line-beginning-position) |
798 |
|
(save-excursion |
799 |
|
(while (and (zerop (forward-line -1)) |
800 |
|
(looking-at comment-re))) |
801 |
|
;; We may have gone too far. Go forward again. |
802 |
|
(line-beginning-position |
803 |
|
(if (looking-at (concat ".*\\(?:" comment-start-skip "\\)")) |
804 |
|
1 2)))) |
805 |
|
;; Find the beginning of the first line past the region to fill. |
806 |
|
(save-excursion |
807 |
|
(while (progn (forward-line 1) |
808 |
|
(looking-at comment-re))) |
809 |
|
(point))) |
810 |
|
;; Obey paragraph starters and boundaries within comments. |
811 |
|
(let* ((paragraph-separate |
812 |
|
;; Use the default values since they correspond to |
813 |
|
;; the values to use for plain text. |
814 |
|
(concat paragraph-separate "\\|[ \t]*\\(?:" |
815 |
|
comment-start-skip "\\)\\(?:" |
816 |
|
(default-value 'paragraph-separate) "\\)")) |
817 |
|
(paragraph-start |
818 |
|
(concat paragraph-start "\\|[ \t]*\\(?:" |
819 |
|
comment-start-skip "\\)\\(?:" |
820 |
|
(default-value 'paragraph-start) "\\)")) |
821 |
|
(paragraph-ignore-fill-prefix nil) |
822 |
|
(fill-prefix comment-fill-prefix) |
823 |
|
(after-line (if has-code-and-comment |
824 |
|
(line-beginning-position 2)))) |
825 |
|
(setq end (progn (forward-paragraph) (point))) |
826 |
|
;; If this comment starts on a line with code, |
827 |
|
;; include that line in the filling. |
828 |
|
(setq beg (progn (backward-paragraph) |
829 |
|
(if (eq (point) after-line) |
830 |
|
(forward-line -1)) |
831 |
|
(point))))) |
832 |
|
|
833 |
|
;; Find the fill-prefix to use. |
834 |
|
(cond |
835 |
|
(fill-prefix) ; Use the user-provided fill prefix. |
836 |
|
((and adaptive-fill-mode ; Try adaptive fill mode. |
837 |
|
(setq fill-prefix (fill-context-prefix beg end)) |
838 |
|
(string-match comment-start-skip fill-prefix))) |
839 |
|
(t |
840 |
|
(setq fill-prefix comment-fill-prefix))) |
841 |
|
|
842 |
|
;; Don't fill with narrowing. |
843 |
|
(or |
844 |
|
(fill-region-as-paragraph |
845 |
|
beg end justify nil |
846 |
|
;; Don't canonicalize spaces within the code just before |
847 |
|
;; the comment. |
848 |
|
(save-excursion |
849 |
|
(goto-char beg) |
850 |
|
(if (looking-at fill-prefix) |
851 |
|
nil |
852 |
|
(re-search-forward comment-start-skip)))) |
853 |
|
;; Make sure we don't return nil. |
854 |
|
t)))))) |
855 |
|
|
856 |
(defun fill-region (from to &optional justify nosqueeze to-eop) |
(defun fill-region (from to &optional justify nosqueeze to-eop) |
857 |
"Fill each of the paragraphs in the region. |
"Fill each of the paragraphs in the region. |
858 |
A prefix arg means justify as well. |
A prefix arg means justify as well. |
878 |
(if current-prefix-arg 'full)))) |
(if current-prefix-arg 'full)))) |
879 |
(unless (memq justify '(t nil none full center left right)) |
(unless (memq justify '(t nil none full center left right)) |
880 |
(setq justify 'full)) |
(setq justify 'full)) |
881 |
(let (end beg fill-pfx) |
(let (max beg fill-pfx) |
882 |
(save-restriction |
(goto-char (max from to)) |
883 |
(goto-char (max from to)) |
(when to-eop |
884 |
(when to-eop |
(skip-chars-backward "\n") |
885 |
(skip-chars-backward "\n") |
(forward-paragraph)) |
886 |
(forward-paragraph)) |
(setq max (copy-marker (point) t)) |
887 |
(setq end (point)) |
(goto-char (setq beg (min from to))) |
888 |
(goto-char (setq beg (min from to))) |
(beginning-of-line) |
889 |
(beginning-of-line) |
(while (< (point) max) |
890 |
(narrow-to-region (point) end) |
(let ((initial (point)) |
891 |
(while (not (eobp)) |
end) |
892 |
(let ((initial (point)) |
;; If using hard newlines, break at every one for filling |
893 |
end) |
;; purposes rather than using paragraph breaks. |
894 |
;; If using hard newlines, break at every one for filling |
(if use-hard-newlines |
895 |
;; purposes rather than using paragraph breaks. |
(progn |
896 |
(if use-hard-newlines |
(while (and (setq end (text-property-any (point) max |
897 |
(progn |
'hard t)) |
898 |
(while (and (setq end (text-property-any (point) (point-max) |
(not (= ?\n (char-after end))) |
899 |
'hard t)) |
(not (>= end max))) |
900 |
(not (= ?\n (char-after end))) |
(goto-char (1+ end))) |
901 |
(not (= end (point-max)))) |
(setq end (if end (min max (1+ end)) max)) |
902 |
(goto-char (1+ end))) |
(goto-char initial)) |
903 |
(setq end (if end (min (point-max) (1+ end)) (point-max))) |
(forward-paragraph 1) |
904 |
(goto-char initial)) |
(setq end (min max (point))) |
905 |
(forward-paragraph 1) |
(forward-paragraph -1)) |
906 |
(setq end (point)) |
(if (< (point) beg) |
907 |
(forward-paragraph -1)) |
(goto-char beg)) |
908 |
(if (< (point) beg) |
(if (>= (point) initial) |
909 |
(goto-char beg)) |
(setq fill-pfx |
910 |
(if (>= (point) initial) |
(fill-region-as-paragraph (point) end justify nosqueeze)) |
911 |
(setq fill-pfx |
(goto-char end)))) |
912 |
(fill-region-as-paragraph (point) end justify nosqueeze)) |
fill-pfx)) |
|
(goto-char end)))) |
|
|
fill-pfx))) |
|
913 |
|
|
914 |
|
|
915 |
(defcustom default-justification 'left |
(defcustom default-justification 'left |
930 |
This returns the value of the text-property `justification', |
This returns the value of the text-property `justification', |
931 |
or the variable `default-justification' if there is no text-property. |
or the variable `default-justification' if there is no text-property. |
932 |
However, it returns nil rather than `none' to mean \"don't justify\"." |
However, it returns nil rather than `none' to mean \"don't justify\"." |
933 |
(let ((j (or (get-text-property |
(let ((j (or (get-text-property |
934 |
;; Make sure we're looking at paragraph body. |
;; Make sure we're looking at paragraph body. |
935 |
(save-excursion (skip-chars-forward " \t") |
(save-excursion (skip-chars-forward " \t") |
936 |
(if (and (eobp) (not (bobp))) |
(if (and (eobp) (not (bobp))) |
937 |
(1- (point)) (point))) |
(1- (point)) (point))) |
938 |
'justification) |
'justification) |
970 |
(save-restriction |
(save-restriction |
971 |
(if whole-par |
(if whole-par |
972 |
(let ((paragraph-start (if use-hard-newlines "." paragraph-start)) |
(let ((paragraph-start (if use-hard-newlines "." paragraph-start)) |
973 |
(paragraph-ignore-fill-prefix (if use-hard-newlines t |
(paragraph-ignore-fill-prefix (if use-hard-newlines t |
974 |
paragraph-ignore-fill-prefix))) |
paragraph-ignore-fill-prefix))) |
975 |
(goto-char begin) |
(goto-char begin) |
976 |
(while (and (bolp) (not (eobp))) (forward-char 1)) |
(while (and (bolp) (not (eobp))) (forward-char 1)) |
1027 |
|
|
1028 |
;; A line has up to six parts: |
;; A line has up to six parts: |
1029 |
;; |
;; |
1030 |
;; >>> hello. |
;; >>> hello. |
1031 |
;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline] |
;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline] |
1032 |
;; |
;; |
1033 |
;; "Indent-1" is the left-margin indentation; normally it ends at column |
;; "Indent-1" is the left-margin indentation; normally it ends at column |
1039 |
;; Trailing whitespace is not counted as part of the line length when |
;; Trailing whitespace is not counted as part of the line length when |
1040 |
;; center- or right-justifying. |
;; center- or right-justifying. |
1041 |
;; |
;; |
1042 |
;; All parts of the line are optional, although the final newline can |
;; All parts of the line are optional, although the final newline can |
1043 |
;; only be missing on the last line of the buffer. |
;; only be missing on the last line of the buffer. |
1044 |
|
|
1045 |
(defun justify-current-line (&optional how eop nosqueeze) |
(defun justify-current-line (&optional how eop nosqueeze) |
1047 |
Normally does full justification: adds spaces to the line to make it end at |
Normally does full justification: adds spaces to the line to make it end at |
1048 |
the column given by `current-fill-column'. |
the column given by `current-fill-column'. |
1049 |
Optional first argument HOW specifies alternate type of justification: |
Optional first argument HOW specifies alternate type of justification: |
1050 |
it can be `left', `right', `full', `center', or `none'. |
it can be `left', `right', `full', `center', or `none'. |
1051 |
If HOW is t, will justify however the `current-justification' function says to. |
If HOW is t, will justify however the `current-justification' function says to. |
1052 |
If HOW is nil or missing, full justification is done by default. |
If HOW is nil or missing, full justification is done by default. |
1053 |
Second arg EOP non-nil means that this is the last line of the paragraph, so |
Second arg EOP non-nil means that this is the last line of the paragraph, so |
1076 |
count) |
count) |
1077 |
(end-of-line) |
(end-of-line) |
1078 |
;; Check if this is the last line of the paragraph. |
;; Check if this is the last line of the paragraph. |
1079 |
(if (and use-hard-newlines (null eop) |
(if (and use-hard-newlines (null eop) |
1080 |
(get-text-property (point) 'hard)) |
(get-text-property (point) 'hard)) |
1081 |
(setq eop t)) |
(setq eop t)) |
1082 |
(skip-chars-backward " \t") |
(skip-chars-backward " \t") |
1090 |
(beginning-of-line) |
(beginning-of-line) |
1091 |
(skip-chars-forward " \t") |
(skip-chars-forward " \t") |
1092 |
;; Skip over fill-prefix. |
;; Skip over fill-prefix. |
1093 |
(if (and fill-prefix |
(if (and fill-prefix |
1094 |
(not (string-equal fill-prefix "")) |
(not (string-equal fill-prefix "")) |
1095 |
(equal fill-prefix |
(equal fill-prefix |
1096 |
(buffer-substring |
(buffer-substring |
1097 |
(point) (min (point-max) (+ (length fill-prefix) |
(point) (min (point-max) (+ (length fill-prefix) |
1098 |
(point)))))) |
(point)))))) |
1099 |
(forward-char (length fill-prefix)) |
(forward-char (length fill-prefix)) |
1100 |
(if (and adaptive-fill-mode |
(if (and adaptive-fill-mode |
1101 |
(looking-at adaptive-fill-regexp)) |
(looking-at adaptive-fill-regexp)) |
1102 |
(goto-char (match-end 0)))) |
(goto-char (match-end 0)))) |
1103 |
(setq fp-end (point)) |
(setq fp-end (point)) |
1109 |
(setq endcol (current-column)) |
(setq endcol (current-column)) |
1110 |
|
|
1111 |
;; HOW can't be null or left--we would have exited already |
;; HOW can't be null or left--we would have exited already |
1112 |
(cond ((eq 'right how) |
(cond ((eq 'right how) |
1113 |
(setq ncols (- fc endcol)) |
(setq ncols (- fc endcol)) |
1114 |
(if (< ncols 0) |
(if (< ncols 0) |
1115 |
;; Need to remove some indentation |
;; Need to remove some indentation |
1116 |
(delete-region |
(delete-region |
1117 |
(progn (goto-char fp-end) |
(progn (goto-char fp-end) |
1118 |
(if (< (current-column) (+ indent ncols)) |
(if (< (current-column) (+ indent ncols)) |
1119 |
(move-to-column (+ indent ncols) t)) |
(move-to-column (+ indent ncols) t)) |
1123 |
(goto-char beg) |
(goto-char beg) |
1124 |
(indent-to (+ indent ncols)) |
(indent-to (+ indent ncols)) |
1125 |
;; If point was at beginning of text, keep it there. |
;; If point was at beginning of text, keep it there. |
1126 |
(if (= beg pos) |
(if (= beg pos) |
1127 |
(move-marker pos (point))))) |
(move-marker pos (point))))) |
1128 |
|
|
1129 |
((eq 'center how) |
((eq 'center how) |
1192 |
(save-excursion |
(save-excursion |
1193 |
(move-to-left-margin nil t) |
(move-to-left-margin nil t) |
1194 |
;; Position ourselves after any fill-prefix. |
;; Position ourselves after any fill-prefix. |
1195 |
(if (and fill-prefix |
(if (and fill-prefix |
1196 |
(not (string-equal fill-prefix "")) |
(not (string-equal fill-prefix "")) |
1197 |
(equal fill-prefix |
(equal fill-prefix |
1198 |
(buffer-substring |
(buffer-substring |
1199 |
(point) (min (point-max) (+ (length fill-prefix) |
(point) (min (point-max) (+ (length fill-prefix) |
1200 |
(point)))))) |
(point)))))) |
1201 |
(forward-char (length fill-prefix))) |
(forward-char (length fill-prefix))) |
1205 |
(defun unjustify-region (&optional begin end) |
(defun unjustify-region (&optional begin end) |
1206 |
"Remove justification whitespace from region. |
"Remove justification whitespace from region. |
1207 |
For centered or right-justified regions, this function removes any indentation |
For centered or right-justified regions, this function removes any indentation |
1208 |
past the left margin from each line. For full-justified lines, it removes |
past the left margin from each line. For full-justified lines, it removes |
1209 |
extra spaces between words. It does nothing in other justification modes. |
extra spaces between words. It does nothing in other justification modes. |
1210 |
Arguments BEGIN and END are optional; default is the whole buffer." |
Arguments BEGIN and END are optional; default is the whole buffer." |
1211 |
(save-excursion |
(save-excursion |
1231 |
When filling a mail message, pass a regexp for CITATION-REGEXP |
When filling a mail message, pass a regexp for CITATION-REGEXP |
1232 |
which will match the prefix of a line which is a citation marker |
which will match the prefix of a line which is a citation marker |
1233 |
plus whitespace, but no other kind of prefix. |
plus whitespace, but no other kind of prefix. |
1234 |
Also, if CITATION-REGEXP is non-nil, don't fill header lines." |
Also, if CITATION-REGEXP is non-nil, don't fill header lines." |
1235 |
(interactive (progn |
(interactive (progn |
1236 |
(barf-if-buffer-read-only) |
(barf-if-buffer-read-only) |
1237 |
(list (region-beginning) (region-end) |
(list (region-beginning) (region-end) |
1241 |
|
|
1242 |
(defun fill-individual-paragraphs (min max &optional justify citation-regexp) |
(defun fill-individual-paragraphs (min max &optional justify citation-regexp) |
1243 |
"Fill paragraphs of uniform indentation within the region. |
"Fill paragraphs of uniform indentation within the region. |
1244 |
This command divides the region into \"paragraphs\", |
This command divides the region into \"paragraphs\", |
1245 |
treating every change in indentation level or prefix as a paragraph boundary, |
treating every change in indentation level or prefix as a paragraph boundary, |
1246 |
then fills each paragraph using its indentation level as the fill prefix. |
then fills each paragraph using its indentation level as the fill prefix. |
1247 |
|
|
1261 |
When filling a mail message, pass a regexp for CITATION-REGEXP |
When filling a mail message, pass a regexp for CITATION-REGEXP |
1262 |
which will match the prefix of a line which is a citation marker |
which will match the prefix of a line which is a citation marker |
1263 |
plus whitespace, but no other kind of prefix. |
plus whitespace, but no other kind of prefix. |
1264 |
Also, if CITATION-REGEXP is non-nil, don't fill header lines." |
Also, if CITATION-REGEXP is non-nil, don't fill header lines." |
1265 |
(interactive (progn |
(interactive (progn |
1266 |
(barf-if-buffer-read-only) |
(barf-if-buffer-read-only) |
1267 |
(list (region-beginning) (region-end) |
(list (region-beginning) (region-end) |
1313 |
(if fill-individual-varying-indent |
(if fill-individual-varying-indent |
1314 |
;; If this line is a separator line, with or |
;; If this line is a separator line, with or |
1315 |
;; without prefix, end the paragraph. |
;; without prefix, end the paragraph. |
1316 |
(and |
(and |
1317 |
(not (looking-at paragraph-separate)) |
(not (looking-at paragraph-separate)) |
1318 |
(save-excursion |
(save-excursion |
1319 |
(not (and (looking-at fill-prefix-regexp) |
(not (and (looking-at fill-prefix-regexp) |
1327 |
;; If fill prefix is shorter than a new |
;; If fill prefix is shorter than a new |
1328 |
;; fill prefix computed here, end paragraph. |
;; fill prefix computed here, end paragraph. |
1329 |
(let ((this-line-fill-prefix |
(let ((this-line-fill-prefix |
1330 |
(fill-individual-paragraphs-prefix |
(fill-individual-paragraphs-prefix |
1331 |
citation-regexp))) |
citation-regexp))) |
1332 |
(>= (length fill-prefix) |
(>= (length fill-prefix) |
1333 |
(length this-line-fill-prefix))) |
(length this-line-fill-prefix))) |
1334 |
(save-excursion |
(save-excursion |
1335 |
(not (progn (forward-char |
(not (progn (forward-char |