unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Inserting output from a program into a buffer
@ 2010-02-21  2:45 Tim Johnson
  2010-02-21  3:15 ` Tim Landscheidt
  0 siblings, 1 reply; 14+ messages in thread
From: Tim Johnson @ 2010-02-21  2:45 UTC (permalink / raw)
  To: help-gnu-emacs

emacs 22.3.1 on slack 13.0/32-bit

Consider the following text:

[[10:00 11:27][14:43 14:57]] ;; ^output here

Given that I have selected:
"[[10:00 11:27][14:43 14:57]]" 
as a region, I would like to send that region to an external application
and insert the output as indicated.

I've recently started using emacs again after several years, and have in
the past written quite a few elisp functions and keybindings for my own
use. The external application has already been written, I've used it
with vim and "r !<shell command>" for years.

References to relevant and related elisp functions and scripts would
probably be sufficient, however, if someone has done this already - why
re-invent the wheel?

TIA
-- 
Tim 
tim@johnsons-web.com
http://www.akwebsoft.com


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

* Re: Inserting output from a program into a buffer
  2010-02-21  2:45 Inserting output from a program into a buffer Tim Johnson
@ 2010-02-21  3:15 ` Tim Landscheidt
  2010-02-21  4:45   ` Tim Johnson
  0 siblings, 1 reply; 14+ messages in thread
From: Tim Landscheidt @ 2010-02-21  3:15 UTC (permalink / raw)
  To: help-gnu-emacs

Tim Johnson <tim@johnsons-web.com> wrote:

> emacs 22.3.1 on slack 13.0/32-bit

> Consider the following text:

> [[10:00 11:27][14:43 14:57]] ;; ^output here

> Given that I have selected:
> "[[10:00 11:27][14:43 14:57]]"
> as a region, I would like to send that region to an external application
> and insert the output as indicated.

> I've recently started using emacs again after several years, and have in
> the past written quite a few elisp functions and keybindings for my own
> use. The external application has already been written, I've used it
> with vim and "r !<shell command>" for years.

> References to relevant and related elisp functions and scripts would
> probably be sufficient, however, if someone has done this already - why
> re-invent the wheel?

Not exactly what you require, but
C-u M-x shell-command-on-region RET will /replace/ the re-
gion with the output of the shell-command operated on the
original region.

Tim


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

* Re: Inserting output from a program into a buffer
  2010-02-21  3:15 ` Tim Landscheidt
@ 2010-02-21  4:45   ` Tim Johnson
  2010-02-21  5:02     ` Barry Margolin
  2010-02-21  6:28     ` tomas
  0 siblings, 2 replies; 14+ messages in thread
From: Tim Johnson @ 2010-02-21  4:45 UTC (permalink / raw)
  To: help-gnu-emacs

On 2010-02-21, Tim Landscheidt <tim@tim-landscheidt.de> wrote:
> Tim Johnson <tim@johnsons-web.com> wrote:
>
>> emacs 22.3.1 on slack 13.0/32-bit
>
>> Consider the following text:
>
>> [[10:00 11:27][14:43 14:57]] ;; ^output here
>
>> Given that I have selected:
>> "[[10:00 11:27][14:43 14:57]]"
>> as a region, I would like to send that region to an external application
>> and insert the output as indicated.
>
>> I've recently started using emacs again after several years, and have in
>> the past written quite a few elisp functions and keybindings for my own
>> use. The external application has already been written, I've used it
>> with vim and "r !<shell command>" for years.
>
>> References to relevant and related elisp functions and scripts would
>> probably be sufficient, however, if someone has done this already - why
>> re-invent the wheel?
>
> Not exactly what you require, but
> C-u M-x shell-command-on-region RET will /replace/ the re-
> gion with the output of the shell-command operated on the
> original region.
  Hi Tim: 
	Thanks for the reply. I had found that command, but unfortunately
	replacing the region is the last thing I want to do. The end game is a
	"time card", as you might guess from the data structure and the data
	structure needs to remain intact.
	Thanks Again!
-- 
Tim 
tim@johnsons-web.com
http://www.akwebsoft.com


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

* Re: Inserting output from a program into a buffer
  2010-02-21  4:45   ` Tim Johnson
@ 2010-02-21  5:02     ` Barry Margolin
       [not found]       ` <slrnho2o6e.49k.tim@bart.johnson.com>
  2010-02-21  6:28     ` tomas
  1 sibling, 1 reply; 14+ messages in thread
From: Barry Margolin @ 2010-02-21  5:02 UTC (permalink / raw)
  To: help-gnu-emacs

In article <slrnho1bpi.49k.tim@bart.johnson.com>,
 Tim Johnson <tim@johnsons-web.com> wrote:

> On 2010-02-21, Tim Landscheidt <tim@tim-landscheidt.de> wrote:
> > Tim Johnson <tim@johnsons-web.com> wrote:
> >
> >> emacs 22.3.1 on slack 13.0/32-bit
> >
> >> Consider the following text:
> >
> >> [[10:00 11:27][14:43 14:57]] ;; ^output here
> >
> >> Given that I have selected:
> >> "[[10:00 11:27][14:43 14:57]]"
> >> as a region, I would like to send that region to an external application
> >> and insert the output as indicated.
> >
> >> I've recently started using emacs again after several years, and have in
> >> the past written quite a few elisp functions and keybindings for my own
> >> use. The external application has already been written, I've used it
> >> with vim and "r !<shell command>" for years.
> >
> >> References to relevant and related elisp functions and scripts would
> >> probably be sufficient, however, if someone has done this already - why
> >> re-invent the wheel?
> >
> > Not exactly what you require, but
> > C-u M-x shell-command-on-region RET will /replace/ the re-
> > gion with the output of the shell-command operated on the
> > original region.
>   Hi Tim: 
> 	Thanks for the reply. I had found that command, but unfortunately
> 	replacing the region is the last thing I want to do. The end game is a
> 	"time card", as you might guess from the data structure and the data
> 	structure needs to remain intact.
> 	Thanks Again!

If you use it without the C-u modifier, the output will be put in the 
*Shell Output* buffer.  You could use M-x insert-buffer to copy the 
output into the current buffer.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


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

* Re: Inserting output from a program into a buffer
  2010-02-21  4:45   ` Tim Johnson
  2010-02-21  5:02     ` Barry Margolin
@ 2010-02-21  6:28     ` tomas
  1 sibling, 0 replies; 14+ messages in thread
From: tomas @ 2010-02-21  6:28 UTC (permalink / raw)
  To: Tim Johnson; +Cc: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sat, Feb 20, 2010 at 10:45:08PM -0600, Tim Johnson wrote:
> On 2010-02-21, Tim Landscheidt <tim@tim-landscheidt.de> wrote:
> > Tim Johnson <tim@johnsons-web.com> wrote:
> >
> >> emacs 22.3.1 on slack 13.0/32-bit
> >
> >> Consider the following text:

[...]

> > Not exactly what you require, but
> > C-u M-x shell-command-on-region RET will /replace/ the re-
> > gion with the output of the shell-command operated on the
> > original region.
>   Hi Tim: 
> 	Thanks for the reply. I had found that command, but unfortunately
> 	replacing the region is the last thing I want to do. The end game is a
> 	"time card", as you might guess from the data structure and the data
> 	structure needs to remain intact.
> 	Thanks Again!

But shell-command-on-region has an optional parameter called REPLACE.
This might be what you are looking for (you'll need to wrap the call,
REPLACE is set to t on interactive calls, though).

Regards
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFLgNJ9Bcgs9XrR2kYRAjZZAJ4wHq3pVIRTcS4btOCjZhvoA37gMgCfVi1V
dqoU+yWZE6rcu7yxRCiOv5I=
=bRfw
-----END PGP SIGNATURE-----




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

* Re: Inserting output from a program into a buffer
       [not found]         ` <87zl324774.fsf@lion.rapttech.com.au>
@ 2010-02-22  0:45           ` Tim Johnson
  2010-02-22  1:57             ` Pascal J. Bourguignon
  2010-02-22  7:22             ` Tim X
  0 siblings, 2 replies; 14+ messages in thread
From: Tim Johnson @ 2010-02-22  0:45 UTC (permalink / raw)
  To: help-gnu-emacs

On 2010-02-21, Tim X <timx@nospam.dev.null> wrote:
>>   Thanks
>
> I'm just interested in what the external program you need to call is and
> what it does?
>
> I only mention this as I handle some timeclock/timecard requirements I
> have using existing emacs functionality. Maybe there is an alternative
> way to approach this issue that would give a better result. 
  Hi Tim:
	The following data structure:
	[[13:22 13:41][14:50 15:04][15:58 16:03][16:53 16:58]]
	Is a nested block or list recognized by the rebol programming
	language. Each value: 13:22, 15:04 etc is a value of datatype "time!"
	and rebol has the ability of easily doing math on such datatypes.
	Regardless of what programming language that I might be working in,
	I use this approach to "pipe" such a data structure to a simple
	application that I have written. That application then returns
	the sum, which is inserted into the buffer. (See my initial post)

	The rebol binary is very easy to install. Small footprint, no
	external libraries are necessary. I would be happy to provide
	the application as well as the elisp code to manipulate it.

	As for the elisp code, I now have a function that grabs the data
	structure and puts it in the kill-ring.

	I now have to ask another question myself, related to this subject.
	I'd like to copy _only_ the text at the car of the kill ring to a
	variable, for further processing use:
	Example:
	alt-: (car kill-ring) => 
	#("[0:58 1:42 0:43 1:41]" 0 20 (fontified t) 20 21 (rear-nonsticky t
	fontified t))
	How I can copy the first item
	"[0:58 1:42 0:43 1:41]"
	from the car of kill-ring into a variable?
	Thanks again
-- 
Tim 
tim@johnsons-web.com
http://www.akwebsoft.com


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

* Re: Inserting output from a program into a buffer
  2010-02-22  0:45           ` Tim Johnson
@ 2010-02-22  1:57             ` Pascal J. Bourguignon
  2010-02-22  4:45               ` Tim Johnson
  2010-02-22  7:22             ` Tim X
  1 sibling, 1 reply; 14+ messages in thread
From: Pascal J. Bourguignon @ 2010-02-22  1:57 UTC (permalink / raw)
  To: help-gnu-emacs

Tim Johnson <tim@johnsons-web.com> writes:

> On 2010-02-21, Tim X <timx@nospam.dev.null> wrote:
>>>   Thanks
>>
>> I'm just interested in what the external program you need to call is and
>> what it does?
>>
>> I only mention this as I handle some timeclock/timecard requirements I
>> have using existing emacs functionality. Maybe there is an alternative
>> way to approach this issue that would give a better result. 
>   Hi Tim:
> 	The following data structure:
> 	[[13:22 13:41][14:50 15:04][15:58 16:03][16:53 16:58]]
> 	Is a nested block or list recognized by the rebol programming
> 	language. Each value: 13:22, 15:04 etc is a value of datatype "time!"
> 	and rebol has the ability of easily doing math on such datatypes.
> 	Regardless of what programming language that I might be working in,
> 	I use this approach to "pipe" such a data structure to a simple
> 	application that I have written. That application then returns
> 	the sum, which is inserted into the buffer. (See my initial post)
>
> 	The rebol binary is very easy to install. Small footprint, no
> 	external libraries are necessary. I would be happy to provide
> 	the application as well as the elisp code to manipulate it.
>
> 	As for the elisp code, I now have a function that grabs the data
> 	structure and puts it in the kill-ring.
>
> 	I now have to ask another question myself, related to this subject.
> 	I'd like to copy _only_ the text at the car of the kill ring to a
> 	variable, for further processing use:
> 	Example:
> 	alt-: (car kill-ring) => 
> 	#("[0:58 1:42 0:43 1:41]" 0 20 (fontified t) 20 21 (rear-nonsticky t
> 	fontified t))
> 	How I can copy the first item
> 	"[0:58 1:42 0:43 1:41]"
> 	from the car of kill-ring into a variable?
> 	Thanks again

This is the first item!

You can check that:

(string= #("[0:58 1:42 0:43 1:41]" 0 20 (fontified t) 20 21 (rear-nonsticky t
               fontified t))
         "[0:58 1:42 0:43 1:41]") --> t

What appears as #("[0:58 1:42 0:43 1:41]" 0 20 (fontified t) 20 21
(rear-nonsticky t fontified t)) is a string, with properties.


You could get a string without attribute using the function
buffer-substring-no-properties instead of buffer-substring.

or using gnus-string-remove-all-properties:

(defun gnus-string-remove-all-properties (string)
  (condition-case ()
      (let ((s string))
         (set-text-properties 0 (length string) nil string)
         s)
    (error string)))

but there is no reason to lose time removing them in general.

-- 
__Pascal Bourguignon__


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

* Re: Inserting output from a program into a buffer
  2010-02-22  1:57             ` Pascal J. Bourguignon
@ 2010-02-22  4:45               ` Tim Johnson
  2010-02-22  9:41                 ` Pascal J. Bourguignon
  2010-02-22 23:06                 ` jpkotta
  0 siblings, 2 replies; 14+ messages in thread
From: Tim Johnson @ 2010-02-22  4:45 UTC (permalink / raw)
  To: help-gnu-emacs

On 2010-02-22, Pascal J. Bourguignon <pjb@informatimago.com> wrote:
<..>
> You could get a string without attribute using the function
> buffer-substring-no-properties instead of buffer-substring.
 Oh! Deja vu here... I believe that what I really need is 
 buffer-substring-<etc> to copy the region directly to a
 variable.
 thanks Pascal

-- 
Tim 
tim@johnsons-web.com
http://www.akwebsoft.com


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

* Re: Inserting output from a program into a buffer
  2010-02-22  0:45           ` Tim Johnson
  2010-02-22  1:57             ` Pascal J. Bourguignon
@ 2010-02-22  7:22             ` Tim X
  1 sibling, 0 replies; 14+ messages in thread
From: Tim X @ 2010-02-22  7:22 UTC (permalink / raw)
  To: help-gnu-emacs

Tim Johnson <tim@johnsons-web.com> writes:

> On 2010-02-21, Tim X <timx@nospam.dev.null> wrote:
>>>   Thanks
>>
>> I'm just interested in what the external program you need to call is and
>> what it does?
>>
>> I only mention this as I handle some timeclock/timecard requirements I
>> have using existing emacs functionality. Maybe there is an alternative
>> way to approach this issue that would give a better result. 
>   Hi Tim:
> 	The following data structure:
> 	[[13:22 13:41][14:50 15:04][15:58 16:03][16:53 16:58]]
> 	Is a nested block or list recognized by the rebol programming
> 	language. Each value: 13:22, 15:04 etc is a value of datatype "time!"
> 	and rebol has the ability of easily doing math on such datatypes.
> 	Regardless of what programming language that I might be working in,
> 	I use this approach to "pipe" such a data structure to a simple
> 	application that I have written. That application then returns
> 	the sum, which is inserted into the buffer. (See my initial post)
>
> 	The rebol binary is very easy to install. Small footprint, no
> 	external libraries are necessary. I would be happy to provide
> 	the application as well as the elisp code to manipulate it.
>
> 	As for the elisp code, I now have a function that grabs the data
> 	structure and puts it in the kill-ring.
>
> 	I now have to ask another question myself, related to this subject.
> 	I'd like to copy _only_ the text at the car of the kill ring to a
> 	variable, for further processing use:
> 	Example:
> 	alt-: (car kill-ring) => 
> 	#("[0:58 1:42 0:43 1:41]" 0 20 (fontified t) 20 21 (rear-nonsticky t
> 	fontified t))
> 	How I can copy the first item
> 	"[0:58 1:42 0:43 1:41]"
> 	from the car of kill-ring into a variable?


OK, just in case you don't know, emacs already has all this
functional,functionality. At its simplest, you have timeclock.el, which
enables you to keep a data file to clock time and provide basic
summaries etc. 

In addition to that, various packages, such as org-mode, provide support
for tracking time done on projects, and tasks and can even be used to do
basic billing etc. 

With respect to your other question, if you only need the first element
from the list, I'd extract that before putting it on the kill ring. 

Tim


-- 
tcross (at) rapttech dot com dot au


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

* Re: Inserting output from a program into a buffer
  2010-02-22  4:45               ` Tim Johnson
@ 2010-02-22  9:41                 ` Pascal J. Bourguignon
  2010-02-22 19:44                   ` Tim Johnson
  2010-02-22 23:06                 ` jpkotta
  1 sibling, 1 reply; 14+ messages in thread
From: Pascal J. Bourguignon @ 2010-02-22  9:41 UTC (permalink / raw)
  To: help-gnu-emacs

Tim Johnson <tim@johnsons-web.com> writes:

> On 2010-02-22, Pascal J. Bourguignon <pjb@informatimago.com> wrote:
> <..>
>> You could get a string without attribute using the function
>> buffer-substring-no-properties instead of buffer-substring.
>  Oh! Deja vu here... I believe that what I really need is 
>  buffer-substring-<etc> to copy the region directly to a
>  variable.

In general, functions don't modify variables.  This would be a bad case
of side effect.  In emacs lisp it would be somewhat possible, since all
the variables are dynamic variables.  (In Scheme or Common Lisp it
wouldn't even be possible for lexical variables!).  However, even with
dynamic variables, you cannot modify the variables if they are rebound
in an inner dynamic scope.

(defun f (x)
   (let ((var 42))
      (list var
            (progn (set-var x)
                   var))))

(defun set-var (x) 
   (setq var x))

(defvar var 0)
(set-var 33)
var    ; --> 33        ; seems to work.
(f 22) ; --> (42 22)   ; seems to have worked.
var    ; --> 33        ; but it really didn't worked.

Well, really, it worked as it should but not as you would have wanted.

(Notice that this effect is a design bug in LISP that was detected in
1960 and corrected in the following years by the introduction of lexical
variables.  emacs lisp is somewhat retrograde (or at least conservator)
on this point).


Anyways, it is a bad idea to try to modify variables from called
functions.


What you really need is to BIND the RESULT of buffer-string and other
functions TO a variable.  This is done with LET:

(let ((string (buffer-substring (point-min) (+ 10 (point-min)))))
  (string= "Newsgroups" string))

--> t


Or, in the improbable case you need to keep a value across functions,
you may store it in a global variables (but global variables should be
avoided as side effects in functions trying to modify them).

(defvar *my-text* "")
(setf *my-text* (buffer-substring (point-min) (+ 10 (point-min))))
*my-text* ; --> #("Newsgroups" 0 10 (fontified t face message-header-name))

Notice that while there's no lexical variables to protect against, I
still think that we should use the *x* convention in emacs lisp for
global variables, so that they're not accidentally shadowed by normal
variables (lexical wanabees) such as the poor var above.


But who I am to say such things, I'm not RMS...
-- 
__Pascal Bourguignon__


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

* Re: Inserting output from a program into a buffer
  2010-02-22  9:41                 ` Pascal J. Bourguignon
@ 2010-02-22 19:44                   ` Tim Johnson
  2010-02-22 23:23                     ` Pascal J. Bourguignon
  0 siblings, 1 reply; 14+ messages in thread
From: Tim Johnson @ 2010-02-22 19:44 UTC (permalink / raw)
  To: help-gnu-emacs

On 2010-02-22, Pascal J. Bourguignon <pjb@informatimago.com> wrote:
<...>
> In general, functions don't modify variables.  This would be a bad case
> of side effect.  In emacs lisp it would be somewhat possible, since all
>                    var))))
>
> (Notice that this effect is a design bug in LISP that was detected in
> 1960 and corrected in the following years by the introduction of lexical
> variables.  emacs lisp is somewhat retrograde (or at least conservator)
> on this point).
>
>
> Anyways, it is a bad idea to try to modify variables from called
> functions.
>
>
> What you really need is to BIND the RESULT of buffer-string and other
> functions TO a variable.  This is done with LET:
>
> (let ((string (buffer-substring (point-min) (+ 10 (point-min)))))
>   (string= "Newsgroups" string))
>
> --> t
 Pascal, the total of what you have written is valuable to me in
 understand programming elisp. I don't fully follow all of what you say,
 but I hope that it will soak in.
 I've now written a function that copies a delimited 'form' (sexp or
 other data structure)
 I submit it here for your comments. Your comments will no doubt further
 enlighten me on your insights.
 ;; code follows
(defun tj-copy-previous-region()
  "Grab a symmetrically delimited data structure beginning on the same line
  as the cursor"
  (interactive)
  (let ((boundary(point))found end success (origin (point)))
  (beginning-of-line-text)
  (if (member (char-after) '(40 91 123))
    (progn 
    (setq found (point)))
    (progn
    (setq found(re-search-forward "(\\|\\[\\|{" boundary t))
    (if found
      (progn
      (backward-char 1)
      (setq found (point)))
      (message "*** No Opening delimiter found on this line ***"))))
  (when found
    (forward-sexp)
    (setq end (point))
    (setq success (buffer-substring found end))
    (message "** RESULT: %s ***" success)
    (goto-char origin)) 
  success))
  ;; needs a little more factoring and I note some redundant bindings...
> But who I am to say such things, I'm not RMS...
  :) We all owe RMS a great debt for his contributions and his
  passionate opinions are well known....
thanks
-- 
Tim 
tim@johnsons-web.com
http://www.akwebsoft.com


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

* Re: Inserting output from a program into a buffer
  2010-02-22  4:45               ` Tim Johnson
  2010-02-22  9:41                 ` Pascal J. Bourguignon
@ 2010-02-22 23:06                 ` jpkotta
  1 sibling, 0 replies; 14+ messages in thread
From: jpkotta @ 2010-02-22 23:06 UTC (permalink / raw)
  To: help-gnu-emacs

On Feb 21, 10:45 pm, Tim Johnson <t...@johnsons-web.com> wrote:
> On 2010-02-22, Pascal J. Bourguignon <p...@informatimago.com> wrote:
> <..>> You could get a string without attribute using the function
> > buffer-substring-no-properties instead of buffer-substring.
>
>  Oh! Deja vu here... I believe that what I really need is
>  buffer-substring-<etc> to copy the region directly to a
>  variable.
>  thanks Pascal
>
> --
> Tim
> t...@johnsons-web.comhttp://www.akwebsoft.com

I wrote this a couple of weeks ago, and just extended it to work when
the region is active.

,----
| ;; see also C-u M-|
| (defun insert-from-shell-current-line ()
|   "Run the current line as a shell command, and put the output
|   immediately after it.  If the region is active, use for the
|   command."
|   (interactive "*")
|   (let ((start (line-beginning-position))
|         (stop (line-end-position)))
|     (when (region-active-p)
|       (setq start (region-beginning))
|       (setq stop (region-end)))
|     (goto-char stop)
|     (insert "\n")
|     (insert (shell-command-to-string
|              (buffer-substring start stop)))))
`----



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

* Re: Inserting output from a program into a buffer
  2010-02-22 19:44                   ` Tim Johnson
@ 2010-02-22 23:23                     ` Pascal J. Bourguignon
  2010-02-23  0:44                       ` Tim Johnson
  0 siblings, 1 reply; 14+ messages in thread
From: Pascal J. Bourguignon @ 2010-02-22 23:23 UTC (permalink / raw)
  To: help-gnu-emacs

Tim Johnson <tim@johnsons-web.com> writes:

> On 2010-02-22, Pascal J. Bourguignon <pjb@informatimago.com> wrote:
> <...>
>> In general, functions don't modify variables.  This would be a bad case
>> of side effect.  In emacs lisp it would be somewhat possible, since all
>>                    var))))
>>
>> (Notice that this effect is a design bug in LISP that was detected in
>> 1960 and corrected in the following years by the introduction of lexical
>> variables.  emacs lisp is somewhat retrograde (or at least conservator)
>> on this point).
>>
>>
>> Anyways, it is a bad idea to try to modify variables from called
>> functions.
>>
>>
>> What you really need is to BIND the RESULT of buffer-string and other
>> functions TO a variable.  This is done with LET:
>>
>> (let ((string (buffer-substring (point-min) (+ 10 (point-min)))))
>>   (string= "Newsgroups" string))
>>
>> --> t
>  Pascal, the total of what you have written is valuable to me in
>  understand programming elisp. I don't fully follow all of what you say,
>  but I hope that it will soak in.
>  I've now written a function that copies a delimited 'form' (sexp or
>  other data structure)
>  I submit it here for your comments. Your comments will no doubt further
>  enlighten me on your insights.
>  ;; code follows
> (defun tj-copy-previous-region()
>   "Grab a symmetrically delimited data structure beginning on the same line
>   as the cursor"
>   (interactive)
>   (let ((boundary(point))found end success (origin (point)))
>   (beginning-of-line-text)
>   (if (member (char-after) '(40 91 123))
>     (progn 
>     (setq found (point)))
>     (progn
>     (setq found(re-search-forward "(\\|\\[\\|{" boundary t))
>     (if found
>       (progn
>       (backward-char 1)
>       (setq found (point)))
>       (message "*** No Opening delimiter found on this line ***"))))
>   (when found
>     (forward-sexp)
>     (setq end (point))
>     (setq success (buffer-substring found end))
>     (message "** RESULT: %s ***" success)
>     (goto-char origin)) 
>   success))
>   ;; needs a little more factoring and I note some redundant bindings...
>> But who I am to say such things, I'm not RMS...
>   :) We all owe RMS a great debt for his contributions and his
>   passionate opinions are well known....
> thanks


You could write it like this:

(defun tj-copy-previous-region ()
  "Save a list surrounding or before the cursor into the kill-ring."
  (interactive)
  (save-excursion
    (let ((boundary (point)))
      (beginning-of-line-text)
      (if (re-search-forward "[([{]" boundary t)
          (let ((start (match-beginning 0)))
            (goto-char (1- start))
            (forward-sexp)
            (kill-ring-save start (point)))
          (error "No Opening delimiter found on this line")))))

aaaa (1 2 3 4 5 6 7 8 9) zzzz
             ^ M-x tj-copy-previous-region RET C-n C-n C-y inserts: 
(1 2 3 4 5 6 7 8 9)

Notice: 

- spaces before opening parentheses not preceded by an opening
  parenthesis.

- indentation done automatically by emacs.

- avoidance of setf or setq.

- do actually what was documented (by the function name), ie. use
  kill-ring-save instead of buffer-substring.

- use of save-excursion to save the excursion

- use of error to deal with exceptional cases instead of just message.
  error will do the right thing depending on the local circumstances
  (ie. it will message the error in interactive use, but enter the
  debugger or have the error otherwise handled if the programmer asks it
  to).


-- 
__Pascal Bourguignon__


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

* Re: Inserting output from a program into a buffer
  2010-02-22 23:23                     ` Pascal J. Bourguignon
@ 2010-02-23  0:44                       ` Tim Johnson
  0 siblings, 0 replies; 14+ messages in thread
From: Tim Johnson @ 2010-02-23  0:44 UTC (permalink / raw)
  To: help-gnu-emacs

On 2010-02-22, Pascal J. Bourguignon <pjb@informatimago.com> wrote:
> Tim Johnson <tim@johnsons-web.com> writes:
>
>> On 2010-02-22, Pascal J. Bourguignon <pjb@informatimago.com> wrote:
> You could write it like this:
>
> (defun tj-copy-previous-region ()
>   "Save a list surrounding or before the cursor into the kill-ring."
>   (interactive)
>   (save-excursion
>     (let ((boundary (point)))
>       (beginning-of-line-text)
>       (if (re-search-forward "[([{]" boundary t)
>           (let ((start (match-beginning 0)))
>             (goto-char (1- start))
>             (forward-sexp)
>             (kill-ring-save start (point)))
>           (error "No Opening delimiter found on this line")))))
>
> aaaa (1 2 3 4 5 6 7 8 9) zzzz
>              ^ M-x tj-copy-previous-region RET C-n C-n C-y inserts: 
> (1 2 3 4 5 6 7 8 9)
Very nice illustration.
> Notice: 
>
> - spaces before opening parentheses not preceded by an opening
>   parenthesis.
 Understood.
> - indentation done automatically by emacs.
 (blush) was using vim.
> - avoidance of setf or setq.
 Thus the nested 'lets. Aha!
> - do actually what was documented (by the function name), ie. use
>   kill-ring-save instead of buffer-substring.
 K
> - use of save-excursion to save the excursion
 K
> - use of error to deal with exceptional cases instead of just message.
>   error will do the right thing depending on the local circumstances
>   (ie. it will message the error in interactive use, but enter the
>   debugger or have the error otherwise handled if the programmer asks it
>   to).
K

Thank you very much for this lesson
Regards
-- 
Tim 
tim@johnsons-web.com
http://www.akwebsoft.com


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

end of thread, other threads:[~2010-02-23  0:44 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-02-21  2:45 Inserting output from a program into a buffer Tim Johnson
2010-02-21  3:15 ` Tim Landscheidt
2010-02-21  4:45   ` Tim Johnson
2010-02-21  5:02     ` Barry Margolin
     [not found]       ` <slrnho2o6e.49k.tim@bart.johnson.com>
     [not found]         ` <87zl324774.fsf@lion.rapttech.com.au>
2010-02-22  0:45           ` Tim Johnson
2010-02-22  1:57             ` Pascal J. Bourguignon
2010-02-22  4:45               ` Tim Johnson
2010-02-22  9:41                 ` Pascal J. Bourguignon
2010-02-22 19:44                   ` Tim Johnson
2010-02-22 23:23                     ` Pascal J. Bourguignon
2010-02-23  0:44                       ` Tim Johnson
2010-02-22 23:06                 ` jpkotta
2010-02-22  7:22             ` Tim X
2010-02-21  6:28     ` tomas

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).