【问题标题】:How do you automatically quote long strings in emacs at line breaks ala pycharm你如何在换行符ala pycharm自动引用emacs中的长字符串
【发布时间】:2013-10-11 02:14:58
【问题描述】:

虽然我真的很喜欢 PyCharm,但我想真正学习如何在 emacs 中做到这一点。当我清理 pep8 违规尤其是长行违规时,我真的很欣赏 PyCharm 自动引用行尾或添加我通常在 emacs 中手动执行的任何其他内容的能力。 emacs中是否有一个插件可以做到这一点?例如:

发件人:

output_string += '<table border="1"><tr><th>Column</th> <th>Type</th> <th>Required</th></tr>'

当我换行时,它会自动添加所需的行尾:

output_string += '<table border="1"><tr><th>Column</th> <th>Type</th> ' \ 
88                           '<th>Required</th></tr>'

【问题讨论】:

标签: emacs editor emacs24


【解决方案1】:

添加到您的设置中:

(defun python-newline ()
  (interactive)
  (let ((char (car (inside-string?))))
    (if char
      (progn
        (insert (format "%c \\" char))
        (newline-and-indent)
        (insert (format "%c" char)))
      (newline))))

(add-hook
 'python-mode-hook
 (lambda()
   ;; ...
   (define-key python-mode-map (kbd "RET") 'python-newline)))

(defun inside-string? ()
  (interactive)
  (let ((p (nth 8 (syntax-ppss))))
    (memq (char-after p) '(?\" ?\')))) 

【讨论】:

  • 我已经尝试过了,它可以工作,只是现在如果它是上下文敏感的,就像在大括号或圆括号中一样,它不会添加“\”字符,因为它不需要跨度>
  • 请提供一个最小的例子。 python-newline 也绑定到 C-m。如果你什么都不想做,你可以C-j
猜你喜欢
  • 2011-01-22
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 2018-03-16
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
  • 2013-06-01
相关资源
最近更新 更多