+(defun ada-fix-sort-context-pred (a b)
+ "Predicate for `sort-subr'; sorts \"limited with\", \"private with\" last.
+Returns non-nil if a should preceed b in buffer."
+ ;; a, b are buffer ranges in the current buffer
+ (cl-flet
+ ((starts-with
+ (pat reg)
+ (string= pat (buffer-substring-no-properties (car reg)
+ (min (point-max)
+ (+(car reg) (length pat)))))))
+ (cond
+ ((and
+ (starts-with "limited with" a)
+ (starts-with "private with" b))
+ t)
+
+ ((and
+ (starts-with "limited with" a)
+ (not (starts-with "limited with" b)))
+ nil)
+
+ ((and
+ (not (starts-with "limited with" a))
+ (starts-with "limited with" b))
+ t)
+
+ ((and
+ (starts-with "private with" a)
+ (not (starts-with "private with" b)))
+ nil)
+
+ ((and
+ (not (starts-with "private with" a))
+ (starts-with "private with" b))
+ t)
+
+ (t
+ (> 0 (compare-buffer-substrings
+ nil (car a) (cdr a)
+ nil (car b) (cdr b))) )
+ )))
+
+(defun ada-fix-sort-context-clause (beg end)
+ "Sort context clauses in range BEG END."
+ (save-excursion
+ (save-restriction
+ (narrow-to-region beg end)
+ (goto-char (point-min))
+ (sort-subr nil 'forward-line 'end-of-line nil nil 'ada-fix-sort-context-pred)
+ )))
+