root/lang/elisp/anything-c-lisp-complete-symbol/anything-c-lisp-complete-symbol.el @ 8456

Revision 8456, 3.8 kB (checked in by imakado, 6 years ago)

lang/elisp/anything-c-lisp-complete-symbol/anything-c-lisp-complete-symbol.el
anythingを使ったelispシンボルの補完。

Line 
1;;; anything-c-lisp-complete-symbol.el ---  lisp-complete-symbol for anything -*- coding: utf-8; mode: emacs-lisp; -*-
2
3;; Author: Kenji Imakado <ken.imakaado@gmail.com>
4;; Version: 0.1
5;; Keywords: anything emacs-lisp
6
7;; This file is free software; you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation; either version 2, or (at your option)
10;; any later version.
11
12;; This file is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs; see the file COPYING.  If not, write to the
19;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20;; Boston, MA 02110-1301, USA.
21
22;;; Commentary
23
24;; lisp-complete-symbol with anything interface
25
26;; setting sample
27;; (define-key 'lisp-mode-map (kbd "C-M-i") 'anything-lisp-complete-symbol)
28;; or
29;; add to your anything-sources
30
31;; type C-M-i complete lisp symbol
32;; when before symbol started is `(' complete from only function symbol
33;; otherwise from all typeof symbol
34
35;; when anything-lisp-complete-symbol called with prefix arg (in case this sample, type: C-u C-M-i)
36;; complete from all typeof symbol.
37
38(require 'anything)
39
40(defun anything-lisp-complete-symbol (&optional all)
41  (interactive "P")
42  (let ((anything-sources (list anything-c-source-lisp-complete-symbol))
43        (anything-c-lisp-complete-symbol-all-type all))
44    (anything)))
45
46(defvar anything-c-lisp-complete-symbol-candidates nil)
47(defvar anything-c-source-lisp-complete-symbol
48  `((name . "Complete Symbol")
49    (init . (lambda ()
50              (setq anything-c-lisp-complete-symbol-candidates
51                    (anything-c-lisp-complete-symbol-get-cands))))
52    (candidates . anything-c-lisp-complete-symbol-candidates)
53    (action . (("Insert" . (lambda (candidate)
54                             (delete-backward-char (length anything-c-lisp-complete-symbol-initial-input))
55                             (insert candidate)))
56               ("Describe symbol" . (lambda (candidate)
57                                      (let* ((sym (intern-soft candidate))
58                                             (describe-fn (if (fboundp sym)
59                                                              'describe-function
60                                                            'describe-variable)))
61                                        (funcall describe-fn sym))))))))
62
63(defvar anything-c-lisp-complete-symbol-all-type nil)
64(defvar anything-c-lisp-complete-symbol-initial-input nil)
65(defun anything-c-lisp-complete-symbol-get-cands ()
66  (multiple-value-setq
67      (anything-c-lisp-complete-symbol-initial-input beg end type)
68      (anything-c-lisp-complete-symbol-get-cmp-context))
69  (cond
70   ((and (eq type 'function)
71         (not anything-c-lisp-complete-symbol-all-type))
72    (all-completions anything-c-lisp-complete-symbol-initial-input obarray 'functionp))
73   (t
74    (all-completions anything-c-lisp-complete-symbol-initial-input obarray))))
75
76(defun anything-c-lisp-complete-symbol-get-cmp-context ()
77  (let ((start (point))
78        (end (point))
79        (syntax "w_")
80        (type nil))
81    (condition-case nil
82        (save-excursion
83          (skip-syntax-backward syntax)
84          (setq start (point))
85          (when (eq (preceding-char) (string-to-char "("))
86            (setq type 'function))
87          (values (buffer-substring-no-properties start end) start end type))
88      (error (values "" (point) (point) nil)))))
89
90(provide 'anything-c-lisp-complete-symbol)
91;;; anything-c-lisp-complete-symbol.el ends here
Note: See TracBrowser for help on using the browser.