unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* automatically turn on hexl-mode
@ 2003-06-04 21:27 Kin Cho
  2003-06-04 23:37 ` Pascal Bourguignon
  2003-06-05 15:27 ` Benjamin Riefenstahl
  0 siblings, 2 replies; 4+ messages in thread
From: Kin Cho @ 2003-06-04 21:27 UTC (permalink / raw)


Hi,

In my find-file-hooks function, I want to turn on hexl-mode
automatically based on content.  I.e.,

(and (buffer-content-looks-binary) (hexl-mode))

What's a reasonable implementation of buffer-content-looks-binary?

-kin

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: automatically turn on hexl-mode
  2003-06-04 21:27 automatically turn on hexl-mode Kin Cho
@ 2003-06-04 23:37 ` Pascal Bourguignon
  2003-06-05 15:27 ` Benjamin Riefenstahl
  1 sibling, 0 replies; 4+ messages in thread
From: Pascal Bourguignon @ 2003-06-04 23:37 UTC (permalink / raw)



Kin Cho <kin@neoscale.com> writes:

> Hi,
> 
> In my find-file-hooks function, I want to turn on hexl-mode
> automatically based on content.  I.e.,
> 
> (and (buffer-content-looks-binary) (hexl-mode))
> 
> What's a reasonable implementation of buffer-content-looks-binary?
> 
> -kin

There's no universal reasonable implementation for that function.

If your non binary files are  pure ASCII, then you could check for the
presence  of character  greater than  127.  If  they're  ISO-8859, you
could check  for the presence of  characters between 128  and 144. For
virtally all  the other encoding,  you can't say anything  because all
the codes are used.  


You may trust file: 

(defun buffer-content-looks-binary ()
    (file-content-looks-binary (buffer-file-name)))

(defun file-content-looks-binary (filename)
    (/= 0 (shell-command 
            (format "file %s | grep -q -s text" 
                    (shell-quote-argument filename)))))





-- 
__Pascal_Bourguignon__                   http://www.informatimago.com/
----------------------------------------------------------------------
Do not adjust your mind, there is a fault in reality.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: automatically turn on hexl-mode
  2003-06-04 21:27 automatically turn on hexl-mode Kin Cho
  2003-06-04 23:37 ` Pascal Bourguignon
@ 2003-06-05 15:27 ` Benjamin Riefenstahl
  2003-06-05 18:10   ` Kin Cho
  1 sibling, 1 reply; 4+ messages in thread
From: Benjamin Riefenstahl @ 2003-06-05 15:27 UTC (permalink / raw)


Hi Kin,


Kin Cho <kin@neoscale.com> writes:
> In my find-file-hooks function, I want to turn on hexl-mode
> automatically based on content.  I.e.,
>
> (and (buffer-content-looks-binary) (hexl-mode))
>
> What's a reasonable implementation of buffer-content-looks-binary?

<philosophical>
It's not easy to define formally what "binary" is.  Is an MS Word
document binary?  An UTF-16 document?  A gzipped text file?  And all
of these can be shown in Emacs, even though some of them need external
helpers.  Also for some purposes the actual question behind "is it
binary?" may be different than for others.  E.g. what CVS actually
needs to know is "Can I change the line ends and expand RCS keywords?"
</philosophical>

I recently had to find all binary files in a development project, a
collection of 13186 files.  Several methods said that there were 897
binary files in between.  The simplest and still reliable criterium
was to just look for a NUL byte in the first 1K of the file.


Hope this helps, benny

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: automatically turn on hexl-mode
  2003-06-05 15:27 ` Benjamin Riefenstahl
@ 2003-06-05 18:10   ` Kin Cho
  0 siblings, 0 replies; 4+ messages in thread
From: Kin Cho @ 2003-06-05 18:10 UTC (permalink / raw)


Hi,

> The simplest and still reliable criterium was to just look for
> a NUL byte in the first 1K of the file.

Coincidentally, I settled on this heuristic as well.

-kin

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-06-05 18:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-06-04 21:27 automatically turn on hexl-mode Kin Cho
2003-06-04 23:37 ` Pascal Bourguignon
2003-06-05 15:27 ` Benjamin Riefenstahl
2003-06-05 18:10   ` Kin Cho

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).