From a6ad80553d5ef1d332de3a1e4bbdf85eaf36b1fc Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Wed, 19 Jul 2023 06:00:55 -0700 Subject: [PATCH 0/1] *** NOT A PATCH *** *** BLURB HERE *** F. Jason Park (1): [5.6] Make erc-fill-wrap work with left-hand stamps etc/ERC-NEWS | 7 +- lisp/erc/erc-backend.el | 23 +- lisp/erc/erc-compat.el | 1 + lisp/erc/erc-fill.el | 111 +++++++--- lisp/erc/erc-stamp.el | 203 +++++++++++++----- lisp/erc/erc.el | 26 ++- test/lisp/erc/erc-fill-tests.el | 37 ++++ test/lisp/erc/erc-stamp-tests.el | 2 +- test/lisp/erc/erc-tests.el | 6 + .../fill/snapshots/stamps-left-01.eld | 1 + 10 files changed, 313 insertions(+), 104 deletions(-) create mode 100644 test/lisp/erc/resources/fill/snapshots/stamps-left-01.eld Interdiff: diff --git a/lisp/erc/erc-fill.el b/lisp/erc/erc-fill.el index 9f39f41133d..6c2228f6337 100644 --- a/lisp/erc/erc-fill.el +++ b/lisp/erc/erc-fill.el @@ -129,8 +129,8 @@ erc-fill-wrap-margin-width (defcustom erc-fill-wrap-margin-side nil "Margin side to use with `erc-fill-wrap-mode'. -A value of nil means ERC should decide based on -`erc-insert-timestamp-function', which obviously cannot work for +A value of nil means ERC should decide based on the value of +`erc-insert-timestamp-function', which does not work for user-defined functions." :package-version '(ERC . "5.6") ; FIXME sync on release :type '(choice (const nil) (const left) (const right))) @@ -297,12 +297,29 @@ erc-fill-wrap-cycle-visual-movement ('non-input nil)))) (message "erc-fill-wrap movement: %S" erc-fill--wrap-visual-keys)) +(defun erc-fill-wrap-toggle-truncate-lines (arg) + "Toggle `truncate-lines' and maybe reinstate `visual-line-mode'." + (interactive "P") + (let ((wantp (if arg + (natnump (prefix-numeric-value arg)) + (not truncate-lines))) + (buffer (current-buffer))) + (if wantp + (setq truncate-lines t) + (walk-windows (lambda (window) + (when (eq buffer (window-buffer window)) + (set-window-hscroll window 0))) + nil t) + (visual-line-mode +1))) + (force-mode-line-update)) + (defvar-keymap erc-fill-wrap-mode-map ; Compat 29 :doc "Keymap for ERC's `fill-wrap' module." :parent visual-line-mode-map " " #'erc-fill--wrap-kill-line " " #'erc-fill--wrap-end-of-line " " #'erc-fill--wrap-beginning-of-line + " " #'erc-fill-wrap-toggle-truncate-lines "C-c a" #'erc-fill-wrap-cycle-visual-movement ;; Not sure if this is problematic because `erc-bol' takes no args. " " #'erc-fill--wrap-beginning-of-line) @@ -361,9 +378,9 @@ fill-wrap left-margin-width 0 right-margin-width 0) ;; Only give this a local binding if known for sure. - (pcase erc-fill-wrap-margin-side - ('right (setq erc-stamp--margin-left-p nil)) - ('left (setq erc-stamp--margin-left-p t))) + (when erc-fill-wrap-margin-side + (setq erc-stamp--margin-left-p + (pcase erc-fill-wrap-margin-side ('right nil) ('left t)))) (setq erc-fill--function #'erc-fill-wrap) (add-function :after (local 'erc-stamp--insert-date-function) #'erc-fill--wrap-stamp-insert-prefixed-date) @@ -412,18 +429,21 @@ erc-fill--wrap-continued-message-p (widen) (when (eq 'erc-timestamp (field-at-pos m)) (set-marker m (field-end m))) - (and (eq 'PRIVMSG (get-text-property m 'erc-command)) - (not (eq (get-text-property m 'erc-ctcp) 'ACTION)) - (cons (get-text-property m 'erc-timestamp) - (get-text-property (1+ m) 'erc-data))))) + (and-let* + (((eq 'PRIVMSG (get-text-property m 'erc-command))) + ((not (eq (get-text-property m 'erc-ctcp) + 'ACTION))) + (spr (next-single-property-change m 'erc-speaker))) + (cons (get-text-property m 'erc-timestamp) + (get-text-property spr 'erc-speaker))))) (ts (pop props)) ((not (time-less-p (erc-stamp--current-time) ts))) ((time-less-p (time-subtract (erc-stamp--current-time) ts) erc-fill--wrap-max-lull)) - (nick (buffer-substring-no-properties - (1+ (point-min)) (- (point) 2))) + (speaker (next-single-property-change (point-min) 'erc-speaker)) + (nick (get-text-property speaker 'erc-speaker)) (props) - ((erc-nick-equal-p (car props) nick)))) + ((erc-nick-equal-p props nick)))) (set-marker erc-fill--wrap-last-msg (point-min)))) (defun erc-fill--wrap-stamp-insert-prefixed-date (&rest args) @@ -520,6 +540,7 @@ erc-fill-wrap-nudge (unless (get-buffer-window) (user-error "Command called in an undisplayed buffer")) (let* ((total (erc-fill--wrap-nudge arg)) + (leftp erc-stamp--margin-left-p) (win-ratio (/ (float (- (window-point) (window-start))) (- (window-end nil t) (window-start))))) (when (zerop arg) @@ -538,11 +559,10 @@ erc-fill-wrap-nudge (cl-incf total (erc-fill--wrap-nudge a)) (recenter (round (* win-ratio (window-height)))))))) (dolist (key '(?\) ?_ ?+)) - (let* ((leftp erc-stamp--margin-left-p) - (a (pcase key - (?\) 0) - (?_ (if leftp (abs arg) (- (abs arg)))) - (?+ (if leftp (- (abs arg)) (abs arg)))))) + (let ((a (pcase key + (?\) 0) + (?_ (if leftp (abs arg) (- (abs arg)))) + (?+ (if leftp (- (abs arg)) (abs arg)))))) (define-key map (vector (list key)) (lambda () (interactive) @@ -552,8 +572,9 @@ erc-fill-wrap-nudge map) t (lambda () - (message "Fill prefix: %d (%+d col%s)" - erc-fill--wrap-value total (if (> (abs total) 1) "s" ""))) + (message "Fill prefix: %d (%+d col%s); Margin: %d" + erc-fill--wrap-value total (if (> (abs total) 1) "s" "") + (if leftp left-margin-width right-margin-width))) "Use %k for further adjustment" 1) (recenter (round (* win-ratio (window-height)))))) diff --git a/lisp/erc/erc-stamp.el b/lisp/erc/erc-stamp.el index 727d334f13b..eff99766d81 100644 --- a/lisp/erc/erc-stamp.el +++ b/lisp/erc/erc-stamp.el @@ -397,6 +397,8 @@ erc-stamp--display-margin-mode #'erc-stamp--init-margins-on-connect nil t)) (add-function :filter-return (local 'filter-buffer-substring-function) #'erc--remove-text-properties) + (add-hook 'erc--setup-buffer-hook + #'erc-stamp--refresh-left-margin-prompt nil t) (when erc-stamp--margin-left-p (add-hook 'erc--refresh-prompt-hook #'erc-stamp--display-prompt-in-left-margin nil t))) @@ -405,6 +407,8 @@ erc-stamp--display-margin-mode (add-hook 'erc-after-connect #'erc-stamp--init-margins-on-connect t) (remove-hook 'erc--refresh-prompt-hook #'erc-stamp--display-prompt-in-left-margin t) + (remove-hook 'erc--setup-buffer-hook + #'erc-stamp--refresh-left-margin-prompt t) (kill-local-variable (if erc-stamp--margin-left-p 'left-margin-width 'right-margin-width)) -- 2.41.0