Groups | Search | Server Info | Login | Register


Groups > de.comp.editoren > #567

Re: Ausrichtung

From Stefan Reuther <stefan.news@arcor.de>
Newsgroups de.comp.editoren
Subject Re: Ausrichtung
Date 2025-05-06 20:38 +0200
Message-ID <vvdrvu.4pg.1@stefan.msgid.phost.de> (permalink)
References <Ausrichten-20250506143932@ram.dialup.fu-berlin.de>

Show all headers | 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