aboutsummaryrefslogtreecommitdiff
path: root/data/core/commands/doc.lua
blob: af5d58e222475def95bcb29e41785188c1c33576 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
local core = require "core"
local command = require "core.command"
local common = require "core.common"
local config = require "core.config"
local encodings = require "core.doc.encodings"
local translate = require "core.doc.translate"
local style = require "core.style"
local DocView = require "core.docview"
local tokenizer = require "core.tokenizer"


local function doc()
  return core.active_view.doc
end


local function doc_multiline_selections(sort)
  local iter, state, idx, line1, col1, line2, col2 = doc():get_selections(sort)
  return function()
    idx, line1, col1, line2, col2 = iter(state, idx)
    if idx and line2 > line1 and col2 == 1 then
      line2 = line2 - 1
      col2 = #doc().lines[line2]
    end
    return idx, line1, col1, line2, col2
  end
end

local function append_line_if_last_line(line)
  if line >= #doc().lines then
    doc():insert(line, math.huge, "\n")
  end
end

local function save(filename)
  local abs_filename
  if filename then
    filename = core.normalize_to_project_dir(filename)
    abs_filename = core.project_absolute_path(filename)
  end
  local ok, err = pcall(doc().save, doc(), filename, abs_filename)
  if ok then
    local saved_filename = doc().filename
    core.log("Saved \"%s\"", saved_filename)
  else
    core.error(err)
    core.nag_view:show("Saving failed", string.format("Couldn't save file \"%s\". Do you want to save to another location?", doc().filename), {
      { text = "Yes", default_yes = true },
      { text = "No", default_no = true }
    }, function(item)
      if item.text == "Yes" then
        core.add_thread(function()
          -- we need to run this in a thread because of the odd way the nagview is.
          command.perform("doc:save-as")
        end)
      end
    end)
  end
end

local function save_existing(doc)
  if doc.filename then doc:save() end
end

local function cut_or_copy(delete)
  local full_text = ""
  local text = ""
  core.cursor_clipboard = {}
  core.cursor_clipboard_whole_line = {}
  for idx, line1, col1, line2, col2 in doc():get_selections(true, true) do
    if line1 ~= line2 or col1 ~= col2 then
      text = doc():get_text(line1, col1, line2, col2)
      full_text = full_text == "" and text or (text .. " " .. full_text)
      core.cursor_clipboard_whole_line[idx] = false
      if delete then
        doc():delete_to_cursor(idx, 0)
      end
    else -- Cut/copy whole line
      -- Remove newline from the text. It will be added as needed on paste.
      text = string.sub(doc().lines[line1], 1, -2)
      full_text = full_text == "" and text .. "\n" or (text .. "\n" .. full_text)
      core.cursor_clipboard_whole_line[idx] = true
      if delete then
        if line1 < #doc().lines then
          doc():remove(line1, 1, line1 + 1, 1)
        elseif #doc().lines == 1 then
          doc():remove(line1, 1, line1, math.huge)
        else
          doc():remove(line1 - 1, math.huge, line1, math.huge)
        end
        doc():set_selections(idx, line1, col1, line2, col2)
      end
    end
    core.cursor_clipboard[idx] = text
  end
  if delete then doc():merge_cursors() end
  core.cursor_clipboard["full"] = full_text
  system.set_clipboard(full_text)
end

local function set_primary_selection(doc)
  -- Doesn't work on Windows, so avoid spending time getting the text
  if PLATFORM ~= "Windows" then
    system.set_primary_selection(doc:get_selection_text())
  end
end

local function split_cursor(dv, direction)
  local new_cursors = {}
  local dv_translate = direction < 0
    and DocView.translate.previous_line
    or DocView.translate.next_line
  for _, line1, col1 in dv.doc:get_selections() do
    if line1 + direction >= 1 and line1 + direction <= #dv.doc.lines then
      table.insert(new_cursors, { dv_translate(dv.doc, line1, col1, dv) })
    end
  end
  -- add selections in the order that will leave the "last" added one as doc.last_selection
  local start, stop = 1, #new_cursors
  if direction < 0 then
    start, stop = #new_cursors, 1
  end
  for i = start, stop, direction do
    local v = new_cursors[i]
    dv.doc:add_selection(v[1], v[2])
  end
  core.blink_reset()
end

local function set_cursor(dv, x, y, snap_type)
  local line, col = dv:resolve_screen_position(x, y)
  dv.doc:set_selection(line, col, line, col)
  if snap_type == "word" or snap_type == "lines" then
    command.perform("doc:select-" .. snap_type)
  end
  dv.mouse_selecting = { line, col, snap_type }
  core.blink_reset()
end

local function set_encoding(doc, charset)
  doc.encoding = charset
  if doc.bom then
    doc.bom = encoding.get_charset_bom(charset)
  end
end

local function line_comment(comment, line1, col1, line2, col2)
  local start_comment = (type(comment) == 'table' and comment[1] or comment) .. " "
  local end_comment = (type(comment) == 'table' and " " .. comment[2])
  local uncomment = true
  local start_offset = math.huge
  for line = line1, line2 do
    local text = doc().lines[line]
    local s = text:find("%S")
    if s then
      local cs, ce = text:find(start_comment, s, true)
      if cs ~= s then
        uncomment = false
      end
      start_offset = math.min(start_offset, s)
    end
  end

  local end_line = col2 == #doc().lines[line2]
  for line = line1, line2 do
    local text = doc().lines[line]
    local s = text:find("%S")
    if s and uncomment then
      if end_comment and text:sub(#text - #end_comment, #text - 1) == end_comment then
        doc():remove(line, #text - #end_comment, line, #text)
      end
      local cs, ce = text:find(start_comment, s, true)
      if ce then
        doc():remove(line, cs, line, ce + 1)
      end
    elseif s then
      doc():insert(line, start_offset, start_comment)
      if end_comment then
        doc():insert(line, #doc().lines[line], " " .. comment[2])
      end
    end
  end
  col1 = col1 + (col1 > start_offset and #start_comment or 0) * (uncomment and -1 or 1)
  col2 = col2 + (col2 > start_offset and #start_comment or 0) * (uncomment and -1 or 1)
  if end_comment and end_line then
    col2 = col2 + #end_comment * (uncomment and -1 or 1)
  end
  return line1, col1, line2, col2
end

local function block_comment(comment, line1, col1, line2, col2)
  -- automatically skip spaces
  local word_start = doc():get_text(line1, col1, line1, math.huge):find("%S")
  local word_end = doc():get_text(line2, 1, line2, col2):find("%s*$")
  col1 = col1 + (word_start and (word_start - 1) or 0)
  col2 = word_end and word_end or col2

  local block_start = doc():get_text(line1, col1, line1, col1 + #comment[1])
  local block_end = doc():get_text(line2, col2 - #comment[2], line2, col2)

  if block_start == comment[1] and block_end == comment[2] then
    -- remove up to 1 whitespace after the comment
    local start_len, stop_len = #comment[1], #comment[2]
    if doc():get_text(line1, col1 + #comment[1], line1, col1 + #comment[1] + 1):find("%s$") then
      start_len = start_len + 1
    end
    if doc():get_text(line2, col2 - #comment[2] - 1, line2, col2):find("^%s") then
      stop_len = stop_len + 1
    end

    doc():remove(line1, col1, line1, col1 + start_len)
    col2 = col2 - (line1 == line2 and start_len or 0)
    doc():remove(line2, col2 - stop_len, line2, col2)

    return line1, col1, line2, col2 - stop_len
  else
    doc():insert(line1, col1, comment[1] .. " ")
    col2 = col2 + (line1 == line2 and (#comment[1] + 1) or 0)
    doc():insert(line2, col2, " " .. comment[2])

    return line1, col1, line2, col2 + #comment[2] + 1
  end
end

local function insert_paste(doc, value, whole_line, idx)
  if whole_line then
    local line1, col1 = doc:get_selection_idx(idx)
    doc:insert(line1, 1, value:gsub("\r", "").."\n")
    -- Because we're inserting at the start of the line,
    -- if the cursor is in the middle of the line
    -- it gets carried to the next line along with the old text.
    -- If it's at the start of the line it doesn't get carried,
    -- so we move it of as many characters as we're adding.
    if col1 == 1 then
      doc:move_to_cursor(idx, #value+1)
    end
  else
    doc:text_input(value:gsub("\r", ""), idx)
  end
end

local commands = {
  ["doc:select-none"] = function(dv)
    local l1, c1 = dv.doc:get_selection_idx(dv.doc.last_selection)
    if not l1 then
      l1, c1 = dv.doc:get_selection_idx(1)
    end
    dv.doc:set_selection(l1, c1)
    dv.doc:clear_search_selections()
  end,

  ["doc:cut"] = function()
    cut_or_copy(true)
  end,

  ["doc:copy"] = function()
    cut_or_copy(false)
  end,

  ["doc:undo"] = function(dv)
    dv.doc:undo()
  end,

  ["doc:redo"] = function(dv)
    dv.doc:redo()
  end,

  ["doc:paste"] = function(dv)
    local clipboard = system.get_clipboard()
    -- If the clipboard has changed since our last look, use that instead
    if core.cursor_clipboard["full"] ~= clipboard then
      core.cursor_clipboard = {}
      core.cursor_clipboard_whole_line = {}
      for idx in dv.doc:get_selections() do
        insert_paste(dv.doc, clipboard, false, idx)
      end
      return
    end
    -- Use internal clipboard(s)
    -- If there are mixed whole lines and normal lines, consider them all as normal
    local only_whole_lines = true
    for _,whole_line in pairs(core.cursor_clipboard_whole_line) do
      if not whole_line then
        only_whole_lines = false
        break
      end
    end
    if #core.cursor_clipboard_whole_line == (#dv.doc.selections/4) then
    -- If we have the same number of clipboards and selections,
    -- paste each clipboard into its corresponding selection
      for idx in dv.doc:get_selections() do
        insert_paste(dv.doc, core.cursor_clipboard[idx], only_whole_lines, idx)
      end
    else
      -- Paste every clipboard and add a selection at the end of each one
      local new_selections = {}
      for idx in dv.doc:get_selections() do
        for cb_idx in ipairs(core.cursor_clipboard_whole_line) do
          insert_paste(dv.doc, core.cursor_clipboard[cb_idx], only_whole_lines, idx)
          if not only_whole_lines then
            table.insert(new_selections, {dv.doc:get_selection_idx(idx)})
          end
        end
        if only_whole_lines then
          table.insert(new_selections, {dv.doc:get_selection_idx(idx)})
        end
      end
      local first = true
      for _,selection in pairs(new_selections) do
        if first then
          dv.doc:set_selection(table.unpack(selection))
          first = false
        else
          dv.doc:add_selection(table.unpack(selection))
        end
      end
    end
  end,

  ["doc:paste-primary-selection"] = function(dv, x, y)
    if type(x) == "number" and type(y) == "number" then
      set_cursor(dv, x, y, "set")
      -- Workaround to avoid that a middle mouse drag starts selecting
      dv.mouse_selecting = nil
    end
    dv.doc:text_input(system.get_primary_selection() or "")
  end,

  ["doc:newline"] = function(dv)
    for idx, line, col in dv.doc:get_selections(false, true) do
      local indent = dv.doc.lines[line]:match("^[\t ]*")
      if col <= #indent then
        indent = indent:sub(#indent + 2 - col)
      end
      -- Remove current line if it contains only whitespace
      if not config.keep_newline_whitespace and dv.doc.lines[line]:match("^%s+$") then
        dv.doc:remove(line, 1, line, math.huge)
      end
      dv.doc:text_input("\n" .. indent, idx)
    end
  end,

  ["doc:newline-below"] = function(dv)
    for idx, line in dv.doc:get_selections(false, true) do
      local indent = dv.doc.lines[line]:match("^[\t ]*")
      dv.doc:insert(line, math.huge, "\n" .. indent)
      dv.doc:set_selections(idx, line + 1, math.huge)
    end
  end,

  ["doc:newline-above"] = function(dv)
    for idx, line in dv.doc:get_selections(false, true) do
      local indent = dv.doc.lines[line]:match("^[\t ]*")
      dv.doc:insert(line, 1, indent .. "\n")
      dv.doc:set_selections(idx, line, math.huge)
    end
  end,

  ["doc:delete"] = function(dv)
    for idx, line1, col1, line2, col2 in dv.doc:get_selections(true, true) do
      if line1 == line2 and col1 == col2 and dv.doc.lines[line1]:find("^%s*$", col1) then
        dv.doc:remove(line1, col1, line1, math.huge)
      end
      dv.doc:delete_to_cursor(idx, translate.next_char)
    end
  end,

  ["doc:backspace"] = function(dv)
    local _, indent_size = dv.doc:get_indent_info()
    for idx, line1, col1, line2, col2 in dv.doc:get_selections(true, true) do
      if line1 == line2 and col1 == col2 then
        local text = dv.doc:get_text(line1, 1, line1, col1)
        if #text >= indent_size and text:find("^ *$") then
          dv.doc:delete_to_cursor(idx, 0, -indent_size)
          goto continue
        end
      end
      dv.doc:delete_to_cursor(idx, translate.previous_char)
      ::continue::
    end
  end,

  ["doc:select-all"] = function(dv)
    dv.doc:set_selection(1, 1, math.huge, math.huge)
    set_primary_selection(dv.doc)
    -- avoid triggering DocView:scroll_to_make_visible
    dv.last_line1 = 1
    dv.last_col1 = 1
    dv.last_line2 = #dv.doc.lines
    dv.last_col2 = #dv.doc.lines[#dv.doc.lines]
  end,

  ["doc:select-lines"] = function(dv)
    for idx, line1, _, line2 in dv.doc:get_selections(true) do
      append_line_if_last_line(line2)
      dv.doc:set_selections(idx, line2 + 1, 1, line1, 1)
    end
    set_primary_selection(dv.doc)
  end,

  ["doc:select-word"] = function(dv)
    for idx, line1, col1 in dv.doc:get_selections(true) do
      local line1, col1 = translate.start_of_word(dv.doc, line1, col1)
      local line2, col2 = translate.end_of_word(dv.doc, line1, col1)
      dv.doc:set_selections(idx, line2, col2, line1, col1)
    end
    set_primary_selection(dv.doc)
  end,

  ["doc:join-lines"] = function(dv)
    for idx, line1, col1, line2, col2 in dv.doc:get_selections(true) do
      if line1 == line2 then line2 = line2 + 1 end
      local text = dv.doc:get_text(line1, 1, line2, math.huge)
      text = text:gsub("(.-)\n[\t ]*", function(x)
        return x:find("^%s*$") and x or x .. " "
      end)
      dv.doc:insert(line1, 1, text)
      dv.doc:remove(line1, #text + 1, line2, math.huge)
      if line1 ~= line2 or col1 ~= col2 then
        dv.doc:set_selections(idx, line1, math.huge)
      end
    end
  end,

  ["doc:indent"] = function(dv)
    for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
      local l1, c1, l2, c2 = dv.doc:indent_text(false, line1, col1, line2, col2)
      if l1 then
        dv.doc:set_selections(idx, l1, c1, l2, c2)
      end
    end
  end,

  ["doc:unindent"] = function(dv)
    for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
      local l1, c1, l2, c2 = dv.doc:indent_text(true, line1, col1, line2, col2)
      if l1 then
        dv.doc:set_selections(idx, l1, c1, l2, c2)
      end
    end
  end,

  ["doc:duplicate-lines"] = function(dv)
    for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
      append_line_if_last_line(line2)
      local text = doc():get_text(line1, 1, line2 + 1, 1)
      dv.doc:insert(line2 + 1, 1, text)
      local n = line2 - line1 + 1
      dv.doc:set_selections(idx, line1 + n, col1, line2 + n, col2)
    end
  end,

  ["doc:delete-lines"] = function(dv)
    for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
      append_line_if_last_line(line2)
      dv.doc:remove(line1, 1, line2 + 1, 1)
      dv.doc:set_selections(idx, line1, col1)
    end
  end,

  ["doc:move-lines-up"] = function(dv)
    for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
      append_line_if_last_line(line2)
      if line1 > 1 then
        local text = doc().lines[line1 - 1]
        dv.doc:insert(line2 + 1, 1, text)
        dv.doc:remove(line1 - 1, 1, line1, 1)
        dv.doc:set_selections(idx, line1 - 1, col1, line2 - 1, col2)
      end
    end
  end,

  ["doc:move-lines-down"] = function(dv)
    for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
      append_line_if_last_line(line2 + 1)
      if line2 < #dv.doc.lines then
        local text = dv.doc.lines[line2 + 1]
        dv.doc:remove(line2 + 1, 1, line2 + 2, 1)
        dv.doc:insert(line1, 1, text)
        dv.doc:set_selections(idx, line1 + 1, col1, line2 + 1, col2)
      end
    end
  end,

  ["doc:toggle-block-comments"] = function(dv)
    for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
      local current_syntax = dv.doc.syntax
      if line1 > 1 then
        -- Use the previous line state, as it will be the state
        -- of the beginning of the current line
        local state = dv.doc.highlighter:get_line(line1 - 1).state
        if state then
          local syntaxes = tokenizer.extract_subsyntaxes(dv.doc.syntax, state)
          -- Go through all the syntaxes until the first with `block_comment` defined
          for _, s in pairs(syntaxes) do
            if s.block_comment then
              current_syntax = s
              break
            end
          end
        end
      end
      local comment = current_syntax.block_comment
      if not comment then
        if dv.doc.syntax.comment then
          command.perform "doc:toggle-line-comments"
        end
        return
      end
      -- if nothing is selected, toggle the whole line
      if line1 == line2 and col1 == col2 then
        col1 = 1
        col2 = #dv.doc.lines[line2]
      end
      dv.doc:set_selections(idx, block_comment(comment, line1, col1, line2, col2))
    end
  end,

  ["doc:toggle-line-comments"] = function(dv)
    for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
      local current_syntax = dv.doc.syntax
      if line1 > 1 then
        -- Use the previous line state, as it will be the state
        -- of the beginning of the current line
        local state = dv.doc.highlighter:get_line(line1 - 1).state
        if state then
          local syntaxes = tokenizer.extract_subsyntaxes(dv.doc.syntax, state)
          -- Go through all the syntaxes until the first with comments defined
          for _, s in pairs(syntaxes) do
            if s.comment or s.block_comment then
              current_syntax = s
              break
            end
          end
        end
      end
      local comment = current_syntax.comment or current_syntax.block_comment
      if comment then
        dv.doc:set_selections(idx, line_comment(comment, line1, col1, line2, col2))
      end
    end
  end,

  ["doc:upper-case"] = function(dv)
    dv.doc:replace(string.uupper)
  end,

  ["doc:lower-case"] = function(dv)
    dv.doc:replace(string.ulower)
  end,

  ["doc:go-to-line"] = function(dv)
    local items
    local function init_items()
      if items then return end
      items = {}
      local mt = { __tostring = function(x) return x.text end }
      for i, line in ipairs(dv.doc.lines) do
        local item = { text = line:sub(1, -2), line = i, info = "line: " .. i }
        table.insert(items, setmetatable(item, mt))
      end
    end

    core.command_view:enter("Go To Line", {
      submit = function(text, item)
        local line = item and item.line or tonumber(text)
        if not line then
          core.error("Invalid line number or unmatched string")
          return
        end
        dv.doc:set_selection(line, 1  )
        dv:scroll_to_line(line, true)
      end,
      suggest = function(text)
        if not text:find("^%d*$") then
          init_items()
          return common.fuzzy_match(items, text)
        end
      end,
      draw_text = function(item, font, color, x, y, w, h)
        y = common.round(y + (h - font:get_height()) / 2)
        local tx = x
        local last_token = nil
        local tokens = dv.doc.highlighter:get_line(item.line).tokens
        local tokens_count = #tokens
        if tokens_count > 0 and string.sub(tokens[tokens_count], -1) == "\n" then
          last_token = tokens_count - 1
        end
        for tidx, type, text in dv.doc.highlighter:each_token(item.line) do
          color = style.syntax[type] or style.syntax["normal"]
          -- do not render newline, fixes issue #1164
          if tidx == last_token then text = text:sub(1, -2) end
          tx = renderer.draw_text(font, text, tx, y, color)
          if tx > (x + w) - font:get_width(item.info) - style.padding.x * 2 then break end
        end
      end
    })
  end,

  ["doc:toggle-line-ending"] = function(dv)
    dv.doc.crlf = not dv.doc.crlf
  end,

  ["doc:change-encoding"] = function(dv)
    encodings.select_encoding("Select Output Encoding", function(charset)
      set_encoding(dv.doc, charset)
      save_existing(dv.doc)
    end)
  end,

  ["doc:reload-with-encoding"] = function(dv)
    encodings.select_encoding("Reload With Encoding", function(charset)
      set_encoding(dv.doc, charset)
      dv.doc:reload()
    end)
  end,

  ["doc:toggle-overwrite"] = function(dv)
    dv.doc.overwrite = not dv.doc.overwrite
    core.blink_reset() -- to show the cursor has changed edit modes
  end,

  ["doc:save-as"] = function(dv)
    local last_doc = core.last_active_view and core.last_active_view.doc
    local text
    if dv.doc.filename then
      text = dv.doc.filename
    elseif last_doc and last_doc.filename then
      local dirname, filename = core.last_active_view.doc.abs_filename:match("(.*)[/\\](.+)$")
      text = core.normalize_to_project_dir(dirname) .. PATHSEP
      if text == core.root_project().path then text = "" end
    end
    core.command_view:enter("Save As", {
      text = text,
      submit = function(filename)
        save(common.home_expand(filename))
      end,
      suggest = function (text)
        return common.home_encode_list(common.path_suggest(common.home_expand(text)))
      end
    })
  end,

  ["doc:save"] = function(dv)
    if dv.doc.filename then
      save()
    else
      command.perform("doc:save-as")
    end
  end,

  ["doc:reload"] = function(dv)
    dv.doc:reload()
  end,

  ["file:rename"] = function(dv)
    local old_filename = dv.doc.filename
    if not old_filename then
      core.error("Cannot rename unsaved doc")
      return
    end
    core.command_view:enter("Rename", {
      text = old_filename,
      submit = function(filename)
        save(common.home_expand(filename))
        core.log("Renamed \"%s\" to \"%s\"", old_filename, filename)
        if filename ~= old_filename then
          os.remove(old_filename)
        end
      end,
      suggest = function (text)
        return common.home_encode_list(common.path_suggest(common.home_expand(text)))
      end
    })
  end,

  ["file:delete"] = function(dv)
    local filename = dv.doc.abs_filename
    if not filename then
      core.error("Cannot remove unsaved doc")
      return
    end
    for i,docview in ipairs(core.get_views_referencing_doc(dv.doc)) do
      local node = core.root_view.root_node:get_node_for_view(docview)
      node:close_view(core.root_view.root_node, docview)
    end
    os.remove(filename)
    core.log("Removed \"%s\"", filename)
  end,

  ["doc:select-to-cursor"] = function(dv, x, y, clicks)
    local line1, col1 = select(3, doc():get_selection())
    local line2, col2 = dv:resolve_screen_position(x, y)
    dv.mouse_selecting = { line1, col1, nil }
    dv.doc:set_selection(line2, col2, line1, col1)
    set_primary_selection(dv.doc)
  end,

  ["doc:create-cursor-previous-line"] = function(dv)
    split_cursor(dv, -1)
    dv.doc:merge_cursors()
  end,

  ["doc:create-cursor-next-line"] = function(dv)
    split_cursor(dv, 1)
    dv.doc:merge_cursors()
  end
}

command.add(function(x, y)
  if x == nil or y == nil or not core.active_view:extends(DocView) then return false end
  local dv = core.active_view
  local x1,y1,x2,y2 = dv.position.x, dv.position.y, dv.position.x + dv.size.x, dv.position.y + dv.size.y
  return x >= x1 + dv:get_gutter_width() and x < x2 and y >= y1 and y < y2, dv, x, y
end, {
  ["doc:set-cursor"] = function(dv, x, y)
    set_cursor(dv, x, y, "set")
  end,

  ["doc:set-cursor-word"] = function(dv, x, y)
    set_cursor(dv, x, y, "word")
  end,

  ["doc:set-cursor-line"] = function(dv, x, y, clicks)
    set_cursor(dv, x, y, "lines")
  end,

  ["doc:split-cursor"] = function(dv, x, y, clicks)
    local line, col = dv:resolve_screen_position(x, y)
    local removal_target = nil
    for idx, line1, col1 in dv.doc:get_selections(true) do
      if line1 == line and col1 == col and #doc().selections > 4 then
        removal_target = idx
      end
    end
    if removal_target then
      dv.doc:remove_selection(removal_target)
    else
      dv.doc:add_selection(line, col, line, col)
    end
    dv.mouse_selecting = { line, col, "set" }
  end
})

command.add(function()
  if not core.active_view:extends(DocView) then return false end
  local doc = core.active_view.doc
  local bom = encoding.get_charset_bom(doc.encoding or "none")
  return  bom ~= nil, doc, bom
end, {
  ["doc:disable-bom"] = function(doc)
    doc.bom = nil
    save_existing(doc)
  end,

  ["doc:enable-bom"] = function(doc, bom)
    doc.bom = bom
    save_existing(doc)
  end,

  ["doc:toggle-bom"] = function(doc, bom)
    if doc.bom then doc.bom = nil else doc.bom = bom end
    save_existing(doc)
  end,
})

local translations = {
  ["previous-char"] = translate,
  ["next-char"] = translate,
  ["previous-word-start"] = translate,
  ["next-word-end"] = translate,
  ["previous-block-start"] = translate,
  ["next-block-end"] = translate,
  ["start-of-doc"] = translate,
  ["end-of-doc"] = translate,
  ["start-of-line"] = translate,
  ["end-of-line"] = translate,
  ["start-of-word"] = translate,
  ["start-of-indentation"] = translate,
  ["end-of-word"] = translate,
  ["previous-line"] = DocView.translate,
  ["next-line"] = DocView.translate,
  ["previous-page"] = DocView.translate,
  ["next-page"] = DocView.translate,
}

for name, obj in pairs(translations) do
  commands["doc:move-to-" .. name] = function(dv)
    dv.doc:move_to(obj[name:gsub("-", "_")], dv)
  end
  commands["doc:select-to-" .. name] = function(dv)
    dv.doc:select_to(obj[name:gsub("-", "_")], dv)
    set_primary_selection(dv.doc)
  end
  commands["doc:delete-to-" .. name] = function(dv)
    dv.doc:delete_to(obj[name:gsub("-", "_")], dv)
  end
end

commands["doc:move-to-previous-char"] = function(dv)
  for idx, line1, col1, line2, col2 in dv.doc:get_selections(true) do
    if line1 ~= line2 or col1 ~= col2 then
      dv.doc:set_selections(idx, line1, col1)
    else
      dv.doc:move_to_cursor(idx, translate.previous_char)
    end
  end
  dv.doc:merge_cursors()
end

commands["doc:move-to-next-char"] = function(dv)
  for idx, line1, col1, line2, col2 in dv.doc:get_selections(true) do
    if line1 ~= line2 or col1 ~= col2 then
      dv.doc:set_selections(idx, line2, col2)
    else
      dv.doc:move_to_cursor(idx, translate.next_char)
    end
  end
  dv.doc:merge_cursors()
end

command.add("core.docview", commands)