Wilfred / ag.el

An Emacs frontend to The Silver Searcher
http://agel.readthedocs.org/en/latest/
525 stars 61 forks source link

Incorrect cursor placement in minibuffer when prefixed #159

Open Raphus-cucullatus opened 5 years ago

Raphus-cucullatus commented 5 years ago

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: https://github.com/Wilfred/ag.el/blob/bd81d68466e44301505629454dfc689b6c17d94b/ag.el#L262

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 bd81d68466e44301505629454dfc689b6c17d94b 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.