unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* favorite subject DWIM
@ 2021-08-12 10:41 Emanuel Berg via Users list for the GNU Emacs text editor
  2021-08-15  4:00 ` Abhiseck Paira
  0 siblings, 1 reply; 3+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-08-12 10:41 UTC (permalink / raw)
  To: help-gnu-emacs

I've done this so many times each time doing it in a slightly
different way (I'm sure) so I thought one would do an "ideal"
DWIM function and find out once for all how to do it...

So instead of "(list bg ed)", do something funkadelic...

?

;;; -*- lexical-binding: t -*-
;;;
;;; this file:
;;;   https://dataswamp.org/~incal/public_html/emacs-init/dwim.el

(defun test-dwim (&optional beg end)
  (interactive (when (use-region-p)
                 (list (region-beginning) (region-end)) ))
  (let ((bg (or beg (point-min)))
        (ed (or end (point-max))) )
    (list bg ed) ))

(when nil
  (progn
    (set-mark 10)
    (forward-line 3)
    (call-interactively #'test-dwim) ) ; (10 500)

  (call-interactively #'test-dwim)     ; ( 1 604)

  (test-dwim 30 100)                   ; (30 100)

  (test-dwim)                          ; ( 1 604)
)

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: favorite subject DWIM
  2021-08-12 10:41 favorite subject DWIM Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-08-15  4:00 ` Abhiseck Paira
  2021-08-15  4:30   ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 3+ messages in thread
From: Abhiseck Paira @ 2021-08-15  4:00 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 1360 bytes --]



> ;;; -*- lexical-binding: t -*-
> ;;;
> ;;; this file:
> ;;;   https://dataswamp.org/~incal/public_html/emacs-init/dwim.el

This file is not found (404).

> (defun test-dwim (&optional beg end)
>   (interactive (when (use-region-p)
>                  (list (region-beginning) (region-end)) ))
>   (let ((bg (or beg (point-min)))
>         (ed (or end (point-max))) )
>     (list bg ed) ))

Why do you have interactive declaration? The return value (list bg
ed) totally useless to user. Wouldn't most of the time this function be
called by some other function?

Nit-picking: I think the let form is unnecessary.

(defun test-dwim (&optional beg end)
  (interactive (when (use-region-p)
                 (list (region-beginning) (region-end))))
  (list (or beg (point-min))
        (or end (point-max))))

> (when nil
>   (progn
>     (set-mark 10)
>     (forward-line 3)
>     (call-interactively #'test-dwim) ) ; (10 500)
>
>   (call-interactively #'test-dwim)     ; ( 1 604)
>
>   (test-dwim 30 100)                   ; (30 100)
>
>   (test-dwim)                          ; ( 1 604)
> )

Since I couldn't find your file, I may be lacking context, what it seems
here you're testing the function?


-- 
Abhiseck Paira
E34E 825B 979E EB9F 8505  F80E E93D 353B 7740 0709
https://social.linux.pizza/@redstarfish

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

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

* Re: favorite subject DWIM
  2021-08-15  4:00 ` Abhiseck Paira
@ 2021-08-15  4:30   ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 3+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-08-15  4:30 UTC (permalink / raw)
  To: help-gnu-emacs

Abhiseck Paira wrote:

>> ;;; -*- lexical-binding: t -*-
>> ;;;
>> ;;; this file:
>> ;;;   https://dataswamp.org/~incal/public_html/emacs-init/dwim.el
>
> This file is not found (404).

You are right, here is the correct URL:

  https://dataswamp.org/~incal/emacs-init/dwim.el

I still yank the whole file, last.

>> (defun test-dwim (&optional beg end)
>>   (interactive (when (use-region-p)
>>                  (list (region-beginning) (region-end)) ))
>>   (let ((bg (or beg (point-min)))
>>         (ed (or end (point-max))) )
>>     (list bg ed) ))
>
> Why do you have interactive declaration? [...] Wouldn't most
> of the time this function be called by some other function?

It depends on the actual function but since it involves the
region it is probably used most often interactively.

> Nit-picking: I think the let form is unnecessary.

Here the values are only returned but one can think of more
complicated stuff where the values are used repeatedly.

But actually it isn't a bad thing to be clear about that step
and get it over first thing, it is needed for the &optional
arguments which will be nil if called from Lisp
without arguments.

>> (when nil
>>   (progn
>>     (set-mark 10)
>>     (forward-line 3)
>>     (call-interactively #'test-dwim) ) ; (10 500)
>>
>>   (call-interactively #'test-dwim)     ; ( 1 604)
>>
>>   (test-dwim 30 100)                   ; (30 100)
>>
>>   (test-dwim)                          ; ( 1 604)
>> )
>
> Since I couldn't find your file, I may be lacking context,
> what it seems here you're testing the function?

Yes, as you see it can be used with a region, without a region
(whole buffer acted upon - DWIM), with arguments from Lisp,
and without - in which case again the whole buffer will be
used, so the interactive/no-region is analogous to
from-Lisp/no-arguments.

DWIM!

;;; -*- lexical-binding: t -*-
;;;
;;; this file:
;;;   https://dataswamp.org/~incal/emacs-init/dwim.el

(defun test-dwim (&optional beg end)
  (interactive (when (use-region-p)
                 (list (region-beginning) (region-end)) ))
  (let ((bg (or beg (point-min)))
        (ed (or end (point-max))) )
    (list bg ed) ))

(when nil
  (progn
    (set-mark 10)
    (forward-line 3)
    (call-interactively #'test-dwim) ) ; (10 500)

  (call-interactively #'test-dwim)     ; ( 1 604)

  (test-dwim 30 100)                   ; (30 100)

  (test-dwim)                          ; ( 1 604)
)

-- 
underground experts united
https://dataswamp.org/~incal




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

end of thread, other threads:[~2021-08-15  4:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-08-12 10:41 favorite subject DWIM Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-15  4:00 ` Abhiseck Paira
2021-08-15  4:30   ` Emanuel Berg via Users list for the GNU Emacs text editor

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