help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: elisp function that evaluates a line up to last character


From: Eric Abrahamsen
Subject: Re: elisp function that evaluates a line up to last character
Date: Tue, 19 Mar 2019 11:50:39 -0700
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

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.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]