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

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

Re: same data appears twice, interactive then function body


From: Jean Louis
Subject: Re: same data appears twice, interactive then function body
Date: Fri, 18 Dec 2020 21:09:43 +0300
User-agent: Mutt/2.0 (3d08634) (2020-11-07)

* 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")))



reply via email to

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