【问题标题】:Smart home in EmacsEmacs 中的智能家居
【发布时间】:2010-09-13 19:16:32
【问题描述】:

你能在 Emacs 中为 home 键设置智能行为吗?智能我的意思是,它应该转到第一个非空白字符,然后在第二次按下时转到 0,然后在第三次按下时返回到第一个非空白字符,而不是转到第 0 个字符,以此类推。 拥有智能端也不错。

【问题讨论】:

    标签: emacs


    【解决方案1】:

    我的版本:移到可视行的开头、第一个非空白或行首。

    (defun smart-beginning-of-line ()
        "Move point to beginning-of-line or first non-whitespace character"
      (interactive "^")
      (let ((p (point)))
        (beginning-of-visual-line)
        (if (= p (point)) (back-to-indentation))
        (if (= p (point)) (beginning-of-line))))
    (global-set-key [home] 'smart-beginning-of-line)
    (global-set-key "\C-a" 'smart-beginning-of-line)
    

    [home]"\C-a"(control+a)键:

    • 将光标(点)移动到视线的起点。
    • 如果它已经在可视行的开头,则将其移动到该行的第一个非空白字符。
    • 如果已经存在,则将其移至行首。
    • 移动时,保留区域 (interactive "^")。

    这取自@cjm 和@thomas;然后我添加了视觉线的东西。 (抱歉我的英语不好)。

    【讨论】:

      【解决方案2】:
      (defun smart-beginning-of-line ()
        "Move point to first non-whitespace character or beginning-of-line.
      
      Move point to the first non-whitespace character on this line.
      If point was already at that position, move point to beginning of line."
        (interactive "^") ; Use (interactive) in Emacs 22 or older
        (let ((oldpos (point)))
          (back-to-indentation)
          (and (= oldpos (point))
               (beginning-of-line))))
      
      (global-set-key [home] 'smart-beginning-of-line)
      

      我不太确定 smart end 会做什么。您通常有很多尾随空格吗?

      注意:此功能与 Robert Vuković 的主要区别在于,即使光标已经存在,他总是在第一次按键时移动到第一个非空白字符。在这种情况下,我的会移动到第 0 列。

      另外,他使用了(beginning-of-line-text),而我使用了(back-to-indentation)。它们非常相似,但它们之间存在一些差异。 (back-to-indentation) 总是移动到一行的第一个非空白字符。 (beginning-of-line-text) 有时会跳过它认为无关紧要的非空白字符。例如,在仅注释行上,它移动到注释文本的第一个字符,而不是注释标记。但是这两个函数都可以在我们的任何一个答案中使用,具体取决于您喜欢哪种行为。

      【讨论】:

      • 不幸的是,它在启用 cua 的情况下不起作用,在这种情况下,选择移位不起作用,选择整行的移位不起作用。有什么建议吗?
      • @Alexander Stolz,我从不使用 cua 模式,但尝试在 defun 之后添加 (put 'smart-beginning-of-line 'CUA 'move)(即,就在 global-set-key 行之前)。
      • 有没有办法在不打开 shift-select-mode 的情况下使 shift-selection 工作?
      • 由于 Emacs 版本现在是 24,我建议编写示例代码默认使用 (interactive "^") 并添加 ; Use (interactive) with Emacs 22 or older。 Gorlum0(上面的评论)可能不是唯一一个努力让示例代码与班次选择一起工作的人。
      • 我对 emacs 比较陌生。我试图将其修改为更便携,所以我添加了这一行:(if (version< "22" emacs-version) (interactive "^") (interactive))。但这给了我错误:Wrong type argument: commandp, smart-beginning-of-line。有人介意解释为什么 with 不起作用吗?
      【解决方案3】:

      现在有一个包可以做到这一点,mwim(移动我的意思)

      【讨论】:

        【解决方案4】:

        我改编@Vucovic代码先跳转到beggining-of-line

        (defun my-smart-beginning-of-line ()
          "Move point to beginning-of-line. If repeat command it cycle
        position between `back-to-indentation' and `beginning-of-line'."
          (interactive "^")
          (if (and (eq last-command 'my-smart-beginning-of-line)
                   (= (line-beginning-position) (point)))
              (back-to-indentation)
            (beginning-of-line)))
        
        (global-set-key [home] 'my-smart-beginning-of-line)
        

        【讨论】:

          【解决方案5】:

          感谢这个方便的功能。我现在一直使用它并且喜欢它。我只做了一个小改动: (交互的) 变成: (互动“^”)

          来自 emacs 帮助: 如果以^' andshift-select-mode' 开头的字符串不为nil,则Emacs 首先调用函数`handle-shift-select'。

          如果您使用 shift-select-mode,这基本上会使 shift-home 选择从当前位置到行首。它在 minibuffer 中特别有用。

          【讨论】:

            【解决方案6】:

            请注意,已经有一个返回缩进功能,它可以执行您希望第一个智能家居功能执行的操作,即转到行上的第一个非空白字符。它默认绑定到 M-m。

            【讨论】:

            • 是的,如果您阅读接受的答案,您会注意到关于回缩进和行首文本之间一些差异的讨论。
            • 对我来说,比智能家居钥匙更有用的是让 M-m 在后续按下时变得智能,类似于 C-l 过载的方式。
            【解决方案7】:

            这适用于 GNU Emacs,我没有用 XEmacs 尝试过。

            
            (defun My-smart-home () "Odd home to beginning of line, even home to beginning of text/code."
                (interactive)
                (if (and (eq last-command 'My-smart-home)
                        (/= (line-beginning-position) (point)))
                (beginning-of-line)
                (beginning-of-line-text))
            )
            
            (global-set-key [home] 'My-smart-home)
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-08-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-06-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多