unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* elisp function that evaluates a line up to last character
@ 2019-03-19 17:15 Jason Thomas
  2019-03-19 18:39 ` Eric Abrahamsen
  0 siblings, 1 reply; 3+ messages in thread
From: Jason Thomas @ 2019-03-19 17:15 UTC (permalink / raw)
  To: help-gnu-emacs

Hello,

I am trying to write a function that will (1) take a new region, defined by
the first character in the current line (where the cursor is) and end the
region at the penultimate character in the same line; and
(2) pass this text to another function (called ess-eval-region).

Basically, I am trying to evaluate some code that ends with a semi-colon,
but I need to leave out the semi-colon (during evaluation).  Here is what I
have so far (at the end, I try to bind this function to <C-return> and
activate
this binding in ess mode):

(defun my-stata-eval-delim-line (&optional beg end)
  (interactive)
  (let ((beg (cond (beg beg)
                   ((region-active-p)
                    (region-beginning))
                   (t (line-beginning-position))))
        (end (cond (end end)
                   ((region-active-p)
                    (copy-marker (region-end))
                    (backward-char))
                   (t (line-end-position)))))
    (ess-eval-region beg end)))
(eval-after-load 'ess-stata-mode
                 '(define-key ess-mode-map "<C-return>"
'my-stata-eval-delim-line))

but I keep getting an error when I try to use the function on the following
line:

ds;

  "Wrong number of arguments: (3 . 5), 2"

Any suggestions/hints about what I am doing wrong (I'm sure there is a long
list)
is greatly appreciated!

Thanks in advance,
Jason


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

* Re: elisp function that evaluates a line up to last character
  2019-03-19 17:15 elisp function that evaluates a line up to last character Jason Thomas
@ 2019-03-19 18:39 ` Eric Abrahamsen
  2019-03-19 18:50   ` Eric Abrahamsen
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Abrahamsen @ 2019-03-19 18:39 UTC (permalink / raw)
  To: help-gnu-emacs

Jason Thomas <jarathomas@gmail.com> writes:

> Hello,
>
> I am trying to write a function that will (1) take a new region, defined by
> the first character in the current line (where the cursor is) and end the
> region at the penultimate character in the same line; and
> (2) pass this text to another function (called ess-eval-region).
>
> Basically, I am trying to evaluate some code that ends with a semi-colon,
> but I need to leave out the semi-colon (during evaluation).  Here is what I
> have so far (at the end, I try to bind this function to <C-return> and
> activate
> this binding in ess mode):
>
> (defun my-stata-eval-delim-line (&optional beg end)
>   (interactive)
>   (let ((beg (cond (beg beg)
>                    ((region-active-p)
>                     (region-beginning))
>                    (t (line-beginning-position))))
>         (end (cond (end end)
>                    ((region-active-p)
>                     (copy-marker (region-end))
>                     (backward-char))
>                    (t (line-end-position)))))
>     (ess-eval-region beg end)))
> (eval-after-load 'ess-stata-mode
>                  '(define-key ess-mode-map "<C-return>"
> 'my-stata-eval-delim-line))
>
> but I keep getting an error when I try to use the function on the following
> line:
>
> ds;
>
>   "Wrong number of arguments: (3 . 5), 2"

This just looks like `ess-eval-region' requires between 3 and 5 arguments,
but you've only given it two: beg and end.

Depending on how you're going to use this function, might also consider
using the "r" interactive code, sort of like:

(defun my-stata-eval-delim-line (&optional beg end)
  (interactive "r")
  (let ((beg (if (region-active-p)
		 beg
               (line-beginning-position)))
        (end (if (region-active-p)
		 end
               (- (line-end-position) 1))))
    (ess-eval-region beg end)))




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

* Re: elisp function that evaluates a line up to last character
  2019-03-19 18:39 ` Eric Abrahamsen
@ 2019-03-19 18:50   ` Eric Abrahamsen
  0 siblings, 0 replies; 3+ messages in thread
From: Eric Abrahamsen @ 2019-03-19 18:50 UTC (permalink / raw)
  To: help-gnu-emacs

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> Jason Thomas <jarathomas@gmail.com> writes:
>
>> Hello,
>>
>> I am trying to write a function that will (1) take a new region, defined by
>> the first character in the current line (where the cursor is) and end the
>> region at the penultimate character in the same line; and
>> (2) pass this text to another function (called ess-eval-region).
>>
>> Basically, I am trying to evaluate some code that ends with a semi-colon,
>> but I need to leave out the semi-colon (during evaluation).  Here is what I
>> have so far (at the end, I try to bind this function to <C-return> and
>> activate
>> this binding in ess mode):
>>
>> (defun my-stata-eval-delim-line (&optional beg end)
>>   (interactive)
>>   (let ((beg (cond (beg beg)
>>                    ((region-active-p)
>>                     (region-beginning))
>>                    (t (line-beginning-position))))
>>         (end (cond (end end)
>>                    ((region-active-p)
>>                     (copy-marker (region-end))
>>                     (backward-char))
>>                    (t (line-end-position)))))
>>     (ess-eval-region beg end)))
>> (eval-after-load 'ess-stata-mode
>>                  '(define-key ess-mode-map "<C-return>"
>> 'my-stata-eval-delim-line))
>>
>> but I keep getting an error when I try to use the function on the following
>> line:
>>
>> ds;
>>
>>   "Wrong number of arguments: (3 . 5), 2"
>
> This just looks like `ess-eval-region' requires between 3 and 5 arguments,
> but you've only given it two: beg and end.
>
> Depending on how you're going to use this function, might also consider
> using the "r" interactive code, sort of like:
>
> (defun my-stata-eval-delim-line (&optional beg end)
>   (interactive "r")
>   (let ((beg (if (region-active-p)
> 		 beg
>                (line-beginning-position)))
>         (end (if (region-active-p)
> 		 end
>                (- (line-end-position) 1))))
>     (ess-eval-region beg end)))

Or stick it in the interactive call -- I can never remember what the
recommended approach is:

(defun my-stata-eval-delim-line (beg end)
  (interactive
   (if (region-active-p)
       (list (region-beginning) (region-end))
     (list (line-beginning-position)
	   (- (line-end-position) 1))))
  (ess-eval-region beg end))

This way you can call it three ways: interactively with no region,
interactively with region, and programmatically with two arguments.




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

end of thread, other threads:[~2019-03-19 18:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-03-19 17:15 elisp function that evaluates a line up to last character Jason Thomas
2019-03-19 18:39 ` Eric Abrahamsen
2019-03-19 18:50   ` Eric Abrahamsen

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