all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* yank-repeat-newline
@ 2011-07-26 19:12 Andreas Röhler
  2011-07-26 19:19 ` yank-repeat-newline Teemu Likonen
  2011-07-26 19:32 ` yank-repeat-newline Drew Adams
  0 siblings, 2 replies; 7+ messages in thread
From: Andreas Röhler @ 2011-07-26 19:12 UTC (permalink / raw
  To: help-gnu-emacs@gnu.org List

Hi,

there was a thread "repeating yank" on this list, which resulted here in 
two functions:

(defun yank-repeat (arg)
   "With numerical ARG, repeat last yank ARG times. "
   (interactive "p*")
   (dotimes (i arg)
     (insert (car kill-ring))))

(defun yank-repeat-newline (arg)
   "With numerical ARG, repeat last yank ARG times,
also insert a newline. "
   (interactive "p*")
   (dotimes (i arg)
     (insert (concat (car kill-ring) "\n"))))

Now when trying to combine both, get difficulties

;; not working now
(defun yank-repeat-newline (arg &optional nl)
   "With numerical ARG, repeat last yank ARG times.
With optional arg NL, also insert newlines. "
   (interactive "p\nP*")
   (let ((nl nl)
         (num arg))
     (dotimes (i num)
       (if nl
           (insert (concat (car kill-ring) "\n"))
         (insert (car kill-ring))))))

What is the recommended way writing that?

Thanks,

Andreas

--
https://launchpad.net/python-mode
https://launchpad.net/s-x-emacs-werkstatt/



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

* Re: yank-repeat-newline
  2011-07-26 19:12 yank-repeat-newline Andreas Röhler
@ 2011-07-26 19:19 ` Teemu Likonen
  2011-07-26 19:24   ` yank-repeat-newline Andreas Röhler
  2011-07-26 19:32 ` yank-repeat-newline Drew Adams
  1 sibling, 1 reply; 7+ messages in thread
From: Teemu Likonen @ 2011-07-26 19:19 UTC (permalink / raw
  To: Andreas Röhler; +Cc: help-gnu-emacs

* 2011-07-26T21:12:35+02:00 * Andreas Röhler wrote:

> Now when trying to combine both, get difficulties
>
> ;; not working now
> (defun yank-repeat-newline (arg &optional nl)
>   "With numerical ARG, repeat last yank ARG times.
> With optional arg NL, also insert newlines. "
>   (interactive "p\nP*")
>   (let ((nl nl)
>         (num arg))
>     (dotimes (i num)
>       (if nl
>           (insert (concat (car kill-ring) "\n"))
>         (insert (car kill-ring))))))
>
> What is the recommended way writing that?

Please tell us what you want. How should the command work? How do you
want to execute it and what should it do?



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

* Re: yank-repeat-newline
  2011-07-26 19:19 ` yank-repeat-newline Teemu Likonen
@ 2011-07-26 19:24   ` Andreas Röhler
  2011-07-26 19:35     ` yank-repeat-newline Teemu Likonen
  0 siblings, 1 reply; 7+ messages in thread
From: Andreas Röhler @ 2011-07-26 19:24 UTC (permalink / raw
  To: Teemu Likonen; +Cc: help-gnu-emacs

Am 26.07.2011 21:19, schrieb Teemu Likonen:
> * 2011-07-26T21:12:35+02:00 * Andreas Röhler wrote:
>
>> Now when trying to combine both, get difficulties
>>
>> ;; not working now
>> (defun yank-repeat-newline (arg&optional nl)
>>    "With numerical ARG, repeat last yank ARG times.
>> With optional arg NL, also insert newlines. "
>>    (interactive "p\nP*")
>>    (let ((nl nl)
>>          (num arg))
>>      (dotimes (i num)
>>        (if nl
>>            (insert (concat (car kill-ring) "\n"))
>>          (insert (car kill-ring))))))
>>
>> What is the recommended way writing that?
>
> Please tell us what you want. How should the command work? How do you
> want to execute it and what should it do?
>

It's in the docstring: repeat last yank resp. to numerical arg.
Also I want in some cases have a newline added - ie insert yanks vertically.



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

* RE: yank-repeat-newline
  2011-07-26 19:12 yank-repeat-newline Andreas Röhler
  2011-07-26 19:19 ` yank-repeat-newline Teemu Likonen
@ 2011-07-26 19:32 ` Drew Adams
  1 sibling, 0 replies; 7+ messages in thread
From: Drew Adams @ 2011-07-26 19:32 UTC (permalink / raw
  To: 'Andreas Röhler', help-gnu-emacs

> (defun yank-repeat-newline (arg &optional nl)
>    "With numerical ARG, repeat last yank ARG times.
> With optional arg NL, also insert newlines. "
>    (interactive "p\nP*")
>    (let ((nl nl)
>          (num arg))
>      (dotimes (i num)
>        (if nl
>            (insert (concat (car kill-ring) "\n"))
>          (insert (car kill-ring))))))

You don't need the `let'.

But you cannot do what your doc string claims, at least not the way you're
trying.

As Teemu explained, there is only _one_ prefix arg. You can look at either its
numeric value or its raw value or both, but there is only ONE prefix arg.

You apparently want to have a prefix arg express both a numeric quantity and a
boolean.  If the user uses C-u (or its variants) to specify a (non-nil) prefix
arg then, well, the raw value is non-nil.  If the raw value is nil, then the
user did not use C-u (or its variants).

If you want to let the user specify a numeric value, default 1, and also specify
whether to add a newline, then one way to do that is to distinguish positive
from negative prefix arg (there's your boolean).

E.g.:

M-x yank...    -> just one, no newline
M-- yank...    -> one, newline
C-u -1 yank... -> one, newline
C-u -2 yank... -> two, newlines
C-u  2 yank... -> two, no newlines

Something like this:

(defun myyank (&optional arg)
  (interactive "p")
  (dotimes (i (abs arg))
    (if (natnump arg)
	(insert (car kill-ring))
      (insert (concat (car kill-ring) "\n")))))




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

* Re: yank-repeat-newline
  2011-07-26 19:24   ` yank-repeat-newline Andreas Röhler
@ 2011-07-26 19:35     ` Teemu Likonen
  2011-07-26 19:45       ` yank-repeat-newline Andreas Röhler
  0 siblings, 1 reply; 7+ messages in thread
From: Teemu Likonen @ 2011-07-26 19:35 UTC (permalink / raw
  To: Andreas Röhler; +Cc: help-gnu-emacs

* 2011-07-26T21:24:03+02:00 * Andreas Röhler wrote:

> Am 26.07.2011 21:19, schrieb Teemu Likonen:
>> Please tell us what you want. How should the command work? How do you
>> want to execute it and what should it do?

> It's in the docstring: repeat last yank resp. to numerical arg. Also I
> want in some cases have a newline added - ie insert yanks vertically.

Yes, but how do you want to execute those "some cases"? For example, you
could make the command ask:


    (defun yank-repeat-newline (arg &optional nl)
      "With numerical ARG, repeat last yank ARG times.
    With optional arg NL, also insert newlines. "
      (interactive (list (prefix-numeric-value current-prefix-arg)
                         (y-or-n-p "Add a newline? ")))
      (dotimes (i arg)
        (insert (car kill-ring))
        (if nl (insert "\n"))))



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

* Re: yank-repeat-newline
  2011-07-26 19:35     ` yank-repeat-newline Teemu Likonen
@ 2011-07-26 19:45       ` Andreas Röhler
  2011-07-26 20:33         ` yank-repeat-newline Drew Adams
  0 siblings, 1 reply; 7+ messages in thread
From: Andreas Röhler @ 2011-07-26 19:45 UTC (permalink / raw
  To: Teemu Likonen; +Cc: help-gnu-emacs

Am 26.07.2011 21:35, schrieb Teemu Likonen:
> * 2011-07-26T21:24:03+02:00 * Andreas Röhler wrote:
>
>> Am 26.07.2011 21:19, schrieb Teemu Likonen:
>>> Please tell us what you want. How should the command work? How do you
>>> want to execute it and what should it do?
>
>> It's in the docstring: repeat last yank resp. to numerical arg. Also I
>> want in some cases have a newline added - ie insert yanks vertically.
>
> Yes, but how do you want to execute those "some cases"? For example, you
> could make the command ask:
>
>
>      (defun yank-repeat-newline (arg&optional nl)
>        "With numerical ARG, repeat last yank ARG times.
>      With optional arg NL, also insert newlines. "
>        (interactive (list (prefix-numeric-value current-prefix-arg)
>                           (y-or-n-p "Add a newline? ")))
>        (dotimes (i arg)
>          (insert (car kill-ring))
>          (if nl (insert "\n"))))
>

Hi Teemu, hi Drew,

thanks a lot both.

Can still only one argument specify in example above.
Drew presented a solution indeed.

However, after all think Emacs could make a better use of these both 
interactive codes by separating it. It's a kind of interference with "P" 
"p" which makes things more complex then needed and doesn't contribute - 
IMHO.

Will present the matter at emacs-devel.

So far,

Andreas







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

* RE: yank-repeat-newline
  2011-07-26 19:45       ` yank-repeat-newline Andreas Röhler
@ 2011-07-26 20:33         ` Drew Adams
  0 siblings, 0 replies; 7+ messages in thread
From: Drew Adams @ 2011-07-26 20:33 UTC (permalink / raw
  To: 'Andreas Röhler', 'Teemu Likonen'
  Cc: help-gnu-emacs, emacs-devel

> However, after all think Emacs could make a better use of these both 
> interactive codes by separating it. It's a kind of interference with
> "P" "p" which makes things more complex then needed and doesn't 
> contribute - IMHO.
> Will present the matter at emacs-devel.

I suggest you think more about it, Andreas.

You cannot "separate" the two behaviors if they are represented by exactly the
same argument (value).  How can any given prefix arg - that is, one single
value, represent both the number of yanks and whether to add a newline to each
yank?  If it is one value then it cannot distinguish two things.

Things are even simpler - you can forget about arguments, prefix or otherwise.
You cannot separate the two command behaviors if you want them to be triggered
by the _same user input event_ and you want nothing else to affect the behavior,
besides the user.  It does not matter how you communicate the user behavior to
the command (prefix arg or otherwise): the problem is the same.

Only the user behavior can distinguish the behaviors you want (unless you want
to distinguish based on tide levels, time of day, etc.).  So you need two
different user behaviors - the user needs some way to let your command know what
to do.

Having the user distinguish using positive and non-positive prefix args is one
way.  But you need _some_ user-level difference.

Describe to yourself exactly what you expect the different user inputs to be,
which would distinguish the command behavior cases.  Figure out what you want
the _user_ to do differently in the two cases and then you'll know how to code
it to get the behavior you want.

Just think a little more about it, and I'm sure you'll have a Eureka moment.




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

end of thread, other threads:[~2011-07-26 20:33 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-07-26 19:12 yank-repeat-newline Andreas Röhler
2011-07-26 19:19 ` yank-repeat-newline Teemu Likonen
2011-07-26 19:24   ` yank-repeat-newline Andreas Röhler
2011-07-26 19:35     ` yank-repeat-newline Teemu Likonen
2011-07-26 19:45       ` yank-repeat-newline Andreas Röhler
2011-07-26 20:33         ` yank-repeat-newline Drew Adams
2011-07-26 19:32 ` yank-repeat-newline Drew Adams

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.