comp.lang.ada
 help / color / mirror / Atom feed
* elisp to align colons in a record - improved version
@ 1998-05-29  0:00 Matthew Heaney
  0 siblings, 0 replies; only message in thread
From: Matthew Heaney @ 1998-05-29  0:00 UTC (permalink / 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
	





^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1998-05-29  0:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-05-29  0:00 elisp to align colons in a record - improved version Matthew Heaney

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