Function: htmlize-untabify-string

htmlize-untabify-string is a byte-compiled function defined in htmlize.el.

Signature

(htmlize-untabify-string TEXT START-COLUMN)

Documentation

Untabify TEXT, assuming it starts at START-COLUMN.

Source Code

;; Defined in ~/.emacs.d/elpa/htmlize-20250724.1703/htmlize.el
(defun htmlize-untabify-string (text start-column)
  "Untabify TEXT, assuming it starts at START-COLUMN."
  (let ((column start-column)
        (last-match 0)
        (chunk-start 0)
        chunks match-pos tab-size)
    (while (string-match "[\t\n]" text last-match)
      (setq match-pos (match-beginning 0))
      (cond ((eq (aref text match-pos) ?\t)
             ;; Encountered a tab: create a chunk of text followed by
             ;; the expanded tab.
             (push (substring text chunk-start match-pos) chunks)
             ;; Increase COLUMN by the length of the text we've
             ;; skipped since last tab or newline.  (Encountering
             ;; newline resets it.)
             (cl-incf column (- match-pos last-match))
             ;; Calculate tab size based on tab-width and COLUMN.
             (setq tab-size (- tab-width (% column tab-width)))
             ;; Expand the tab, carefully recreating the `display'
             ;; property if one was on the TAB.
             (let ((display (get-text-property match-pos 'display text))
                   (expanded-tab (aref htmlize-tab-spaces tab-size)))
               (when display
                 (setq expanded-tab (copy-sequence expanded-tab))
                 (put-text-property 0 tab-size 'display display expanded-tab))
               (push expanded-tab chunks))
             (cl-incf column tab-size)
             (setq chunk-start (1+ match-pos)))
            (t
             ;; Reset COLUMN at beginning of line.
             (setq column 0)))
      (setq last-match (1+ match-pos)))
    ;; If no chunks have been allocated, it means there have been no
    ;; tabs to expand.  Return TEXT unmodified.
    (if (null chunks)
        text
      (when (< chunk-start (length text))
        ;; Push the remaining chunk.
        (push (substring text chunk-start) chunks))
      ;; Generate the output from the available chunks.
      (htmlize-concat (nreverse chunks)))))