0

For my c (c++) files, I added this reference to a cc-mode to my .emacs file:

(setq c-default-style "stroustrup"
c-basic-offset 4)

I would like to use the same for a custom extension ".dec." I tried to naively adjust this SO question doing

(defun my-decaf-mode ()   
    (when (and (stringp buffer-file-name)   
           (string-match "\\.dec\\'" buffer-file-name))   

           (setq c-default-style "stroustrup")    
           (c-basic-offset 4)) )    

(add-hook 'find-file-hook 'my-decaf-mode)   

which didn't work (I wrote a few lines in C; the support I would get giving the file a .c extension was not there). I also tried the actual customization type as in

           (setq c-default-style "stroustrup"    
           c-basic-offset 4)) )    

How could I get this done?

Community
  • 1
  • 1
gnometorule
  • 2,151
  • 2
  • 20
  • 29
  • How about something like this: `(add-to-list 'auto-mode-alist '("\\.dec\\'" . c-mode))` and then use something like a `c-mode-hook` -- e.g., `(add-hook 'c-mode-hook 'my-decaf-mode)`? – lawlist Apr 17 '14 at 17:37
  • @lawlist: Thanks for the suggestion. Could you type this out? I'm not quite sure what to replace with what exactly? :) (my emacs customization skill is on the rudimentary side) – gnometorule Apr 17 '14 at 17:40
  • Do you really want to create your own major mode, or do you just want `c-mode` with some extra stuff going on whenever there is a `*.dec` extension? – lawlist Apr 17 '14 at 17:41
  • @lawlist: All i want is that as soon as emacs sees a .dec extension, it should use the "stroustrup" type cc-mode (with the one additional specification being an indent size of 4)). ("stroustrup" style is optional if any of the other modes somehow is easier to get; "K&R", say, is nice too) – gnometorule Apr 17 '14 at 17:42
  • I would give my two samples a try, and if that doesn't work for you, then I'm afraid someone more advanced than I will need to take over from here . . . I grepped the Emacs source code and found a `c-mode-hook`, but *not* a `cc-mode-hook`, and I do not have enough background to know the difference between `c-mode` and `cc-mode` -- sorry. I would image that the ultimate solution to your question will look similar to my first comment. – lawlist Apr 17 '14 at 17:45
  • 1
    @lawlist: Cool! This seems to do the trick (maybe it says to treat a .dec file like a .c file, and as .c is modified already in my .emacs file, .dec is re-directed to the same modification). May I suggest to copy your first comment into an answer, so I can upvote and accept? – gnometorule Apr 17 '14 at 17:54

1 Answers1

1

This answer assumes that the function my-decaf-mode does exactly what the original poster wants, and that the only thing needed is to associate *.dec extension files with c-mode and then call the function my-decaf-mode when the major-mode c-mode is activated in a buffer:

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

(add-hook 'c-mode-hook 'my-decaf-mode)
lawlist
  • 13,099
  • 3
  • 49
  • 158