git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v7 3/3] git-jump: invoke emacs/emacsclient

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Nov 25, 2022, 08:55 UTC
Message-ID
<221125.8635a7o123.gmgdl@evledraar.gmail.com>
In-Reply-To
<d8233f9617563d7c7168afc6e1abfaba57e54038.1669347422.git.gitgitgadget@gmail.com>
On Fri, Nov 25 2022, Yoichi Nakayama via GitGitGadget wrote:
Show 37 quoted lines
> From: Yoichi Nakayama <yoichi.nakayama@gmail.com>
>
> It works with GIT_EDITOR="emacs", "emacsclient" or "emacsclient -t"
>
> Signed-off-by: Yoichi Nakayama <yoichi.nakayama@gmail.com>
> ---
>  contrib/git-jump/git-jump | 17 ++++++++++++++++-
>  1 file changed, 16 insertions(+), 1 deletion(-)
>
> diff --git a/contrib/git-jump/git-jump b/contrib/git-jump/git-jump
> index cc97b0dcf02..eef9cda832f 100755
> --- a/contrib/git-jump/git-jump
> +++ b/contrib/git-jump/git-jump
> @@ -23,7 +23,22 @@ EOF
>  
>  open_editor() {
>  	editor=`git var GIT_EDITOR`
> -	eval "$editor -q \$1"
> +	case "$editor" in
> +	*emacs*)
> +		# Supported editor values are:
> +		# - emacs
> +		# - emacsclient
> +		# - emacsclient -t
> +		#
> +		# Wait for completion of the asynchronously executed process
> +		# to avoid race conditions in case of "emacsclient".
> +		eval "$editor --eval \"(let ((buf (compilation-start \\\"cat \$1\\\" 'grep-mode))) (pop-to-buffer buf) (select-frame-set-input-focus (selected-frame)) (while (get-buffer-process buf) (sleep-for 0.1)))\""
> +		;;
> +	*)
> +		# assume anything else is vi-compatible
> +		eval "$editor -q \$1"
> +		;;
> +	esac
>  }
>  
>  mode_diff() {

I'd really like to have some closer and smarter emacs integration like this.

But I don't see why we need to run the grep ourselves, pipe it to a temporary file, and then discover that we're using emacs, and --eval code into it to switch to that buffer, and fake up a "M-x grep" command with a compilation buffer to make it look like we ran M-x grep in the first place.

Let's just ... run M-x grep earlier? Then we can skip all the earlier steps.

I experimented with this a bit locally, and I didn't get the "switch to
buffer" semantics to work with this (but that's presumably easy, I'm
just rusty on my elisp APIs), but something in this direction seems much
better:
	
	diff --git a/contrib/git-jump/git-jump b/contrib/git-jump/git-jump
	index eef9cda832f..c932d7acd0f 100755
	--- a/contrib/git-jump/git-jump
	+++ b/contrib/git-jump/git-jump
	@@ -22,23 +22,7 @@ EOF
	 }
	 
	 open_editor() {
	-	editor=`git var GIT_EDITOR`
	-	case "$editor" in
	-	*emacs*)
	-		# Supported editor values are:
	-		# - emacs
	-		# - emacsclient
	-		# - emacsclient -t
	-		#
	-		# Wait for completion of the asynchronously executed process
	-		# to avoid race conditions in case of "emacsclient".
	-		eval "$editor --eval \"(let ((buf (compilation-start \\\"cat \$1\\\" 'grep-mode))) (pop-to-buffer buf) (select-frame-set-input-focus (selected-frame)) (while (get-buffer-process buf) (sleep-for 0.1)))\""
	-		;;
	-	*)
	-		# assume anything else is vi-compatible
	-		eval "$editor -q \$1"
	-		;;
	-	esac
	+	eval "$editor -q \$1"
	 }
	 
	 mode_diff() {
	@@ -83,11 +67,14 @@ mode_ws() {
	 }
	 
	 use_stdout=
	+use_magic=t
	 while test $# -gt 0; do
	 	case "$1" in
	 	--stdout)
	 		use_stdout=t
	-		shift
	+		;;
	+	--no-magic)
	+		use_magic=
	 		;;
	 	--*)
	 		usage >&2
	@@ -96,7 +83,8 @@ while test $# -gt 0; do
	 	*)
	 		break
	 		;;
	-	esac
	+	esac &&
	+	shift
	 done
	 if test $# -lt 1; then
	 	usage >&2
	@@ -105,6 +93,22 @@ fi
	 mode=$1; shift
	 type "mode_$mode" >/dev/null 2>&1 || { usage >&2; exit 1; }
	 
	+editor=`git var GIT_EDITOR`
	+if test "$use_magic" && test "$mode" = "grep"
	+then
	+	case "$editor" in
	+	*emacs*)
	+		set -x
	+		eval "$editor --eval \" \
	+			(grep \\\"git grep -H "$@"\\\") \
	+		\""
	+		exit
	+		;;
	+	*)
	+		;;
	+	esac
	+fi
	+
	 if test "$use_stdout" = "t"; then
	 	"mode_$mode" "$@"
	 	exit 0

I.e. if we're going to trust emacs to eval this code, and assume that grep.el etc. works, let's just run the equivalent of M-x grep with our 'git grep' command.

This is already better in that "grep" understands that I searched for "foo.*bar", so that's highlighted in the resulting buffer, just as with normal "grep" commands.

This is missing the bit where we'd need to jump.grepCmd etc, so it's incomplete.

I think this is all the prior art we'd need to invoke "git grep" the right way from emacs's "grep": https://github.com/eglaysher/find-things-fast/blob/master/find-things-fast.el#L246

B.t.w. I'd think the "--no-magic" here is something you'd want too. I like this new behavior (sans the comments above), but presumably there's people using emacs as their EDITOR who don't want to have this magic behavior, having an opt-out would be neat.

I.e. if you have an existing customization intercepting these then this will screw with that, but maybe that's OK...

Previous: Yoichi Nakayama via GitGitGadgetNext: Yoichi Nakayama
Message 55 of 68 in “git-jump: support Emacs”
  1. 0/2 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 19, 2022
  2. 1/2 git-jump: add an optional argument 'stdout'Yoichi Nakayama via GitGitGadget, Nov 19, 2022
  3. 2/2 git-jump: invoke emacsclientYoichi Nakayama via GitGitGadget, Nov 19, 2022
  4. Eric SunshineNov 19, 2022
  5. Yoichi NakayamaNov 19, 2022
  6. Eric SunshineNov 19, 2022
  7. Yoichi NakayamaNov 21, 2022
  8. 0/2 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 20, 2022
  9. 1/2 git-jump: add an optional argument 'stdout'Yoichi Nakayama via GitGitGadget, Nov 20, 2022
  10. Junio C HamanoNov 21, 2022
  11. Yoichi NakayamaNov 21, 2022
  12. Jeff KingNov 21, 2022
  13. 2/2 git-jump: invoke emacsclientYoichi Nakayama via GitGitGadget, Nov 20, 2022
  14. 0/2 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 21, 2022
  15. 1/2 git-jump: add an optional argument '--stdout'Yoichi Nakayama via GitGitGadget, Nov 21, 2022
  16. Jeff KingNov 21, 2022
  17. Junio C HamanoNov 21, 2022
  18. Yoichi NakayamaNov 22, 2022
  19. Jeff KingNov 22, 2022
  20. Yoichi NakayamaNov 22, 2022
  21. 2/2 git-jump: invoke emacs/emacsclientYoichi Nakayama via GitGitGadget, Nov 21, 2022
  22. Jeff KingNov 21, 2022
  23. Yoichi NakayamaNov 22, 2022
  24. 0/2 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 22, 2022
  25. 1/2 git-jump: add an optional argument '--stdout'Yoichi Nakayama via GitGitGadget, Nov 22, 2022
  26. Jeff KingNov 22, 2022
  27. 2/2 git-jump: invoke emacs/emacsclientYoichi Nakayama via GitGitGadget, Nov 22, 2022
  28. Phillip WoodNov 22, 2022
  29. Yoichi NakayamaNov 23, 2022
  30. Jeff KingNov 22, 2022
  31. Yoichi NakayamaNov 23, 2022
  32. Jeff KingNov 24, 2022
  33. Yoichi NakayamaNov 24, 2022
  34. Yoichi NakayamaNov 24, 2022
  35. Jeff KingNov 24, 2022
  36. Jeff KingNov 24, 2022
  37. Yoichi NakayamaNov 25, 2022
  38. 0/3 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 23, 2022
  39. 1/3 git-jump: add an optional argument '--stdout'Yoichi Nakayama via GitGitGadget, Nov 23, 2022
  40. 2/3 git-jump: move valid-mode check earlierJeff King via GitGitGadget, Nov 23, 2022
  41. 3/3 git-jump: invoke emacs/emacsclientYoichi Nakayama via GitGitGadget, Nov 23, 2022
  42. Phillip WoodNov 23, 2022
  43. Yoichi NakayamaNov 23, 2022
  44. Jeff KingNov 24, 2022
  45. 0/3 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 24, 2022
  46. 1/3 git-jump: add an optional argument '--stdout'Yoichi Nakayama via GitGitGadget, Nov 24, 2022
  47. 2/3 git-jump: move valid-mode check earlierJeff King via GitGitGadget, Nov 24, 2022
  48. 3/3 git-jump: invoke emacs/emacsclientYoichi Nakayama via GitGitGadget, Nov 24, 2022
  49. 0/3 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 25, 2022
  50. 1/3 git-jump: add an optional argument '--stdout'Yoichi Nakayama via GitGitGadget, Nov 25, 2022
  51. Ævar Arnfjörð BjarmasonNov 25, 2022
  52. Yoichi NakayamaNov 27, 2022
  53. 2/3 git-jump: move valid-mode check earlierJeff King via GitGitGadget, Nov 25, 2022
  54. 3/3 git-jump: invoke emacs/emacsclientYoichi Nakayama via GitGitGadget, Nov 25, 2022
  55. Ævar Arnfjörð BjarmasonNov 25, 2022
  56. Yoichi NakayamaNov 25, 2022
  57. Ævar Arnfjörð BjarmasonNov 25, 2022
  58. Jeff KingNov 28, 2022
  59. Ævar Arnfjörð BjarmasonNov 28, 2022
  60. Yoichi NakayamaNov 28, 2022
  61. Yoichi NakayamaNov 27, 2022
  62. 0/3 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 27, 2022
  63. 1/3 git-jump: add an optional argument '--stdout'Yoichi Nakayama via GitGitGadget, Nov 27, 2022
  64. 2/3 git-jump: move valid-mode check earlierJeff King via GitGitGadget, Nov 27, 2022
  65. 3/3 git-jump: invoke emacs/emacsclientYoichi Nakayama via GitGitGadget, Nov 27, 2022
  66. Phillip WoodNov 28, 2022
  67. Junio C HamanoNov 28, 2022
  68. Yoichi NakayamaNov 29, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.