all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Dmitry Gutov <dgutov@yandex.ru>
To: emacs-devel@gnu.org
Subject: Re: General delimited literals in ruby-mode patch
Date: Fri, 10 Feb 2012 04:42:02 +0400	[thread overview]
Message-ID: <87k43vecyt.fsf@yandex.ru> (raw)
In-Reply-To: 8739ammd8l.fsf@yandex.ru

[-- Attachment #1: Type: text/plain, Size: 746 bytes --]

Dmitry Gutov <dgutov@yandex.ru> writes:

> The bug: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=6286.

I missed an entry for general delimited strings in
`font-lock-ruby-keywords'. It didn't break anything, just did
unnecessary work in the certain narrow case which it supported.

Now moved it to `ruby-font-lock-syntactic-keywords' with appropriate
conversion.

Also removed `ruby-general-delimiter-beg-re' as a separate variable,
since its value is used only once.

Not sure if I should touch `ruby-parse-partial', `ruby-forward-sexp',
`ruby-backward-sexp', and `ruby-expr-beg'.

P.S.
Why is the case of (fboundp #'syntax-propertize-rules) being nil still
being handled, by the way?
This ruby-mode is not compatible with Emacs 23 either way.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: general delimited literals patch v2 --]
[-- Type: text/x-patch, Size: 4920 bytes --]

diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
index bf26497..e93f8b3 100644
--- a/lisp/progmodes/ruby-mode.el
+++ b/lisp/progmodes/ruby-mode.el
@@ -793,8 +793,8 @@ and `\\' when preceded by `?'."
 ;;                           (not (or (eolp) (looking-at "#")
 ;;                                    (and (eq (car (nth 1 state)) ?{)
 ;;                                         (looking-at "|"))))))
-                       (or (not (eq ?/ c))
-                           (null (nth 0 (ruby-parse-region (or begin parse-start) (point)))))
+                       ;; not a regexp or general delimited literal
+                       (null (nth 0 (ruby-parse-region (or begin parse-start) (point))))
                        (or (not (eq ?| (char-after (point))))
                            (save-excursion
                              (or (eolp) (forward-char -1))
@@ -1117,6 +1117,7 @@ See `add-log-current-defun-function'."
         "Syntactic keywords for Ruby mode.  See `syntax-propertize-function'."
         (goto-char start)
         (ruby-syntax-propertize-heredoc end)
+        (ruby-syntax-general-delimiters-goto-beg)
         (funcall
          (syntax-propertize-rules
           ;; #{ }, #$hoge, #@foo are not comments
@@ -1136,7 +1137,10 @@ See `add-log-current-defun-function'."
           ("^\\(=\\)begin\\_>" (1 "!"))
           ;; Handle here documents.
           ((concat ruby-here-doc-beg-re ".*\\(\n\\)")
-           (7 (prog1 "\"" (ruby-syntax-propertize-heredoc end)))))
+           (7 (prog1 "\"" (ruby-syntax-propertize-heredoc end))))
+          ;; Handle percent literals: %w(), %q{}, etc.
+          ("\\(?:^\\|[[ \t\n<+(,=]\\)\\(%\\)[qQrswWx]?\\([[:punct:]]\\)"
+           (1 (prog1 "|" (ruby-syntax-propertize-general-delimiters end)))))
          (point) end))
 
       (defun ruby-syntax-propertize-heredoc (limit)
@@ -1162,6 +1166,41 @@ See `add-log-current-defun-function'."
               ;; Make extra sure we don't move back, lest we could fall into an
               ;; inf-loop.
               (if (< (point) start) (goto-char start))))))
+
+      (defun ruby-syntax-general-delimiters-goto-beg ()
+        (let ((state (syntax-ppss)))
+          ;; Move to the start of the literal, in case it's multiline.
+          ;; TODO: determine the literal type more reliably here?
+          (when (eq t (nth 3 state))
+            (goto-char (nth 8 state))
+            (beginning-of-line))))
+
+      (defun ruby-syntax-propertize-general-delimiters (limit)
+        (goto-char (match-beginning 2))
+        (let* ((op (char-after))
+               (ops (char-to-string op))
+               (cl (or (cdr (aref (syntax-table) op))
+                       (cdr (assoc op '((?< . ?>))))))
+               parse-sexp-lookup-properties)
+          (ignore-errors
+            (if cl
+                (progn  ; paired delimiters
+                  ;; Delimiter pairs of the same kind can be nested
+                  ;; inside the literal, as long as they are balanced.
+                  ;; Create syntax table that ignores other characters.
+                  (with-syntax-table (make-char-table 'syntax-table nil)
+                    (modify-syntax-entry op (concat "(" (char-to-string cl)))
+                    (modify-syntax-entry cl (concat ")" ops))
+                    (modify-syntax-entry ?\\ "\\")
+                    (save-restriction
+                      (narrow-to-region (point) limit)
+                      (forward-list))))  ; skip to the paired character
+              ;; single character delimiter
+              (re-search-forward (concat "[^\\]\\(?:\\\\\\\\\\)*"
+                                         (regexp-quote ops)) limit nil))
+            ;; if we reached here, the closing delimiter was found
+            (put-text-property (1- (point)) (point)
+                               'syntax-table (string-to-syntax "|")))))
       )
 
   ;; For Emacsen where syntax-propertize-rules is not (yet) available,
@@ -1206,6 +1245,10 @@ This should only be called after matching against `ruby-here-doc-end-re'."
      (4 (7 . ?/))
      (6 (7 . ?/)))
     ("^=en\\(d\\)\\_>" 1 "!")
+    ;; general delimited string
+    ("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
+     (3 "\"")
+     (5 "\""))
     ("^\\(=\\)begin\\_>" 1 (ruby-comment-beg-syntax))
     ;; Currently, the following case is highlighted incorrectly:
     ;;
@@ -1414,9 +1457,6 @@ See `font-lock-syntax-table'.")
      1 font-lock-variable-name-face)
    '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
      0 font-lock-variable-name-face)
-   ;; general delimited string
-   '("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
-     (2 font-lock-string-face))
    ;; constants
    '("\\(^\\|[^_]\\)\\b\\([A-Z]+\\(\\w\\|_\\)*\\)"
      2 font-lock-type-face)

  reply	other threads:[~2012-02-10  0:42 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-07 23:32 General delimited literals in ruby-mode patch Dmitry Gutov
2012-02-10  0:42 ` Dmitry Gutov [this message]
2012-02-10  5:03   ` Dmitry Gutov
2012-04-24 17:09     ` bug#6286: " Stefan Monnier
2012-04-25  3:03       ` Dmitry Gutov
2012-04-24 15:43   ` Stefan Monnier
2012-04-24 23:46     ` Dmitry Gutov
2012-04-25 14:15       ` Stefan Monnier
     [not found]         ` <4F981DEA.6060700@yandex.ru>
     [not found]           ` <jwv4ns7iubj.fsf-monnier+emacs@gnu.org>
2012-04-28 20:20             ` Dmitry Gutov
2012-05-03  5:39             ` Dmitry Gutov
2012-08-14  3:56               ` Dmitry Gutov
2012-08-14 12:40                 ` Stefan Monnier
2012-08-14 17:46                   ` Dmitry Gutov
2012-08-15  2:33                     ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k43vecyt.fsf@yandex.ru \
    --to=dgutov@yandex.ru \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.