about summary refs log tree commit diff
path: root/pkgs/profpatsch/xdg-open/purs/Config.purs
blob: eb0f94b0971780106da5a459b73db2c9b5337407 (plain) (blame)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
module Config where

import XdgOpen

type Special cmd =
  { openInEditor :: cmd
  , openInBrowser :: cmd
  , fetchHttpUrlMime :: cmd
  , composeMailTo :: cmd
  , execInTerminalEmulator :: cmd -> cmd
  , dmenuListBinariesAndExec :: cmd
  , notify :: String → cmd
  }

type DrvBinary = { package :: String, binary :: String }
type Pkgs = { pkg :: DrvBinary -> Executable, pkgOnDemand :: DrvBinary -> Executable }

wrapCommand :: Command -> Command -> Command
wrapCommand wrapper cmd =
  { exe: wrapper.exe
  , args: \(template :: Arg) ->
      wrapper.args
        template ++ [ ArgString cmd.exe ] ++ cmd.args template
  }

oneArg :: Executable -> Command
oneArg exe = { exe, args: \(file :: Arg) -> [ file ] }

mime :: Pkgs -> Special Command -> Config
mime pkgs special = do
  let
    pkgSame :: String -> Executable
    pkgSame name = pkgs.pkg { package: name, binary: name }

    pkgSameOnDemand :: String -> Executable
    pkgSameOnDemand name = pkgs.pkgOnDemand { package: name, binary: name }

  let
    m =
      { text:
          { html: { mime: [ "text", "html" ], cmd: special.openInBrowser }
          , gemini:
              { mime: [ "text", "gemini" ]
              , cmd: oneArg (pkgSame "lagrange")
              }
          , gopher:
              { mime: [ "text", "gopher" ]
              , cmd: oneArg (pkgSame "lagrange")
              }
          , xml:
              { mime: [ "text", "xml" ], cmd: special.openInBrowser }
          , csv:
              { mime: [ "text", "csv" ]
              , cmd: oneArg (pkgSameOnDemand "libreoffice")
              }
          , any:
              { mime: [ "text", "any" ], cmd: special.openInEditor }

          }
      , mailAddress:
          { mime: [ "special", "mailaddress" ]
          , cmd: special.composeMailTo
          }
      , torrent:
          { mime: [ "application", "x-bittorrent" ]
          , cmd: special.notify "No xdg-open handler for the torrent"
          }
      , irc:
          { mime: [ "x-scheme-handler", "irc" ]
          , cmd: special.notify "No xdg-open handler for the irc link"
          }
      , file:
          { mime: [ "x-scheme-handler", "file" ]
          , cmd:
              special.notify
                "No xdg-open handler for the x-scheme-handler/file"
          }
      , image:
          { gif:
              { mime: [ "image", "gif" ], cmd: special.openInBrowser }
          , svg:
              { mime: [ "image", "svg+xml" ]
              , cmd: oneArg (pkgSame "inkscape")
              }
          , any:
              { mime: [ "image", "*" ], cmd: oneArg (pkgSame "imv") }
          }
      , pdf:
          { mime: [ "application", "pdf" ]
          , cmd: oneArg (pkgSame "zathura")
          }
      , pgpKey:
          { mime: [ "application", "pgp-keys" ]
          , cmd:
              { exe: pkgs.pkg { package: "gnupg", binary: "gpg" }
              , args:
                  \(file :: Arg) ->
                    [ ArgString "--import"
                    , ArgString "--import-options"
                    , ArgString "show-only"
                    , file
                    ]
              }
          }
      , directory:
          { mime: [ "inode", "directory" ]
          , cmd:
              special.execInTerminalEmulator
                (oneArg (pkgSame "ranger"))
          }
      , opendocumentAny:
          { mime: [ "application/vnd.oasis.opendocument.*" ]
          , cmd: oneArg (pkgSameOnDemand "libreoffice")
          }
      , openxmlformatsAny:
          { mime: [ "application/vnd.openxmlformats-officedocument.*" ]
          , cmd: oneArg (pkgSameOnDemand "libreoffice")
          }
      , msword:
          { mime: [ "application/msword" ]
          , cmd: oneArg (pkgSameOnDemand "libreoffice")
          }
      , any:
          { mime: [ "*" ], cmd: special.dmenuListBinariesAndExec }
      }
  { orderedMimeMatchers:
      [ m.text.html
      , m.text.gemini
      , m.text.gopher
      , m.text.xml
      , m.text.csv
      , m.text.any
      , m.mailAddress
      , m.torrent
      , m.irc
      , m.file
      , m.image.gif
      , m.image.svg
      , m.image.any
      , m.pdf
      , m.opendocumentAny
      , m.openxmlformatsAny
      , m.pgpKey
      , m.directory
      , m.any
      ]
  , uriMimeGlobs:
      [ { desc: "http link"
        , glob: [ "http://*", "https://*" ]
        , schemaPrefix: [ "http", "https" ]
        , handler: m.text.html
        }
      , { desc: "gemini link"
        , glob: [ "gemini://*" ]
        , schemaPrefix: [ "gemini" ]
        , handler: m.text.gemini
        }
      , { desc: "gemini link"
        , glob: [ "gopher://*", "gophers://*" ]
        , schemaPrefix: [ "gopher", "gophers" ]
        , handler: m.text.gopher
        }
      , { glob: [ "mailto:*" ]
        , desc: "mail address"
        , schemaPrefix: [ "mailto" ]
        , handler: m.mailAddress
        }
      , { glob: [ "magnet:*" ]
        , desc: "bittorrent magnet link"
        , schemaPrefix: [ "magnet" ]
        , handler: m.torrent
        }
      , { desc: "irc channel"
        , glob: [ "irc:*", "ircs:*" ]
        , schemaPrefix: [ "irc", "ircs" ]
        , handler: m.irc
        }
      ] :: Array UriMimeGlob

  }