0

I'm a beginner in Emacs and Elisp, but do have programming experience.

My question is quite straightforward, I want to have a command open-git that when in a git directory, it opens the url inside a browser.

I have the following code

(defun open-git ()
  "Opens the git repository in a browser."
  (interactive)
  (browse-url (replace-regexp-in-string
               "git@\\(.*\\):\\(.*\\)/\\(.*\\).git", ; Regex
                "https://\\1/\\2/\\3.git/", ; Replacement
                (shell-command-to-string "git remote get-url origin")) ; String
              )
  )

The git repository url is in a ssh format, meaning for browse-url to work correctly, I have to regex replace it. I have used M-x regexp-builder to confirm it matches correctly. However, when trying to execute the command from a file buffer, it gives me the error Symbol's function definition is void: \,. Reading up on it, I saw that \, is used to run regex interactively for the replacement. But I'm not using \,, is it being used internally?

On a git url that is for example [email protected]:foo/bar.git the matches are:

  1. github.com
  2. foo
  3. bar

Obviously I can just set the git url to the https version and be done with it. But I also just want to understand Emacs and Elisp better, so figured this was a nice easy exercise.

1

1 Answer 1

2

Remove the commas after the regex and the replacement

(defun open-git ()
  "Opens the git repository in a browser."
  (interactive)
  (browse-url (replace-regexp-in-string
               "git@\\(.*\\):\\(.*\\)/\\(.*\\).git" ; Regex
                "https://\\1/\\2/\\3.git/" ; Replacement
                (shell-command-to-string "git remote get-url origin")))) ; String
5
  • OML, was that the issue? Thank you so much! I would expect Flycheck or similar to highlight it as a syntax issue. Commented Feb 10 at 12:52
  • 1
    Commas are valid syntax in elisp, but not for separating list items. The byte-compiler does complain about your original code: "Warning: the function ‘,’ is not known to be defined." (which makes sense if you understand that the lisp reader is producing the likes of (\, "https://\\1/\\2/\\3.git/") from your code (which in turn is because of the way that commas are expected to be used, within backquoted forms).
    – phils
    Commented Feb 10 at 23:53
  • N.b. The interactive regexp replacement case (introduced in Emacs 22.1) is something different -- for that case \, was simply selected as the syntax for evaluating the following expression as lisp code in order to be a syntactic analogue of the pre-existing use of a comma for evaluating the following expression within a backquoted form (with the latter being what you were effectively running into here).
    – phils
    Commented Feb 11 at 0:08
  • 1
    Your take-away is probably that if you ever see errors mentioning commas, the first thing to do is check whether you've accidentally used them as list separators :)
    – phils
    Commented Feb 11 at 0:13
  • I suppose my experience with other languages is what was my downfall with this, I will keep in mind that lists should not be separated with commas in lisp Commented Feb 11 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.