omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Questions about editor

    Pythonista
    3
    16
    8541
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Obelisk
      Obelisk last edited by

      1. I wanna try editor.replace_text, it works well in example, but if i add editor.make_new_file, it wont work after editor.make_new_file, is there any way to make it?
      2. Is there any way to make other language syntax highlight in editor and no need to run?
      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        in the beta, some other languages highlight in editor.

        what is the error you get when using replace_text? can you post your code of what you tried?

        1 Reply Last reply Reply Quote 0
        • Obelisk
          Obelisk last edited by

          In the latest beta,other languages highlight in editor?

          there's an example in editor module tourial,it works well,but if i add make_new_file before replace_text it wont work. I will try other functions.

          #Comment/Uncomment selected lines
          
          import editor
          
          text = editor.get_text()
          selection = editor.get_line_selection()
          selected_text = text[selection[0]:selection[1]]
          is_comment = selected_text.strip().startswith('#')
          replacement = ''
          for line in selected_text.splitlines():
          	if is_comment:
          		if line.strip().startswith('#'):
          			replacement += line[line.find('#') + 1:] + '\n'
          		else:
          			replacement += line + '\n'
          	else:
          		replacement += '#' + line + '\n'
          
          editor.replace_text(selection[0], selection[1], replacement)
          editor.set_selection(selection[0], selection[0] + len(replacement) - 1)
          
          
          1 Reply Last reply Reply Quote 0
          • JonB
            JonB last edited by

            your best hope of getting help on these forums is to be specific:

            -post the code you think should work, not the code you started from with a vague deacription of what you then changed

            -tell what you expected to happen, and what atually happened when you ran the code (did you get a crash, an exception, or just unexpect d behavior)

            in the latest beta, calling editor.make_new_file with no arguments causes a crash. calling it with a filename creates a file as expected. doing that before calling replace_text makes no sense, because the new file will have no text to replace!

            1 Reply Last reply Reply Quote 0
            • Obelisk
              Obelisk last edited by

              I mean add

              editor.make_new_file(tmp, 'test')
              
              editor.set_selection(0, 5)
              
              editor.replace_text(0, 5, 'good')
              

              before replace_text, then all replace_text dont work, how to make it?

              1 Reply Last reply Reply Quote 0
              • ccc
                ccc last edited by

                editor.set_selection(0, 4) # the word 'test' contains 4 characters

                1 Reply Last reply Reply Quote 0
                • Obelisk
                  Obelisk last edited by

                  Thanks, i thought wrong, but as if replace_text dont work yet.

                  1 Reply Last reply Reply Quote 0
                  • ccc
                    ccc last edited by

                    I am lost...

                    import editor
                    editor.make_new_file('tmp', 'test')  # quotes around tmp
                    editor.set_selection(0, 4)           # 4 instead of 5
                    editor.replace_text(0, 4, 'good')    # 4 instead of 5
                    

                    ...works as expected in the current v1.6 beta.

                    1 Reply Last reply Reply Quote 0
                    • JonB
                      JonB last edited by

                      why set selection before replace? im pretty sure replace resets the selection?

                      1 Reply Last reply Reply Quote 0
                      • ccc
                        ccc last edited by

                        Try taking it out and see... I was surprised myself.

                        1 Reply Last reply Reply Quote 0
                        • Obelisk
                          Obelisk last edited by

                          Sorry,i dont use 1.6 beta,please ignore below:

                          #Comment/Uncomment selected lines
                          
                          import editor
                          
                          text = editor.get_text()
                          selection = editor.get_line_selection()
                          selected_text = text[selection[0]:selection[1]]
                          is_comment = selected_text.strip().startswith('#')
                          replacement = ''
                          for line in selected_text.splitlines():
                          	if is_comment:
                          		if line.strip().startswith('#'):
                          			replacement += line[line.find('#') + 1:] + '\n'
                          		else:
                          			replacement += line + '\n'
                          	else:
                          		replacement += '#' + line + '\n'
                          
                          #import editor
                          editor.make_new_file('tmp', 'test')  # quotes around tmp
                          #editor.set_selection(0, 4)           # 4 instead of 5
                          editor.replace_text(0, 4, 'good')    # 4 instead of 5
                          
                          editor.replace_text(selection[0], selection[1], replacement)
                          editor.set_selection(selection[0], selection[0] + len(replacement) - 1)
                          
                          
                          1 Reply Last reply Reply Quote 0
                          • JonB
                            JonB last edited by

                            ccc, you just have to give the editor a moment to create and open the file before writing to it. for large files, it was a good idea to call get_path until the proper path was returned, but for this example a small sleep works, or even the set selection, etc.

                            import editor,time
                            editor.make_new_file('tmp', 'test')  
                            time.sleep(0.5)
                            editor.replace_text(0, 4, 'good')    
                            

                            obelisk...not sure what you are trying to do! are you still having trouble?

                            1 Reply Last reply Reply Quote 0
                            • Obelisk
                              Obelisk last edited by

                              Many thanks. Could u give an example to make other language syntax highlight in editor and no need to run?

                              1 Reply Last reply Reply Quote 0
                              • ccc
                                ccc last edited by

                                Create a new file called junk.html in the Editor (the .html is important) and paste in this content:

                                <magenta="red">black</magenta> <style type="text/css">/* grey */</style>
                                

                                The text magenta, red, black, and grey will be in those colors (depending on which theme you have selected).

                                1 Reply Last reply Reply Quote 0
                                • JonB
                                  JonB last edited by

                                  syntax highlighting of html is a feature of the beta,not 1.5

                                  1 Reply Last reply Reply Quote 0
                                  • Obelisk
                                    Obelisk last edited by

                                    Thanks very much,what to do next?use default theme or blackboard theme?

                                    1 Reply Last reply Reply Quote 0
                                    • First post
                                      Last post
                                    Powered by NodeBB Forums | Contributors