Skip to content

Incorrect cursor placement in minibuffer when prefixed #159

@Raphus-cucullatus

Description

@Raphus-cucullatus

I sometimes want to pass additional argument to ag. According to the DOCSTRING,

If called with a prefix, prompts for flags to pass to ag.

It works well when my search string contains no shell-quote characters. For example, if I use C-u M-x ag, and search "struct", command args will be prompted in the minibuffer with the point/cursor placed at the end of other args and before the "--" and wait for my input (Here I use <cursor> to indicate the cursor position):

ag command: ag --literal --group --line-number --column --color --color-match 30\;43 --color-path 1\;32 --smart-case --stats <cursor> -- struct .

However, if my search string contains shell-quote characters, the cursor is placed weirdly:

  1. if I use C-u M-x ag, and search "struct kvm":
ag command: ag --literal --group --line-number --column --color --color-match 30\;43 --color-path 1\;32 --smart-case --stats  <cursor>-- struct\ kvm .
  1. if there are more shell-quote characters, for example, search "struct kvm {":
ag command: ag --literal --group --line-number --column --color --color-match 30\;43 --color-path 1\;32 --smart-case --stats  --<cursor> struct\ kvm\ \{ .

The cursor is placed after the "--"!

If I want to add additional argument, I have to move the cursor back before "--". It seems that the cursor position in the minibuffer is incorrectly calculated.

My guess

In ag/search, the point position is calculated as follows:

ag.el/ag.el

Line 262 in bd81d68

(let ((adjusted-point (- (length command-string) (length string) 5)))

However, here command-string is quoted, while string is unquoted. So direct subtraction does not take the backslash into account. In my case, command-string is ag --literal --group ... struct\ kvm\ \{ ., while string is struct kvm {.

Subtracting by 5 is also incorrect after the introduction of the commit bd81d68 since the length after "--" can be arbitrary due to :files.

Other Information

On my machine, the output of ag --version is

ag version 2.2.0

Features:
  +jit +lzma +zlib

My Emacs version is 26.1 (Download from emacsformacosx).

I'm using: macOS Mojave 10.14.6.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions