Change mch_errmsg and mch_msg from macro to function

This commit is contained in:
erw7
2019-06-02 09:41:10 +09:00
parent 53551d823e
commit 6fad1736fb
2 changed files with 56 additions and 61 deletions

View File

@@ -2554,7 +2554,7 @@ static int do_more_prompt(int typed_char)
return retval; return retval;
} }
#if defined(USE_MCH_ERRMSG) #if defined(USE_MCH_ERRMSG) || defined(WIN32)
# ifdef mch_errmsg # ifdef mch_errmsg
# undef mch_errmsg # undef mch_errmsg
@@ -2563,18 +2563,17 @@ static int do_more_prompt(int typed_char)
# undef mch_msg # undef mch_msg
# endif # endif
/* # if defined(USE_MCH_ERRMSG)
* Give an error message. To be used when the screen hasn't been initialized // Give an error message. To be used when the screen hasn't been initialized
* yet. When stderr can't be used, collect error messages until the GUI has // yet. When stderr can't be used, collect error messages until the GUI has
* started and they can be displayed in a message box. // started and they can be displayed in a message box.
*/
void mch_errmsg(const char *const str) void mch_errmsg(const char *const str)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
# ifdef UNIX # ifdef UNIX
/* On Unix use stderr if it's a tty. // On Unix use stderr if it's a tty.
* When not going to start the GUI also use stderr. // When not going to start the GUI also use stderr.
* On Mac, when started from Finder, stderr is the console. */ // On Mac, when started from Finder, stderr is the console.
if (os_isatty(2)) { if (os_isatty(2)) {
fprintf(stderr, "%s", str); fprintf(stderr, "%s", str);
return; return;
@@ -2592,7 +2591,7 @@ void mch_errmsg(const char *const str)
ga_grow(&error_ga, len); ga_grow(&error_ga, len);
memmove(error_ga.ga_data + error_ga.ga_len, str, len); memmove(error_ga.ga_data + error_ga.ga_len, str, len);
# ifdef UNIX # ifdef UNIX
/* remove CR characters, they are displayed */ // remove CR characters, they are displayed/
{ {
char_u *p; char_u *p;
@@ -2605,22 +2604,20 @@ void mch_errmsg(const char *const str)
} }
} }
# endif # endif
--len; /* don't count the NUL at the end */ len--; // don't count the NUL at the end
error_ga.ga_len += len; error_ga.ga_len += len;
} }
/* // Give a message. To be used when the screen hasn't been initialized yet.
* Give a message. To be used when the screen hasn't been initialized yet. // When there is no tty, collect messages until the GUI has started and they
* When there is no tty, collect messages until the GUI has started and they // can be displayed in a message box.
* can be displayed in a message box.
*/
void mch_msg(char *str) void mch_msg(char *str)
{ {
# ifdef UNIX # ifdef UNIX
/* On Unix use stdout if we have a tty. This allows "vim -h | more" and // On Unix use stdout if we have a tty. This allows "vim -h | more" and
* uses mch_errmsg() when started from the desktop. // uses mch_errmsg() when started from the desktop.
* When not going to start the GUI also use stdout. // When not going to start the GUI also use stdout.
* On Mac, when started from Finder, stderr is the console. */ // On Mac, when started from Finder, stderr is the console.
if (os_isatty(2)) { if (os_isatty(2)) {
printf("%s", str); printf("%s", str);
return; return;
@@ -2628,7 +2625,32 @@ void mch_msg(char *str)
# endif # endif
mch_errmsg(str); mch_errmsg(str);
} }
#endif /* USE_MCH_ERRMSG */ # else
void mch_errmsg(char *str)
{
wchar_t *utf16str;
int conversion_result = utf8_to_utf16((str), &utf16str);
if (conversion_result != 0) {
EMSG2("utf8_to_utf16 failed: %d", conversion_result);
} else {
fwprintf(stderr, L"%ls", utf16str);
xfree(utf16str);
}
}
void mch_msg(char *str)
{
wchar_t *utf16str;
int conversion_result = utf8_to_utf16((str), &utf16str);
if (conversion_result != 0) {
EMSG2("utf8_to_utf16 failed: %d", conversion_result);
} else {
wprintf(L"%ls", utf16str);
xfree(utf16str);
}
}
# endif // USE_MCH_ERRMSG
#endif // USE_MCH_ERRMSG && WIN32
/* /*
* Put a character on the screen at the current message position and advance * Put a character on the screen at the current message position and advance

View File

@@ -35,10 +35,6 @@ enum { NUMBUFLEN = 65 };
#include "nvim/gettext.h" #include "nvim/gettext.h"
#ifdef WIN32
# include "nvim/mbyte.h" // for utf8_to_utf16
#endif
// special attribute addition: Put message in history // special attribute addition: Put message in history
#define MSG_HIST 0x1000 #define MSG_HIST 0x1000
@@ -291,30 +287,7 @@ enum { FOLD_TEXT_LEN = 51 }; //!< buffer size for get_foldtext()
// functions of these names. The declarations would break if the defines had // functions of these names. The declarations would break if the defines had
// been seen at that stage. But it must be before globals.h, where error_ga // been seen at that stage. But it must be before globals.h, where error_ga
// is declared. // is declared.
#ifdef WIN32 #ifndef WIN32
# define mch_errmsg(str) \
do { \
wchar_t *utf16str; \
int conversion_result = utf8_to_utf16((str), &utf16str); \
if (conversion_result != 0) { \
EMSG2("utf8_to_utf16 failed: %d", conversion_result); \
} else { \
fwprintf(stderr, L"%ls", utf16str); \
xfree(utf16str); \
} \
} while (0)
# define mch_msg(str) \
do { \
wchar_t *utf16str; \
int conversion_result = utf8_to_utf16((str), &utf16str); \
if (conversion_result != 0) { \
EMSG2("utf8_to_utf16 failed: %d", conversion_result); \
} else { \
wprintf(L"%ls", utf16str); \
xfree(utf16str); \
} \
} while (0)
#else
# define mch_errmsg(str) fprintf(stderr, "%s", (str)) # define mch_errmsg(str) fprintf(stderr, "%s", (str))
# define mch_msg(str) printf("%s", (str)) # define mch_msg(str) printf("%s", (str))
#endif #endif