emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

fixing url-unhex-string for unicode/multi-byte charsets


From: Boruch Baum
Subject: fixing url-unhex-string for unicode/multi-byte charsets
Date: Fri, 6 Nov 2020 02:47:42 -0500
User-agent: NeoMutt/20180716

In the thread "Friendlier dired experience", Michael Albinus noted that
the new emacs feature to place remote files in the local trash performs
hex-encoding on remote file-names as if they were URLs, which led me to
discover that was also happening for local files encoded in multi-byte
(eg. unicode) character-set encodings. Neither of these cases were being
properly handled by the current emacs function `url-unhex-string'. We
noticed this for the case of restoring a trashed file, but it can be
expected to exhibit in other cases.

I've solved the problem for diredc, using code from the emacs-w3m
project (thanks). Whether for the general emacs case it should be
handled by altering function `url-unhex-string', or whether a second
function should be created isn't for me to decide, so here's my fix for
you to discuss, decide, apply.

--8<--cut here-(start)------------------------------------------- >8
(defun diredc--decode-hexlated-string (str)
  "Convert hexlated string to human-readable, with charset coding support.
This function improves upon `url-unhex-string' by handled
hexlated multi-byte and unicode characters. Credit to the
`emacs-w3m' project for the core-code, at
`w3m-url-decode-string'."
  ;; NOTE: This technique should be used by `url-unhex-string' itself,
  ;;       or integrated otherwise into emacs.
  (let ((start 0)
        (case-fold-search t)
        (regexp "%\\(?:\\([0-9a-f][0-9a-f]\\)\\|0d%0a\\)"))
    (with-temp-buffer
      (set-buffer-multibyte nil)
      (while (string-match regexp str start)
        (insert (substring str start (match-beginning 0))
                   (if (match-beginning 1)
                      (string-to-number (match-string 1 str) 16)
                    ?\n))
      (setq start (match-end 0)))
      (insert (substring str start))
      (decode-coding-string
        (buffer-string)
        (with-coding-priority nil
               (car (detect-coding-region (point-min) (point-max))))))))
--8<--cut here-(end)--------------------------------------------- >8

--
hkp://keys.gnupg.net
CA45 09B5 5351 7C11 A9D1  7286 0036 9E45 1595 8BC0



reply via email to

[Prev in Thread] Current Thread [Next in Thread]