简体   繁体   English

修复自动完成模式和linum模式烦恼

[英]fix an auto-complete-mode and linum-mode annoyance

I'm using auto-complete-mode which I think is totally fantastic. 我正在使用auto-complete-mode ,我认为这非常棒。 I'm also a big fan of linum-mode but I've got a very irritating issue when the two are used together, especially when I'm working in a new buffer (or a buffer with very few lines). 我也是linum-mode忠实粉丝,但是当两者一起使用时,我有一个非常恼人的问题,特别是当我在一个新的缓冲区(或一个很少行的缓冲区)工作时。

Basically the buffer is 'x' lines long but when auto-complete kicks in it "adds" lines to the buffer, so linum-mode keeps switching, for example, between displaying line numbers on one column or two columns, depending on whether auto-complete is suggesting a completion or not. 基本上缓冲区是'x'行很长但是当自动完成时它会向缓冲区“添加”行,因此linum-mode保持切换,例如,在一列或两列上显示行号,具体取决于是否auto-complete表示完成与否。

So you type a sentence and you see your buffer's content frantically shifting from left to right at every keypress. 因此,您键入一个句子,您会看到缓冲区的内容在每次按键时都会从左向右疯狂地移动。 It is really annoying. 真的很烦人。

I take it the solution involves configuring the linum-format variable but I don't know how. 我认为解决方案涉及配置linum格式变量,但我不知道如何。

Ideally it would be great if my linum-format was: 理想情况下,如果我的linum格式是:

  • dynamic 动态
  • right-aligned 右对齐
  • considering there are 'y' more lines to the buffer than what the buffer actually has 考虑到缓冲区的'y'行比缓冲区实际有的多

My rationale being that auto-complete shall not suggest more than 'y' suggestion and that, hence, the two shall start playing nicely together. 我的理由是, auto-complete不应该提出超过'y'的建议,因此,两者应该开始很好地一起玩。

For example, if 'y' is set to 20 and my buffer has 75 lines, then linum should use two columns: because no matter where I am auto-complete shall not make the buffer 'bigger' than 99 lines. 例如,如果'y'设置为20而我的缓冲区有75行,那么linum应该使用两列:因为无论我在哪里auto-complete都不会使缓冲区“大于”99行。

On the contrary, if 'y' is still set to 20 and my buffer has 95 lines, then linum should use three columns because otherwise if I'm near the end of the buffer and auto-complete kicks in my buffer shall start "wobbling" left and right when I type. 相反,如果'y'仍然设置为20并且我的缓冲区有95行,那么linum应该使用三列,否则如果我接近缓冲区的末尾并且我的缓冲区中的auto-complete踢将开始“摇摆“当我输入时左右。

I'd rather not hardcode "3 columns wide" for linum . 对于linum我宁愿不用硬编码“3列宽”。

I guess using "dynamic but always at least two columns" would somehow fix most annoyances but still something as I described would be great. 我想使用“动态但总是至少两列”会以某种方式解决大多数烦恼,但仍然像我描述的那样会很棒。

PS: I realize that my 'fix' would imply that linum would always display on at least two columns, and I'm fine with that... As long as it stays right-aligned and use 2, 3 or 4 columns depending on the need. PS:我意识到我的'修复'意味着linum总是会显示在至少两列上,我很好...只要它保持右对齐并使用2,3或4列,具体取决于需要。

Simply put the following line in .emacs which resolves this issue. 只需将以下行放在.emacs中即可解决此问题。 It is in auto-complete.el. 它在auto-complete.el中。

(ac-linum-workaround)

I've written a couple of previous answers on modifying the linum-mode output, which you could probably adapt to your purposes. 我已经写了几个关于修改linum-mode输出的答案,你可能已经适应了你的目的。

Edit: Here's the most basic version of that code (also on EmacsWiki , albeit somewhat buried), which doesn't modify the default output at all, but uses the techniques from those other answers to be more efficient than the default code. 编辑:这是该代码的最基本版本(也在EmacsWiki上 ,尽管有点埋没),它根本不修改默认输出,但使用其他答案中的技术比默认代码更有效。 That's probably a more useful starting point for you. 这对你来说可能是一个更有用的起点。

(defvar my-linum-format-string "%4d")

(add-hook 'linum-before-numbering-hook 'my-linum-get-format-string)

(defun my-linum-get-format-string ()
  (let* ((width (length (number-to-string
                         (count-lines (point-min) (point-max)))))
         (format (concat "%" (number-to-string width) "d")))
    (setq my-linum-format-string format)))

(setq linum-format 'my-linum-format)

(defun my-linum-format (line-number)
  (propertize (format my-linum-format-string line-number) 'face 'linum))

Just have the same problem, after seeing 'patching the source' I believe it could be done with advice . 在看到“修补源代码”之后遇到同样的问题我相信可以通过advice完成。 Here is what I come up with 这是我想出来的

(defadvice linum-update
  (around tung/suppress-linum-update-when-popup activate)
  (unless (ac-menu-live-p)
    ad-do-it))

I would like to use popup-live-p as mentioned but unfortunately it requires the variable for the popup, which we couldn't know in advance. 我想使用popup-live-p ,但不幸的是它需要弹出窗口的变量,我们事先无法知道。

Update: I ended up patching the source for linum.el. 更新:我最终修补了linum.el的源代码。 I added an extra hook that runs before updates. 我添加了一个在更新之前运行的额外钩子。

Here's the patched file: linum.el (github) 这是修补文件: linum.el(github)

Here's the code I have in my init.el: 这是我在init.el中的代码:

;; Load custom linum.
(load-file "~/.emacs.d/linum.el")

;; Suppress line number updates while auto-complete window
;; is displayed.
(add-hook 'linum-before-update-hook
          '(lambda ()
             (when auto-complete-mode
               (if (ac-menu-live-p)
                   (setq linum-suppress-updates t)
                 (setq linum-suppress-updates nil)))))

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM