Mercurial > emacs
comparison lisp/dabbrev.el @ 106234:e5945bbfa5b1
(dabbrev--minibuffer-origin): Use minibuffer-selected-window.
(dabbrev-completion): Use completion-in-region.
(dabbrev--abbrev-at-point): Simplify regexp.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Wed, 25 Nov 2009 05:31:05 +0000 |
parents | 009383a57ce8 |
children | 1d1d5d9bd884 |
comparison
equal
deleted
inserted
replaced
106233:13733fc37569 | 106234:e5945bbfa5b1 |
---|---|
339 | 339 |
340 ;;---------------------------------------------------------------- | 340 ;;---------------------------------------------------------------- |
341 ;; Macros | 341 ;; Macros |
342 ;;---------------------------------------------------------------- | 342 ;;---------------------------------------------------------------- |
343 | 343 |
344 ;;; Get the buffer that mini-buffer was activated from | |
345 (defsubst dabbrev--minibuffer-origin () | 344 (defsubst dabbrev--minibuffer-origin () |
346 (car (cdr (buffer-list)))) | 345 "Get the buffer from which mini-buffer." |
346 (window-buffer (minibuffer-selected-window))) | |
347 | 347 |
348 ;; Make a list of some of the elements of LIST. | 348 ;; Make a list of some of the elements of LIST. |
349 ;; Check each element of LIST, storing it temporarily in the | 349 ;; Check each element of LIST, storing it temporarily in the |
350 ;; variable ELEMENT, and include it in the result | 350 ;; variable ELEMENT, and include it in the result |
351 ;; if CONDITION evaluates non-nil. | 351 ;; if CONDITION evaluates non-nil. |
362 ;;---------------------------------------------------------------- | 362 ;;---------------------------------------------------------------- |
363 ;; Exported functions | 363 ;; Exported functions |
364 ;;---------------------------------------------------------------- | 364 ;;---------------------------------------------------------------- |
365 | 365 |
366 ;;;###autoload (define-key esc-map "/" 'dabbrev-expand) | 366 ;;;###autoload (define-key esc-map "/" 'dabbrev-expand) |
367 ;;;??? Do we want this? | 367 ;;??? Do we want this? |
368 ;;;###autoload (define-key esc-map [?\C-/] 'dabbrev-completion) | 368 ;;;###autoload (define-key esc-map [?\C-/] 'dabbrev-completion) |
369 | 369 |
370 ;;;###autoload | 370 ;;;###autoload |
371 (defun dabbrev-completion (&optional arg) | 371 (defun dabbrev-completion (&optional arg) |
372 "Completion on current word. | 372 "Completion on current word. |
373 Like \\[dabbrev-expand] but finds all expansions in the current buffer | 373 Like \\[dabbrev-expand] but finds all expansions in the current buffer |
374 and presents suggestions for completion. | 374 and presents suggestions for completion. |
375 | 375 |
376 With a prefix argument, it searches all buffers accepted by the | 376 With a prefix argument ARG, it searches all buffers accepted by the |
377 function pointed out by `dabbrev-friend-buffer-function' to find the | 377 function pointed out by `dabbrev-friend-buffer-function' to find the |
378 completions. | 378 completions. |
379 | 379 |
380 If the prefix argument is 16 (which comes from C-u C-u), | 380 If the prefix argument is 16 (which comes from \\[prefix-argument] \\[prefix-argument]), |
381 then it searches *all* buffers." | 381 then it searches *all* buffers." |
382 (interactive "*P") | 382 (interactive "*P") |
383 (dabbrev--reset-global-variables) | 383 (dabbrev--reset-global-variables) |
384 (let* ((dabbrev-check-other-buffers (and arg t)) | 384 (let* ((dabbrev-check-other-buffers (and arg t)) |
385 (dabbrev-check-all-buffers | 385 (dabbrev-check-all-buffers |
386 (and arg (= (prefix-numeric-value arg) 16))) | 386 (and arg (= (prefix-numeric-value arg) 16))) |
387 (abbrev (dabbrev--abbrev-at-point)) | 387 (abbrev (dabbrev--abbrev-at-point)) |
388 (beg (progn (search-backward abbrev) (point))) | |
389 (end (progn (search-forward abbrev) (point))) | |
388 (ignore-case-p (and (if (eq dabbrev-case-fold-search 'case-fold-search) | 390 (ignore-case-p (and (if (eq dabbrev-case-fold-search 'case-fold-search) |
389 case-fold-search | 391 case-fold-search |
390 dabbrev-case-fold-search) | 392 dabbrev-case-fold-search) |
391 (or (not dabbrev-upcase-means-case-search) | 393 (or (not dabbrev-upcase-means-case-search) |
392 (string= abbrev (downcase abbrev))))) | 394 (string= abbrev (downcase abbrev))))) |
425 (t | 427 (t |
426 (mapc (function (lambda (string) | 428 (mapc (function (lambda (string) |
427 (intern (downcase string) my-obarray))) | 429 (intern (downcase string) my-obarray))) |
428 completion-list))) | 430 completion-list))) |
429 (setq dabbrev--last-obarray my-obarray) | 431 (setq dabbrev--last-obarray my-obarray) |
430 (setq dabbrev--last-completion-buffer (current-buffer)) | 432 (setq dabbrev--last-completion-buffer (current-buffer)))) |
431 ;; Find the longest common string. | 433 (completion-in-region beg end my-obarray))) |
432 (setq init (try-completion abbrev my-obarray)))) | |
433 ;;-------------------------------- | |
434 ;; Let the user choose between the expansions | |
435 ;;-------------------------------- | |
436 (or (stringp init) | |
437 (setq init abbrev)) | |
438 (cond | |
439 ;; * Replace string fragment with matched common substring completion. | |
440 ((and (not (string-equal init "")) | |
441 (not (string-equal (downcase init) (downcase abbrev)))) | |
442 (if (> (length (all-completions init my-obarray)) 1) | |
443 (message "Repeat `%s' to see all completions" | |
444 (key-description (this-command-keys))) | |
445 (message "The only possible completion")) | |
446 (dabbrev--substitute-expansion nil abbrev init nil)) | |
447 (t | |
448 ;; * String is a common substring completion already. Make list. | |
449 (message "Making completion list...") | |
450 (with-output-to-temp-buffer "*Completions*" | |
451 (display-completion-list (all-completions init my-obarray) | |
452 init)) | |
453 (message "Making completion list...done"))) | |
454 (and (window-minibuffer-p (selected-window)) | |
455 (message nil)))) | |
456 | 434 |
457 ;;;###autoload | 435 ;;;###autoload |
458 (defun dabbrev-expand (arg) | 436 (defun dabbrev-expand (arg) |
459 "Expand previous word \"dynamically\". | 437 "Expand previous word \"dynamically\". |
460 | 438 |
588 | 566 |
589 ;;---------------------------------------------------------------- | 567 ;;---------------------------------------------------------------- |
590 ;; Local functions | 568 ;; Local functions |
591 ;;---------------------------------------------------------------- | 569 ;;---------------------------------------------------------------- |
592 | 570 |
593 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer. | |
594 (defun dabbrev--same-major-mode-p (other-buffer) | 571 (defun dabbrev--same-major-mode-p (other-buffer) |
572 "Check if OTHER-BUFFER has the same major mode as current buffer." | |
595 (eq major-mode | 573 (eq major-mode |
596 (with-current-buffer other-buffer | 574 (with-current-buffer other-buffer |
597 major-mode))) | 575 major-mode))) |
598 | 576 |
599 ;;; Back over all abbrev type characters and then moves forward over | |
600 ;;; all skip characters. | |
601 (defun dabbrev--goto-start-of-abbrev () | 577 (defun dabbrev--goto-start-of-abbrev () |
578 "Back over all abbrev type characters and then moves forward over | |
579 all skip characters." | |
602 ;; Move backwards over abbrev chars | 580 ;; Move backwards over abbrev chars |
603 (save-match-data | 581 (save-match-data |
604 (when (> (point) (minibuffer-prompt-end)) | 582 (when (> (point) (minibuffer-prompt-end)) |
605 (forward-char -1) | 583 (forward-char -1) |
606 (while (and (looking-at dabbrev--abbrev-char-regexp) | 584 (while (and (looking-at dabbrev--abbrev-char-regexp) |
612 (forward-char 1))) | 590 (forward-char 1))) |
613 (and dabbrev-abbrev-skip-leading-regexp | 591 (and dabbrev-abbrev-skip-leading-regexp |
614 (while (looking-at dabbrev-abbrev-skip-leading-regexp) | 592 (while (looking-at dabbrev-abbrev-skip-leading-regexp) |
615 (forward-char 1))))) | 593 (forward-char 1))))) |
616 | 594 |
617 ;;; Extract the symbol at point to serve as abbreviation. | |
618 (defun dabbrev--abbrev-at-point () | 595 (defun dabbrev--abbrev-at-point () |
596 "Extract the symbol at point to serve as abbreviation." | |
619 ;; Check for error | 597 ;; Check for error |
620 (if (bobp) | 598 (if (bobp) |
621 (error "No possible abbreviation preceding point")) | 599 (error "No possible abbreviation preceding point")) |
622 ;; Return abbrev at point | 600 ;; Return abbrev at point |
623 (save-excursion | 601 (save-excursion |
628 ;; This is so the user can get successive words | 606 ;; This is so the user can get successive words |
629 ;; by typing the punctuation followed by M-/. | 607 ;; by typing the punctuation followed by M-/. |
630 (save-match-data | 608 (save-match-data |
631 (if (save-excursion | 609 (if (save-excursion |
632 (forward-char -1) | 610 (forward-char -1) |
633 (not (looking-at (concat "\\(" | 611 (not (looking-at (or dabbrev-abbrev-char-regexp |
634 (or dabbrev-abbrev-char-regexp | 612 "\\sw\\|\\s_")))) |
635 "\\sw\\|\\s_") | |
636 "\\)+")))) | |
637 (if (re-search-backward (or dabbrev-abbrev-char-regexp | 613 (if (re-search-backward (or dabbrev-abbrev-char-regexp |
638 "\\sw\\|\\s_") | 614 "\\sw\\|\\s_") |
639 nil t) | 615 nil t) |
640 (forward-char 1) | 616 (forward-char 1) |
641 (error "No possible abbreviation preceding point")))) | 617 (error "No possible abbreviation preceding point")))) |
642 ;; Now find the beginning of that one. | 618 ;; Now find the beginning of that one. |
643 (dabbrev--goto-start-of-abbrev) | 619 (dabbrev--goto-start-of-abbrev) |
644 (buffer-substring-no-properties | 620 (buffer-substring-no-properties |
645 dabbrev--last-abbrev-location (point)))) | 621 dabbrev--last-abbrev-location (point)))) |
646 | 622 |
647 ;;; Initializes all global variables | |
648 (defun dabbrev--reset-global-variables () | 623 (defun dabbrev--reset-global-variables () |
624 "Initialize all global variables." | |
649 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer | 625 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer |
650 ;; must not be reset here. | 626 ;; must not be reset here. |
651 (setq dabbrev--last-table nil | 627 (setq dabbrev--last-table nil |
652 dabbrev--last-abbreviation nil | 628 dabbrev--last-abbreviation nil |
653 dabbrev--last-abbrev-location nil | 629 dabbrev--last-abbrev-location nil |