Index: trac/ticket/api.py
===================================================================
--- trac/ticket/api.py	(revision 8022)
+++ trac/ticket/api.py	(working copy)
@@ -354,10 +354,13 @@
     def get_permission_actions(self):
         return ['TICKET_APPEND', 'TICKET_CREATE', 'TICKET_CHGPROP',
                 'TICKET_VIEW', 'TICKET_EDIT_CC', 'TICKET_EDIT_DESCRIPTION',
+                'TICKET_EDIT_COMMENT',
                 ('TICKET_MODIFY', ['TICKET_APPEND', 'TICKET_CHGPROP']),
+                ('TICKET_DELETE_COMMENT', ['TICKET_EDIT_COMMENT']),
                 ('TICKET_ADMIN', ['TICKET_CREATE', 'TICKET_MODIFY',
                                   'TICKET_VIEW', 'TICKET_EDIT_CC',
-                                  'TICKET_EDIT_DESCRIPTION'])]
+                                  'TICKET_EDIT_DESCRIPTION',
+                                  'TICKET_DELETE_COMMENT'])]
 
     # IWikiSyntaxProvider methods
 
Index: trac/ticket/web_ui.py
===================================================================
--- trac/ticket/web_ui.py	(revision 8022)
+++ trac/ticket/web_ui.py	(working copy)
@@ -39,7 +39,8 @@
 from trac.timeline.api import ITimelineEventProvider
 from trac.util import get_reporter_id
 from trac.util.compat import any
-from trac.util.datefmt import format_datetime, to_timestamp, utc
+from trac.util.datefmt import parse_date, pretty_timedelta, to_datetime, \
+                              format_datetime, to_timestamp, utc
 from trac.util.text import CRLF, shorten_line, obfuscate_email_address, \
                            exception_to_unicode
 from trac.util.presentation import separated
@@ -474,7 +475,35 @@
                 return self._render_history(req, ticket, data, text_fields)
             elif action == 'diff':
                 return self._render_diff(req, ticket, data, text_fields)
-        elif req.method == 'POST': # 'Preview' or 'Submit'
+        elif req.method == 'POST': # 'Preview' or 'Submit' or Comment Edit/Del
+        
+            # Cancel comment edit or delete
+            if 'cancel_comment' in req.args:
+                req.redirect(req.href.ticket(ticket.id))                
+            # Edit a ticket comment
+            elif ('edit_comment' in req.args and 
+                  'TICKET_EDIT_COMMENT' in req.perm(ticket.resource)):
+                author = req.args.get('comment_author')
+                if (author == req.authname or 
+                   'TRAC_ADMIN' in req.perm(ticket.resource)):
+                    comment = req.args.get('edited_comment')
+                    comment += "^^%s&%s^^" % (to_timestamp(datetime.now(utc)), 
+                                              get_reporter_id(req, 'author'))
+                    when = req.args.get('edit_comment_when')
+                    when_ts = to_timestamp(parse_date(when, req.tz))
+                    ticket.edit_comment(comment, when_ts)
+                    req.redirect(req.href.ticket(ticket.id))
+            # Delete a ticket comment
+            elif ('delete_comment' in req.args and 
+                  'TICKET_DELETE_COMMENT' in req.perm(ticket.resource)):             
+                author = req.args.get('comment_author')
+                if (author == req.authname or 
+                    'TRAC_ADMIN' in req.perm(ticket.resource)):  
+                    when = req.args.get('delete_comment_when')               
+                    when_ts = to_timestamp(parse_date(when, req.tz))
+                    ticket.delete_comment(when_ts)
+                    req.redirect(req.href.ticket(ticket.id))
+        
             # Do any action on the ticket?
             actions = TicketSystem(self.env).get_available_actions(
                 req, ticket)
@@ -1214,6 +1243,10 @@
         """Insert ticket data into the template `data`"""
         replyto = req.args.get('replyto')
         data['replyto'] = replyto
+        if req.args.get('cnum_edit'):
+            data['cnum_edit'] = req.args.get('cnum_edit')
+        elif req.args.get('cnum_del'):
+            data['cnum_del'] = req.args.get('cnum_del')
         data['version'] = ticket.resource.version
         data['description_change'] = None
 
@@ -1442,6 +1475,15 @@
                     current['cnum'] = autonum
             # some common processing for fields
             if field == 'comment':
+                # find comments that have been edited and create pretty
+                # "edited by" messages for them
+                if new.endswith('^^'):
+                    m = re.search('\^\^(.+?)\&(.+?)\^\^$', new)
+                    ts = int(m.group(1))
+                    user = m.group(2)
+                    new = new[:m.start(0)]
+                    current['comment_edited'] = ("\n\n''Edited %s ago by %s.''" 
+                        % (pretty_timedelta(to_datetime(ts)), user))
                 current['comment'] = new
                 if old:
                     if '.' in old: # retrieve parent.child relationship
Index: trac/ticket/model.py
===================================================================
--- trac/ticket/model.py	(revision 8022)
+++ trac/ticket/model.py	(working copy)
@@ -363,7 +363,26 @@
 
         for listener in TicketSystem(self.env).change_listeners:
             listener.ticket_deleted(self)
+        
+    def edit_comment(self, cmt, ts, db=None):
+        db, handle_ta = self._get_db_for_write(db)
+        cursor = db.cursor()
 
+        cursor.execute("UPDATE ticket_change SET newvalue=%s "
+                       "WHERE ticket=%s AND time=%s AND field='comment'",
+                       (cmt, self.id, ts))
+        if handle_ta:
+            db.commit()
+        
+    def delete_comment(self, ts, db=None):
+        db, handle_ta = self._get_db_for_write(db)
+        cursor = db.cursor()
+        
+        cursor.execute("DELETE FROM ticket_change "
+                       "WHERE ticket=%s AND time=%s AND field='comment'",
+                       (self.id, ts))
+        if handle_ta:
+            db.commit()        
 
 def simplify_whitespace(name):
     """Strip spaces and remove duplicate spaces within names"""
Index: trac/ticket/templates/ticket.html
===================================================================
--- trac/ticket/templates/ticket.html	(revision 8022)
+++ trac/ticket/templates/ticket.html	(working copy)
@@ -48,7 +48,7 @@
       <a href="#comment:$cnum">$prefix$cnum</a>
     </py:def>
 
-    <py:def function="display_change(change)">
+    <py:def function="display_change(change, edit_cnum=0)">
       <ul py:if="change.fields" class="changes">
         <li py:for="field_name, field in change.fields.items()">
           <strong>${field_name}</strong>
@@ -69,8 +69,11 @@
           </py:choose>
         </li>
       </ul>
-      <div py:if="'comment' in change" class="comment searchable" xml:space="preserve">
+      <div py:if="'comment' in change and str(change.cnum) != edit_cnum" class="comment searchable" xml:space="preserve">
         ${wiki_to_html(context, change.comment, escape_newlines=preserve_newlines)}
+        <py:if test="change.comment_edited">
+          ${wiki_to_html(context, change.comment_edited, escape_newlines=preserve_newlines)}
+        </py:if>
       </div>
     </py:def>
 
@@ -204,7 +207,7 @@
         <py:if test="ticket.exists and changes">
           <h2>Change History</h2>
           <div id="changelog">
-            <form py:for="change in changes" method="get" action="#comment" class="printableform">
+            <py:for each="change in changes" class="printableform">
               <div class="change">
                 <h3 class="change">
                   <span class="threading" py:if="'cnum' in change"
@@ -224,12 +227,48 @@
                   Changed ${dateinfo(change.date)} ago by ${authorinfo(change.author)}
                 </h3>
                 <div py:if="'cnum' in change and 'TICKET_APPEND' in perm(ticket.resource)" class="inlinebuttons">
-                  <input type="hidden" name="replyto" value="${change.cnum}" />
-                  <input type="submit" value="${_('Reply')}" title="Reply to comment ${change.cnum}" />
+                  <form method="get" action="#comment">
+                    <input type="hidden" name="replyto" value="${change.cnum}" />
+                    <input type="submit" value="${_('Reply')}" title="Reply to comment ${change.cnum}" />
+                  </form>
                 </div>
-                ${display_change(change)}
+                ${display_change(change, ('TICKET_DELETE_COMMENT' in perm(ticket.resource) and cnum_edit or 0))}
+                <py:choose>
+                  <py:when test="'TICKET_EDIT_COMMENT' in perm(ticket.resource) and str(change.cnum) == cnum_edit">
+                    <form method="post">
+                      <p><textarea name="edited_comment" class="wikitext" rows="10" cols="78">${change.comment}</textarea></p>
+                      <input type="hidden" name="edit_comment_when" value="${change.date.isoformat()}" />
+                      <input type="hidden" name="comment_author" value="${change.author}" />    
+                      <input type="submit" name="edit_comment" value="${_('Submit changes')}" title="Submit changes to comment ${change.cnum}" />
+                      <input type="submit" name="cancel_comment" value="${_('Cancel')}" title="Cancel comment edit" />
+                    </form>
+                  </py:when>
+                  <py:when test="'TICKET_DELETE_COMMENT' in perm(ticket.resource) and str(change.cnum) == cnum_del">
+                    <h4>Comment deletion is permanent. Are you sure you want to delete this comment?</h4>
+                    <form method="post">
+                      <input type="hidden" name="delete_comment_when" value="${change.date.isoformat()}" />
+                      <input type="hidden" name="comment_author" value="${change.author}" />
+                      <input type="submit" name="delete_comment" value="${_('Delete')}" title="Delete comment ${change.cnm}" />
+                      <input type="submit" name="cancel_comment" value="${_('Cancel')}" title="Cancel comment delete" />
+                    </form>
+                  </py:when>
+                  <py:when test="change.comment">
+                    <form py:if="'TICKET_EDIT_COMMENT' in perm(ticket.resource) and 
+                                 (authname == change.author or 'TRAC_ADMIN' in perm(ticket.resource))" 
+                          method="get" action="#comment:${change.cnum}" class="commentbuttons">
+                      <input type="hidden" name="cnum_edit" value="${change.cnum}" />
+                      <input type="submit" value="${_('Edit')}" title="Edit comment ${change.cnum}" />
+                    </form>
+                    <form py:if="'TICKET_DELETE_COMMENT' in perm(ticket.resource) and 
+                                 (authname == change.author or 'TRAC_ADMIN' in perm(ticket.resource))" 
+                          method="get" action="#comment:${change.cnum}" class="commentbuttons">
+                      <input type="hidden" name="cnum_del" value="${change.cnum}" />
+                      <input type="submit" value="${_('Delete')}" title="Delete comment ${change.cnum}" />
+                    </form>       
+                  </py:when>
+                </py:choose>
               </div>
-            </form>
+            </py:for>
           </div>
         </py:if>
       </py:if>
Index: trac/htdocs/css/ticket.css
===================================================================
--- trac/htdocs/css/ticket.css	(revision 8022)
+++ trac/htdocs/css/ticket.css	(working copy)
@@ -76,6 +76,9 @@
 }
 
 #changelog { border: 1px outset #996; padding: 1em }
+#changelog .commentbuttons {
+ display: inline;
+}
 #preview { border: 1px solid #d7d7d7; padding: 1em }
 #preview h3, #changelog h3 {
  border-bottom: 1px solid #d7d7d7;

