From 48159c16b58af959555ced5cbd510835db5ea17b Mon Sep 17 00:00:00 2001 From: Gregory Heytings Date: Thu, 13 Jan 2022 10:31:43 +0200 Subject: [PATCH] Undelete deleted frames. * lisp/frame.el (undelete-frame): New command. (undelete-frame--handle-delete-frame): New auxiliary function. (undelete-frame--deleted-frames): New auxiliary variables. (undelete-frame-mode): New minor mode. (ctl-x-5-map): Bind the new command. * etc/NEWS: Document the new command and minor mode. * src/frame.c (Fdelete_frame): Update docstring, and mention the minor mode. * lisp/menu-bar.el (menu-bar-file-menu): Add an entry for the new command. * doc/emacs/frames.tex (Frame Commands): Document the new command and minor mode. See bug#51883. --- doc/emacs/frames.texi | 10 ++++++ etc/NEWS | 14 ++++++-- lisp/frame.el | 75 +++++++++++++++++++++++++++++++++++++++++++ lisp/menu-bar.el | 9 ++++++ src/frame.c | 5 ++- 5 files changed, 110 insertions(+), 3 deletions(-) diff --git a/doc/emacs/frames.texi b/doc/emacs/frames.texi index ce43408101d..c641b8ccb14 100644 --- a/doc/emacs/frames.texi +++ b/doc/emacs/frames.texi @@ -512,6 +512,16 @@ frames by specifying @dfn{frame parameters}. @xref{Frame Parameters}. Delete the selected frame (@code{delete-frame}). This signals an error if there is only one frame. +@item C-x 5 u +@kindex C-x 5 u +@findex undelete-frame +@findex undelete-frame-mode +When @code{undelete-frame-mode} is enabled, undelete one of the 16 +most recently deleted frames. Without a prefix argument, undelete the +most recently deleted frame. With a numerical prefix argument between +1 and 16, where 1 is the most recently deleted frame, undelete the +corresponding deleted frame. + @item C-z @kindex C-z @r{(X windows)} Minimize (or iconify) the selected Emacs frame diff --git a/etc/NEWS b/etc/NEWS index 8bd74e6453e..6df77624a27 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -273,10 +273,15 @@ height use 'window-height' in combination with 'body-lines'. *** 'other-window-scroll-default' can define the other window to scroll. -** Rcirc +** Frames +++ -*** New command 'rcirc-when'. +*** Deleted frames can now be undeleted. +The 16 most recently deleted frames can be undeleted with 'C-x 5 u' when +'undelete-frame-mode' is enabled. Without a prefix argument, undelete +the most recently deleted frame. With a numerical prefix argument +between 1 and 16, where 1 is the most recently deleted frame, undelete +the corresponding deleted frame. ** Tab Bars and Tab Lines @@ -309,6 +314,11 @@ The Emacs server will be automatically stopped when certain conditions are met. The conditions are given by the argument, which can be 'empty', 'delete-frame' or 'kill-terminal'. +** Rcirc + ++++ +*** New command 'rcirc-when'. + * Editing Changes in Emacs 29.1 --- diff --git a/lisp/frame.el b/lisp/frame.el index 62b73f3157b..599ffe591a5 100644 --- a/lisp/frame.el +++ b/lisp/frame.el @@ -2529,6 +2529,80 @@ deleting them." (if iconify (iconify-frame this) (delete-frame this))) (setq this next)))) +(eval-when-compile (require 'frameset)) + +(defvar undelete-frame--deleted-frames nil + "Internal variable used by `undelete-frame--handle-delete-frame'.") + +(defun undelete-frame--handle-delete-frame (frame) + "Save the configuration of frames deleted with `delete-frame'. +Only the 16 most recently deleted frames are saved." + (when (frame-live-p frame) + (setq undelete-frame--deleted-frames + (cons + (cons + (display-graphic-p) + (frameset-save + (list frame) + ;; When the daemon is started from a graphical + ;; environment, TTY frames have a 'display' parameter set + ;; to the value of $DISPLAY (see the note in + ;; `server--on-display-p'). Do not store that parameter + ;; in the frameset, otherwise `frameset-restore' attempts + ;; to restore a graphical frame. + :filters (if (display-graphic-p) + frameset-filter-alist + (cons '(display . :never) + frameset-filter-alist)))) + undelete-frame--deleted-frames)) + (if (> (length undelete-frame--deleted-frames) 16) + (setq undelete-frame--deleted-frames + (butlast undelete-frame--deleted-frames))))) + +(define-minor-mode undelete-frame-mode + "Enable the `undelete-frame' command." + :group 'frames + :global t + (if undelete-frame-mode + (add-hook 'delete-frame-functions + #'undelete-frame--handle-delete-frame -75) + (remove-hook 'delete-frame-functions + #'undelete-frame--handle-delete-frame) + (setq undelete-frame--deleted-frames nil))) + +(defun undelete-frame (&optional arg) + "Undelete a frame deleted with `delete-frame'. +Without a prefix argument, undelete the most recently deleted +frame. +With a numerical prefix argument ARG between 1 and 16, where 1 is +most recently deleted frame, undelete the ARGth deleted frame. +When called from Lisp, returns the new frame." + (interactive "P") + (if (not undelete-frame-mode) + (user-error "Undelete-Frame mode is disabled") + (if (consp arg) + (user-error "Missing deleted frame number argument") + (let* ((number (pcase arg ('nil 1) ('- -1) (_ arg))) + (frames (frame-list)) + (frameset (nth (1- number) undelete-frame--deleted-frames)) + (graphic (display-graphic-p))) + (if (not (<= 1 number 16)) + (user-error "%d is not a valid deleted frame number argument" + number) + (if (not frameset) + (user-error "No deleted frame with number %d" number) + (if (not (eq graphic (car frameset))) + (user-error + "Cannot undelete a %s display frame on a %s display" + (if graphic "non-graphic" "graphic") + (if graphic "graphic" "non-graphic")) + (setq undelete-frame--deleted-frames + (delq frameset undelete-frame--deleted-frames)) + (frameset-restore (cdr frameset)) + (let ((frame (car (seq-difference (frame-list) frames)))) + (when frame + (select-frame-set-input-focus frame) + frame))))))))) ;;; Window dividers. (defgroup window-divider nil @@ -2873,6 +2947,7 @@ See also `toggle-frame-maximized'." (define-key ctl-x-5-map "o" #'other-frame) (define-key ctl-x-5-map "5" #'other-frame-prefix) (define-key ctl-x-5-map "c" #'clone-frame) +(define-key ctl-x-5-map "u" #'undelete-frame) (define-key global-map [f11] #'toggle-frame-fullscreen) (define-key global-map [(meta f10)] #'toggle-frame-maximized) (define-key esc-map [f10] #'toggle-frame-maximized) diff --git a/lisp/menu-bar.el b/lisp/menu-bar.el index e0cf6c588c4..3e00dca6f82 100644 --- a/lisp/menu-bar.el +++ b/lisp/menu-bar.el @@ -109,6 +109,15 @@ (bindings--define-key menu [separator-tab] menu-bar-separator)) + (bindings--define-key menu [enable-undelete-frame-mode] + '(menu-item "Enable Frame Undeletion" undelete-frame-mode + :visible (null undelete-frame-mode) + :help "Enable frame undeletion for this session")) + (bindings--define-key menu [undelete-last-deleted-frame] + '(menu-item "Undelete Frame" undelete-frame + :visible undelete-frame-mode + :help "Undelete the most recently deleted frame")) + ;; Don't use delete-frame as event name because that is a special ;; event. (bindings--define-key menu [delete-this-frame] diff --git a/src/frame.c b/src/frame.c index c0f4f3ecde3..e5d74edc168 100644 --- a/src/frame.c +++ b/src/frame.c @@ -2382,9 +2382,12 @@ delete_frame (Lisp_Object frame, Lisp_Object force) } DEFUN ("delete-frame", Fdelete_frame, Sdelete_frame, 0, 2, "", - doc: /* Delete FRAME, permanently eliminating it from use. + doc: /* Delete FRAME, eliminating it from use. FRAME must be a live frame and defaults to the selected one. +When `undelete-frame-mode' is enabled, the 16 most recently deleted +frames can be undeleted with `undelete-frame', which see. + A frame may not be deleted if its minibuffer serves as surrogate minibuffer for another frame. Normally, you may not delete a frame if all other frames are invisible, but if the second optional argument -- 2.39.2