0a70a08635641a2d24bad7a09412bb5ae217c7db — Phil Hagelberg 12 days ago 04b1829 master
Allow creating new files that don't already exist.
3 files changed, 22 insertions(+), 16 deletions(-)

M main.lua
M polywell/commands.fnl
M polywell/old.lua
M main.lua => main.lua +1 -1
@@ 66,7 66,7 @@ love.load = function()
     if(lume.find(arg, "--fuzz")) then
        require("config")
-       require("fuzz")
+       fennel.dofile("fuzz.fnl")
     else
        love.graphics.setFont(love.graphics.newFont("FSEX300.ttf", 16))
        love.keyboard.setTextInput(true)

M polywell/commands.fnl => polywell/commands.fnl +8 -11
@@ 11,11 11,7 @@ (fn find-file []
    "Prompt for a filename and open it in a new buffer.
  If it's already open, switch to the existing buffer."
-   (let [callback (fn [input]
-                    ;; TODO: support creating new files
-                    (when (= :file (state.fs.type input))
-                      (editor.open input)))
-         keep-backspacing? (fn []
+   (let [keep-backspacing? (fn []
                              (let [input (editor.get-input)]
                                (and (< 0 (# input))
                                     (not= "/" (: input :sub -1 -1)))))


@@ 44,11 40,12 @@ (lume.split "/")
                  (doto (table.remove))
                  (table.concat "/"))]
-     (editor.read-line "Open: " callback {:completer completer
-                                          :bind {:map {"backspace" maybe-backspace-dir}}
-                                          :initial-input (if (= (# dir) 0)
-                                                             dir
-                                                             (.. dir "/"))})))
+     (editor.read-line "Open: " editor.open
+                       {:completer completer
+                        :bind {:map {"backspace" maybe-backspace-dir}}
+                        :initial-input (if (= (# dir) 0)
+                                           dir
+                                           (.. dir "/"))})))
  
  (fn close [confirm]
    "Close the current buffer, unless it has unsaved changes."


@@ 304,7 301,7 @@ (replace-input ""))))
  
  (fn default-indent [lines prev-line-number]
-   (# (: (. lines prev-line-number) :match "^ *")))
+   (# (: (or (. lines prev-line-number) "") :match "^ *")))
  
  (fn indent []
    "Indent the current line according to mode-specific rules.

M polywell/old.lua => polywell/old.lua +13 -4
@@ 589,18 589,27 @@ end
  end
  
- local open = function(path, mode, no_file, props)
+ local function open(path, mode, no_file, props)
     path = relativize_path(path)
     state["last-buffer"] = state.b
     state.b = get_buffer(path)
     if(state.b) then
        if(mode) then activate_mode(mode) end
     else
-       if(state.fs.type(path) ~= "file" and not no_file) then
-          echo("Tried to open a directory or something.")
-       elseif no_file then
+       if no_file then
           state.b = make_buffer(path, {""}, {["no-file"] = no_file})
           table.insert(state.buffers, state.b)
+       elseif(state.fs.type(path) == nil) then
+          local make_file = function(input)
+             if input:lower():match("^y") or input == "" then
+                frontend.write(path, "")
+                open(path, mode, no_file, props)
+             end
+          end
+          state.b = state["last-buffer"]
+          return read_line("File does not exist; create? [Y/n] ", make_file)
+       elseif(state.fs.type(path) ~= "file" and not no_file) then
+          echo("Tried to open a directory or something.")
        else
           local lines = lume.split(state.fs.read(path), "\n")
           state.b = make_buffer(path, lines, {})