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

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

Re: Need to know how to goto-column


From: Michael Heerdegen
Subject: Re: Need to know how to goto-column
Date: Sat, 05 Dec 2020 22:40:13 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Jean Louis <bugs@gnu.support> writes:

> Show me the `my-print-table-to-string' if you have it.

Thrown together late at night and in a hurry, expect some nonsense
errors (and a useless docstring):

#+begin_src emacs-lisp
(defun my-print-table-to-string (table &optional col-formatters alignments)
  "Print formatted TABLE, a matrix of strings, to a string and return it."
  (let* ((nb-cols (length (car table)))
         (fill (lambda (l) (let ((d (- nb-cols (length l))))
                             (if (< 0 d) (append l (make-list d nil)) l))))
         (col-formatters (funcall fill col-formatters))
         (alignments     (funcall fill alignments))
         (table (mapcar (lambda (line)
                          (cl-mapcar
                           (lambda (entry formatter)
                             (if formatter (funcall formatter entry) entry))
                           line col-formatters))
                        table))
         (max-col-lengths
          (mapcar
           (lambda (i)
             (apply #'max
                    (mapcar #'length (mapcar (apply-partially #'nth i)
                                             table))))
           (number-sequence 0 (1- nb-cols)))))
    (mapconcat (lambda (line)
                 (let ((i 0))
                   (mapconcat
                    #'identity
                    (cl-mapcar
                     (lambda (entry alignment)
                       (prog1 (let ((padding (- (nth i max-col-lengths)
                                                (length entry))))
                                (pcase alignment
                                  ('right (concat (make-string padding ?\ ) 
entry))
                                  ((and (or 'center 'centered)
                                        (let left (/ padding 2)))
                                   (concat (make-string left ?\ )
                                           entry
                                           (make-string (- padding left) ?\ )))
                                  (_ (concat entry (make-string padding ?\ )))))
                         (cl-incf i)))
                     line alignments)
                    " ")))
               table "\n")))
#+end_src

Regards,

Michael.



reply via email to

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