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.

  • 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 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 9: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 0:31
  • I did, and also raised this thread groups.google.com/forum/#!topic/clojure/F8isrR-u-qQ getting a pointer to bitbucket.org/morgon/jfreeze – Joe Corneli Jun 21 '15 at 14:49
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))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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