NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
intelligent tab and bracket placement


Joined: 01 Dec 2003
Posts: 3
Reply with quote
hi,

thanks for having this feature forum..

I love PHPEd, but there are several things my Emacs does that PHPed doesn't, relating to auto-formatting of code and code placement. i'd love to see these features added in a future version.

The features fall into two categories:


  1. INTELLIGENT TAB (aka TAB AS AUTO-FORMATTING KEY)
  2. INTELLIGENT BRACKET PLACEMENT


i'll go into detail with these below. please let me know if i've not been specific enough or if there are errors, etc..


  1. INTELLIGENT TAB (aka TAB AS AUTO-FORMATTING KEY)

    TAB auto-formats the code on the line, inserting TABs or spaces as needed (and as user chose in options, depending on whether they want to use actual tab chars or spaces). thus, the number of tabs/spaces inserted can vary with a single hit of the TAB key.

    In emacs PHP-mode, if i type:
    Code:

    if ($x == 5) {|


    (where '|' is my cursor) and then i hit the ENTER key, screen will now look like this:

    Code:

    if ($x == 5) {
    |


    this much is just like PHPed. however, now if i hit the TAB key,

    Code:

    if ($x == 5) {
      |


    now the cursor is 2 spaces out.

    at first this may look just like PHPed behavior. however, hitting the TAB key repeatedly does nothing new in Emacs, whereas in PHPed, it inserts more and more TABs (or spaces).

    the advantages of this will be seen in the next few examples, but for now suffice it to say that the this saves time for the programmer. the difference is that the editor has figured out where your code needs to go. this saves you time and also serves as a syntax check.

    to provide examples, i need to classify them. so, as far as i can tell, there are three sub-variants of intelligent TAB, depending on where the cursor is:

    intelligent TAB:

    1. moves code to where it should be on the line
    2. removes/adds white space as necessary at beginning (but not at end) of code on that line
    3. places cursor intelligently on the line depending where you are:

      1. CURSOR AT OR BEFORE START OF CODE:

        let's say i enter the first line of inner code to this section:

        Code:

        if ($x == 5) {
          print ("got it");|


        ok, now i hit HOME (or in Emacs, Ctrl-a) to go to beginning of line:

        Code:

        if ($x == 5) {
        | print("got it");


        now i insert a few spaces:

        Code:

        if ($x == 5) {
                  | print("got it");


        now i hit TAB once, and the line is automatically formatted BACK to where it should be (with cursor at start of code on that line -- 2 chars out):

        Code:

        if ($x == 5) {
          |print("got it");


        furthermore, no matter how many more times i hit TAB, it will be the same as above: nothing will change now since the line is formatted correctly.

      2. CURSOR IN MIDDLE OF CODE:

        now suppose i go back to the beginning of line again:

        Code:

        if ($x == 5) {
        |  print("got it");


        and insert some spaces again:

        Code:

        if ($x == 5) {
              |  print("got it");


        and now move the cursor to the MIDDLE of the code:

        Code:

        if ($x == 5) {
                 prin|t("got it");


        now if i hit TAB, the code is once again returned to its correct place on the line, but ALSO the cursor stays INSIDE the code where it WAS:

        Code:

        if ($x == 5) {
          prin|t("got it");



      3. CURSOR **AT** END OF CODE:

        now if i return the cursor to the beginning of the line via a HOME or Ctrl-a in Emacs:

        Code:

        if ($x == 5) {
          |print("got it");


        and then add some more spaces again:

        Code:

        if ($x == 5) {
                       |print("got it");


        and then hit END (or Ctrl-e in Emacs) to go to end of line:

        Code:

        if ($x == 5) {
                       print("got it");|


        and then hit TAB, the cursor stays where it was (at end of line) but extra spaces are removed and code starts 2 chars out again:

        Code:

        if ($x == 5) {
          print("got it");|



      4. CURSOR **AFTER** END OF CODE (WHITESPACE AT END OF LINE):

        if i go back to beginning of line:

        Code:

        if ($x == 5) {
        | print("got it");


        and add some more spaces again:

        Code:

        if ($x == 5) {
                       |print("got it");


        and then hit END (or Ctrl-e in Emacs) to go to end of line:

        Code:

        if ($x == 5) {
                       print("got it");|


        and then add MORE spaces at the end of the line:

        Code:

        if ($x == 5) {
                       print("got it");        |


        and then hit TAB, the cursor stays where it was (at end of line). note that extra spaces at end of line have NOT been removed, and the cursor has stayed in same position relative to the code on the line, though the code itself has been shifted in to its proper position:

        Code:

        if ($x == 5) {
          print("got it");        |


        NOTE: this i would actually prefer to be improved over the emacs version. i'd rather that the extra w.s. at the END of the line ALSO be removed. so theoretically, the result from above would be:

        Code:

        if ($x == 5) {
          print("got it");|



    4. INTELLIGENT BRACKET PLACEMENT

      when closing the section, the closing bracket gets auto-indented correctly (directly under the 'i' in 'if'). this happens REGARDLESS of where the cursor is at the time.

      for instance, continuing with the example above, if i go ahead and hit ENTER:

      Code:

      if ($x == 5) {
        print("got it");
      |


      and now i add in a bunch of spaces:

      Code:

      if ($x == 5) {
        print("got it");
                 |


      but now at this point I type in '}', then this closing bracket is put NOT out at the end (as PHPEd would do it) but instead, in its correct place directly under the 'i' in 'if':

      Code:

      if ($x == 5) {
        print("got it");
      }|


      and all extra spaces/tabs have been removed and cursor follows closing bracket.

      furthermore, if i hit TAB again any number of times at this point, nothing more happens -- i.e., no more tabs/spaces are added.

      also, the same would be true had this particular section of code been indented twice.

      for instance, suppose the 'if' started 4 spaces (2 indents) in:

      Code:

          if ($x == 5) {
            print("got it");|


      if i hit ENTER at this point, i'll be taken to directly under the 'p' in 'print' to start a 2nd line of inner code for this section:

      Code:

          if ($x == 5) {
            print("got it");
            |


      but now if i just close out the section by typing a '}', the closing bracket will appear automatically under the 'i' in 'if', as it should (instead of under the 'e' as PHPed would do it):

      Code:

          if ($x == 5) {
            print("got it");
          }|


      also note that this bracket placement and code placement work regardless of which bracket style i use. thus, in Emacs, if i use the "opening-bracket-on-next-line" method, the editor accomodates, putting the opening bracket in the right place and also --- when i hit return the second time (and hit TAB) --- placing my cursor at the right place to start code:

      Code:

          if ($x == 5)
          {
            |


      finally, adding my code in and closing up the section, the editor accomodates to the alternative style with the closing bracket in the right place as well:

      Code:

          if ($x == 5)
          {
            print("got it");
          }|



=========

...all of the above helps me auto-format code, saving time. also, it helps check syntax because if code DOESN'T indent properly when i hit TAB, then i KNOW that i've just typed in something bogus on the CURRENT line or the line previous.

please let me know if i've not been specific enough, or i've made an error, or if some of this is already a feature and i've missed it somehow, etc.

jeff stern
View user's profileFind all posts by jasSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 7887
Reply with quote
Hi Jeff,

All you said is just excellent and with pretty good examples.

Although I'd like to see your comments about the following
1. suppose we have code like this

Code:
    if xxxxxx { yyyyyyy
zzzzzzzzzzzzzzzzzzzzz
wwwwwwwwwwwwww
         |


where would closing bracket be inserted ?
I suspect it would be placed under i in if. Right ?

2. suppose we have code like this
Code:
   if xxxxxxx {
zzzzzzzzzzzzzzzzzzz
wwww|wwwwwwwww


how would the bottom line be formatted if I press TAB ?
I suspect the leftmost w would be two spaces on right than i in if. Right ?

3. suppose we have code like this
Code:
   if xxxxxxx {
zzzz?>zz{zzzz<?php zzzzzzzzz
wwww|wwwwwwwww


how would the bottom line be formatted if I press TAB ?

and finally how does TAB work if there is some text selected in the editor ?
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 01 Dec 2003
Posts: 3
Reply with quote
hi ddmitrie,

thanks for the compliments Smile and i will try to answer your questions.

first please let me apologize for not returning your email right away.. it's been a combination of being busy for the holiday season and avoiding the detail of going back through and answering your question.. Smile Smile please excuse me..

i will answer the current question since i didn't tell you about php-mode before (see below) and i owe you an answer.

however, please note that for me to experiment with emacs's php-mode behavior and then record it all here in the forum with bbcode is rather time intensive for me and most-likely quite error-prone. also, my response time sux (as you can see Smile

so maybe it might be better/faster/more accurate for you to load and use emacs (with php-mode) directly to get answers to these types of questions? again, i should have said this in the first place, sorry..

emacs's main web site is at http://www.gnu.org/software/emacs/emacs.html and if you hunt around you can find several windows versions (free and commercial). there are also many different pre-packaged versions. for instance, on my redhat-linux 9, i use a compilation/distribution of emacs which comes with that os (version 21.2.1), distributed by its vendor...

regarding php-mode:

as you probably know (and i am weak here so please correct me anyone), emacs is itself written in a variant of the artificial intelligence language LISP -- the variant being called 'e-lisp'.

furthermore, the various 'modes' for emacs are also written in e-lisp. also, they are most often contributed by third parties. php-mode for emacs is such a third-party contribution and its project is on the web at http://sourceforge.net/projects/php-mode/ for instance.. there you can talk to its authors and ask them any questions you may have, read their forums, etc.

okay, here's best answers i can give for your questions... hope they help..

1) suppose the 'i' in 'if' is one indent in (2 spaces before it). generally the '}' will go under the 'i' in 'if' regardless of where your zzzzzzzzzzz and wwwwwww lines are.

however, long before you get to this point, php-mode will not like it that you have yyyyyyyyyyyy code after the '{' and will have told you this by not indenting the zzzzzzzzzzz or wwwwwwwwwww lines correctly. instead, when you hit TAB on the zzzzzzzzz or wwwwwwww lines (or hit ';' at the end -- same behavior as TAB), it will indent the zzzzzzzzz line only 1 space --- not 4 spaces. this tells you that php-mode is having a problem with something --- in this case the yyyyyy stuff.

if you move yyyyy down to the next line, then with each line you hit TAB or finish off with a ';', these lines will now be moved 4 spaces in (2 indents) which is correct.

regardless of whether the yyyyy is there and regardless of whether the zzzzzzzz and wwwwwww lines are correctly indented, the closing bracket ('}') will be inserted directly udner the 'i' in 'if' --- and this is regardless of where you are on the line at the time you hit '}'.

2) this is a similar question to 1, above. again, under most circumstances, your zzzzz line ('b=1;') would have a ';' at the end, and so would have been properly indented automatically by the time you got to the www line ('c=1;'). however, if like above, i go back up and forcefully un-indent the zzzzzzzz and wwwww lines:

Code:

  if (a==1) {
b=1;
c|=1;


and then hit TAB then under this condition php-mode gives me error feedback by indenting the 'c' line only one space -- not 4, as it should be:

Code:

  if (a==1) {
b=1;
 c|=1;


again, this is the way the php-mode tells me the user that something is bogus and it doesn't know what to do and i should fix it.

at this point i realize b line is wrong, i go back up 1 line and hit TAB:

Code:

  if (a==1) {
    b|=1;
 c=1;


which indents b line correctly. then i go down to the c line and hit TAB and now we're all lined up correctly again:

Code:

  if (a==1) {
    b=1;
    c|=1;


then i go to end of line (after semi-colon):

Code:

  if (a==1) {
    b=1;
    c=1;|


and hit RETURN and i'm back in business right where i need to be again to continue typing:

Code:

  if (a==1) {
    b=1;
    c=1;
    |


( i should also mention that besides indenting single lines, php-mode can auto-indent the whole buffer with the 'Ctrl-x h ESC Ctrl-\' command -- rather awkward and i keep the dang thing on a post-it note on my screen BUT i use it when i've screwed up so badly on my code (not just a line or two above but somewhere further up that i can't find) that i need to auto-indent the whole buffer to find it: the error is then easy to find, again because where the indenting stops working correctly is most likely where i did something bizarre.)

3) when i try the following under emacs php-mode:

Code:

  if (a==1) {
b=1;?>hello there<?php c=1;
d=1|


and then hit ';', again, the mode puts the d line out only 1 character. again, php-mode's way of telling me it doesn't grok what i did on the line before. the problem is not with the <?php but with the fact that i forcefully removed the indentation on the 'b' line:

Code:

  if (a==1) {
b=1;?>hello there<?php c=1;
 d=1;|


now, if i get to your question by going to the beginning of the 'd' line and erasing the space:

Code:

   if (a==1) {
 b=1;?>hello there<?php c=1;
|d=1;


and now moving over to between the 'd' and the '=' (middle of wwwwww line as you have it):

Code:

  if (a==1) {
b=1;?>hello there<?php c=1;
d|=1;


and i now hit TAB, it is the same behavior as i just had with the ';': php-mode again indents the line only 1 space out:

Code:

  if (a==1) {
b=1;?>hello there<?php c=1;
 d|=1;


again: php-mode's way of saying, 'hey dude, something's not write on one or more lines above). so finally, again, same behavior may be 'fixed' by going up one line (to b line) hitting TAB:

Code:

  if (a==1) {
    b=|1;?>hello there<?php c=1;
 d=1;


and then coming back down to the d line:

Code:

  if (a==1) {
    b=1;?>hello there<?php c=1;
 d=1;|


and hitting TAB again. at that point, d will space out properly and we're back in business:

Code:

  if (a==1) {
    b=1;?>hello there<?php c=1;
    d=1;|



4) TAB works correctly on the code whether or not code is 'selected' (there are two different ways to 'select' code in emacs -- behavior is same regardless).

i hope this helped.. please let me know what i've left out or obfuscated..

yours,
jeff
View user's profileFind all posts by jasSend private message


Joined: 25 Nov 2014
Posts: 4
Reply with quote
here's best answers i can give for your questions... hope they help..

View user's profileFind all posts by tongclub48Send private message
intelligent tab and bracket placement
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic