Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undo last closed tab #309

Closed
Moult opened this issue May 31, 2024 · 7 comments
Closed

Undo last closed tab #309

Moult opened this issue May 31, 2024 · 7 comments

Comments

@Moult
Copy link
Contributor

Moult commented May 31, 2024

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
Copy link
Owner

rkd77 commented May 31, 2024

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

@Moult
Copy link
Contributor Author

Moult commented May 31, 2024

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
Copy link
Owner

rkd77 commented May 31, 2024

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
Copy link
Contributor Author

Moult commented Jun 3, 2024

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

@rkd77
Copy link
Owner

rkd77 commented Jun 3, 2024

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

rkd77 added a commit that referenced this issue Sep 1, 2024
The uri of the latest closed tab uri is remembered in struct terminal.
And when the reopen-last-closed-tab action is executed, then uri
is open in new tab in foreground. Only one is in memory.
If you close two tabs, only on one of them can be reopened.

No key is assigned to this action by default.
@rkd77
Copy link
Owner

rkd77 commented Sep 1, 2024

@Moult check this code ^. You must assign key to reopen-last-closed-tab action or run it in exmode.

@Moult
Copy link
Contributor Author

Moult commented Dec 6, 2024

Thanks, sorry for the very late reply. I can confirm that with the latest commit the function works as expected. Awesome! I've bound it to key 'u'.

@Moult Moult closed this as completed Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants