[Top][All Lists]

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

Re: regexp / replacement for variable

From: Jan Misol
Subject: Re: regexp / replacement for variable
Date: Sun, 22 Feb 2004 13:18:21 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040208

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

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

So I had to upgrade my requirements :)
I found,
and now it looks like this:

    (cons "\\.\\([hH].*\\)\\'" "User defined C/C++ header")
      " * 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)


lawrence mitchell wrote:
Jan Misol wrote:


c-x c-f test.h

should automatically insert:

  #ifndef _TEST_H_
  #ifndef _TEST_H_



  (defun new-c-header ()
    "Insert c-header skeleton."
    (interactive "")
      (setq bname (upcase(buffer-name)))
       (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 ()
  (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.


(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 via email to

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