]> git.eshelyaron.com Git - emacs.git/commitdiff
More SES printer functions.
authorVincent Belaïche <vincent.belaiche@sourceforge.net>
Fri, 23 May 2025 07:28:56 +0000 (09:28 +0200)
committerEshel Yaron <me@eshelyaron.com>
Tue, 27 May 2025 14:31:06 +0000 (16:31 +0200)
* lisp/ses.el (ses-standard-printer-functions): add 'ses-left' &
'ses-left-span' to the list.
(ses--align): New helper function, takes most of the code
previously in 'ses-center'
(ses-center): Use 'ses--align'.
(ses--span):  New helper function, takes most of the code
previously in 'ses-center-span.
(ses-center-span): Use 'ses--span'.'
(ses-left): New function.
(ses-left-span): New function.

(cherry picked from commit ce08defd0a9a02a69edb781671047545baae14d7)

lisp/ses.el

index aa3eb1bad403e3526b328743efcd824f05c5af9b..90ea4ad4b258535e19063f4372ba3e47a17a5d83 100644 (file)
@@ -299,7 +299,10 @@ Used for listing local printers or renamed cells.")
 
 (defconst ses-standard-printer-functions
   '(ses-center
-    ses-center-span ses-dashfill ses-dashfill-span
+    ses-center-span
+    ses-left
+    ses-left-span
+    ses-dashfill ses-dashfill-span
     ses-tildefill-span
     ses-prin1)
   "List of print functions to be included in initial history of printer functions.
@@ -4080,43 +4083,83 @@ either (ses-range BEG END) or (list ...).  The TEST is evaluated."
   (put x 'side-effect-free t))
 
 
-;;----------------------------------------------------------------------------
-;; Standard print functions
-;;----------------------------------------------------------------------------
-
-(defun ses-center (value &optional span fill printer)
-  "Print VALUE, centered within column.
-FILL is the fill character for centering (default = space).
-SPAN indicates how many additional rightward columns to include
-in width (default = 0).
-PRINTER is the printer to use for printing the value, default is the
-column printer if any, or the spreadsheet the spreadsheet default
-printer otherwise."
+(defun ses--align (value align-fn span fill printer)
+  "Helper fonction for \\{ses-center} and \\{ses-left}. Please refer to these functions help.
+ALIGN-FN shall be a function to concatenate the padding, it shall have
+parameters (VALUE WIDTH FILL) with:
+VALUE a string already formatted by PRINTER to which padding is to be
+concatenated.
+WIDTH the additional width to be padded if >0, <= 0 if no padding is to
+be added.
+FILL the fill character to be padded."
   (setq printer (or printer  (ses-col-printer ses--col) ses--default-printer))
   (let ((width   (ses-col-width ses--col))
        half)
-    (or fill (setq fill ?\s))
     (or span (setq span 0))
     (setq value (ses-call-printer printer value))
     (dotimes (x span)
       (setq width (+ width 1 (ses-col-width (+ ses--col span (- x))))))
     ;; Set column width.
     (setq width (- width (string-width value)))
-    (if (<= width 0)
-       value ; Too large for field, anyway.
-      (setq half (make-string (/ width 2) fill))
-      (concat half value half
-             (if (oddp width) (char-to-string fill))))))
+    (funcall align-fn value width fill)))
 
-(defun ses-center-span (value &optional fill printer)
-  "Print VALUE, centered within the span that starts in the current column
-and continues until the next nonblank column.
-FILL specifies the fill character (default = space)."
+;;----------------------------------------------------------------------------
+;; Standard print functions
+;;----------------------------------------------------------------------------
+
+(defun ses-center (value &optional span fill printer)
+  "Print VALUE, centered within column.
+FILL is the fill character for centering (default = space).
+SPAN indicates how many additional rightward columns to include in
+width (default = 0).
+PRINTER is the printer to use for printing the value, default is the
+column printer if any, or the spreadsheet default printer otherwise."
+  (ses--align value
+              (lambda (value width fill)
+                (if (<= width 0)
+                   value ; Too large for field, anyway.
+                  (let ((half (make-string (/ width 2) fill)))
+                    (concat half value half
+                           (if (oddp width) (char-to-string fill))))))
+              span (or fill ?\s) printer))
+
+(defun ses--span (align-fn value fill printer)
+  "Helper function for \\{ses-center-span} and \\{ses-left-span}. Please refer to these functions help.
+ALIGN-FN shall be a function such as \\{ses-center} or \\{ses-left}."
   (let ((end (1+ ses--col)))
     (while (and (< end ses--numcols)
                (memq (ses-cell-value ses--row end) '(nil *skip*)))
       (setq end (1+ end)))
-    (ses-center value (- end ses--col 1) fill printer)))
+    (funcall align-fn value (- end ses--col 1) fill printer)))
+
+
+(defun ses-center-span (value &optional fill printer)
+  "Print VALUE, centered within the span that starts in the current column
+and continues until the next nonblank column.
+FILL specifies the fill character (default = space)."
+  (ses--span #'ses-center value fill printer))
+
+(defun ses-left (value &optional span fill printer)
+  "Print VALUE, left aligned within column.
+FILL is the fill character for aligning (default = '-').
+SPAN indicates how many additional rightward columns to include
+in width (default = 0).
+PRINTER is the printer to use for printing the value, default is the
+column printer if any, or the spreadsheet the spreadsheet default
+printer otherwise."
+  (ses--align value
+              (lambda (value width fill)
+                (if (<= width 0)
+                   value ; Too large for field, anyway.
+                  (concat value (make-string width fill))))
+              span (or fill ?-) printer))
+
+(defun ses-left-span (value &optional fill printer)
+  "Print VALUE, aligned left within the span that starts in the current column
+and continues until the next nonblank column.
+FILL specifies the fill character (default = '-')."
+  (ses--span #'ses-left value fill printer))
+
 
 (defun ses-dashfill (value &optional span printer)
   "Print VALUE centered using dashes.