all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Format with %-*
@ 2024-07-08 16:07 Heime
  2024-07-08 17:47 ` Bruno Barbier
  0 siblings, 1 reply; 6+ messages in thread
From: Heime @ 2024-07-08 16:07 UTC (permalink / raw)
  To: Heime via Users list for the GNU Emacs text editor

Why do I get "error: Invalid format operation %*" and how can I fix it ?



(defun insert-labels-in-columns (monbf col1-labels col2-labels col3-labels)
  "Insert three lists of labels into BUFFER-NAME in three columns."

  (with-current-buffer (get-buffer-create monbf)

    (erase-buffer)  ; Clear the buffer before inserting new labels

    (let ((max-rows (max (length col1-labels)
                         (length col2-labels)
                         (length col3-labels)))
          (col-width 20))  ; Width of each column

      (dotimes (i max-rows)
        (insert (format "%-*s" col-width (or (nth i col1-labels) "")))
        (insert (format "%-*s" col-width (or (nth i col2-labels) "")))
        (insert (or (nth i col3-labels) ""))
        (insert "\n"))))

    (pop-to-buffer monbf))



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

* Re: Format with %-*
  2024-07-08 16:07 Format with %-* Heime
@ 2024-07-08 17:47 ` Bruno Barbier
  2024-07-08 19:53   ` Heime
  0 siblings, 1 reply; 6+ messages in thread
From: Bruno Barbier @ 2024-07-08 17:47 UTC (permalink / raw)
  To: Heime, Heime via Users list for the GNU Emacs text editor


Heime <heimeborgia@protonmail.com> writes:

> Why do I get "error: Invalid format operation %*" and how can I fix it ?
>
>
>
> (defun insert-labels-in-columns (monbf col1-labels col2-labels col3-labels)
[...]

It seems that your problem could be reproduce with this one line:

  (format "%-*s" 10 "label")

It raises this error:

  (error "Invalid format operation %*")

and, indeed, I don't see the format control "%*" anywhere in the
'format' documentation, so, it looks like it's not a valid format
operation.

If you just copy/pasted a C 'printf' format, you probably should use
something like this instead:

   (format (format "%%-%ss" 10) "label")

HTH,

Bruno



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

* Re: Format with %-*
  2024-07-08 17:47 ` Bruno Barbier
@ 2024-07-08 19:53   ` Heime
  2024-07-09  8:07     ` Bruno Barbier
  2024-07-09  8:59     ` Michael Heerdegen via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 6+ messages in thread
From: Heime @ 2024-07-08 19:53 UTC (permalink / raw)
  To: Bruno Barbier; +Cc: Heime via Users list for the GNU Emacs text editor






Sent with Proton Mail secure email.

On Monday, July 8th, 2024 at 5:47 PM, Bruno Barbier <brubar.cs@gmail.com> wrote:

> Heime heimeborgia@protonmail.com writes:
> 
> > Why do I get "error: Invalid format operation %*" and how can I fix it ?
> > 
> > (defun insert-labels-in-columns (monbf col1-labels col2-labels col3-labels)
> 
> [...]
> 
> It seems that your problem could be reproduce with this one line:
> 
> (format "%-s" 10 "label")
> 
> It raises this error:
> 
> (error "Invalid format operation %")
> 
> and, indeed, I don't see the format control "%*" anywhere in the
> 'format' documentation, so, it looks like it's not a valid format
> operation.
> 
> If you just copy/pasted a C 'printf' format, you probably should use
> something like this instead:
> 
> (format (format "%%-%ss" 10) "label")

How can I construct a format that uses a space spx followed by a field of width 13 
into which I insert a label lbv2.

(insert (format (format "%%s%-%ds" 13) spx lbv2)))




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

* Re: Format with %-*
  2024-07-08 19:53   ` Heime
@ 2024-07-09  8:07     ` Bruno Barbier
  2024-07-09  8:59     ` Michael Heerdegen via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 6+ messages in thread
From: Bruno Barbier @ 2024-07-09  8:07 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

Heime <heimeborgia@protonmail.com> writes:

> How can I construct a format that uses a space spx followed by a field of width 13 
> into which I insert a label lbv2.
>
> (insert (format (format "%%s%-%ds" 13) spx lbv2)))

I wouldn't try to put everything in the format; i.e. I woud insert some
space, then insert the field.



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

* Re: Format with %-*
  2024-07-08 19:53   ` Heime
  2024-07-09  8:07     ` Bruno Barbier
@ 2024-07-09  8:59     ` Michael Heerdegen via Users list for the GNU Emacs text editor
  2024-07-09 13:39       ` Emanuel Berg
  1 sibling, 1 reply; 6+ messages in thread
From: Michael Heerdegen via Users list for the GNU Emacs text editor @ 2024-07-09  8:59 UTC (permalink / raw)
  To: help-gnu-emacs

Heime <heimeborgia@protonmail.com> writes:

> How can I construct a format that uses a space spx followed by a field
> of width 13
> into which I insert a label lbv2.

In Elisp we typically use `string-pad' and `truncate-string-to-width'.

It's also possible to add padding with `format' though - like in

(format "%10s" "test") -> "      test"

See `format' docstring.


Michael.




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

* Re: Format with %-*
  2024-07-09  8:59     ` Michael Heerdegen via Users list for the GNU Emacs text editor
@ 2024-07-09 13:39       ` Emanuel Berg
  0 siblings, 0 replies; 6+ messages in thread
From: Emanuel Berg @ 2024-07-09 13:39 UTC (permalink / raw)
  To: help-gnu-emacs

Michael Heerdegen via Users list for the GNU Emacs text editor wrote:

>> How can I construct a format that uses a space spx followed
>> by a field of width 13 into which I insert a label lbv2.
>
> In Elisp we typically use `string-pad' and
> `truncate-string-to-width'.
>
> It's also possible to add padding with `format' though -
> like in
>
> (format "%10s" "test") -> "      test"

I have made a lot of stuff like the example below.

But never felt 100% about it ...

(defun faster (old new)
  (format "%.1f%%" (* 100 (1- (/ new old 1.0)))) )

;; (faster 0.2  0.5)  ; 150.0%
;; (faster 1.35 0.84) ; -37.8%
;; (faster  200  400) ; 100.0%
;; (faster 1000 1000) ;   0.0%
;; (faster 1000  100) ; -90.0%

(defun percent (nom &optional denom str)
  (or denom (setq denom 1))
  (let ((perc (/ nom denom 0.01)))
    (if str
        (format "%.1f%%" perc)
      perc)))

;; (percent 0.25)           ; 25.0
;; (percent 50 75)          ; 66.66 ...
;; (percent (// 1 2) nil t) ; 50.0%
;; (percent 1019 22 t)      ; 4631.8%

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




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

end of thread, other threads:[~2024-07-09 13:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-08 16:07 Format with %-* Heime
2024-07-08 17:47 ` Bruno Barbier
2024-07-08 19:53   ` Heime
2024-07-09  8:07     ` Bruno Barbier
2024-07-09  8:59     ` Michael Heerdegen via Users list for the GNU Emacs text editor
2024-07-09 13:39       ` Emanuel Berg

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.