rkd77 / elinks

Fork of elinks
Other
317 stars 34 forks source link

Undo last closed tab #309

Open Moult opened 1 month ago

Moult commented 1 month ago

Is there a way to undo the last closed tab? I sometimes accidentally hit the "close tab" hotkey by mistake. Is there a way to reopen the tab?

rkd77 commented 1 month ago

You can press 'h' to see History of visited pages.

Moult commented 1 month ago

That's useful, but the tab I close might be so far down the history list that I don't know which one it is. Is there another way to know?

rkd77 commented 1 month ago
import elinks
import fcntl
import termios
import os
import sys

latest_closed_tab_url = ''

def try_to_close_tab():
    """Save url when closing tab."""
    global latest_closed_tab_url
    latest_closed_tab_url = elinks.current_url()
    with open('/dev/stdin', 'w') as fd:
        for ch in "c": # tab-close
            try:
                fcntl.ioctl(fd, termios.TIOCSTI, ch)
            except Exception as e:
                sys.stderr.write(str(e)+' '+ch+"\n")

def try_goto_url():
    """Go to latest closed tab"""
    global latest_closed_tab_url
    with open('/dev/stdin', 'w') as fd:
        for ch in ":goto-url " + latest_closed_tab_url + "\n": # exmode retype url
            try:
               fcntl.ioctl(fd, termios.TIOCSTI, ch)
            except Exception as e:
               sys.stderr.write(str(e)+' '+ch+"\n")

#def copy_url():
#    """ Copy current url """
#    os.system('clear')
#    print(elinks.current_url())

elinks.bind_key("F5", try_to_close_tab)
elinks.bind_key("F6", try_goto_url)

Maybe it can be simpler. Save this file as ~/.config/elinks/hooks.py . Requires python scripting and exmode. F5 Saves url and type 'c', that's mean close tab. F6 Restores saved url. It types ':goto-url ' ans saved url

I don't think that adding "undo closed tab" will be really useful in elinks code.

Moult commented 1 month ago

I get Errno 5] Input/output error c when I press F5 :(

rkd77 commented 1 month ago

Recent kernels due to security reasons have disabled TIOCSTI. You can set: dev.tty.legacy_tiocsti=1 in /etc/sysctl.conf