From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Gary Wessle Newsgroups: gmane.emacs.help Subject: Re: a function to enter string Date: 22 Feb 2007 06:12:19 +1100 Organization: iPrimus Customer - reports relating to abuse should be sent to abuse@iprimus.com.au Message-ID: References: <87tzxh6831.fsf@thalassa.informatimago.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1172086862 29476 80.91.229.12 (21 Feb 2007 19:41:02 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Wed, 21 Feb 2007 19:41:02 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Feb 21 20:41:00 2007 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1HJxKl-0005ku-KR for geh-help-gnu-emacs@m.gmane.org; Wed, 21 Feb 2007 20:40:59 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1HJxKk-0006lo-Vh for geh-help-gnu-emacs@m.gmane.org; Wed, 21 Feb 2007 14:40:59 -0500 Original-Lines: 79 Original-NNTP-Posting-Host: 203.134.67.67 Original-Path: shelby.stanford.edu!headwall.stanford.edu!newsfeed.media.kyoto-u.ac.jp!newsfeed.gamma.ru!Gamma.RU!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.freenet.de!news.germany.com!feed.xsnews.nl!border-1.ams.xsnews.nl!news-out.superfeed.co.uk!propagator2-sterling.newsfeeds.com!news-in2.newsgroups.com!newsfeed.pacific.net.au!token.pipenetworks.com!218.100.2.59.MISMATCH!not-for-mail Original-Newsgroups: gnu.emacs.help Original-X-Trace: 1172085135 un-2park-reader-01.sydney.pipenetworks.com.au 1150 203.134.67.67:4155 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.4 X-Original-NNTP-Posting-Host: 58.178.32.177 X-Original-NNTP-Posting-Host: 127.0.0.1 Original-X-Complaints-To: Abuse, including message IDs to abuse@pipenetworks.com Original-Xref: shelby.stanford.edu gnu.emacs.help:145781 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:41386 Archived-At: Pascal Bourguignon writes: > Gary Wessle writes: > > > Hi > > > > I have this key macro in my emacs which puts "#include " in > > the first blank line in the buffer, also another key macro which puts > > the string "std::string" at the point. > > > > (fset 'str > > [?\M- > > return]) > > > > (fset ':s > > "std::string") > > > > I want when I type M-x :s which is the second key macro above, it > > puts the string "std::string" at point as well as checks to see if > > "#include " is at the top of the buffer, if not it inserts > > it so that I don't have to do it. > > > > how can this be done? > > > I would write a command, like: > > (defun std-string () > (interactive) > (insert "std::string") > (save-excursion > (beginning-of-buffer) > (unless (re-search-forward "#include " nil t) > ;; it is silly to search the first white line to insert > ;; the #include, but that's what you asked... > ;; It would be better to just skip over then title comment, > ;; and to _insert_ a new line for the #include. > ;; indeed you are right, I would also choose this as a better option. how would that change the code below? I think the condition of the "if" statement will change. but I have no clue how to put it together. thanks again. > (if (re-search-forward "^[ \t]*$" nil t) > (progn > (beginning-of-line) > (insert "#include ")) > (progn > ;; See how it's ludicruous? What should we do > ;; when there's no empty line??? > (end-of-buffer) > (insert "\n#include ")))))) > > The symbols whose name starts with a colon are special. They get > automatically bound to themselves. So we can evaluate :xyz instead of > ':xyz when we want :xyz. We call them keywords, since they're often > used as "syntax" keywords. While it's possible to use them to name > functions (given that emacs lisp is a "lisp-2", meaning that a symbol > can be bound to a value and to a function at the same time), I > wouldn't advise to bind functions (or even key macro) to keywords. > > > You can invoke such a command with M-x stdstr RET and if it's too much > to type, you can bind the command to some key sequence. For example, > to bind it to the sequence F8 s you can put the following in your > ~/.emacs : > > > (defun c++-meat () > (local-set-key (kbd " s") 'std-string) > ;; ... > ) > > (add-hook 'c++-mode-hook 'c++-meat) >