Project

General

Profile

Bug #441

Emacs UI: C-c C-e does not ignore keywords inside strings

Added by John Abbott over 10 years ago. Updated about 10 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
EmacsUI
Target version:
Start date:
12 Feb 2014
Due date:
% Done:

100%

Estimated time:
5.00 h
Spent time:

Description

The emacs key sequence C-c C-e should automatically insert the correct End... keyword to close a syntactical construct. It works unless a CoCoA keyword appears inside a string literal -- I quite often write strings containing the word "for".

Fix it (at least for normal string literals)!


Related issues

Related to CoCoA-5 - Bug #470: Emacs UI: cocoa5-close-block should beep when there is no block to closeClosed2014-03-12

History

#1 Updated by John Abbott over 10 years ago

I believe the relevant function is cocoa5-close-block. The bug probably lies inside cocoa5-last-unended-begin (defn starts on line 860).

I noticed this very strange line (881); the first error is to catch all errors, the second error is to throw an error with message "Couldn't find..."

        (error (error "Couldn't find unended command")))

#2 Updated by John Abbott over 10 years ago

  • Status changed from New to In Progress
  • Assignee set to John Abbott
  • % Done changed from 0 to 50

I have added a regexp "suffix" so that keywords are recognised "outside strings".

Here is the regexp

"[^\"]*\\(\"[^\"]*\"\\)*[^\"]*$" 

This regexp effectively just counts how many double quote characters there are after the keyword, so it will be confused by strings which contain (an odd number of) escaped double quote characters. Since strings containing escaped double quote characters are rare, I think this may be a fair compromise.

I did try creating a regexp which handles escaped double quotes but it didn't work -- I'm not entirely surprised as it was quite a convoluted regexp :-)

Should we accept my current regexp as an adequate solution? (and change this issue to "feedback"?)

#3 Updated by John Abbott over 10 years ago

My regexp doesn't work quite as I hoped: it fails of the matching start keyword is on the same line as point. Here's a specific example:

If N > 1 Then PrintLn "For"; 

Executing C-c C-e at the end of this line does not find If. :-(

#4 Updated by John Abbott over 10 years ago

  • Status changed from In Progress to Resolved
  • % Done changed from 50 to 80

I now have a new regexp which seems to work better:

"[^\"]*\\(\"[^\"]*\"\\)*[^\"]*\\($\\|\\'\\)" 

I have also added a save-restriction to the fn cocoa5-last-unended-begin

I have a more complicated regexp commented out; it seems not to work, but I cannot figure out why :-(

Marking issue as "resolved"; but it needs more testing...

#5 Updated by John Abbott over 10 years ago

Here is are two test cases. The first works OK; the second does not.

Define works(X)
  PrintLn "str1 keyword For";
  If X > 1 Then PrintLn "str2", "str3";

Two uses of C-c C-e should correctly close the If and then the Define.

Define NotWorks(X)
  PrintLn "str1 keyword For";
  If X > 1 Then PrintLn "str2 quote mark \" ", "str3";

The simple regexp just counts " symbols and so thinks that If is inside a string and that For is outside.

Now I believe my fancier regexp works properly. It is very confusing with all those backslash escape characters!
I do not put the regexp here because it confuses redmine (which loses almost the entire post!

#6 Updated by John Abbott over 10 years ago

The regexp

"[^\"]*\\(\"\\([^\\\"]\\|\\\\.\\)*\"[^\"]*\\)*\\($\\|\\'\\)" 

#7 Updated by Anna Maria Bigatti over 10 years ago

  • Target version set to CoCoA-5.1.0 Easter14

#8 Updated by John Abbott about 10 years ago

  • Status changed from Resolved to Feedback
  • % Done changed from 80 to 90
  • Estimated time set to 5.00 h

I have just tried the NotWorks example from comment-5; it works correctly with the current Emacs UI code. Changing state to Feedback

#9 Updated by John Abbott about 10 years ago

  • Status changed from Feedback to Closed
  • % Done changed from 90 to 100

After the mindboggling refresher course in regexps, I'm closing this because it all (magically?) works.

Also available in: Atom PDF