{"id":10020824,"date":"2025-06-11T01:20:37","date_gmt":"2025-06-10T17:20:37","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020824"},"modified":"2025-06-11T01:20:37","modified_gmt":"2025-06-10T17:20:37","slug":"%e8%ae%a9-emacs-%e7%95%a5%e5%b8%a6%e6%84%9f%e6%80%a7","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020824","title":{"rendered":"\u8ba9 Emacs \u7565\u5e26\u611f\u6027"},"content":{"rendered":"<blockquote><p>=&gt; <strong>\u4e0a\u4e00\u7bc7<\/strong>\uff1a<a href=\"https:\/\/segmentfault.com\/a\/1190000046618396\" target=\"_blank\" rel=\"noopener noreferrer\">\u5728 Emacs \u7f13\u51b2\u533a\u91cc\u884c\u8d70\u7684\u59ff\u52bf<\/a><\/p><\/blockquote>\n<h2>\u524d\u8a00<\/h2>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5c1d\u8bd5\u7528 Elisp \u7f16\u7a0b\u6765\u89e3\u4e00\u9053\u5e94\u7528\u9898\u3002\u8fd9\u9053\u5e94\u7528\u9898\u5bf9\u6211\u800c\u8a00\uff0c\u9887\u4e3a\u91cd\u8981\uff0c\u5bf9\u4f60\u800c\u8a00\uff0c\u53ef\u4f5c\u5b66\u4e60 Elisp \u7f16\u7a0b\u4e00\u4f8b\u3002\u5047\u8bbe Emacs \u7684\u5f53\u524d\u7f13\u51b2\u533a\u5185\u5b58\u5728\u4e00\u4e9b\u5f62\u5982\u4ee5\u4e0b\u5185\u5bb9\u7684\u7247\u6bb5\uff1a<\/p>\n<pre><code class=\"c\">@ \u8fd9\u662f\u4e00\u6bb5 C \u4ee3\u7801 #\nint foo(void) {\n    return 42;\n}\n@<\/code><\/pre>\n<p>\u540c\u65f6\uff0c\u5f53\u524d\u7f13\u51b2\u533a\u5185\u4e5f\u6709\u4e00\u4e9b\u5176\u4ed6\u5185\u5bb9\uff0c\u4f46\u6211\u4eec\u65e0\u9700\u5173\u5fc3\u3002\u73b0\u5728\uff0c\u5149\u6807\u662f\u843d\u5728\u4e0a\u8ff0\u7247\u6bb5\u5185\u7684\uff0c\u4f8b\u5982\u843d\u5728\u6570\u5b57 <code>42<\/code> \u7684 <code>4<\/code> \u4e0a\u3002\u6211\u4eec\u770b\u5230\u7684\u73b0\u8c61\u662f\u5982\u6b64\uff0c\u4f46\u662f\u80fd\u5426\u901a\u8fc7 Elisp \u7a0b\u5e8f\u611f\u77e5\u5149\u6807\u6b63\u5904\u4e8e\u8fd9\u6837\u7684\u533a\u57df\u5185\u5462\uff1f<\/p>\n<p>\u4e3a\u4e86\u8ba9\u95ee\u9898\u66f4\u660e\u786e\u4e00\u4e9b\uff0c\u53ef\u5c06\u4e0a\u8ff0\u7247\u6bb5\u62bd\u8c61\u4e3a\u4ee5\u4e0b\u5f62\u5f0f\uff1a<\/p>\n<pre><code>@ \u7247\u6bb5\u540d\u79f0 #\n\u7247\u6bb5\u5185\u5bb9\n@<\/code><\/pre>\n<p>\u4e0a\u8ff0\u5f62\u5f0f\u4e2d\uff0c\u7247\u6bb5\u540d\u79f0\u4e0d\u4f1a\u5305\u542b <code>#<\/code> \u5b57\u7b26\uff0c\u7247\u6bb5\u5185\u5bb9\u4e2d\u4e5f\u4e0d\u5b58\u5728\u4efb\u4f55\u4e00\u884c\u6587\u5b57\u53ea\u542b\u6709\u5b57\u7b26 <code>@<\/code> \u7684\u60c5\u51b5\u3002\u4e8e\u662f\uff0c\u6211\u4eec\u7684\u95ee\u9898\u4fbf\u53ef\u4ee5\u660e\u786e\u4e3a\uff0c\u5f53\u5149\u6807\u5904\u4e8e\u7247\u6bb5\u5185\u5bb9\u533a\u57df\uff0c\u6b64\u65f6\u80fd\u5426\u901a\u8fc7 Elisp \u611f\u77e5\u5149\u6807\u5904\u4e8e\u4e0a\u8ff0\u5f62\u5f0f\u7684\u7247\u6bb5\u5185\u5462\uff1f\u4e3a\u4e86\u4fbf\u4e8e\u63cf\u8ff0\uff0c\u6211\u4eec\u5c06\u4e0a\u8ff0\u62bd\u8c61\u7684\u7247\u6bb5\u5f62\u5f0f\u79f0\u4e3a <a href=\"https:\/\/segmentfault.com\/orez-v1\/index.html\" target=\"_blank\" rel=\"noopener noreferrer\">Orez \u5f62\u5f0f<\/a>\u2026\u2026\u5f00\u59cb\u660e\u76ee\u5f20\u80c6\u5939\u5e26\u79c1\u8d27\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4ece\u5149\u6807\u5f53\u524d\u4f4d\u7f6e\u51fa\u53d1\uff0c\u5411\u540e\uff08\u5411\u7f13\u51b2\u533a\u9996\u90e8\u65b9\u5411\uff09\u904d\u5386\u7f13\u51b2\u533a\uff0c\u5e76\u63a2\u6d4b\u4f55\u65f6\u9047\u5230\u4ee5 <code>@<\/code> \u5f00\u5934\u4e14\u4ee5 <code>#<\/code> \u7ed3\u5c3e\u7684\u4e00\u884c\u6587\u5b57\uff0c\u5e76\u4e14\u4e5f\u5411\u524d\uff08\u5411\u7f13\u51b2\u533a\u5c3e\u90e8\u65b9\u5411\uff09\u904d\u5386\u7f13\u51b2\u533a\uff0c\u5e76\u63a2\u6d4b\u4f55\u65f6\u9047\u5230\u53ea\u5305\u542b <code>@<\/code> \u7684\u4e00\u884c\u6587\u5b57\uff0c\u82e5\u8fd9\u4e24\u4e2a\u65b9\u5411\u7684\u63a2\u6d4b\u7686\u6709\u6240\u5f97\uff0c\u4fbf\u53ef\u5224\u5b9a\u5149\u6807\u6b63\u5904\u4e8e Orez \u5f62\u5f0f\u533a\u57df\u3002<\/p>\n<p>\u4e0a\u8ff0\u7b97\u6cd5\u5e76\u4e0d\u56f0\u96be\uff0c\u5173\u952e\u5728\u4e8e\uff0c\u5982\u4f55\u5224\u65ad\u4e00\u884c\u6587\u5b57\u662f\u5426\u542b\u6709 <code>@<\/code> \u5f00\u5934\u4e14\u4ee5 <code>#<\/code> \u7ed3\u5c3e\uff0c\u4ee5\u53ca\u662f\u5426\u53ea\u5305\u542b <code>@<\/code>\u3002\u8fd9\u4e24\u4e2a\u5173\u952e\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u57fa\u4e8e Emacs \u63d0\u4f9b\u7684\u5b57\u7b26\u4e32\u5339\u914d\u51fd\u6570\u4e88\u4ee5\u89e3\u51b3\u3002<\/p>\n<h2>\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u662f\u4e00\u79cd\u5fae\u578b\u8bed\u8a00\uff0c\u53ef\u7528\u4e8e\u63cf\u8ff0\u6587\u5b57\u6a21\u5f0f\u2014\u2014\u6587\u5b57\u7684\u300c\u5f62\u72b6\u300d\u3002\u4f8b\u5982\uff0c\u4e00\u6bb5\u6587\u5b57\uff0c\u6211\u4eec\u77e5\u9053\u5b83\u662f\u4ee5 <code>@<\/code> \u5f00\u5934\u4e14\u4ee5 <code>#<\/code> \u7ed3\u5c3e\uff0c\u4e14\u9664\u9996\u5c3e\u5916\uff0c\u5176\u4ed6\u6587\u5b57\u7686\u975e <code>#<\/code>\uff0c\u5bf9\u4e8e\u8fd9\u79cd\u5f62\u5f0f\u7684\u6587\u5b57\uff0c\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u8868\u8ff0\u4e3a <code>^@[^#]+#$<\/code>\u3002\u5018\u82e5\u4f60\u4ece\u672a\u4e86\u89e3\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5e94\u8be5\u4f1a\u89c9\u5f97\u8fd9\u662f\u8574\u542b\u67d0\u79cd\u795e\u79d8\u529b\u91cf\u7684\u5492\u8bed\u3002\u4e8b\u5b9e\u4e0a\uff0c\u53ea\u8981\u7565\u52a0\u89e3\u91ca\uff0c\u4f60\u4fbf\u4f1a\u660e\u767d\u4e00\u5207\u90fd\u5f88\u7b80\u5355\u3002<\/p>\n<ul>\n<li><code>^<\/code> \u8868\u793a\u4e00\u6bb5\u6587\u5b57\u7684\u9996\u90e8\u3002<\/li>\n<li><code>[^#]<\/code> \u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u5b83\u4e0d\u662f <code>#<\/code>\u3002<\/li>\n<li><code>[^#]+<\/code> \u8868\u793a\u5b58\u5728\u4e00\u4e2a\u6216\u591a\u4e2a\u975e <code>#<\/code> \u5b57\u7b26\u3002<\/li>\n<li><code>#<\/code> \u5c31\u662f\u5b57\u7b26 <code>#<\/code>\u3002<\/li>\n<li><code>$<\/code> \u8868\u793a\u4e00\u6bb5\u6587\u5b57\u7684\u5c3e\u90e8\u3002<\/li>\n<\/ul>\n<p>\u4e5f\u53ef\u4ee5\u8ba9\u4e0a\u8ff0\u6b63\u5219\u8868\u8fbe\u5f0f\u6240\u8868\u8fbe\u7684\u6587\u5b57\u6a21\u5f0f\u66f4\u4e3a\u5bbd\u6cdb\u4e00\u4e9b\uff0c\u4f8b\u5982 <code>^[ \/t]*@[^#]+#[ \/t]*$<\/code>\uff0c\u5176\u4e2d <code>[ \/t]<\/code> \u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u5b83\u53ef\u4ee5\u662f\u7a7a\u683c\uff0c\u4e5f\u53ef\u4ee5\u662f\u5236\u8868\u7b26\uff08\u5373\u4f7f\u7528 Tab \u952e\u8f93\u5165\u7684\u5b57\u7b26\uff09\uff0c\u800c <code>[ \/t]*<\/code> \u5219\u8868\u793a\u5b58\u5728 0 \u4e2a\u6216 1 \u4e2a\u6216\u66f4\u591a\u4e2a\u5b57\u7b26\uff0c\u5b83\u4eec\u6216\u4e3a\u7a7a\u683c\uff0c\u6216\u4e3a\u5236\u8868\u7b26\u3002<\/p>\n<p>\u5018\u82e5\u67d0\u6bb5\u6587\u5b57\u7b26\u5408\u67d0\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6240\u8868\u8fbe\u7684\u6587\u5b57\u6a21\u5f0f\uff0c\u4fbf\u79f0\u8be5\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u8be5\u6bb5\u6587\u5b57\u3002\u6211\u4eec\u53ef\u4ee5\u7528 Emacs \u63d0\u4f9b\u7684 <code>string-match<\/code> \u505a\u4e00\u4e9b\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u8bd5\u9a8c\u3002\u4f8b\u5982<\/p>\n<pre><code class=\"lisp\">(let ((x &quot;@ i am foo #&quot;))\n  (if (string-match &quot;^@[^#]+#$&quot; x)\n      (message &quot;hit!&quot;)\n    (message &quot;failed!&quot;))) ;; \u4f1a\u8f93\u51fa hit!<\/code><\/pre>\n<p>\u518d\u4f8b\u5982<\/p>\n<pre><code class=\"lisp\">(let ((x &quot;    @ i am foo #&quot;))\n  (if (string-match &quot;^@[^#]+#$&quot; x)\n      (message &quot;hit!&quot;)\n    (message &quot;failed!&quot;))) ;; \u4f1a\u8f93\u51fa failed!<\/code><\/pre>\n<p>\u518d\u4f8b\u5982<\/p>\n<pre><code class=\"lisp\">(let ((x &quot;    @ i am foo #&quot;))\n  (if (string-match &quot;^[ \/t]*@[^#]+#$&quot; x)\n      (message &quot;hit!&quot;)\n    (message &quot;failed!&quot;))) ;; \u4f1a\u8f93\u51fa hit!<\/code><\/pre>\n<p>\u51e1\u662f\u80fd\u8ba9 <code>string-match<\/code> \u7684\u6c42\u503c\u7ed3\u679c\u4e3a\u771f\uff0c\u5373\u4e3a <code>t<\/code> \u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5b57\u7b26\u4e32\uff0c\u79f0\u4e8c\u8005\u5339\u914d\u3002Emacs \u6240\u652f\u6301\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6709\u4e00\u4e2a\u529f\u80fd\u662f\u5141\u8bb8\u6211\u4eec\u4ece\u5b83\u6240\u5339\u914d\u7684\u5b57\u7b26\u4e32\u4e2d\u6355\u83b7\u4e00\u4e9b\u6587\u5b57\u3002\u4f8b\u5982\uff0c\u6355\u83b7\u4e0a\u8ff0\u6700\u540e\u4e00\u4e2a\u793a\u4f8b\u4e2d <code>x<\/code> \u7684 <code>i am foo<\/code> \u90e8\u5206\uff0c\u53ea\u9700\u5c06\u4e0e\u4e4b\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4fee\u6539\u4e3a<\/p>\n<pre><code>^[ \/t]*@[ \/t]*\/\/([^#]+\/\/)[ \/t]*#$<\/code><\/pre>\n<p>\u5176\u4e2d <code>\/\/(<\/code> \u548c <code>\/\/)<\/code> \u8868\u793a\u53ef\u6355\u83b7\u5b83\u4eec\u6240\u5305\u56f4\u7684\u90e8\u5206\uff0c\u5373 <code>[^#]+<\/code>\u3002\u6355\u83b7\u7ed3\u679c\u53ef\u901a\u8fc7 <code>match-string<\/code> \u83b7\u53d6\uff0c\u4f8b\u5982<\/p>\n<pre><code class=\"lisp\">(let ((x &quot;    @ i am foo #&quot;))\n  (if (string-match &quot;^[ \/t]*@\/\/([^#]+\/\/)#$&quot; x)\n      (message &quot;%s&quot; (string-trim (match-string 1 x)))\n    (message &quot;failed!&quot;))) ;; \u4f1a\u8f93\u51fa i am foo<\/code><\/pre>\n<p><code>match-string<\/code> \u7684\u7b2c 1 \u4e2a\u53c2\u6570\u8868\u793a\u83b7\u53d6\u7b2c\u51e0\u4e2a\u6355\u83b7\uff0c\u7531\u4e8e\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u53ea\u6709\u4e00\u5904\u6355\u83b7\uff0c\u6545\u8be5\u53c2\u6570\u4e3a 1\u3002<code>string-trim<\/code> \u51fd\u6570\u7528\u4e8e\u6d88\u9664\u5b57\u7b26\u4e32\u524d\u5bfc\u4e0e\u672b\u5c3e\u7a7a\u767d\u5b57\u7b26\u3002<\/p>\n<p>\u4e5f\u8bb8\u4f60\u5df2\u7ecf\u611f\u53d7\u5230\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5f3a\u5927\uff0c\u5b83\u80fd\u5bf9\u5b57\u7b26\u4e32\u5b9e\u73b0\u6a21\u7cca\u5339\u914d\uff0c\u53ef\u662f\u4f60\u5e94\u8be5\u4e5f\u80fd\u611f\u53d7\u5230\u5b83\u7684\u5f0a\u7aef\uff0c\u4e00\u65e6\u8981\u5339\u914d\u7684\u6587\u672c\u8f83\u4e3a\u590d\u6742\uff0c\u4e3a\u5176\u6240\u5199\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5f88\u5feb\u4f60\u4fbf\u96be\u89e3\u5176\u610f\u4e86\uff0c\u4ea6\u5373\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u51e0\u4e4e\u4e0d\u5177\u5907\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<h2>rx \u8bb0\u6cd5<\/h2>\n<p>\u4e3a\u4e86\u8ba9\u6b63\u5219\u8868\u8fbe\u5f0f\u5177\u5907\u53ef\u7ef4\u62a4\u6027\uff0cEmacs \u63d0\u4f9b\u4e86 rx \u8bb0\u6cd5\uff0c\u4ea6\u5373\u4f60\u53ef\u4ee5\u901a\u8fc7 rx \u8868\u8fbe\u5f0f\u6784\u9020\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u4f8b\u5982<\/p>\n<pre><code class=\"lisp\">(rx line-start (zero-or-more (any &quot; \/t&quot;))\n    &quot;@&quot;\n    (one-or-more (not &quot;#&quot;))\n    &quot;#&quot;\n    (zero-or-more (any &quot; \/t&quot;)) line-end)<\/code><\/pre>\n<p>\u5176\u6c42\u503c\u7ed3\u679c\u4e3a<\/p>\n<pre><code>&quot;^[ \/t]*@[^#]+#[ \/t]*$&quot;<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u7528 <code>rx-let<\/code> \u8868\u8fbe\u5f0f\uff0c\u5b9a\u4e49\u4e00\u4e9b\u5c40\u90e8\u53d8\u91cf\uff0c\u5c06\u5176\u4f5c\u4e3a\u4e00\u4e9b\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u300c\u7b80\u5199\u300d\uff0c\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\u4e0e\u4e0a\u6587\u7684 <code>rx<\/code> \u8868\u8fbe\u5f0f\u7b49\u6548\u3002<\/p>\n<pre><code class=\"lisp\">(rx-let ((padding (zero-or-more (any &quot; \/t&quot;)))\n         (name-area (one-or-more (not &quot;#&quot;))))\n  (rx line-start padding &quot;@&quot; name-area &quot;#&quot; padding line-end))<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u5728 rx \u8bb0\u6cd5\u4e2d\uff0c\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u4f5c\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u8bb0\u53f7\uff0c\u53ea\u80fd\u7528 <code>rx-let<\/code>\uff0c\u800c\u4e0d\u80fd\u7528 <code>let<\/code>\u3002<\/p>\n<p>\u82e5\u9700\u8981\u6784\u9020\u5e26\u6709\u6355\u83b7\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5728 rx \u8bb0\u6cd5\u53ef\u4f7f\u7528 <code>group<\/code>\u3002\u4f8b\u5982<\/p>\n<pre><code class=\"lisp\">(rx-let ((padding (zero-or-more (any &quot; \/t&quot;)))\n         (name-area (one-or-more (not &quot;#&quot;))))\n  (rx line-start padding &quot;@&quot; (group name-area) &quot;#&quot; padding line-end))<\/code><\/pre>\n<p>\u6c42\u503c\u7ed3\u679c\u4e3a<\/p>\n<pre><code class=\"regex\">&quot;^[ \/t]*@\/\/([^#]+\/\/)#[ \/t]*$&quot;<\/code><\/pre>\n<p>\u867d\u7136\u6b63\u5219\u8868\u8fbe\u5f0f\u8981\u6bd4 rx \u8bb0\u6cd5\u66f4\u7b80\u7ea6\uff0c\u4f46\u662f rx \u8bb0\u6cd5\u66f4\u5bb9\u6613\u8ba9\u6211\u4eec\u7406\u89e3\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7ed3\u6784\uff0c\u6545\u800c\u4ee5\u540e\u6211\u4eec\u5c3d\u91cf\u5728 Elisp \u4e2d\u4f7f\u7528 rx \u8bb0\u6cd5\uff0c\u800c\u975e\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u4ee5\u4e0b\u662f rx \u8bb0\u6cd5\u7684\u5e94\u7528\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"lisp\">(let ((x &quot;    @ i am foo #&quot;)\n      (re (rx-let ((padding (zero-or-more (any &quot; \/t&quot;)))\n                   (name-area (one-or-more (not &quot;#&quot;))))\n            (rx line-start padding &quot;@&quot; (group name-area) &quot;#&quot; line-end))))\n  (if (string-match re x)\n      (message &quot;%s&quot; (match-string 1 x))\n    (message &quot;failed!&quot;))) ;; \u4f1a\u8f93\u51fa i am foo<\/code><\/pre>\n<h2>\u611f\u77e5<\/h2>\n<p>\u5e0c\u671b\u4f60\u8fd8\u6ca1\u6709\u5fd8\u8bb0\u6211\u4eec\u7684\u4efb\u52a1\uff0c\u4ece\u5f53\u524d\u7f13\u51b2\u533a\u7684\u5149\u6807\u6240\u5728\u4f4d\u7f6e\u5411\u540e\u63a2\u6d4b\uff0c\u5bfb\u627e\u6b63\u5219\u8868\u8fbe\u5f0f <code>^@[^#]+#$<\/code> \u53ef\u5339\u914d\u7684\u4e00\u884c\u6587\u5b57\uff0c\u6b64\u4e8b\u73b0\u5728\u5df2\u65e0\u4efb\u4f55\u96be\u70b9\uff0c\u51fd\u6570 <code>orez-area-beginning<\/code> \u53ef\u4ee5\u83b7\u5f97 Orez \u5f62\u5f0f\u533a\u57df\u7684\u8d77\u70b9\uff0c\u82e5\u5149\u6807\u5e76\u672a\u5728 Orez \u5f62\u5f0f\u533a\u57df\u5185\u90e8\uff0c\u5219\u8be5\u51fd\u6570\u7684\u7ed3\u679c\u4e3a <code>nil<\/code>\u3002<\/p>\n<pre><code class=\"lisp\">(defun orez-area-beginning ()\n  (let (re line)\n    (setq re (rx line-start &quot;@&quot;\n                 (one-or-more (not &quot;#&quot;))\n                 &quot;#&quot; line-end))\n    (catch 'break\n      (while t\n        (setq line (buffer-substring-no-properties (pos-bol) (pos-eol)))\n        (if (string-match re line)\n            (throw 'break (point))\n          (progn\n            (when (&lt;= (point) (point-min))\n              (throw 'break nil))\n            (forward-line -1))))\n      nil)))<\/code><\/pre>\n<p>\u4e3a\u4e86\u4fbf\u4e8e\u4f60\u7406\u89e3\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u5c06\u5176\u7ffb\u8bd1\u6210\u4e86\u4ee5\u4e0b C \u8bed\u8a00\u4f2a\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"c\">int orez_area_beginning(void) {\n    Regex re = \u7531 rx \u8bb0\u6cd5\u6784\u9020\u7684\u6b63\u5219\u8868\u8fbe\u5f0f;\n    while (1) {\n        String line = \u5f53\u524d\u7684\u4e00\u884c\u6587\u5b57;\n        if (re \u4e0e line \u5339\u914d) {\n            return point();\n        } else {\n            if (point() &lt;= point_min()) {\n                return -1; \/* \u8fd4\u56de\u65e0\u6548\u4f4d\u7f6e\uff0c\u8868\u793a\u63a2\u6d4b\u5931\u8d25 *\/\n            }\n            forward_line(-1); \/* \u540e\u9000\u4e00\u884c *\/\n        }\n    }\n    return -1; \/* \u8fd4\u56de\u65e0\u6548\u4f4d\u7f6e\uff0c\u8868\u793a\u63a2\u6d4b\u5931\u8d25 *\/\n}<\/code><\/pre>\n<p>\u5411\u524d\u63a2\u6d4b\u8fc7\u7a0b\uff0c\u8981\u6bd4\u5411\u540e\u63a2\u6d4b\u7565\u5fae\u7b80\u5355\u4e00\u4e9b\uff0c\u4e0b\u9762\u6211\u76f4\u63a5\u4ee5 <code>orez-area-end<\/code> \u51fd\u6570\u5b9e\u73b0\u8be5\u8fc7\u7a0b\uff0c\u4e14\u4e0d\u518d\u4ee5 C \u4f2a\u4ee3\u7801\u4e88\u4ee5\u6ce8\u91ca\u3002<\/p>\n<pre><code class=\"lisp\">(defun orez-area-end ()\n  (let (re line)\n    (setq re (rx line-start &quot;@&quot; line-end))\n    (catch 'break\n      (while t\n        (setq line (buffer-substring-no-properties (pos-bol) (pos-eol)))\n        (if (string-match re line)\n            (throw 'break (point))\n          (progn\n            (when (&gt;= (point) (point-max))\n              (throw 'break nil))\n            (forward-line))))\n      nil)))<\/code><\/pre>\n<p>\u57fa\u4e8e <code>orez-area-beginning<\/code> \u548c <code>orez-area-end<\/code> \u7684\u7ed3\u679c\u4fbf\u53ef\u786e\u5b9a\u5149\u6807\u662f\u5426\u843d\u5728 Orez \u5f62\u5f0f\u533a\u57df\u3002<\/p>\n<pre><code class=\"lisp\">(defun orez-area? ()\n  (if (and (orez-area-beginning) (orez-area-end))\n      t\n    nil))<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4f7f\u7528\u4e86\u5e03\u5c14\u8fd0\u7b97\u4e2d\u7684\u300c\u4e0e\u300d\u8fd0\u7b97 <code>and<\/code>\u3002Elisp \u7684\u5e03\u5c14\u8fd0\u7b97\u8fd8\u6709\u300c\u6216\u300d\u8fd0\u7b97 <code>or<\/code> \u4ee5\u53ca\u524d\u6587\u5728\u6784\u9020 rx \u8bb0\u6cd5\u65f6\u7528\u8fc7\u7684\u300c\u975e\u300d\u8fd0\u7b97 <code>not<\/code>\u3002\u57fa\u4e8e\u8fd9\u4e09\u79cd\u8fd0\u7b97\uff0c\u53ef\u4ee5\u6784\u9020\u590d\u6742\u7684\u903b\u8f91\u8868\u8fbe\u5f0f\u3002<\/p>\n<h2>bobp \u548c eobp<\/h2>\n<p><code>orez-area-beginning<\/code> \u548c <code>orez-area-end<\/code> \u7684\u5b9a\u4e49\u4e2d\uff0c\u7686\u5728 <code>while<\/code> \u8868\u8fbe\u5f0f\u4e2d\u5224\u65ad\u5149\u6807\u662f\u5426\u5df2\u62b5\u8fbe\u7f13\u51b2\u533a\u9996\u90e8\u548c\u5c3e\u90e8\uff0c\u5373<\/p>\n<pre><code class=\"lisp\">(&lt;= (point) (point-min))<\/code><\/pre>\n<p>\u548c<\/p>\n<pre><code>(&gt;= (point) (point-max))<\/code><\/pre>\n<p>\u5b9e\u9645\u4e0a\uff0cEmacs \u4e3a\u4e0a\u8ff0\u8fd9\u4e24\u79cd\u60c5\u51b5\u7684\u5224\u65ad\u63d0\u4f9b\u4e86\u51fd\u6570 <code>bobp<\/code> \u548c <code>eobp<\/code>\uff0c\u6545\u800c\u53ef\u7528 <code>(bobp)<\/code> \u548c <code>(eobp)<\/code> \u5206\u522b\u4ee3\u66ff\u4e0a\u8ff0\u8868\u8fbe\u5f0f\u3002\u6545\u800c\u5c06 <code>orez-area-beginning<\/code> \u548c <code>orez-area-end<\/code> \u91cd\u65b0\u5b9a\u4e49\u4e3a<\/p>\n<pre><code class=\"lisp\">(defun orez-area-beginning ()\n  (let (re line)\n    (setq re (rx line-start &quot;@&quot;\n                 (one-or-more (not &quot;#&quot;))\n                 &quot;#&quot; line-end))\n    (catch 'break\n      (while (not (bobp))\n        (setq line (buffer-substring-no-properties (pos-bol) (pos-eol)))\n        (if (string-match re line)\n            (throw 'break (point))\n          (forward-line -1)))\n      nil)))<\/code><\/pre>\n<pre><code class=\"lisp\">(defun orez-area-end ()\n  (let (re line)\n    (setq re (rx line-start &quot;@&quot; line-end))\n    (catch 'break\n      (while (not (eobp))\n        (setq line (buffer-substring-no-properties (pos-bol) (pos-eol)))\n        (if (string-match re line)\n            (throw 'break (point))\n          (forward-line)))\n      nil)))<\/code><\/pre>\n<h2>\u73b0\u573a\u4fdd\u5b58<\/h2>\n<p>\u82e5\u5149\u6807\u5728 Orez \u5f62\u5f0f\u533a\u57df\uff0c\u800c\u4f60\u4e5f\u771f\u7684\u8bd5\u7740\u7528\u8fc7 <code>in-orez-area?<\/code> \u51fd\u6570\uff0c\u4fbf\u4f1a\u53d1\u73b0\uff0cEmacs \u5bf9\u8be5\u51fd\u6570\u6c42\u503c\u540e\uff0c\u5149\u6807\u4f1a\u88ab\u79fb\u52a8\u5230 Orez \u5f62\u5f0f\u533a\u57df\u7684\u672b\u5c3e\u3002\u539f\u56e0\u662f <code>orez-area-beginning<\/code> \u548c <code>orez-area-end<\/code> \u51fd\u6570\u4f7f\u7528\u4e86\u9010\u884c\u79fb\u52a8\u5149\u6807\u51fd\u6570 <code>forward-line<\/code>\u3002\u82e5\u60f3\u5728\u5e94\u7528 <code>in-orez-area?<\/code> \u4e4b\u540e\u80fd\u5c06\u5149\u6807\u590d\u539f\uff0c\u4f60\u53ef\u4ee5\u5148\u7528\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u4fdd\u5b58\u5149\u6807\u4f4d\u7f6e\uff0c\u65f6\u5019\u518d\u5c06\u5149\u6807\u79fb\u81f3\u8be5\u4f4d\u7f6e\uff0c\u4f8b\u5982<\/p>\n<pre><code class=\"lisp\">(defun in-orez-area? ()\n  (let ((x (point)))\n    (if (and (orez-area-beginning) (orez-area-end))\n        (progn\n          (goto-char x)\n          t)\n      (progn\n        (goto-char x)\n        nil))))<\/code><\/pre>\n<p>Emacs \u4e3a\u4e86\u4e0d\u8ba9\u4f60\u5982\u6b64\u8d39\u5fc3\uff0c\u5b83\u63d0\u4f9b\u4e86 <code>save-excursion<\/code> \u8868\u8fbe\u5f0f\uff0c\u53ef\u5b8c\u6210\u7b49\u6548\u5de5\u4f5c\uff0c\u5176\u7528\u6cd5\u5982\u4e0b<\/p>\n<pre><code class=\"lisp\">(defun in-orez-area? ()\n  (save-excursion\n    (if (and (orez-area-beginning) (orez-area-end))\n        t\n      nil)))<\/code><\/pre>\n<p><strong>\u7ec3\u4e60<\/strong>\uff1a\u82e5 Orez \u5f62\u5f0f\u66f4\u4e3a\u590d\u6742\uff0c\u4f8b\u5982\u7247\u6bb5\u540d\u79f0\u53ef\u80fd\u8de8\u8d8a\u591a\u884c\uff0c\u884c\u95f4\u4ee5 <code>\/<\/code> \u8fde\u63a5\uff0c\u4f8b\u5982<\/p>\n<pre><code>@ \u8fd9\u662f\u53ef\u8de8\u8d8a \/\n  \u591a\u884c\u7684\u7247\u6bb5\u540d\u79f0 #\n\u7247\u6bb5\u5185\u5bb9\n@<\/code><\/pre>\n<p>\u6b64\u65f6\uff0c\u4f60\u8be5\u5982\u4f55\u5b9e\u73b0 <code>orez-area-beginning<\/code> \u51fd\u6570\u5462\uff1f<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>Orez \u662f\u6211\u7f16\u5199\u7684\u6587\u5b66\u7f16\u7a0b\u5de5\u5177\u3002\u6240\u8c13\u6587\u5b66\u7f16\u7a0b\uff0c\u5373\u7a0b\u5e8f\u7684\u6587\u6863\u4e0e\u4ee3\u7801\u662f\u6df7\u5408\u6001\uff0c\u5373\u6587\u6863\u7247\u6bb5\u548c\u4ee3\u7801\u7247\u6bb5\u5f7c\u6b64\u7ea0\u7f20\u3002Orez \u53ef\u4ece\u6587\u5b66\u7a0b\u5e8f\u91cc\u62bd\u53d6\u53ef\u7f16\u8bd1\/\u89e3\u91ca\u7684\u5b8c\u6574\u4ee3\u7801\uff0c\u4e5f\u53ef\u5c06\u6587\u5b66\u7a0b\u5e8f\u8f6c\u5316\u4e3a\u7528\u4e8e\u6587\u6863\u6392\u7248\u7684\u6e90\u6587\u4ef6\uff0c\u7531 TeX \u6216\u7c7b\u4f3c\u7684\u6392\u7248\u8f6f\u4ef6\u751f\u6210\u7a0b\u5e8f\u6587\u6863\u3002<\/p>\n<p>\u6211\u4e4b\u6240\u4ee5\u9700\u8981\u5728 Emacs \u91cc\u8bc6\u522b Orez \u5f62\u5f0f\u533a\u57df\uff0c\u662f\u56e0\u4e3a\u6587\u5b66\u7a0b\u5e8f\u91cc\u53ef\u80fd\u5b58\u5728\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u7684\u4ee3\u7801\u7247\u6bb5\uff0cEmacs \u5f88\u96be\u4ee5\u7edf\u4e00\u7684\u6a21\u5f0f\u7f16\u8f91\u5b83\u4eec\u3002\u5018\u82e5\u80fd\u8bc6\u522b Orez \u533a\u57df\uff0c\u5c06\u8fd9\u4e9b\u4ee3\u7801\u7247\u6bb5\u4e34\u65f6\u63d0\u53d6\u5230\u53e6\u4e00\u4e2a\u7a97\u53e3\u4e2d\u7684\u7f13\u51b2\u533a\uff0c\u5e76\u5f00\u542f\u76f8\u5e94\u7684\u7f16\u7a0b\u8bed\u8a00\u6a21\u5f0f\uff0c\u5219 Emacs \u4fbf\u53ef\u4f5c\u4e3a\u6587\u5b66\u7f16\u7a0b\u6240\u7528\u7684\u4e13\u4e1a\u7f16\u8f91\u5668\u4e86\u3002<\/p>\n<p>\u73b0\u5728\u5b8c\u6210\u8fd9\u4e00\u76ee\u7684\u6240\u9700\u7684 Elisp \u8bed\u6cd5\u548c Emacs \u51fd\u6570\uff0c\u6211\u5df2\u7ecf\u57fa\u672c\u638c\u63e1\u4e86\uff0c\u751a\u81f3\u8fd9\u4e00\u76ee\u7684\u4e5f\u5df2\u7ecf\u521d\u6b65\u5f97\u4ee5\u5b9e\u73b0\u3002\u4f60\u867d\u7136\u6ca1\u6709\u8fd9\u4e00\u8ffd\u6c42\uff0c\u4f46\u4f60\u5df2\u7ecf\u5177\u5907\u4e86\u9a7e\u9a6d Emacs \u7684\u57fa\u672c\u80fd\u529b\u4e86\uff0c\u5269\u4e0b\u7684\u53ea\u662f\u601d\u8003\u4f60\u7684\u8ffd\u6c42\u5e76\u5c1d\u8bd5\u5b9e\u73b0\u5b83\u4eec\u3002<\/p>\n<p><strong>\u7ec3\u4e60<\/strong>\uff1a\u4f60\u80fd\u5728 Emacs \u91cc\uff0c\u7528 Elisp \u7a0b\u5e8f\u5b9e\u73b0\u5982\u4e0b\u56fe\u6240\u793a\u7684\u6548\u679c\u5417\uff1f\u6240\u9700\u7684\u5168\u90e8\u77e5\u8bc6\uff0c\u4f60\u90fd\u662f\u5177\u5907\u7684\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"598\" height=\"389\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdjMDa\" alt=\"Orez \u4ee3\u7801\u7247\u6bb5\u7f16\u8f91\u8fc7\u7a0b\" title=\"Orez \u4ee3\u7801\u7247\u6bb5\u7f16\u8f91\u8fc7\u7a0b\"><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046621831\">https:\/\/segmentfault.com\/a\/1190000046621831<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>=&gt; \u4e0a\u4e00\u7bc7\uff1a\u5728 Emacs \u7f13\u51b2\u533a\u91cc\u884c\u8d70\u7684\u59ff\u52bf \u524d\u8a00 \u73b0\u5728\uff0c\u6211\u4eec\u5c1d\u8bd5\u7528 Elisp \u7f16\u7a0b\u6765\u89e3\u4e00\u9053\u5e94\u7528\u9898 [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020824","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020824"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020824\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}