unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Michal Nazarewicz <mina86@mina86.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: 18730@debbugs.gnu.org
Subject: bug#18730: [PATCH 1/3] tildify.el: introduce a `tildify-space-string' variable
Date: Mon, 17 Nov 2014 16:41:08 +0100	[thread overview]
Message-ID: <1416238870-31725-1-git-send-email-mina86@mina86.com> (raw)
In-Reply-To: <jwvh9ygw7a4.fsf-monnier+emacsbugs@gnu.org>

Deprecate `tildify-string-alist' variable and instead introduce
a new `tildify-space-string' variable.  The alist was somehow
complicated to use, both for users and in code, and did not work
correctly with derived modes.  Instead of trying to make its
handling even more complicated, replace it with
`tildify-space-string' variable that is set as a buffer-local
variable in plain-tex, sgml and nxml modes.
---
 etc/NEWS                        |  3 +++
 lisp/nxml/nxml-mode.el          | 10 ++++++++++
 lisp/textmodes/sgml-mode.el     |  9 +++++++++
 lisp/textmodes/tex-mode.el      |  4 ++++
 lisp/textmodes/tildify.el       | 38 +++++++++++++++++++++++++-------------
 test/automated/tildify-tests.el |  4 ++--
 6 files changed, 53 insertions(+), 15 deletions(-)

On Mon, Nov 03 2014, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
>> I'm slightly afraid of using no-break space since it may confuse people
>> who use incompatible encodings.
>
> Such encodings are pretty rare, I think.
>
>> Should I just go ahead with it anyway and have Emacs deal with
>> encodings once user attempts to save the file?
>
> Yes.

Changed the code to default to no-break space.

> As an XHTML user, I'd prefer seeing &nbsp; over some numerical constant,
> so if there's a way to auto-detect when it can be used, that's
> even better.  But feel free to install your patch with &#160; and keep
> the &nbsp; for "maybe if I find the time later".

Added a FIXME.

You gave a thumbs up for this patch already, but it had a few
modifications and I wanted to mail it in the context of the next two
patches to get a final ack.

diff --git a/etc/NEWS b/etc/NEWS
index 4282dc5..838a800 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -298,6 +298,9 @@ use PDF instead of DVI.
 By default, 32 spaces and four TABs are considered to be too much but
 `whitespace-big-indent-regexp' can be configured to change that.
 
+** tildify: `tildify-space-string' variable has been added making
+`tildify-string-alist' obsolete.
+
 ** Obsolete packages
 
 ---
diff --git a/lisp/nxml/nxml-mode.el b/lisp/nxml/nxml-mode.el
index 4859bbc..47f8066 100644
--- a/lisp/nxml/nxml-mode.el
+++ b/lisp/nxml/nxml-mode.el
@@ -449,6 +449,8 @@ reference.")
     (when rng-validate-mode
       (rng-validate-while-idle (current-buffer)))))
 
+(defvar tildify-space-string)
+
 ;;;###autoload
 (define-derived-mode nxml-mode text-mode "nXML"
   ;; We use C-c C-i instead of \\[nxml-balanced-close-start-tag-inline]
@@ -505,6 +507,14 @@ be treated as a single markup item, set the variable
 Many aspects this mode can be customized using
 \\[customize-group] nxml RET."
   ;; (kill-all-local-variables)
+  ;; If encoding does not allow non-break space character, use reference..
+  ;; FIXME: This duplicates code from sgml-mode, perhaps derive from it?
+  ;; FIXME: Perhaps use &nbsp; if possible (e.g. XHTML)?
+  (setq-local tildify-space-string
+              (if (equal (decode-coding-string
+                          (encode-coding-string " " buffer-file-coding-system)
+                          buffer-file-coding-system) " ")
+                  " " "&#160;"))
   (set (make-local-variable 'mode-line-process) '((nxml-degraded "/degraded")))
   ;; We'll determine the fill prefix ourselves
   (make-local-variable 'adaptive-fill-mode)
diff --git a/lisp/textmodes/sgml-mode.el b/lisp/textmodes/sgml-mode.el
index 39ac062..9d1cb03 100644
--- a/lisp/textmodes/sgml-mode.el
+++ b/lisp/textmodes/sgml-mode.el
@@ -456,6 +456,8 @@ This function is designed for use in `fill-nobreak-predicate'.
 	 (skip-chars-backward "/?!")
 	 (eq (char-before) ?<))))
 
+(defvar tildify-space-string)
+
 ;;;###autoload
 (define-derived-mode sgml-mode text-mode '(sgml-xml-mode "XML" "SGML")
   "Major mode for editing SGML documents.
@@ -477,6 +479,13 @@ Do \\[describe-key] on the following bindings to discover what they do.
 \\{sgml-mode-map}"
   (make-local-variable 'sgml-saved-validate-command)
   (make-local-variable 'facemenu-end-add-face)
+  ;; If encoding does not allow non-break space character, use reference..
+  ;; FIXME: Perhaps use &nbsp; if possible (e.g. when we know its HTML)?
+  (setq-local tildify-space-string
+              (if (equal (decode-coding-string
+                          (encode-coding-string " " buffer-file-coding-system)
+                          buffer-file-coding-system) " ")
+                  " " "&#160;"))
   ;;(make-local-variable 'facemenu-remove-face-function)
   ;; A start or end tag by itself on a line separates a paragraph.
   ;; This is desirable because SGML discards a newline that appears
diff --git a/lisp/textmodes/tex-mode.el b/lisp/textmodes/tex-mode.el
index bc10eab..0cfc0cf 100644
--- a/lisp/textmodes/tex-mode.el
+++ b/lisp/textmodes/tex-mode.el
@@ -1203,9 +1203,13 @@ Entering SliTeX mode runs the hook `text-mode-hook', then the hook
   (setq tex-command slitex-run-command)
   (setq tex-start-of-header "\\\\documentstyle{slides}\\|\\\\documentclass{slides}"))
 
+(defvar tildify-space-string)
+
 (defun tex-common-initialization ()
   ;; Regexp isearch should accept newline and formfeed as whitespace.
   (setq-local search-whitespace-regexp "[ \t\r\n\f]+")
+  ;; Use tilde as hard-space character in tildify package.
+  (setq-local tildify-space-string "~")
   ;; A line containing just $$ is treated as a paragraph separator.
   (setq-local paragraph-start "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$")
   ;; A line starting with $$ starts a paragraph,
diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el
index 91f5a38..865dcec 100644
--- a/lisp/textmodes/tildify.el
+++ b/lisp/textmodes/tildify.el
@@ -4,7 +4,7 @@
 
 ;; Author:     Milan Zamazal <pdm@zamazal.org>
 ;;             Michal Nazarewicz <mina86@mina86.com>
-;; Version:    4.5.4
+;; Version:    4.5.5
 ;; Keywords:   text, TeX, SGML, wp
 
 ;; This file is part of GNU Emacs.
@@ -86,15 +86,24 @@ mode, the item for the mode SYMBOL is looked up in the alist instead."
                                        (integer :tag "Group "))
                                (symbol :tag "Like other")))))
 
-(defcustom tildify-string-alist
-  '((latex-mode . "~")
-    (tex-mode . latex-mode)
-    (plain-tex-mode . latex-mode)
-    (sgml-mode . "&nbsp;")
-    (html-mode . sgml-mode)
-    (xml-mode . "&#160;") ; XML does not define &nbsp; use numeric reference
-    (nxml-mode . xml-mode)
-    (t . " "))
+(defcustom tildify-space-string "\u00A0"
+  "Representation of a hard (a.k.a. no-break) space in current major mode.
+
+Used by `tildify-buffer' in places where space is required but line
+cannot be broken.  For example \"~\" for TeX or \"&#160;\" for SGML,
+HTML and XML modes.  A no-break space Unicode character (\"\\u00A0\")
+might be used for other modes if compatible encoding is used.
+
+If nil, current major mode has no way to represent a hard space."
+  :version "25.1"
+  :group 'tildify
+  :type '(choice (const :tag "Space character (no hard-space representation)"
+                        " ")
+                 (const :tag "No-break space (U+00A0)" "\u00A0")
+                 (string :tag "Custom string"))
+  :safe t)
+
+(defcustom tildify-string-alist ()
   "Alist specifying what is a hard space in the current major mode.
 
 Each alist item is of the form (MAJOR-MODE . STRING) or
@@ -118,6 +127,8 @@ mode, the item for the mode SYMBOL is looked up in the alist instead."
                        (choice (const  :tag "No-break space (U+00A0)" "\u00A0")
                                (string :tag "String    ")
                                (symbol :tag "Like other")))))
+(make-obsolete-variable 'tildify-string-alist
+                        'tildify-space-string "25.1")
 
 (defcustom tildify-ignored-environments-alist
   `((latex-mode
@@ -193,7 +204,7 @@ END-REGEX defines end of the corresponding text part and can be either:
 ;;;###autoload
 (defun tildify-region (beg end &optional dont-ask)
   "Add hard spaces in the region between BEG and END.
-See variables `tildify-pattern-alist', `tildify-string-alist', and
+See variables `tildify-pattern-alist', `tildify-space-string', and
 `tildify-ignored-environments-alist' for information about configuration
 parameters.
 This function performs no refilling of the changed text.
@@ -214,7 +225,7 @@ won't be prompted for confirmation of each substitution."
 ;;;###autoload
 (defun tildify-buffer (&optional dont-ask)
   "Add hard spaces in the current buffer.
-See variables `tildify-pattern-alist', `tildify-string-alist', and
+See variables `tildify-pattern-alist', `tildify-space-string', and
 `tildify-ignored-environments-alist' for information about configuration
 parameters.
 This function performs no refilling of the changed text.
@@ -303,7 +314,8 @@ replacements done and response is one of symbols: t (all right), nil
     (let* ((alist (tildify--pick-alist-entry tildify-pattern-alist))
 	   (regexp (car alist))
 	   (match-number (cadr alist))
-	   (tilde (tildify--pick-alist-entry tildify-string-alist))
+	   (tilde (or (tildify--pick-alist-entry tildify-string-alist)
+	              tildify-space-string))
 	   (end-marker (copy-marker end))
 	   answer
 	   bad-answer
diff --git a/test/automated/tildify-tests.el b/test/automated/tildify-tests.el
index 53c2e62..e532cf0 100644
--- a/test/automated/tildify-tests.el
+++ b/test/automated/tildify-tests.el
@@ -73,7 +73,7 @@ after `tildify-buffer' is run."
 (ert-deftest tildify-test-html ()
   "Tests tildification in an HTML document"
   (let* ((sentence (tildify-test--example-sentence " "))
-         (with-nbsp (tildify-test--example-sentence "&nbsp;")))
+         (with-nbsp (tildify-test--example-sentence " ")))
     (tildify-test--test '(html-mode sgml-mode)
                         (tildify-test--example-html sentence sentence)
                         (tildify-test--example-html sentence with-nbsp))))
@@ -81,7 +81,7 @@ after `tildify-buffer' is run."
 (ert-deftest tildify-test-xml ()
   "Tests tildification in an XML document"
   (let* ((sentence (tildify-test--example-sentence " "))
-         (with-nbsp (tildify-test--example-sentence "&#160;")))
+         (with-nbsp (tildify-test--example-sentence " ")))
     (tildify-test--test '(nxml-mode)
                         (tildify-test--example-html sentence sentence t)
                         (tildify-test--example-html sentence with-nbsp t))))
-- 
2.1.0.rc2.206.gedb03e5






  reply	other threads:[~2014-11-17 15:41 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-15  8:01 bug#18730: [PATCH] tildify.el: Add `auto-tildify' and `auto-tildify-mode' Michal Nazarewicz
2014-10-15 14:35 ` Stefan Monnier
2014-10-16  9:34   ` Michal Nazarewicz
2014-10-16 14:03     ` Stefan Monnier
2014-10-16 14:57       ` Stefan Monnier
2014-10-16 16:07       ` Michal Nazarewicz
2014-10-16 19:39         ` Stefan Monnier
2014-10-17  8:44           ` Michal Nazarewicz
2014-10-17 13:06             ` Stefan Monnier
2014-10-22 23:19               ` Michal Nazarewicz
2014-10-24 22:51                 ` Stefan Monnier
2014-10-28 22:01                   ` bug#18730: [PATCH] tildify.el: introduce a `tildify-space-string' variable Michal Nazarewicz
2014-10-30 16:27                     ` Stefan Monnier
2014-11-03 15:59                       ` Michal Nazarewicz
2014-11-03 17:00                         ` Stefan Monnier
2014-11-17 15:41                           ` Michal Nazarewicz [this message]
2014-11-17 15:41                             ` bug#18730: [PATCH 2/3] tildify.el: introduce a `tildify-pattern' variable Michal Nazarewicz
2014-11-17 15:41                             ` bug#18730: [PATCH 3/3] tildify.el: introduce a `tildify-foreach-region-function' variable Michal Nazarewicz
2014-11-17 17:38                             ` bug#18730: [PATCH 1/3] tildify.el: introduce a `tildify-space-string' variable Stefan Monnier
2014-10-16 13:17 ` bug#18730: [PATCH] tildify.el: Add `auto-tildify' and `auto-tildify-mode' Ted Zlatanov
2014-10-16 14:16   ` Michal Nazarewicz
2014-10-16 14:55     ` Stefan Monnier
2014-10-16 17:17       ` Ted Zlatanov
2014-10-16 13:19 ` Ted Zlatanov
2014-10-16 15:34 ` bug#18730: [PATCHv2 1/2] tildify.el (tildify--pick-alist-entry): rename from tildify-mode-alist Michal Nazarewicz
2014-10-16 15:34   ` bug#18730: [PATCHv2 2/2] tildify.el: Add `auto-tildify' and `auto-tildify-mode' Michal Nazarewicz
2014-10-16 19:30   ` bug#18730: [PATCHv2 1/2] tildify.el (tildify--pick-alist-entry): rename from tildify-mode-alist Stefan Monnier
2014-11-24 14:20 ` bug#18730: [PATCH 1/2] tildify.el: Add `tildify-space' and `tildify-mode' Michal Nazarewicz
2014-11-24 14:20   ` bug#18730: [PATCH 2/2] tildify.el: Add `tildify-double-space-undos' Michal Nazarewicz
2014-12-10 17:44   ` bug#18730: [PATCH 1/2] tildify.el: Add `tildify-space' and `tildify-mode' Michal Nazarewicz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1416238870-31725-1-git-send-email-mina86@mina86.com \
    --to=mina86@mina86.com \
    --cc=18730@debbugs.gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).