Here's an implementation of exactly what the OP is asking for, although I don't think it is of any use to anybody (except possibly the OP, but even there I have my doubts, as the extended discussion in the comments shows). It assumes that there is one long line starting at the beginning of the buffer and that you want to reformat it as a block of some specified width that is centered in the window of the buffer.
(defun insertN (c count)
(insert (make-string count c)))
(defun center-block (text-width)
"Assumption: there is one long line at the beginning of the buffer which this function
is going to mangle in order to center it in the window.
Assumption: the given text-width is smaller than the window body width."
(interactive "NText width: ")
(let* ((s (buffer-substring-no-properties (point-min) (point-max)))
(slen (length s))
(W (window-body-width))
(H (window-body-height))
(L text-width)
(nlines (/ (+ slen L) L)) ; number of physical lines of block
; when shrunk to the required line length
(toplines (/ (+ (- H nlines) 2) 2)) ; how many lines to leave at the top
(lspaces (/ (- W L) 2)) ; how many spaces to insert at the beginning
(rspaces (- (- W L) lspaces)) ; how many spaces to go to the right end of the window
(nspaces (+ lspaces rspaces)) ; how many spaces to insert between physical lines
sub)
(erase-buffer)
;; insert the proper number of newlines
(insertN ?\n toplines)
;; insert the proper number of spaces at the beginning of the string
(insertN ?\s lspaces)
;; break up the string into substrings of length L and insert each
;; one, followed by the appropriate number of spaces. The loop inserts
;; the *full* logical lines of the block: there might be a tail that is
;; left over if the length of the string is not a multiple of L which is
;; inserted after the loop is done.
(while (>= (length s) L)
(setq sub (substring s 0 L)) ; first L chars
(setq s (substring s L)) ; rest
;; insert the substring
(insert sub)
;; insert nspaces spaces: rspaces to reach the right end of the window
;; and lspaces to indent the next physical line.
(insertN ?\s nspaces))
;; insert what's left over.
(insert s)))
I have commented it copiously in an attempt to make it understandable to the OP.
Invoke it with M-x center-block RET 60 RET
(or whatever width you want to specify instead of 60) while the cursor is in the window of the buffer that you are trying to modify.
Why should there be any space on the left or right?
mostly for focusing on a text block, in a temp buffer (i didn't plan on doing this on a real file anyway, just temp buffer) @NickD and yes, i already slightly know that to know the center of the screen, i need to calculate the height of the display (1366x768, so the latter) by 2, then need to calculate which width to reformat the text in...(this is where i'm kind of stuck) but at least i know how to do this manually, just need to do it in elisp too :) – Nordine Lotfi May 16 '20 at 19:43site
link is a .html page, which is a different thing. – Ian May 17 '20 at 11:41