簡體   English   中英

Emacs中的Cell模式

[英]Cell mode in Emacs

假設我有一個包含代碼的緩沖區(在本例中為Python),其組織如下:

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

# this is a comment

### this is also a comment

.. still cell 2 ..

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

字符序列##用於分隔緩沖區中的cells (代碼區域/塊)。 caracter #在Python中啟動注釋,因此##被視為語言注釋。 類似的結構可以用例如Elisp構建;; 或其他編程語言。

我想,以限定一個Emacs命令調用時,它定義了當前cell (即, cell在其上 /光標當前坐。)是Emacs的region (即它突出的小區)。

我怎么能在Emacs中做到這一點?

以供參考:

  • 這類似於MATLAB中的單元格或代碼部分的概念
  • 這是在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."

工作正常。 是否有理由不使用縮進級別而不是注釋作為錨點?

那將是這樣的:

(defun mark-cell ()
  (interactive)
  (search-backward-regexp "^##\\($\\|[^#]\\)" nil 'noerror)
  (push-mark)
  (end-of-line)
  (search-forward-regexp "^##\\($\\|[^#]\\)" nil 'noerror)
  (beginning-of-line)
  (activate-mark))

對我來說,它沒有突出顯示單元格(你可以用Cx Cx手動完成),即使這是activate-mark應該做的,如果我理解正確的話。

EmacsWiki ,有兩個包:

python-x還提供了一些額外的功能

python-cell在python緩沖區中提供類似Matlab的單元格

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM