0

Parsing parentheses: smie vs syntax table gives one example of how to use syntax-propertize-function but I wonder if someone could help me with another example. I'm working with clojure-mode. Here's a recipe to reproduce the problem:

;; load with emacs -Q -l clojure-init.el

(package-initialize nil)
(setq package-enable-at-startup nil)
(unless (assoc-default "melpa" package-archives)
  (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t)
  (package-refresh-contents))

(unless (package-installed-p 'use-package)
  (package-install 'use-package))

(setq use-package-verbose t)
(require 'use-package)

(use-package clojure-mode
  :ensure t :defer t)

Then M-x clojure-mode RET and insert

(["phonemes" java.lang.String] ["rhymingPair" [Ljava.lang.String;])

You'll see that backward-sexp / forward-sexp breaks in the viscinity of the [Ljava... and that the last few characters are understood as a comment. More specifically, ["rhymingPair" [Ljava.lang.String;] should be understood as one valid sexp.

Joe Corneli
  • 1,786
  • 1
  • 14
  • 27
  • Unless I missed out on some secret clojure-syntax, what you've entered is no valid clojure code and the last few characters are indeed a comment. – politza Jun 19 '15 at 21:09
  • `(type (into-array String ["Awesome"]))` returns `[Ljava.lang.String;` and `(list (type (into-array String ["Awesome"])) 1)` returns `([Ljava.lang.String; 1)`. So if you believe in [homoiconicity](https://en.wikipedia.org/wiki/Homoiconicity) then it is valid code. That said, `([Ljava.lang.String; 1)` can't be entered on the REPL in CIDER. I don't know if that's a CIDER bug or a _bona fide_ feature of Clojure. The main point is that I have a bunch of output from Clojure that contains this symbol, and I'd like to be able to navigate it in Emacs via forward- and backward-sexp commands. – Joe Corneli Jun 19 '15 at 23:34
  • `if you believe in homoiconicity ` -- You've watched to many clojure videos. – politza Jun 20 '15 at 09:48
  • "I don't know if that's a CIDER bug or a _bona fide_ feature of Clojure." Then start a regular repl and try it. ;-) – Malabarba Jun 21 '15 at 00:31
  • I did, and also raised this thread https://groups.google.com/forum/#!topic/clojure/F8isrR-u-qQ getting a pointer to https://bitbucket.org/morgon/jfreeze – Joe Corneli Jun 21 '15 at 14:49

1 Answers1

2
(defun clojure-syntax-propertize-array-class (beg end)
  "Put word `syntax-table' property on \"[Lfoo.Bar;\"."
  (let ((word-syntax (string-to-syntax "w")))
    (put-text-property
     beg end 'syntax-table nil)
    (save-excursion
      (goto-char beg)
      (while (re-search-forward
              (rx (and (group ?\[)
                       ?L
                       (* (or (syntax ?w) ?.))
                       (group ?\;)))
              end t)
        (dotimes (i 2)
          (put-text-property
           (match-beginning (1+ i))
           (match-end (1+ i))
           'syntax-table word-syntax))))))

(define-minor-mode clojure-weird-syntax-mode
  nil nil nil nil
  (cond
   (clojure-weird-syntax-mode
    (jit-lock-register 'clojure-syntax-propertize-array-class)
    (setq-local parse-sexp-lookup-properties t))
   (clojure-weird-syntax-mode
    (jit-lock-unregister 'clojure-syntax-propertize-array-class)
    (kill-local-variable 'parse-sexp-lookup-properties))))
politza
  • 3,316
  • 14
  • 16