Function: vhdl-align-inline-comment-region-1

vhdl-align-inline-comment-region-1 is a byte-compiled function defined in vhdl-mode.el.gz.

Signature

(vhdl-align-inline-comment-region-1 BEG END &optional SPACING)

Documentation

Align inline comments in region.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/vhdl-mode.el.gz
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Align inline comments

(defun vhdl-align-inline-comment-region-1 (beg end &optional spacing)
  "Align inline comments in region."
  (save-excursion
    (let ((start-max comment-column)
	  (length-max 0)
	  comment-list start-list tmp-list start length
	  cur-start prev-start no-code)
      (setq spacing (or spacing 2))
      (vhdl-prepare-search-2
       (goto-char beg)
       ;; search for comment start positions and lengths
       (while (< (point) end)
	 (when (and (not (looking-at "^\\s-*\\(begin\\|end\\)\\>"))
		    (looking-at "^\\(.*?[^ \t\n\r\f-]+\\)\\s-*\\(--.*\\)$")
		    (not (save-excursion (goto-char (match-beginning 2))
					 (vhdl-in-literal))))
	   (setq start (+ (- (match-end 1) (match-beginning 1)) spacing))
	   (setq length (- (match-end 2) (match-beginning 2)))
	   (setq start-max (max start start-max))
	   (setq length-max (max length length-max))
	   (push (cons start length) comment-list))
	 (beginning-of-line 2))
       (setq comment-list
             (sort comment-list (lambda (a b) (> (car a) (car b)))))
       ;; reduce start positions
       (setq start-list (list (caar comment-list)))
       (setq comment-list (cdr comment-list))
       (while comment-list
	 (unless (or (= (caar comment-list) (car start-list))
		     (<= (+ (car start-list) (cdar comment-list))
			    end-comment-column))
	   (push (caar comment-list) start-list))
	 (setq comment-list (cdr comment-list)))
       ;; align lines as nicely as possible
       (goto-char beg)
       (while (< (point) end)
	 (setq cur-start nil)
	 (when (and (not (looking-at "^\\s-*\\(begin\\|end\\)\\>"))
		    (or (and (looking-at "^\\(.*?[^ \t\n\r\f-]+\\)\\(\\s-*\\)\\(--.*\\)$")
			     (not (save-excursion
				    (goto-char (match-beginning 3))
				    (vhdl-in-literal))))
			(and (looking-at "^\\(\\)\\(\\s-*\\)\\(--.*\\)$")
			     (>= (- (match-end 2) (match-beginning 2))
				 comment-column))))
	   (setq start (+ (- (match-end 1) (match-beginning 1)) spacing))
	   (setq length (- (match-end 3) (match-beginning 3)))
	   (setq no-code (= (match-beginning 1) (match-end 1)))
	   ;; insert minimum whitespace
	   (goto-char (match-end 2))
	   (delete-region (match-beginning 2) (match-end 2))
	   (insert-char ?\  spacing)
	   (setq tmp-list start-list)
	   ;; insert additional whitespace to align
	   (setq cur-start
		 (cond
		  ;; align comment-only line to inline comment of previous line
		  ((and no-code prev-start
			(<= length (- end-comment-column prev-start)))
		   prev-start)
		  ;; align all comments at `start-max' if this is possible
		  ((<= (+ start-max length-max) end-comment-column)
		   start-max)
		  ;; align at `comment-column' if possible
		  ((and (<= start comment-column)
			(<= length (- end-comment-column comment-column)))
		   comment-column)
		  ;; align at left-most possible start position otherwise
		  (t
		   (while (and tmp-list (< (car tmp-list) start))
		     (setq tmp-list (cdr tmp-list)))
		   (car tmp-list))))
	   (indent-to cur-start))
	 (setq prev-start cur-start)
	 (beginning-of-line 2))))))