Bug 140917

Summary: Not possible to set cursor to beginning of message with templates
Product: [Applications] kmail Reporter: Tobias <flabbergasted>
Component: templatesAssignee: kdepim bugs <kdepim-bugs>
Severity: normal CC: bluedzins, dmiceman
Priority: NOR    
Version: 1.9.7   
Target Milestone: ---   
Platform: Compiled Sources   
OS: Linux   
Latest Commit: Version Fixed In:

Description Tobias 2007-01-30 20:33:49 UTC
Version:            (using KDE Devel)
Installed from:    Compiled sources
OS:                Linux


I just updated to KDE 3.5.6 because I read about templates in kmail and thought this would make it possible to set the cursor to the beginning of the mail when replying with the quoted message below. However, if I define a template like



for "Reply to sender", the cursor is always in the second line (like it used to be with old versions). If I put a space or any kind of text before the %CURSOR it works (i.e. the text and the Cursor are in the first line) but that's not what I want.

I hope you will be able to fix this. Thanks so much for this otherwise great application.
Comment 1 Dmitry Morozhnikov 2007-01-31 14:58:12 UTC
Does this occur on sope specific email types or on all emails? 

Can`t reproduce with code from svn, will try stock 3.5.6..
Comment 2 Tobias 2007-01-31 16:16:45 UTC
It happens for all kinds of emails for me. I'm not using the SVN version, though.
Comment 3 Thomas McGuire 2007-02-10 15:42:49 UTC
*** Bug 141461 has been marked as a duplicate of this bug. ***
Comment 4 Maciej Pilichowski 2007-02-10 17:03:02 UTC
My report:
Consider such template: 
%REM="Default reply template"%- 
 For me when replying the first line should be quotation and the cursor should be placed at it. But KMail (I don't know why) inserts one blank line as the first line, so the mail really starts from the second line. 
I am not so sure it is duplicate -- I am just saying KMail adds an extra line at the beginning.
Comment 5 Dmitry Morozhnikov 2007-02-10 18:37:35 UTC
Oh, this is simple :-) To achieve desired behavior, your template should look like:

%REM="Default reply template"%-

because _any_ symbol in template is meaningful, including newlines.

Or alternatively:

%REM="Default reply template"%-

because %- mean "eat next newline"
Comment 6 Maciej Pilichowski 2007-02-10 18:50:01 UTC
Thank you, I fixed my templates and I can see now the bug Tobias described.
Comment 7 Dmitry Morozhnikov 2007-02-10 19:14:44 UTC
Still can`t reproduce with stock kmail. If template is:


then there are empty line before quotes (as expected), but cursor position is correct -- on first empty line.
Comment 8 Maciej Pilichowski 2007-02-10 19:29:27 UTC
As you said before:
Now, quotation begins from the first line, but you are at the second.
Comment 9 Maciej Pilichowski 2007-02-10 19:30:35 UTC
To be exact, I use 
%REM="Default reply template"%- 
Comment 10 Tobias 2007-02-11 09:59:26 UTC
I just tried the %-. I didn't know about it before. But even if I set it before the Cursor like


then the quote starts at the first line but the cursor is at the second. I even tried deleting my kmailrc and start with a fresh one because I thought there might be something wrong with it but that didn't help, either. 

Comment 11 Tobias 2007-02-11 10:02:34 UTC
Since it seems I don't know about all template symbols... is there a symbol like a backspace character that I could use? Maybe this would work as an easy fix for now.
As I said it also works if I use something like


Then the cursor is behind the "Hi" in the first line. It seems it's just not possible for me to set it to the upper left position.

Comment 12 Tobias 2007-02-11 10:38:22 UTC
I now tried it with a stable kmail from kde 3.5.6 that I compiled myself and it's the same. I'll try a snapshot version later. Maybe there IS something wrong with my settings...

Comment 13 Maciej Pilichowski 2007-02-11 11:07:27 UTC
It is sufficient to see the bug comparing:



In both cases the quotation starts from the first line, but in the first case cursor is in the second line, and in the second case in the first line.
Comment 14 Tobias 2007-02-12 20:51:57 UTC
Ok, I tried with a current snapshot and it's the same (unless I did something wrong... in Help it still said kmail 1.9.6...)

Comment 15 Maciej Pilichowski 2007-02-12 21:01:29 UTC
Tobias, relax, for now it is 2:1 (bug vs. worksforme) :-DDD.
Comment 16 groot 2007-02-15 00:13:17 UTC
The WORKSFORMEs have it. In r633599, I set the reply-to-sender template as follows: Settings->Configure KMail->Composer->Templates->Reply to Sender and fill in the text that started this whole bug:

Apply, then OK. Pick a message, shift-A to reply to sender and I get a blank line, blank line, [someone] wrote: followed by the quote, with cursor on the first line. Bug solved, right?

Well, except if I do
I *also* get the cursor on the first line, before the "A".
Comment 17 groot 2007-02-15 16:02:39 UTC
SVN commit 633860 by adridg:

When the message is set to a composer with a specific cursor position,
(ie. not 0, eg. from a template containing %CURSOR) then don't reset the
cursor position when appending the signature. This makes it possible
to use %CURSOR to place the cursor anywhere in a template. Useful
for reply templates, at any rate.

BUG: 140917
CCMAIL: kde-pim@kde.org

 M  +14 -2     branches/work/kdepim-3.5.5+/kmail/kmcomposewin.cpp  
 M  +9 -0      branches/work/kdepim-3.5.5+/kmail/kmcomposewin.h  

--- branches/work/kdepim-3.5.5+/kmail/kmcomposewin.cpp #633859:633860
@@ -177,7 +177,8 @@
     mEncryptWithChiasmus( false ),
     mComposer( 0 ),
     mLabelWidth( 0 ),
-    mAutoSaveTimer( 0 ), mLastAutoSaveErrno( 0 )
+    mAutoSaveTimer( 0 ), mLastAutoSaveErrno( 0 ),
+    mPreserveUserCursorPosition( false )
   mClassicalRecipients = GlobalSettings::self()->recipientsEditorType() ==
@@ -1982,6 +1983,11 @@
         QTimer::singleShot( 0, this, SLOT(slotAppendSignature()) );
+    if ( mMsg->getCursorPos() > 0 ) {
+      // The message has a cursor position explicitly set, so avoid
+      // changing it when appending the signature.
+      mPreserveUserCursorPosition = true;
+    }
   setModified( isModified );
@@ -4118,8 +4124,14 @@
     // for append and prepend, move the cursor to 0,0, for insertAt,
     // keep it in the same row, but move to first column
-    if ( !append && pos == 0 )
+    if ( ( !append && pos == 0 ) || mPreserveUserCursorPosition ) {
        mEditor->setCursorPositionFromStart( (unsigned int)mMsg->getCursorPos() );
+       // Only keep the cursor from the mMsg *once* based on the
+       // preserve-cursor-position setting; this handles the case where
+       // the message comes from a template with a specific cursor
+       // position set and the signature is appended automatically.
+       mPreserveUserCursorPosition = false;
+    }
--- branches/work/kdepim-3.5.5+/kmail/kmcomposewin.h #633859:633860
@@ -862,6 +862,15 @@
   QPopupMenu *mActNowMenu;
   QPopupMenu *mActLaterMenu;
+  /** If the message in this composer has a cursor position set (for 
+  *   instance because it comes from a template containing %CURSOR)
+  *   then we need to preserve that cursor position even when auto-
+  *   appending (or prepending) the signature during composer setup.
+  *   Set to true *once* (and only in setMsg() at that) to avoid
+  *   accidentally moving the cursor.
+  */
+  bool mPreserveUserCursorPosition;
Comment 18 Maciej Pilichowski 2007-06-20 19:19:26 UTC
Does KMail 1.9.7 have this fix included? I am asking because I still cannot set the cursor to be in the first line.
Comment 19 Thomas McGuire 2007-06-20 19:34:22 UTC
>Does KMail 1.9.7 have this fix included? I am asking because I still cannot set the cursor to be in the first line. 
My neither. I guess nobody ported it to the right branch (having that many branches is a pain really). Reopening.
Comment 20 Thomas McGuire 2007-07-17 13:39:13 UTC
Mass-changing component to "templates" and adding original template author to 
CC list.
Comment 21 Thomas McGuire 2007-08-15 16:40:18 UTC
SVN commit 700430 by tmcguire:

Fix the cursor position for templates.
Now %CURSOR works again, this was somehow lost in porting.
Additionally, it is now possible again to set the cursor to the first line.

BUG: 140917
CCBUG: 141459

 M  +2 -3      kmcomposewin.cpp  
 M  +8 -0      kmedit.cpp  
 M  +4 -0      kmedit.h  

--- trunk/KDE/kdepim/kmail/kmcomposewin.cpp #700429:700430
@@ -4635,9 +4635,8 @@
 void KMComposeWin::setReplyFocus( bool hasMessage )
-  if ( hasMessage ) {
-    mEditor->setCursorPosition( 1, 0 );
-  }
+  if ( hasMessage )
+    mEditor->setCursorPositionFromStart( (unsigned int) mMsg->getCursorPos() );
 void KMComposeWin::setFocusToSubject()
--- trunk/KDE/kdepim/kmail/kmedit.cpp #700429:700430
@@ -759,4 +759,12 @@
+void KMEdit::setCursorPositionFromStart( unsigned int pos ) {
+  unsigned int l = 0;
+  unsigned int c = 0;
+  posToRowCol( pos, l, c );
+  setCursorPosition( l, c );
+  ensureCursorVisible();
 #include "kmedit.moc"
--- trunk/KDE/kdepim/kmail/kmedit.h #700429:700430
@@ -91,6 +91,10 @@
   unsigned int lineBreakColumn() const;
+  /** set cursor to absolute position pos */
+  void setCursorPositionFromStart(unsigned int pos);
   void spellcheck_done( int result );
   void attachPNGImageData( const QByteArray &image );
Comment 22 Kevin Funk 2007-10-21 11:11:47 UTC
Isn't that included in 3.5.8? Still doesn't work here. My template: 

Comment 23 Tobias 2007-10-21 13:53:08 UTC
Yes, doesn't work for me either on Kubuntu Gutsy. Did the patch really fix it and just isn't applied? I tried to compile it myself but somehow got lost in the process so I can't really tell.
Comment 24 Thomas McGuire 2007-10-21 21:59:19 UTC
The patch is not in the 3.5 branch, only in trunk.
Comment 25 Tobias 2007-10-22 06:53:27 UTC
Does this mean we can't expect it before KDE 4? Or will there be another bugfixing release of the 3.5 branch?
Comment 26 Thomas McGuire 2007-10-22 20:07:20 UTC
>Does this mean we can't expect it before KDE 4? Or will there be another bugfixing release of the 3.5 branch? 
I don't know if there will be another 3.5 release, but anyhow, someone needs to backport this first, for which I don't have time unfortunately. Sorry.