Skip to content
2009/05/18 / highmt

auto-complete.el 表示乱れパッチ(多分)

auto-completeで、バッファの終わりを超えるときに表示が若干乱れる現象に対するパッチです。
バッファの最後にnewlineが追加されるのも消してみました。
ポップアップがなるべく乱れないことを優先したので
出てくる位置とか絞り込みの動作とかがおかしいことがあります。

Version 0.2.0に対するパッチです。
Meadow-3.00-r4213-1、emacs 22.2.0ubuntu2 で確認していますが、
前回のパッチのこともあるしやっぱりだめかもしれません。

それにしてもこのElispはとても素敵です。

--- auto-complete.el.orig	Sat May 16 06:21:52 2009
+++ auto-complete.el	Mon May 18 05:05:47 2009
@@ -432,10 +432,21 @@
             (setq column (- column width))))
       (if (> ac-menu-direction 0)
           (progn
-            (forward-line)
-            (if (eq line (line-number-at-pos))
-                (newline)
-              (forward-line -1))
+            (save-excursion
+              (catch 'exit-loop
+                (dotimes (i ac-candidate-menu-height)
+                  (forward-line)
+                  (end-of-line)
+                  (when (eq (point-max) (point))
+                    (cond ((and (<= (current-column) (+ column width)))
+                           (if (<= (window-start) (- line ac-candidate-menu-height))
+                               (progn
+                                 (setq line (- line ac-candidate-menu-height 1)
+                                       column (+ column 1)
+                                       ac-menu-direction -1))
+                               (when (< 0 (current-column))
+                                   (setq column (max 0 (- (current-column) width 1)))))))
+                    (throw 'exit-loop t)))))
             (setq ac-menu (ac-menu-create (1+ line) column width ac-candidate-menu-height))
             (setq ac-point point))
         (setq ac-menu (ac-menu-create (- line ac-candidate-menu-height) column width ac-candidate-menu-height))
@@ -1083,14 +1094,29 @@
   "Create popup menu."
   (save-excursion
     (let ((overlays (make-vector height nil))
-          (window (selected-window)))
+          (window (selected-window))
+          (prev-eobp nil))
       (goto-line line)
       (dotimes (i height)
-        (move-to-column column)
-        (let (overlay begin w current-column (prefix "") (postfix ""))
+        (let (overlay begin w current-column (prefix "") (postfix "")
+              (eobp nil) (pre-last-line-of-buffer nil) (last-line-of-buffer nil) (eol-column 0))
+          (setq eobp (eobp))
+          (save-excursion
+            (end-of-line)
+            (setq eol-column (current-column)))
+          (move-to-column column)
           (setq current-column (current-column))
+          (setq eobp (or eobp (eobp)))
+          (when (and eobp (not (eolp)) (or (eq i 0) (> eol-column (+ column width))))
+            (end-of-line)
+            (setq current-column 0
+                  pre-last-line-of-buffer t))
+          (when prev-eobp
+            (end-of-line)
+            (setq current-column 0))
+          (setq prev-eobp eobp)
           (cond
-           ((> current-column column)
+           ((and (> current-column column))
             (backward-char)
             (setq current-column (current-column))
             (if (< current-column column)
@@ -1106,21 +1132,40 @@
             (forward-char))
           (if (< w 0)
               (setq postfix (make-string (- w) ? )))
-          (if (ac-menu-last-line-of-buffer)
+          (when (and (eobp) (or (not pre-last-line-of-buffer)
+                                (and (eq i 0) (<= eol-column (+ column width)))))
               (setq postfix (concat postfix "\n")))
+          (when (or eobp
+                    (<= eol-column (+ column width)))
+            (setq last-line-of-buffer t))
 
           (setq overlay (make-overlay begin (point)))
+
           (overlay-put overlay 'window window)
           (overlay-put overlay 'prefix prefix)
           (overlay-put overlay 'postfix postfix)
+          (overlay-put overlay 'pre-last-line-of-buffer pre-last-line-of-buffer)
+          (overlay-put overlay 'last-line-of-buffer last-line-of-buffer)
           (overlay-put overlay 'width width)
           (overlay-put overlay 'set-string-function
                        (lambda (menu overlay string &optional face)
                          (overlay-put overlay
                                       'after-string
-                                      (concat (overlay-get overlay 'prefix)
-                                              (propertize (ac-menu-create-line-string menu string) 'face face)
-                                              (overlay-get overlay 'postfix)))))
+                                      (let ((line-string (ac-menu-create-line-string menu string)))
+                                        (concat 
+                                         (if (overlay-get overlay 'pre-last-line-of-buffer) "\n" "")
+                                         (overlay-get overlay 'prefix)
+                                         (propertize line-string 'face face)
+                                         (propertize
+                                          (let ((trail-string "")
+                                                (menu-width (ac-menu-width menu)))
+                                            (when (or (overlay-get overlay 'last-line-of-buffer))
+                                              (let ((string-length (length line-string)))
+                                                (when (< string-length menu-width)
+                                                  (setq trail-string (concat trail-string (make-string (- menu-width string-length) ? ))))))
+                                            trail-string)
+                                          'invisible t)
+                                         (overlay-get overlay 'postfix))))))
           (aset overlays i overlay))
         (forward-line))
       (let ((i 100))

—————-
2009/05/18 05:09
一部日本語文字列で乱れるケースがあったので修正。

広告
%d人のブロガーが「いいね」をつけました。