unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] Enhance the use of prefix arguments when filling text
@ 2016-11-12 11:21 Phil Sainty
  2016-11-12 11:55 ` Eli Zaretskii
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Phil Sainty @ 2016-11-12 11:21 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1730 bytes --]

This patch enables the use of a numeric prefix argument to select a
one-time `fill-column' value, over-riding the value for that call.

It takes effect before the main fill loop in `fill-region-as-paragraph'
(which is ultimately used by all the other fill commands), by which time
the likes of `emacs-lisp-docstring-fill-column' have already been dealt
with, and won't clobber the prefix value.

A prefix argument of `-' now means "un-fill" (turning multiple lines
into a single long line), by using most-positive-fixnum as the
fill-column for that call.

All fill commands accept a JUSTIFY argument, and at present a prefix
argument of any kind always simply means JUSTIFY is 'full. As that
interactive behaviour is implemented independently by each command,
this patch necessarily changes the interactive argument handling for
each fill command, to prevent them from justifying when we are just
setting a fill column.

With the patch, negative numeric arguments now mean JUSTIFY using the
specified fill column.

In all cases a plain C-u still means (only) JUSTIFY.

A new user option `fill-interactive-prefix-type' determines whether or
not the new behaviour is used.  There is a setting to revert to the
original behaviour (where any prefix argument means JUSTIFY), and also
a hybrid setting to combine the old and new behaviours in a practical
manner.

This initial patch addresses only the commands in fill.el itself, but
changes for other fill commands ought to follow a similar pattern.

The patch is also missing documentation changes to the modified commands
and to the info manuals, but I'm sending it as-is for review of the
general idea and implementation.

The patch was rolled against the emacs-25 branch.


-Phil


[-- Attachment #2: 0001-Enhance-use-of-prefix-arguments-when-filling-text.patch --]
[-- Type: text/x-patch, Size: 6302 bytes --]

From acbbc93a4efb33850dbe5e7a42c4c237962fe354 Mon Sep 17 00:00:00 2001
From: Phil Sainty <psainty@orcon.net.nz>
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


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2016-11-14 23:14 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-11-12 11:21 [PATCH] Enhance the use of prefix arguments when filling text Phil Sainty
2016-11-12 11:55 ` Eli Zaretskii
2016-11-13 10:33   ` Phil Sainty
2016-11-13 15:15     ` Eli Zaretskii
2016-11-14 23:14       ` Noam Postavsky
2016-11-12 17:21 ` Herring, Davis
2016-11-13 10:21   ` Phil Sainty
2016-11-12 18:53 ` Stefan Monnier
2016-11-13 10:23   ` Phil Sainty
2016-11-14 22:56   ` John Wiegley

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).