Advertisement
pzoxicuv

Wrap Text Plugin

Nov 14th, 2011
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.05 KB | None | 0 0
  1. /* When enabled, plugin wraps selected text in single/double quotes or parentheses.
  2.  * Press ctrl+ ' , " , or ( to surround selected text.
  3.  *
  4.  * Build with:
  5.  * gcc -c surroundText.c -fPIC `pkg-config --cflags geany`
  6.  * gcc surroundText.o -o surroundText.so -shared `pkg-config --libs geany`
  7.  *
  8.  */
  9.  
  10. #include <geanyplugin.h>
  11.  
  12. GeanyPlugin *geany_plugin;
  13. GeanyData *geany_data;
  14. GeanyFunctions *geany_functions;
  15.  
  16. PLUGIN_VERSION_CHECK (147);
  17. PLUGIN_SET_INFO ("Text Encloser",
  18.     "Encloses selected text with single quotes, double quotes, or parentheses.",
  19.     "0.1", "Alex M.");
  20.  
  21. gboolean on_editor_notify(GObject *, GeanyEditor *, SCNotification *, gpointer);
  22.  
  23. void plugin_init(GeanyData *data)
  24. {  
  25.     plugin_signal_connect (geany_plugin, NULL, "editor_notify", FALSE, (GCallback)
  26.         &on_editor_notify, NULL);
  27.        
  28. }
  29.  
  30. gboolean on_editor_notify(GObject *object, GeanyEditor *editor, SCNotification *nt, gpointer data)
  31. {  
  32.     if (nt->nmhdr.code == SCN_KEY)
  33.     {
  34.         if ((nt->ch == 0x22 || nt->ch == 0x27 || nt->ch == 0x28) &&
  35.             sci_get_selected_text_length (editor->sci) > 0)
  36.         {
  37.             int end = 0;
  38.             gchar prior_char [2];   /* prior_char is the char inserted before selection,
  39.                         with a zero terminator.  end_char is the char inserted
  40.                         after the selection, also with a zero terminator. */
  41.             gchar end_char [2];
  42.             prior_char [1] = 0;    
  43.             end_char [1] = 0;
  44.             prior_char [0] = nt->ch; /* prior_char is always the character that was typed,
  45.                     end_char might be something different (eg closing brace) */                
  46.             if (nt->ch == 0x22)
  47.                 end_char [0] = 0x22;
  48.             else if (nt->ch == 0x27)
  49.                 end_char [0] = 0x27;
  50.             else
  51.                 end_char [0] = 0x29;
  52.                
  53.             end = sci_get_selection_end (editor->sci);
  54.             sci_insert_text (editor->sci, sci_get_selection_start (editor->sci),
  55.                 prior_char);
  56.             sci_insert_text (editor->sci, end+1, end_char); /* end+1 to account for the
  57.                                     newly added character*/
  58.             sci_set_current_position (editor->sci, end+2, TRUE);
  59.            
  60.             return FALSE;
  61.         }      
  62.     }
  63.    
  64.     return FALSE;
  65. }
  66.  
  67. void plugin_cleanup(void)
  68. {
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement