diff options
| author | Lars Ingebrigtsen | 2022-04-14 19:36:08 +0200 |
|---|---|---|
| committer | Lars Ingebrigtsen | 2022-04-14 19:39:01 +0200 |
| commit | be54c25dbb42425701cee3d669d37acdacfa17ce (patch) | |
| tree | 93e7082f9db58dc02e476648e807eb4738f31ed0 | |
| parent | eab0105696f6cd306842e3ede1830fbf1c7057ec (diff) | |
| download | emacs-be54c25dbb42425701cee3d669d37acdacfa17ce.tar.gz emacs-be54c25dbb42425701cee3d669d37acdacfa17ce.zip | |
Allow resizing vtable columns by dragging
* lisp/emacs-lisp/vtable.el (vtable--insert-header-line): Allow
resizing by dragging headers.
(vtable--drag-resize-column): New function.
(vtable-narrow-current-column): Refactor out common bits.
(vtable--alter-column-width): To here.
(vtable-widen-current-column): Rewrite to use
vtable-narrow-current-column.
| -rw-r--r-- | lisp/emacs-lisp/vtable.el | 47 |
1 files changed, 31 insertions, 16 deletions
diff --git a/lisp/emacs-lisp/vtable.el b/lisp/emacs-lisp/vtable.el index d53f8b07450..5900d886e80 100644 --- a/lisp/emacs-lisp/vtable.el +++ b/lisp/emacs-lisp/vtable.el | |||
| @@ -579,7 +579,11 @@ This also updates the displayed table." | |||
| 579 | (lambda (column index) | 579 | (lambda (column index) |
| 580 | (let* ((name (propertize | 580 | (let* ((name (propertize |
| 581 | (vtable-column-name column) | 581 | (vtable-column-name column) |
| 582 | 'face (list 'header-line (vtable-face table)))) | 582 | 'face (list 'header-line (vtable-face table)) |
| 583 | 'keymap (define-keymap | ||
| 584 | "<header-line> <drag-mouse-1>" | ||
| 585 | #'vtable--drag-resize-column | ||
| 586 | "<header-line> <down-mouse-1>" #'ignore))) | ||
| 583 | (start (point)) | 587 | (start (point)) |
| 584 | (indicator (vtable--indicator table index)) | 588 | (indicator (vtable--indicator table index)) |
| 585 | (indicator-width (string-pixel-width indicator)) | 589 | (indicator-width (string-pixel-width indicator)) |
| @@ -606,6 +610,24 @@ This also updates the displayed table." | |||
| 606 | (insert "\n") | 610 | (insert "\n") |
| 607 | (add-face-text-property start (point) 'header-line))) | 611 | (add-face-text-property start (point) 'header-line))) |
| 608 | 612 | ||
| 613 | (defun vtable--drag-resize-column (e) | ||
| 614 | "Resize the column by dragging." | ||
| 615 | (interactive "e") | ||
| 616 | (let* ((pos-start (event-start e)) | ||
| 617 | (obj (posn-object pos-start))) | ||
| 618 | (with-current-buffer (window-buffer (posn-window pos-start)) | ||
| 619 | (let ((column | ||
| 620 | (get-text-property (if obj (cdr obj) | ||
| 621 | (posn-point pos-start)) | ||
| 622 | 'vtable-column | ||
| 623 | (car obj))) | ||
| 624 | (start-x (car (posn-x-y pos-start))) | ||
| 625 | (end-x (car (posn-x-y (event-end e))))) | ||
| 626 | (when (> column 0) | ||
| 627 | (vtable--alter-column-width (vtable-current-table) | ||
| 628 | (1- column) | ||
| 629 | (- end-x start-x))))))) | ||
| 630 | |||
| 609 | (defun vtable--recompute-numerical (table line) | 631 | (defun vtable--recompute-numerical (table line) |
| 610 | "Recompute numericalness of columns if necessary." | 632 | "Recompute numericalness of columns if necessary." |
| 611 | (let ((columns (vtable-columns table)) | 633 | (let ((columns (vtable-columns table)) |
| @@ -768,14 +790,17 @@ If N isn't given, N defaults to 1. | |||
| 768 | Interactively, N is the prefix argument." | 790 | Interactively, N is the prefix argument." |
| 769 | (interactive "p") | 791 | (interactive "p") |
| 770 | (let* ((table (vtable-current-table)) | 792 | (let* ((table (vtable-current-table)) |
| 771 | (column (vtable-current-column)) | 793 | (column (vtable-current-column))) |
| 772 | (widths (vtable--widths table))) | ||
| 773 | (unless column | 794 | (unless column |
| 774 | (user-error "No column under point")) | 795 | (user-error "No column under point")) |
| 796 | (vtable--alter-column-width table column | ||
| 797 | (- (* (vtable--char-width table) (or n 1)))))) | ||
| 798 | |||
| 799 | (defun vtable--alter-column-width (table column delta) | ||
| 800 | (let ((widths (vtable--widths table))) | ||
| 775 | (setf (aref widths column) | 801 | (setf (aref widths column) |
| 776 | (max (* (vtable--char-width table) 2) | 802 | (max (* (vtable--char-width table) 2) |
| 777 | (- (aref widths column) | 803 | (+ (aref widths column) delta))) |
| 778 | (* (vtable--char-width table) (or n 1))))) | ||
| 779 | ;; Store the width so it'll be respected on a revert. | 804 | ;; Store the width so it'll be respected on a revert. |
| 780 | (setf (vtable-column-width (elt (vtable-columns table) column)) | 805 | (setf (vtable-column-width (elt (vtable-columns table) column)) |
| 781 | (format "%dpx" (aref widths column))) | 806 | (format "%dpx" (aref widths column))) |
| @@ -787,17 +812,7 @@ If N isn't given, N defaults to 1. | |||
| 787 | 812 | ||
| 788 | Interactively, N is the prefix argument." | 813 | Interactively, N is the prefix argument." |
| 789 | (interactive "p") | 814 | (interactive "p") |
| 790 | (let* ((table (vtable-current-table)) | 815 | (vtable-narrow-current-column (- n))) |
| 791 | (column (vtable-current-column)) | ||
| 792 | (widths (vtable--widths table))) | ||
| 793 | (unless column | ||
| 794 | (user-error "No column under point")) | ||
| 795 | (cl-incf (aref widths column) | ||
| 796 | (* (vtable--char-width table) (or n 1))) | ||
| 797 | ;; Store the width so it'll be respected on a revert. | ||
| 798 | (setf (vtable-column-width (elt (vtable-columns table) column)) | ||
| 799 | (format "%dpx" (aref widths column))) | ||
| 800 | (vtable-revert))) | ||
| 801 | 816 | ||
| 802 | (defun vtable-previous-column () | 817 | (defun vtable-previous-column () |
| 803 | "Go to the previous column." | 818 | "Go to the previous column." |