let indent_buffer (v : Ed_sourceview.sourceview) args =
let current_line = v#current_line in
let b = v#file#buffer in
let code = v#file#of_utf8
(v#file#mode_from_display
(b#get_text ~start: b#start_iter ~stop: b#end_iter ()))
in
match Ed_ocaml_lexer.get_lines_indentation code with
`Failure (e,(start,stop),_) ->
let err = Printf.sprintf "chars %d-%d: %s"
start stop (Ed_ocaml_lexer.report_error e) in
Ed_misc.set_active_action_message (Ed_misc.to_utf8 err)
| `Success indentations ->
let lines = Ed_misc.split_string ~keep_empty: true code ['\n'] in
let nb_lines = List.length lines in
let nb_indentations = List.length indentations in
let indentations =
if nb_indentations < nb_lines then
indentations @
(Ed_misc.make_list (nb_lines - nb_indentations) None)
else
indentations
in
b#delete ~start: b#start_iter ~stop: b#end_iter;
v#place_cursor b#start_iter;
List.iter2
(fun line nopt ->
let line =
match nopt with
None -> line^"\n"
| Some n ->
let s = remove_first_blanks line in
Printf.sprintf "%s%s\n" (String.make n ' ') s
in
b#insert (v#file#mode_to_display (v#file#to_utf8 line))
)
lines
indentations;
v#place_cursor (b#get_iter (`LINE current_line));
let message =
Printf.sprintf "%d lines indented / %d lines in buffer"
nb_indentations nb_lines
in
Ed_misc.set_active_action_message (Ed_misc.to_utf8 message)