- (unless company-mode (error "Company not enabled"))
- (when (company-manual-begin)
- (save-window-excursion
- (let* ((height (window-height))
- (row (cdr (posn-col-row (posn-at-point))))
- (selected (nth company-selection company-candidates))
- (buffer (funcall company-backend 'doc-buffer selected)))
- (if (not buffer)
- (error "No documentation available.")
- (display-buffer buffer)
- (and (< (window-height) height)
- (< (- (window-height) row 2) company-tooltip-limit)
- (recenter (- (window-height) row 2)))
- (while (eq 'scroll-other-window
- (key-binding (vector (list (read-event)))))
- (scroll-other-window))
- (when last-input-event
- (clear-this-command-keys t)
- (setq unread-command-events (list last-input-event))))))))
+ (company-electric
+ (let ((selected (nth company-selection company-candidates)))
+ (display-buffer (or (funcall company-backend 'doc-buffer selected)
+ (error "No documentation available")) t))))
+(put 'company-show-doc-buffer 'company-keep t)
+
+(defun company-show-location ()
+ "Temporarily display a buffer showing the selected candidate in context."
+ (interactive)
+ (company-electric
+ (let* ((selected (nth company-selection company-candidates))
+ (location (funcall company-backend 'location selected))
+ (pos (or (cdr location) (error "No location available")))
+ (buffer (or (and (bufferp (car location)) (car location))
+ (find-file-noselect (car location) t))))
+ (with-selected-window (display-buffer buffer t)
+ (if (bufferp (car location))
+ (goto-char pos)
+ (goto-line pos))
+ (set-window-start nil (point))))))
+(put 'company-show-location 'company-keep t)
+
+;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(defvar company-callback nil)
+(make-variable-buffer-local 'company-callback)
+
+(defun company-remove-callback (&optional ignored)
+ (remove-hook 'company-completion-finished-hook company-callback t)
+ (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t))
+
+(defun company-begin-backend (backend &optional callback)
+ "Start a completion at point using BACKEND."
+ (interactive (let ((val (completing-read "Company back-end: "
+ obarray
+ 'functionp nil "company-")))
+ (when val
+ (list (intern val)))))
+ (when callback
+ (setq company-callback
+ `(lambda (completion)
+ (funcall ',callback completion)
+ (company-remove-callback)))
+ (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
+ (add-hook 'company-completion-finished-hook company-callback nil t))
+ (setq company-backend backend)
+ ;; Return non-nil if active.
+ (or (company-manual-begin)
+ (error "Cannot complete at point")))
+
+(defun company-begin-with (candidates
+ &optional prefix-length require-match callback)
+ "Start a completion at point.
+CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length of
+the prefix that already is in the buffer before point. It defaults to 0.
+
+CALLBACK is a function called with the selected result if the user successfully
+completes the input.
+
+Example:
+\(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
+ (company-begin-backend
+ (let ((start (- (point) (or prefix-length 0))))
+ `(lambda (command &optional arg &rest ignored)
+ (case command-history
+ ('prefix (message "prefix %s" (buffer-substring ,start (point)))
+ (when (>= (point) ,start)
+ (buffer-substring ,start (point))))
+ ('candidates (all-completions arg ',candidates))
+ ('require-match ,require-match))))
+ callback))