diff options
author | Lars Ingebrigtsen <larsi@gnus.org> | 2022-02-05 08:50:05 +0100 |
---|---|---|
committer | Lars Ingebrigtsen <larsi@gnus.org> | 2022-02-05 08:50:05 +0100 |
commit | 224aeb2877fffc894e9d1b31f7f44db0e55160c3 (patch) | |
tree | fea0b93f7f78a04e5d1d099aaf7cdbd2dd37c8a1 /lisp/yank-media.el | |
parent | 89d419255b662e6e4e916ecc2afa733a788e9b1e (diff) | |
download | emacs-224aeb2877fffc894e9d1b31f7f44db0e55160c3.tar.gz emacs-224aeb2877fffc894e9d1b31f7f44db0e55160c3.tar.bz2 emacs-224aeb2877fffc894e9d1b31f7f44db0e55160c3.zip |
Fix yank-media utf-16 detection
* lisp/yank-media.el (yank-media--utf-16-p): Factor out into its
own function for easier testing and fix the code.
Diffstat (limited to 'lisp/yank-media.el')
-rw-r--r-- | lisp/yank-media.el | 47 |
1 files changed, 20 insertions, 27 deletions
diff --git a/lisp/yank-media.el b/lisp/yank-media.el index 8c758777247..5cd75eb3186 100644 --- a/lisp/yank-media.el +++ b/lisp/yank-media.el @@ -155,33 +155,7 @@ non-supported selection data types." (format "%s" data)) ((string-match-p "\\`text/" (symbol-name data-type)) ;; We may have utf-16, which Emacs won't detect automatically. - (let ((coding-system - (and (zerop (mod (length data) 2)) - (let ((stats (vector 0 0))) - (dotimes (i (length data)) - (when (zerop (elt data i)) - (setf (aref stats (mod i 2)) - (1+ (aref stats (mod i 2)))))) - ;; We have some nuls... - (and (not (and (zerop (elt stats 0)) - (zerop (elt stats 1)))) - ;; If we have more than 90% every-other nul, then it's - ;; pretty likely to be utf-16. - (cond - ((> (if (zerop (elt stats 1)) - 1 - (/ (float (elt stats 0)) - (float (elt stats 1)))) - 0.9) - ;; Big endian. - 'utf-16-be) - ((> (if (zerop (elt stats 0)) - 1 - (/ (float (elt stats 1)) - (float (elt stats 0)))) - 0.9) - ;; Little endian. - 'utf-16-le))))))) + (let ((coding-system (yank-media--utf-16-p data))) (if coding-system (decode-coding-string data coding-system) ;; Some programs add a nul character at the end of text/* @@ -192,6 +166,25 @@ non-supported selection data types." (t data))) +(defun yank-media--utf-16-p (data) + (and (zerop (mod (length data) 2)) + (let ((stats (vector 0 0))) + (dotimes (i (length data)) + (when (zerop (elt data i)) + (setf (aref stats (mod i 2)) + (1+ (aref stats (mod i 2)))))) + ;; If we have more than 90% every-other nul, then it's + ;; pretty likely to be utf-16. + (cond + ((> (/ (float (elt stats 0)) (/ (length data) 2)) + 0.9) + ;; Big endian. + 'utf-16-be) + ((> (/ (float (elt stats 1)) (/ (length data) 2)) + 0.9) + ;; Little endian. + 'utf-16-le))))) + (provide 'yank-media) ;;; yank-media.el ends here |