   (save-excursion
     (not (eq (forward-line) 0))))
 
-(defun ac-menu-create (line column width height)
+(defun ac-menu-create (line column width height direction)
   "Create popup menu."
   (save-excursion
     (let ((overlays (make-vector height nil))
-          (window (selected-window)))
+          (window (selected-window))
+          menu-visual-column
+          current-visual-column)
       (goto-line line)
+      (move-to-column column)
+      (setq menu-visual-column (ac-current-physical-column))
       (dotimes (i height)
-        (move-to-column column)
-        (let (overlay begin w current-column (prefix "") (postfix ""))
-          (setq current-column (current-column))
-          (cond
-           ((> current-column column)
+        (let (overlay begin w (prefix "") (postfix ""))
+	  (vertical-motion direction)
+          (move-to-column (+ (current-column) menu-visual-column))
+	  (setq current-visual-column (ac-current-physical-column))
+
+	  (cond
+           ((> current-visual-column menu-visual-column)
             (backward-char)
-            (setq current-column (current-column))
-            (if (< current-column column)
-                (setq prefix (make-string (- column current-column) ? ))))
-           ((< current-column column)
-            (setq prefix (make-string (- column current-column) ? ))))
+            (setq current-visual-column (ac-current-physical-column))
+            (if (< current-visual-column menu-visual-column)
+                (setq prefix (make-string (- menu-visual-column current-visual-column) ? ))))
+	   ;; Extend short buffer lines by menu prefix (line of spaces)
+           ((< current-visual-column menu-visual-column)
+            (setq prefix (make-string (- menu-visual-column current-visual-column) ? ))))
 
           (setq begin (point))
           (setq w (+ width (length prefix)))

      (progn
        (setq ac-saved-local-map overriding-terminal-local-map)
        (if (eq ac-saved-local-map ac-complete-mode-map)
            ;; maybe never reach here
            (setq ac-saved-local-map nil))
        (setq overriding-terminal-local-map ac-complete-mode-map))
    ;; rearrange ac-mode-map pair first
    (assq-delete-all 'ac-completing minor-mode-map-alist)
    (push (cons 'ac-completing ac-complete-mode-map) minor-mode-map-alist)))


;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; This extension provides a way to complete with popup menu like:
;;
;;     def-!-
;;     +-----------------+
;;     |defun::::::::::::|
;;     |defvar           |
;;     |defmacro         |
;;     |       ...       |
;;     +-----------------+
;;
;; You can complete by typing and selecting menu.
;; Enjoy!

;;; Qualification:
;;
;; This extension can work properly on GNU Emacs 22 or higher.

;;; Installation:
;;
;; To use this extension, locate auto-complete.el to your load-path directory.
;;
;;     $ cp auto-complete.el ~/.emacs.d/
;;
;; And write following code into your .emacs.
;;
;;     (require 'auto-complete)
;;     (global-auto-complete-mode t)

;;; Tips:
;;
;; Use C-n/C-p to select candidates
;; --------------------------------
;;
;; Add following code to your .emacs.
;; 
;;     (define-key ac-complete-mode-map "\C-n" 'ac-next)
;;     (define-key ac-complete-mode-map "\C-p" 'ac-previous)
;;
;;
;; Don't start completion automatically
;; ------------------------------------
;;
;; Add following code to your .emacs.
;;
;;     (setq ac-auto-start nil)
;;     (global-set-key "\M-/" 'ac-start)
;;
;; or
;;
;;     ;; start completion when entered 3 characters
;;     (setq ac-auto-start 3)
;;
;;
;; Stop completion
;; ---------------
;;
;; Add following code to your .emacs.
;;
;;     (define-key ac-complete-mode-map "\M-/" 'ac-stop)
;;
;; Now you can stop completion by pressing M-/.
;;
;;
;; Completion by TAB
;; -----------------
;;
;; Add following code to your .emacs.
;;
;;     (define-key ac-complete-mode-map "\t" 'ac-complete)
;;     (define-key ac-complete-mode-map "\r" nil)
;;
;;
;; Do What I Mean mode
;; -------------------
;;
;; If DWIM (Do What I Mean) mode is enabled,
;; the following features is available:
