Akuli / porcupine

A decent editor written in tkinter
MIT License
161 stars 46 forks source link

As I'm typing a new document, dont let me get to the bottom of the screen before scolling #5

Open carlbordum opened 7 years ago

carlbordum commented 7 years ago

The cursor should be kept 4 or 5 lines above the bottom imo

Akuli commented 7 years ago

Currently I just let tkinter do whatever it wants with this. I'll probably fix this tomorrow.

Akuli commented 7 years ago

What kind of cursor movement would you like? When you go near the bottom should it scroll down line by line or half a page or so at a time? I checked how different editors do this. Vim and Geany work like porcupine works now, nano and emacs scroll down half a page at a time.

carlbordum commented 7 years ago

When you press enter: create new line if cursor is at one of the four last lines, also scroll one line

Akuli commented 7 years ago

So far I haven't seen any other editors actually do this. Can you name one? If you haven't it's OK and porcupine can still do this.

carlbordum commented 7 years ago

My own setup (Vim) does this. Am not sure where the option is, though.

Akuli commented 7 years ago

Vim didn't do that to me with default settings. I'll implement this some day :)

Akuli commented 7 years ago

What do you think?

import functools
import tkinter as tk
import tkinter.font as tkfont

class Zaab1tText(tk.Text):

    # see() is supposed to scroll the widget so that the index is
    # visible, so it makes sense to override it for this
    @functools.wraps(tk.Text.see)
    def see(self, index):
        # do whatever tkinter does by default
        super().see(index)

        # self['height'] is wrong if the widget has been stretched or
        # squeezed by pack or grid
        font = tkfont.Font(name=self['font'], exists=True)
        rowcount = self.winfo_height() // font.metrics('linespace')

        # make sure that we can also see n lines on either side, usually
        # n is 5 but it's smaller if the text widget is too big for 5
        space_on_each_side = (rowcount-1) // 2    # -1 is the current row
        n = min(space_on_each_side, 5)
        super().see('%s + %d lines' % (index, n))
        super().see('%s - %d lines' % (index, n))

if __name__ == '__main__':
    root = tk.Tk()
    text = Zaab1tText(root)
    text.pack(fill='both', expand=True)      # make it stretchable
    text.bind('<Return>', (lambda event: text.after_idle(text.see, 'insert')))
    with open(__file__, 'r') as f:
        text.insert('1.0', f.read())
    root.mainloop()

This code doesn't require porcupine in any way. Just run it in a python with tkinter installed.

Akuli commented 6 years ago

Tkinter doesn't seem to support scrolling down further than the bottom of the file, so the only possible workaround is to add a bunch of fake newlines at the end of the text widget and scroll down with these. I'd be happy to proved wrong though. Sorry :(

carlbordum commented 6 years ago

Damn, I still think this would be nice. You can go to the bottom of a document in vim and press zz to kind of see what I mean.

Akuli commented 6 years ago

I just did that, and it did what I expected it to do... Even though I can't do this at the end of the file, would you like to keep a distance between the cursor and the bottom of the view elsewhere? That's doable if there are lines to go between the cursor and the bottom of the text widget.

Akuli commented 6 years ago

@Zaab1t can you try the latest master? I added some magic to fix this and I think it's definitely a lot better than before. You'll see it if you navigate up and down with arrow keys in the middle of a long file.

Akuli commented 6 years ago
<Zaab1t> this si nice
<Akuli> go to the very end of the file
<Akuli> you'll notice that it doesn't scroll past the end
<Akuli> does that matter so much that i can't close ur github issue?
<Zaab1t> I like this
<Zaab1t> I haven't seen an editor with this feature
<Zaab1t> I still feel like typing at the end would be better WITH this
<Zaab1t> great work!
<Akuli> thanks :)
Akuli commented 4 years ago

reopening because i have an idea for making it scroll past the end: add bottom margin to text widget (if possible)

Akuli commented 4 years ago

related to #37