[Top][All Lists]

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

Re: c-style-alist

From: PJ Weisberg
Subject: Re: c-style-alist
Date: Sat, 25 Sep 2010 15:07:31 -0700

On Sat, Sep 25, 2010 at 7:02 AM, Andreas Röhler
<> wrote:
> Am 25.09.2010 13:54, schrieb Andrea Crotti:
>> I solved  making that mode a derived-mode from cc-mode.
>> Then I created another derived-mode for c++ files used by omnetpp, but
>> I'm struggling to make it automatically enabled.
>> It's very very simple
>> --8<---------------cut here---------------start------------->8---
>> (require 'derived)
>> (define-derived-mode cpp-omnet-mode c++-mode "C++ Omnet mode"
>>   "Major mode for editing c++ files used with omnet++"
>>   )
>> (provide 'cpp-omnet-mode)
>> --8<---------------cut here---------------end--------------->8---
>> and I thought I could do simply something like
>> --8<---------------cut here---------------start------------->8---
>>   ;; Look for the file .ini or the header inclusion
>>   (defun is-omnet-cpp-file ()
>>     (if
>>         (or (file-exists-p "omnetpp.ini")
>>             (search-forward "<omnetpp.h>"))
>>         (cpp-omnet-mode)))
>>   ;FIXME: Not working correctly yet, because it goes in infinite loop
>>   ;; (add-hook 'c++-mode-hook 'is-omnet-cpp-file)
>> --8<---------------cut here---------------end--------------->8---
>> But it's not fine, since it will evaluate infinitely this hook.
>> Another possibility would be to use "find-file-hook", but it doesn't
>> really make sense because the files possible are a subset of c++ files.
>> How can I make it non recurse keeping this?
>> Or some other suggestions?
> Maybe like this:
> (defun is-omnet-cpp-file ()
>  (or (cpp-omnet-mode)
>      (if
>          (or (file-exists-p "omnetpp.ini")
>              (search-forward "<omnetpp.h>"))
>          (cpp-omnet-mode))))
> There is no path with "omnetpp.ini", which however should not cause a
> infinite.

It looks to me like what's happening is that in the hook it switches
to cpp-omnet-mode, and since that's derived from c++-mode it results
in c++-mode-hooks getting run again, so it again switches to
cpp-omnet-mode and runs c++-mode-hooks, ad infinitum.

I'm not an expert and I'm sure there's a better way to do it, but one
way I could suggest is to set a buffer-local variable to mark that
you're switching to cpp-omnet-mode, and don't switch to cpp-omnet-mode
again if it's already set.

reply via email to

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