comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthew_heaney@acm.org>
Subject: elisp to align colons in a record - improved version
Date: 1998/05/29
Date: 1998-05-29T00:00:00+00:00	[thread overview]
Message-ID: <m33edundne.fsf@mheaney.ni.net> (raw)



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 "\\<end\\> *\\<record\\> *;" nil nil)

    (beginning-of-line)
    (setq end-of-region (point))

    ;; find "record"
    (re-search-backward "\\<record\\>" 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
	





                 reply	other threads:[~1998-05-29  0:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox