Subject: Re: bug#35689: Customizable char-fold

From: Juri Linkov <[email-masked]>
Date: Tue, 21 May 2019 23:34:20 +0300

>>> I can't find a standard way of doing this.  So instead of using eval-and-compile
>>> I'll try to recalculate the value explicitly when variables are customized:
>>>
>>>   (when (or (get 'char-fold-include-base  'customized-value)
>>>             (get 'char-fold-include-alist 'customized-value)
>>>             (get 'char-fold-exclude-alist 'customized-value))
>>>     (setq char-fold-table (char-fold-make-table)))
>>
>> Instead of looking at symbol property values, which can make for a
>> confusing time when setting variables outside of customize, I think it
>> would be nicer to do something like this:
>>
>>     (defcustom char-fold-include-base char-fold--include-base-default
>>       :initialize #'custom-initialize-changed
>>       :set (lambda (sym val)
>>              (set-default sym val)
>>              ;; FIXME: Maybe delay this until after-init-time,
>>              ;; to avoid redundant calls to char-fold-make-table.
>
> I tried different possible values of :initialize,
> but not custom-initialize-changed.  I'll try this now.

I see no problems other than redundant calls to char-fold-make-table
when more than one variable is customized.  char-fold.el is autoloaded
when isearch calls char-fold-to-regexp for the first time, so I'm not
sure how after-init-hook could help in this case.

diff --git a/lisp/char-fold.el b/lisp/char-fold.el
index 426b1a9f84..16d6d484f0 100644
--- a/lisp/char-fold.el
+++ b/lisp/char-fold.el
@@ -21,13 +21,22 @@
 
 ;;; Code:
 
-(eval-and-compile (put 'char-fold-table 'char-table-extra-slots 1))
+(eval-and-compile
+  (put 'char-fold-table 'char-table-extra-slots 1)
+  (defconst char-fold--symmetric-default nil)
+  (defconst char-fold--include-alist-default
+    '((?\" "＂" "“" "”" "”" "„" "⹂" "〞" "‟" "‟" "❞" "❝" "❠" "“" "„" "〝" "〟" "🙷" "🙶" "🙸" "«" "»")
+      (?' "❟" "❛" "❜" "‘" "’" "‚" "‛" "‚" "󠀢" "❮" "❯" "‹" "›")
+      (?` "❛" "‘" "‛" "󠀢" "❮" "‹")))
+  (defconst char-fold--exclude-alist-default nil))
+
 
-(defconst char-fold-table
-  (eval-when-compile
-    (let ((equiv (make-char-table 'char-fold-table))
-          (equiv-multi (make-char-table 'char-fold-table))
-          (table (unicode-property-table-internal 'decomposition)))
+(eval-and-compile
+  (defun char-fold-make-table ()
+    (let* ((equiv (make-char-table 'char-fold-table))
+           (equiv-multi (make-char-table 'char-fold-table))
+           (search-spaces-regexp nil) ; bug#35802
+           (table (unicode-property-table-internal 'decomposition)))
       (set-char-table-extra-slot equiv 0 equiv-multi)
 
       ;; Ensure the table is populated.
@@ -75,7 +84,12 @@ char-fold-table
                                       (aref equiv-multi (car decomp))))
                         (aset equiv (car decomp)
                               (cons (char-to-string char)
-                                    (aref equiv (car decomp))))))))
+                                    (aref equiv (car decomp))))
+                        (when (or (bound-and-true-p char-fold-symmetric)
+                                  char-fold--symmetric-default)
+                          (aset equiv char
+                                (cons (char-to-string (car decomp))
+                                      (aref equiv (car decomp)))))))))
                (funcall make-decomp-match-char decomp char)
                ;; Do it again, without the non-spacing characters.
                ;; This allows 'a' to match 'ä'.
@@ -97,13 +111,20 @@ char-fold-table
        table)
 
       ;; Add some manual entries.
-      (dolist (it '((?\" "＂" "“" "”" "”" "„" "⹂" "〞" "‟" "‟" "❞" "❝" "❠" "“" "„" "〝" "〟" "🙷" "🙶" "🙸" "«" "»")
-                    (?' "❟" "❛" "❜" "‘" "’" "‚" "‛" "‚" "󠀢" "❮" "❯" "‹" "›")
-                    (?` "❛" "‘" "‛" "󠀢" "❮" "‹")))
+      (dolist (it (or (bound-and-true-p char-fold-include-alist)
+                      char-fold--include-alist-default))
         (let ((idx (car it))
               (chars (cdr it)))
           (aset equiv idx (append chars (aref equiv idx)))))
 
+      ;; Remove some entries.
+      (dolist (it (or (bound-and-true-p char-fold-exclude-alist)
+                      char-fold--exclude-alist-default))
+        (let ((idx (car it))
+              (char (cdr it)))
+          (when (aref equiv idx)
+            (aset equiv idx (remove (char-to-string char) (aref equiv idx))))))
+
       ;; Convert the lists of characters we compiled into regexps.
       (map-char-table
        (lambda (char dec-list)
@@ -112,7 +133,11 @@ char-fold-table
                (set-char-table-range equiv char re)
              (aset equiv char re))))
        equiv)
-      equiv))
+      equiv)))
+
+(defconst char-fold-table
+  (eval-when-compile
+    (char-fold-make-table))
   "Used for folding characters of the same group during search.
 This is a char-table with the `char-fold-table' subtype.
 
@@ -135,6 +160,40 @@ char-fold-table
 
 Exceptionally for the space character (32), ALIST is ignored.")
 
+(defcustom char-fold-symmetric char-fold--symmetric-default
+  "Include symmetric mappings from composite character back to base letter."
+  :type 'boolean
+  :initialize #'custom-initialize-changed
+  :set (lambda (sym val)
+         (set-default sym val)
+         ;; FIXME: Maybe delay this until after-init-hook,
+         ;; to avoid redundant calls to char-fold-make-table.
+         (setq char-fold-table (char-fold-make-table)))
+  :group 'matching
+  :version "27.1")
+
+(defcustom char-fold-include-alist char-fold--include-alist-default
+  "Additional character mappings to include."
+  :type '(alist :key-type (character :tag "From")
+                :value-type (repeat (string :tag "To")))
+  :initialize #'custom-initialize-changed
+  :set (lambda (sym val)
+         (set-default sym val)
+         (setq char-fold-table (char-fold-make-table)))
+  :group 'lisp
+  :version "27.1")
+
+(defcustom char-fold-exclude-alist char-fold--exclude-alist-default
+  "Character mappings to exclude from default setting."
+  :type '(alist :key-type (character :tag "From")
+                :value-type (character :tag "To"))
+  :initialize #'custom-initialize-changed
+  :set (lambda (sym val)
+         (set-default sym val)
+         (setq char-fold-table (char-fold-make-table)))
+  :group 'lisp
+  :version "27.1")
+
 (defun char-fold--make-space-string (n)
   "Return a string that matches N spaces."
   (format "\\(?:%s\\|%s\\)"