[Top][All Lists]

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

Re: Variable-width font indentation

From: Clément Pit-Claudel
Subject: Re: Variable-width font indentation
Date: Thu, 8 Mar 2018 09:05:43 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0

On 2018-03-08 08:21, Eli Zaretskii wrote:
>> From: Clément Pit-Claudel <address@hidden>
>> Date: Wed, 7 Mar 2018 17:13:09 -0500
>> This version is a bit faster, and it uses 'display (space …) instead of 
>> ghost strings:
>> ...
> It seems you forgot to post ~/measure-px-widths.

Indeed, apologies :/

(defun ~/measure-px-widths (beg end)
  (let ((widths nil))
    (while (< beg end)
      (push (car (window-text-pixel-size nil beg (1+ beg))) widths)
      (cl-incf beg))
    (nreverse widths)))

(defun ~/variable-pitch-indent ()
    (let ((spine [])
          (prev-bol nil)
          (prev-eol nil))
      (goto-char (point-min))
      (while (not (eobp))
        (let ((bol (point)))
          (unless (eolp)
            (let* ((indentation-amount (current-column))
                   (trunc-spine-to (min (length spine) indentation-amount)))
              (setq spine (seq-subseq spine 0 trunc-spine-to))
              ;; Extend spine based on previous line
              (when (and prev-bol (< (length spine) indentation-amount))
                (let ((beg (+ prev-bol (length spine)))
                      (end (min prev-eol (+ prev-bol indentation-amount))))
                  (setq spine (vconcat spine (~/measure-px-widths beg end)))))
              (setq prev-bol bol prev-eol (point-at-eol))))
          (dotimes (idx (min (length spine) (- (point) bol)))
            (let ((w (aref spine idx)))
              (put-text-property (+ bol idx) (+ bol idx 1)
                                 'display `(space :width (,w)))))
          (forward-line 1))))))

reply via email to

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