Subject: Re: bug#36085: 26.2; find-dired octal escapes instead of Cyrillic text

From: Visuwesh <[email-masked]>
Date: Sun, 13 Mar 2022 11:35:01 +0530

[சனி, ஜூன் 08 2019] Eli Zaretskii wrote:

Hi Eli,

>> From: Mattias Engdegård <[email-masked]>
>> Date: Sat, 8 Jun 2019 17:14:11 +0200
>> 
>> Eli wrote:
>> 
>> > P.S. Emacs could perhaps go above and beyond the call of duty, and
>> > attempt to convert the octal escapes back to readable text. But I
>> > don't think we should do it, as it's a clear bug in
>> > 'find'. Nonetheless, if someone wants to submit patches to do such
>> > a conversion, I won't block them.
>> 
>> The default (BSD) find in macOS does not seem to escape anything;
>> files named Портрет or APL\360 are printed exactly that way. Thus,
>> Emacs would need to know what 'find' it is running. This appears to
>> validate your recommendation.
>
> Indeed, the hard part is to distinguish between \nnn an octal escape
> and the literal string "\nnn".  That difficulty is one reason why
> gdb-mi.el performs a similar decoding only as an opt-in optional
> behavior.

After being annoyed by the same exact behaviour, and with the helpful
hint about gdb-mi.el, I came up with the following function.  With a
preliminary testing, it does not choke on literal "\nnn" and it does not
noticeably slow down find-dired unlike the xargs option.  Maybe, we can
include something like this, WDYT?

    (defun vz/find-dired-unescape ()
      "Unescape the C-style octal escape strings."
      (while (not (eobp))
        (when-let ((beg (next-single-property-change (point) 'dired-filename))
                   (props (text-properties-at beg)))
          (goto-char beg)
          (while (and (re-search-forward (rx "\\" (group (any "0-7") (? (any "0-7") (? (any "0-7")))))
                                         (line-end-position) 'noerror)
                      (not (eq (char-before (match-beginning 0)) ?\\)))
            (let ((num (string-to-number (match-string 1) 8)))
              (replace-match (unibyte-string num) t nil nil 0)))
          (decode-coding-region beg (line-end-position) buffer-file-coding-system)
          (set-text-properties  beg (line-end-position) props))
        (forward-line)))

    (custom-set-variables
     '(find-ls-option (cons "-ls" "-dlis"))
     '(find-dired-refine-function #'vz/find-dired-unescape))