How to compose region like “<?php foo; bar; ?>”

前端 未结 2 1342
感动是毒
感动是毒 2021-01-24 08:22

I want fold php block in php template file.

  

  

相关标签:
2条回答
  • 2021-01-24 08:50

    So, you want to input a P and it should output something like <?php xxx; xxx; ?>? Did I understand your question correctly?

    In that case, check out yasnippet (more info on emacswiki). It lets you define your own snippets (the syntax is really easy) like P, let it have placeholders (so you can replace xxx; and xxx; with something else when calling the snippet) and such. It's really handy. Just create a new snippet (the name of the file dictates it's shortcut, ie, name it P it this example) and input something like:

    # name: My own P-snippet.
    # --
    <?php ${first}; ${second}; ?>
    

    No need to make own font-locks, no need to use regexp. Just create a new snippet. After creating (and loading it, I usually restart emacs since I'm lazy) your snippet, open up a php-mode buffer, type P and press tab. Magic! :)

    0 讨论(0)
  • 2021-01-24 08:57

    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 …)))
    
    0 讨论(0)
提交回复
热议问题