From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Pascal Bourguignon Newsgroups: gmane.emacs.help Subject: Re: a function to enter string Date: Tue, 20 Feb 2007 00:28:18 +0100 Organization: Informatimago Message-ID: <87tzxh6831.fsf@thalassa.informatimago.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1171928438 26554 80.91.229.12 (19 Feb 2007 23:40:38 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 19 Feb 2007 23:40:38 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Feb 20 00:40:31 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 1HJI7T-0003RU-8D for geh-help-gnu-emacs@m.gmane.org; Tue, 20 Feb 2007 00:40:31 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1HJI7S-00049f-SL for geh-help-gnu-emacs@m.gmane.org; Mon, 19 Feb 2007 18:40:30 -0500 Original-Path: shelby.stanford.edu!headwall.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 74 Original-X-Trace: individual.net zj7jUPnNtfIN8jRwssc1gAmrZY3BB5r0Hnzk/tgGozJ8XiikQs Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.91 (gnu/linux) Cancel-Lock: sha1:jqDxV+ack42PB9uMeaL5qan8rOo= Original-Xref: shelby.stanford.edu gnu.emacs.help:145730 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:41334 Archived-At: 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. ;; (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) -- __Pascal Bourguignon__ http://www.informatimago.com/ ATTENTION: Despite any other listing of product contents found herein, the consumer is advised that, in actuality, this product consists of 99.9999999999% empty space.