emacs-devel
[Top][All Lists]
Advanced

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

Detecting the coding system of a file programmatically


From: Andrea Cardaci
Subject: Detecting the coding system of a file programmatically
Date: Fri, 10 Aug 2018 03:02:55 +0200

Hi,

I'm in a situation where I need to batch process (readonly) a number
of file with Emacs, my current approach is the following:

(with-temp-buffer
  (insert-file-contents-literally path)
  (decode-coding-region (point-min) (point-max) 'utf-8)
  (... do suff with the buffer ...))

I use `insert-file-contents-literally' because the non-literally
counterpart is too slow (about twice as much apparently) as it does a
bunch of stuff in addition to simply populate the buffer.
Unfortunately, one of these things is to decode the buffer.

Now instead of hardcoding 'utf-8 I'd like to detect the correct
encoding where possible, so I tried experimenting with
`find-operation-coding-system'. I created a latin-1 file (which gets
recognised properly when I visit it) and tried the following:

(with-temp-buffer
  (setq path "~/tmp/latin-1")
  (insert-file-contents-literally path)
  (find-operation-coding-system
   'insert-file-contents
   (cons path (current-buffer))))

But all I get is (undecided). Now my question is twofold: is this the
best approach for what I'm trying to achieve? And in any case, why
does the latter example does not work as expected? (And hence how I
can detect the coding system programmatically?)


Best,

Andrea



reply via email to

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