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

    compat-29: Add substitute-quotes
---
 NEWS.org        |  2 ++
 compat-25.el    | 22 ++++++++++++++++++++++
 compat-28.el    | 18 ++++++++++++++++++
 compat-29.el    | 14 ++++++++++++++
 compat-tests.el | 29 +++++++++++++++++++++++++++++
 compat.texi     | 32 ++++++++++++++++++++++++++++++++
 6 files changed, 117 insertions(+)

diff --git a/NEWS.org b/NEWS.org
index 0cd51fbbe7..4b3ef8fb8b 100644
--- a/NEWS.org
+++ b/NEWS.org
@@ -14,6 +14,7 @@
 - compat-28: Add ~color-dark-p~.
 - compat-28: Add ~with-window-non-dedicated~.
 - compat-28: Add ~directory-files-and-attributes~ with COUNT argument.
+- compat-28: Add ~text-quoting-style~.
 - compat-29: Add ~compiled-function-p~.
 - compat-29: Add ~plist-get~ generalized variable.
 - compat-29: Add ~plistp~.
@@ -23,6 +24,7 @@
 - compat-29: Add ~buffer-local-set-state~ and ~buffer-local-restore-state~.
 - compat-29: Add ~use-region-beginning~, ~use-region-end~ and 
~use-region-noncontiguous-p~.
 - compat-29: Add ~get-scratch-buffer-create~.
+- compat-29: Add ~substitute-quotes~.
 
 * Release of "Compat" Version 29.1.2.0
 
diff --git a/compat-25.el b/compat-25.el
index 4a4aff9e06..6186ace9dc 100644
--- a/compat-25.el
+++ b/compat-25.el
@@ -70,6 +70,28 @@ usage: (bool-vector &rest OBJECTS)"
             ?\\ ?/))
       (aref name (1- (length name)))))
 
+;;;; Defined in doc.c
+
+(compat-defvar text-quoting-style nil ;; <compat-tests:text-quoting-style>
+  "Style to use for single quotes in help and messages.
+
+The value of this variable determines substitution of grave accents
+and apostrophes in help output (but not for display of Info
+manuals) and in functions like `message' and `format-message', but not
+in `format'.
+
+The value should be one of these symbols:
+  `curve':    quote with curved single quotes ‘like this’.
+  `straight': quote with straight apostrophes \\='like this\\='.
+  `grave':    quote with grave accent and apostrophe \\=`like this\\=';
+              i.e., do not alter the original quote marks.
+  nil:        like `curve' if curved single quotes are displayable,
+              and like `grave' otherwise.  This is the default.
+
+You should never read the value of this variable directly from a Lisp
+program.  Use the function `text-quoting-style' instead, as that will
+compute the correct value for the current terminal in the nil case.")
+
 ;;;; Defined in simple.el
 
 ;; `save-excursion' behaved like `save-mark-and-excursion' before 25.1.
diff --git a/compat-28.el b/compat-28.el
index 953ce2e721..a4b494df27 100644
--- a/compat-28.el
+++ b/compat-28.el
@@ -783,5 +783,23 @@ are 30 days long."
      (* (or (decoded-time-month time) 0) 60 60 24 30)
      (* (or (decoded-time-year time) 0) 60 60 24 365)))
 
+;;;; Defined in doc.c
+
+(compat-defun text-quoting-style () ;; <compat-tests:text-quoting-style>
+  "Return the current effective text quoting style.
+If the variable `text-quoting-style' is `grave', `straight' or
+`curve', just return that value.  If it is nil (the default), return
+`grave' if curved quotes cannot be displayed (for instance, on a
+terminal with no support for these characters), otherwise return
+`quote'.  Any other value is treated as `grave'.
+
+Note that in contrast to the variable `text-quoting-style', this
+function will never return nil."
+  (cond
+   ((memq text-quoting-style '(grave straight curve))
+    text-quoting-style)
+   ((not text-quoting-style) 'grave)
+   (t 'curve)))
+
 (provide 'compat-28)
 ;;; compat-28.el ends here
diff --git a/compat-29.el b/compat-29.el
index 6d6adfe7f6..60f5b2ec0c 100644
--- a/compat-29.el
+++ b/compat-29.el
@@ -1124,6 +1124,20 @@ command exists in this specific map, but it doesn't have 
the
         (delete (last key) submap)))
     def))
 
+;;;; Defined in help.el
+
+(compat-defun substitute-quotes (string) ;; <compat-tests:substitute-quotes>
+  "Substitute quote characters for display.
+Each grave accent \\=` is replaced by left quote, and each
+apostrophe \\=' is replaced by right quote.  Left and right quote
+characters are specified by `text-quoting-style'."
+  (cond ((eq (text-quoting-style) 'curve)
+         (string-replace "`" "‘"
+                         (string-replace "'" "’" string)))
+        ((eq (text-quoting-style) 'straight)
+         (string-replace "`" "'" string))
+        (t string)))
+
 ;;;; Defined in button.el
 
 (compat-defun button--properties (callback data help-echo) ;; 
<compat-tests:buttonize>
diff --git a/compat-tests.el b/compat-tests.el
index 9f6fcd50e7..92ea3f86e2 100644
--- a/compat-tests.el
+++ b/compat-tests.el
@@ -2771,5 +2771,34 @@
     (should-equal (point) 1)
     (should-equal (mark) 2)))
 
+(ert-deftest text-quoting-style ()
+  (should (text-quoting-style))
+  (let ((text-quoting-style t))
+    (should-equal 'curve (text-quoting-style)))
+  (let ((text-quoting-style 'foo))
+    (should-equal 'curve (text-quoting-style)))
+  (let ((text-quoting-style 'grave))
+    (should-equal 'grave (text-quoting-style))))
+
+(ert-deftest substitute-quotes ()
+  (let ((text-quoting-style 'curve))
+    (should-equal (substitute-quotes "quotes ‘like this’") "quotes ‘like 
this’")
+    (should-equal (substitute-quotes "`x'") "‘x’")
+    (should-equal (substitute-quotes "`") "‘")
+    (should-equal (substitute-quotes "'") "’")
+    (should-equal (substitute-quotes "\\`") "\\‘"))
+  (let ((text-quoting-style 'straight))
+    (should-equal (substitute-quotes "quotes `like this'") "quotes 'like 
this'")
+    (should-equal (substitute-quotes "`x'") "'x'")
+    (should-equal (substitute-quotes "`") "'")
+    (should-equal (substitute-quotes "'") "'")
+    (should-equal (substitute-quotes "\\`") "\\'"))
+  (let ((text-quoting-style 'grave))
+    (should-equal (substitute-quotes "quotes `like this'") "quotes `like 
this'")
+    (should-equal (substitute-quotes "`x'") "`x'")
+    (should-equal (substitute-quotes "`") "`")
+    (should-equal (substitute-quotes "'") "'")
+    (should-equal (substitute-quotes "\\`") "\\`")))
+
 (provide 'compat-tests)
 ;;; compat-tests.el ends here
diff --git a/compat.texi b/compat.texi
index 2732ecced6..9839a28f09 100644
--- a/compat.texi
+++ b/compat.texi
@@ -237,6 +237,24 @@ manage to provide for each Emacs version.
 The following functions and macros implemented in 25.1, and are provided
 by Compat:
 
+@c copied from lispref/help.texi
+@defopt text-quoting-style
+The value of this user option is a symbol that specifies the style
+Emacs should use for single quotes in the wording of help and
+messages.  If the option's value is @code{curve}, the style is
+@t{‘like this’} with curved single quotes.  If the value is
+@code{straight}, the style is @t{'like this'} with straight
+apostrophes.  If the value is @code{grave}, quotes are not translated
+and the style is @t{`like this'} with grave accent and apostrophe, the
+standard style before Emacs version 25.  The default value @code{nil}
+acts like @code{curve} if curved single quotes seem to be displayable,
+and like @code{grave} otherwise.
+
+This option is useful on platforms that have problems with curved
+quotes.  You can customize it freely according to your personal
+preference.
+@end defopt
+
 @c based on lisp/simple.el
 @defun region-bounds
 Return the boundaries of the region.  Value is a list of one or more
@@ -1468,6 +1486,14 @@ functionality.
 The following functions and macros implemented in 28.1, and are provided
 by Compat:
 
+@c copied from lispref/help.texi
+@defun text-quoting-style
+You should not read the value of the variable
+@code{text-quoting-style} directly.  Instead, use this function with
+the same name to dynamically compute the correct quoting style on the
+current terminal in the @code{nil} case described above.
+@end defun
+
 @c copied from lispref/strings.texi
 @defun string-search needle haystack &optional start-pos
 Return the position of the first instance of @var{needle} in
@@ -2060,6 +2086,12 @@ provided by Compat. Note that due to upstream changes, 
it might happen
 that there will be the need for changes, so use these functions with
 care.
 
+@c copied from lispref/help.texi
+@defun substitute-quotes string
+This function works like @code{substitute-command-keys}, but only
+replaces quote characters.
+@end defun
+
 @c based on lisp/simple.el
 @defun get-scratch-buffer-create
 Return the *scratch* buffer, creating a new one if needed.

Reply via email to