all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Emacs . How can I associate *.cu files as cc mode as a default ?
@ 2007-09-19  0:10 Mike H
  2007-09-19  0:28 ` Tyler Smith
  0 siblings, 1 reply; 14+ messages in thread
From: Mike H @ 2007-09-19  0:10 UTC (permalink / raw)
  To: help-gnu-emacs

Emacs . How can I associate *.cu files as cc mode as a default ?
cc mode is automatically triggered when I open *.c, *.cpp files.
However, I would like to add the new file type :)

Thank you!

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

* Re: Emacs . How can I associate *.cu files as cc mode as a default ?
  2007-09-19  0:10 Emacs . How can I associate *.cu files as cc mode as a default ? Mike H
@ 2007-09-19  0:28 ` Tyler Smith
  2007-09-19  1:09   ` Mike H
  2007-09-19  6:24   ` Gordon Beaton
  0 siblings, 2 replies; 14+ messages in thread
From: Tyler Smith @ 2007-09-19  0:28 UTC (permalink / raw)
  To: help-gnu-emacs

On 2007-09-19, Mike H <hspnew@gmail.com> wrote:
> Emacs . How can I associate *.cu files as cc mode as a default ?
> cc mode is automatically triggered when I open *.c, *.cpp files.
> However, I would like to add the new file type :)

You can do this with find-file-hooks. This is code that runs anytime
you find (open) a file. In this case, you want to check and see if the
file you've found ends with .cu, and if so, to use cc-mode. This
function will do that:

(defun my-find-file-hook()
  (let ((fn (buffer-file-name)))
    (when (string-match "\\.cu$" fn)
      (cc-mode))))

You have to tell Emacs to run this function every time you open a new
file, so you add the hook: 

(add-hook 'find-file-hooks 'my-find-file-hook)

Put both forms in your .emacs, and the next time you open emacs you'll
be all ready to go. (you can also load them into a running emacs using
C-x C-e to execute the previous command in a lisp-mode buffer, such as
scratch or .emacs).

HTH,

Tyler

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

* Re: Emacs . How can I associate *.cu files as cc mode as a default ?
  2007-09-19  0:28 ` Tyler Smith
@ 2007-09-19  1:09   ` Mike H
  2007-09-19  1:25     ` Mike H
  2007-09-19  6:24   ` Gordon Beaton
  1 sibling, 1 reply; 14+ messages in thread
From: Mike H @ 2007-09-19  1:09 UTC (permalink / raw)
  To: help-gnu-emacs

On Sep 18, 8:28 pm, Tyler Smith <tyler.sm...@mail.mcgill.ca> wrote:
> On 2007-09-19, Mike H <hsp...@gmail.com> wrote:
>
> > Emacs . How can I associate *.cu files as cc mode as a default ?
> > cc mode is automatically triggered when I open *.c, *.cpp files.
> > However, I would like to add the new file type :)
>
> You can do this with find-file-hooks. This is code that runs anytime
> you find (open) a file. In this case, you want to check and see if the
> file you've found ends with .cu, and if so, to use cc-mode. This
> function will do that:
>
> (defun my-find-file-hook()
>   (let ((fn (buffer-file-name)))
>     (when (string-match "\\.cu$" fn)
>       (cc-mode))))
>
> You have to tell Emacs to run this function every time you open a new
> file, so you add the hook:
>
> (add-hook 'find-file-hooks 'my-find-file-hook)
>
> Put both forms in your .emacs, and the next time you open emacs you'll
> be all ready to go. (you can also load them into a running emacs using
> C-x C-e to execute the previous command in a lisp-mode buffer, such as
> scratch or .emacs).
>
> HTH,
>
> Tyler


Thank you !  I really appreciate it !

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

* Re: Emacs . How can I associate *.cu files as cc mode as a default ?
  2007-09-19  1:09   ` Mike H
@ 2007-09-19  1:25     ` Mike H
  2007-09-19  2:01       ` Tyler Smith
  0 siblings, 1 reply; 14+ messages in thread
From: Mike H @ 2007-09-19  1:25 UTC (permalink / raw)
  To: help-gnu-emacs

Just one thing ,

When I put those scripts in, and when I open *.cu files,
I get the following...

Symbol's function definition is void


Any solutions? :)

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

* Re: Emacs . How can I associate *.cu files as cc mode as a default ?
  2007-09-19  1:25     ` Mike H
@ 2007-09-19  2:01       ` Tyler Smith
  2007-09-19  2:04         ` Mike H
  0 siblings, 1 reply; 14+ messages in thread
From: Tyler Smith @ 2007-09-19  2:01 UTC (permalink / raw)
  To: help-gnu-emacs

On 2007-09-19, Mike H <hspnew@gmail.com> wrote:
> Just one thing ,
>
> When I put those scripts in, and when I open *.cu files,
> I get the following...
>
> Symbol's function definition is void
>

It should be (c-mode), not (cc-mode). The following works for
me:

(defun my-find-file-hook()
  (let ((fn (buffer-file-name)))
    (when (string-match "\\.cu$" fn)
      (c-mode))))
(add-hook 'find-file-hooks 'my-find-file-hook)

Tyler

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

* Re: Emacs . How can I associate *.cu files as cc mode as a default ?
  2007-09-19  2:01       ` Tyler Smith
@ 2007-09-19  2:04         ` Mike H
  2007-09-19 15:13           ` Richard G Riley
  0 siblings, 1 reply; 14+ messages in thread
From: Mike H @ 2007-09-19  2:04 UTC (permalink / raw)
  To: help-gnu-emacs

On Sep 18, 10:01 pm, Tyler Smith <tyler.sm...@mail.mcgill.ca> wrote:
> On 2007-09-19, Mike H <hsp...@gmail.com> wrote:
>
> > Just one thing ,
>
> > When I put those scripts in, and when I open *.cu files,
> > I get the following...
>
> > Symbol's function definition is void
>
> It should be (c-mode), not (cc-mode). The following works for
> me:
>
> (defun my-find-file-hook()
>   (let ((fn (buffer-file-name)))
>     (when (string-match "\\.cu$" fn)
>       (c-mode))))
> (add-hook 'find-file-hooks 'my-find-file-hook)
>
> Tyler


Thanks a lot !! it works now

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

* Re: Emacs . How can I associate *.cu files as cc mode as a default ?
  2007-09-19  0:28 ` Tyler Smith
  2007-09-19  1:09   ` Mike H
@ 2007-09-19  6:24   ` Gordon Beaton
  1 sibling, 0 replies; 14+ messages in thread
From: Gordon Beaton @ 2007-09-19  6:24 UTC (permalink / raw)
  To: help-gnu-emacs

On 19 Sep 2007 00:28:09 GMT, Tyler Smith wrote:
> This function will do that:
>
> (defun my-find-file-hook()
>   (let ((fn (buffer-file-name)))
>     (when (string-match "\\.cu$" fn)
>       (cc-mode))))
>
> You have to tell Emacs to run this function every time you open a new
> file, so you add the hook:
>
> (add-hook 'find-file-hooks 'my-find-file-hook)


No need for the function. Just add the extension to auto-mode-alist:

      (add-to-list 'auto-mode-alist '("\\.cu$" . c-mode))

/gordon

--

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

* Re: Emacs . How can I associate *.cu files as cc mode as a default ?
  2007-09-19  2:04         ` Mike H
@ 2007-09-19 15:13           ` Richard G Riley
  2007-09-19 15:47             ` Tyler Smith
  0 siblings, 1 reply; 14+ messages in thread
From: Richard G Riley @ 2007-09-19 15:13 UTC (permalink / raw)
  To: help-gnu-emacs

Mike H <hspnew@gmail.com> writes:

> On Sep 18, 10:01 pm, Tyler Smith <tyler.sm...@mail.mcgill.ca> wrote:
>> On 2007-09-19, Mike H <hsp...@gmail.com> wrote:
>>
>> > Just one thing ,
>>
>> > When I put those scripts in, and when I open *.cu files,
>> > I get the following...
>>
>> > Symbol's function definition is void
>>
>> It should be (c-mode), not (cc-mode). The following works for
>> me:
>>
>> (defun my-find-file-hook()
>>   (let ((fn (buffer-file-name)))
>>     (when (string-match "\\.cu$" fn)
>>       (c-mode))))
>> (add-hook 'find-file-hooks 'my-find-file-hook)
>>
>> Tyler
>
>
> Thanks a lot !! it works now

See the other reply. It is a better way:

      (add-to-list 'auto-mode-alist '("\\.cu$" . c-mode))

find-file-hooks is obsolete in newer versions of emacs.

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

* Re: Emacs . How can I associate *.cu files as cc mode as a default ?
  2007-09-19 15:13           ` Richard G Riley
@ 2007-09-19 15:47             ` Tyler Smith
  2007-09-19 16:15               ` Gordon Beaton
  2007-09-19 19:46               ` Stefan Monnier
  0 siblings, 2 replies; 14+ messages in thread
From: Tyler Smith @ 2007-09-19 15:47 UTC (permalink / raw)
  To: help-gnu-emacs

On 2007-09-19, Richard G Riley <rileyrgdev@googlemail.com> wrote:
>> On Sep 18, 10:01 pm, Tyler Smith <tyler.sm...@mail.mcgill.ca> wrote:
>>> It should be (c-mode), not (cc-mode). The following works for
>>> me:
>>>
>>> (defun my-find-file-hook()
>>>   (let ((fn (buffer-file-name)))
>>>     (when (string-match "\\.cu$" fn)
>>>       (c-mode))))
>>> (add-hook 'find-file-hooks 'my-find-file-hook)
>
> See the other reply. It is a better way:
>
>       (add-to-list 'auto-mode-alist '("\\.cu$" . c-mode))
>
> find-file-hooks is obsolete in newer versions of emacs.

Thanks for the correction. I see now that I had a few 'auto-mode-alist
lines in my .emacs serving this purpose, but I didn't really
understand them.

Reading the manual, I see that find-file-hooks is now obsolete - will
find-file-hook be obsolete also? What should I replace the following
code with:

(defun my-find-file-hook()
  (let ((fn (buffer-file-name)))
    (when (string-match "latex2rtf" fn)
      (set-variable 'tab-width 4))))
(add-hook 'find-file-hook 'my-find-file-hook)

I use this because a project I'm working on uses a tab-width of 4 for
formatting their c code. I take it that's not ideal, but it's not my
decision. Anyways, all the files are in a directory called latex2rtf,
so this hook does what I need. What is the preferred way to accomplish
this?

Thanks,

Tyler

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

* Re: Emacs . How can I associate *.cu files as cc mode as a default ?
  2007-09-19 15:47             ` Tyler Smith
@ 2007-09-19 16:15               ` Gordon Beaton
  2007-09-19 17:55                 ` Tyler Smith
  2007-09-19 19:46               ` Stefan Monnier
  1 sibling, 1 reply; 14+ messages in thread
From: Gordon Beaton @ 2007-09-19 16:15 UTC (permalink / raw)
  To: help-gnu-emacs

On 19 Sep 2007 15:47:19 GMT, Tyler Smith wrote:
> (defun my-find-file-hook()
>   (let ((fn (buffer-file-name)))
>     (when (string-match "latex2rtf" fn)
>       (set-variable 'tab-width 4))))
> (add-hook 'find-file-hook 'my-find-file-hook)
>
> I use this because a project I'm working on uses a tab-width of 4
> for formatting their c code. I take it that's not ideal, but it's
> not my decision. Anyways, all the files are in a directory called
> latex2rtf, so this hook does what I need. What is the preferred way
> to accomplish this?

If you want to do this for all files of that type, then c-mode-hook is
a better place to put the function, which doesn't need to check the
filename:

  (add-hook 'c-mode-hook (lambda() (setq c-basic-offset 4)))

Alternatively, use file variables to save settings specific to that
file in the file itself. Put a comment like this one near the end of
the file:

  /*
   * Local Variables:
   * mode:c
   * c-basic-offset: 4
   * End:
   */

/gordon

--

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

* Re: Emacs . How can I associate *.cu files as cc mode as a default ?
  2007-09-19 16:15               ` Gordon Beaton
@ 2007-09-19 17:55                 ` Tyler Smith
  2007-09-19 19:14                   ` Lowell Gilbert
  0 siblings, 1 reply; 14+ messages in thread
From: Tyler Smith @ 2007-09-19 17:55 UTC (permalink / raw)
  To: help-gnu-emacs

On 2007-09-19, Gordon Beaton <n.o.t@for.email> wrote:
> On 19 Sep 2007 15:47:19 GMT, Tyler Smith wrote:
>> (add-hook 'find-file-hook 'my-find-file-hook)
>>
>
> If you want to do this for all files of that type, then c-mode-hook is
> a better place to put the function, which doesn't need to check the
> filename:
>
>   (add-hook 'c-mode-hook (lambda() (setq c-basic-offset 4)))

Thanks for the suggestion, but this is something I want to use only on 
files for this particular project, not all c code. 

>
> Alternatively, use file variables to save settings specific to that
> file in the file itself. Put a comment like this one near the end of
> the file:
>
>   /*
>    * Local Variables:
>    * mode:c
>    * c-basic-offset: 4
>    * End:
>    */
>

Another good suggestion, and maybe the best alternative to my 
find-file-hook. Is there something in particular that is bad about 
find-file-hook, or is it just because there are better options to use 
that I should avoid it?

Thanks!

Tyler

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

* Re: Emacs . How can I associate *.cu files as cc mode as a default ?
  2007-09-19 17:55                 ` Tyler Smith
@ 2007-09-19 19:14                   ` Lowell Gilbert
  0 siblings, 0 replies; 14+ messages in thread
From: Lowell Gilbert @ 2007-09-19 19:14 UTC (permalink / raw)
  To: help-gnu-emacs

Tyler Smith <tyler.smith@mail.mcgill.ca> writes:

> On 2007-09-19, Gordon Beaton <n.o.t@for.email> wrote:
>> On 19 Sep 2007 15:47:19 GMT, Tyler Smith wrote:
>>> (add-hook 'find-file-hook 'my-find-file-hook)
>>>
>>
>> If you want to do this for all files of that type, then c-mode-hook is
>> a better place to put the function, which doesn't need to check the
>> filename:
>>
>>   (add-hook 'c-mode-hook (lambda() (setq c-basic-offset 4)))
>
> Thanks for the suggestion, but this is something I want to use only on 
> files for this particular project, not all c code. 

I have something like this, to avoid needing to put file-local
variables in every file in a subtree:

                   (if (string-match "/path/to/files/" (buffer-file-name))
                                       (my-work-environment))

where my-work-environment is a function I wrote that sets a lot of 
configuration to match corporate source code guidelines.

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

* Re: Emacs . How can I associate *.cu files as cc mode as a default ?
  2007-09-19 15:47             ` Tyler Smith
  2007-09-19 16:15               ` Gordon Beaton
@ 2007-09-19 19:46               ` Stefan Monnier
  2007-09-20 23:17                 ` Tyler Smith
  1 sibling, 1 reply; 14+ messages in thread
From: Stefan Monnier @ 2007-09-19 19:46 UTC (permalink / raw)
  To: help-gnu-emacs

> Reading the manual, I see that find-file-hooks is now obsolete - will
> find-file-hook be obsolete also?

No: find-file-hooks is obsolete only because it was renamed to find-file-hook.

> What should I replace the following
> code with:

> (defun my-find-file-hook()
>   (let ((fn (buffer-file-name)))
>     (when (string-match "latex2rtf" fn)
>       (set-variable 'tab-width 4))))
> (add-hook 'find-file-hook 'my-find-file-hook)

You can keep it as-is.  Although I'd use (setq tab-width 4) instead of
set-variable (set-variable is meant for interactive use, not that it matters
in this case).

Actually I'd probably do it that way instead:

   (defun my-latex2rtf-c-mode ()
     (c-mode)
     (setq tab-width 4))

   (add-to-list 'auto-mode-alist
                '("/latex2rtf/.*\\.c\\'" . my-latex2rtf-c-mode))

Not sure if it's better.


        Stefan

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

* Re: Emacs . How can I associate *.cu files as cc mode as a default ?
  2007-09-19 19:46               ` Stefan Monnier
@ 2007-09-20 23:17                 ` Tyler Smith
  0 siblings, 0 replies; 14+ messages in thread
From: Tyler Smith @ 2007-09-20 23:17 UTC (permalink / raw)
  To: help-gnu-emacs

On 2007-09-19, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
>> Reading the manual, I see that find-file-hooks is now obsolete - will
>> find-file-hook be obsolete also?
>
> No: find-file-hooks is obsolete only because it was renamed to find-file-hook.
>
>> What should I replace the following
>> code with:
>
>> (defun my-find-file-hook()
>>   (let ((fn (buffer-file-name)))
>>     (when (string-match "latex2rtf" fn)
>>       (set-variable 'tab-width 4))))
>> (add-hook 'find-file-hook 'my-find-file-hook)
>
> You can keep it as-is.  Although I'd use (setq tab-width 4) instead of
> set-variable (set-variable is meant for interactive use, not that it matters
> in this case).
>
> Actually I'd probably do it that way instead:
>
>    (defun my-latex2rtf-c-mode ()
>      (c-mode)
>      (setq tab-width 4))
>
>    (add-to-list 'auto-mode-alist
>                 '("/latex2rtf/.*\\.c\\'" . my-latex2rtf-c-mode))
>

Actually, that's closer to what I really want. Thanks for the
suggestion! And to Lowell for your help too.

Cheers,

Tyler

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

end of thread, other threads:[~2007-09-20 23:17 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-09-19  0:10 Emacs . How can I associate *.cu files as cc mode as a default ? Mike H
2007-09-19  0:28 ` Tyler Smith
2007-09-19  1:09   ` Mike H
2007-09-19  1:25     ` Mike H
2007-09-19  2:01       ` Tyler Smith
2007-09-19  2:04         ` Mike H
2007-09-19 15:13           ` Richard G Riley
2007-09-19 15:47             ` Tyler Smith
2007-09-19 16:15               ` Gordon Beaton
2007-09-19 17:55                 ` Tyler Smith
2007-09-19 19:14                   ` Lowell Gilbert
2007-09-19 19:46               ` Stefan Monnier
2007-09-20 23:17                 ` Tyler Smith
2007-09-19  6:24   ` Gordon Beaton

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.