Skip to content

Commit 07f8cfb

Browse files
authored
Merge pull request zyedidia#3502 from JoeKar/feature/help-split
action/command: Allow `-vsplit` & `-hsplit` as optional argument for `help` Additionally the help, vsplit and hsplit command can now open multiple files like the tab command.
2 parents 1023c8d + 39b2b26 commit 07f8cfb

File tree

5 files changed

+99
-33
lines changed

5 files changed

+99
-33
lines changed

internal/action/actions.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,8 @@ func (h *BufPane) ToggleHelp() bool {
17231723
if h.Buf.Type == buffer.BTHelp {
17241724
h.Quit()
17251725
} else {
1726-
h.openHelp("help")
1726+
hsplit := config.GlobalSettings["helpsplit"] == "hsplit"
1727+
h.openHelp("help", hsplit, false)
17271728
}
17281729
return true
17291730
}

internal/action/command.go

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ func (h *BufPane) ReopenCmd(args []string) {
428428
}
429429
}
430430

431-
func (h *BufPane) openHelp(page string) error {
431+
func (h *BufPane) openHelp(page string, hsplit bool, forceSplit bool) error {
432432
if data, err := config.FindRuntimeFile(config.RTHelp, page).Data(); err != nil {
433433
return errors.New(fmt.Sprintf("Unable to load help text for %s: %v", page, err))
434434
} else {
@@ -437,33 +437,74 @@ func (h *BufPane) openHelp(page string) error {
437437
helpBuffer.SetOptionNative("hltaberrors", false)
438438
helpBuffer.SetOptionNative("hltrailingws", false)
439439

440-
if h.Buf.Type == buffer.BTHelp {
440+
if h.Buf.Type == buffer.BTHelp && !forceSplit {
441441
h.OpenBuffer(helpBuffer)
442-
} else {
442+
} else if hsplit {
443443
h.HSplitBuf(helpBuffer)
444+
} else {
445+
h.VSplitBuf(helpBuffer)
444446
}
445447
}
446448
return nil
447449
}
448450

449-
// HelpCmd tries to open the given help page in a horizontal split
451+
// HelpCmd tries to open the given help page according to the split type
452+
// configured with the "helpsplit" option. It can be overriden by the optional
453+
// arguments "-vpslit" or "-hsplit". In case more than one help page is given
454+
// as argument then it opens all of them with the defined split type.
450455
func (h *BufPane) HelpCmd(args []string) {
456+
hsplit := config.GlobalSettings["helpsplit"] == "hsplit"
451457
if len(args) < 1 {
452458
// Open the default help if the user just typed "> help"
453-
h.openHelp("help")
459+
h.openHelp("help", hsplit, false)
454460
} else {
455-
if config.FindRuntimeFile(config.RTHelp, args[0]) != nil {
456-
err := h.openHelp(args[0])
457-
if err != nil {
458-
InfoBar.Error(err)
461+
var topics []string
462+
forceSplit := false
463+
const errSplit = "hsplit and vsplit are not allowed at the same time"
464+
for _, arg := range args {
465+
switch arg {
466+
case "-vsplit":
467+
if forceSplit {
468+
InfoBar.Error(errSplit)
469+
return
470+
}
471+
hsplit = false
472+
forceSplit = true
473+
case "-hsplit":
474+
if forceSplit {
475+
InfoBar.Error(errSplit)
476+
return
477+
}
478+
hsplit = true
479+
forceSplit = true
480+
default:
481+
topics = append(topics, arg)
482+
}
483+
}
484+
485+
if len(topics) < 1 {
486+
// Do the same as without arg
487+
h.openHelp("help", hsplit, forceSplit)
488+
return
489+
}
490+
if len(topics) > 1 {
491+
forceSplit = true
492+
}
493+
494+
for _, topic := range topics {
495+
if config.FindRuntimeFile(config.RTHelp, topic) != nil {
496+
err := h.openHelp(topic, hsplit, forceSplit)
497+
if err != nil {
498+
InfoBar.Error(err)
499+
}
500+
} else {
501+
InfoBar.Error("Sorry, no help for ", topic)
459502
}
460-
} else {
461-
InfoBar.Error("Sorry, no help for ", args[0])
462503
}
463504
}
464505
}
465506

466-
// VSplitCmd opens a vertical split with file given in the first argument
507+
// VSplitCmd opens one or more vertical splits with the files given as arguments
467508
// If no file is given, it opens an empty buffer in a new split
468509
func (h *BufPane) VSplitCmd(args []string) {
469510
if len(args) == 0 {
@@ -472,16 +513,18 @@ func (h *BufPane) VSplitCmd(args []string) {
472513
return
473514
}
474515

475-
buf, err := buffer.NewBufferFromFile(args[0], buffer.BTDefault)
476-
if err != nil {
477-
InfoBar.Error(err)
478-
return
479-
}
516+
for _, a := range args {
517+
buf, err := buffer.NewBufferFromFile(a, buffer.BTDefault)
518+
if err != nil {
519+
InfoBar.Error(err)
520+
return
521+
}
480522

481-
h.VSplitBuf(buf)
523+
h.VSplitBuf(buf)
524+
}
482525
}
483526

484-
// HSplitCmd opens a horizontal split with file given in the first argument
527+
// HSplitCmd opens one or more horizontal splits with the files given as arguments
485528
// If no file is given, it opens an empty buffer in a new split
486529
func (h *BufPane) HSplitCmd(args []string) {
487530
if len(args) == 0 {
@@ -490,21 +533,24 @@ func (h *BufPane) HSplitCmd(args []string) {
490533
return
491534
}
492535

493-
buf, err := buffer.NewBufferFromFile(args[0], buffer.BTDefault)
494-
if err != nil {
495-
InfoBar.Error(err)
496-
return
497-
}
536+
for _, a := range args {
537+
buf, err := buffer.NewBufferFromFile(a, buffer.BTDefault)
538+
if err != nil {
539+
InfoBar.Error(err)
540+
return
541+
}
498542

499-
h.HSplitBuf(buf)
543+
h.HSplitBuf(buf)
544+
}
500545
}
501546

502547
// EvalCmd evaluates a lua expression
503548
func (h *BufPane) EvalCmd(args []string) {
504549
InfoBar.Error("Eval unsupported")
505550
}
506551

507-
// NewTabCmd opens the given file in a new tab
552+
// NewTabCmd opens one or more tabs with the files given as arguments
553+
// If no file is given, it opens an empty buffer in a new tab
508554
func (h *BufPane) NewTabCmd(args []string) {
509555
width, height := screen.Screen.Size()
510556
iOffset := config.GetInfoBarOffset()

internal/config/settings.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var optionValidators = map[string]optionValidator{
2929
"detectlimit": validateNonNegativeValue,
3030
"encoding": validateEncoding,
3131
"fileformat": validateChoice,
32+
"helpsplit": validateChoice,
3233
"matchbracestyle": validateChoice,
3334
"multiopen": validateChoice,
3435
"reload": validateChoice,
@@ -41,6 +42,7 @@ var optionValidators = map[string]optionValidator{
4142
var OptionChoices = map[string][]string{
4243
"clipboard": {"internal", "external", "terminal"},
4344
"fileformat": {"unix", "dos"},
45+
"helpsplit": {"hsplit", "vsplit"},
4446
"matchbracestyle": {"underline", "highlight"},
4547
"multiopen": {"tab", "hsplit", "vsplit"},
4648
"reload": {"prompt", "auto", "disabled"},
@@ -109,6 +111,7 @@ var DefaultGlobalOnlySettings = map[string]interface{}{
109111
"divchars": "|-",
110112
"divreverse": true,
111113
"fakecursor": false,
114+
"helpsplit": "hsplit",
112115
"infobar": true,
113116
"keymenu": false,
114117
"mouse": true,

runtime/help/commands.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ quotes here but these are not necessary when entering the command in micro.
2121
This command will modify `bindings.json` and overwrite any bindings to
2222
`key` that already exist.
2323

24-
* `help ['topic']`: opens the corresponding help topic. If no topic is provided
25-
opens the default help screen. Help topics are stored as `.md` files in the
26-
`runtime/help` directory of the source tree, which is embedded in the final
27-
binary.
24+
* `help ['topic'] ['flags']`: opens the corresponding help topics.
25+
If no topic is provided opens the default help screen. If multiple topics are
26+
provided (separated via ` `) they are opened all as splits.
27+
Help topics are stored as `.md` files in the `runtime/help` directory of
28+
the source tree, which is embedded in the final binary.
29+
The `flags` are optional.
30+
* `-hsplit`: Opens the help topic in a horizontal split
31+
* `-vsplit`: Opens the help topic in a vertical split
32+
33+
The default split type is defined by the global `helpsplit` option.
2834

2935
* `save ['filename']`: saves the current buffer. If the file is provided it
3036
will 'save as' the filename.
@@ -72,12 +78,15 @@ quotes here but these are not necessary when entering the command in micro.
7278
command's output will be displayed in one line when it finishes running.
7379

7480
* `vsplit ['filename']`: opens a vertical split with `filename`. If no filename
75-
is provided, a vertical split is opened with an empty buffer.
81+
is provided, a vertical split is opened with an empty buffer. If multiple
82+
files are provided (separated via ` `) they are opened all as splits.
7683

7784
* `hsplit ['filename']`: same as `vsplit` but opens a horizontal split instead
7885
of a vertical split.
7986

80-
* `tab ['filename']`: opens the given file in a new tab.
87+
* `tab ['filename']`: opens the given file in a new tab. If no filename
88+
is provided, a tab is opened with an empty buffer. If multiple files are
89+
provided (separated via ` `) they are opened all as tabs.
8190

8291
* `tabmove '[-+]n'`: Moves the active tab to another slot. `n` is an integer.
8392
If `n` is prefixed with `-` or `+`, then it represents a relative position

runtime/help/options.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ Here are the available options:
172172
default value: `unknown`. This will be automatically overridden depending
173173
on the file you open.
174174

175+
* `helpsplit`: sets the split type to be used by the `help` command.
176+
Possible values:
177+
* `vsplit`: open help in a vertical split pane
178+
* `hsplit`: open help in a horizontal split pane
179+
180+
default value: `hsplit`
181+
175182
* `hlsearch`: highlight all instances of the searched text after a successful
176183
search. This highlighting can be temporarily turned off via the
177184
`UnhighlightSearch` action (triggered by the Esc key by default) or toggled

0 commit comments

Comments
 (0)