all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* distinguish .h files
@ 2010-10-20 15:37 Andrea Crotti
  2010-10-20 15:48 ` Lennart Borgman
  0 siblings, 1 reply; 18+ messages in thread
From: Andrea Crotti @ 2010-10-20 15:37 UTC (permalink / raw)
  To: help-gnu-emacs


It's a bit annoying that the header files .h BUT for c++ are not
automatically recognized (but of course understandable).

I tried the following approach but it doesn't work, maybe the function
is executed too early?
Or what else can be the problem?

--8<---------------cut here---------------start------------->8---
  (defun is-cpp-header-file ()
    "enable c++-mode when finding classes"
    (when (re-search-forward "class ")
      (c++-mode)))
  
  ;;(add-hook 'c-mode-common-hook 'is-cpp-header-file)
--8<---------------cut here---------------end--------------->8---




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

* Re: distinguish .h files
  2010-10-20 15:37 distinguish .h files Andrea Crotti
@ 2010-10-20 15:48 ` Lennart Borgman
  2010-10-21  9:23   ` Oleksandr Gavenko
       [not found]   ` <mailman.0.1287653038.19033.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 18+ messages in thread
From: Lennart Borgman @ 2010-10-20 15:48 UTC (permalink / raw)
  To: Andrea Crotti; +Cc: help-gnu-emacs

On Wed, Oct 20, 2010 at 5:37 PM, Andrea Crotti
<andrea.crotti.0@gmail.com> wrote:
>
> It's a bit annoying that the header files .h BUT for c++ are not
> automatically recognized (but of course understandable).
>
> I tried the following approach but it doesn't work, maybe the function
> is executed too early?
> Or what else can be the problem?
>
> --8<---------------cut here---------------start------------->8---
>  (defun is-cpp-header-file ()
>    "enable c++-mode when finding classes"
>    (when (re-search-forward "class ")
>      (c++-mode)))
>
>  ;;(add-hook 'c-mode-common-hook 'is-cpp-header-file)
> --8<---------------cut here---------------end--------------->8---

Please have a look at

  (info "(emacs) Choosing Modes")



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

* Re: distinguish .h files
       [not found] <mailman.0.1287589050.20659.help-gnu-emacs@gnu.org>
@ 2010-10-20 15:55 ` Richard Riley
  2010-10-20 16:46 ` Juhapekka Tolvanen
  1 sibling, 0 replies; 18+ messages in thread
From: Richard Riley @ 2010-10-20 15:55 UTC (permalink / raw)
  To: help-gnu-emacs

Andrea Crotti <andrea.crotti.0@gmail.com> writes:

> It's a bit annoying that the header files .h BUT for c++ are not
> automatically recognized (but of course understandable).
>
> I tried the following approach but it doesn't work, maybe the function
> is executed too early?
> Or what else can be the problem?
>
>
>
>
> --8<---------------cut here---------------start------------->8---
>   (defun is-cpp-header-file ()
>     "enable c++-mode when finding classes"
>     (when (re-search-forward "class ")
>       (c++-mode)))
>
>   ;;(add-hook 'c-mode-common-hook 'is-cpp-header-file)
> --8<---------------cut here---------------end--------------->8---
>

Something like this will do it for you - add the following as a first
line :-

/* -*-c++-*- */

regards

r.


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

* Re: distinguish .h files
       [not found] <mailman.0.1287589050.20659.help-gnu-emacs@gnu.org>
  2010-10-20 15:55 ` Richard Riley
@ 2010-10-20 16:46 ` Juhapekka Tolvanen
  1 sibling, 0 replies; 18+ messages in thread
From: Juhapekka Tolvanen @ 2010-10-20 16:46 UTC (permalink / raw)
  To: help-gnu-emacs



Andrea Crotti <andrea.crotti.0@gmail.com> writes:

> It's a bit annoying that the header files .h BUT for c++ are not
> automatically recognized (but of course understandable).

Why don't you name those files so that their name ends ".hh" instead of ".h"?


-- 
Juhapekka "naula" Tolvanen * http colon slash slash iki dot fi slash juhtolv
"Quidquid Latine dictum sit altum videtur."


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

* Re: distinguish .h files
  2010-10-20 15:48 ` Lennart Borgman
@ 2010-10-21  9:23   ` Oleksandr Gavenko
  2010-10-21 10:37     ` Tassilo Horn
       [not found]   ` <mailman.0.1287653038.19033.help-gnu-emacs@gnu.org>
  1 sibling, 1 reply; 18+ messages in thread
From: Oleksandr Gavenko @ 2010-10-21  9:23 UTC (permalink / raw)
  To: help-gnu-emacs

On 20.10.2010 18:48, Lennart Borgman wrote:
> On Wed, Oct 20, 2010 at 5:37 PM, Andrea Crotti
>>
>> It's a bit annoying that the header files .h BUT for c++ are not
>> automatically recognized (but of course understandable).
>>
>> I tried the following approach but it doesn't work, maybe the function
>> is executed too early?
>> Or what else can be the problem?
>>
>> --8<---------------cut here---------------start------------->8---
>>   (defun is-cpp-header-file ()
>>     "enable c++-mode when finding classes"
>>     (when (re-search-forward "class ")
>>       (c++-mode)))
>>
>>   ;;(add-hook 'c-mode-common-hook 'is-cpp-header-file)
>> --8<---------------cut here---------------end--------------->8---
>
> Please have a look at
>
>    (info "(emacs) Choosing Modes")
>
    Third, Emacs tries to determine the major mode by looking at the
text at the start of the buffer, based on the variable
`magic-mode-alist'.  By default, this variable is `nil' (an empty
list), so Emacs skips this step; however, you can customize it in your
init file

How long start of the buffer? Which variable control this behavior?




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

* Re: distinguish .h files
       [not found]   ` <mailman.0.1287653038.19033.help-gnu-emacs@gnu.org>
@ 2010-10-21 10:15     ` Pascal J. Bourguignon
  0 siblings, 0 replies; 18+ messages in thread
From: Pascal J. Bourguignon @ 2010-10-21 10:15 UTC (permalink / raw)
  To: help-gnu-emacs

Oleksandr Gavenko <gavenko@bifit.com.ua> writes:

> On 20.10.2010 18:48, Lennart Borgman wrote:
>> On Wed, Oct 20, 2010 at 5:37 PM, Andrea Crotti
>>>
>>> It's a bit annoying that the header files .h BUT for c++ are not
>>> automatically recognized (but of course understandable).
>>>
>>> I tried the following approach but it doesn't work, maybe the function
>>> is executed too early?
>>> Or what else can be the problem?
>>>
>>> --8<---------------cut here---------------start------------->8---
>>>   (defun is-cpp-header-file ()
>>>     "enable c++-mode when finding classes"
>>>     (when (re-search-forward "class ")
>>>       (c++-mode)))
>>>
>>>   ;;(add-hook 'c-mode-common-hook 'is-cpp-header-file)
>>> --8<---------------cut here---------------end--------------->8---
>>
>> Please have a look at
>>
>>    (info "(emacs) Choosing Modes")
>>
>    Third, Emacs tries to determine the major mode by looking at the
> text at the start of the buffer, based on the variable
> `magic-mode-alist'.  By default, this variable is `nil' (an empty
> list), so Emacs skips this step; however, you can customize it in your
> init file
>
> How long start of the buffer? Which variable control this behavior?

I would use find-file-hook.

(defun c++-.h-meat ()
  (interactive)
  (when (and (string= "h" (file-name-extension (buffer-file-name)))
             (progn (goto-char (point-min))
                    (re-search-forward "\\<class\\>")))
    (c++-mode)))

(add-hook 'find-file-hook 'c++-.h-meat)
    
-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


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

* Re: distinguish .h files
  2010-10-21  9:23   ` Oleksandr Gavenko
@ 2010-10-21 10:37     ` Tassilo Horn
  2010-10-21 10:43       ` Tassilo Horn
                         ` (2 more replies)
  0 siblings, 3 replies; 18+ messages in thread
From: Tassilo Horn @ 2010-10-21 10:37 UTC (permalink / raw)
  To: help-gnu-emacs

Oleksandr Gavenko <gavenko@bifit.com.ua> writes:

Hi Oleksandr,

>> Please have a look at
>>
>>    (info "(emacs) Choosing Modes")
>>
>    Third, Emacs tries to determine the major mode by looking at the
> text at the start of the buffer, based on the variable
> `magic-mode-alist'.  By default, this variable is `nil' (an empty
> list), so Emacs skips this step; however, you can customize it in your
> init file
>
> How long start of the buffer? Which variable control this behavior?

I don't know exactly, but in general only the first line is relevant,
with some shebang like

#!/bin/bash

But read a bit on:

,----[ (info "(emacs)Choosing Modes") ]
|    Alternatively, an element of `magic-mode-alist' may have the form
| 
|      (MATCH-FUNCTION . MODE-FUNCTION)
| 
| where MATCH-FUNCTION is a Lisp function that is called at the beginning
| of the buffer; if the function returns non-`nil', Emacs set the major
| mode wit MODE-FUNCTION.
`----

This should do the trick:

--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---

Bye,
Tassilo




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

* Re: distinguish .h files
  2010-10-21 10:37     ` Tassilo Horn
@ 2010-10-21 10:43       ` Tassilo Horn
  2010-10-21 11:04       ` Andrea Crotti
  2010-10-21 12:53       ` Oleksandr Gavenko
  2 siblings, 0 replies; 18+ messages in thread
From: Tassilo Horn @ 2010-10-21 10:43 UTC (permalink / raw)
  To: help-gnu-emacs

Tassilo Horn <tassilo@member.fsf.org> writes:

>>    Third, Emacs tries to determine the major mode by looking at the
>> text at the start of the buffer, based on the variable
>> `magic-mode-alist'.  By default, this variable is `nil' (an empty
>> list), so Emacs skips this step; however, you can customize it in
>> your init file
>>
>> How long start of the buffer? Which variable control this behavior?
>
> I don't know exactly, but in general only the first line is relevant,
> with some shebang like
>
> #!/bin/bash

Oh, just a correction: Usually shebang lines should be handled
automatically by `interpreter-mode-alist'.

Bye,
Tassilo




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

* Re: distinguish .h files
  2010-10-21 10:37     ` Tassilo Horn
  2010-10-21 10:43       ` Tassilo Horn
@ 2010-10-21 11:04       ` Andrea Crotti
  2010-10-21 12:53       ` Oleksandr Gavenko
  2 siblings, 0 replies; 18+ messages in thread
From: Andrea Crotti @ 2010-10-21 11:04 UTC (permalink / raw)
  To: help-gnu-emacs

Tassilo Horn <tassilo@member.fsf.org> writes:

>
>
> This should do the trick:
>
> (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))
>
> Bye,
> Tassilo

Great it works perfectly, now I have to understand how the heck I can
make "gtags" understand c++ code...
But that's another story...

Instead, is not possible somehow that when I say

public class X : public Y {

}

I get all the needed declarations automatically.
I didn't find anything useful in CEDET about that, but maybe there is something...




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

* Re: distinguish .h files
  2010-10-21 10:37     ` Tassilo Horn
  2010-10-21 10:43       ` Tassilo Horn
  2010-10-21 11:04       ` Andrea Crotti
@ 2010-10-21 12:53       ` Oleksandr Gavenko
  2010-10-21 12:56         ` Oleksandr Gavenko
  2010-10-21 12:56         ` Andrea Crotti
  2 siblings, 2 replies; 18+ messages in thread
From: Oleksandr Gavenko @ 2010-10-21 12:53 UTC (permalink / raw)
  To: help-gnu-emacs

On 21.10.2010 13:37, Tassilo Horn wrote:
> Oleksandr Gavenko<gavenko@bifit.com.ua>  writes:
>
> Hi Oleksandr,
>
>>> Please have a look at
>>>
>>>     (info "(emacs) Choosing Modes")
>>>
>>     Third, Emacs tries to determine the major mode by looking at the
>> text at the start of the buffer, based on the variable
>> `magic-mode-alist'.  By default, this variable is `nil' (an empty
>> list), so Emacs skips this step; however, you can customize it in your
>> init file
>>
>> How long start of the buffer? Which variable control this behavior?
>
> I don't know exactly,...
>
> --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.




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

* Re: distinguish .h files
  2010-10-21 12:53       ` Oleksandr Gavenko
@ 2010-10-21 12:56         ` Oleksandr Gavenko
  2010-10-21 12:56         ` Andrea Crotti
  1 sibling, 0 replies; 18+ messages in thread
From: Oleksandr Gavenko @ 2010-10-21 12:56 UTC (permalink / raw)
  To: help-gnu-emacs

On 21.10.2010 15:53, Oleksandr Gavenko wrote:
> On 21.10.2010 13:37, Tassilo Horn wrote:
>> Oleksandr Gavenko<gavenko@bifit.com.ua> writes:
>>
>> Hi Oleksandr,
>>
>>>> Please have a look at
>>>>
>>>> (info "(emacs) Choosing Modes")
>>>>
>>> Third, Emacs tries to determine the major mode by looking at the
>>> text at the start of the buffer, based on the variable
>>> `magic-mode-alist'. By default, this variable is `nil' (an empty
>>> list), so Emacs skips this step; however, you can customize it in your
>>> init file
>>>
>>> How long start of the buffer? Which variable control this behavior?
>>
>> I don't know exactly,...
>>
>> --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 get doc buffer for 'magic-mode-alist', go to source where this var defined
and search for keyword 'limit':

(defvar magic-mode-regexp-match-limit 4000
   "Upper limit on `magic-mode-alist' regexp matches.
Also applies to `magic-fallback-mode-alist'.")




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

* Re: distinguish .h files
  2010-10-21 12:53       ` Oleksandr Gavenko
  2010-10-21 12:56         ` Oleksandr Gavenko
@ 2010-10-21 12:56         ` Andrea Crotti
  2010-10-21 13:31           ` Oleksandr Gavenko
  2010-10-21 13:46           ` Tassilo Horn
  1 sibling, 2 replies; 18+ messages in thread
From: Andrea Crotti @ 2010-10-21 12:56 UTC (permalink / raw)
  To: help-gnu-emacs

Oleksandr Gavenko <gavenko@bifit.com.ua> writes:

>> I don't know exactly,...
>>
>> --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?




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

* Re: distinguish .h files
  2010-10-21 12:56         ` Andrea Crotti
@ 2010-10-21 13:31           ` Oleksandr Gavenko
  2010-10-21 13:42             ` Andrea Crotti
       [not found]             ` <mailman.1.1287669068.3931.help-gnu-emacs@gnu.org>
  2010-10-21 13:46           ` Tassilo Horn
  1 sibling, 2 replies; 18+ messages in thread
From: Oleksandr Gavenko @ 2010-10-21 13:31 UTC (permalink / raw)
  To: help-gnu-emacs

On 21.10.2010 15:56, Andrea Crotti wrote:
> Oleksandr Gavenko<gavenko@bifit.com.ua>  writes:
>
>>> I don't know exactly,...
>>>
>>> --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?
>
Suppose header firstly have a lot of '#define', so 'class' keyword
far from 'magic-mode-alist'. And .h file was opened in c-mode.

I look ALT/MFS headers (not all).
Usually 'class' matches near offset in 900 bytes.




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

* Re: distinguish .h files
  2010-10-21 13:31           ` Oleksandr Gavenko
@ 2010-10-21 13:42             ` Andrea Crotti
       [not found]             ` <mailman.1.1287669068.3931.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 18+ messages in thread
From: Andrea Crotti @ 2010-10-21 13:42 UTC (permalink / raw)
  To: help-gnu-emacs

Oleksandr Gavenko <gavenko@bifit.com.ua> writes:

>>
> Suppose header firstly have a lot of '#define', so 'class' keyword
> far from 'magic-mode-alist'. And .h file was opened in c-mode.
>
> I look ALT/MFS headers (not all).
> Usually 'class' matches near offset in 900 bytes.

Ok well I see your point, but actually I don't know what else really
distinguish c and c++ headers.
There are a c++ libraries which I could use, but every time they're
different.

For such big files that don't reach a class before quitting looking for
I can set it manually...




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

* Re: distinguish .h files
  2010-10-21 12:56         ` Andrea Crotti
  2010-10-21 13:31           ` Oleksandr Gavenko
@ 2010-10-21 13:46           ` Tassilo Horn
  2010-10-21 14:47             ` Andrea Crotti
  1 sibling, 1 reply; 18+ messages in thread
From: Tassilo Horn @ 2010-10-21 13:46 UTC (permalink / raw)
  To: help-gnu-emacs

Andrea Crotti <andrea.crotti.0@gmail.com> 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




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

* Re: distinguish .h files
       [not found]             ` <mailman.1.1287669068.3931.help-gnu-emacs@gnu.org>
@ 2010-10-21 13:58               ` Pascal J. Bourguignon
  0 siblings, 0 replies; 18+ messages in thread
From: Pascal J. Bourguignon @ 2010-10-21 13:58 UTC (permalink / raw)
  To: help-gnu-emacs

Andrea Crotti <andrea.crotti.0@gmail.com> writes:

> Oleksandr Gavenko <gavenko@bifit.com.ua> writes:
>
>>>
>> Suppose header firstly have a lot of '#define', so 'class' keyword
>> far from 'magic-mode-alist'. And .h file was opened in c-mode.
>>
>> I look ALT/MFS headers (not all).
>> Usually 'class' matches near offset in 900 bytes.
>
> Ok well I see your point, but actually I don't know what else really
> distinguish c and c++ headers.
> There are a c++ libraries which I could use, but every time they're
> different.
>
> For such big files that don't reach a class before quitting looking for
> I can set it manually...

By the way, auto-mode-alist regular expressions are applied on the whole
pathname, so you could specify that the .h that are in some directories
are actually C++.

(push '("\\(/usr/include/g++/\\|/home/myself/src/c++project/\\).*\\.h\\'"
        . c++-mode)
      auto-mode-alist)


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


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

* Re: distinguish .h files
  2010-10-21 13:46           ` Tassilo Horn
@ 2010-10-21 14:47             ` Andrea Crotti
  2010-10-21 15:21               ` Tassilo Horn
  0 siblings, 1 reply; 18+ messages in thread
From: Andrea Crotti @ 2010-10-21 14:47 UTC (permalink / raw)
  To: help-gnu-emacs

Tassilo Horn <tassilo@member.fsf.org> writes:

>
> 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:
>
> (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))
>
> Bye,
> Tassilo

Yes well but the limit is not so wrong.
I mean I prefer to manually (eventually because if I only read I don't
care so much) switch the mode instead of waiting 10 seconds for getting
the file open.

I don't know maybe it's very fast but I think that in general is correct...




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

* Re: distinguish .h files
  2010-10-21 14:47             ` Andrea Crotti
@ 2010-10-21 15:21               ` Tassilo Horn
  0 siblings, 0 replies; 18+ messages in thread
From: Tassilo Horn @ 2010-10-21 15:21 UTC (permalink / raw)
  To: help-gnu-emacs

Andrea Crotti <andrea.crotti.0@gmail.com> writes:

Hi Andrea,

>> 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:
>>
>> (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)))))
>
> Yes well but the limit is not so wrong.

No, at least not for ancient computers.

> I mean I prefer to manually (eventually because if I only read I don't
> care so much) switch the mode instead of waiting 10 seconds for
> getting the file open.

I've just tried the worst case I can produce on my computer (core duo
with 2.1 GHz) by opening a 660kb C-header file.  That's worst case
because it's the largest header I could find and because it is plain C
and not C++, so the function above will search the whole buffer.  But
still I cannot perceive any hang or latency...

But the better way is to use the original function definition and
enlarge `magic-mode-regexp-match-limit' to some higher value like 50000
or so...

Bye,
Tassilo




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

end of thread, other threads:[~2010-10-21 15:21 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-10-20 15:37 distinguish .h files Andrea Crotti
2010-10-20 15:48 ` Lennart Borgman
2010-10-21  9:23   ` Oleksandr Gavenko
2010-10-21 10:37     ` Tassilo Horn
2010-10-21 10:43       ` Tassilo Horn
2010-10-21 11:04       ` Andrea Crotti
2010-10-21 12:53       ` Oleksandr Gavenko
2010-10-21 12:56         ` Oleksandr Gavenko
2010-10-21 12:56         ` Andrea Crotti
2010-10-21 13:31           ` Oleksandr Gavenko
2010-10-21 13:42             ` Andrea Crotti
     [not found]             ` <mailman.1.1287669068.3931.help-gnu-emacs@gnu.org>
2010-10-21 13:58               ` Pascal J. Bourguignon
2010-10-21 13:46           ` Tassilo Horn
2010-10-21 14:47             ` Andrea Crotti
2010-10-21 15:21               ` Tassilo Horn
     [not found]   ` <mailman.0.1287653038.19033.help-gnu-emacs@gnu.org>
2010-10-21 10:15     ` Pascal J. Bourguignon
     [not found] <mailman.0.1287589050.20659.help-gnu-emacs@gnu.org>
2010-10-20 15:55 ` Richard Riley
2010-10-20 16:46 ` Juhapekka Tolvanen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.