Xah Talk Show 2025-02-04 Ep612 emacs lisp replace invisible char

vidthumb fZpjGHHC4NA
vidthumb fZpjGHHC4NA
(defun xah-replace-invisible-char-in-dir (Xdir Xregex)
  "Replace invisible chars in all files in a dir.
Similar to `xah-replace-invisible-char'

todo.
• by default, shouldn't remove many of the invisible chars. e.g. RIGHT-TO-LEFT MARK. it can damage the text. so, use with care or change the replace list
• if the file is not changed, close it
• add a option to save backup
• use with-temp-file instead find-file because the latter is super slow, it invoke syntax coloring and lots hooks. however, you lose the syntax coloring

Created: 2025-02-04
Version: 2025-02-04"
  (interactive)
  (let (xfilelist)
    (setq xfilelist (directory-files-recursively Xdir Xregex nil (lambda (x) (not (string-match-p "/\\." x)))))
    (mapc
     (lambda (x)
       (let ((case-replace nil)
             (case-fold-search nil)
             (xregex
              (regexp-opt
               (mapcar (lambda (x) (char-to-string (car x))) xah-replace-invisible-char-list)))
             xresult
             )
         (find-file x)
         (goto-char (point-min))
         (while (re-search-forward xregex nil t)
           (let (xcharId xname)
             (setq xcharId (string-to-char (match-string 0)))
             (setq xname (get-char-code-property xcharId 'name))
             (replace-match "")
             (push (vector xname xcharId (or buffer-file-name (buffer-name)) (point)) xresult)
             (overlay-put (make-overlay (point) (progn (forward-word) (point))) 'face 'font-lock-warning-face)))))
     xfilelist)))

;; (xah-replace-invisible-char-in-dir "c:/Users/xah/.emacs.d/temp/" "txt$")