* a function to enter string
@ 2007-02-19 20:27 Gary Wessle
2007-02-19 23:28 ` Pascal Bourguignon
2007-02-20 8:30 ` Gordon Beaton
0 siblings, 2 replies; 5+ messages in thread
From: Gary Wessle @ 2007-02-19 20:27 UTC (permalink / raw)
To: help-gnu-emacs
Hi
I have this key macro in my emacs which puts "#include <string>" in
the first blank line in the buffer, also another key macro which puts
the string "std::string" at the point.
(fset 'str
[?\M-<?\M-} ?# ?i ?n ?c ?l ?u ?d ?e ? ?< ?s ?t ?r ?i ?n ?g ?>
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 <string>" 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?
many thanks.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: a function to enter string
2007-02-19 20:27 a function to enter string Gary Wessle
@ 2007-02-19 23:28 ` Pascal Bourguignon
2007-02-20 16:06 ` Mathias Dahl
2007-02-21 19:12 ` Gary Wessle
2007-02-20 8:30 ` Gordon Beaton
1 sibling, 2 replies; 5+ messages in thread
From: Pascal Bourguignon @ 2007-02-19 23:28 UTC (permalink / raw)
To: help-gnu-emacs
Gary Wessle <phddas@yahoo.com> writes:
> Hi
>
> I have this key macro in my emacs which puts "#include <string>" in
> the first blank line in the buffer, also another key macro which puts
> the string "std::string" at the point.
>
> (fset 'str
> [?\M-<?\M-} ?# ?i ?n ?c ?l ?u ?d ?e ? ?< ?s ?t ?r ?i ?n ?g ?>
> 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 <string>" 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 <string>" 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 <string>"))
(progn
;; See how it's ludicruous? What should we do
;; when there's no empty line???
(end-of-buffer)
(insert "\n#include <string>"))))))
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 "<f8> 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.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: a function to enter string
2007-02-19 23:28 ` Pascal Bourguignon
@ 2007-02-20 16:06 ` Mathias Dahl
2007-02-21 19:12 ` Gary Wessle
1 sibling, 0 replies; 5+ messages in thread
From: Mathias Dahl @ 2007-02-20 16:06 UTC (permalink / raw)
To: help-gnu-emacs
Pascal Bourguignon <pjb@informatimago.com> writes:
> 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.
One could also use an abbrev. Not the normal variant, but the one that
can execute a Lisp expression (which in this case would be the nice
command Pascal wrote).
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: a function to enter string
2007-02-19 23:28 ` Pascal Bourguignon
2007-02-20 16:06 ` Mathias Dahl
@ 2007-02-21 19:12 ` Gary Wessle
1 sibling, 0 replies; 5+ messages in thread
From: Gary Wessle @ 2007-02-21 19:12 UTC (permalink / raw)
To: help-gnu-emacs
Pascal Bourguignon <pjb@informatimago.com> writes:
> Gary Wessle <phddas@yahoo.com> writes:
>
> > Hi
> >
> > I have this key macro in my emacs which puts "#include <string>" in
> > the first blank line in the buffer, also another key macro which puts
> > the string "std::string" at the point.
> >
> > (fset 'str
> > [?\M-<?\M-} ?# ?i ?n ?c ?l ?u ?d ?e ? ?< ?s ?t ?r ?i ?n ?g ?>
> > 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 <string>" 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 <string>" 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 <string>"))
> (progn
> ;; See how it's ludicruous? What should we do
> ;; when there's no empty line???
> (end-of-buffer)
> (insert "\n#include <string>"))))))
>
> 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 "<f8> s") 'std-string)
> ;; ...
> )
>
> (add-hook 'c++-mode-hook 'c++-meat)
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: a function to enter string
2007-02-19 20:27 a function to enter string Gary Wessle
2007-02-19 23:28 ` Pascal Bourguignon
@ 2007-02-20 8:30 ` Gordon Beaton
1 sibling, 0 replies; 5+ messages in thread
From: Gordon Beaton @ 2007-02-20 8:30 UTC (permalink / raw)
To: help-gnu-emacs
On 20 Feb 2007 07:27:52 +1100, Gary Wessle wrote:
> I have this key macro in my emacs which puts "#include <string>" in
> the first blank line in the buffer, also another key macro which
> puts the string "std::string" at the point.
Not really what you're asking, but I use the autoinsert package for
things like that. Just create a template and it will be loaded
whenever you create new files of the given type.
(require 'autoinsert)
(add-hook 'find-file-hooks 'auto-insert)
(setq auto-insert-directory "~/.emacs.d/insert/")
(define-auto-insert "\\.sh\\'" "sh-insert.sh")
(define-auto-insert "\\.tex\\'" "tex-insert.tex")
(define-auto-insert "\\.el\\'" "el-insert.el")
(define-auto-insert "\\.c\\'" "c-insert.c") ;;
With autoinsert you can insert dynamic content as well, for example
when you create a Java file it can insert a correctly named class
declaration based on the filename, and then leave the cursor where you
want to start typing.
/gordon
--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2007-02-21 19:12 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-02-19 20:27 a function to enter string Gary Wessle
2007-02-19 23:28 ` Pascal Bourguignon
2007-02-20 16:06 ` Mathias Dahl
2007-02-21 19:12 ` Gary Wessle
2007-02-20 8:30 ` Gordon Beaton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).