Skip to content

Commit 411ca91

Browse files
author
Charlie Gordon
committed
Add describe-buffer
* add do_describe_buffer to enumerate all buffer attributes in a popup * analyze buffer contents and show map of bytes present in buffer * this eases debugging when non ASCII bytes are hard to see
1 parent 6d75431 commit 411ca91

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

extras.c

+138
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,142 @@ static void do_set_eol_type(EditState *s, int eol_type)
717717
eb_set_charset(s->b, s->b->charset, eol_type);
718718
}
719719

720+
static void do_describe_buffer(EditState *s, int argval)
721+
{
722+
char buf[256];
723+
buf_t descbuf, *desc;
724+
EditBuffer *b = s->b;
725+
EditBuffer *b1;
726+
int show;
727+
int total_size;
728+
729+
b1 = new_help_buffer(&show);
730+
if (!b1)
731+
return;
732+
733+
total_size = s->b->total_size;
734+
735+
eb_printf(b1, "Buffer Statistics\n\n");
736+
737+
eb_printf(b1, " name: %s\n", b->name);
738+
eb_printf(b1, " filename: %s\n", b->filename);
739+
eb_printf(b1, " modified: %d\n", b->modified);
740+
eb_printf(b1, " total_size: %d\n", total_size);
741+
if (total_size > 0) {
742+
int nb_chars, line, col;
743+
744+
eb_get_pos(b, &line, &col, total_size);
745+
nb_chars = eb_get_char_offset(b, total_size);
746+
747+
eb_printf(b1, " lines: %d\n", line);
748+
eb_printf(b1, " chars: %d\n", nb_chars);
749+
}
750+
eb_printf(b1, " mark: %d\n", b->mark);
751+
eb_printf(b1, " offset: %d\n", b->offset);
752+
753+
eb_printf(b1, " tab_width: %d\n", b->tab_width);
754+
eb_printf(b1, " fill_column: %d\n", b->fill_column);
755+
756+
desc = buf_init(&descbuf, buf, countof(buf));
757+
if (b->eol_type == EOL_UNIX)
758+
buf_printf(desc, " unix");
759+
if (b->eol_type == EOL_DOS)
760+
buf_printf(desc, " dos");
761+
if (b->eol_type == EOL_MAC)
762+
buf_printf(desc, " mac");
763+
764+
eb_printf(b1, " eol_type: %d %s\n", b->eol_type, buf);
765+
eb_printf(b1, " charset: %s (bytes=%d, shift=%d)\n",
766+
b->charset->name, b->char_bytes, b->char_shift);
767+
768+
desc = buf_init(&descbuf, buf, countof(buf));
769+
if (b->flags & BF_SAVELOG)
770+
buf_printf(desc, " SAVELOG");
771+
if (b->flags & BF_SYSTEM)
772+
buf_printf(desc, " SYSTEM");
773+
if (b->flags & BF_READONLY)
774+
buf_printf(desc, " READONLY");
775+
if (b->flags & BF_PREVIEW)
776+
buf_printf(desc, " PREVIEW");
777+
if (b->flags & BF_LOADING)
778+
buf_printf(desc, " LOADING");
779+
if (b->flags & BF_SAVING)
780+
buf_printf(desc, " SAVING");
781+
if (b->flags & BF_DIRED)
782+
buf_printf(desc, " DIRED");
783+
if (b->flags & BF_UTF8)
784+
buf_printf(desc, " UTF8");
785+
if (b->flags & BF_RAW)
786+
buf_printf(desc, " RAW");
787+
if (b->flags & BF_TRANSIENT)
788+
buf_printf(desc, " TRANSIENT");
789+
if (b->flags & BF_STYLES)
790+
buf_printf(desc, " STYLES");
791+
792+
eb_printf(b1, " flags: 0x%02x %s\n", b->flags, buf);
793+
eb_printf(b1, " probed: %d\n", b->probed);
794+
795+
eb_printf(b1, " data_type: %s\n", b->data_type->name);
796+
eb_printf(b1, " pages: %d\n", b->nb_pages);
797+
eb_printf(b1, " file_handle: %d\n", b->file_handle);
798+
799+
eb_printf(b1, " save_log: %d (new_index=%d, current=%d, nb_logs=%d)\n",
800+
b->save_log, b->log_new_index, b->log_current, b->nb_logs);
801+
eb_printf(b1, " styles: %d (cur_style=%d, bytes=%d, shift=%d)\n",
802+
!!b->b_styles, b->cur_style, b->style_bytes, b->style_shift);
803+
804+
if (total_size > 0) {
805+
u8 buf[4096];
806+
int count[256];
807+
int offset, c, i, col;
808+
809+
memset(count, 0, sizeof(count));
810+
for (offset = 0; offset < total_size;) {
811+
int size = eb_read(b, offset, buf, countof(buf));
812+
for (i = 0; i < size; i++)
813+
count[buf[i]] += 1;
814+
offset += size;
815+
}
816+
eb_printf(b1, "\nByte stats:\n");
817+
818+
for (col = i = 0; i < 256; i++) {
819+
if (count[i] == 0)
820+
continue;
821+
switch (i) {
822+
case '\b': c = 'b'; break;
823+
case '\f': c = 'f'; break;
824+
case '\t': c = 't'; break;
825+
case '\r': c = 'r'; break;
826+
case '\n': c = 'n'; break;
827+
case '\\': c = '\\'; break;
828+
case '\'': c = '\''; break;
829+
default: c = 0; break;
830+
}
831+
if (c != 0)
832+
col += eb_printf(b1, " '\\%c'", c);
833+
else
834+
if (i >= ' ' && i < 0x7f)
835+
col += eb_printf(b1, " '%c'", i);
836+
else
837+
col += eb_printf(b1, " 0x%02x", i);
838+
839+
col += eb_printf(b1, " %-4d", count[i]);
840+
if (col >= 60) {
841+
eb_printf(b1, "\n");
842+
col = 0;
843+
}
844+
}
845+
if (col) {
846+
eb_printf(b1, "\n");
847+
}
848+
}
849+
850+
b1->flags |= BF_READONLY;
851+
if (show) {
852+
show_popup(b1);
853+
}
854+
}
855+
720856
static CmdDef extra_commands[] = {
721857
CMD2( KEY_META('='), KEY_NONE,
722858
"compare-windows", do_compare_windows, ESi, "ui" )
@@ -778,6 +914,8 @@ static CmdDef extra_commands[] = {
778914
CMD2( KEY_NONE, KEY_NONE,
779915
"set-eol-type", do_set_eol_type, ESi,
780916
"ui{EOL Type [0=Unix, 1=Dos, 2=Mac]: }")
917+
CMD2( KEY_NONE, KEY_NONE,
918+
"describe-buffer", do_describe_buffer, ESi, "ui")
781919

782920
CMD_DEF_END,
783921
};

0 commit comments

Comments
 (0)