all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* same data appears twice, interactive then function body
@ 2020-12-16  1:23 Emanuel Berg via Users list for the GNU Emacs text editor
  2020-12-16  2:44 ` Stefan Monnier
  0 siblings, 1 reply; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-16  1:23 UTC (permalink / raw)
  To: help-gnu-emacs

Check out this code, how do you avoid having 80 appear twice?

(defun insert-string-centered (string &optional width)
  (interactive
   (list (read-from-minibuffer "string: ")
         (string-to-number (read-from-minibuffer "width [80]: "))) )
  (let*((max         (if (< 0 width) width 80))
        (str-len     (length string))
        (padding     (+ (/ (- max str-len) 2) (if (= 0 (mod str-len 2)) 1 0)))
        (padding-str (make-string padding ?\s)) )
    (insert padding-str string) ))

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: same data appears twice, interactive then function body
  2020-12-16  1:23 same data appears twice, interactive then function body Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-12-16  2:44 ` Stefan Monnier
  2020-12-16  4:02   ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 15+ messages in thread
From: Stefan Monnier @ 2020-12-16  2:44 UTC (permalink / raw)
  To: help-gnu-emacs

> Check out this code, how do you avoid having 80 appear twice?

> (defun insert-string-centered (string &optional width)
>   (interactive
>    (list (read-from-minibuffer "string: ")
>          (string-to-number (read-from-minibuffer "width [80]: "))) )
>   (let*((max         (if (< 0 width) width 80))
> [...]

Here's my take on it:

    (defun insert-string-centered (string &optional width)
      (interactive
       (list (read-from-minibuffer "string: ")
             (string-to-number (read-from-minibuffer "width [80]: "))) )
      (let*((max         (if (< 0 width) width (+ 42 42 -4)))
    [...]

Now the question is how to avoid having 42 appear twice, I guess,


        Stefan




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

* Re: same data appears twice, interactive then function body
  2020-12-16  2:44 ` Stefan Monnier
@ 2020-12-16  4:02   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-12-16  4:16     ` 2QdxY4RzWzUUiLuE
  2020-12-16  4:16     ` Jean Louis
  0 siblings, 2 replies; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-16  4:02 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier wrote:

> Here's my take on it:
>
>     (defun insert-string-centered (string &optional width)
>       (interactive
>        (list (read-from-minibuffer "string: ")
>              (string-to-number (read-from-minibuffer "width [80]: "))) )
>       (let*((max         (if (< 0 width) width (+ 42 42 -4)))
>     [...]
>
> Now the question is how to avoid having 42 appear twice,
> I guess

Ha :)

No but do tell!

Maybe it can't be done, even?

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: same data appears twice, interactive then function body
  2020-12-16  4:02   ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-12-16  4:16     ` 2QdxY4RzWzUUiLuE
  2020-12-16  4:31       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-12-16  4:16     ` Jean Louis
  1 sibling, 1 reply; 15+ messages in thread
From: 2QdxY4RzWzUUiLuE @ 2020-12-16  4:16 UTC (permalink / raw)
  To: help-gnu-emacs

On 2020-12-16 at 05:02:36 +0100,
Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:

> Stefan Monnier wrote:
> 
> > Here's my take on it:
> >
> >     (defun insert-string-centered (string &optional width)
> >       (interactive
> >        (list (read-from-minibuffer "string: ")
> >              (string-to-number (read-from-minibuffer "width [80]: "))) )
> >       (let*((max         (if (< 0 width) width (+ 42 42 -4)))
> >     [...]
> >
> > Now the question is how to avoid having 42 appear twice,
> > I guess
> 
> Ha :)
> 
> No but do tell!
> 
> Maybe it can't be done, even?

That depends on the unspecified requirements and constraints).

What about a defvar outside the function (completely untested)?

(defvar *default-width* 80
  "default width")
(defun insert-string-centered (string &optional width)
  (interactive
    (list (read-from-minibuffer "string: ")
          (string-to-number
            (read-from-minibuffer
              (format "width [%d]: " *max-width*)))))
  (let* ((max (if (< 0 width) width *default-width*)))
    ...))



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

* Re: same data appears twice, interactive then function body
  2020-12-16  4:02   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-12-16  4:16     ` 2QdxY4RzWzUUiLuE
@ 2020-12-16  4:16     ` Jean Louis
  2020-12-16  4:32       ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 15+ messages in thread
From: Jean Louis @ 2020-12-16  4:16 UTC (permalink / raw)
  To: help-gnu-emacs

* Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2020-12-16 07:03]:
> Stefan Monnier wrote:
> 
> > Here's my take on it:
> >
> >     (defun insert-string-centered (string &optional width)
> >       (interactive
> >        (list (read-from-minibuffer "string: ")
> >              (string-to-number (read-from-minibuffer "width [80]: "))) )
> >       (let*((max         (if (< 0 width) width (+ 42 42 -4)))
> >     [...]
> >
> > Now the question is how to avoid having 42 appear twice,
> > I guess

I remember from micro computers that we used division in this way
to find the center.

(defun insert-centered-text (text &optional width)
  (interactive "MText: ")
  (let* ((width (or width (window-text-width)))
	 (length (length (string-trim text)))
	 (pos (/ (- width length) 2)))
    (move-to-column pos t)
    (insert text)
    (insert "\n")))



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

* Re: same data appears twice, interactive then function body
  2020-12-16  4:16     ` 2QdxY4RzWzUUiLuE
@ 2020-12-16  4:31       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-12-16  4:36         ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-16  4:31 UTC (permalink / raw)
  To: help-gnu-emacs

2QdxY4RzWzUUiLuE wrote:

> What about a defvar outside the function (completely
> untested)?
>
> (defvar *default-width* 80
>   "default width")

:)

BTW one is discouraged from using that *cl-convention*
in Emacs.

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: same data appears twice, interactive then function body
  2020-12-16  4:16     ` Jean Louis
@ 2020-12-16  4:32       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-12-16  4:43         ` Yuri Khan
  0 siblings, 1 reply; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-16  4:32 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

> (defun insert-centered-text (text &optional width)
>   (interactive "MText: ")
>   (let* ((width (or width (window-text-width)))
> 	 (length (length (string-trim text)))
> 	 (pos (/ (- width length) 2)))
>     (move-to-column pos t)
>     (insert text)
>     (insert "\n")))

Oh, so there is a `window-text-width'!

Great, that will do!

Thanks!

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: same data appears twice, interactive then function body
  2020-12-16  4:31       ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-12-16  4:36         ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-16  4:36 UTC (permalink / raw)
  To: help-gnu-emacs

(defun insert-string-center (string &optional width)
  (interactive
   (list (read-from-minibuffer "string: ")
         (string-to-number (read-from-minibuffer (format "width [%s]: " (window-text-width))))) )
  (let*((max         (if (< 0 width) width (window-text-width)))
        (str-len     (length string))
        (padding     (+ (/ (- max str-len) 2) (if (= 0 (mod str-len 2)) 1 0)))
        (padding-str (make-string padding ?\s)) )
    (insert padding-str string) ))

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: same data appears twice, interactive then function body
  2020-12-16  4:32       ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-12-16  4:43         ` Yuri Khan
  2020-12-16  5:19           ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 15+ messages in thread
From: Yuri Khan @ 2020-12-16  4:43 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Wed, 16 Dec 2020 at 11:35, Emanuel Berg via Users list for the GNU
Emacs text editor <help-gnu-emacs@gnu.org> wrote:

> Oh, so there is a `window-text-width'!

Depending on what you are trying to achieve, ‘fill-column’ might fit better.



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

* Re: same data appears twice, interactive then function body
  2020-12-16  4:43         ` Yuri Khan
@ 2020-12-16  5:19           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-12-18 14:50             ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-16  5:19 UTC (permalink / raw)
  To: help-gnu-emacs

Yuri Khan wrote:

>> Oh, so there is a `window-text-width'!
>
> Depending on what you are trying to achieve, ‘fill-column’
> might fit better.

It works great! I realized you don't even need
`window-text-width' twice! [improved code last]

I use this with files like this:

  https://dataswamp.org/~incal/FILM

(defun insert-string-centered (string &optional width)
  (interactive
   (list (read-from-minibuffer "string: ")
         (string-to-number (read-from-minibuffer "width [window]: " ))))
  (let*((max         (if (< 0 width) width (window-text-width)))
        (str-len     (length string))
        (padding     (- (/ (- max str-len) 2)
                        (if (= 0 (mod str-len 2)) 1 0)) )
        (padding-str (make-string padding ?\s)) )
    (insert padding-str string) ))
(defalias 'isc #'insert-string-centered)

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: same data appears twice, interactive then function body
  2020-12-16  5:19           ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-12-18 14:50             ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-12-18 16:35               ` Drew Adams
  2020-12-18 18:09               ` Jean Louis
  0 siblings, 2 replies; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-18 14:50 UTC (permalink / raw)
  To: help-gnu-emacs

Technologist Adams was kind enough to write me a private mail
telling me my function `insert-string-centered' didn't work
when used from Lisp. Well, that's what I think anyway, since
I received it in my mailbox with no reference in the headers
to either gmane.emacs.help or help-gnu-emacs@gnu.org, and it
doesn't seem to be anywhere else to be found, either!

Probably, he wanted to save me from being all embarrassed in
from of everyone. Well, that was kind! But no worries!
Actually, and to be frank about it, this isn't the first
mistake I ever ever made as a programmer...

Anyway here is a new version, that God willing will work even
under such severe circumstances...

(defun insert-string-centered (str &optional width)
  (interactive
   (list (read-from-minibuffer "string: ")
         (string-to-number (read-from-minibuffer "width [window]: " )) ))
  (let*((span    (if (and width (< 0 width)) width (window-text-width)))
        (str-len (length str))
        (pad     (- (/ (- span str-len) 2)
                    (if (zerop (mod str-len 2)) 1 0) ))
        (pad-str (make-string pad ?\s)) )
    (insert pad-str str) ))
(defalias 'isc #'insert-string-centered)

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* RE: same data appears twice, interactive then function body
  2020-12-18 14:50             ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-12-18 16:35               ` Drew Adams
  2020-12-18 18:15                 ` Jean Louis
  2020-12-18 18:09               ` Jean Louis
  1 sibling, 1 reply; 15+ messages in thread
From: Drew Adams @ 2020-12-18 16:35 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs

> Technologist Adams was kind enough to write me a private mail

Apologies, but that was only by accident.  As mentioned
earlier, when I use Reply All to reply to your messages
to an Emacs mailing list, the To field has only your name,
and the Cc field is blank.  I have to remember that when
replying to you I need to manually add the mailing list
to the Cc or To field.

IOW, nothing special intended.  I'm no expert in email,
and I'm not about to try reconfiguring my email client.

> telling me my function `insert-string-centered' didn't work
> when used from Lisp. Well, that's what I think anyway, since
> I received it in my mailbox with no reference in the headers
> to either gmane.emacs.help or help-gnu-emacs@gnu.org, and it
> doesn't seem to be anywhere else to be found, either!
>
> Probably, he wanted to save me from being all embarrassed in
> from of everyone. Well, that was kind! But no worries!
> Actually, and to be frank about it, this isn't the first
> mistake I ever ever made as a programmer...

See above for the (mundane) explanation.



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

* Re: same data appears twice, interactive then function body
  2020-12-18 14:50             ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-12-18 16:35               ` Drew Adams
@ 2020-12-18 18:09               ` Jean Louis
  2020-12-20  4:45                 ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 15+ messages in thread
From: Jean Louis @ 2020-12-18 18:09 UTC (permalink / raw)
  To: help-gnu-emacs

* Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2020-12-18 17:51]:
> Technologist Adams was kind enough to write me a private mail
> telling me my function `insert-string-centered' didn't work
> when used from Lisp. Well, that's what I think anyway, since
> I received it in my mailbox with no reference in the headers
> to either gmane.emacs.help or help-gnu-emacs@gnu.org, and it
> doesn't seem to be anywhere else to be found, either!
> 
> Probably, he wanted to save me from being all embarrassed in
> from of everyone. Well, that was kind! But no worries!
> Actually, and to be frank about it, this isn't the first
> mistake I ever ever made as a programmer...
> 
> Anyway here is a new version, that God willing will work even
> under such severe circumstances...
> 
(defun insert-string-centered (str &optional width)
  (interactive
   (list (read-from-minibuffer "string: ")
         (string-to-number (read-from-minibuffer "width [window]: " )) ))
  (let*((span    (if (and width (< 0 width)) width (window-text-width)))
        (str-len (length str))
        (pad     (- (/ (- span str-len) 2)
                    (if (zerop (mod str-len 2)) 1 0) ))
        (pad-str (make-string pad ?\s)) )
    (insert pad-str str) ))
(defalias 'isc #'insert-string-centered)

Look:
(insert-string-centered "Hello")                                        Hello

Do you see how it will not center text as it will be influenced by the
cursor position? It should not be influenced by cursor position. 

Question is what means "centered". Does it mean centered to the width
of text or to the width of the window.

There is function (current-fill-column) that will tell what is the
width of the text. Maybe this is what user expects to be centered.

This one here is using `move-to-column' so because of that it is not
influenced by the cursor position.
                                         
(defun insert-centered-text (text &optional width)
  (interactive "MText: ")
  (let* ((width (or width (window-text-width)))
         (length (length (string-trim text)))
         (pos (/ (- width length) 2)))
    (move-to-column pos t)	     
    (insert text)
    (insert "\n")))

And this one here will consider (current-fill-column) to be the
authoritative width.

(defun insert-centered-text (text &optional width)
  (interactive "MText: ")
  (let* ((width (or width (current-fill-column)))
         (length (length (string-trim text)))
         (pos (/ (- width length) 2)))
    (move-to-column pos t)	     
    (insert text)
    (insert "\n")))



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

* Re: same data appears twice, interactive then function body
  2020-12-18 16:35               ` Drew Adams
@ 2020-12-18 18:15                 ` Jean Louis
  0 siblings, 0 replies; 15+ messages in thread
From: Jean Louis @ 2020-12-18 18:15 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs, Emanuel Berg

* Drew Adams <drew.adams@oracle.com> [2020-12-18 19:38]:
> > Technologist Adams was kind enough to write me a private mail
> 
> Apologies, but that was only by accident.  As mentioned
> earlier, when I use Reply All to reply to your messages
> to an Emacs mailing list, the To field has only your name,
> and the Cc field is blank.  I have to remember that when
> replying to you I need to manually add the mailing list
> to the Cc or To field.

While using mutt I do not get Emmanuel's email address but I get the
Emacs mailing list email address. I use g for group reply.

And I can press L for list reply option.

I find his setup alright if one wants to reply to the list, and not to
duplicate emails. I get bunch of duplicate emails that way.



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

* Re: same data appears twice, interactive then function body
  2020-12-18 18:09               ` Jean Louis
@ 2020-12-20  4:45                 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-20  4:45 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

> Do you see how it will not center text as it will be
> influenced by the cursor position? It should not be
> influenced by cursor position.

I don't know actually ... shouldn't it? This is more versatile
so I'll keep it. But so far, I've only had use of
it interactively.

> Question is what means "centered". Does it mean centered to
> the width of text or to the width of the window.

You set what it means with the width argument :) [1]

It is used for files like this:

  https://dataswamp.org/~incal/FILM

[1] https://dataswamp.org/~incal/emacs-init/edit.el line 11

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

end of thread, other threads:[~2020-12-20  4:45 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-12-16  1:23 same data appears twice, interactive then function body Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-16  2:44 ` Stefan Monnier
2020-12-16  4:02   ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-16  4:16     ` 2QdxY4RzWzUUiLuE
2020-12-16  4:31       ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-16  4:36         ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-16  4:16     ` Jean Louis
2020-12-16  4:32       ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-16  4:43         ` Yuri Khan
2020-12-16  5:19           ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-18 14:50             ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-18 16:35               ` Drew Adams
2020-12-18 18:15                 ` Jean Louis
2020-12-18 18:09               ` Jean Louis
2020-12-20  4:45                 ` Emanuel Berg via Users list for the GNU Emacs text editor

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.