From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Floyd Davidson Newsgroups: gmane.emacs.help Subject: Re: regexp / replacement for variable Date: Sun, 22 Feb 2004 03:14:12 -0900 Organization: __________ Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: <874qtj2twr.fld@barrow.com> References: NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1077452288 21684 80.91.224.253 (22 Feb 2004 12:18:08 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sun, 22 Feb 2004 12:18:08 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Feb 22 13:18:02 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 1AusYc-0006Xo-00 for ; Sun, 22 Feb 2004 13:18:02 +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 1AusYF-0004Is-NO for geh-help-gnu-emacs@m.gmane.org; Sun, 22 Feb 2004 07:17:39 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!cyclone.bc.net!HSNX.atgi.net!falcon.america.net!eagle.america.net.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help User-Agent: gnus 5.10.6/XEmacs 21.4.15/Linux 2.6.0 Cancel-Lock: sha1:yZiHaoqhP/BjuP0FHnHkQYObjI4= Original-Lines: 105 Original-NNTP-Posting-Host: 209.124.156.151 Original-X-Trace: eagle.america.net 1077452098 209.124.156.151 (Sun, 22 Feb 2004 07:14:58 EST) Original-NNTP-Posting-Date: Sun, 22 Feb 2004 07:14:58 EST Original-Xref: shelby.stanford.edu gnu.emacs.help:121119 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:17069 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:17069 Jan Misol wrote: >I'm new to elisp but I want my beloved editor >to generate the standard c-header stuff when creating >a new .h/.hpp header. > >> c-x c-f test.h > >should automatically insert: > > #ifndef _TEST_H_ > #ifndef _TEST_H_ > > #endif > >depending on the given filename. We probably want to use more of the filename than just the base, to allow similar header files in different directories without name space collisions. >I put the following to my .emacs file: > > (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. > >(and how could the new-c-header() be invoked by creating a >new .h/.cpp file?) > >jan There are some other considerations too. It is nice if this is automatic, but it also must avoid trying to add the #define to system headers and/or headers from other packages that are just being looked at. Below is one way to do that. It decides that a file is "empty" if it has fewer than 34 bytes in it, and otherwise will not make an insertion. When it does, it will include directories from the pathname up to a maximum of to 32 characters total (by default, which can be re-configured on the fly) into the symbol name used for the #define. Incidentally, it might be useful to expand on this concept and make it more general. Instead of running it from the c-mode-hook as I've done below, it could be run from the find-file-hook, and use an alist of regular expressions and associated boilerplate files. Similar to the auto-mode-alist, it would insert user defined boilerplate based on the filename being edited. The file to insert could be massaged by a fairly simple set of macro sustitutions built into the insertion process to allow using environment variables and Emacs functions or variables on a per instance basis triggered by macros in the boilerplate. Hmmm... sounds like a default "forms" boilerplate; somebody must have done that already... ;;; ;;; Insert boilerplate into a new header file ;;; (defvar fld-header-name-width-max 32) (defun fld-new-h-header () (interactive) (let ( ;; get file name and extension (name (upcase (file-name-sans-extension (buffer-file-name)))) (extn (upcase (file-name-extension (buffer-file-name))))) ;; trim off excessive length (when name (let ((start 0)) (while (and (< fld-header-name-width-max (string-width name)) (< start (string-width name))) (progn (when (string= (substring name start (1+ start)) "/") (progn (setq name (substring name (1+ start) nil)) (setq start -1))) (setq start (1+ start)))))) ;; to mung, or not to mung... (when (or (string= extn "HPP") (string= extn "H")) (save-excursion (when (< (point-max) 34) (progn (goto-char (point-min)) (insert (format "#ifndef _%s.%s_\n#define _%s.%s_\n\n#endif" name extn name extn)))))))) ;; add it to c-mode (which assumes the appropriate suffixes are ;; in the auto-mode-alist to enable c-mode for the target files. (add-hook 'c-mode-hook 'fld-new-h-header) -- Floyd L. Davidson Ukpeagvik (Barrow, Alaska) floyd@barrow.com