代码之家  ›  专栏  ›  技术社区  ›  thebjorn

Emacs命令在光标上方插入和缩进行

  •  10
  • thebjorn  · 技术社区  · 17 年前

    1. 从一行的任意位置按C-return键,将光标移动到上方新插入的空白行,缩进正确(或至少与原始行相同)。
    2. 能够拖动任何文本。。。
    3. 和C-u C-space返回到原始位置

    我设法做到了1,但我的emacs fu不够强壮,无法完成其余的工作。

    3 回复  |  直到 14 年前
        1
  •  9
  •   ashawley    7 年前

    以下是我拙劣的解决方案:

    (defun my-insert-before-line ()
      (interactive)
      (save-excursion
        (beginning-of-line)
        ; I've changed the order of (yank) and (indent-according-to-mode)
        ; in order to handle the case when yanked line comes with its own indent
        (yank)(indent-according-to-mode)
        ; could be as well changed to simple (newline) it's metter of taste
        ; and of usage
        (newline-and-indent)))
    

        2
  •  3
  •   Cheeso    17 年前

    Emacs有一个记录宏,即kmacro开始宏和kmacro结束宏。

    录制完宏后,请命名最后一个kbd宏。然后访问.emacs,插入kbd宏。

    然后有一个定义宏的fset语句。它可能看起来很有趣,而且不像elisp那样可维护,但如果您将它塞进.emacs中,该宏(以该名称命名)将可用于任何编辑会话。您还可以将其绑定到密钥序列。

        3
  •  3
  •   thebjorn    17 年前

    回答我自己的问题可能形式不好,但Cheeso的回答促使我在十年内第二次使用lisp编程(我最初的版本是一个命名的键盘宏,但它跨越了kill/mark环)。这是我想到的

    (defun insert-and-indent-line-above ()
      (interactive)
      (push-mark)
      (let* 
        ((ipt (progn (back-to-indentation) (point)))
         (bol (progn (move-beginning-of-line 1) (point)))
         (indent (buffer-substring bol ipt)))
        (newline)
        (previous-line)
        (insert indent)))
    
    (global-set-key [ (control return) ] 'insert-and-indent-line-above)
    

    也许有很多更好的方法可以做到这一点,但两个小时的lisp黑客攻击很难被称为浪费时间:-)