@@ -295,143 +295,6 @@ is nil."
295295 (delete-file bufferfile)
296296 (delete-file outputfile))))
297297
298- (defun ocamlformat-args (name start-line end-line )
299- (let*
300- ((margin-args
301- (cond
302- ((equal ocamlformat-margin-mode 'window )
303- (list " --margin" (number-to-string (window-body-width ))))
304- ((equal ocamlformat-margin-mode 'fill )
305- (list " --margin" (number-to-string fill-column)))
306- (t
307- '())))
308- (enable-args
309- (cond
310- ((equal ocamlformat-enable 'disable )
311- (list " --disable" ))
312- ((equal ocamlformat-enable 'enable-outside-detected-project )
313- (list " --enable-outside-detected-project" ))
314- (t
315- '())))
316- (extension-args
317- (cond
318- ((eq ocamlformat-file-kind 'implementation )
319- (list " --impl" ))
320- ((eq ocamlformat-file-kind 'interface )
321- (list " --intf" )))))
322- (append margin-args enable-args extension-args
323- (list
324- " -"
325- " --name" name
326- " --numeric"
327- " --range" (format " %d -%d " start-line end-line)))))
328-
329- (defun ocamlformat-region (start end )
330- (interactive " r" )
331- (let*
332- ((ext (file-name-extension buffer-file-name t ))
333- (bufferfile (file-truename (make-temp-file " ocamlformat" nil ext)))
334- (errorfile (file-truename (make-temp-file " ocamlformat" nil ext)))
335- (errbuf
336- (cond
337- ((eq ocamlformat-show-errors 'buffer )
338- (get-buffer-create " *compilation*" ))
339- ((eq ocamlformat-show-errors 'echo )
340- (get-buffer-create " *OCamlFormat stderr*" ))))
341- (start-line (line-number-at-pos start))
342- (end-line (line-number-at-pos end))
343- (indents-str
344- (with-output-to-string
345- (if (/= 0
346- (apply 'call-process-region
347- (point-min ) (point-max ) ocamlformat-command nil
348- (list standard-output errorfile) nil
349- (ocamlformat-args buffer-file-name start-line end-line)))
350- (progn
351- (if errbuf
352- (progn
353- (with-current-buffer errbuf
354- (setq buffer-read-only nil )
355- (erase-buffer ))
356- (ocamlformat--process-errors
357- (buffer-file-name ) bufferfile errorfile errbuf)))
358- (message " Could not apply ocamlformat " )))))
359- (indents (mapcar 'string-to-number (split-string indents-str))))
360- (unwind-protect
361- (save-excursion
362- (goto-char start)
363- (mapcar
364- #' (lambda (indent ) (indent-line-to indent) (forward-line ))
365- indents))
366- (delete-file errorfile)
367- (delete-file bufferfile))))
368-
369- (defun ocamlformat-line ()
370- (interactive nil )
371- (ocamlformat-region (point ) (point )))
372-
373- (defun ocamlformat--enable-indent ()
374- " Whether the indentation feature is enabled."
375- (version<= " 0.19.0" (ocamlformat-version)))
376-
377- ;;;### autoload
378- (defun ocamlformat-setup-indent ()
379- (interactive nil )
380- (when (ocamlformat--enable-indent)
381- (setq-local indent-line-function #'ocamlformat-line )
382- (setq-local indent-region-function #'ocamlformat-region )))
383-
384- ;;;### autoload
385- (defun ocamlformat-caml-mode-setup ()
386- (ocamlformat-setup-indent)
387- (local-unset-key " \t " )) ; ; caml-mode rebinds TAB !
388-
389- (defun ocamlformat-newline-and-indent (&optional arg )
390- " Insert a newline, then indent according to `ocamlformat-line' .
391- With ARG, perform this action that many times."
392- (interactive " *p" )
393- (delete-horizontal-space t )
394- (unless arg
395- (setq arg 1 ))
396- (dotimes (_ arg)
397- (newline nil t )
398- (insert " x" )
399- (ocamlformat-line)
400- (delete-char -1 )))
401-
402- (defun ocamlformat-set-newline-and-indent ()
403- " Bind RET to `ocamlformat-newline-and-indent' ."
404- (when (ocamlformat--enable-indent)
405- (local-set-key (kbd " RET" ) 'ocamlformat-newline-and-indent )))
406-
407- (defun ocamlformat-version ()
408- " Get the version of the installed ocamlformat."
409- (car
410- (split-string
411- (shell-command-to-string
412- (format " %s --version" (shell-quote-argument ocamlformat-command)))
413- " -"
414- t
415- split-string-default-separators)))
416-
417- (defun ocamlformat--add-hooks ()
418- " Link ocamlformat with tuareg-mode and caml-mode."
419- (progn
420- (add-hook 'tuareg-mode-hook 'ocamlformat-setup-indent t )
421- (add-hook 'tuareg-mode-hook 'ocamlformat-set-newline-and-indent )
422- (add-hook 'caml-mode-hook 'ocamlformat-caml-mode-setup t )
423- (add-hook 'caml-mode-hook 'ocamlformat-set-newline-and-indent )))
424-
425- (pcase ocamlformat-enable
426- ; ; never hook
427- ('disable '())
428- ; ; always hook
429- ('enable-outside-detected-project (ocamlformat--add-hooks))
430- ; ; only hook if there is an .ocamlformat file at the root of the project
431- ('enable
432- (if (vc-find-root default-directory " .ocamlformat" )
433- (ocamlformat--add-hooks))))
434-
435298(provide 'ocamlformat )
436299
437300; ;; ocamlformat.el ends here
0 commit comments