branch: externals/compat
commit 2aa415a6fa0ab87802e79b119f09746080425bc6
Author: Daniel Mendler <m...@daniel-mendler.de>
Commit: Daniel Mendler <m...@daniel-mendler.de>

    compat-29: Add use-region-noncontiguous-p
---
 NEWS.org        |  4 +++-
 compat-25.el    |  9 ++++++++-
 compat-26.el    | 13 +++++++++++++
 compat-29.el    |  4 ++++
 compat-tests.el | 32 ++++++++++++++++++++++++++++++++
 compat.texi     | 20 ++++++++++++++++++++
 6 files changed, 80 insertions(+), 2 deletions(-)

diff --git a/NEWS.org b/NEWS.org
index 0a370bbcd0..0cd51fbbe7 100644
--- a/NEWS.org
+++ b/NEWS.org
@@ -5,6 +5,8 @@
 - compat-25: Add ~hash-table-empty-p~.
 - compat-25: Add ~macroexp-parse-body~ and ~macroexp-quote~.
 - compat-25: Add ~save-mark-and-excursion~.
+- compat-25: Add ~region-noncontiguous-p~.
+- compat-26: Add ~region-bounds~.
 - compat-27: Add ~date-ordinal-to-time~.
 - compat-27: Add ~make-decoded-time~.
 - compat-27: Add ~ring-resize~.
@@ -19,7 +21,7 @@
 - compat-29: Add ~delete-line~.
 - compat-29: Add ~with-narrowing~.
 - compat-29: Add ~buffer-local-set-state~ and ~buffer-local-restore-state~.
-- compat-29: Add ~use-region-beginning~ and ~use-region-end~.
+- compat-29: Add ~use-region-beginning~, ~use-region-end~ and 
~use-region-noncontiguous-p~.
 - compat-29: Add ~get-scratch-buffer-create~.
 
 * Release of "Compat" Version 29.1.2.0
diff --git a/compat-25.el b/compat-25.el
index f0ac3acf4d..4a4aff9e06 100644
--- a/compat-25.el
+++ b/compat-25.el
@@ -73,7 +73,14 @@ usage: (bool-vector &rest OBJECTS)"
 ;;;; Defined in simple.el
 
 ;; `save-excursion' behaved like `save-mark-and-excursion' before 25.1.
-(compat-defalias save-mark-and-excursion save-excursion)
+(compat-defalias save-mark-and-excursion save-excursion) ;; 
<compat-tests:save-mark-and-excursion>
+
+(declare-function region-bounds nil) ;; Defined in compat-26.el
+(compat-defun region-noncontiguous-p () ;; 
<compat-tests:region-noncontiguous-p>
+  "Return non-nil if the region contains several pieces.
+An example is a rectangular region handled as a list of
+separate contiguous regions for each line."
+  (let ((bounds (region-bounds))) (and (cdr bounds) bounds)))
 
 ;;;; Defined in subr.el
 
diff --git a/compat-26.el b/compat-26.el
index a139251726..4340113dab 100644
--- a/compat-26.el
+++ b/compat-26.el
@@ -69,6 +69,19 @@ SEQUENCE may be a list, a vector, a boolean vector, or a 
string."
         (line-number-at-pos position))
     (line-number-at-pos position)))
 
+;;;; Defined in simple.el
+
+(compat-defun region-bounds () ;; <compat-tests:region-bounds>
+  "Return the boundaries of the region.
+Value is a list of one or more cons cells of the form (START . END).
+It will have more than one cons cell when the region is non-contiguous,
+see `region-noncontiguous-p' and `extract-rectangle-bounds'."
+  (if (eval-when-compile (< emacs-major-version 25))
+      ;; FIXME: The `region-extract-function' of Emacs 24 has no support for 
the
+      ;; bounds argument.
+      (list (cons (region-beginning) (region-end)))
+    (funcall region-extract-function 'bounds)))
+
 ;;;; Defined in subr.el
 
 (compat-defun alist-get (key alist &optional default remove testfn) ;; 
<compat-tests:alist-get>
diff --git a/compat-29.el b/compat-29.el
index 6315a07bb9..99fd3d5712 100644
--- a/compat-29.el
+++ b/compat-29.el
@@ -352,6 +352,10 @@ CONDITION."
 
 ;;;; Defined in simple.el
 
+(compat-defun use-region-noncontiguous-p () ;; 
<compat-tests:region-noncontiguous-p>
+  "Return non-nil for a non-contiguous region if `use-region-p'."
+  (and (use-region-p) (region-noncontiguous-p)))
+
 (compat-defun use-region-beginning () ;; <compat-tests:use-region>
   "Return the start of the region if `use-region-p'."
   (and (use-region-p) (region-beginning)))
diff --git a/compat-tests.el b/compat-tests.el
index fd324a96a0..9f6fcd50e7 100644
--- a/compat-tests.el
+++ b/compat-tests.el
@@ -2689,6 +2689,38 @@
     (should-equal 2 (use-region-beginning))
     (should-equal 7 (use-region-end))))
 
+(ert-deftest region-bounds ()
+  (should-error (region-bounds))
+  ;; FIXME: On Emacs 24 `region-bounds' always returns a continuous region.
+  (when (> emacs-major-version 24)
+    (let ((region-extract-function #'ignore))
+      (should-not (region-bounds)))
+    (let ((region-extract-function (lambda (_) '((2 . 3) (6 . 7)))))
+      (should-equal (region-bounds) '((2 . 3) (6 . 7)))))
+  (with-temp-buffer
+    (insert "abc\ndef\n")
+    (set-mark 2)
+    (goto-char 7)
+    (should-equal (region-bounds) '((2 . 7)))))
+
+(ert-deftest region-noncontiguous-p ()
+  (when (> emacs-major-version 24)
+    (let ((region-extract-function (lambda (_) '((2 . 3) (6 . 7)))))
+      (should (region-noncontiguous-p))))
+  (with-temp-buffer
+    (insert "abc\ndef\n")
+    (set-mark 2)
+    (goto-char 7)
+    (transient-mark-mode)
+    (should-not (region-noncontiguous-p))
+    (should-not (use-region-noncontiguous-p))
+    (should (use-region-p))
+    ;; FIXME: On Emacs 24 `region-bounds' always returns a continuous region.
+    (when (> emacs-major-version 24)
+      (let ((region-extract-function (lambda (_) '((2 . 3) (6 . 7)))))
+        (should (region-noncontiguous-p))
+        (should (use-region-noncontiguous-p))))))
+
 (ert-deftest get-scratch-buffer-create ()
   (should-equal "*scratch*" (buffer-name (get-scratch-buffer-create)))
   (should-equal initial-major-mode (buffer-local-value 'major-mode 
(get-scratch-buffer-create))))
diff --git a/compat.texi b/compat.texi
index 90184ef41a..2732ecced6 100644
--- a/compat.texi
+++ b/compat.texi
@@ -237,6 +237,21 @@ manage to provide for each Emacs version.
 The following functions and macros implemented in 25.1, and are provided
 by Compat:
 
+@c based on lisp/simple.el
+@defun region-bounds
+Return the boundaries of the region.  Value is a list of one or more
+cons cells of the form @code{(start . end)}.  It will have more than
+one cons cell when the region is non-contiguous, see
+@code{region-noncontiguous-p} and @code{extract-rectangle-bounds}.
+@end defun
+
+@c based on lisp/simple.el
+@defun region-noncontiguous-p
+Return non-nil if the region contains several pieces.  An example is a
+rectangular region handled as a list of separate contiguous regions
+for each line.
+@end defun
+
 @c copied from lispref/positions.texi
 @defmac save-mark-and-excursion body@dots{}
 This macro is like @code{save-excursion}, but also saves and restores
@@ -2050,6 +2065,11 @@ care.
 Return the *scratch* buffer, creating a new one if needed.
 @end defun
 
+@c based on lisp/simple.el
+@defun use-region-noncontiguous-p
+Return non-nil for a non-contiguous region if @code{use-region-p}.
+@end defun
+
 @c based on lisp/simple.el
 @defun use-region-end
 Return the end of the region if @code{use-region-p}.

Reply via email to