Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > de.comp.editoren > #567

Re: Ausrichtung

Path csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Stefan Reuther <stefan.news@arcor.de>
Newsgroups de.comp.editoren
Subject Re: Ausrichtung
Date Tue, 6 May 2025 20:38:21 +0200
Lines 82
Message-ID <vvdrvu.4pg.1@stefan.msgid.phost.de> (permalink)
References <Ausrichten-20250506143932@ram.dialup.fu-berlin.de>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Trace individual.net XCvA/YuaSJUoqr/o1z5SxAm1ryliWh1Fvg6FjM1Jm7r/5RiRs2
Cancel-Lock sha1:piNxl0Ti6H4S0OSfZmGJTpmE2d8= sha256:pWz++NdkKne7w46hQDoND9/6jeLFeAIQlOXmJo66Xu8=
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.12.1 Hamster/2.1.0.1538
In-Reply-To <Ausrichten-20250506143932@ram.dialup.fu-berlin.de>
Xref csiph.com de.comp.editoren:567

Show key headers only | View raw


Am 06.05.2025 um 15:39 schrieb Stefan Ram:
>   Seit Jahren wünsche ich mir, daß Editoren automatisch 
>   an Zeichen ausrichten könnten.
> 
> a = 2 # Anfangswert
> beta = 6 # Endwert
> gamma = 1 # Schrittweite

Ich hab mir vor Äonen da ein bisschen Emacs-Lisp gebaut, siehe unten.
Laut Dateidatum war das zwischen 1999 und 2005...

insert-aligned-comment für Kommentare, insert-aligned-interactive für
sonstiges (wie Gleichheitszeichen).

Ich bin mir ziemlich sicher, dass es da inzwischen auch weiter
automatisierte Funktionen gibt, gerade in der Umgebung automatischer
Code-Formatierer (z.B. clang-format AlignConsecutiveAssignments).


  Stefan


Etwas eingekürzt:

(if (not (fboundp 'string-width))
    (defun string-width (text) (length text)))

; Compute indentation. Returns (cons COLUMN NLINES) where NLINES is the
; number of lines above that must possibly be changed.
(defun ali-get-indentation (match)
  (let ((minc 0) (lines 0))
    (while (and (zerop (forward-line -1))
                (search-forward-regexp (concat "\n\\|" match))
                (not (bolp)))   ; when the above matches \n, (bolp) is true
      (goto-char (match-beginning 0))
      (setq minc (max minc (current-column)))
      (setq lines (1+ lines)))
    (cons minc lines)
    )
  )

; align the NLINES previous lines' MATCH to column COL
(defun ali-align-previous-lines (col nlines match)
  (while (> nlines 0)
    (forward-line -1)
    (search-forward-regexp (concat "\n\\|" match))
    (goto-char (match-beginning 0))
    (if (< (current-column) col)
	(insert (make-string (- col (current-column)) ? )))
    (setq nlines (1- nlines))
    )
  )

(defun insert-aligned (text &optional min match)
  "Insert TEXT into current buffer, aligning with lines above"
  (if (not match)
      (setq match (regexp-quote text)))
  (let ((ind-pair (save-excursion (ali-get-indentation match))))
    (let ((cur-col (current-column))
          (ind-col (car ind-pair))
          (ind-lines (cdr ind-pair)))
      (let ((col (max (or min 0) ind-col cur-col)))
	(save-excursion
	  (ali-align-previous-lines col ind-lines match))
	(insert (make-string (- col cur-col) ? ) text)
	)
      )
    )
  )

(defun insert-aligned-comment ()
  "Insert comment, aligning with previous comments"
  (interactive)
  (insert-aligned comment-start comment-column comment-start-skip)
  (save-excursion
    (insert comment-end))
  )

(defun insert-aligned-interactive (TEXT)
  "Like `insert-aligned', but prompt for text"
  (interactive "sInsert aligned: ")
  (insert-aligned TEXT))

Back to de.comp.editoren | Previous | Next | Find similar


Thread

Re: Ausrichtung Stefan Reuther <stefan.news@arcor.de> - 2025-05-06 20:38 +0200

csiph-web