unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: PJ Weisberg <pj@irregularexpressions.net>
To: help-gnu-emacs@gnu.org
Subject: Re: c-style-alist
Date: Sat, 25 Sep 2010 15:07:31 -0700	[thread overview]
Message-ID: <AANLkTi=v6_fdstxjf57DSGAJBUUg=_tJfYKgtjEK9Ey+@mail.gmail.com> (raw)
In-Reply-To: <4C9E00FF.9040105@easy-emacs.de>

On Sat, Sep 25, 2010 at 7:02 AM, Andreas Röhler
<andreas.roehler@easy-emacs.de> 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	other threads:[~2010-09-25 22:07 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-16 14:42 c-style-alist Andrea Crotti
2010-09-25 11:54 ` c-style-alist Andrea Crotti
2010-09-25 14:02   ` c-style-alist Andreas Röhler
2010-09-25 22:07     ` PJ Weisberg [this message]
2010-09-28  9:00       ` c-style-alist Andrea Crotti
2010-09-28 10:41         ` c-style-alist Andreas Röhler
     [not found]         ` <AANLkTinKPzz3aNY0e-7uOvK=3nN7Gs+dgf_3xv4+pLva@mail.gmail.com>
     [not found]           ` <B10177A4-D04A-417D-944C-F21564A9C4A9@gmail.com>
2010-09-29 22:02             ` c-style-alist PJ Weisberg
2010-10-06 14:13               ` c-style-alist Andrea Crotti
2010-10-06 21:40                 ` c-style-alist PJ Weisberg
2010-10-07 16:30                   ` c-style-alist Andrea Crotti

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='AANLkTi=v6_fdstxjf57DSGAJBUUg=_tJfYKgtjEK9Ey+@mail.gmail.com' \
    --to=pj@irregularexpressions.net \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).