doom-modeline icon indicating copy to clipboard operation
doom-modeline copied to clipboard

[Bug] `selection-info` segment does not update after movement commands

Open JordanAnthonyKing opened this issue 3 years ago • 20 comments

Thank you for the bug report

  • [X] I am using the latest version of doom-mode related packages.
  • [X] I checked FAQ.
  • [x] You may also try reproduce the issue using clean environment and minimal configurations with the command emacs -Q.

Bug description

Using a custom modeline like:

  (doom-modeline-def-modeline 'my-simple-line
    '(bar modals matches buffer-info persp-name remote-host word-count parrot selection-info) ; <--
    '(objed-state misc-info debug repl lsp major-mode process vcs checker))

makes the selection-info in the modeline only display 1C, and does not update to reflect the selection.

Steps to reproduce

Create a custom modeline as above with selection-info but without buffer-position

Expected behavior

Selection info should be accurate in the modeline regardless of if buffer-position is displayed or not

OS

Linux

Emacs Version

gccemacs-28

Emacs Configurations

Doom

Error callstack

No response

Anything else

No response

JordanAnthonyKing avatar Sep 30 '21 13:09 JordanAnthonyKing

Can you please provide more details? Did you observe the selection-info was inaccurate? any screenshot or recording? I couldn't reproduce this issue locally.

seagle0128 avatar Sep 30 '21 18:09 seagle0128

I believe I may have conflated the issue, see this screenshot: image 5 lines are highlighted, but the modeline only lists 4. It seems that the modeline updates when something triggers it to, not when the cursor moves. In this lisp file that seems to happen quite a lot, when the cursor passes a variable for instance, but in org-mode: image nothing triggers the update, so selection-info only reports 1L.

JordanAnthonyKing avatar Sep 30 '21 21:09 JordanAnthonyKing

I tried to reproduce this issue although I am not using doom, but I failed. It works well as expected. I suggest to use emacs -Q to reproduce. Thus will figure out whether your personal configurations impact.

emacs

seagle0128 avatar Oct 01 '21 05:10 seagle0128

The gif you provided clearly demonstrates the issue. When buffer-position is used the modeline updates after every motion, when it isn't the modeline doesn't update while moving, but something causes it to (seemingly the popup) once you finish moving. Try again in an org file or other text file where nothing prompts the update.

JordanAnthonyKing avatar Oct 01 '21 07:10 JordanAnthonyKing

Unfortunately I don't understand what's issue here. Actually selection-info and buffer-position are totally different segments, and won't impact each other.

emacs

seagle0128 avatar Oct 01 '21 10:10 seagle0128

I misconstrued the issue when I raised this bug. I'll retitle it.

Please try in org again, but with lines of plain text, no source blocks or other things that evaluate. If I use a source block similar to yours then the selection-info updates when the cursor overlays the region and the expression is evaluated. If you only select plain text, then the selection info never updates from 1C or 1L.

Again, you can see this in the first video you posted, with buffer-info disabled the selection info does not update until a popup for the currently hovered symbol is triggered.

The issue therefore is that the selection-info segment does not update on motion commands, and instead updates when something else causes the modeline to update (or some similar scenario). This is why selection-info works flawlessly with buffer-position, as buffer-position updates on every motion.

Text only: image

Passing the code block: image

JordanAnthonyKing avatar Oct 01 '21 10:10 JordanAnthonyKing

Can you reproduce with emacs -Q? You can see from my recording above, the selection info is displayed in org buffers. I think there are some special configurations for evil in your env.

seagle0128 avatar Oct 01 '21 12:10 seagle0128

In vanilla emacs as default: image

Then with following:

(doom-modeline-def-modeline 'my-simple-line
    '(bar modals matches buffer-info-simple remote-host word-count selection-info parrot)
    '(objed-state misc-info debug repl lsp major-mode process vcs checker))
  (doom-modeline-set-modeline 'my-simple-line 'default)

Gives: image

Selection info stays on 0C after setting mark and moving the cursor. This behaviour is also found while dragging the mouse but the selection info is updated on releasing it, probably because as I said before, the update of selection-info gets triggered by things being evaluated. Mark set appears in the minibuffer when the mouse is released, and this triggers the selection info to be updated. Selection info is not updated on cursor movements alone however.

JordanAnthonyKing avatar Oct 01 '21 13:10 JordanAnthonyKing

Well, I know how to reproduce the issue. It seems only exist on Emacs 28. I tried with Emacs 27 and all work well. I checked the codes again, and no issue was found by me. It seems a bug of Emacs28 itself. Can you double check with other versions, like 26 or 27? Thanks!

seagle0128 avatar Oct 01 '21 14:10 seagle0128

I still see this issue in emacs 27.2 on windows: image As before, sometime the selection will update, for instance if I minimise and then restore emacs, the selection info gets updated, but the update does not happen on cursor movement alone.

My earlier screenshots were of emacs 28 on WSL.

JordanAnthonyKing avatar Oct 02 '21 11:10 JordanAnthonyKing

Wired! Can you please try evaluating the snippet below?

(doom-modeline-def-segment selection-info
  "Information about the current selection, such as how many characters and
lines are selected, or the NxM dimensions of a block selection."
  (when (and (or mark-active (and (bound-and-true-p evil-local-mode)
                                  (eq evil-state 'visual)))
             (doom-modeline--active))
    (cl-destructuring-bind (beg . end)
      (if (and (bound-and-true-p evil-local-mode) (eq evil-state 'visual))
          (cons evil-visual-beginning evil-visual-end)
        (cons (region-beginning) (region-end)))
      (propertize
       (let ((lines (count-lines beg (min end (point-max)))))
         (concat (doom-modeline-spc)
                 (cond ((or (bound-and-true-p rectangle-mark-mode)
                            (and (bound-and-true-p evil-visual-selection)
                                 (eq 'block evil-visual-selection)))
                        (let ((cols (abs (- (doom-modeline-column end)
                                            (doom-modeline-column beg)))))
                          (format "%dx%dB" lines cols)))
                       ((and (bound-and-true-p evil-visual-selection)
                             (eq evil-visual-selection 'line))
                        (format "%dL" lines))
                       ((> lines 1)
                        (format "%dC %dL" (- end beg) lines))
                       (t
                        (format "%dC" (- end beg))))
                 (when doom-modeline-enable-word-count
                   (format " %dW" (count-words beg end)))
                 (doom-modeline-spc)))
       'face 'doom-modeline-highlight)
      (force-mode-line-update))))

seagle0128 avatar Oct 02 '21 13:10 seagle0128

I get an warning evaling that statement and selection-info does not render: image

JordanAnthonyKing avatar Oct 02 '21 13:10 JordanAnthonyKing

Sorry, correct the snippet:

(doom-modeline-def-segment selection-info
  "Information about the current selection, such as how many characters and
lines are selected, or the NxM dimensions of a block selection."
  (when (and (or mark-active (and (bound-and-true-p evil-local-mode)
                                  (eq evil-state 'visual)))
             (doom-modeline--active))
    (cl-destructuring-bind (beg . end)
      (if (and (bound-and-true-p evil-local-mode) (eq evil-state 'visual))
          (cons evil-visual-beginning evil-visual-end)
        (cons (region-beginning) (region-end)))
      (prog1
          (propertize
           (let ((lines (count-lines beg (min end (point-max)))))
             (concat (doom-modeline-spc)
                     (cond ((or (bound-and-true-p rectangle-mark-mode)
                                (and (bound-and-true-p evil-visual-selection)
                                     (eq 'block evil-visual-selection)))
                            (let ((cols (abs (- (doom-modeline-column end)
                                                (doom-modeline-column beg)))))
                              (format "%dx%dB" lines cols)))
                           ((and (bound-and-true-p evil-visual-selection)
                                 (eq evil-visual-selection 'line))
                            (format "%dL" lines))
                           ((> lines 1)
                            (format "%dC %dL" (- end beg) lines))
                           (t
                            (format "%dC" (- end beg))))
                     (when doom-modeline-enable-word-count
                       (format " %dW" (count-words beg end)))
                     (doom-modeline-spc)))
           'face 'doom-modeline-highlight)
        (force-mode-line-update)))))

seagle0128 avatar Oct 02 '21 15:10 seagle0128

Same story: image

JordanAnthonyKing avatar Oct 02 '21 17:10 JordanAnthonyKing

I did some troubleshootings today, and found the root cause is region-begining and region-end weren't updated timely in this case. No idea how to address it.

seagle0128 avatar Oct 03 '21 05:10 seagle0128

Is there no hook we can add to update these variables?

JordanAnthonyKing avatar Oct 04 '21 10:10 JordanAnthonyKing

region-begining and region-end are built-in C functions.

Try this snippet:

(doom-modeline-def-segment selection-info
  (when (and (or mark-active (and (bound-and-true-p evil-local-mode)
                                  (eq evil-state 'visual)))
             (doom-modeline--active))
    (format " %d %d" (region-beginning) (region-end))))

seagle0128 avatar Oct 04 '21 10:10 seagle0128

That outputs this: image But also doesn't update.

JordanAnthonyKing avatar Oct 05 '21 16:10 JordanAnthonyKing

That demonstrates (region-beginning) and (region-end) are not changed while moving the cursor in the region. I am not sure if it's as designed. I am afraid having to inquiry to Emacs maintainers.

seagle0128 avatar Oct 05 '21 16:10 seagle0128

¯\(ツ)

JordanAnthonyKing avatar Oct 05 '21 21:10 JordanAnthonyKing

@JordanAnthonyKing Did you upgraded to 28.2? Are you still seeing this issue? I tried with 28 and 29 recently and no issue there.

seagle0128 avatar Oct 08 '22 11:10 seagle0128

I've kept the segment disabled in my modeline since :L I'll take a look tomorrow.

JordanAnthonyKing avatar Oct 18 '22 21:10 JordanAnthonyKing

@seagle0128 I have encountered the same bug. I used the following configuration to reproduce it:

(require 'package)

(setq debug-on-error t
      no-byte-compile t
      byte-compile-warnings nil
      inhibit-startup-screen t
      package-archives '(("melpa" . "https://melpa.org/packages/")
                         ("gnu" . "https://elpa.gnu.org/packages/"))
      package-user-dir (make-temp-file "min-init")
      custom-file (expand-file-name "custom.el" package-user-dir))

(delete-file package-user-dir)
(add-hook 'kill-emacs-hook `(lambda ()
                              (delete-directory ,package-user-dir t)))

(let* ((pkg-list '(evil doom-modeline)))

  (package-initialize)
  (package-refresh-contents)

  (mapc (lambda (pkg)
          (unless (package-installed-p pkg)
            (package-install pkg))
          (require pkg))
        pkg-list))

(doom-modeline-mode)
(evil-mode)
(line-number-mode -1)

(run-with-timer 0.1 0.1 (lambda () (when (region-active-p) (message "%s" (region-end)))))

I'm using Emacs 30.0.50 on Linux.

I could track the bug down to the line-number-mode. If I remove the line (line-number-mode -1) or enable line-number-mode after starting emacs the bug disappears and the selection-info is updated accordingly.

Using the timer above I could also see that (region-end) is updated accordingly (at least in the normal visual column mode), unlike earlier suspected. So I assume this is indeed an error with doom-modeline and not with emacs.

SPFabGerman avatar Aug 19 '23 18:08 SPFabGerman

@SPFabGerman I believe this issue has addressed in f45a5a2.

seagle0128 avatar Aug 20 '23 10:08 seagle0128