Skip to content

Commit

Permalink
nano: added options tabstospaces & autoindent
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Oct 1, 2019
1 parent ff75120 commit 2e09ce4
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public class Nano implements Editor {
private boolean cut2end = false;
private boolean tempFile = false;
private String historyLog = null;
private boolean tabsToSpaces = false;
private boolean autoIndent = false;

// Input
protected final List<Buffer> buffers = new ArrayList<>();
Expand Down Expand Up @@ -178,7 +180,9 @@ public static String[] usage() {
" -v --view Don't allow the contents of the file to be altered: read-only mode.",
" -k --cutfromcursor Make the 'Cut Text' command cut from the current cursor position to the end of the line",
" -t --tempfile Save a changed buffer without prompting (when exiting with ^X).",
" -H --historylog=name Log search strings to file, so they can be retrieved in later sessions"
" -H --historylog=name Log search strings to file, so they can be retrieved in later sessions",
" -E --tabstospaces Convert typed tabs to spaces.",
" -i --autoindent Indent new lines to the previous line's indentation."
};
return usage;
}
Expand Down Expand Up @@ -335,11 +339,34 @@ private int charPosition(int line, int displayPosition, CursorMovement move){
return out;
}

String blanks(int nb) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nb; i++) {
sb.append(' ');
}
return sb.toString();
}

void insert(String insert) {
String text = lines.get(line);
int pos = charPosition(offsetInLine + column);
insert = insert.replaceAll("\r\n", "\n");
insert = insert.replaceAll("\r", "\n");
if (tabsToSpaces && insert.length() == 1 && insert.charAt(0) == '\t') {
int len = pos == text.length() ? length(text + insert) : length(text.substring(0, pos) + insert);
insert = blanks(len - offsetInLine - column);
}
if (autoIndent && insert.length() == 1 && insert.charAt(0) == '\n') {
for (char c : lines.get(line).toCharArray()) {
if (c == ' ') {
insert += c;
} else if (c == '\t') {
insert += c;
} else {
break;
}
}
}
String mod;
String tail = "";
if (pos == text.length()) {
Expand Down Expand Up @@ -1923,6 +1950,12 @@ public Nano(Terminal terminal, Path root, Options opts, ConfigurationPath config
if (opts.isSet("historylog")) {
historyLog = opts.get("historyLog");
}
if (opts.isSet("tabstospaces")) {
tabsToSpaces = true;
}
if (opts.isSet("autoindent")) {
autoIndent = true;
}
}
bindKeys();
if (configPath != null && historyLog != null) {
Expand Down Expand Up @@ -1981,6 +2014,10 @@ private void parseConfig(Path file) throws IOException {
cut2end = val;
} else if (option.equals("tempfile")) {
tempFile = val;
} else if (option.equals("tabstospaces")) {
tabsToSpaces = val;
} else if (option.equals("autoindent")) {
autoIndent = val;
} else {
errorMessage = "Nano config: Unknown or unsupported configuration option " + option;
}
Expand Down Expand Up @@ -2228,6 +2265,14 @@ public void run() throws IOException {
highlight = !highlight;
setMessage("Highlight " + (highlight ? "enabled" : "disabled"));
break;
case TABS_TO_SPACE:
tabsToSpaces = !tabsToSpaces;
setMessage("Conversion of typed tabs to spaces " + (tabsToSpaces ? "enabled" : "disabled"));
break;
case AUTO_INDENT:
autoIndent = !autoIndent;
setMessage("Auto indent " + (autoIndent ? "enabled" : "disabled"));
break;
default:
setMessage("Unsupported " + op.name().toLowerCase().replace('_', '-'));
break;
Expand Down

1 comment on commit 2e09ce4

@mattirn
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.