all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Making code more Emacs Lisp-like
@ 2009-11-02 15:19 Sarir Khamsi
  2009-11-02 20:05 ` Joseph Brenner
  0 siblings, 1 reply; 4+ messages in thread
From: Sarir Khamsi @ 2009-11-02 15:19 UTC (permalink / raw
  To: help-gnu-emacs

I wrote this function

(defun sk-convert-to-ctor-init-list ()
  "Convert a C++ member variable declaration to a ctor init list line.
To use this, copy all member variable declarations into the constructor's member initialization list area and execute this command on each line. At the end of this command, it moves you to the next line setting it up for a key binding."
  (interactive)
  (save-excursion
    (end-of-line)
    (delete-horizontal-space)           ; delete whitespace at end of line
    (backward-delete-char-untabify 1)   ; delete the ";"
    (insert "(),")
    (search-backward-regexp "[\t *]")     ; look for whitespace or "*"
    (set-mark (+ (point) 1))              ; save the current position
    (beginning-of-line)
    (kill-region (point) (mark))
    (c-indent-command))
  (next-line 1))

to take a region that containers C++ code for member variable
declarations and convert it to a constructor's member init list. Is
there a better (or a more ELisp) way than this approach? Thanks.

Sarir

-- 
Sarir Khamsi
software guy
sarir.khamsi@raytheon.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Making code more Emacs Lisp-like
  2009-11-02 15:19 Making code more Emacs Lisp-like Sarir Khamsi
@ 2009-11-02 20:05 ` Joseph Brenner
  2009-11-02 22:17   ` Sarir Khamsi
  0 siblings, 1 reply; 4+ messages in thread
From: Joseph Brenner @ 2009-11-02 20:05 UTC (permalink / raw
  To: help-gnu-emacs


Sarir Khamsi <sarir.khamsi@raytheon.com> writes:

> I wrote this function
>
> (defun sk-convert-to-ctor-init-list ()
>   "Convert a C++ member variable declaration to a ctor init list line.
> To use this, copy all member variable declarations into the constructor's member initialization list area and execute this command on each line. At the end of this command, it moves you to the next line setting it up for a key binding."
>   (interactive)
>   (save-excursion
>     (end-of-line)
>     (delete-horizontal-space)           ; delete whitespace at end of line
>     (backward-delete-char-untabify 1)   ; delete the ";"
>     (insert "(),")
>     (search-backward-regexp "[\t *]")     ; look for whitespace or "*"
>     (set-mark (+ (point) 1))              ; save the current position
>     (beginning-of-line)
>     (kill-region (point) (mark))
>     (c-indent-command))
>   (next-line 1))
>
> to take a region that containers C++ code for member variable
> declarations and convert it to a constructor's member init list. Is
> there a better (or a more ELisp) way than this approach? Thanks.

If you look up the Help screen for each function (using the default
bindings: cursor to the function, and do a "f1 f"), you'll often see
hints on better ways of doing things from inside of elisp.

For example, if you look up "set-mark", there's an example that's
easily adapted to what you're doing here:

>     (set-mark (+ (point) 1))              ; save the current position
>     (beginning-of-line)
>     (kill-region (point) (mark))

Would become:

   (let ((beg (point)))
      (beginning-of-line)
      (delete-region beg (point)))

The goal there is to avoid messing with the kill-ring or the mark
history if that's not necessary.  Otherwise you may surprise the
user with the unwanted side-effects.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Making code more Emacs Lisp-like
  2009-11-02 20:05 ` Joseph Brenner
@ 2009-11-02 22:17   ` Sarir Khamsi
  2009-11-03  0:39     ` Joseph Brenner
  0 siblings, 1 reply; 4+ messages in thread
From: Sarir Khamsi @ 2009-11-02 22:17 UTC (permalink / raw
  To: help-gnu-emacs

Joseph Brenner <doom@kzsu.stanford.edu> writes:

> For example, if you look up "set-mark", there's an example that's
> easily adapted to what you're doing here:
>
>>     (set-mark (+ (point) 1))              ; save the current position
>>     (beginning-of-line)
>>     (kill-region (point) (mark))
>
> Would become:
>
>    (let ((beg (point)))
>       (beginning-of-line)
>       (delete-region beg (point)))

Thanks, this looks better.

> The goal there is to avoid messing with the kill-ring or the mark
> history if that's not necessary.  Otherwise you may surprise the
> user with the unwanted side-effects.

save-excursion is supposed to "Save point, mark, and current buffer;
execute BODY; restore those things" but I understand that you want to
avoid modifying the kill ring. Thanks.

Sarir

-- 
Sarir Khamsi
sarir.khamsi@raytheon.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Making code more Emacs Lisp-like
  2009-11-02 22:17   ` Sarir Khamsi
@ 2009-11-03  0:39     ` Joseph Brenner
  0 siblings, 0 replies; 4+ messages in thread
From: Joseph Brenner @ 2009-11-03  0:39 UTC (permalink / raw
  To: help-gnu-emacs


Sarir Khamsi <sarir.khamsi@raytheon.com> writes:
> Joseph Brenner <doom@kzsu.stanford.edu> writes:

>> The goal there is to avoid messing with the kill-ring or the mark
>> history if that's not necessary.  Otherwise you may surprise the
>> user with the unwanted side-effects.
>
> save-excursion is supposed to "Save point, mark, and current buffer;
> execute BODY; restore those things" but I understand that you want to
> avoid modifying the kill ring. Thanks.

Good point... but don't always assume that save-excursion is really
going to save what you think it will.  Every now and then I find
a case where it doesn't seem to want to restore the current buffer,
and I need to do that manually.

(No, I haven't yet filed a bug report.  Maybe next time I see the
problem.)


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-11-03  0:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-02 15:19 Making code more Emacs Lisp-like Sarir Khamsi
2009-11-02 20:05 ` Joseph Brenner
2009-11-02 22:17   ` Sarir Khamsi
2009-11-03  0:39     ` Joseph Brenner

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.