comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: Emacs Ada-Mode
Date: 1998/05/26
Date: 1998-05-26T00:00:00+00:00	[thread overview]
Message-ID: <m3g1hw92ea.fsf@mheaney.ni.net> (raw)
In-Reply-To: 356680b9.18964466@news.geccs.gecm.com


brianorpin@bigfoot.com (Brian Orpin) writes:

> What I would like is an extension to the mode to align the colons in
> record declarations.
> 
> Anyone done this already?

I wrote this today.  It aligns the colons in record declarations
that aren't too complex.

You'll have to have written at least "record", "end record", and one
declaration (id/type pair) to use this; otherwise, you'll get mismatch
errors.  It moves point to the last declaration, so you can use it
as you're entering the record components.

This isn't very fancy.  Let me know if it doesn't meet your needs.

Matt

;;; STX
(defun mjh-format-record ()
  (interactive)
  (let (start-of-region
	end-of-region
	decl
	decl-list
	decl-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))

;;; (message (buffer-substring start-of-region end-of-region))

    ;; calculate number of declarations in record
    (setq decl-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 declarations into identifier/type pairs
    (while (> decl-count 0)

      ;; terminate search for id/type pair at end of line (this isn't a
      ;; very sophisiticated parse
      (end-of-line)

      ;; parse a declaration of the form "<id> : <whatever>;"
      (let ((eol (point)))
	(beginning-of-line)
	(re-search-forward
	 "^\\([ \t]*[A-Za-z][A-Za-z0-9_]*\\)[ \t]*:[ \t]*\\(.*;\\)" eol nil))

      ;; create a cons cell to store identifier string (car) and
      ;; whatever follows the colon (cdr)
      (setq decl (cons (match-string 1) (match-string 2)))

;;;   (message (concat "'" (car decl) "'  '" (cdr decl) "'"))

      ;; prepend the id/type pair to the declaration list
      (setq decl-list (cons decl decl-list))

      ;; calculate length of largest identifier
      (let ((decl-len (length (car decl))))
	(if (> decl-len max-len)
	    (setq max-len decl-len)))

      ;; advance to the next declaration
      (forward-line)
      (setq decl-count (1- decl-count))) ; end of while loop

    ;; reverse declaration list, so that the order of items matches
    ;; the order of declarations
    (setq decl-list (nreverse decl-list))

    ;; we've recorded all the declarations (decl-list), now delete
    ;; text that was there
    (delete-region start-of-region (point)) ;end-of-region)

    ;; insert declarations, adding padding to end of each identifier
    (while decl-list
      ;; pop identifier/type pair off of declaration list
      (setq decl (car decl-list))

      ;; insert identifier as originally written
      (insert (car decl))

      ;; insert padding characters
      (insert-char ? (- max-len (length (car decl))))

      (insert " : ")

      ;; insert type (technically, whatever followed colon)
      (insert (cdr decl))

      (insert "\n")

      (setq decl-list (cdr decl-list))) ; end of while loop

    ;; move point the end of the last declaration
    (forward-line -1)
    (end-of-line))) ; end of mjh-format-record

(add-hook 'ada-mode-hook
	  '(lambda ()
	     (define-key ada-mode-map "\C-c\C-r" 'mjh-format-record)))





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

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <356680b9.18964466@news.geccs.gecm.com>
1998-05-19  0:00 ` Emacs Ada-Mode David C. Hoos, Sr.
1998-05-19  0:00   ` David  Weller
1998-05-19  0:00   ` John McCabe
1998-05-19  0:00 ` Philippe Waroquiers
1998-05-20  0:00   ` Scott Evans
     [not found]   ` <356283c4.2281076@news.geccs.gecm.com>
1998-05-20  0:00     ` John McCabe
1998-05-21  0:00 ` Simon Wright
     [not found]   ` <35693157.3762034@news.geccs.gecm.com>
1998-05-22  0:00     ` Mattias Sj�sv�rd
1998-05-26  0:00   ` Stephen.leake
1998-05-26  0:00 ` Matthew Heaney [this message]
2006-08-18 12:07 Emacs ada-mode Stephen Leake
replies disabled

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