all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Jan Misol <misol@liblss.org>
Subject: Re: regexp / replacement for variable
Date: Sun, 22 Feb 2004 13:18:21 +0100	[thread overview]
Message-ID: <40389E0D.9060408@liblss.org> (raw)
In-Reply-To: <?fnord?87u11kgcn2.fsf@ID-97657.usr.dfncis.de>

Thanks, this was very helpful ...
(Sorry, the last mail reached you directly ...)

Adding:
   (add-hook 'find-file-hooks 'auto-insert)
to my .emacs does it already!

So I had to upgrade my requirements :)
I found http://www.linuxgazette.com/issue39/marsden.html,
and now it looks like this:

   (define-auto-insert
     (cons "\\.\\([hH].*\\)\\'" "User defined C/C++ header")
     '(nil
       "/*"
       " * Filename: " (buffer-name) "\n"
       " * Author: " (user-full-name) " <" user-mail-address ">\n"
       " * Time-stamp: <>\n"
       " */\n"
       (let* ((prfx (substring buffer-file-name 0 (match-beginning 0)))
            (sffx (substring buffer-file-name (match-beginning 1) (match-end 0)))
            (prfx (file-name-nondirectory prfx))
            (ident (concat "_" (upcase prfx) "_" (upcase sffx) "_")))
         (concat "#ifndef " ident "\n"
                 "#define " ident  "\n\n\n\n"
                 "#endif /* " ident " */\n"))))

   (add-hook 'write-file-hooks 'time-stamp)


Jan

lawrence mitchell wrote:
> Jan Misol wrote:
> 
> 
> [...]
> 
> 
>>>c-x c-f test.h
> 
> 
>>should automatically insert:
> 
> 
>>   #ifndef _TEST_H_
>>   #ifndef _TEST_H_
> 
> 
>>   #endif
> 
> 
> [...]
> 
> 
>>   (defun new-c-header ()
>>     "Insert c-header skeleton."
>>     (interactive "")
>>     (progn
>>       (setq bname (upcase(buffer-name)))
>>       (insert
>>        (message "#ifndef %s\n\#define %s\n\n#endif"
>>           bname bname))))
> 
> 
>>Apart from knowing that "message" might not be the right
>>choice here, I don't know how to modify the value of bname!?
>>"replace-regexp" doesn't seem to be the what I'm looking for.
> 
> 
> Well, how about using `file-name-sans-extension' to strip the
> trailing .h, and then insert using `format'.
> 
> (defun my-new-c-header ()
>   (interactive)
>   (let ((name (upcase (file-name-sans-extension (buffer-file-name)))))
>     ;; Only insert if _name_H isn't already defined.
>     (unless (save-excursion
>               (goto-char (point-min))
>               (search-forward (format "#ifndef _%s_H" name))
>       (insert (format "#ifndef _%s_H\n#define _%s_H\n\n#endif"))))))
> 
> 
>>(and how could the new-c-header() be invoked by creating a
>>new .h/.cpp file?)
> 
> 
> You could then either add this as a hook to `c-mode-hook' or
> `find-file-hooks'.  In the case of the latter, you'd want to
> predicate it on being a header file.  In fact, you'd want to do
> the same for the former too.
> 
> You could do this by adding an extra test to the `unless' form
> in the above function.
> 
> say:
> 
> (unless (and (string-match "\\.h") (buffer-file-name)
>              ...)
>   ...)
> 
> Slightly orthogonal to all this, you may find reading the Emacs
> Lisp Introduction enlightening.
> 
> Alternately, instead of trying to roll your own function, it may
> well be that Emacs already has the functionality you need
> built-in.  See the info node "autotype", in Emacs, you can get
> there via C-h i d m Autotype RET.
> 
> Hope some of this helps.

  reply	other threads:[~2004-02-22 12:18 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.262.1077404990.340.help-gnu-emacs@gnu.org>
2004-02-22  0:41 ` regexp / replacement for variable Kin Cho
2004-02-22  0:51 ` lawrence mitchell
2004-02-22 12:18   ` Jan Misol [this message]
2004-02-22 12:14 ` Floyd Davidson
     [not found] <mailman.372.1077629684.340.help-gnu-emacs@gnu.org>
2004-02-24 14:03 ` Joakim Hove
2004-02-24 20:09 ` Floyd Davidson
2004-02-21 23:08 Jan Misol
2004-02-24 13:31 ` Kevin Dziulko

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

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

  git send-email \
    --in-reply-to=40389E0D.9060408@liblss.org \
    --to=misol@liblss.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.
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.