pzoxicuv

Text encloser

Nov 14th, 2011
68
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* When enabled, plugin wraps selected text in single/double quotes, parentheses,
  2.  * or square brackets.
  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. gint num_selected = 0;
  24.  
  25. void plugin_init(GeanyData *data)
  26. {  
  27.     plugin_signal_connect (geany_plugin, NULL, "editor_notify", FALSE, (GCallback)
  28.         &on_editor_notify, NULL);
  29.        
  30. }
  31.  
  32. /* Keeps track of current selection length.  If a character is typed and the selection
  33.  * length was > 0 and the character was a quote, bracket, or parenthesis, surround the
  34.  * selected text with the appropriate characters: Undo the character insert which replaced
  35.  * the selected text, insert the enclosing characters before and after selected text
  36.  * and move cursor to after second enclosing character.
  37.  */
  38.  
  39. gboolean on_editor_notify (GObject *object, GeanyEditor *editor, SCNotification *nt, gpointer data)
  40. {  
  41.     gchar prior_char [2];   /* Zero-terminated arrays for enclosing chars */
  42.     gchar end_char [2];
  43.     prior_char [1] = 0;
  44.     end_char [1] = 0;  
  45.                            
  46.     if (nt->nmhdr.code == SCN_UPDATEUI && nt->updated == SC_UPDATE_SELECTION)
  47.         num_selected = sci_get_selected_text_length (editor->sci);
  48.        
  49.     else if (nt->nmhdr.code == SCN_CHARADDED)
  50.     {
  51.         if ((nt->ch == 0x27 || nt->ch == 0x22 || nt->ch == 0x28 || nt->ch == 0x5B) && num_selected > 1)
  52.         {
  53.             prior_char [0] = nt->ch;
  54.             end_char [0] = nt->ch;
  55.             if (nt->ch == 0x28)
  56.                 end_char [0] = 0x29;
  57.             else if (nt->ch == 0x5B)
  58.                 end_char [0] = 0x5D;
  59.            
  60.             sci_send_command (editor->sci, SCI_UNDO);
  61.             sci_insert_text (editor->sci, sci_get_current_position (editor->sci) - num_selected + 1,
  62.                 prior_char);
  63.             sci_insert_text (editor->sci, sci_get_current_position (editor->sci), end_char);
  64.             sci_set_current_position (editor->sci, sci_get_current_position (editor->sci) + 1, TRUE);
  65.            
  66.             num_selected = 0;
  67.             return TRUE;
  68.         }
  69.         num_selected = 0;
  70.     }
  71.    
  72.     else if (nt->nmhdr.code == SCN_MODIFIED && (nt->modificationType & SC_MOD_DELETETEXT) ==
  73.         SC_MOD_DELETETEXT) {
  74.         num_selected = 0;
  75.     }
  76.    
  77.     return FALSE;
  78. }
  79.  
  80. void plugin_cleanup(void)
  81. {
  82.  
  83. }
RAW Paste Data