Subject: bug#18847: 24.4; Inconsistent behaviour of M-h with negative arguments

From: [email-masked] (H. Dieter Wilhelm)
Date: Mon, 03 Nov 2014 18:56:00 +0100

Stefan Monnier <[email-masked]> writes:

> [ Be careful to keep [email-masked] in the Cc, please.  ]

Sorry, I overlooked the Cc field.

....

>> 2) When applying M-h (or any other marking command) at the end of a
>>    buffer (at least interactively, I failed to address this), the right
>>    thing to do is marking (a) previous element(s).  I would like to know
>>    if you find this a good idea.
>
> I have no opinion on this, I never use M-h myself.

Interesting, no M-h!  Anyway, in the meantime I tested M-h more
thoroughly in these borderline cases and I think its behaviour is
actually not correct.  I suggest to separate this issue and possible
enhancements from the current subject.

>> 3) M-h (C-M-h and C-x C-p ) differ from C-@ and C-M-@ that they are
>>    always marking (a) whole element(s).  I wanted to clarify in the
>>    documentation string that a marking from point is also achievable
>>    with M-h.
>
> So this part is just a docstring change, right?

Exactly.

>> 4) M-h does signal an error applying zero as an argument, the other
>>    marking commands just ignore zero, so I thought a message might be a
>>    good compromise to the current state...
>
> I have no opinion on this one either.  It doesn't seem terribly
> important, so reducing discrepancies in this case might be the
> dominating factor.

Right, I dropped the zero argument message. (But still inhibited M-h to
activate the mark in this situation.)

Thank you for your patience

       Dieter

(defun mark-paragraph (&optional arg allow-extend)
  "Put point at beginning of this paragraph, mark at end.
The paragraph marked is the one that contains point or follows
point.

With argument ARG, puts mark at the end of this or a following
paragraph, so that the number of paragraphs marked equals ARG.

If ARG is negative, point is put at the end of this paragraph,
mark is put at the beginning of this or a previous paragraph.

Interactively (or if ALLOW-EXTEND is non-nil), if this command is
repeated or (in Transient Mark mode) if the mark is active, it
marks the next ARG paragraphs after the region already marked.
This also means when activating the mark immediately before using
this command, the current paragraph is only marked from point."
  (interactive "P\np")
  (let ((numeric-arg (prefix-numeric-value arg)))
    (cond ((zerop numeric-arg))
	  ((and allow-extend
		(or (and (eq last-command this-command) mark-active)
		    (region-active-p)))
	   (if arg
	       (setq arg numeric-arg)
	     (if (< (mark) (point))
		 (setq arg -1)
	       (setq arg 1)))
	   (set-mark
	    (save-excursion
	      (goto-char (mark))
	      (forward-paragraph arg)
	      (point))))
	  (t
	   (forward-paragraph numeric-arg)
	   (push-mark nil t t)
	   (backward-paragraph numeric-arg)))))

diff --git a/lisp/textmodes/paragraphs.el b/lisp/textmodes/paragraphs.el
index 3e77d37..e1a735d 100644
--- a/lisp/textmodes/paragraphs.el
+++ b/lisp/textmodes/paragraphs.el
@@ -370,34 +370,47 @@ See `forward-paragraph' for more information."
   (forward-paragraph (- arg)))
 
 (defun mark-paragraph (&optional arg allow-extend)
-  "Put point at beginning of this paragraph, mark at end.
-The paragraph marked is the one that contains point or follows point.
-
-With argument ARG, puts mark at end of a following paragraph, so that
-the number of paragraphs marked equals ARG.
-
-If ARG is negative, point is put at end of this paragraph, mark is put
-at beginning of this or a previous paragraph.
-
-Interactively (or if ALLOW-EXTEND is non-nil), if this command is
-repeated or (in Transient Mark mode) if the mark is active,
-it marks the next ARG paragraphs after the ones already marked."
-  (interactive "p\np")
-  (unless arg (setq arg 1))
-  (when (zerop arg)
-    (error "Cannot mark zero paragraphs"))
-  (cond ((and allow-extend
-	      (or (and (eq last-command this-command) (mark t))
-		  (and transient-mark-mode mark-active)))
-	 (set-mark
-	  (save-excursion
-	    (goto-char (mark))
-	    (forward-paragraph arg)
-	    (point))))
-	(t
-	 (forward-paragraph arg)
-	 (push-mark nil t t)
-	 (backward-paragraph arg))))
+  "Put mark at beginning of this paragraph,  point at end.
+The paragraph marked is the one that contains point or follows
+point.
+
+With argument ARG, puts mark at the end of this or a following
+paragraph, so that the number of paragraphs marked equals ARG.
+
+If ARG is negative, point is put at the beginning of this
+paragraph, mark is put at the end of this or a previous
+paragraph.
+
+Interactively, if this command is repeated or (in Transient Mark
+Mode) if the mark is active, it marks the next ARG paragraphs
+after the region already marked.  This also means when activating
+the mark immediately before using this command, the current
+paragraph is only marked from point."
+  (interactive "P\np")
+  (let ((numeric-arg (prefix-numeric-value arg)))
+    (cond ((eobp)			; smart-aleck?
+	   (backward-paragraph (abs numeric-arg))
+	   (push-mark nil t t)
+	   (forward-paragraph (abs numeric-arg)))
+	  ((and allow-extend
+		(or (region-active-p)
+		    (and (eq last-command this-command) mark-active)))
+	   (if arg
+	       (setq arg numeric-arg)
+	     (if (< (mark) (point))
+		 (setq arg -1)
+	       (setq arg 1)))
+	   (set-mark
+	    (save-excursion
+	      (goto-char (mark))
+	      (forward-paragraph arg)
+	      (point))))
+	  ((zerop numeric-arg)
+	   (message "Will not mark zero paragraphs."))
+	  (t
+	   (forward-paragraph numeric-arg)
+	   (push-mark nil t t)
+	   (backward-paragraph numeric-arg)))))
 
 (defun kill-paragraph (arg)
   "Kill forward to end of paragraph.

-- 
Best wishes
H. Dieter Wilhelm
Darmstadt, Germany