From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1282b0736782a855,start X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: elisp to align colons in a record - improved version Date: 1998/05/29 Message-ID: #1/1 X-Deja-AN: 357554782 Organization: Network Intensive Newsgroups: comp.lang.ada Date: 1998-05-29T00:00:00+00:00 List-Id: This new and improved version handles comment lines, blank lines, default values, etc among record components. Hope this is helpful, Matt (add-hook 'ada-mode-hook '(lambda () (define-key ada-mode-map "\C-c\C-r" 'mjh-format-record))) (defun mjh-format-record () (interactive) (let (start-of-region end-of-region comp comp-list comp-count (max-len 0)) ;; find "end record;" (beginning-of-line) (re-search-forward "\\ *\\ *;" nil nil) (beginning-of-line) (setq end-of-region (point)) ;; find "record" (re-search-backward "\\" nil nil) (forward-line) (setq start-of-region (point)) ;; calculate number of lines between "record" and "end record" (setq comp-count (count-lines start-of-region end-of-region)) ;; get rid of tabs, because it messes up calculation of length (untabify start-of-region end-of-region) ;;; ;;; parse record components into identifier/type pairs ;;; (while (> comp-count 0) (cond ((looking-at "^\\( *[A-Za-z0-9_]+\\) *: *\\([^\n]*\n\\)") ;; this is an actual record component (setq comp (cons (match-string 1) (match-string 2))) ;; calculate length of largest identifier (let ((comp-len (length (car comp)))) (if (> comp-len max-len) (setq max-len comp-len)))) ((looking-at " *\\(--\\)?[^\n]*\n") ;; this is a line without an identifier/colon (setq comp (cons nil (match-string 0)))) (t ;; there's some kind of syntax error (error "(unable to parse line)"))) ;; end cond ;; squirral away info about contents of line (setq comp-list (cons comp comp-list)) ;; set up for next iteration (forward-line) (setq comp-count (1- comp-count))) ;; end while ;; put list in same order as actual lines of program text (setq comp-list (nreverse comp-list)) ;; delete the text that was there (delete-region start-of-region (point)) ;;; ;;; insert text, adding padding to end of each identifier ;;; (while comp-list (setq comp (car comp-list)) ;; insert identifier (if (car comp) (progn ;; insert identifier as originally written (insert (car comp)) ;; insert padding characters (insert-char ? (- max-len (length (car comp)))) (insert " : "))) ;; end if ;; insert whatever else is on the line (insert (cdr comp)) (setq comp-list (cdr comp-list))) ;; end while ;; move to end of "end record" (end-of-line))) ;;; end defun mjh-format-record