如何组合区域,如“<?php foo;酒吧; ?>" [英] How to compose region like "<?php foo; bar; ?>"

查看:96
本文介绍了如何组合区域,如“<?php foo;酒吧; ?>"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在php模板文件中使用php块。

 
< html>
<?php
$ bar = foo();
echo $ bar;
?>
< h1><?php echo $ title; ?>< / H1>
< / html>

到>>>>>

< html>
{{PHP}}
< h1> {{PHP}}< / h1>
< / html>

首先,我尝试使用font-lock-add-keywords,它失败了。
非常感谢@Gilles(支持?建议?我不知道哪个字应该在这里,对不起)。

 
(eval-after-load'php-mode
'(progn
(setq font-lock-multiline t)
(font-lock-add-keywords
'php-模式`((\\(<?php。*?> \\)(
(0(progn(compose-region(match-beginning 1))
-end 1)の)
nil)))))))

然后,








(开始缓冲区)
(while(re-search-forward\\(< \\?php\\(.\\ | \ $ n $ t $ b $;(make-overlay(match-beginning 0)(match-end 0))
;;(hide-region-hide)
;;'hide-region-hide'不起作用,所以t ry compos-region
(compose-region(match-beginning 0)
(match-end 0)
;; {{PHP}};使用词不能完美。




,谢谢XD

解决方案

您的正则表达式有一个简单的错误:是一个特殊字符,但是你想要字面上匹配,所以你需要一个反斜杠。请注意,字符串文字引用和正则表达式引用是正交的,因此正则表达式反斜杠需要在字符串文字中加倍。



另外,正则表达式是贪心的,所以。* 部分可能比您想要的更多,如果您再次发生?>(),如果您更换 by [\\\
>]
,这样可以防止比赛超出第一个> 如果您使用Emacs≥23,您可以使用非贪心的操作符,尽可能早地停止匹配:。*?

 \\(< \\?php [^ \\\
>] * \\?> \\)(

这将显示类似<?php foo bar?>( as Ƥ(



正则表达式中的反斜杠圆括号 \( ... \)定义一个组; (match-beginning 1)(match-end 1) retur在第一(和唯一)组的边界位置。



正则表达式的文档位于 Emacs手册



如果你希望比赛能够跨越多个行,您需要 [^>] * \\(.\\ | \\\
\\) *?
在正则表达式中。另外,您必须告诉Font Lock库以多行方式扩展其搜索(出于效率原因,默认情况下,它会限制行末尾的所有搜索)。使用这个:

 (eval-after-load'php-mode 
'(progn
font-lock-multiline t)
(font-lock-add-keywords ...)))


I want fold php block in php template file.

  <html>
<?php
  $bar = foo();
  echo $bar;
?>
  <h1><?php echo $title; ?></h1>
  </html>

to >>>>

  <html>
  {{PHP}}
  <h1>{{PHP}}</h1>
  </html>

First, I try to use font-lock-add-keywords, it failed. Many thanks to @Gilles for (support? advice? I don't know which word should be here, sorry).

(eval-after-load 'php-mode
  '(progn
     (setq font-lock-multiline t)
     (font-lock-add-keywords
      'php-mode `(("\\(<?php .* ?>\\)("
                   (0 (progn (compose-region (match-beginning 1)
                                             (match-end 1) "の")
                             nil)))))))

Then, I try below, It works.

(defun lot/php-hide ()
  "compose php block in buffer"
  (interactive)
  (beginning-of-buffer)
  (while (re-search-forward "\\(<\\?php\\(.\\|\n\\)*?\\?>\\)" nil t)
    ;; (make-overlay (match-beginning 0) (match-end 0))
    ;; (hide-region-hide)
    ;; 'hide-region-hide' doesn't work, so try compose-region
    (compose-region (match-beginning 0)
                    (match-end 0)
                    ;; "{{PHP}}" ; use word can't work perfect.
                    "の"
                    )
    )
  )

Show me the error if have, Thank you XD

解决方案

There's a simple mistake in your regexp: ? is a special character, but you want to match it literally, so you need a backslash before it. Note that string literal quoting and regexp quoting are orthogonal, so the regexp backslash needs to be doubled in a string literal.

Additionally, regexps are greedy, so the .* part may match more than you intended if you have another occurence of ?>( later on the line. If you replace . by [\n>], this will prevent the match from extending beyond the first >. If you use Emacs ≥23, you can use a non-greedy operator instead, to stop the match as early as possible: .*?.

"\\(<\\?php [^\n>]* \\?>\\)("

This will show things like <?php foo bar?>( as Ƥ(.

The backslashed parentheses \(\) in a regexp delimit a group; (match-beginning 1) and (match-end 1) return the boundary positions for the first (and only) group.

The documentation of regexps is in the Emacs manual.

If you want the match to extend across multiple lines, you need [^>]* or \\(.\\|\n\\)*? in the regexp. Additionally, you must tell the Font Lock library to extend its searches on multiple lines (by default, it limits all searches at the end of the line, for efficiency reasons). Use this:

 (eval-after-load 'php-mode
   '(progn
      (setq font-lock-multiline t)
      (font-lock-add-keywords …)))

这篇关于如何组合区域,如“&lt;?php foo;酒吧; ?&GT;&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆