emacs-devel
[Top][All Lists]
Advanced

[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: Mon, 5 Mar 2018 20:40:14 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0

On 2018-03-05 20:30, Paul Eggert wrote:
> On 03/05/2018 05:06 PM, Clément Pit-Claudel wrote:
>> In variable-pitch mode, each new indentation level has a
>> slightly-different width, instead of a regular progression of 4
>> spaces (in Python) and 2 spaces (in Elisp) at each level.
> 
> That's fine. Although it's only a small thing, to me it's even a
> small plus, as having every indent level be the same number of pixels
> unnaturally focuses the reader on a distracting regularity that is
> not intrinsic to the code.

I've been playing with this a bit more; until now, I'd mostly stopped at my 
previous objection.

The following code gives a preview of what the algorithm that we've been 
discussing produces:

(defun ~/variable-pitch-indent ()
  (interactive)
  (save-excursion
    (let ((spine "")
          (face `(:foreground ,(face-attribute 'default :background nil))))
      (goto-char (point-min))
      (while (not (eobp))
        (back-to-indentation)
        (unless (eolp)
          (let* ((indentation-amount (current-column))
                 (spine-cut (min (length spine) indentation-amount))
                 (spine-prefix (substring spine 0 spine-cut))
                 (spine-padding (make-string (- indentation-amount spine-cut) 
?\s))
                 (spine-end (buffer-substring (point) (point-at-eol))))
            (setq spine
                  (with-temp-buffer
                    ;; HACK: expand spaces
                    (setq-local tab-width 8)
                    (insert spine-prefix spine-padding spine-end)
                    (untabify (point-min) (point-max))
                    (buffer-string)))
            (put-text-property (point-at-bol) (point) 'display
                               (propertize (concat spine-prefix spine-padding)
                                           'face face))))
        (forward-line 1)))))

I've tested it on two files: src/termcap.c and lisp/dabbrev.el.  To try it out, 
open one of these files in emacs -Q, then run M-x ~/variable-pitch-indent, and 
then M-x variable-pitch-mode.

I do agree that it doesn't look too bad, and presumably a C implementation of 
the algorithm above would be very fast, since it could build the "spine" above 
during redisplay.

Clément.



reply via email to

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