aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/textformat.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/textformat.c')
-rw-r--r--src/nvim/textformat.c125
1 files changed, 57 insertions, 68 deletions
diff --git a/src/nvim/textformat.c b/src/nvim/textformat.c
index fbea1ccfb7..b69d438a59 100644
--- a/src/nvim/textformat.c
+++ b/src/nvim/textformat.c
@@ -1,13 +1,10 @@
-// This is an open source non-commercial project. Dear PVS-Studio, please check
-// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
-
// textformat.c: text formatting functions
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
-#include "nvim/ascii.h"
+#include "nvim/ascii_defs.h"
#include "nvim/buffer_defs.h"
#include "nvim/change.h"
#include "nvim/charset.h"
@@ -17,29 +14,30 @@
#include "nvim/eval.h"
#include "nvim/eval/typval_defs.h"
#include "nvim/ex_cmds_defs.h"
+#include "nvim/func_attr.h"
#include "nvim/getchar.h"
#include "nvim/globals.h"
#include "nvim/indent.h"
#include "nvim/indent_c.h"
-#include "nvim/macros.h"
+#include "nvim/macros_defs.h"
#include "nvim/mark.h"
#include "nvim/mbyte.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/move.h"
-#include "nvim/normal.h"
#include "nvim/ops.h"
#include "nvim/option.h"
+#include "nvim/option_vars.h"
#include "nvim/os/input.h"
-#include "nvim/pos.h"
+#include "nvim/pos_defs.h"
#include "nvim/search.h"
+#include "nvim/state_defs.h"
#include "nvim/strings.h"
#include "nvim/textformat.h"
#include "nvim/textobject.h"
-#include "nvim/types.h"
#include "nvim/undo.h"
-#include "nvim/vim.h"
+#include "nvim/vim_defs.h"
#include "nvim/window.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
@@ -72,11 +70,11 @@ bool has_format_option(int x)
void internal_format(int textwidth, int second_indent, int flags, bool format_only, int c)
{
int cc;
- int save_char = NUL;
+ char save_char = NUL;
bool haveto_redraw = false;
const bool fo_ins_blank = has_format_option(FO_INS_BLANK);
const bool fo_multibyte = has_format_option(FO_MBYTE_BREAK);
- const bool fo_rigor_tw = has_format_option(FO_RIGOROUS_TW);
+ const bool fo_rigor_tw = has_format_option(FO_RIGOROUS_TW);
const bool fo_white_par = has_format_option(FO_WHITE_PAR);
bool first_line = true;
colnr_T leader_len;
@@ -93,7 +91,7 @@ void internal_format(int textwidth, int second_indent, int flags, bool format_on
&& !(State & VREPLACE_FLAG)) {
cc = gchar_cursor();
if (ascii_iswhite(cc)) {
- save_char = cc;
+ save_char = (char)cc;
pchar_cursor('x');
}
}
@@ -104,7 +102,6 @@ void internal_format(int textwidth, int second_indent, int flags, bool format_on
int wantcol; // column at textwidth border
int foundcol; // column for start of spaces
int end_foundcol = 0; // column for start of word
- colnr_T len;
colnr_T virtcol;
int orig_col = 0;
char *saved_text = NULL;
@@ -309,7 +306,7 @@ void internal_format(int textwidth, int second_indent, int flags, bool format_on
col = curwin->w_cursor.col;
inc_cursor();
- cc = ncc;
+ cc = ncc;
ncc = gchar_cursor();
// handle insert
ncc = (ncc != NUL) ? ncc : c;
@@ -318,7 +315,7 @@ void internal_format(int textwidth, int second_indent, int flags, bool format_on
if (allow_break) {
// Break only when we are not at end of line.
- end_foundcol = foundcol = ncc == NUL? 0 : curwin->w_cursor.col;
+ end_foundcol = foundcol = ncc == NUL ? 0 : curwin->w_cursor.col;
break;
}
curwin->w_cursor.col = col;
@@ -423,7 +420,6 @@ void internal_format(int textwidth, int second_indent, int flags, bool format_on
for (int i = 0; i < padding; i++) {
ins_str(" ");
}
- changed_bytes(curwin->w_cursor.lnum, leader_len);
} else {
(void)set_indent(second_indent, SIN_CHANGED);
}
@@ -441,7 +437,7 @@ void internal_format(int textwidth, int second_indent, int flags, bool format_on
// Check if cursor is not past the NUL off the line, cindent
// may have added or removed indent.
curwin->w_cursor.col += startcol;
- len = (colnr_T)strlen(get_cursor_line_ptr());
+ colnr_T len = (colnr_T)strlen(get_cursor_line_ptr());
if (curwin->w_cursor.col > len) {
curwin->w_cursor.col = len;
}
@@ -458,7 +454,7 @@ void internal_format(int textwidth, int second_indent, int flags, bool format_on
}
if (save_char != NUL) { // put back space after cursor
- pchar_cursor((char_u)save_char);
+ pchar_cursor(save_char);
}
curwin->w_p_lbr = has_lbr;
@@ -521,10 +517,8 @@ static bool ends_in_white(linenr_T lnum)
static bool same_leader(linenr_T lnum, int leader1_len, char *leader1_flags, int leader2_len,
char *leader2_flags)
{
- int idx1 = 0, idx2 = 0;
- char *p;
- char *line1;
- char *line2;
+ int idx1 = 0;
+ int idx2 = 0;
if (leader1_len == 0) {
return leader2_len == 0;
@@ -536,7 +530,7 @@ static bool same_leader(linenr_T lnum, int leader1_len, char *leader1_flags, int
// If first leader has 's' flag, the lines can only be joined if there is
// some text after it and the second line has the 'm' flag.
if (leader1_flags != NULL) {
- for (p = leader1_flags; *p && *p != ':'; p++) {
+ for (char *p = leader1_flags; *p && *p != ':'; p++) {
if (*p == COM_FIRST) {
return leader2_len == 0;
}
@@ -563,9 +557,9 @@ static bool same_leader(linenr_T lnum, int leader1_len, char *leader1_flags, int
// Get current line and next line, compare the leaders.
// The first line has to be saved, only one line can be locked at a time.
- line1 = xstrdup(ml_get(lnum));
+ char *line1 = xstrdup(ml_get(lnum));
for (idx1 = 0; ascii_iswhite(line1[idx1]); idx1++) {}
- line2 = ml_get(lnum + 1);
+ char *line2 = ml_get(lnum + 1);
for (idx2 = 0; idx2 < leader2_len; idx2++) {
if (!ascii_iswhite(line2[idx2])) {
if (line1[idx1++] != line2[idx2]) {
@@ -588,7 +582,6 @@ static bool same_leader(linenr_T lnum, int leader1_len, char *leader1_flags, int
/// false when the previous line is in the same paragraph.
static bool paragraph_start(linenr_T lnum)
{
- char *p;
int leader_len = 0; // leader len of current line
char *leader_flags = NULL; // flags for leader of current line
int next_leader_len = 0; // leader len of next line
@@ -597,7 +590,7 @@ static bool paragraph_start(linenr_T lnum)
if (lnum <= 1) {
return true; // start of the file
}
- p = ml_get(lnum - 1);
+ char *p = ml_get(lnum - 1);
if (*p == NUL) {
return true; // after empty line
}
@@ -633,19 +626,14 @@ static bool paragraph_start(linenr_T lnum)
/// @param prev_line may start in previous line
void auto_format(bool trailblank, bool prev_line)
{
- pos_T pos;
- colnr_T len;
- char *old;
- char *new, *pnew;
- int wasatend;
- int cc;
+ char *linep;
if (!has_format_option(FO_AUTO)) {
return;
}
- pos = curwin->w_cursor;
- old = get_cursor_line_ptr();
+ pos_T pos = curwin->w_cursor;
+ char *old = get_cursor_line_ptr();
// may remove added space
check_auto_format(false);
@@ -655,10 +643,10 @@ void auto_format(bool trailblank, bool prev_line)
// in 'formatoptions' and there is a single character before the cursor.
// Otherwise the line would be broken and when typing another non-white
// next they are not joined back together.
- wasatend = (pos.col == (colnr_T)strlen(old));
+ int wasatend = (pos.col == (colnr_T)strlen(old));
if (*old != NUL && !trailblank && wasatend) {
dec_cursor();
- cc = gchar_cursor();
+ int cc = gchar_cursor();
if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
&& has_format_option(FO_ONE_LETTER)) {
dec_cursor();
@@ -708,13 +696,13 @@ void auto_format(bool trailblank, bool prev_line)
// need to add a space when 'w' is in 'formatoptions' to keep a paragraph
// formatted.
if (!wasatend && has_format_option(FO_WHITE_PAR)) {
- new = get_cursor_line_ptr();
- len = (colnr_T)strlen(new);
+ linep = get_cursor_line_ptr();
+ colnr_T len = (colnr_T)strlen(linep);
if (curwin->w_cursor.col == len) {
- pnew = xstrnsave(new, (size_t)len + 2);
- pnew[len] = ' ';
- pnew[len + 1] = NUL;
- ml_replace(curwin->w_cursor.lnum, pnew, false);
+ char *plinep = xstrnsave(linep, (size_t)len + 2);
+ plinep[len] = ' ';
+ plinep[len + 1] = NUL;
+ ml_replace(curwin->w_cursor.lnum, plinep, false);
// remove the space later
did_add_space = true;
} else {
@@ -733,25 +721,25 @@ void auto_format(bool trailblank, bool prev_line)
/// @param end_insert true when ending Insert mode
void check_auto_format(bool end_insert)
{
- int c = ' ';
- int cc;
+ if (!did_add_space) {
+ return;
+ }
- if (did_add_space) {
- cc = gchar_cursor();
- if (!WHITECHAR(cc)) {
- // Somehow the space was removed already.
+ int cc = gchar_cursor();
+ if (!WHITECHAR(cc)) {
+ // Somehow the space was removed already.
+ did_add_space = false;
+ } else {
+ int c = ' ';
+ if (!end_insert) {
+ inc_cursor();
+ c = gchar_cursor();
+ dec_cursor();
+ }
+ if (c != NUL) {
+ // The space is no longer at the end of the line, delete it.
+ del_char(false);
did_add_space = false;
- } else {
- if (!end_insert) {
- inc_cursor();
- c = gchar_cursor();
- dec_cursor();
- }
- if (c != NUL) {
- // The space is no longer at the end of the line, delete it.
- del_char(false);
- did_add_space = false;
- }
}
}
}
@@ -825,7 +813,7 @@ void op_format(oparg_T *oap, bool keep_cursor)
saved_cursor = oap->cursor_start;
}
- format_lines((linenr_T)oap->line_count, keep_cursor);
+ format_lines(oap->line_count, keep_cursor);
// Leave the cursor at the first non-blank of the last formatted line.
// If the cursor was moved one line back (e.g. with "Q}") go to the next
@@ -884,7 +872,7 @@ void op_formatexpr(oparg_T *oap)
int fex_format(linenr_T lnum, long count, int c)
{
int use_sandbox = was_set_insecurely(curwin, "formatexpr", OPT_LOCAL);
- int r;
+ const sctx_T save_sctx = current_sctx;
// Set v:lnum to the first line number and v:count to the number of lines.
// Set v:char to the character to be inserted (can be NUL).
@@ -894,17 +882,20 @@ int fex_format(linenr_T lnum, long count, int c)
// Make a copy, the option could be changed while calling it.
char *fex = xstrdup(curbuf->b_p_fex);
+ current_sctx = curbuf->b_p_script_ctx[BV_FEX].script_ctx;
+
// Evaluate the function.
if (use_sandbox) {
sandbox++;
}
- r = (int)eval_to_number(fex);
+ int r = (int)eval_to_number(fex);
if (use_sandbox) {
sandbox--;
}
set_vim_var_string(VV_CHAR, NULL, -1);
xfree(fex);
+ current_sctx = save_sctx;
return r;
}
@@ -940,7 +931,7 @@ void format_lines(linenr_T line_count, bool avoid_fex)
// length of a line to force formatting: 3 * 'tw'
const int max_len = comp_textwidth(true) * 3;
- // check for 'q', '2' and '1' in 'formatoptions'
+ // check for 'q', '2', 'n' and 'w' in 'formatoptions'
const bool do_comments = has_format_option(FO_Q_COMS); // format comments
int do_comments_list = 0; // format comments with 'n' or '2'
const bool do_second_indent = has_format_option(FO_Q_SECOND);
@@ -1108,15 +1099,13 @@ void format_lines(linenr_T line_count, bool avoid_fex)
}
if (next_leader_len > 0) {
(void)del_bytes(next_leader_len, false, false);
- mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
- (long)-next_leader_len, 0);
+ mark_col_adjust(curwin->w_cursor.lnum, 0, 0, -next_leader_len, 0);
} else if (second_indent > 0) { // the "leader" for FO_Q_SECOND
int indent = (int)getwhitecols_curline();
if (indent > 0) {
(void)del_bytes(indent, false, false);
- mark_col_adjust(curwin->w_cursor.lnum,
- (colnr_T)0, 0L, (long)-indent, 0);
+ mark_col_adjust(curwin->w_cursor.lnum, 0, 0, -indent, 0);
}
}
curwin->w_cursor.lnum--;