gpt4 book ai didi

python - Emacs 中的单元模式

转载 作者:太空狗 更新时间:2023-10-30 02:32:14 25 4
gpt4 key购买 nike

假设我有一个缓冲区,代码(在本例中为 Python)组织如下:

.. cell 1 ..
##
.. cell 2 ..

# this is a comment

### this is also a comment

.. still cell 2 ..

##
.. cell 3 (code that is indented)

字符序列 ## 用于分隔缓冲区中的 cells(代码区域/ block )。字符 # 在 Python 中开始注释,因此 ## 被语言视为注释。类似的结构可以建立在例如。使用 ;; 或其他编程语言的 Elisp。

我想定义一个 Emacs 命令,当被调用时,它定义了当前的 cell(即 cellpoint 所在的 cell/cursor 当前位于。)成为 Emacs region (即突出显示单元格)。

我如何在 Emacs 中执行此操作?

供引用:

  • 类似于细胞的概念或 code sections在 MATLAB 中
  • 这是一个thread在 Vim 中实现此功能。

最佳答案

这是一个解决方案:

(defun python-inside-comment-p ()
(save-excursion
(beginning-of-line 1)
(looking-at "^#")))

(defun python-select-cell ()
(interactive)
(goto-char
(if (re-search-backward "^\\s-*##[^#]" nil t)
(match-end 0)
(point-min)))
(while (and (python-inside-comment-p)
(eq 0 (forward-line 1)))
nil)
(set-mark (point))
(goto-char
(if (re-search-forward "^\\s-*\\(##[^#]\\)" nil t)
(- (match-beginning 1) 2)
(point-max))))

测试:

print "Beautiful is better than ugly."
##
print "Explicit is better than implicit."
print "Simple is better than complex."
print "Complex is better than complicated."
# this is a comment
print "Flat is better than nested."
### this is also a comment
print "Sparse is better than dense."
##
print "Readability counts."
print "Special cases aren't special enough to break the rules."
print "Although practicality beats purity."
print "Errors should never pass silently."
print "Unless explicitly silenced."

工作正常。是否有理由不使用缩进级别而不是评论作为 anchor ?

关于python - Emacs 中的单元模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19453360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com