【问题标题】:Emacs function to add symbol to __all__ in Python mode?Emacs 函数在 Python 模式下向 __all__ 添加符号?
【发布时间】:2009-05-13 20:50:43
【问题描述】:

是否有现有的 Emacs 函数在编辑 Python 代码时将当前点下的符号添加到 __all__

例如,假设光标位于foo 中的第一个o

#    v---- cursor is on that 'o'
def foo():
    return 42

如果你做了 M-x python-add-to-all(或其他),它会将'foo' 添加到__all__

当我用谷歌搜索时,我没有看到一个,但和往常一样,我可能遗漏了一些明显的东西。

更新

我尝试了Trey Jackson's answer(谢谢,Trey!)并进行了一些修复/增强,以防万一有人感兴趣(不再重复插入,如果没有,则将__all__ 放在更典型的位置' t 已经存在):

(defun python-add-to-all () “取点下的符号并将其添加到 __all__ 清单,如果它还没有的话。” (交互的) (保存游览 (let ((thing (thing-at-point 'symbol))) (if (progn (goto-char (point-min)) (让(找到) (而(和(未找到) (re-search-forward (rx symbol-start "__all__" symbol-end (0+空格)“=”(0+空格) (语法左括号)) 无 t)) (setq found (not (python-in-string/comment)))) 成立)) (当 (不是 (看着 (rx-to-string `(and (0+ (not (syntax close-parenthesis))) (语法字符串引用),事物(语法字符串引用))))) (插入(格式“\'%s\',”东西)) (缓冲区开始) ;;放在任何导入行之前,或者如果没有,则放在第一类或 ;;功能。 (when (not (re-search-forward (rx bol (or "import" "from") symbol-end) nil t)) (re-search-forward (rx symbol-start (or "def" "class") symbol-end) nil t)) (前锋-1) (插入 (格式 "\n__all__ = [\'%s\']\n" 东西))))))

【问题讨论】:

    标签: python emacs


    【解决方案1】:

    不是 python 程序员,我不确定这是否涵盖所有情况,但在一个简单的情况下对我有用。如果数组存在,它会将符号添加到数组中,如果不存在则创建__all__注意:它不解析数组以避免重复插入。

    (defun python-add-to-all ()
      "take the symbol under the point and add to the __all__ routine"
      (interactive)
      (save-excursion
        (let ((thing (thing-at-point 'word))
              p)
          (if (progn (goto-char (point-min))
                     (re-search-forward "^__all__ = \\[" nil t))
              (insert (format "\"%s\", " thing))
            (goto-char (point-min))
            (insert (format "__all__ = [\"%s\"]\n" thing))))))
    

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多