Skip to content

Commit f7244d0

Browse files
JoeKarwilbertom
andauthored
Add reload setting - finalization of zyedidia#2627 (zyedidia#2845)
* Add reload setting Can be set to: * auto - Automatically reload files that changed * disabled - Do not reload files * prompt - Prompt the user about reloading the file. * option: Add default value for reload option and documentation --------- Co-authored-by: Wilberto Morales <wilbertomorales777@gmail.com>
1 parent 9da6af9 commit f7244d0

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

internal/action/bufpane.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -395,20 +395,34 @@ func (h *BufPane) Name() string {
395395
return n
396396
}
397397

398+
func (h *BufPane) getReloadSetting() string {
399+
reloadSetting := h.Buf.Settings["reload"]
400+
return reloadSetting.(string)
401+
}
402+
398403
// HandleEvent executes the tcell event properly
399404
func (h *BufPane) HandleEvent(event tcell.Event) {
400405
if h.Buf.ExternallyModified() && !h.Buf.ReloadDisabled {
401-
InfoBar.YNPrompt("The file on disk has changed. Reload file? (y,n,esc)", func(yes, canceled bool) {
402-
if canceled {
403-
h.Buf.DisableReload()
404-
}
405-
if !yes || canceled {
406-
h.Buf.UpdateModTime()
407-
} else {
408-
h.Buf.ReOpen()
409-
}
410-
})
406+
reload := h.getReloadSetting()
411407

408+
if reload == "prompt" {
409+
InfoBar.YNPrompt("The file on disk has changed. Reload file? (y,n,esc)", func(yes, canceled bool) {
410+
if canceled {
411+
h.Buf.DisableReload()
412+
}
413+
if !yes || canceled {
414+
h.Buf.UpdateModTime()
415+
} else {
416+
h.Buf.ReOpen()
417+
}
418+
})
419+
} else if reload == "auto" {
420+
h.Buf.ReOpen()
421+
} else if reload == "disabled" {
422+
h.Buf.DisableReload()
423+
} else {
424+
InfoBar.Message("Invalid reload setting")
425+
}
412426
}
413427

414428
switch e := event.(type) {

internal/config/settings.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ var optionValidators = map[string]optionValidator{
5252
"fileformat": validateLineEnding,
5353
"encoding": validateEncoding,
5454
"multiopen": validateMultiOpen,
55+
"reload": validateReload,
5556
}
5657

5758
func ReadSettings() error {
@@ -294,6 +295,7 @@ var defaultCommonSettings = map[string]interface{}{
294295
"mkparents": false,
295296
"permbackup": false,
296297
"readonly": false,
298+
"reload": "prompt",
297299
"rmtrailingws": false,
298300
"ruler": true,
299301
"relativeruler": false,
@@ -526,3 +528,19 @@ func validateMultiOpen(option string, value interface{}) error {
526528

527529
return nil
528530
}
531+
532+
func validateReload(option string, value interface{}) error {
533+
val, ok := value.(string)
534+
535+
if !ok {
536+
return errors.New("Expected string type for reload")
537+
}
538+
539+
switch val {
540+
case "prompt", "auto", "disabled":
541+
default:
542+
return errors.New(option + " must be 'prompt', 'auto' or 'disabled'")
543+
}
544+
545+
return nil
546+
}

runtime/help/options.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ Here are the available options:
281281

282282
default value: `false`
283283

284+
* `reload`: controls the reload behavior of the current buffer in case the file
285+
has changed. The available options are `prompt`, `auto` & `disabled`.
286+
287+
default value: `prompt`
288+
284289
* `rmtrailingws`: micro will automatically trim trailing whitespaces at ends of
285290
lines. Note: This setting overrides `keepautoindent`
286291

0 commit comments

Comments
 (0)