[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#427843: klipper support for regexp backreferences



forwarded 427843 https://bugs.kde.org/show_bug.cgi?id=94837
tags 427843 +patch
thanks

hi nacho et al,

On Thursday 07 June 2007 12:18:07 Nacho Barrientos Arias wrote:

> The following setting works for me:
>
> Pattern:
>   "^#[0-9]{5,6}."
> Action:
>   konsole -e links http://bugs.debian.org/`echo %s | sed 's/\#//g'`


as we discussed on irc, it's not quite the same thing, is much less graceful, 
introduces quoting issues etc.  so, i've thrown together a patch that adds 
this support, more "the way it outta be", attached.

so now, in addition to '%s', you also have %n, where n is a non-negative 
integer.  %0 is equivalent to %s, %1 matches the first group, etc.  values of 
n greater than the largest group index don't get expanded.

the patch is fairly minimal, and i did a cursory valgrind check to make sure 
that it wasn't leaking memory (though someone more familiar with QStringList 
might want to double check my logic). 

also, i see a few-years-old br upstream, so i'll forward the info there as 
well.


	sean
diff -Nru kdebase-3.5.7/klipper/urlgrabber.cpp kdebase-3.5.7.sean/klipper/urlgrabber.cpp
--- kdebase-3.5.7/klipper/urlgrabber.cpp	2005-10-10 17:03:59.000000000 +0200
+++ kdebase-3.5.7.sean/klipper/urlgrabber.cpp	2007-06-07 21:03:21.000000000 +0200
@@ -51,7 +51,6 @@
 {
     if( m_config == NULL )
         m_config = kapp->config();
-    myCurrentAction = 0L;
     myMenu = 0L;
     myPopupKillTimeout = 8;
     m_stripWhiteSpace = true;
@@ -160,6 +159,7 @@
 
         QString item;
         myCommandMapper.clear();
+        myGroupingMapper.clear();
 
         myPopupKillTimer->stop();
         delete myMenu;
@@ -184,6 +184,7 @@
                 else
                     id = myMenu->insertItem( SmallIcon(command->pixmap), item);
                 myCommandMapper.insert( id, command );
+                myGroupingMapper.insert( id, action->capturedTexts() );
             }
         }
 
@@ -224,19 +225,27 @@
 	break;
     default:
         ClipCommand *command = myCommandMapper.find( id );
-        if ( !command )
+        QStringList *backrefs = myGroupingMapper.find( id );
+        if ( !command || !backrefs )
             qWarning("Klipper: can't find associated action");
         else
-            execute( command );
+            execute( command, backrefs );
     }
 }
 
 
-void URLGrabber::execute( const struct ClipCommand *command ) const
+void URLGrabber::execute( const struct ClipCommand *command,
+                          QStringList *backrefs) const
 {
     if ( command->isEnabled ) {
         QMap<QChar,QString> map;
         map.insert( 's', myClipData );
+		int brCounter = -1;
+		QStringList::Iterator it = backrefs->begin();
+		while( it != backrefs->end() ) {
+			map.insert( char(++brCounter + '0') , *it );
+			++it;
+		}
         QString cmdLine = KMacroExpander::expandMacrosShellQuote( command->command, map );
 
         if ( cmdLine.isEmpty() )
diff -Nru kdebase-3.5.7/klipper/urlgrabber.h kdebase-3.5.7.sean/klipper/urlgrabber.h
--- kdebase-3.5.7/klipper/urlgrabber.h	2005-10-10 17:03:59.000000000 +0200
+++ kdebase-3.5.7.sean/klipper/urlgrabber.h	2007-06-07 20:58:48.000000000 +0200
@@ -72,7 +72,8 @@
     
 private:
   const ActionList& matchingActions( const QString& );
-  void execute( const struct ClipCommand *command ) const;
+  void execute( const struct ClipCommand *command, 
+                QStringList *backrefs ) const;
   void editData();
   bool isAvoidedWindow() const;
   void actionMenu( bool wm_class_check );
@@ -83,6 +84,7 @@
   QString myClipData;
   ClipAction *myCurrentAction;
   QIntDict<ClipCommand> myCommandMapper;
+  QIntDict<QStringList> myGroupingMapper;
   KPopupMenu *myMenu;
   QTimer *myPopupKillTimer;
   int myPopupKillTimeout;
@@ -127,8 +129,13 @@
 
   void  setRegExp( const QString& r) 	      { myRegExp = QRegExp( r ); }
   QString regExp() 			const { return myRegExp.pattern(); }
-  inline bool matches( const QString& string ) const {
-      return ( myRegExp.search( string ) != -1 );
+  inline bool matches( const QString& string ) {
+    int res = myRegExp.search( string ) ;
+    if ( res != -1 ) {
+      myCapturedTexts = myRegExp.capturedTexts();
+      return true;
+    } 
+    return false;
   }
 
   void 	setDescription( const QString& d)     { myDescription = d; }
@@ -147,9 +154,15 @@
    */
   void save( KConfig * ) const;
 
+  /**
+   * Returns the most recent list of matched group backreferences.
+   * Note: you probably need to call matches() first.
+   */
+  inline const QStringList* capturedTexts() const { return &myCapturedTexts; }
 
 private:
   QRegExp 		myRegExp;
+  QStringList	myCapturedTexts;
   QString 		myDescription;
   QPtrList<ClipCommand> 	myCommands;
 

Attachment: signature.asc
Description: This is a digitally signed message part.


Reply to: