Menu

auto-indentation

mathmax
2008-12-26
2012-11-13
  • mathmax

    mathmax - 2008-12-26

    Hello

    Auto-indentation is a cool feature in notepad++ 5.1.x but I think it could be improved.
    For years, I'm dreaming of auto-indentation in xml, html and css in notepad++.

    For example, if I type "<Item>", I would not only like that the tag auto-close but also to insert 2 newlines between the opening and closing tag and an indentation on the second line. So as soon as I type ">"

    I'll get :

    <Item>
            | (The cursor here)
    <Item> 

    The same behavior could be applied to css, c#, c++,... delimiters "{ }".

    It would save a lot of time... We can find such behaviors in some big IDE such as Visual Studio, but I would really like to have them in a light editor.

    If you want to do even better, I propose to implements such actions as Rules.
    A pseudo-example of rule could be : if the user type "{" in a css document then the action described above will be applied.
    Notepad++ could have some rules by default and the user could also add its own rules.

    What do you think about this proposition ?

     
    • mathmax

      mathmax - 2009-01-21

      It doesn't work because the hotstring ':*b0:>>::' is acting as a hotkey and when you type '>>' instead of typing the hotstring it executes the code under itself

      No it doesn't. It throws an error. I would like the code under to be executed... What should I change to make it work ?

      prozaker, thank you for you code, but I don't succeed to make it work. I've created a .css file, opened it in npp and typed "{" or "bd" but nothing happened.

       
    • Bob Uncle

      Bob Uncle - 2008-12-30

      Sounds like you need to create a macro to do this behaviour for you.

       
    • mathmax

      mathmax - 2009-01-07

      Not exactly because you have to use a shortcuts for that. I want this to be automatic. Moreover I don't understand how macro work. I click recording, then I do the modifications I need, then I click on stop. But I cannot save the macro and replay it.

       
    • Airdrik

      Airdrik - 2009-01-08

      There's a plugin that helps with writing html code in the Notepad++ Plugins project.  I don't remember if it was in the TextFX plugin, or another HTML-specific plugin, but it had an option to automatically add the closing tag immediately after the cursor when you closed the first tag.  You can try contacting the author of that plugin to see if he is willing to implement the rest of your suggestion. 

       
    • prozaker

      prozaker - 2009-01-13

      you can also try autohotkey, it's a program that can make 'hotstrings' and describes pretty much what you are saying.

      I have my braces autogenerated, on my css files, and most of my html tags on my html files

      for example I have :*:<ta::<table  width="100%" cellpadding="0" style="border-collapse:collapse;">`n`t<tr>`n`t<td>`n`n</td>`n{backspace}</tr>`n{backspace}</table>{up 3}`t

      which writes as soon as I type <ta

      <table  width="100%" cellpadding="0" style="border-collapse:collapse;">
          <tr>
              <td>
                 
              </td>
          </tr>
      </table>

       
    • mathmax

      mathmax - 2009-01-13

      Thank you prozaker.

      I've downloaded autohotkey and trying to execute your script but I get an error when trying to load it. It says : "invalid hotkey". are you sure you give the exact script you use ?

       
      • LMS_RampagE

        LMS_RampagE - 2009-01-13

        » Are you sure you give the exact script you use ? 

        The script prozaker wrote,  works fine.

        » It says : "invalid hotkey".

        Yes, Autohotkey spit such messages (even if the code seems to be ok), because everything is written on the same line. In Autohotkey, a good way of coding is to type the commands beneath the hotstring/hotkey (even if the commands are  shorter)

        ; type this to have the script running permanently
        #persistent

        :*:<ta::
        <table width="100%" cellpadding="0" style="border-collapse:collapse;">`n`t<tr>`n`t<td>`n`n</td>`n{backspace}</tr>`n{backspace}</table>{up 3}`t
        return

        ; save it and run it

        After that open your favorite text editor (Npp ofc :-|\) and type
        <ta. It should look like:

        <table width="100%" cellpadding="0" style="border-collapse:collapse;">
            <tr>
                <td>
                    
                </td>
            </tr>
        </table>

        Cheers,
        LMS_RampagE

         
    • mathmax

      mathmax - 2009-01-13

      Ok it works. But now I want this script only in npp. I try adding this around it :

      IfWinActive, % - Notepad++ and regexmatch(WinGetTitle, ".+\xml - Notepad++")
      {

      }

      But it doesn't work. Di you know why ?

      Which other actions do you use with this tool ? Is it possible to have a shortcut wor browsing next/previous directory in the windows explorer ?

       
      • prozaker

        prozaker - 2009-01-14

        let me check my script as i have it only for css files. ill post it tomorrow

         
      • LMS_RampagE

        LMS_RampagE - 2009-01-14

        » IfWinActive, % - Notepad++ and regexmatch(WinGetTitle, ".+\xml - Notepad++")

        » But it doesn't work. Di you know why ?

        • Well, WinGetTitle used this way always yields NOTHING (""). You must specify a variable to keep the winTitle and after that use this variable in RegExMatch function.

        • If I've got you right, the hotstring should be fired only if Npp is active and the current document is *.xml. If so the NeedleRegEx is typed badly. I mean what about if we have extension like *.XML or if  document's  title is for example "blabla.xml - Notepad++.lms". IMHO we need only the end of the document title to be a match.

        Possible solution:

        :*b0:<ta::

        ; b0  instructs Autohotkey to disable automatic backspacing
        ; this command is needed because the hotstring  is fired
        ; always when you type "<ta" and will autoreplace "<ta" with
        ; nothing if there's no match

        IfWinActive, ahk_class Notepad++
        {
            WinGetTitle, NppTitle
            if RegExMatch(NppTitle, "i)\.xml - Notepad\+\+$")
                sendinput, ble width="100`%" cellpadding="0" style="border-collapse:collapse;">`n`t<tr>`n`t<td>`n`n</td>`n{backspace}</tr>`n{backspace}</table>{up 3}`t
        }

        ; You can also use #IfWinActive directive to enable the hotstring
        ; only when Npp is active...

        return

        LMS_RampagE

         
      • Jan Schreiber

        Jan Schreiber - 2009-01-13

        > Is it possible to have a shortcut wor browsing next/previous directory in the windows explorer ?
        What about Alt+Left/Right? :-)

         
    • mathmax

      mathmax - 2009-01-14

      I change the script to :
      :*b0:>>::

      IfWinActive, ahk_class Notepad++
      {
      WinGetTitle, NppTitle
      if RegExMatch(NppTitle, "i)\.xml - Notepad\+\+$")
      {
      >`n`n{backspace}{up 1}`t 
      }
      }
      return

      but it doesn't work. The message is :
      This line doesn't contains a recognize action. Do you know why ?

      Why do you use i) in the regular expression ?

       
      • LMS_RampagE

        LMS_RampagE - 2009-01-14

        It doesn't work because the hotstring ':*b0:>>::' is acting as a hotkey and  when you type '>>'  instead of typing the hotstring it executes the code under itself

        :*b0:>>:: 
        ifWinActive, ahk_class Notepad++ 

        WinGetTitle, NppTitle 
        if RegExMatch(NppTitle, "i)\.xml - Notepad\+\+$")

        ; at that position Autohotkey expects some command
        ;but gets only a string
        ; You should use something like sendinput
        ; it is the same as :*b0:>>::>`n`n{backspace}{up 1}`t 
        ; but is executed in if statement
        SendInput,  >`n`n{backspace}{up 1}`t  ;{right 3}


        return

        » Why do you use i) in the regular expression ? 

        I used it because the 'i' flag stands for Case-insensitive matching. I mean if you don't type "i)" in a "\.xml - Notepad\+\+$" , when you open document with extension *.XML it won't be a match and the hotstirng won't fire. IMO it should work  with lower/upper/mixed letters in the extension.

        Cheers,
        LMS_RampagE

         
    • mathmax

      mathmax - 2009-01-15

      I change the script to :
      :*b0:>>::

      IfWinActive, ahk_class Notepad++
      {
      WinGetTitle, NppTitle
      if RegExMatch(NppTitle, "i)\.xml - Notepad\+\+$")
      {
      >`n`n{backspace}{up 1}`t 
      }
      }
      return

      but it doesn't work. The message is :
      This line doesn't contains a recognize action. Do you know why ?

      Why do you use i) in the regular expression ?

       
    • prozaker

      prozaker - 2009-01-15

      this is the top part of my script you should be able to modify it to your needs.

      ;Lines that start with a
      ;semicolon, such as this one, are comments.  They are not executed.

      SetTitleMatchMode RegEx
      #IfWinActive, .css - Notepad\+\+
      :?*:{::
      Sendinput, {{}`n`n{}}{up}`t
      Return
      #IfWinActive, .Message
      ::bd::
      Sendinput, Buen día{space}
      Return
      SetTitleMatchMode 2
      #IfWinActive, ahk_class gdkWindowToplevel
      :?c*:nN::
      Sendinput, {Ctrl Down}{Shift Down}u{Shift Up}{Ctrl Up}00f1{space}
      Return
      #IfWinActive

      :?c*:nN::ñ

      ;============== basic html strings ==========

      :*:<html::`t<{!}DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">`n<html xmlns="http://www.w3.org/1999/xhtml">`n<head>`n<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />`n<title>Editme</title>`n<link href="css/main.css" rel="stylesheet" type="text/css" />`n</head>`n`n<body>`n`n</body>`n</html>{up 2}`t

       
    • mathmax

      mathmax - 2009-01-16

      I change the script to :
      :*b0:>>::

      IfWinActive, ahk_class Notepad++
      {
      WinGetTitle, NppTitle
      if RegExMatch(NppTitle, "i)\.xml - Notepad\+\+$")
      {
      >`n`n{backspace}{up 1}`t 
      }
      }
      return

      but it doesn't work. The message is :
      This line doesn't contains a recognize action. Do you know why ?

      Why do you use i) in the regular expression ?

       
    • mathmax

      mathmax - 2009-01-20

      It doesn't work because the hotstring ':*b0:>>::' is acting as a hotkey and when you type '>>' instead of typing the hotstring it executes the code under itself

      No it doesn't. It throws an error. I would like the code under to be executed... What should I change to make it work ?

      prozaker, thank you for you code, but I don't succeed to make it work. I've created a .css file, opened it in npp and typed "{" or "bd" but nothing happened.