From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: lawrence mitchell Newsgroups: gmane.emacs.help Subject: Re: regexp / replacement for variable Date: Sun, 22 Feb 2004 00:51:13 +0000 Organization: funfunfun Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1077411721 29597 80.91.224.253 (22 Feb 2004 01:02:01 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sun, 22 Feb 2004 01:02:01 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Feb 22 02:01:54 2004 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1Aui0I-0006md-00 for ; Sun, 22 Feb 2004 02:01:54 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.30) id 1Auhzb-0006vX-FB for geh-help-gnu-emacs@m.gmane.org; Sat, 21 Feb 2004 20:01:11 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!fu-berlin.de!uni-berlin.de!vegetable.demon.co.UK!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 69 Original-NNTP-Posting-Host: vegetable.demon.co.uk (80.177.16.3) Original-X-Trace: news.uni-berlin.de 1077411073 48596472 I 80.177.16.3 ([97657]) X-No-Yes: No Mail-Copies-To: nobody User-Agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3.50 Cancel-Lock: sha1:Upatq+9CZecYjsT+m41eLti3ddk= Original-Xref: shelby.stanford.edu gnu.emacs.help:121109 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:17059 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:17059 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. -- lawrence mitchell