From acbbc93a4efb33850dbe5e7a42c4c237962fe354 Mon Sep 17 00:00:00 2001 From: Phil Sainty Date: Sat, 12 Nov 2016 22:31:53 +1300 Subject: [PATCH] Enhance the use of prefix arguments when filling text * lisp/textmodes/fill.el (fill-region-as-paragraph): Modify fill-column locally, based on the interactive prefix argument. (fill-interactive-prefix-type): New user option. (fill-interactive-column, fill-interactive-justify-arg): New functions. (fill-region-as-paragraph, fill-paragraph, fill-region, fill-nonuniform-paragraphs, fill-individual-paragraphs): Change how interactive JUSTIFY argument is calculated. --- lisp/textmodes/fill.el | 94 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 88 insertions(+), 6 deletions(-) diff --git a/lisp/textmodes/fill.el b/lisp/textmodes/fill.el index 100e2a2..788546a 100644 --- a/lisp/textmodes/fill.el +++ b/lisp/textmodes/fill.el @@ -618,6 +618,87 @@ fill-indent-to-left-margin (indent-line-to (current-left-margin)) (put-text-property beg (point) 'face 'default))) +(defcustom fill-interactive-prefix-type 'column-and-justify + "Determines how fill commands interpret a prefix argument. + +Under the default `column-and-justify' setting, a plain C-u +argument sets the JUSTIFY argument; a positive numeric argument +specifies `fill-column' for the duration of the command; a +negative numeric argument specifies both a `fill-column' value +and JUSTIFY together; and a plain `-' argument causes the text to +be 'un-filled' into a single line. Temporary fill-column values +are displayed in the echo area. A numeric prefix argument of +zero does not modify the fill-column, but also causes the current +value to be displayed. + +The `justify' setting is the original behaviour, where any prefix +argument simply means JUSTIFY. + +The `hybrid' setting behaves like `column-and-justify' with the +exception of single-digit numeric arguments, which mean JUSTIFY. +Because fill-columns of less than 10 are unlikely to be useful, +this setting provides the new features while still enabling the +use of a numeric prefix argument to mean JUSTIFY for users who +were used to doing that." + :type '(choice (const :tag "Fill-column and/or justify" column-and-justify) + (const :tag "Justify only" justify) + (const :tag "Hybrid" hybrid)) + :safe 'symbolp + :group 'fill) + +(defun fill-interactive-justify-arg () + "Determine the JUSTIFY argument for the current fill command +based on the interactive `current-prefix-arg', and the user +option `fill-interactive-prefix-type'. + +Called in the interactive forms of fill commands. + +Returns either 'full or nil in all cases." + (cond + ;; column-and-justify + ((eq fill-interactive-prefix-type 'column-and-justify) + (if (or (consp current-prefix-arg) + (and (integerp current-prefix-arg) + (< current-prefix-arg 0))) + 'full)) + ;; hybrid + ((eq fill-interactive-prefix-type 'hybrid) + (if (or (consp current-prefix-arg) + (and (integerp current-prefix-arg) + (< current-prefix-arg 10))) + 'full)) + ;; justify + ((eq fill-interactive-prefix-type 'justify) + (if current-prefix-arg 'full)))) + +(defun fill-interactive-column (col) + "Return column COL, modified based on the current prefix arg +and the user option `fill-interactive-prefix-type'. + +`fill-region-as-paragraph' let-binds `fill-column' to this value." + (cond + ;; justify (do nothing; ignore remaining conditions) + ((eq fill-interactive-prefix-type 'justify)) + ;; un-fill + ((eq current-prefix-arg '-) + (setq col most-positive-fixnum) + (message "Un-filling")) + ;; column-and-justify + ((and (eq fill-interactive-prefix-type 'column-and-justify) + (integerp current-prefix-arg)) + (unless (eq 0 current-prefix-arg) + (setq col (abs current-prefix-arg))) + (message "Using fill column %d" col)) + ;; hybrid + ((and (eq fill-interactive-prefix-type 'hybrid) + (integerp current-prefix-arg) + (or (> current-prefix-arg 9) + (< current-prefix-arg 0))) + (setq col (abs current-prefix-arg)) + (message "Using fill column %d" col))) + ;; return the column, possibly unchanged. + col) + (defun fill-region-as-paragraph (from to &optional justify nosqueeze squeeze-after) "Fill the region as one paragraph. @@ -642,7 +723,7 @@ fill-region-as-paragraph (interactive (progn (barf-if-buffer-read-only) (list (region-beginning) (region-end) - (if current-prefix-arg 'full)))) + (fill-interactive-justify-arg)))) (unless (memq justify '(t nil none full center left right)) (setq justify 'full)) @@ -726,7 +807,8 @@ fill-region-as-paragraph ;; This is the actual filling loop. (goto-char from) - (let (linebeg) + (let ((fill-column (fill-interactive-column fill-column)) + linebeg) (while (< (point) to) (setq linebeg (point)) (move-to-column (current-fill-column)) @@ -803,7 +885,7 @@ fill-paragraph region, instead of just filling the current paragraph." (interactive (progn (barf-if-buffer-read-only) - (list (if current-prefix-arg 'full) t))) + (list (fill-interactive-justify-arg) t))) (or ;; 1. Fill the region if it is active when called interactively. (and region transient-mark-mode mark-active @@ -1023,7 +1105,7 @@ fill-region (interactive (progn (barf-if-buffer-read-only) (list (region-beginning) (region-end) - (if current-prefix-arg 'full)))) + (fill-interactive-justify-arg)))) (unless (memq justify '(t nil none full center left right)) (setq justify 'full)) (let ((start-point (point-marker)) @@ -1383,7 +1465,7 @@ fill-nonuniform-paragraphs (interactive (progn (barf-if-buffer-read-only) (list (region-beginning) (region-end) - (if current-prefix-arg 'full)))) + (fill-interactive-justify-arg)))) (let ((fill-individual-varying-indent t)) (fill-individual-paragraphs min max justifyp citation-regexp))) @@ -1413,7 +1495,7 @@ fill-individual-paragraphs (interactive (progn (barf-if-buffer-read-only) (list (region-beginning) (region-end) - (if current-prefix-arg 'full)))) + (fill-interactive-justify-arg)))) (save-restriction (save-excursion (goto-char min) -- 2.8.3