diff options
author | Martin Rudalics <rudalics@gmx.at> | 2017-06-25 11:33:25 +0200 |
---|---|---|
committer | Martin Rudalics <rudalics@gmx.at> | 2017-06-25 11:33:25 +0200 |
commit | 1886246f6f40b8c376467f71605141035959cae0 (patch) | |
tree | 74bf775484482a19c182d1c6ad2d984a8c610eb1 /lisp | |
parent | 431471376440a69a2f5ca2a49e3c7e09c834c132 (diff) | |
download | emacs-1886246f6f40b8c376467f71605141035959cae0.tar.gz |
Provide additional support for child frames
Provide mouse dragging and resizing of frames. Allow resizing
frames proportionally. Provide additional functionality for
child frames. Minor bug fixes.
* lisp/frame.el (frame-border-width, frame-pixel-width)
(frame-pixel-height): Alias to `frame-internal-border-width',
`frame-native-width' and `frame-native-height'.
(frame-inner-width, frame-inner-height, frame-outer-width)
(frame-outer-height): New functions.
* lisp/minibuffer.el (completion-auto-help): Fix typo.
* lisp/mouse.el (mouse-drag-line, mouse-drag-mode-line)
(mouse-drag-header-line): Allow moving a frame by dragging the
mode line of its bottommost window (on a minibuffer-less frame)
or the header line of its topmost window.
(mouse-drag-vertical-line): Mention argument in doc-string.
(mouse-resize-frame, mouse-drag-frame, mouse-drag-left-edge)
(mouse-drag-top-left-corner, mouse-drag-top-edge)
(mouse-drag-top-right-corner, mouse-drag-right-edge)
(mouse-drag-bottom-right-corner, mouse-drag-bottom-edge)
(mouse-drag-bottom-left-corner): New functions for resizing a
frame by dragging its internal border together with
corresponding key bindings.
* lisp/tooltip.el (tooltip-frame-parameters): Add
'no-special-glyphs' to default parameters and update version
tag.
* lisp/window.el (frame-auto-hide-function): Add choice to make
frame invisible and update version tag.
(window--delete): Handle 'auto-hide-function' frame parameter.
(window--maybe-raise-frame): Respect 'no-focus-on-map' and
'no-accept-focus' frame parameters.
(display-buffer--action-function-custom-type): Add
`display-buffer-in-child-frame'.
(display-buffer): Mention `display-buffer-in-child-frame' in
doc-string.
(display-buffer-in-child-frame): New action function for
`display-buffer'.
(window--sanitize-margin): Return zero when MARGIN cannot be
sanitized.
(fit-frame-to-buffer): Major rewrite to handle child frames and
'fit-frame-to-buffer-sizes' and 'fit-frame-to-buffer-margins'
frame parameters.
(window-largest-empty-rectangle--maximums-1)
(window-largest-empty-rectangle--maximums)
(window-largest-empty-rectangle--disjoint-maximums)
(window-largest-empty-rectangle): New functions.
* src/dispextern.h (WINDOW_WANTS_MODELINE_P)
(WINDOW_WANTS_HEADER_LINE_P): Remove. Functionality is now
provided by corresponding functions window_wants_modeline and
window_wants_header_line in window.c. Adjust users.
* src/dispnew.c (adjust_glyph_matrix)
(buffer_posn_from_coords): Use window_wants_modeline and
window_wants_header_line instead of WINDOW_WANTS_MODELINE_P and
WINDOW_WANTS_HEADER_LINE_P.
* src/frame.c (keep_ratio): New function.
(adjust_frame_size): Call keep_ratio for each of F's child
frames.
(make_frame): Initialize no_special_glyphs slot.
(frame_internal_border_part): New function.
(Fframe_pixel_width, Fframe_pixel_height, Fborder_width): Rename
to Fframe_native_width, Fframe_native_height mand
Fframe_internal_border_width.
(frame_parm_table): Add Qno_special_glyphs entry.
(frame_float_type): New enumeration type.
(frame_float): New function to handle frame size and position
ratios.
(x_set_frame_parameters): Handle size and position ratios.
(x_set_no_special_glyphs): New function
(x_figure_window_size): Handle size and position ratios.
(syms_of_frame): Add Qdisplay_monitor_attributes_list,
Qno_special_glyphs, Qframe_edges, Qkeep_ratio, Qwidth_only,
Qheight_only, Qleft_only and Qtop_only.
* src/frame.h (internal_border_part): New enumeration type.
(struct frame): New slot no_special_glyphs.
(FRAME_NO_SPECIAL_GLYPHS): New macro.
* src/gtkutil.c (xg_frame_restack): Return immediately for
GTK versions before 2.18.0.
* src/keyboard.c (internal_border_parts): New array constant.
(make_lispy_position): For frames with border dragging enabled
return internal border part.
(syms_of_keyboard): New symbols Qdrag_internal_border,
Qleft_edge, Qtop_left_corner, Qtop_edge, Qtop_right_corner,
Qright_edge, Qbottom_right_corner, Qbottom_edge and
Qbottom_left_corner.
* src/minibuf.c (read_minibuf_unwind): When exiting the
minibuffer deal with frames that have the 'minibuffer-exit'
parameter set.
(syms_of_minibuf): New symbol Qminibuffer_exit.
* src/nsfns.m (frame_parm_handler): Add entry for
x_set_no_special_glyphs.
(Fx_create_frame): Handle 'no-special-glyphs' parameter.
Intitialize new cursor types for dragging frame borders.
* src/nsterm.h (struct ns_output): Add new cursor types for
dragging frame borders.
* src/w32fns.c (w32_frame_parm_handlers): Add entry for
x_set_no_special_glyphs.
(Fx_create_frame): Handle 'no-special-glyphs' parameter.
Intitialize new cursor types for dragging frame borders.
* src/w32term.h (struct w32_output): Add new cursor types for
dragging frame borders.
* src/window.c (coordinates_in_window)
(Fwindow_line_height, window_internal_height): Use
window_wants_modeline and window_wants_header_line instead of
WINDOW_WANTS_MODELINE_P and WINDOW_WANTS_HEADER_LINE_P.
(Fwindow_lines_pixel_dimensions): New function.
(window_parameter): New function.
(Fwindow_parameter): Call window_parameter.
(window_wants_mode_line, window_wants_header_line): New
functions replacing the macros WINDOW_WANTS_MODELINE_P and
WINDOW_WANTS_HEADER_LINE_P from dispextern.h.
(syms_of_window): New symbols Qmode_line_format and
Qheader_line_format.
* src/window.h: Reorganize and re-comment macros. Use
window_wants_modeline and window_wants_header_line instead of
WINDOW_WANTS_MODELINE_P and WINDOW_WANTS_HEADER_LINE_P.
(MINI_NON_ONLY_WINDOW_P, MINI_ONLY_WINDOW_P): Minor rewrite.
(WINDOW_BUFFER): New macro.
(WINDOW_BOX_LEFT_EDGE_COL, WINDOW_BOX_RIGHT_EDGE_COL): Remove.
* src/xdisp.c (window_text_bottom_y, window_box_height)
(window_box, start_display)
(compute_window_start_on_continuation_line)
(try_cursor_movement, redisplay_window)
(try_window_reusing_current_matrix, try_window_id)
(display_line, expose_window): Use window_wants_modeline and
window_wants_header_line instead of WINDOW_WANTS_MODELINE_P and
WINDOW_WANTS_HEADER_LINE_P.
(pos_visible_p, display_mode_lines): Respect W's
'mode-line-format' and 'header-line-format' window parameters.
(init_iterator): Use window_wants_modeline and
window_wants_header_line instead of WINDOW_WANTS_MODELINE_P and
WINDOW_WANTS_HEADER_LINE_P. For tip frames respect
no_special_glyphs value.
(note_mouse_highlight): Set frame border cursors when on
internal border.
(x_draw_right_divider, x_draw_bottom_divider): Try to improve
drawing of window dividers.
* src/xfns.c (mouse_cursor): Add entries for border parts.
(mouse_cursor_types): Add entries for cursor types to drag
frame borders.
(INSTALL_CURSOR): Add entries for new cursor types to drag
frame borders.
(Fx_create_frame): Handle 'no-special-glyphs' parameter.
(x_frame_parm_handlers): Add entry for
x_set_no_special_glyphs.
(Vx_window_left_edge_shape, Vx_window_top_left_corner_shape)
(Vx_window_top_edge_shape, Vx_window_top_right_corner_shape)
(Vx_window_right_edge_shape)
(Vx_window_bottom_right_corner_shape)
(Vx_window_bottom_edge_shape)
(Vx_window_bottom_left_corner_shape): New variables.
(x_frame_restack): Call xg_frame_restack only for GTK versions
starting with 2.18.0.
* src/xterm.c (x_free_frame_resources): Remove new cursors for
dragging frame borders.
* src/xterm.h (struct x_output): Add new cursor types for
dragging frame borders.
* doc/lispref/display.texi (Size of Displayed Text): Document
`window-lines-pixel-dimensions'.
* doc/lispref/elisp.texi (Top): Add entry for "Mouse Dragging
Parameters".
* doc/lispref/frames.texi (Frame Size): Replace
frame-pixel-width/-height by frame-native-width/-height. Add
frame-inner-width/-height and frame-outer-width/-height docs.
(Position Parameters): Describe specifying position as ratios.
Clarify remark about positions relative to bottom/ridge display
edge.
(Size Parameters): Describe specifying sizes as ratios.
Describe 'fit-frame-to-buffer-margins' and
'fit-frame-to-buffer-sizes' parameters.
(Layout Parameters): Describe 'no-special-glyphs' parameter.
(Frame Interaction Parameters): Describe 'auto-hide-function',
'minibuffer-exit' and 'keep-ratio' parameters.
(Mouse Dragging Parameters): New section describing
'drag-internal-border', 'drag-with-header-line',
'drag-with-mode-line', 'snap-width', 'top-visible' and
'bottom-visible' parameters.
(Management Parameters): Mention that `override-redirect' has
no effect on MS Windows.
(Font and Color Parameters): Mention child frames for `alpha'
parameter.
(Child Frames): Rewrite section with description and cross
references to new frame parameters added.
* doc/lispref/modes.texi (Mode Line Basics): Mention
'mode-line-format' and 'header-line-format' window parameters.
* doc/lispref/windows.texi (Resizing Windows): Mention effect
of `fit-frame-to-buffer-margins' for child frames.
(Display Action Functions): New action function
`display-buffer-in-child-frame'.
(Quitting Windows): Mention `make-frame-invisible' as optional
value of `frame-auto-hide-function' and `auto-hide-function'
frame paameter.
(Coordinates and Windows): Describe new function
`window-largest-empty-rectangle'.
(Window Parameters): Describe new parameters 'mode-line-format'
and 'header-line-format'. Index all window parameters described
in this section.
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/frame.el | 32 | ||||
-rw-r--r-- | lisp/minibuffer.el | 2 | ||||
-rw-r--r-- | lisp/mouse.el | 433 | ||||
-rw-r--r-- | lisp/tooltip.el | 6 | ||||
-rw-r--r-- | lisp/window.el | 615 |
5 files changed, 865 insertions, 223 deletions
diff --git a/lisp/frame.el b/lisp/frame.el index b7a55169281..b54df6fa160 100644 --- a/lisp/frame.el +++ b/lisp/frame.el @@ -1110,6 +1110,38 @@ differing font heights." If FRAME is omitted, describe the currently selected frame." (cdr (assq 'width (frame-parameters frame)))) +(defalias 'frame-border-width 'frame-internal-border-width) +(defalias 'frame-pixel-width 'frame-native-width) +(defalias 'frame-pixel-height 'frame-native-height) + +(defun frame-inner-width (&optional frame) + "Return inner width of FRAME in pixels. +FRAME defaults to the selected frame." + (setq frame (window-normalize-frame frame)) + (- (frame-native-width frame) + (* 2 (frame-internal-border-width frame)))) + +(defun frame-inner-height (&optional frame) + "Return inner height of FRAME in pixels. +FRAME defaults to the selected frame." + (setq frame (window-normalize-frame frame)) + (- (frame-native-height frame) + (* 2 (frame-internal-border-width frame)))) + +(defun frame-outer-width (&optional frame) + "Return outer width of FRAME in pixels. +FRAME defaults to the selected frame." + (setq frame (window-normalize-frame frame)) + (let ((edges (frame-edges frame 'outer-edges))) + (- (nth 2 edges) (nth 0 edges)))) + +(defun frame-outer-height (&optional frame) + "Return outer height of FRAME in pixels. +FRAME defaults to the selected frame." + (setq frame (window-normalize-frame frame)) + (let ((edges (frame-edges frame 'outer-edges))) + (- (nth 3 edges) (nth 1 edges)))) + (declare-function x-list-fonts "xfaces.c" (pattern &optional face frame maximum width)) diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index c3480cd6c64..e5b1029c01f 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -746,7 +746,7 @@ If the current buffer is not a minibuffer, erase its entire contents." (defcustom completion-auto-help t "Non-nil means automatically provide help for invalid completion input. -If the value is t the *Completion* buffer is displayed whenever completion +If the value is t the *Completions* buffer is displayed whenever completion is requested but cannot be done. If the value is `lazy', the *Completions* buffer is only displayed after the second failed attempt to complete." diff --git a/lisp/mouse.el b/lisp/mouse.el index 9b6b169e568..e0794435d7a 100644 --- a/lisp/mouse.el +++ b/lisp/mouse.el @@ -380,7 +380,7 @@ This command must be bound to a mouse click." (defun mouse-drag-line (start-event line) "Drag a mode line, header line, or vertical line with the mouse. -START-EVENT is the starting mouse-event of the drag action. LINE +START-EVENT is the starting mouse event of the drag action. LINE must be one of the symbols `header', `mode', or `vertical'." ;; Give temporary modes such as isearch a chance to turn off. (run-hooks 'mouse-leave-buffer-hook) @@ -405,29 +405,15 @@ must be one of the symbols `header', `mode', or `vertical'." ;; window's edge we drag. (cond ((eq line 'header) - (if (window-at-side-p window 'top) - ;; We can't drag the header line of a topmost window. - (setq draggable nil) - ;; Drag bottom edge of window above the header line. - (setq window (window-in-direction 'above window t)))) - ((eq line 'mode) - (if (and (window-at-side-p window 'bottom) - ;; Allow resizing the minibuffer window if it's on the - ;; same frame as and immediately below `window', and it's - ;; either active or `resize-mini-windows' is nil. - (let ((minibuffer-window (minibuffer-window frame))) - (not (and (eq (window-frame minibuffer-window) frame) - (or (not resize-mini-windows) - (eq minibuffer-window - (active-minibuffer-window))))))) - (setq draggable nil))) + ;; Drag bottom edge of window above the header line. + (setq window (window-in-direction 'above window t))) + ((eq line 'mode)) ((eq line 'vertical) (let ((divider-width (frame-right-divider-width frame))) (when (and (or (not (numberp divider-width)) (zerop divider-width)) (eq (frame-parameter frame 'vertical-scroll-bars) 'left)) (setq window (window-in-direction 'left window t)))))) - (let* ((exitfun nil) (move (lambda (event) (interactive "e") @@ -530,20 +516,405 @@ must be one of the symbols `header', `mode', or `vertical'." t (lambda () (setq track-mouse old-track-mouse))))))) (defun mouse-drag-mode-line (start-event) - "Change the height of a window by dragging on the mode line." + "Change the height of a window by dragging on its mode line. +START-EVENT is the starting mouse event of the drag action. + +If the drag happens in a mode line on the bottom of a frame and +that frame's `drag-with-mode-line' parameter is non-nil, drag the +frame instead." (interactive "e") - (mouse-drag-line start-event 'mode)) + (let* ((start (event-start start-event)) + (window (posn-window start)) + (frame (window-frame window))) + (cond + ((not (window-live-p window))) + ((or (not (window-at-side-p window 'bottom)) + ;; Allow resizing the minibuffer window if it's on the + ;; same frame as and immediately below `window', and it's + ;; either active or `resize-mini-windows' is nil. + (let ((minibuffer-window (minibuffer-window frame))) + (and (eq (window-frame minibuffer-window) frame) + (or (not resize-mini-windows) + (eq minibuffer-window + (active-minibuffer-window)))))) + (mouse-drag-line start-event 'mode)) + ((and (frame-parameter frame 'drag-with-mode-line) + (window-at-side-p window 'bottom) + (let ((minibuffer-window (minibuffer-window frame))) + (not (eq (window-frame minibuffer-window) frame)))) + ;; Drag frame when the window is on the bottom of its frame and + ;; there is no minibuffer window below. + (mouse-drag-frame start-event 'move))))) (defun mouse-drag-header-line (start-event) - "Change the height of a window by dragging on the header line." + "Change the height of a window by dragging on its header line. +START-EVENT is the starting mouse event of the drag action. + +If the drag happens in a header line on the top of a frame and +that frame's `drag-with-header-line' parameter is non-nil, drag +the frame instead." (interactive "e") - (mouse-drag-line start-event 'header)) + (let* ((start (event-start start-event)) + (window (posn-window start))) + (if (and (window-live-p window) + (not (window-at-side-p window 'top))) + (mouse-drag-line start-event 'header) + (let ((frame (window-frame window))) + (when (frame-parameter frame 'drag-with-header-line) + (mouse-drag-frame start-event 'move)))))) (defun mouse-drag-vertical-line (start-event) - "Change the width of a window by dragging on the vertical line." + "Change the width of a window by dragging on a vertical line. +START-EVENT is the starting mouse event of the drag action." (interactive "e") (mouse-drag-line start-event 'vertical)) +(defun mouse-resize-frame (frame x-diff y-diff &optional x-move y-move) + "Helper function for `mouse-drag-frame'." + (let* ((frame-x-y (frame-position frame)) + (frame-x (car frame-x-y)) + (frame-y (cdr frame-x-y)) + alist) + (if (> x-diff 0) + (when x-move + (setq x-diff (min x-diff frame-x)) + (setq x-move (- frame-x x-diff))) + (let* ((min-width (frame-windows-min-size frame t nil t)) + (min-diff (max 0 (- (frame-inner-width frame) min-width)))) + (setq x-diff (max x-diff (- min-diff))) + (when x-move + (setq x-move (+ frame-x (- x-diff)))))) + + (if (> y-diff 0) + (when y-move + (setq y-diff (min y-diff frame-y)) + (setq y-move (- frame-y y-diff))) + (let* ((min-height (frame-windows-min-size frame nil nil t)) + (min-diff (max 0 (- (frame-inner-height frame) min-height)))) + (setq y-diff (max y-diff (- min-diff))) + (when y-move + (setq y-move (+ frame-y (- y-diff)))))) + + (unless (zerop x-diff) + (when x-move + (push `(left . ,x-move) alist)) + (push `(width . (text-pixels . ,(+ (frame-text-width frame) x-diff))) + alist)) + (unless (zerop y-diff) + (when y-move + (push `(top . ,y-move) alist)) + (push `(height . (text-pixels . ,(+ (frame-text-height frame) y-diff))) + alist)) + (when alist + (modify-frame-parameters frame alist)))) + +(defun mouse-drag-frame (start-event part) + "Drag a frame or one of its edges with the mouse. +START-EVENT is the starting mouse event of the drag action. Its +position window denotes the frame that will be dragged. + +PART specifies the part that has been dragged and must be one of +the symbols 'left', 'top', 'right', 'bottom', 'top-left', +'top-right', 'bottom-left', 'bottom-right' to drag an internal +border or edge. If PART equals 'move', this means to move the +frame with the mouse." + ;; Give temporary modes such as isearch a chance to turn off. + (run-hooks 'mouse-leave-buffer-hook) + (let* ((echo-keystrokes 0) + (start (event-start start-event)) + (window (posn-window start)) + ;; FRAME is the frame to drag. + (frame (if (window-live-p window) + (window-frame window) + window)) + (width (frame-native-width frame)) + (height (frame-native-height frame)) + ;; PARENT is the parent frame of FRAME or, if FRAME is a + ;; top-level frame, FRAME's workarea. + (parent (frame-parent frame)) + (parent-edges + (if parent + `(0 0 ,(frame-native-width parent) ,(frame-native-height parent)) + (let* ((attributes + (car (display-monitor-attributes-list))) + (workarea (assq 'workarea attributes))) + (and workarea + `(,(nth 1 workarea) ,(nth 2 workarea) + ,(+ (nth 1 workarea) (nth 3 workarea)) + ,(+ (nth 2 workarea) (nth 4 workarea))))))) + (parent-left (and parent-edges (nth 0 parent-edges))) + (parent-top (and parent-edges (nth 1 parent-edges))) + (parent-right (and parent-edges (nth 2 parent-edges))) + (parent-bottom (and parent-edges (nth 3 parent-edges))) + ;; `pos-x' and `pos-y' record the x- and y-coordinates of the + ;; last sampled mouse position. Note that we sample absolute + ;; mouse positions to avoid that moving the mouse from one + ;; frame into another gets into our way. `last-x' and `last-y' + ;; records the x- and y-coordinates of the previously sampled + ;; position. The differences between `last-x' and `pos-x' as + ;; well as `last-y' and `pos-y' determine the amount the mouse + ;; has been dragged between the last two samples. + pos-x-y pos-x pos-y + (last-x-y (mouse-absolute-pixel-position)) + (last-x (car last-x-y)) + (last-y (cdr last-x-y)) + ;; `snap-x' and `snap-y' record the x- and y-coordinates of the + ;; mouse position when FRAME snapped. As soon as the + ;; difference between `pos-x' and `snap-x' (or `pos-y' and + ;; `snap-y') exceeds the value of FRAME's `snap-width' + ;; parameter, unsnap FRAME (at the respective side). `snap-x' + ;; and `snap-y' nil mean FRAME is curerntly not snapped. + snap-x snap-y + (exitfun nil) + (move + (lambda (event) + (interactive "e") + (when (consp event) + (setq pos-x-y (mouse-absolute-pixel-position)) + (setq pos-x (car pos-x-y)) + (setq pos-y (cdr pos-x-y)) + (cond + ((eq part 'left) + (mouse-resize-frame frame (- last-x pos-x) 0 t)) + ((eq part 'top) + (mouse-resize-frame frame 0 (- last-y pos-y) nil t)) + ((eq part 'right) + (mouse-resize-frame frame (- pos-x last-x) 0)) + ((eq part 'bottom) + (mouse-resize-frame frame 0 (- pos-y last-y))) + ((eq part 'top-left) + (mouse-resize-frame + frame (- last-x pos-x) (- last-y pos-y) t t)) + ((eq part 'top-right) + (mouse-resize-frame + frame (- pos-x last-x) (- last-y pos-y) nil t)) + ((eq part 'bottom-left) + (mouse-resize-frame + frame (- last-x pos-x) (- pos-y last-y) t)) + ((eq part 'bottom-right) + (mouse-resize-frame + frame (- pos-x last-x) (- pos-y last-y))) + ((eq part 'move) + (let* ((old-position (frame-position frame)) + (old-left (car old-position)) + (old-top (cdr old-position)) + (left (+ old-left (- pos-x last-x))) + (top (+ old-top (- pos-y last-y))) + right bottom + ;; `snap-width' (maybe also a yet to be provided + ;; `snap-height') could become floats to handle + ;; proportionality wrt PARENT. We don't do any + ;; checks on this parameter so far. + (snap-width (frame-parameter frame 'snap-width))) + ;; Docking and constraining. + (when (and (numberp snap-width) parent-edges) + (cond + ;; Docking at the left parent edge. + ((< pos-x last-x) + (cond + ((and (> left parent-left) + (<= (- left parent-left) snap-width)) + ;; Snap when the mouse moved leftward and + ;; FRAME's left edge would end up within + ;; `snap-width' pixels from PARENT's left edge. + (setq snap-x pos-x) + (setq left parent-left)) + ((and (<= left parent-left) + (<= (- parent-left left) snap-width) + snap-x (<= (- snap-x pos-x) snap-width)) + ;; Stay snapped when the mouse moved leftward + ;; but not more than `snap-width' pixels from + ;; the time FRAME snapped. + (setq left parent-left)) + (t + ;; Unsnap when the mouse moved more than + ;; `snap-width' pixels leftward from the time + ;; FRAME snapped. + (setq snap-x nil)))) + ((> pos-x last-x) + (setq right (+ left width)) + (cond + ((and (< right parent-right) + (<= (- parent-right right) snap-width)) + ;; Snap when the mouse moved rightward and + ;; FRAME's right edge would end up within + ;; `snap-width' pixels from PARENT's right edge. + (setq snap-x pos-x) + (setq left (- parent-right width))) + ((and (>= right parent-right) + (<= (- right parent-right) snap-width) + snap-x (<= (- pos-x snap-x) snap-width)) + ;; Stay snapped when the mouse moved rightward + ;; but not more more than `snap-width' pixels + ;; from the time FRAME snapped. + (setq left (- parent-right width))) + (t + ;; Unsnap when the mouse moved rightward more + ;; than `snap-width' pixels from the time FRAME + ;; snapped. + (setq snap-x nil))))) + + (cond + ((< pos-y last-y) + (cond + ((and (> top parent-top) + (<= (- top parent-top) snap-width)) + ;; Snap when the mouse moved upward and FRAME's + ;; top edge would end up within `snap-width' + ;; pixels from PARENT's top edge. + (setq snap-y pos-y) + (setq top parent-top)) + ((and (<= top parent-top) + (<= (- parent-top top) snap-width) + snap-y (<= (- snap-y pos-y) snap-width)) + ;; Stay snapped when the mouse moved upward but + ;; not more more than `snap-width' pixels from + ;; the time FRAME snapped. + (setq top parent-top)) + (t + ;; Unsnap when the mouse moved upward more than + ;; `snap-width' pixels from the time FRAME + ;; snapped. + (setq snap-y nil)))) + ((> pos-y last-y) + (setq bottom (+ top height)) + (cond + ((and (< bottom parent-bottom) + (<= (- parent-bottom bottom) snap-width)) + ;; Snap when the mouse moved downward and + ;; FRAME's bottom edge would end up within + ;; `snap-width' pixels from PARENT's bottom + ;; edge. + (setq snap-y pos-y) + (setq top (- parent-bottom height))) + ((and (>= bottom parent-bottom) + (<= (- bottom parent-bottom) snap-width) + snap-y (<= (- pos-y snap-y) snap-width)) + ;; Stay snapped when the mouse moved downward + ;; but not more more than `snap-width' pixels + ;; from the time FRAME snapped. + (setq top (- parent-bottom height))) + (t + ;; Unsnap when the mouse moved downward more + ;; than `snap-width' pixels from the time FRAME + ;; snapped. + (setq snap-y nil)))))) + + ;; If requested, constrain FRAME's draggable areas to + ;; PARENT's edges. The `top-visible' parameter should + ;; be set when FRAME has a draggable header-line. If + ;; set to a number, it ascertains that the top of + ;; FRAME is always constrained to the top of PARENT + ;; and that at least as many pixels of FRAME as + ;; specified by that number are visible on each of the + ;; three remaining sides of PARENT. + ;; + ;; The `bottom-visible' parameter should be set when + ;; FRAME has a draggable mode-line. If set to a + ;; number, it ascertains that the bottom of FRAME is + ;; always constrained to the bottom of PARENT and that + ;; at least as many pixels of FRAME as specified by + ;; that number are visible on each of the three + ;; remaining sides of PARENT. + (let ((par (frame-parameter frame 'top-visible)) + bottom-visible) + (unless par + (setq par (frame-parameter frame 'bottom-visible)) + (setq bottom-visible t)) + (when (and (numberp par) parent-edges) + (setq left + (max (min (- parent-right par) left) + (+ (- parent-left width) par))) + (setq top + (if bottom-visible + (min (max top (- parent-top (- height par))) + (- parent-bottom height)) + (min (max top parent-top) + (- parent-bottom par)))))) + + ;; Use `modify-frame-parameters' since `left' and + ;; `top' may want to move FRAME out of its PARENT. + (modify-frame-parameters + frame + `((left . (+ ,left)) (top . (+ ,top))))))) + (setq last-x pos-x) + (setq last-y pos-y)))) + (old-track-mouse track-mouse)) + ;; Start tracking. The special value 'dragging' signals the + ;; display engine to freeze the mouse pointer shape for as long + ;; as we drag. + (setq track-mouse 'dragging) + ;; Loop reading events and sampling the position of the mouse. + (setq exitfun + (set-transient-map + (let ((map (make-sparse-keymap))) + (define-key map [switch-frame] #'ignore) + (define-key map [select-window] #'ignore) + (define-key map [scroll-bar-movement] #'ignore) + (define-key map [mouse-movement] move) + ;; Swallow drag-mouse-1 events to avoid selecting some other window. + (define-key map [drag-mouse-1] + (lambda () (interactive) (funcall exitfun))) + ;; Some of the events will of course end up looked up + ;; with a mode-line, header-line or vertical-line prefix ... + (define-key map [mode-line] map) + (define-key map [header-line] map) + (define-key map [vertical-line] map) + ;; ... and some maybe even with a right- or bottom-divider + ;; prefix. + (define-key map [right-divider] map) + (define-key map [bottom-divider] map) + map) + t (lambda () (setq track-mouse old-track-mouse)))))) + +(defun mouse-drag-left-edge (start-event) + "Drag left edge of a frame with the mouse. +START-EVENT is the starting mouse event of the drag action." + (interactive "e") + (mouse-drag-frame start-event 'left)) + +(defun mouse-drag-top-left-corner (start-event) + "Drag top left corner of a frame with the mouse. +START-EVENT is the starting mouse event of the drag action." + (interactive "e") + (mouse-drag-frame start-event 'top-left)) + +(defun mouse-drag-top-edge (start-event) + "Drag top edge of a frame with the mouse. +START-EVENT is the starting mouse event of the drag action." + (interactive "e") + (mouse-drag-frame start-event 'top)) + +(defun mouse-drag-top-right-corner (start-event) + "Drag top right corner of a frame with the mouse. +START-EVENT is the starting mouse event of the drag action." + (interactive "e") + (mouse-drag-frame start-event 'top-right)) + +(defun mouse-drag-right-edge (start-event) + "Drag right edge of a frame with the mouse. +START-EVENT is the starting mouse event of the drag action." + (interactive "e") + (mouse-drag-frame start-event 'right)) + +(defun mouse-drag-bottom-right-corner (start-event) + "Drag bottom right corner of a frame with the mouse. +START-EVENT is the starting mouse event of the drag action." + (interactive "e") + (mouse-drag-frame start-event 'bottom-right)) + +(defun mouse-drag-bottom-edge (start-event) + "Drag bottom edge of a frame with the mouse. +START-EVENT is the starting mouse event of the drag action." + (interactive "e") + (mouse-drag-frame start-event 'bottom)) + +(defun mouse-drag-bottom-left-corner (start-event) + "Drag bottom left corner of a frame with the mouse. +START-EVENT is the starting mouse event of the drag action." + (interactive "e") + (mouse-drag-frame start-event 'bottom-left)) + (defcustom mouse-select-region-move-to-beginning nil "Effect of selecting a region extending backward from double click. Nil means keep point at the position clicked (region end); @@ -2078,6 +2449,22 @@ is copied instead of being cut." (global-set-key [bottom-divider down-mouse-1] 'mouse-drag-mode-line) (global-set-key [bottom-divider mouse-1] 'ignore) (global-set-key [bottom-divider C-mouse-2] 'mouse-split-window-horizontally) +(global-set-key [left-edge down-mouse-1] 'mouse-drag-left-edge) +(global-set-key [left-edge mouse-1] 'ignore) +(global-set-key [top-left-corner down-mouse-1] 'mouse-drag-top-left-corner) +(global-set-key [top-left-corner mouse-1] 'ignore) +(global-set-key [top-edge down-mouse-1] 'mouse-drag-top-edge) +(global-set-key [top-edge mouse-1] 'ignore) +(global-set-key [top-right-corner down-mouse-1] 'mouse-drag-top-right-corner) +(global-set-key [top-right-corner mouse-1] 'ignore) +(global-set-key [right-edge down-mouse-1] 'mouse-drag-right-edge) +(global-set-key [right-edge mouse-1] 'ignore) +(global-set-key [bottom-right-corner down-mouse-1] 'mouse-drag-bottom-right-corner) +(global-set-key [bottom-right-corner mouse-1] 'ignore) +(global-set-key [bottom-edge down-mouse-1] 'mouse-drag-bottom-edge) +(global-set-key [bottom-edge mouse-1] 'ignore) +(global-set-key [bottom-left-corner down-mouse-1] 'mouse-drag-bottom-left-corner) +(global-set-key [bottom-left-corner mouse-1] 'ignore) (provide 'mouse) diff --git a/lisp/tooltip.el b/lisp/tooltip.el index 367114b83f5..c011f1b01bc 100644 --- a/lisp/tooltip.el +++ b/lisp/tooltip.el @@ -119,7 +119,8 @@ the value of `tooltip-y-offset' is ignored." (defcustom tooltip-frame-parameters '((name . "tooltip") (internal-border-width . 2) - (border-width . 1)) + (border-width . 1) + (no-special-glyphs . t)) "Frame parameters used for tooltips. If `left' or `top' parameters are included, they specify the absolute @@ -130,7 +131,8 @@ of the `tooltip' face are used instead." :type '(repeat (cons :format "%v" (symbol :tag "Parameter") (sexp :tag "Value"))) - :group 'tooltip) + :group 'tooltip + :version "26.1") (defface tooltip '((((class color)) diff --git a/lisp/window.el b/lisp/window.el index 8b07ed462c9..c933996a72f 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -3703,7 +3703,7 @@ are one more than the actual value of these edges. Note that if ABSOLUTE is non-nil, PIXELWISE is implicitly non-nil too." (let* ((window (window-normalize-window window body)) (frame (window-frame window)) - (border-width (frame-border-width frame)) + (border-width (frame-internal-border-width frame)) (char-width (frame-char-width frame)) (char-height (frame-char-height frame)) (left (if pixelwise @@ -4572,12 +4572,13 @@ The function is called with one argument - a frame. Functions affected by this option are those that bury a buffer shown in a separate frame like `quit-window' and `bury-buffer'." :type '(choice (const :tag "Iconify" iconify-frame) + (const :tag "Make invisible" make-frame-invisible) (const :tag "Delete" delete-frame) (const :tag "Do nothing" ignore) function) :group 'windows :group 'frames - :version "24.1") + :version "26.1") (defun window--delete (&optional window dedicated-only kill) "Delete WINDOW if possible. @@ -4595,7 +4596,9 @@ if WINDOW gets deleted or its frame is auto-hidden." (cond (kill (delete-frame frame)) - ((functionp frame-auto-hide-function) + ((functionp (frame-parameter frame 'auto-hide-function)) + (funcall (frame-parameter frame 'auto-hide-function))) + ((functionp frame-auto-hide-function) (funcall frame-auto-hide-function frame)))) 'frame) (deletable @@ -6734,15 +6737,17 @@ live." window)) (defun window--maybe-raise-frame (frame) - (let ((visible (frame-visible-p frame))) - (unless (or (not visible) - ;; Assume the selected frame is already visible enough. - (eq frame (selected-frame)) - ;; Assume the frame from which we invoked the - ;; minibuffer is visible. - (and (minibuffer-window-active-p (selected-window)) - (eq frame (window-frame (minibuffer-selected-window))))) - (raise-frame frame)))) + (make-frame-visible frame) + (unless (or (frame-parameter frame 'no-focus-on-map) + ;; Don't raise frames that should not get focus. + (frame-parameter frame 'no-accept-focus) + ;; Assume the selected frame is already visible enough. + (eq frame (selected-frame)) + ;; Assume the frame from which we invoked the + ;; minibuffer is visible. + (and (minibuffer-window-active-p (selected-window)) + (eq frame (window-frame (minibuffer-selected-window))))) + (raise-frame frame))) ;; FIXME: Not implemented. ;; FIXME: By the way, there could be more levels of dedication: @@ -6762,6 +6767,7 @@ The actual non-nil value of this variable will be copied to the (const display-buffer-pop-up-window) (const display-buffer-same-window) (const display-buffer-pop-up-frame) + (const display-buffer-in-child-frame) (const display-buffer-below-selected) (const display-buffer-at-bottom) (const display-buffer-in-previous-window) @@ -6908,6 +6914,7 @@ Available action functions include: `display-buffer-same-window' `display-buffer-reuse-window' `display-buffer-pop-up-frame' + `display-buffer-in-child-frame' `display-buffer-pop-up-window' `display-buffer-in-previous-window' `display-buffer-use-some-window' @@ -7239,6 +7246,7 @@ raising the frame." (get-largest-window frame t) alist) (window--try-to-split-window (get-lru-window frame t) alist)))) + (prog1 (window--display-buffer buffer window 'window alist display-buffer-mark-dedicated) (unless (cdr (assq 'inhibit-switch-frame alist)) @@ -7258,6 +7266,47 @@ again with `display-buffer-pop-up-window'." (and pop-up-windows (display-buffer-pop-up-window buffer alist)))) +(defun display-buffer-in-child-frame (buffer alist) + "Display BUFFER in a child frame. +By default, this either reuses a child frame of the selected +frame or makes a new child frame of the selected frame. If +successful, return the window used; otherwise return nil. + +If ALIST has a non-nil 'child-frame-parameters' entry, the +corresponding value is an alist of frame parameters to give the +new frame. A 'parent-frame' parameter specifying the selected +frame is provided by default. If the child frame should be or +become the child of any other frame, a corresponding entry must +be added to ALIST." + (let* ((parameters + (append + (cdr (assq 'child-frame-parameters alist)) + `((parent-frame . ,(selected-frame))))) + (parent (or (assq 'parent-frame parameters) + (selected-frame))) + (share (assq 'share-child-frame parameters)) + share1 frame window) + (with-current-buffer buffer + (when (frame-live-p parent) + (catch 'frame + (dolist (frame1 (frame-list)) + (when (eq (frame-parent frame1) parent) + (setq share1 (assq 'share-child-frame + (frame-parameters frame1))) + (when (eq share share1) + (setq frame frame1) + (throw 'frame t)))))) + + (if frame + (setq window (frame-selected-window frame)) + (setq frame (make-frame parameters)) + (setq window (frame-selected-window frame)))) + + (prog1 (window--display-buffer + buffer window 'frame alist display-buffer-mark-dedicated) + (unless (cdr (assq 'inhibit-switch-frame alist)) + (window--maybe-raise-frame frame))))) + (defun display-buffer-below-selected (buffer alist) "Try displaying BUFFER in a window below the selected window. If there is a window below the selected one and that window @@ -7272,7 +7321,8 @@ below the selected one, use that window." (and (not (frame-parameter nil 'unsplittable)) (let ((split-height-threshold 0) split-width-threshold) - (setq window (window--try-to-split-window (selected-window) alist))) + (setq window (window--try-to-split-window + (selected-window) alist))) (window--display-buffer buffer window 'window alist display-buffer-mark-dedicated)) (and (setq window (window-in-direction 'below)) @@ -7885,10 +7935,12 @@ See also `fit-frame-to-buffer-margins'." (declare-function x-display-pixel-height "xfns.c" (&optional terminal)) (defun window--sanitize-margin (margin left right) - "Return MARGIN if it's a number between LEFT and RIGHT." - (when (and (numberp margin) - (<= left (- right margin)) (<= margin right)) - margin)) + "Return MARGIN if it's a number between LEFT and RIGHT. +Return 0 otherwise." + (if (and (numberp margin) + (<= left (- right margin)) (<= margin right)) + margin + 0)) (declare-function tool-bar-height "xdisp.c" (&optional frame pixelwise)) @@ -7906,190 +7958,197 @@ horizontally only. The new position and size of FRAME can be additionally determined by customizing the options `fit-frame-to-buffer-sizes' and -`fit-frame-to-buffer-margins' or the corresponding parameters of -FRAME." +`fit-frame-to-buffer-margins' or setting the corresponding +parameters of FRAME." (interactive) - (unless (and (fboundp 'x-display-pixel-height) - ;; We need the respective sizes now. - (fboundp 'display-monitor-attributes-list)) + (unless (fboundp 'display-monitor-attributes-list) (user-error "Cannot resize frame in non-graphic Emacs")) (setq frame (window-normalize-frame frame)) (when (window-live-p (frame-root-window frame)) - (with-selected-window (frame-root-window frame) - (let* ((char-width (frame-char-width)) - (char-height (frame-char-height)) - (monitor-attributes (car (display-monitor-attributes-list - (frame-parameter frame 'display)))) - (geometry (cdr (assq 'geometry monitor-attributes))) - (display-width (- (nth 2 geometry) (nth 0 geometry))) - (display-height (- (nth 3 geometry) (nth 1 geometry))) - (workarea (cdr (assq 'workarea monitor-attributes))) - ;; Handle margins. - (margins (or (frame-parameter frame 'fit-frame-to-buffer-margins) - fit-frame-to-buffer-margins)) - (left-margin (if (nth 0 margins) - (or (window--sanitize-margin - (nth 0 margins) 0 display-width) - 0) - (nth 0 workarea))) - (top-margin (if (nth 1 margins) - (or (window--sanitize-margin - (nth 1 margins) 0 display-height) - 0) - (nth 1 workarea))) - (workarea-width (nth 2 workarea)) - (right-margin (if (nth 2 margins) - (- display-width - (or (window--sanitize-margin - (nth 2 margins) left-margin display-width) - 0)) - (nth 2 workarea))) - (workarea-height (nth 3 workarea)) - (bottom-margin (if (nth 3 margins) - (- display-height - (or (window--sanitize-margin - (nth 3 margins) top-margin display-height) - 0)) - (nth 3 workarea))) - ;; The pixel width of FRAME (which does not include the - ;; window manager's decorations). - (frame-width (frame-pixel-width)) - ;; The pixel width of the body of FRAME's root window. - (window-body-width (window-body-width nil t)) - ;; The difference in pixels between total and body width of - ;; FRAME's window. - (window-extra-width (- (window-pixel-width) window-body-width)) - ;; The difference in pixels between the frame's pixel width - ;; and the window's body width. This is the space we can't - ;; use for fitting. - (extra-width (- frame-width window-body-width)) - ;; The pixel position of FRAME's left border. We usually - ;; try to leave this alone. - (left - (let ((left (frame-parameter nil 'left))) - (if (consp left) - (funcall (car left) (cadr left)) - left))) - ;; The pixel height of FRAME (which does not include title - ;; line, decorations, and sometimes neither the menu nor - ;; the toolbar). - (frame-height (frame-pixel-height)) - ;; The pixel height of FRAME's root window (we don't care - ;; about the window's body height since the return value of - ;; `window-text-pixel-size' includes header and mode line). - (window-height (window-pixel-height)) - ;; The difference in pixels between the frame's pixel - ;; height and the window's height. - (extra-height (- frame-height window-height)) - ;; The pixel position of FRAME's top border. - (top - (let ((top (frame-parameter nil 'top))) - (if (consp top) - (funcall (car top) (cadr top)) - top))) - ;; Sanitize minimum and maximum sizes. - (sizes (or (frame-parameter frame 'fit-frame-to-buffer-sizes) - fit-frame-to-buffer-sizes)) - (max-height - (cond - ((numberp (nth 0 sizes)) (* (nth 0 sizes) char-height)) - ((numberp max-height) (* max-height char-height)) - (t display-height))) - (min-height - (cond - ((numberp (nth 1 sizes)) (* (nth 1 sizes) char-height)) - ((numberp min-height) (* min-height char-height)) - (t (* window-min-height char-height)))) - (max-width - (cond - ((numberp (nth 2 sizes)) - (- (* (nth 2 sizes) char-width) window-extra-width)) - ((numberp max-width) - (- (* max-width char-width) window-extra-width)) - (t display-width))) - (min-width - (cond - ((numberp (nth 3 sizes)) - (- (* (nth 3 sizes) char-width) window-extra-width)) - ((numberp min-width) - (- (* min-width char-width) window-extra-width)) - (t (* window-min-width char-width)))) - ;; Note: Currently, for a new frame the sizes of the header - ;; and mode line may be estimated incorrectly - (value (window-text-pixel-size - nil t t workarea-width workarea-height t)) - (width (+ (car value) (window-right-divider-width))) - (height - (+ (cdr value) - (window-bottom-divider-width) - (window-scroll-bar-height)))) - ;; Don't change height or width when the window's size is fixed - ;; in either direction or ONLY forbids it. - (cond - ((or (eq window-size-fixed 'width) (eq only 'vertically)) - (setq width nil)) - ((or (eq window-size-fixed 'height) (eq only 'horizontally)) - (setq height nil))) - ;; Fit width to constraints. - (when width - (unless frame-resize-pixelwise - ;; Round to character sizes. - (setq width (* (/ (+ width char-width -1) char-width) - char-width))) - ;; Fit to maximum and minimum widths. - (setq width (max (min width max-width) min-width)) - ;; Add extra width. - (setq width (+ width extra-width)) - ;; Preserve margins. - (let ((right (+ left width))) - (cond - ((> right right-margin) - ;; Move frame to left (we don't know its real width). - (setq left (max left-margin (- left (- right right-margin))))) - ((< left left-margin) - ;; Move frame to right. - (setq left left-margin))))) - ;; Fit height to constraints. - (when height - (unless frame-resize-pixelwise - (setq height (* (/ (+ height char-height -1) char-height) - char-height))) - ;; Fit to maximum and minimum heights. - (setq height (max (min height max-height) min-height)) - ;; Add extra height. - (setq height (+ height extra-height)) - ;; Preserve margins. - (let ((bottom (+ top height))) - (cond - ((> bottom bottom-margin) - ;; Move frame up (we don't know its real height). - (setq top (max top-margin (- top (- bottom bottom-margin))))) - ((< top top-margin) - ;; Move frame down. - (setq top top-margin))))) - ;; Apply changes. - (set-frame-position frame left top) - ;; Clumsily try to translate our calculations to what - ;; `set-frame-size' wants. - (when width - (setq width (- (+ (frame-text-width) width) - extra-width window-body-width))) - (when height - (setq height (- (+ (frame-text-height) height) - extra-height window-height))) - (set-frame-size - frame - (if width - (if frame-resize-pixelwise - width - (/ width char-width)) - (frame-text-width)) - (if height - (if frame-resize-pixelwise - height - (/ height char-height)) - (frame-text-height)) - frame-resize-pixelwise))))) + (let* ((char-width (frame-char-width frame)) + (char-height (frame-char-height frame)) + ;; WINDOW is FRAME's root window. + (window (frame-root-window frame)) + (parent (frame-parent frame)) + (monitor-attributes + (unless parent + (car (display-monitor-attributes-list + (frame-parameter frame 'display))))) + ;; FRAME'S parent or display sizes. Used in connection + ;; with margins. + (geometry + (unless parent + (cdr (assq 'geometry monitor-attributes)))) + (parent-or-display-width + (if parent + (frame-native-width parent) + (- (nth 2 geometry) (nth 0 geometry)))) + (parent-or-display-height + (if parent + (frame-native-height parent) + (- (nth 3 geometry) (nth 1 geometry)))) + ;; FRAME'S parent or workarea sizes. Used when no margins + ;; are specified. + (parent-or-workarea + (if parent + `(0 0 ,parent-or-display-width ,parent-or-display-height) + (cdr (assq 'workarea monitor-attributes)))) + ;; The outer size of FRAME. Needed to calculate the + ;; margins around the root window's body that have to + ;; remain untouched by fitting. + (outer-edges (frame-edges frame 'outer-edges)) + (outer-width (if outer-edges + (- (nth 2 outer-edges) (nth 0 outer-edges)) + ;; A poor guess. + (frame-pixel-width frame))) + (outer-height (if outer-edges + (- (nth 3 outer-edges) (nth 1 outer-edges)) + ;; Another poor guess. + (frame-pixel-height frame))) + ;; The text size of of FRAME. Needed to specify FRAME's + ;; text size after the root window's body's new sizes have + ;; been calculated. + (text-width (frame-text-width frame)) + (text-height (frame-text-height frame)) + ;; WINDOW's body size. + (body-width (window-body-width window t)) + (body-height (window-body-height window t)) + ;; The difference between FRAME's outer size and WINDOW's + ;; body size. + (outer-minus-body-width (- outer-width body-width)) + (outer-minus-body-height (- outer-height body-height)) + ;; The difference between FRAME's text size and WINDOW's + ;; body size (these values "should" be positive). + (text-minus-body-width (- text-width body-width)) + (text-minus-body-height (- text-height body-height)) + ;; The current position of FRAME. + (position (frame-position frame)) + (left (car position)) + (top (cdr position)) + ;; The margins specified for FRAME. These represent pixel + ;; offsets from the left, top, right and bottom edge of the + ;; display or FRAME's parent's native rectangle and have to + ;; take care of the display's taskbar and other obstacles. + ;; If they are unspecified, constrain the resulting frame + ;; to its workarea or the parent frame's native rectangle. + (margins (or (frame-parameter frame 'fit-frame-to-buffer-margins) + fit-frame-to-buffer-margins)) + ;; Convert margins intto pixel offsets from the left-top + ;; corner of FRAME's display or parent. + (left-margin (if (nth 0 margins) + (window--sanitize-margin + (nth 0 margins) 0 parent-or-display-width) + (nth 0 parent-or-workarea))) + (top-margin (if (nth 1 margins) + (window--sanitize-margin + (nth 1 margins) 0 parent-or-display-height) + (nth 1 parent-or-workarea))) + (right-margin (if (nth 2 margins) + (- parent-or-display-width + (window--sanitize-margin + (nth 2 margins) left-margin + parent-or-display-width)) + (nth 2 parent-or-workarea))) + (bottom-margin (if (nth 3 margins) + (- parent-or-display-height + (window--sanitize-margin + (nth 3 margins) top-margin + parent-or-display-height)) + (nth 3 parent-or-workarea))) + ;; Minimum and maximum sizes specified for FRAME. + (sizes (or (frame-parameter frame 'fit-frame-to-buffer-sizes) + fit-frame-to-buffer-sizes)) + ;; Calculate the minimum and maximum pixel sizes of FRAME + ;; from the values provided by the MAX-HEIGHT, MIN-HEIGHT, + ;; MAX-WIDTH and MIN-WIDTH arguments or, if these are nil, + ;; from those provided by `fit-frame-to-buffer-sizes'. + (max-height + (min + (cond + ((numberp max-height) (* max-height char-height)) + ((numberp (nth 0 sizes)) (* (nth 0 sizes) char-height)) + (t parent-or-display-height)) + ;; The following is the maximum height that fits into the + ;; top and bottom margins. + (max (- bottom-margin top-margin outer-minus-body-height)))) + (min-height + (cond + ((numberp min-height) (* min-height char-height)) + ((numberp (nth 1 sizes)) (* (nth 1 sizes) char-height)) + (t (window-min-size window nil nil t)))) + (max-width + (min + (cond + ((numberp max-width) (* max-width char-width)) + ((numberp (nth 2 sizes)) (* (nth 2 sizes) char-width)) + (t parent-or-display-width)) + ;; The following is the maximum width that fits into the + ;; left and right margins. + (max (- right-margin left-margin outer-minus-body-width)))) + (min-width + (cond + ((numberp min-width) (* min-width char-width)) + ((numberp (nth 3 sizes)) (nth 3 sizes)) + (t (window-min-size window t nil t)))) + ;; Note: Currently, for a new frame the sizes of the header + ;; and mode line may be estimated incorrectly + (size + (window-text-pixel-size window t t max-width max-height)) + (width (max (car size) min-width)) + (height (max (cdr size) min-height))) + ;; Don't change height or width when the window's size is fixed + ;; in either direction or ONLY forbids it. + (cond + ((or (eq window-size-fixed 'width) (eq only 'vertically)) + (setq width nil)) + ((or (eq window-size-fixed 'height) (eq only 'horizontally)) + (setq height nil))) + ;; Fit width to constraints. + (when width + (unless frame-resize-pixelwise + ;; Round to character sizes. + (setq width (* (/ (+ width char-width -1) char-width) + char-width))) + ;; The new outer width (in pixels). + (setq outer-width (+ width outer-minus-body-width)) + ;; Maybe move FRAME to preserve margins. + (let ((right (+ left outer-width))) + (cond + ((> right right-margin) + ;; Move frame to left. + (setq left (max left-margin (- left (- right right-margin))))) + ((< left left-margin) + ;; Move frame to right. + (setq left left-margin))))) + ;; Fit height to constraints. + (when height + (unless frame-resize-pixelwise + (setq height (* (/ (+ height char-height -1) char-height) + char-height))) + ;; The new outer height. + (setq outer-height (+ height outer-minus-body-height)) + ;; Preserve margins. + (let ((bottom (+ top outer-height))) + (cond + ((> bottom bottom-margin) + ;; Move frame up. + (setq top (max top-margin (- top (- bottom bottom-margin))))) + ((< top top-margin) + ;; Move frame down. + (setq top top-margin))))) + ;; Apply our changes. + (setq text-width + (if width + (+ width text-minus-body-width) + (frame-text-width frame))) + (setq text-height + (if height + (+ height text-minus-body-height) + (frame-text-height frame))) + (modify-frame-parameters + frame `((left . ,left) (top . ,top) + (width . (text-pixels . ,text-width)) + (height . (text-pixels . ,text-height))))))) (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width preserve-size) "Adjust size of WINDOW to display its buffer's contents exactly. @@ -8286,6 +8345,168 @@ Return non-nil if the window was shrunk, nil otherwise." (when (and (window-combined-p window) (pos-visible-in-window-p (point-min) window)) (fit-window-to-buffer window (window-total-height window)))) + +(defun window-largest-empty-rectangle--maximums-1 (quad maximums) + "Support function for `window-largest-empty-rectangle'." + (cond + ((null maximums) + (list quad)) + ((> (car quad) (caar maximums)) + (cons quad maximums)) + (t + (cons (car maximums) + (window-largest-empty-rectangle--maximums-1 quad (cdr maximums)))))) + +(defun window-largest-empty-rectangle--maximums (quad maximums count) + "Support function for `window-largest-empty-rectangle'." + (setq maximums (window-largest-empty-rectangle--maximums-1 quad maximums)) + (if (> (length maximums) count) + (nbutlast maximums) + maximums)) + +(defun window-largest-empty-rectangle--disjoint-maximums (maximums count) + "Support function for `window-largest-empty-rectangle'." + (setq maximums (sort maximums (lambda (x y) (> (car x) (car y))))) + (let ((new-length 0) + new-maximums) + (while (and maximums (< new-length count)) + (let* ((maximum (car maximums)) + (at (nth 2 maximum)) + (to (nth 3 maximum))) + (catch 'drop + (dolist (new-maximum new-maximums) + (let ((new-at (nth 2 new-maximum)) + (new-to (nth 3 new-maximum))) + (when (if (< at new-at) (> to new-at) (< at new-to)) + ;; Intersection -> drop. + (throw 'drop nil)))) + (setq new-maximums (cons maximum new-maximums)) + (setq new-length (1+ new-length))) + (setq maximums (cdr maximums)))) + + (nreverse new-maximums))) + +(defun window-largest-empty-rectangle (&optional window count min-width min-height positions left) + "Return dimensions of largest empty rectangle in WINDOW. +WINDOW must be a live window and defaults to the selected one. + +The return value is a triple of the width and the start and end +Y-coordinates of the largest rectangle that can be inscribed into +the empty space (the space not displaying any text) of WINDOW's +text area. The return value is nil if the current glyph matrix +of WINDOW is not up-to-date. + +Optional argument COUNT, if non-nil, specifies the maximum number +of rectangles to return. This means that the return value is a +list of triples specifying rectangles with the largest rectangle +first. COUNT can be also a cons cell whose car specifies the +number of rectangles to return and whose cdr, if non-nil, states +that all rectangles returned must be disjoint. + +Note that the right edge of any rectangle returned by this +function is the right edge of WINDOW (the left edge if its buffer +displays RTL text). + +Optional arguments MIN-WIDTH and MIN-HEIGHT, if non-nil, specify +the minimum width and height of any rectangle returned. + +Optional argument POSITIONS, if non-nil, is a cons cell whose car +specifies the uppermost and whose cdr specifies the lowermost +pixel position that must be covered by any rectangle returned. +Note that positions are counted from the start of the text area +of WINDOW. + +Optional argument LEFT, if non-nil, means to return values suitable for +buffers displaying right to left text." + ;; Process lines as returned by ‘window-lines-pixel-dimensions’. + ;; STACK is a stack that contains rows that have to be processed yet. + (let* ((window (window-normalize-window window t)) + (disjoint (and (consp count) (cdr count))) + (count (or (and (numberp count) count) + (and (consp count) (numberp (car count)) (car count)))) + (rows (window-lines-pixel-dimensions window nil nil t t left)) + (rows-at 0) + (max-size 0) + row stack stack-at stack-to + top top-width top-at top-to top-size + max-width max-at max-to maximums) + ;; ROWS-AT is the position where the first element of ROWS starts. + ;; STACK-AT is the position where the first element of STACK starts. + (while rows + (setq row (car rows)) + (if (or (not stack) (>= (car row) (caar stack))) + (progn + (unless stack + (setq stack-at rows-at)) + (setq stack (cons row stack)) + ;; Set ROWS-AT to where the first element of ROWS ends + ;; which, after popping ROW, makes it the start position of + ;; the next ROW. + (setq rows-at (cdr row)) + (setq rows (cdr rows))) + (setq top (car stack)) + (setq stack (cdr stack)) + (setq top-width (car top)) + (setq top-at (if stack (cdar stack) stack-at)) + (setq top-to (cdr top)) + (setq top-size (* top-width (- top-to top-at))) + (unless (or (and min-width (< top-width min-width)) + (and min-height (< (- top-to top-at) min-height)) + (and positions + (or (> top-at (car positions)) + (< top-to (cdr positions))))) + (if count + (if disjoint + (setq maximums (cons (list top-size top-width top-at top-to) + maximums)) + (setq maximums (window-largest-empty-rectangle--maximums + (list top-size top-width top-at top-to) + maximums count))) + (when (> top-size max-size) + (setq max-size top-size) + (setq max-width top-width) + (setq max-at top-at) + (setq max-to top-to)))) + (if (and stack (> (caar stack) (car row))) + ;; Have new top element of stack include old top. + (setq stack (cons (cons (caar stack) (cdr top)) (cdr stack))) + ;; Move rows-at backwards to top-at. + (setq rows-at top-at)))) + + (when stack + ;; STACK-TO is the position where the stack ends. + (setq stack-to (cdar stack)) + (while stack + (setq top (car stack)) + (setq stack (cdr stack)) + (setq top-width (car top)) + (setq top-at (if stack (cdar stack) stack-at)) + (setq top-size (* top-width (- stack-to top-at))) + (unless (or (and min-width (< top-width min-width)) + (and min-height (< (- stack-to top-at) min-height)) + (and positions + (or (> top-at (car positions)) + (< stack-to (cdr positions))))) + (if count + (if disjoint + (setq maximums (cons (list top-size top-width top-at stack-to) + maximums)) + (setq maximums (window-largest-empty-rectangle--maximums + (list top-size top-width top-at stack-to) + maximums count))) + (when (> top-size max-size) + (setq max-size top-size) + (setq max-width top-width) + (setq max-at top-at) + (setq max-to stack-to)))))) + + (cond + (maximums + (if disjoint + (window-largest-empty-rectangle--disjoint-maximums maximums count) + maximums)) + ((> max-size 0) + (list max-width max-at max-to))))) (defun kill-buffer-and-window () "Kill the current buffer and delete the selected window." |