comparison finch/libgnt/gnttextview.c @ 18539:1148da95ddeb

Allow changing the content of a textview using $EDITOR.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Sun, 15 Jul 2007 04:57:03 +0000
parents 09db6fec9dce
children fd01bac27b79
comparison
equal deleted inserted replaced
18538:2e11d73c4ff1 18539:1148da95ddeb
794 void gnt_text_view_set_flag(GntTextView *view, GntTextViewFlag flag) 794 void gnt_text_view_set_flag(GntTextView *view, GntTextViewFlag flag)
795 { 795 {
796 view->flags |= flag; 796 view->flags |= flag;
797 } 797 }
798 798
799 /* Pager and editor setups */
800 struct
801 {
802 GntTextView *tv;
803 char *file;
804 } pageditor;
805
806
807 static void
808 cleanup_pageditor()
809 {
810 unlink(pageditor.file);
811 g_free(pageditor.file);
812
813 pageditor.file = NULL;
814 pageditor.tv = NULL;
815 }
816
817 static void
818 editor_end_cb(int status, gpointer data)
819 {
820 if (status == 0) {
821 char *text = NULL;
822 if (g_file_get_contents(pageditor.file, &text, NULL, NULL)) {
823 gnt_text_view_clear(pageditor.tv);
824 gnt_text_view_append_text_with_flags(pageditor.tv, text, GNT_TEXT_FLAG_NORMAL);
825 gnt_text_view_scroll(GNT_TEXT_VIEW(pageditor.tv), 0);
826 g_free(text);
827 }
828 }
829 cleanup_pageditor();
830 }
831
799 static void 832 static void
800 pager_end_cb(int status, gpointer data) 833 pager_end_cb(int status, gpointer data)
801 { 834 {
802 unlink(data); 835 cleanup_pageditor();
803 g_free(data);
804 } 836 }
805 837
806 static gboolean 838 static gboolean
807 check_for_pager_cb(GntWidget *widget, const char *key, GntTextView *view) 839 check_for_ext_cb(GntWidget *widget, const char *key, GntTextView *view)
808 { 840 {
809 static const char *combin = NULL; 841 static const char *pager = NULL;
842 static const char *editor = NULL;
810 char *argv[] = {NULL, NULL, NULL}; 843 char *argv[] = {NULL, NULL, NULL};
811 static char path[1024]; 844 static char path[1024];
812 static int len = -1; 845 static int len = -1;
813 FILE *file; 846 FILE *file;
814 gboolean ret; 847 gboolean ret;
815 848 gboolean pg;
816 if (combin == NULL) { 849
817 combin = gnt_key_translate(gnt_style_get_from_name("pager", "key")); 850 if (pager == NULL) {
818 if (combin == NULL) 851 pager = gnt_key_translate(gnt_style_get_from_name("pager", "key"));
819 combin = "\033" "v"; 852 if (pager == NULL)
853 pager = "\033" "v";
854 editor = gnt_key_translate(gnt_style_get_from_name("editor", "key"));
855 if (editor == NULL)
856 editor = "\033" "e";
820 len = g_snprintf(path, sizeof(path), "%s" G_DIR_SEPARATOR_S "gnt", g_get_tmp_dir()); 857 len = g_snprintf(path, sizeof(path), "%s" G_DIR_SEPARATOR_S "gnt", g_get_tmp_dir());
821 } else { 858 } else {
822 g_snprintf(path + len, sizeof(path) - len, "XXXXXX"); 859 g_snprintf(path + len, sizeof(path) - len, "XXXXXX");
823 } 860 }
824 861
825 if (strcmp(key, combin)) { 862 if (strcmp(key, pager) == 0) {
863 if (g_object_get_data(G_OBJECT(widget), "pager-for") != view)
864 return FALSE;
865 pg = TRUE;
866 } else if (strcmp(key, editor) == 0) {
867 if (g_object_get_data(G_OBJECT(widget), "editor-for") != view)
868 return FALSE;
869 pg = FALSE;
870 } else {
826 return FALSE; 871 return FALSE;
827 } 872 }
828 873
829 file = fdopen(g_mkstemp(path), "wb"); 874 file = fdopen(g_mkstemp(path), "wb");
830 if (!file) 875 if (!file)
831 return FALSE; 876 return FALSE;
832
833 fprintf(file, "%s", view->string->str); 877 fprintf(file, "%s", view->string->str);
834 fclose(file); 878 fclose(file);
835 argv[0] = gnt_style_get_from_name("pager", "path"); 879
836 argv[0] = argv[0] ? argv[0] : getenv("PAGER"); 880 pageditor.tv = view;
837 argv[0] = argv[0] ? argv[0] : "less"; 881 pageditor.file = g_strdup(path);
838 argv[1] = g_strdup(path); 882
839 ret = gnt_giveup_console(NULL, argv, NULL, NULL, NULL, NULL, pager_end_cb, argv[1]); 883 argv[0] = gnt_style_get_from_name(pg ? "pager" : "editor", "path");
840 if (!ret) 884 argv[0] = argv[0] ? argv[0] : getenv(pg ? "PAGER" : "EDITOR");
841 g_free(argv[1]); 885 argv[0] = argv[0] ? argv[0] : (pg ? "less" : "vim");
886 argv[1] = path;
887 ret = gnt_giveup_console(NULL, argv, NULL, NULL, NULL, NULL, pg ? pager_end_cb : editor_end_cb, NULL);
842 return ret; 888 return ret;
843 } 889 }
844 890
845 void gnt_text_view_attach_pager_widget(GntTextView *view, GntWidget *pager) 891 void gnt_text_view_attach_pager_widget(GntTextView *view, GntWidget *pager)
846 { 892 {
847 g_signal_connect(pager, "key_pressed", G_CALLBACK(check_for_pager_cb), view); 893 g_signal_connect(pager, "key_pressed", G_CALLBACK(check_for_ext_cb), view);
848 } 894 g_object_set_data(G_OBJECT(pager), "pager-for", view);
849 895 }
896
897 void gnt_text_view_attach_editor_widget(GntTextView *view, GntWidget *wid)
898 {
899 g_signal_connect(wid, "key_pressed", G_CALLBACK(check_for_ext_cb), view);
900 g_object_set_data(G_OBJECT(wid), "editor-for", view);
901 }
902