aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2016-09-11 04:01:41 +0200
committerJustin M. Keyes <justinkz@gmail.com>2016-09-13 16:20:09 +0200
commitdf072c3b2b20fb7d3d9d50b5ab0df92827aa628f (patch)
treefb80cb1409d60a6316d534b989451cd9986934e6
parent7eb4d2f79dcc712dae1513516b9db5f574d51437 (diff)
downloadrneovim-df072c3b2b20fb7d3d9d50b5ab0df92827aa628f.tar.gz
rneovim-df072c3b2b20fb7d3d9d50b5ab0df92827aa628f.tar.bz2
rneovim-df072c3b2b20fb7d3d9d50b5ab0df92827aa628f.zip
refactor: eliminate misc2.c
move `call_shell` to misc1.c Move some fns to state.c Move some fns to option.c Move some fns to memline.c Move `vim_chdir*` fns to file_search.c Move some fns to new module, bytes.c Move some fns to fileio.c
-rw-r--r--src/nvim/CMakeLists.txt2
-rw-r--r--src/nvim/api/buffer.c1
-rw-r--r--src/nvim/api/vim.c3
-rw-r--r--src/nvim/api/window.c1
-rw-r--r--src/nvim/buffer.c2
-rw-r--r--src/nvim/charset.c3
-rw-r--r--src/nvim/cursor.c1
-rw-r--r--src/nvim/cursor.h1
-rw-r--r--src/nvim/cursor_shape.c1
-rw-r--r--src/nvim/diff.c1
-rw-r--r--src/nvim/digraph.c1
-rw-r--r--src/nvim/edit.c1
-rw-r--r--src/nvim/eval.c2
-rw-r--r--src/nvim/ex_cmds2.c1
-rw-r--r--src/nvim/ex_docmd.c1
-rw-r--r--src/nvim/ex_eval.c1
-rw-r--r--src/nvim/ex_getln.c1
-rw-r--r--src/nvim/farsi.c1
-rw-r--r--src/nvim/file_search.c113
-rw-r--r--src/nvim/fileio.c96
-rw-r--r--src/nvim/fileio.h2
-rw-r--r--src/nvim/fold.c1
-rw-r--r--src/nvim/garray.c1
-rw-r--r--src/nvim/getchar.c2
-rw-r--r--src/nvim/hardcopy.c1
-rw-r--r--src/nvim/hashtab.c1
-rw-r--r--src/nvim/if_cscope.c1
-rw-r--r--src/nvim/indent.c1
-rw-r--r--src/nvim/indent_c.c1
-rw-r--r--src/nvim/main.c1
-rw-r--r--src/nvim/mark.c1
-rw-r--r--src/nvim/mbyte.c1
-rw-r--r--src/nvim/memfile.c1
-rw-r--r--src/nvim/memline.c95
-rw-r--r--src/nvim/memory.c10
-rw-r--r--src/nvim/memory.h1
-rw-r--r--src/nvim/menu.c2
-rw-r--r--src/nvim/message.c1
-rw-r--r--src/nvim/misc1.c50
-rw-r--r--src/nvim/misc2.c464
-rw-r--r--src/nvim/misc2.h12
-rw-r--r--src/nvim/mouse.c1
-rw-r--r--src/nvim/move.c1
-rw-r--r--src/nvim/normal.c1
-rw-r--r--src/nvim/ops.c2
-rw-r--r--src/nvim/option.c145
-rw-r--r--src/nvim/option.h2
-rw-r--r--src/nvim/os/env.c1
-rw-r--r--src/nvim/os/fs.c1
-rw-r--r--src/nvim/os/input.c2
-rw-r--r--src/nvim/os/shell.c1
-rw-r--r--src/nvim/os/signal.c1
-rw-r--r--src/nvim/os/users.c1
-rw-r--r--src/nvim/os_unix.c1
-rw-r--r--src/nvim/path.c1
-rw-r--r--src/nvim/popupmnu.c1
-rw-r--r--src/nvim/quickfix.c1
-rw-r--r--src/nvim/regexp.c1
-rw-r--r--src/nvim/regexp_nfa.c1
-rw-r--r--src/nvim/screen.c2
-rw-r--r--src/nvim/search.c1
-rw-r--r--src/nvim/shada.c1
-rw-r--r--src/nvim/spell.c1
-rw-r--r--src/nvim/state.c34
-rw-r--r--src/nvim/strings.c1
-rw-r--r--src/nvim/syntax.c1
-rw-r--r--src/nvim/tag.c1
-rw-r--r--src/nvim/tui/input.c1
-rw-r--r--src/nvim/ui.c2
-rw-r--r--src/nvim/undo.c1
-rw-r--r--src/nvim/version.c1
-rw-r--r--src/nvim/window.c2
72 files changed, 489 insertions, 609 deletions
diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt
index e2de2530d6..2b29482a13 100644
--- a/src/nvim/CMakeLists.txt
+++ b/src/nvim/CMakeLists.txt
@@ -89,7 +89,7 @@ endforeach()
list(REMOVE_ITEM NEOVIM_SOURCES ${to_remove})
-# Handle legacy files that don't yet pass -Wconversion.
+# Legacy files that do not yet pass -Wconversion.
set(CONV_SOURCES
buffer.c
diff.c
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c
index cf8d538f86..602372a661 100644
--- a/src/nvim/api/buffer.c
+++ b/src/nvim/api/buffer.c
@@ -14,7 +14,6 @@
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/ex_cmds.h"
#include "nvim/mark.h"
#include "nvim/fileio.h"
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c
index 70e0c55528..d123b6b8d8 100644
--- a/src/nvim/api/vim.c
+++ b/src/nvim/api/vim.c
@@ -14,6 +14,7 @@
#include "nvim/msgpack_rpc/channel.h"
#include "nvim/vim.h"
#include "nvim/buffer.h"
+#include "nvim/file_search.h"
#include "nvim/window.h"
#include "nvim/types.h"
#include "nvim/ex_docmd.h"
@@ -21,7 +22,7 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/eval.h"
-#include "nvim/misc2.h"
+#include "nvim/option.h"
#include "nvim/syntax.h"
#include "nvim/getchar.h"
#include "nvim/os/input.h"
diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c
index 9c7fdb0697..ade0fe7c01 100644
--- a/src/nvim/api/window.c
+++ b/src/nvim/api/window.c
@@ -11,7 +11,6 @@
#include "nvim/window.h"
#include "nvim/screen.h"
#include "nvim/move.h"
-#include "nvim/misc2.h"
/// Gets the current buffer in a window
diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c
index af29798654..b42ad1c18a 100644
--- a/src/nvim/buffer.c
+++ b/src/nvim/buffer.c
@@ -36,6 +36,7 @@
#include "nvim/ex_eval.h"
#include "nvim/ex_getln.h"
#include "nvim/fileio.h"
+#include "nvim/file_search.h"
#include "nvim/fold.h"
#include "nvim/getchar.h"
#include "nvim/hashtab.h"
@@ -48,7 +49,6 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/garray.h"
#include "nvim/move.h"
#include "nvim/option.h"
diff --git a/src/nvim/charset.c b/src/nvim/charset.c
index b5154819b9..78f5d96fc7 100644
--- a/src/nvim/charset.c
+++ b/src/nvim/charset.c
@@ -19,10 +19,11 @@
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/garray.h"
#include "nvim/move.h"
+#include "nvim/option.h"
#include "nvim/os_unix.h"
+#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/path.h"
diff --git a/src/nvim/cursor.c b/src/nvim/cursor.c
index afaa6022c9..3ba9da34f2 100644
--- a/src/nvim/cursor.c
+++ b/src/nvim/cursor.c
@@ -9,6 +9,7 @@
#include "nvim/misc1.h"
#include "nvim/move.h"
#include "nvim/screen.h"
+#include "nvim/state.h"
#include "nvim/vim.h"
#include "nvim/ascii.h"
diff --git a/src/nvim/cursor.h b/src/nvim/cursor.h
index 09cc5a813c..1cbe8a609e 100644
--- a/src/nvim/cursor.h
+++ b/src/nvim/cursor.h
@@ -4,7 +4,6 @@
#include <stdbool.h>
#include "nvim/vim.h"
-#include "nvim/misc2.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "cursor.h.generated.h"
diff --git a/src/nvim/cursor_shape.c b/src/nvim/cursor_shape.c
index 87425ca567..b50462664c 100644
--- a/src/nvim/cursor_shape.c
+++ b/src/nvim/cursor_shape.c
@@ -3,7 +3,6 @@
#include "nvim/vim.h"
#include "nvim/ascii.h"
#include "nvim/cursor_shape.h"
-#include "nvim/misc2.h"
#include "nvim/ex_getln.h"
#include "nvim/charset.h"
#include "nvim/strings.h"
diff --git a/src/nvim/diff.c b/src/nvim/diff.c
index 4826e70727..ef5acf4845 100644
--- a/src/nvim/diff.c
+++ b/src/nvim/diff.c
@@ -21,7 +21,6 @@
#include "nvim/memline.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/memory.h"
#include "nvim/move.h"
#include "nvim/normal.h"
diff --git a/src/nvim/digraph.c b/src/nvim/digraph.c
index 4512b97967..6ba6e659a6 100644
--- a/src/nvim/digraph.c
+++ b/src/nvim/digraph.c
@@ -17,7 +17,6 @@
#include "nvim/getchar.h"
#include "nvim/mbyte.h"
#include "nvim/message.h"
-#include "nvim/misc2.h"
#include "nvim/memory.h"
#include "nvim/garray.h"
#include "nvim/normal.h"
diff --git a/src/nvim/edit.c b/src/nvim/edit.c
index af5ec6c5ac..ff1107db6c 100644
--- a/src/nvim/edit.c
+++ b/src/nvim/edit.c
@@ -29,7 +29,6 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/keymap.h"
#include "nvim/move.h"
#include "nvim/normal.h"
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index 72b1807334..ccbc5a6264 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -44,7 +44,6 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/keymap.h"
#include "nvim/map.h"
#include "nvim/file_search.h"
@@ -63,6 +62,7 @@
#include "nvim/search.h"
#include "nvim/sha256.h"
#include "nvim/spell.h"
+#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/tag.h"
diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c
index d0a17ed099..f68663c60c 100644
--- a/src/nvim/ex_cmds2.c
+++ b/src/nvim/ex_cmds2.c
@@ -27,7 +27,6 @@
#include "nvim/mbyte.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/garray.h"
#include "nvim/memory.h"
#include "nvim/move.h"
diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c
index e3ca177029..45407b7f12 100644
--- a/src/nvim/ex_docmd.c
+++ b/src/nvim/ex_docmd.c
@@ -36,7 +36,6 @@
#include "nvim/menu.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/keymap.h"
#include "nvim/file_search.h"
#include "nvim/garray.h"
diff --git a/src/nvim/ex_eval.c b/src/nvim/ex_eval.c
index 82d4c2b2d5..f518fa0d66 100644
--- a/src/nvim/ex_eval.c
+++ b/src/nvim/ex_eval.c
@@ -15,7 +15,6 @@
#include "nvim/ex_cmds2.h"
#include "nvim/ex_docmd.h"
#include "nvim/message.h"
-#include "nvim/misc2.h"
#include "nvim/memory.h"
#include "nvim/regexp.h"
#include "nvim/strings.h"
diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c
index cd28554970..1a97dc3d6f 100644
--- a/src/nvim/ex_getln.c
+++ b/src/nvim/ex_getln.c
@@ -34,7 +34,6 @@
#include "nvim/menu.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/memory.h"
#include "nvim/cursor_shape.h"
#include "nvim/keymap.h"
diff --git a/src/nvim/farsi.c b/src/nvim/farsi.c
index 61e17128ea..eb22ad1428 100644
--- a/src/nvim/farsi.c
+++ b/src/nvim/farsi.c
@@ -15,7 +15,6 @@
#include "nvim/memline.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/screen.h"
#include "nvim/strings.h"
#include "nvim/vim.h"
diff --git a/src/nvim/file_search.c b/src/nvim/file_search.c
index 78b224f04c..c6cfba8142 100644
--- a/src/nvim/file_search.c
+++ b/src/nvim/file_search.c
@@ -1,48 +1,44 @@
-/* TODO: make some #ifdef for this */
-/*--------[ file searching ]-------------------------------------------------*/
-/*
- * File searching functions for 'path', 'tags' and 'cdpath' options.
- * External visible functions:
- * vim_findfile_init() creates/initialises the search context
- * vim_findfile_free_visited() free list of visited files/dirs of search
- * context
- * vim_findfile() find a file in the search context
- * vim_findfile_cleanup() cleanup/free search context created by
- * vim_findfile_init()
- *
- * All static functions and variables start with 'ff_'
- *
- * In general it works like this:
- * First you create yourself a search context by calling vim_findfile_init().
- * It is possible to give a search context from a previous call to
- * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
- * until you are satisfied with the result or it returns NULL. On every call it
- * returns the next file which matches the conditions given to
- * vim_findfile_init(). If it doesn't find a next file it returns NULL.
- *
- * It is possible to call vim_findfile_init() again to reinitialise your search
- * with some new parameters. Don't forget to pass your old search context to
- * it, so it can reuse it and especially reuse the list of already visited
- * directories. If you want to delete the list of already visited directories
- * simply call vim_findfile_free_visited().
- *
- * When you are done call vim_findfile_cleanup() to free the search context.
- *
- * The function vim_findfile_init() has a long comment, which describes the
- * needed parameters.
- *
- *
- *
- * ATTENTION:
- * ==========
- * Also we use an allocated search context here, this functions are NOT
- * thread-safe!!!!!
- *
- * To minimize parameter passing (or because I'm to lazy), only the
- * external visible functions get a search context as a parameter. This is
- * then assigned to a static global, which is used throughout the local
- * functions.
- */
+// File searching functions for 'path', 'tags' and 'cdpath' options.
+//
+// External visible functions:
+// vim_findfile_init() creates/initialises the search context
+// vim_findfile_free_visited() free list of visited files/dirs of search
+// context
+// vim_findfile() find a file in the search context
+// vim_findfile_cleanup() cleanup/free search context created by
+// vim_findfile_init()
+//
+// All static functions and variables start with 'ff_'
+//
+// In general it works like this:
+// First you create yourself a search context by calling vim_findfile_init().
+// It is possible to give a search context from a previous call to
+// vim_findfile_init(), so it can be reused. After this you call vim_findfile()
+// until you are satisfied with the result or it returns NULL. On every call it
+// returns the next file which matches the conditions given to
+// vim_findfile_init(). If it doesn't find a next file it returns NULL.
+//
+// It is possible to call vim_findfile_init() again to reinitialise your search
+// with some new parameters. Don't forget to pass your old search context to
+// it, so it can reuse it and especially reuse the list of already visited
+// directories. If you want to delete the list of already visited directories
+// simply call vim_findfile_free_visited().
+//
+// When you are done call vim_findfile_cleanup() to free the search context.
+//
+// The function vim_findfile_init() has a long comment, which describes the
+// needed parameters.
+//
+//
+//
+// ATTENTION:
+// ==========
+// We use an allocated search context, these functions are NOT thread-safe!!!!!
+//
+// To minimize parameter passing (or because I'm too lazy), only the
+// external visible functions get a search context as a parameter. This is
+// then assigned to a static global, which is used throughout the local
+// functions.
#include <assert.h>
#include <string.h>
@@ -59,7 +55,7 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
+#include "nvim/option.h"
#include "nvim/os_unix.h"
#include "nvim/path.h"
#include "nvim/strings.h"
@@ -1526,3 +1522,28 @@ theend:
return file_name;
}
+/// Change to a file's directory.
+/// Caller must call shorten_fnames()!
+/// @return OK or FAIL
+int vim_chdirfile(char_u *fname)
+{
+ char_u dir[MAXPATHL];
+
+ STRLCPY(dir, fname, MAXPATHL);
+ *path_tail_with_sep(dir) = NUL;
+ return os_chdir((char *)dir) == 0 ? OK : FAIL;
+}
+
+/// Change directory to "new_dir". Search 'cdpath' for relative directory names.
+int vim_chdir(char_u *new_dir)
+{
+ char_u *dir_name = find_directory_in_path(new_dir, STRLEN(new_dir),
+ FNAME_MESS, curbuf->b_ffname);
+ if (dir_name == NULL) {
+ return -1;
+ }
+ int r = os_chdir((char *)dir_name);
+ xfree(dir_name);
+ return r;
+}
+
diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c
index 89e5fdb776..4226f2e8d2 100644
--- a/src/nvim/fileio.c
+++ b/src/nvim/fileio.c
@@ -32,7 +32,6 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/garray.h"
#include "nvim/move.h"
#include "nvim/normal.h"
@@ -44,6 +43,7 @@
#include "nvim/screen.h"
#include "nvim/search.h"
#include "nvim/sha256.h"
+#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/ui.h"
#include "nvim/types.h"
@@ -4447,11 +4447,95 @@ bool vim_fgets(char_u *buf, int size, FILE *fp) FUNC_ATTR_NONNULL_ALL
return eof == NULL;
}
-/*
- * os_rename() only works if both files are on the same file system, this
- * function will (attempts to?) copy the file across if rename fails -- webb
- * Return -1 for failure, 0 for success.
- */
+/// Read 2 bytes from "fd" and turn them into an int, MSB first.
+int get2c(FILE *fd)
+{
+ int n;
+
+ n = getc(fd);
+ n = (n << 8) + getc(fd);
+ return n;
+}
+
+/// Read 3 bytes from "fd" and turn them into an int, MSB first.
+int get3c(FILE *fd)
+{
+ int n;
+
+ n = getc(fd);
+ n = (n << 8) + getc(fd);
+ n = (n << 8) + getc(fd);
+ return n;
+}
+
+/// Read 4 bytes from "fd" and turn them into an int, MSB first.
+int get4c(FILE *fd)
+{
+ // Use unsigned rather than int otherwise result is undefined
+ // when left-shift sets the MSB.
+ unsigned n;
+
+ n = (unsigned)getc(fd);
+ n = (n << 8) + (unsigned)getc(fd);
+ n = (n << 8) + (unsigned)getc(fd);
+ n = (n << 8) + (unsigned)getc(fd);
+ return (int)n;
+}
+
+/// Read 8 bytes from `fd` and turn them into a time_t, MSB first.
+time_t get8ctime(FILE *fd)
+{
+ time_t n = 0;
+ int i;
+
+ for (i = 0; i < 8; i++) {
+ n = (n << 8) + getc(fd);
+ }
+ return n;
+}
+
+/// Reads a string of length "cnt" from "fd" into allocated memory.
+/// @return pointer to the string or NULL when unable to read that many bytes.
+char *read_string(FILE *fd, size_t cnt)
+{
+ uint8_t *str = xmallocz(cnt);
+ for (size_t i = 0; i < cnt; i++) {
+ int c = getc(fd);
+ if (c == EOF) {
+ xfree(str);
+ return NULL;
+ }
+ str[i] = (uint8_t)c;
+ }
+ return (char *)str;
+}
+
+/// Writes a number to file "fd", most significant bit first, in "len" bytes.
+/// @returns false in case of an error.
+bool put_bytes(FILE *fd, uintmax_t number, size_t len)
+{
+ assert(len > 0);
+ for (size_t i = len - 1; i < len; i--) {
+ if (putc((int)(number >> (i * 8)), fd) == EOF) {
+ return false;
+ }
+ }
+ return true;
+}
+
+/// Writes time_t to file "fd" in 8 bytes.
+/// @returns FAIL when the write failed.
+int put_time(FILE *fd, time_t time_)
+{
+ uint8_t buf[8];
+ time_to_bytes(time_, buf);
+ return fwrite(buf, sizeof(uint8_t), ARRAY_SIZE(buf), fd) == 1 ? OK : FAIL;
+}
+
+/// os_rename() only works if both files are on the same file system, this
+/// function will (attempts to?) copy the file across if rename fails -- webb
+//
+/// @return -1 for failure, 0 for success
int vim_rename(char_u *from, char_u *to)
{
int fd_in;
diff --git a/src/nvim/fileio.h b/src/nvim/fileio.h
index d93f3f3eb3..ceb101167d 100644
--- a/src/nvim/fileio.h
+++ b/src/nvim/fileio.h
@@ -12,6 +12,8 @@
#define READ_DUMMY 0x10 /* reading into a dummy buffer */
#define READ_KEEP_UNDO 0x20 /* keep undo info*/
+#define READ_STRING(x, y) (char_u *)read_string((x), (size_t)(y))
+
/*
* Struct to save values in before executing autocommands for a buffer that is
* not the current buffer.
diff --git a/src/nvim/fold.c b/src/nvim/fold.c
index ac3cf959c8..6ddcbcb13f 100644
--- a/src/nvim/fold.c
+++ b/src/nvim/fold.c
@@ -22,7 +22,6 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/garray.h"
#include "nvim/move.h"
#include "nvim/option.h"
diff --git a/src/nvim/garray.c b/src/nvim/garray.c
index 98cec69b54..9ed3b901ef 100644
--- a/src/nvim/garray.c
+++ b/src/nvim/garray.c
@@ -8,7 +8,6 @@
#include "nvim/vim.h"
#include "nvim/ascii.h"
#include "nvim/log.h"
-#include "nvim/misc2.h"
#include "nvim/memory.h"
#include "nvim/path.h"
#include "nvim/garray.h"
diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c
index ae1857f318..dad0ac33cd 100644
--- a/src/nvim/getchar.c
+++ b/src/nvim/getchar.c
@@ -29,7 +29,6 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/keymap.h"
#include "nvim/garray.h"
#include "nvim/move.h"
@@ -38,6 +37,7 @@
#include "nvim/option.h"
#include "nvim/regexp.h"
#include "nvim/screen.h"
+#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
diff --git a/src/nvim/hardcopy.c b/src/nvim/hardcopy.c
index 1c9b8e18ef..7d4bfd0290 100644
--- a/src/nvim/hardcopy.c
+++ b/src/nvim/hardcopy.c
@@ -23,7 +23,6 @@
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
-#include "nvim/misc2.h"
#include "nvim/garray.h"
#include "nvim/option.h"
#include "nvim/path.h"
diff --git a/src/nvim/hashtab.c b/src/nvim/hashtab.c
index 2da937633e..7d4ae61fc4 100644
--- a/src/nvim/hashtab.c
+++ b/src/nvim/hashtab.c
@@ -28,7 +28,6 @@
#include "nvim/hashtab.h"
#include "nvim/message.h"
#include "nvim/memory.h"
-#include "nvim/misc2.h"
// Magic value for algorithm that walks through the array.
#define PERTURB_SHIFT 5
diff --git a/src/nvim/if_cscope.c b/src/nvim/if_cscope.c
index f2432dd71d..6f19b60f65 100644
--- a/src/nvim/if_cscope.c
+++ b/src/nvim/if_cscope.c
@@ -20,7 +20,6 @@
#include "nvim/eval.h"
#include "nvim/fileio.h"
#include "nvim/message.h"
-#include "nvim/misc2.h"
#include "nvim/memory.h"
#include "nvim/os/time.h"
#include "nvim/path.h"
diff --git a/src/nvim/indent.c b/src/nvim/indent.c
index f197669a97..7f31bb8c5c 100644
--- a/src/nvim/indent.c
+++ b/src/nvim/indent.c
@@ -10,7 +10,6 @@
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/move.h"
#include "nvim/option.h"
#include "nvim/regexp.h"
diff --git a/src/nvim/indent_c.c b/src/nvim/indent_c.c
index efe8e73a3c..6f03cf6037 100644
--- a/src/nvim/indent_c.c
+++ b/src/nvim/indent_c.c
@@ -12,7 +12,6 @@
#include "nvim/indent_c.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
-#include "nvim/misc2.h"
#include "nvim/option.h"
#include "nvim/search.h"
#include "nvim/strings.h"
diff --git a/src/nvim/main.c b/src/nvim/main.c
index aa1cddc6ed..a153931d8a 100644
--- a/src/nvim/main.c
+++ b/src/nvim/main.c
@@ -30,7 +30,6 @@
#include "nvim/memline.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/garray.h"
#include "nvim/log.h"
#include "nvim/memory.h"
diff --git a/src/nvim/mark.c b/src/nvim/mark.c
index fe802e48ba..2a65cf396b 100644
--- a/src/nvim/mark.c
+++ b/src/nvim/mark.c
@@ -21,7 +21,6 @@
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
-#include "nvim/misc2.h"
#include "nvim/normal.h"
#include "nvim/option.h"
#include "nvim/path.h"
diff --git a/src/nvim/mbyte.c b/src/nvim/mbyte.c
index 26d94aa6fa..f577fd847e 100644
--- a/src/nvim/mbyte.c
+++ b/src/nvim/mbyte.c
@@ -84,7 +84,6 @@
#include "nvim/memline.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/memory.h"
#include "nvim/option.h"
#include "nvim/screen.h"
diff --git a/src/nvim/memfile.c b/src/nvim/memfile.c
index 9fb03c4ac7..43412e3916 100644
--- a/src/nvim/memfile.c
+++ b/src/nvim/memfile.c
@@ -48,7 +48,6 @@
#include "nvim/fileio.h"
#include "nvim/memline.h"
#include "nvim/message.h"
-#include "nvim/misc2.h"
#include "nvim/memory.h"
#include "nvim/os_unix.h"
#include "nvim/path.h"
diff --git a/src/nvim/memline.c b/src/nvim/memline.c
index 08e82071d7..5505335769 100644
--- a/src/nvim/memline.c
+++ b/src/nvim/memline.c
@@ -55,7 +55,6 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/option.h"
#include "nvim/os_unix.h"
#include "nvim/path.h"
@@ -3964,20 +3963,19 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp)
return size;
}
-/*
- * Goto byte in buffer with offset 'cnt'.
- */
+/// Goto byte in buffer with offset 'cnt'.
void goto_byte(long cnt)
{
long boff = cnt;
linenr_T lnum;
- ml_flush_line(curbuf); /* cached line may be dirty */
+ ml_flush_line(curbuf); // cached line may be dirty
setpcmark();
- if (boff)
- --boff;
+ if (boff) {
+ boff--;
+ }
lnum = ml_find_line_or_offset(curbuf, (linenr_T)0, &boff);
- if (lnum < 1) { /* past the end */
+ if (lnum < 1) { // past the end
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
curwin->w_curswant = MAXCOL;
coladvance((colnr_T)MAXCOL);
@@ -3989,7 +3987,84 @@ void goto_byte(long cnt)
}
check_cursor();
- /* Make sure the cursor is on the first byte of a multi-byte char. */
- if (has_mbyte)
+ // Make sure the cursor is on the first byte of a multi-byte char.
+ if (has_mbyte) {
mb_adjust_cursor();
+ }
+}
+
+/// Increment the line pointer "lp" crossing line boundaries as necessary.
+/// Return 1 when going to the next line.
+/// Return 2 when moving forward onto a NUL at the end of the line).
+/// Return -1 when at the end of file.
+/// Return 0 otherwise.
+int inc(pos_T *lp)
+{
+ char_u *p = ml_get_pos(lp);
+
+ if (*p != NUL) { // still within line, move to next char (may be NUL)
+ if (has_mbyte) {
+ int l = (*mb_ptr2len)(p);
+
+ lp->col += l;
+ return (p[l] != NUL) ? 0 : 2;
+ }
+ lp->col++;
+ lp->coladd = 0;
+ return (p[1] != NUL) ? 0 : 2;
+ }
+ if (lp->lnum != curbuf->b_ml.ml_line_count) { // there is a next line
+ lp->col = 0;
+ lp->lnum++;
+ lp->coladd = 0;
+ return 1;
+ }
+ return -1;
+}
+
+/// Same as inc(), but skip NUL at the end of non-empty lines.
+int incl(pos_T *lp)
+{
+ int r;
+
+ if ((r = inc(lp)) >= 1 && lp->col) {
+ r = inc(lp);
+ }
+ return r;
+}
+
+int dec(pos_T *lp)
+{
+ char_u *p;
+
+ lp->coladd = 0;
+ if (lp->col > 0) { // still within line
+ lp->col--;
+ if (has_mbyte) {
+ p = ml_get(lp->lnum);
+ lp->col -= (*mb_head_off)(p, p + lp->col);
+ }
+ return 0;
+ }
+ if (lp->lnum > 1) { // there is a prior line
+ lp->lnum--;
+ p = ml_get(lp->lnum);
+ lp->col = (colnr_T)STRLEN(p);
+ if (has_mbyte) {
+ lp->col -= (*mb_head_off)(p, p + lp->col);
+ }
+ return 1;
+ }
+ return -1; // at start of file
+}
+
+/// Same as dec(), but skip NUL at the end of non-empty lines.
+int decl(pos_T *lp)
+{
+ int r;
+
+ if ((r = dec(lp)) == 1 && lp->col) {
+ r = dec(lp);
+ }
+ return r;
}
diff --git a/src/nvim/memory.c b/src/nvim/memory.c
index 8db47b79c1..3e041be4d3 100644
--- a/src/nvim/memory.c
+++ b/src/nvim/memory.c
@@ -440,6 +440,16 @@ void do_outofmem_msg(size_t size)
}
}
+/// Writes time_t to "buf[8]".
+void time_to_bytes(time_t time_, uint8_t buf[8])
+{
+ // time_t can be up to 8 bytes in size, more than uintmax_t in 32 bits
+ // systems, thus we can't use put_bytes() here.
+ for (size_t i = 7, bufi = 0; bufi < 8; i--, bufi++) {
+ buf[bufi] = (uint8_t)((uint64_t)time_ >> (i * 8));
+ }
+}
+
#if defined(EXITFREE)
#include "nvim/file_search.h"
diff --git a/src/nvim/memory.h b/src/nvim/memory.h
index 7b477da2f5..62cc78360c 100644
--- a/src/nvim/memory.h
+++ b/src/nvim/memory.h
@@ -3,6 +3,7 @@
#include <stdint.h> // for uint8_t
#include <stddef.h> // for size_t
+#include <time.h> // for time_t
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "memory.h.generated.h"
diff --git a/src/nvim/menu.c b/src/nvim/menu.c
index 3c2394d579..7c0eee64dd 100644
--- a/src/nvim/menu.c
+++ b/src/nvim/menu.c
@@ -18,9 +18,9 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/keymap.h"
#include "nvim/garray.h"
+#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/ui.h"
diff --git a/src/nvim/message.c b/src/nvim/message.c
index 725c6b080a..1de2347b12 100644
--- a/src/nvim/message.c
+++ b/src/nvim/message.c
@@ -22,7 +22,6 @@
#include "nvim/mbyte.h"
#include "nvim/memory.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/keymap.h"
#include "nvim/garray.h"
#include "nvim/ops.h"
diff --git a/src/nvim/misc1.c b/src/nvim/misc1.c
index cab4edfec8..4ab059c923 100644
--- a/src/nvim/misc1.c
+++ b/src/nvim/misc1.c
@@ -31,7 +31,6 @@
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
-#include "nvim/misc2.h"
#include "nvim/garray.h"
#include "nvim/move.h"
#include "nvim/mouse.h"
@@ -41,6 +40,7 @@
#include "nvim/regexp.h"
#include "nvim/screen.h"
#include "nvim/search.h"
+#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/tag.h"
#include "nvim/ui.h"
@@ -1746,18 +1746,6 @@ int gchar_pos(pos_T *pos)
}
/*
- * Skip to next part of an option argument: Skip space and comma.
- */
-char_u *skip_to_option_part(char_u *p)
-{
- if (*p == ',')
- ++p;
- while (*p == ' ')
- ++p;
- return p;
-}
-
-/*
* Call this function when something in the current buffer is changed.
*
* Most often called through changed_bytes() and changed_lines(), which also
@@ -2691,6 +2679,42 @@ void fast_breakcheck(void)
}
}
+// Call shell. Calls os_call_shell, with 'shellxquote' added.
+int call_shell(char_u *cmd, ShellOpts opts, char_u *extra_shell_arg)
+{
+ int retval;
+ proftime_T wait_time;
+
+ if (p_verbose > 3) {
+ verbose_enter();
+ smsg(_("Calling shell to execute: \"%s\""),
+ cmd == NULL ? p_sh : cmd);
+ ui_putc('\n');
+ verbose_leave();
+ }
+
+ if (do_profiling == PROF_YES) {
+ prof_child_enter(&wait_time);
+ }
+
+ if (*p_sh == NUL) {
+ EMSG(_(e_shellempty));
+ retval = -1;
+ } else {
+ // The external command may update a tags file, clear cached tags.
+ tag_freematch();
+
+ retval = os_call_shell(cmd, opts, extra_shell_arg);
+ }
+
+ set_vim_var_nr(VV_SHELL_ERROR, (varnumber_T)retval);
+ if (do_profiling == PROF_YES) {
+ prof_child_exit(&wait_time);
+ }
+
+ return retval;
+}
+
/// Get the stdout of an external command.
/// If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
/// NULL store the length there.
diff --git a/src/nvim/misc2.c b/src/nvim/misc2.c
deleted file mode 100644
index 8b4d8c7c3e..0000000000
--- a/src/nvim/misc2.c
+++ /dev/null
@@ -1,464 +0,0 @@
-/*
- * misc2.c: Various functions.
- */
-#include <assert.h>
-#include <inttypes.h>
-#include <string.h>
-
-#include "nvim/vim.h"
-#include "nvim/ascii.h"
-#include "nvim/misc2.h"
-#include "nvim/file_search.h"
-#include "nvim/buffer.h"
-#include "nvim/charset.h"
-#include "nvim/cursor.h"
-#include "nvim/diff.h"
-#include "nvim/edit.h"
-#include "nvim/eval.h"
-#include "nvim/ex_cmds.h"
-#include "nvim/ex_docmd.h"
-#include "nvim/ex_getln.h"
-#include "nvim/fileio.h"
-#include "nvim/fold.h"
-#include "nvim/getchar.h"
-#include "nvim/macros.h"
-#include "nvim/mark.h"
-#include "nvim/mbyte.h"
-#include "nvim/memfile.h"
-#include "nvim/memline.h"
-#include "nvim/memory.h"
-#include "nvim/message.h"
-#include "nvim/misc1.h"
-#include "nvim/move.h"
-#include "nvim/option.h"
-#include "nvim/ops.h"
-#include "nvim/os_unix.h"
-#include "nvim/path.h"
-#include "nvim/quickfix.h"
-#include "nvim/regexp.h"
-#include "nvim/screen.h"
-#include "nvim/search.h"
-#include "nvim/spell.h"
-#include "nvim/strings.h"
-#include "nvim/syntax.h"
-#include "nvim/tag.h"
-#include "nvim/ui.h"
-#include "nvim/window.h"
-#include "nvim/os/os.h"
-#include "nvim/os/shell.h"
-
-
-#ifdef INCLUDE_GENERATED_DECLARATIONS
-# include "misc2.c.generated.h"
-#endif
-/*
- * Return TRUE if in the current mode we need to use virtual.
- */
-int virtual_active(void)
-{
- /* While an operator is being executed we return "virtual_op", because
- * VIsual_active has already been reset, thus we can't check for "block"
- * being used. */
- if (virtual_op != MAYBE)
- return virtual_op;
- return ve_flags == VE_ALL
- || ((ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V)
- || ((ve_flags & VE_INSERT) && (State & INSERT));
-}
-
-/*
- * Increment the line pointer "lp" crossing line boundaries as necessary.
- * Return 1 when going to the next line.
- * Return 2 when moving forward onto a NUL at the end of the line).
- * Return -1 when at the end of file.
- * Return 0 otherwise.
- */
-int inc(pos_T *lp)
-{
- char_u *p = ml_get_pos(lp);
-
- if (*p != NUL) { /* still within line, move to next char (may be NUL) */
- if (has_mbyte) {
- int l = (*mb_ptr2len)(p);
-
- lp->col += l;
- return (p[l] != NUL) ? 0 : 2;
- }
- lp->col++;
- lp->coladd = 0;
- return (p[1] != NUL) ? 0 : 2;
- }
- if (lp->lnum != curbuf->b_ml.ml_line_count) { /* there is a next line */
- lp->col = 0;
- lp->lnum++;
- lp->coladd = 0;
- return 1;
- }
- return -1;
-}
-
-/*
- * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
- */
-int incl(pos_T *lp)
-{
- int r;
-
- if ((r = inc(lp)) >= 1 && lp->col)
- r = inc(lp);
- return r;
-}
-
-int dec(pos_T *lp)
-{
- char_u *p;
-
- lp->coladd = 0;
- if (lp->col > 0) { /* still within line */
- lp->col--;
- if (has_mbyte) {
- p = ml_get(lp->lnum);
- lp->col -= (*mb_head_off)(p, p + lp->col);
- }
- return 0;
- }
- if (lp->lnum > 1) { /* there is a prior line */
- lp->lnum--;
- p = ml_get(lp->lnum);
- lp->col = (colnr_T)STRLEN(p);
- if (has_mbyte)
- lp->col -= (*mb_head_off)(p, p + lp->col);
- return 1;
- }
- return -1; /* at start of file */
-}
-
-/*
- * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
- */
-int decl(pos_T *lp)
-{
- int r;
-
- if ((r = dec(lp)) == 1 && lp->col)
- r = dec(lp);
- return r;
-}
-
-/*
- * Return TRUE when 'shell' has "csh" in the tail.
- */
-int csh_like_shell(void)
-{
- return strstr((char *)path_tail(p_sh), "csh") != NULL;
-}
-
-/*
- * Isolate one part of a string option where parts are separated with
- * "sep_chars".
- * The part is copied into "buf[maxlen]".
- * "*option" is advanced to the next part.
- * The length is returned.
- */
-size_t copy_option_part(char_u **option, char_u *buf, size_t maxlen, char *sep_chars)
-{
- size_t len = 0;
- char_u *p = *option;
-
- /* skip '.' at start of option part, for 'suffixes' */
- if (*p == '.')
- buf[len++] = *p++;
- while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL) {
- /*
- * Skip backslash before a separator character and space.
- */
- if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
- ++p;
- if (len < maxlen - 1)
- buf[len++] = *p;
- ++p;
- }
- buf[len] = NUL;
-
- if (*p != NUL && *p != ',') /* skip non-standard separator */
- ++p;
- p = skip_to_option_part(p); /* p points to next file name */
-
- *option = p;
- return len;
-}
-
-/*
- * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
- */
-int get_fileformat(buf_T *buf)
-{
- int c = *buf->b_p_ff;
-
- if (buf->b_p_bin || c == 'u')
- return EOL_UNIX;
- if (c == 'm')
- return EOL_MAC;
- return EOL_DOS;
-}
-
-/*
- * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
- * argument.
- */
-int
-get_fileformat_force (
- buf_T *buf,
- exarg_T *eap /* can be NULL! */
-)
-{
- int c;
-
- if (eap != NULL && eap->force_ff != 0)
- c = eap->cmd[eap->force_ff];
- else {
- if ((eap != NULL && eap->force_bin != 0)
- ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
- return EOL_UNIX;
- c = *buf->b_p_ff;
- }
- if (c == 'u')
- return EOL_UNIX;
- if (c == 'm')
- return EOL_MAC;
- return EOL_DOS;
-}
-
-/// Set the current end-of-line type to EOL_UNIX, EOL_MAC, or EOL_DOS.
-///
-/// Sets 'fileformat'.
-///
-/// @param eol_style End-of-line style.
-/// @param opt_flags OPT_LOCAL and/or OPT_GLOBAL
-void set_fileformat(int eol_style, int opt_flags)
-{
- char *p = NULL;
-
- switch (eol_style) {
- case EOL_UNIX:
- p = FF_UNIX;
- break;
- case EOL_MAC:
- p = FF_MAC;
- break;
- case EOL_DOS:
- p = FF_DOS;
- break;
- }
-
- // p is NULL if "eol_style" is EOL_UNKNOWN.
- if (p != NULL) {
- set_string_option_direct((char_u *)"ff",
- -1,
- (char_u *)p,
- OPT_FREE | opt_flags,
- 0);
- }
-
- // This may cause the buffer to become (un)modified.
- check_status(curbuf);
- redraw_tabline = TRUE;
- need_maketitle = TRUE; // Set window title later.
-}
-
-/*
- * Return the default fileformat from 'fileformats'.
- */
-int default_fileformat(void)
-{
- switch (*p_ffs) {
- case 'm': return EOL_MAC;
- case 'd': return EOL_DOS;
- }
- return EOL_UNIX;
-}
-
-// Call shell. Calls os_call_shell, with 'shellxquote' added.
-int call_shell(char_u *cmd, ShellOpts opts, char_u *extra_shell_arg)
-{
- int retval;
- proftime_T wait_time;
-
- if (p_verbose > 3) {
- verbose_enter();
- smsg(_("Calling shell to execute: \"%s\""),
- cmd == NULL ? p_sh : cmd);
- ui_putc('\n');
- verbose_leave();
- }
-
- if (do_profiling == PROF_YES)
- prof_child_enter(&wait_time);
-
- if (*p_sh == NUL) {
- EMSG(_(e_shellempty));
- retval = -1;
- } else {
- /* The external command may update a tags file, clear cached tags. */
- tag_freematch();
-
- retval = os_call_shell(cmd, opts, extra_shell_arg);
- }
-
- set_vim_var_nr(VV_SHELL_ERROR, (varnumber_T) retval);
- if (do_profiling == PROF_YES) {
- prof_child_exit(&wait_time);
- }
-
- return retval;
-}
-
-/*
- * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
- * NORMAL State with a condition. This function returns the real State.
- */
-int get_real_state(void)
-{
- if (State & NORMAL) {
- if (VIsual_active) {
- if (VIsual_select)
- return SELECTMODE;
- return VISUAL;
- } else if (finish_op)
- return OP_PENDING;
- }
- return State;
-}
-
-/*
- * Change to a file's directory.
- * Caller must call shorten_fnames()!
- * Return OK or FAIL.
- */
-int vim_chdirfile(char_u *fname)
-{
- char_u dir[MAXPATHL];
-
- STRLCPY(dir, fname, MAXPATHL);
- *path_tail_with_sep(dir) = NUL;
- return os_chdir((char *)dir) == 0 ? OK : FAIL;
-}
-
-/*
- * Change directory to "new_dir". Search 'cdpath' for relative directory names.
- */
-int vim_chdir(char_u *new_dir)
-{
- char_u *dir_name;
- int r;
-
- dir_name = find_directory_in_path(new_dir, STRLEN(new_dir),
- FNAME_MESS, curbuf->b_ffname);
- if (dir_name == NULL)
- return -1;
- r = os_chdir((char *)dir_name);
- xfree(dir_name);
- return r;
-}
-
-/*
- * Read 2 bytes from "fd" and turn them into an int, MSB first.
- */
-int get2c(FILE *fd)
-{
- int n;
-
- n = getc(fd);
- n = (n << 8) + getc(fd);
- return n;
-}
-
-/*
- * Read 3 bytes from "fd" and turn them into an int, MSB first.
- */
-int get3c(FILE *fd)
-{
- int n;
-
- n = getc(fd);
- n = (n << 8) + getc(fd);
- n = (n << 8) + getc(fd);
- return n;
-}
-
-/*
- * Read 4 bytes from "fd" and turn them into an int, MSB first.
- */
-int get4c(FILE *fd)
-{
- /* Use unsigned rather than int otherwise result is undefined
- * when left-shift sets the MSB. */
- unsigned n;
-
- n = (unsigned)getc(fd);
- n = (n << 8) + (unsigned)getc(fd);
- n = (n << 8) + (unsigned)getc(fd);
- n = (n << 8) + (unsigned)getc(fd);
- return (int)n;
-}
-
-/*
- * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
- */
-time_t get8ctime(FILE *fd)
-{
- time_t n = 0;
- int i;
-
- for (i = 0; i < 8; ++i)
- n = (n << 8) + getc(fd);
- return n;
-}
-
-/// Reads a string of length "cnt" from "fd" into allocated memory.
-/// @return pointer to the string or NULL when unable to read that many bytes.
-char *read_string(FILE *fd, size_t cnt)
-{
- uint8_t *str = xmallocz(cnt);
- for (size_t i = 0; i < cnt; i++) {
- int c = getc(fd);
- if (c == EOF) {
- xfree(str);
- return NULL;
- }
- str[i] = (uint8_t)c;
- }
- return (char *)str;
-}
-
-/// Writes a number to file "fd", most significant bit first, in "len" bytes.
-/// @returns false in case of an error.
-bool put_bytes(FILE *fd, uintmax_t number, size_t len)
-{
- assert(len > 0);
- for (size_t i = len - 1; i < len; i--) {
- if (putc((int)(number >> (i * 8)), fd) == EOF) {
- return false;
- }
- }
- return true;
-}
-
-/// Writes time_t to file "fd" in 8 bytes.
-/// @returns FAIL when the write failed.
-int put_time(FILE *fd, time_t time_)
-{
- uint8_t buf[8];
- time_to_bytes(time_, buf);
- return fwrite(buf, sizeof(uint8_t), ARRAY_SIZE(buf), fd) == 1 ? OK : FAIL;
-}
-
-/// Writes time_t to "buf[8]".
-void time_to_bytes(time_t time_, uint8_t buf[8])
-{
- // time_t can be up to 8 bytes in size, more than uintmax_t in 32 bits
- // systems, thus we can't use put_bytes() here.
- for (size_t i = 7, bufi = 0; bufi < 8; i--, bufi++) {
- buf[bufi] = (uint8_t)((uint64_t)time_ >> (i * 8));
- }
-}
diff --git a/src/nvim/misc2.h b/src/nvim/misc2.h
deleted file mode 100644
index 28b5b775d2..0000000000
--- a/src/nvim/misc2.h
+++ /dev/null
@@ -1,12 +0,0 @@
-#ifndef NVIM_MISC2_H
-#define NVIM_MISC2_H
-
-#include "nvim/os/shell.h"
-
-#define READ_STRING(x, y) (char_u *)read_string((x), (size_t)(y))
-
-#ifdef INCLUDE_GENERATED_DECLARATIONS
-# include "misc2.h.generated.h"
-#endif
-
-#endif // NVIM_MISC2_H
diff --git a/src/nvim/mouse.c b/src/nvim/mouse.c
index 5efac2623c..2ebe199f47 100644
--- a/src/nvim/mouse.c
+++ b/src/nvim/mouse.c
@@ -4,6 +4,7 @@
#include "nvim/vim.h"
#include "nvim/ascii.h"
#include "nvim/window.h"
+#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/screen.h"
#include "nvim/syntax.h"
diff --git a/src/nvim/move.c b/src/nvim/move.c
index b129c5cb7a..4c814f3ae0 100644
--- a/src/nvim/move.c
+++ b/src/nvim/move.c
@@ -24,7 +24,6 @@
#include "nvim/mbyte.h"
#include "nvim/memline.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/popupmnu.h"
#include "nvim/screen.h"
#include "nvim/strings.h"
diff --git a/src/nvim/normal.c b/src/nvim/normal.c
index 4dec51ce83..68ba9e399f 100644
--- a/src/nvim/normal.c
+++ b/src/nvim/normal.c
@@ -36,7 +36,6 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/keymap.h"
#include "nvim/move.h"
#include "nvim/mouse.h"
diff --git a/src/nvim/ops.c b/src/nvim/ops.c
index 0aae7f5c46..388a72adce 100644
--- a/src/nvim/ops.c
+++ b/src/nvim/ops.c
@@ -30,13 +30,13 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/move.h"
#include "nvim/normal.h"
#include "nvim/option.h"
#include "nvim/path.h"
#include "nvim/screen.h"
#include "nvim/search.h"
+#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/terminal.h"
#include "nvim/ui.h"
diff --git a/src/nvim/option.c b/src/nvim/option.c
index 1ef1c300b9..d8908cca90 100644
--- a/src/nvim/option.c
+++ b/src/nvim/option.c
@@ -6712,3 +6712,148 @@ unsigned int get_bkc_value(buf_T *buf)
{
return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
}
+
+/// Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
+int get_fileformat(buf_T *buf)
+{
+ int c = *buf->b_p_ff;
+
+ if (buf->b_p_bin || c == 'u') {
+ return EOL_UNIX;
+ }
+ if (c == 'm') {
+ return EOL_MAC;
+ }
+ return EOL_DOS;
+}
+
+/// Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
+/// argument.
+///
+/// @param eap can be NULL!
+int get_fileformat_force(buf_T *buf, exarg_T *eap)
+{
+ int c;
+
+ if (eap != NULL && eap->force_ff != 0) {
+ c = eap->cmd[eap->force_ff];
+ } else {
+ if ((eap != NULL && eap->force_bin != 0)
+ ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin) {
+ return EOL_UNIX;
+ }
+ c = *buf->b_p_ff;
+ }
+ if (c == 'u') {
+ return EOL_UNIX;
+ }
+ if (c == 'm') {
+ return EOL_MAC;
+ }
+ return EOL_DOS;
+}
+
+/// Return the default fileformat from 'fileformats'.
+int default_fileformat(void)
+{
+ switch (*p_ffs) {
+ case 'm': return EOL_MAC;
+ case 'd': return EOL_DOS;
+ }
+ return EOL_UNIX;
+}
+
+/// Set the current end-of-line type to EOL_UNIX, EOL_MAC, or EOL_DOS.
+///
+/// Sets 'fileformat'.
+///
+/// @param eol_style End-of-line style.
+/// @param opt_flags OPT_LOCAL and/or OPT_GLOBAL
+void set_fileformat(int eol_style, int opt_flags)
+{
+ char *p = NULL;
+
+ switch (eol_style) {
+ case EOL_UNIX:
+ p = FF_UNIX;
+ break;
+ case EOL_MAC:
+ p = FF_MAC;
+ break;
+ case EOL_DOS:
+ p = FF_DOS;
+ break;
+ }
+
+ // p is NULL if "eol_style" is EOL_UNKNOWN.
+ if (p != NULL) {
+ set_string_option_direct((char_u *)"ff",
+ -1,
+ (char_u *)p,
+ OPT_FREE | opt_flags,
+ 0);
+ }
+
+ // This may cause the buffer to become (un)modified.
+ check_status(curbuf);
+ redraw_tabline = true;
+ need_maketitle = true; // Set window title later.
+}
+
+/// Skip to next part of an option argument: Skip space and comma.
+char_u *skip_to_option_part(char_u *p)
+{
+ if (*p == ',') {
+ p++;
+ }
+ while (*p == ' ') {
+ p++;
+ }
+ return p;
+}
+
+/// Isolate one part of a string option separated by `sep_chars`.
+///
+/// @param[in,out] option advanced to the next part
+/// @param[in,out] buf copy of the isolated part
+/// @param[in] maxlen length of `buf`
+/// @param[in] sep_chars chars that separate the option parts
+///
+/// @return length of `*option`
+size_t copy_option_part(char_u **option, char_u *buf, size_t maxlen,
+ char *sep_chars)
+{
+ size_t len = 0;
+ char_u *p = *option;
+
+ // skip '.' at start of option part, for 'suffixes'
+ if (*p == '.') {
+ buf[len++] = *p++;
+ }
+ while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL) {
+ // Skip backslash before a separator character and space.
+ if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL) {
+ p++;
+ }
+ if (len < maxlen - 1) {
+ buf[len++] = *p;
+ }
+ p++;
+ }
+ buf[len] = NUL;
+
+ if (*p != NUL && *p != ',') { // skip non-standard separator
+ p++;
+ }
+ p = skip_to_option_part(p); // p points to next file name
+
+ *option = p;
+ return len;
+}
+
+/// Return TRUE when 'shell' has "csh" in the tail.
+int csh_like_shell(void)
+{
+ return strstr((char *)path_tail(p_sh), "csh") != NULL;
+}
+
diff --git a/src/nvim/option.h b/src/nvim/option.h
index 3a43b859a8..cf167cdd2c 100644
--- a/src/nvim/option.h
+++ b/src/nvim/option.h
@@ -1,6 +1,8 @@
#ifndef NVIM_OPTION_H
#define NVIM_OPTION_H
+#include "nvim/ex_cmds_defs.h" // for exarg_T
+
/* flags for buf_copy_options() */
#define BCO_ENTER 1 /* going to enter the buffer */
#define BCO_ALWAYS 2 /* always copy the options */
diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c
index edc430410c..83a0262545 100644
--- a/src/nvim/os/env.c
+++ b/src/nvim/os/env.c
@@ -12,7 +12,6 @@
#include "nvim/os/os.h"
#include "nvim/memory.h"
#include "nvim/message.h"
-#include "nvim/misc2.h"
#include "nvim/path.h"
#include "nvim/strings.h"
#include "nvim/eval.h"
diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c
index e63b8b6793..008952fa97 100644
--- a/src/nvim/os/fs.c
+++ b/src/nvim/os/fs.c
@@ -21,7 +21,6 @@
#include "nvim/message.h"
#include "nvim/assert.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/path.h"
#include "nvim/strings.h"
diff --git a/src/nvim/os/input.c b/src/nvim/os/input.c
index c0c73364c0..6873e32f34 100644
--- a/src/nvim/os/input.c
+++ b/src/nvim/os/input.c
@@ -19,7 +19,7 @@
#include "nvim/getchar.h"
#include "nvim/main.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
+#include "nvim/state.h"
#define READ_BUFFER_SIZE 0xfff
#define INPUT_BUFFER_SIZE (READ_BUFFER_SIZE * 4)
diff --git a/src/nvim/os/shell.c b/src/nvim/os/shell.c
index 99ae3af221..661b12accc 100644
--- a/src/nvim/os/shell.c
+++ b/src/nvim/os/shell.c
@@ -19,7 +19,6 @@
#include "nvim/message.h"
#include "nvim/memory.h"
#include "nvim/ui.h"
-#include "nvim/misc2.h"
#include "nvim/screen.h"
#include "nvim/memline.h"
#include "nvim/option_defs.h"
diff --git a/src/nvim/os/signal.c b/src/nvim/os/signal.c
index 411a9658bf..1ac6d3f5e1 100644
--- a/src/nvim/os/signal.c
+++ b/src/nvim/os/signal.c
@@ -14,7 +14,6 @@
#include "nvim/main.h"
#include "nvim/memory.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/event/signal.h"
#include "nvim/os/signal.h"
#include "nvim/event/loop.h"
diff --git a/src/nvim/os/users.c b/src/nvim/os/users.c
index 8ebb7562ef..1c94ef0067 100644
--- a/src/nvim/os/users.c
+++ b/src/nvim/os/users.c
@@ -6,7 +6,6 @@
#include "nvim/os/os.h"
#include "nvim/garray.h"
#include "nvim/memory.h"
-#include "nvim/misc2.h"
#include "nvim/strings.h"
#ifdef HAVE_PWD_H
# include <pwd.h>
diff --git a/src/nvim/os_unix.c b/src/nvim/os_unix.c
index def7e3b0e5..08294fa6a0 100644
--- a/src/nvim/os_unix.c
+++ b/src/nvim/os_unix.c
@@ -27,7 +27,6 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/mouse.h"
#include "nvim/garray.h"
#include "nvim/path.h"
diff --git a/src/nvim/path.c b/src/nvim/path.c
index a2617973b1..a79b7139f1 100644
--- a/src/nvim/path.c
+++ b/src/nvim/path.c
@@ -18,7 +18,6 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/option.h"
#include "nvim/os/os.h"
#include "nvim/os/shell.h"
diff --git a/src/nvim/popupmnu.c b/src/nvim/popupmnu.c
index 328e913e0e..947814de4f 100644
--- a/src/nvim/popupmnu.c
+++ b/src/nvim/popupmnu.c
@@ -13,7 +13,6 @@
#include "nvim/charset.h"
#include "nvim/ex_cmds.h"
#include "nvim/memline.h"
-#include "nvim/misc2.h"
#include "nvim/move.h"
#include "nvim/option.h"
#include "nvim/screen.h"
diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c
index 2a3cdfa790..a7aff15121 100644
--- a/src/nvim/quickfix.c
+++ b/src/nvim/quickfix.c
@@ -27,7 +27,6 @@
#include "nvim/memline.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/memory.h"
#include "nvim/move.h"
#include "nvim/normal.h"
diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c
index f8fd7d4ef8..64a70c295a 100644
--- a/src/nvim/regexp.c
+++ b/src/nvim/regexp.c
@@ -59,7 +59,6 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/garray.h"
#include "nvim/strings.h"
diff --git a/src/nvim/regexp_nfa.c b/src/nvim/regexp_nfa.c
index 1a8de28feb..35308b7411 100644
--- a/src/nvim/regexp_nfa.c
+++ b/src/nvim/regexp_nfa.c
@@ -11,7 +11,6 @@
#include <limits.h>
#include "nvim/ascii.h"
-#include "nvim/misc2.h"
#include "nvim/garray.h"
/*
diff --git a/src/nvim/screen.c b/src/nvim/screen.c
index bb89e49e5f..9a2eeda8b2 100644
--- a/src/nvim/screen.c
+++ b/src/nvim/screen.c
@@ -108,7 +108,6 @@
#include "nvim/menu.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/garray.h"
#include "nvim/move.h"
#include "nvim/normal.h"
@@ -120,6 +119,7 @@
#include "nvim/regexp.h"
#include "nvim/search.h"
#include "nvim/spell.h"
+#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/terminal.h"
diff --git a/src/nvim/search.c b/src/nvim/search.c
index 6e2b69fff7..5f4df3be92 100644
--- a/src/nvim/search.c
+++ b/src/nvim/search.c
@@ -31,7 +31,6 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/move.h"
#include "nvim/mouse.h"
#include "nvim/normal.h"
diff --git a/src/nvim/shada.c b/src/nvim/shada.c
index d2d0d503b9..01c0807d82 100644
--- a/src/nvim/shada.c
+++ b/src/nvim/shada.c
@@ -27,7 +27,6 @@
#include "nvim/api/private/helpers.h"
#include "nvim/buffer.h"
#include "nvim/buffer_defs.h"
-#include "nvim/misc2.h"
#include "nvim/ex_getln.h"
#include "nvim/search.h"
#include "nvim/regexp.h"
diff --git a/src/nvim/spell.c b/src/nvim/spell.c
index 610fb660e7..ba7f31be25 100644
--- a/src/nvim/spell.c
+++ b/src/nvim/spell.c
@@ -312,7 +312,6 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/garray.h"
#include "nvim/normal.h"
#include "nvim/option.h"
diff --git a/src/nvim/state.c b/src/nvim/state.c
index 30133e2201..f792ec00a4 100644
--- a/src/nvim/state.c
+++ b/src/nvim/state.c
@@ -2,10 +2,12 @@
#include "nvim/lib/kvec.h"
+#include "nvim/ascii.h"
#include "nvim/state.h"
#include "nvim/vim.h"
#include "nvim/main.h"
#include "nvim/getchar.h"
+#include "nvim/option_defs.h"
#include "nvim/ui.h"
#include "nvim/os/input.h"
@@ -61,3 +63,35 @@ getkey:
}
}
}
+
+/// Return TRUE if in the current mode we need to use virtual.
+int virtual_active(void)
+{
+ // While an operator is being executed we return "virtual_op", because
+ // VIsual_active has already been reset, thus we can't check for "block"
+ // being used.
+ if (virtual_op != MAYBE) {
+ return virtual_op;
+ }
+ return ve_flags == VE_ALL
+ || ((ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V)
+ || ((ve_flags & VE_INSERT) && (State & INSERT));
+}
+
+/// VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
+/// NORMAL State with a condition. This function returns the real State.
+int get_real_state(void)
+{
+ if (State & NORMAL) {
+ if (VIsual_active) {
+ if (VIsual_select) {
+ return SELECTMODE;
+ }
+ return VISUAL;
+ } else if (finish_op) {
+ return OP_PENDING;
+ }
+ }
+ return State;
+}
+
diff --git a/src/nvim/strings.c b/src/nvim/strings.c
index 37a0fb82da..c1800a0639 100644
--- a/src/nvim/strings.c
+++ b/src/nvim/strings.c
@@ -5,7 +5,6 @@
#include "nvim/vim.h"
#include "nvim/ascii.h"
#include "nvim/strings.h"
-#include "nvim/misc2.h"
#include "nvim/file_search.h"
#include "nvim/buffer.h"
#include "nvim/charset.h"
diff --git a/src/nvim/syntax.c b/src/nvim/syntax.c
index f7a8e4f8b9..bec2b5c5b0 100644
--- a/src/nvim/syntax.c
+++ b/src/nvim/syntax.c
@@ -25,7 +25,6 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/keymap.h"
#include "nvim/garray.h"
#include "nvim/option.h"
diff --git a/src/nvim/tag.c b/src/nvim/tag.c
index dfecfb776d..81256b4f01 100644
--- a/src/nvim/tag.c
+++ b/src/nvim/tag.c
@@ -26,7 +26,6 @@
#include "nvim/mbyte.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/file_search.h"
#include "nvim/garray.h"
#include "nvim/memory.h"
diff --git a/src/nvim/tui/input.c b/src/nvim/tui/input.c
index 44521c5ae6..68ea00ee63 100644
--- a/src/nvim/tui/input.c
+++ b/src/nvim/tui/input.c
@@ -5,7 +5,6 @@
#include "nvim/api/private/helpers.h"
#include "nvim/ascii.h"
#include "nvim/main.h"
-#include "nvim/misc2.h"
#include "nvim/os/os.h"
#include "nvim/os/input.h"
#include "nvim/event/rstream.h"
diff --git a/src/nvim/ui.c b/src/nvim/ui.c
index 306dd6c43a..dd278893c2 100644
--- a/src/nvim/ui.c
+++ b/src/nvim/ui.c
@@ -12,10 +12,8 @@
#include "nvim/ex_cmds2.h"
#include "nvim/fold.h"
#include "nvim/main.h"
-#include "nvim/mbyte.h"
#include "nvim/ascii.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/mbyte.h"
#include "nvim/garray.h"
#include "nvim/memory.h"
diff --git a/src/nvim/undo.c b/src/nvim/undo.c
index fc5d6acaa4..d80aaf443a 100644
--- a/src/nvim/undo.c
+++ b/src/nvim/undo.c
@@ -99,6 +99,7 @@
#include "nvim/quickfix.h"
#include "nvim/screen.h"
#include "nvim/sha256.h"
+#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/types.h"
#include "nvim/os/os.h"
diff --git a/src/nvim/version.c b/src/nvim/version.c
index 403d1fa4f9..bc826d8660 100644
--- a/src/nvim/version.c
+++ b/src/nvim/version.c
@@ -15,7 +15,6 @@
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
-#include "nvim/misc2.h"
#include "nvim/screen.h"
#include "nvim/strings.h"
diff --git a/src/nvim/window.c b/src/nvim/window.c
index 386867a08f..e9a66ad907 100644
--- a/src/nvim/window.c
+++ b/src/nvim/window.c
@@ -28,7 +28,6 @@
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
-#include "nvim/misc2.h"
#include "nvim/file_search.h"
#include "nvim/garray.h"
#include "nvim/move.h"
@@ -41,6 +40,7 @@
#include "nvim/regexp.h"
#include "nvim/screen.h"
#include "nvim/search.h"
+#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/terminal.h"