help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: distinguish .h files


From: Tassilo Horn
Subject: Re: distinguish .h files
Date: Thu, 21 Oct 2010 15:46:06 +0200
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/24.0.50 (gnu/linux)

Andrea Crotti <address@hidden> writes:

Hi Andrea,

>>> --8<---------------cut here---------------start------------->8---
>>> (defun c++-header-file-p ()
>>>    "Return non-nil, if in a C++ header."
>>>    (and (string-match "\\.h$"
>>>                  (or (buffer-file-name)
>>>                      (buffer-name)))
>>>         (save-excursion
>>>      (re-search-forward "\\_<class\\_>" nil t))))
>>>
>>> (add-to-list 'magic-mode-alist
>>>          '(c++-header-file-p . c++-mode))
>>> --8<---------------cut here---------------end--------------->8---
>>>
>> I use your code to get this value (add this after 'string-match'):
>>
>>         (message "%s" (- (point-max) (point-min)))
>>
>> I get 4000.
>
> I don't get your point...
> What's that useful for exactly?

It took me also some seconds to get the context again.  Oleksandr asked
in some previous posting how near to the buffer start the match for
(REGEX . MODE-FN) in magic-mode-alist has to occur.  The 4000 then
either means that Oleksandr's test file was exactly 4000 characters in
size (unlikely), or that the REGEXs (and MATCH-FNs) defined in
magic-mode-alist are checked with a buffer narrowed to the first 4000
characters (likely, *looking at the code*,..., confirmed!).

So my match function won't do the trick if the first "class" occurence
is after the first 4000 characters, maybe because of a long comment at
the start.  I guess, that restriction is made for performance reasons.
This one ignores the restriction and will even work if the first class
is declared after gigabytes of introductory text:

--8<---------------cut here---------------start------------->8---
(defun th-c++-header-file-p ()
  "Return non-nil, if in a C++ header."
  (and (string-match "\\.h$"
                     (or (buffer-file-name)
                         (buffer-name)))
       (save-excursion
         (save-restriction
           (widen)
           (re-search-forward "\\_<class\\_>" nil t)))))

(add-to-list 'magic-mode-alist
             '(th-c++-header-file-p . c++-mode))
--8<---------------cut here---------------end--------------->8---

Bye,
Tassilo




reply via email to

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