package b0

  1. Overview
  2. Docs
Software construction and deployment kit

Install

dune-project
 Dependency

Authors

Maintainers

Sources

b0-0.0.6.tbz
sha512=e9aa779e66c08fc763019f16d4706f465d16c05d6400b58fbd0313317ef33ddea51952e2b058db28e65f7ddb7012f328c8bf02d8f1da17bb543348541a2587f0

doc/src/b0.std/b0_editor.ml.html

Source file b0_editor.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(*---------------------------------------------------------------------------
   Copyright (c) 2018 The b0 programmers. All rights reserved.
   SPDX-License-Identifier: ISC
  ---------------------------------------------------------------------------*)

open B0_std
open Result.Syntax

(* Environment variables *)

module Env = struct
  let visual = "VISUAL"
  let editor = "EDITOR"
  let infos =
    let open Cmdliner in
    Cmd.Env.info visual
      ~doc:"The editor used to edit files. This is a command invocation given \
            to execvp(3) and is used before EDITOR." ::
    Cmd.Env.info editor
      ~doc:"The editor used to edit files. This is a command invocation given \
            to execvp(3) and is used after VISUAL." ::
    []

end

(* Editing *)

type t = Cmd.t

let find ?search ?cmd () = match cmd with
| Some cmd -> Os.Cmd.get ?search cmd
| None ->
    let parse_env cmds env = match cmds with
    | Error _ as e -> e
    | Ok cmds as r ->
        let empty_is_none = true in
        match Os.Env.var' ~empty_is_none Cmd.of_string env with
        | Error _ as e -> e
        | Ok None -> r
      | Ok (Some cmd) -> Ok (cmd :: cmds)
    in
    let cmds = Ok [Cmd.tool "nano"] in
    let cmds = parse_env cmds Env.editor in
    let* cmds = parse_env cmds Env.visual in
    match Os.Cmd.find_first ?search cmds with
    | None -> Error "No runnable editor found in VISUAL or EDITOR"
    | Some cmd -> Ok cmd

let edit_files editor fs = Os.Cmd.run_status Cmd.(editor %% paths fs)