File: anything-menu.el

package info (click to toggle)
anything-el 1.287-2.1
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd, sid, stretch
  • size: 824 kB
  • sloc: lisp: 13,158; sh: 43; makefile: 3
file content (167 lines) | stat: -rw-r--r-- 5,574 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
;;;; anything-menu.el --- anything.el candidate selection outside Emacs 
;; $Id: anything-menu.el,v 1.6 2010/04/01 12:10:35 rubikitch Exp $

;; Copyright (C) 2010  rubikitch

;; Author: rubikitch <rubikitch@ruby-lang.org>
;; Keywords: menu, tools, convenience, anything
;; URL: http://www.emacswiki.org/cgi-bin/wiki/download/anything-menu.el

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:
;;
;; This file provides anything.el candidate selection outside
;; Emacs. You have to enable emacsserver or gnuserv by M-x
;; server-start or M-x gnuserv-start.
;;
;; [EVAL IT] (describe-function 'anything-menu)
;; [EVAL IT] (describe-function 'anything-menu-select)
;; [EVAL IT] (describe-function 'anything-menu-select-from-file)
;;
;; First you have to install anything-menu script, which takes one argument, candidate file.
;; http://www.emacswiki.org/cgi-bin/wiki/download/anything-menu
;;
;; To demonstrate anything-menu, execute the following from shell
;; $ anything-menu ~/.emacs
;;

;;; Commands:
;;
;; Below are complete command list:
;;
;;  `anything-menu'
;;    Call `anything' outside Emacs.
;;
;;; Customizable Options:
;;
;; Below are customizable option list:
;;

;;; Installation:
;;
;; Put anything-menu.el to your load-path.
;; The load-path is usually ~/elisp/.
;; It's set in your ~/.emacs like this:
;; (add-to-list 'load-path (expand-file-name "~/elisp"))
;;
;; And the following to your ~/.emacs startup file.
;;
;; (require 'anything-menu)
;;
;; No need more.

;;; Customize:
;;
;;
;; All of the above can customize by:
;;      M-x customize-group RET anything-menu RET
;;


;;; History:

;; $Log: anything-menu.el,v $
;; Revision 1.6  2010/04/01 12:10:35  rubikitch
;; * document
;; * `anything-menu': ANY-KEYMAP argument
;;
;; Revision 1.5  2010/02/23 20:39:41  rubikitch
;; add `make-frame-visible'
;;
;; Revision 1.4  2010/02/23 16:48:41  rubikitch
;; migemized
;;
;; Revision 1.3  2010/02/23 10:23:52  rubikitch
;; New function `anything-menu-select-from-file'
;;
;; Revision 1.2  2010/02/23 10:10:34  rubikitch
;; implemented
;;
;; Revision 1.1  2010/02/23 09:44:09  rubikitch
;; initial
;;

;;; Code:

(defvar anything-menu-version "$Id: anything-menu.el,v 1.6 2010/04/01 12:10:35 rubikitch Exp $")
(require 'anything)
(defgroup anything-menu nil
  "anything-menu"
  :group 'emacs)

(defvar am/tmp-file "/tmp/.am-tmp-file")
(defvar am/frame nil)
(defun am/set-frame ()
  (unless (and am/frame (frame-live-p am/frame))
    (setq am/frame (make-frame '((name . "anything menu")
                                 (title . "anything menu")))))
  (select-frame am/frame)
  (make-frame-visible am/frame)
  (sit-for 0))

(defun am/close-frame ()
  (ignore-errors (make-frame-invisible am/frame))
  (when (fboundp 'do-applescript)
    (funcall 'do-applescript "tell application \"iTerm\"
                                activate
                             end")))
(defun am/write-result (line)
  (write-region (or line "") nil am/tmp-file))

(defun anything-menu (&optional any-sources any-input any-prompt any-resume any-preselect any-buffer any-keymap)
  "Call `anything' outside Emacs.
Arguments are the same as `anything'.
Pop up anything frame and close it after session."
  (interactive)
  (am/set-frame)
  (unwind-protect
      (let ((anything-samewindow t)
            (anything-display-function 'anything-default-display-buffer))
        (anything any-sources any-input any-prompt any-resume any-preselect any-buffer any-keymap))
    (am/close-frame)))

(defun anything-menu-select (am-prompt &rest am-selections)
  "Select from a list AM-SELECTIONS and write selection to /tmp/.am-tmp-file,
the default file of `am/tmp-file'. "
  (anything-menu `(((name . ,am-prompt)
                    (candidates . am-selections)
                    (migemo)
                    (action . am/write-result)))
                 nil (concat am-prompt ": ") nil nil "*anything menu select*"))

(defun* anything-menu-select-from-file (am-filename &optional (am-prompt "selection"))
  "Select a candidate in file AM-FILENAME and write selection to /tmp/.am-tmp-file,
the default file of `am/tmp-file'.

The anything-menu script calls this function and print selection to stdout."
  (anything-menu `(((name . ,am-prompt)
                    (init . (lambda ()
                              (with-current-buffer (anything-candidate-buffer 'global)
                                (insert-file-contents am-filename))))
                    (candidates-in-buffer)
                    (migemo)
                    (action . am/write-result)))
                 nil (concat am-prompt ": ") nil nil "*anything menu select*"))

(provide 'anything-menu)

;; (save-window-excursion (bg2 "gnudoit '(anything-menu-select \"selections\" \"a\" \"b\")'"))
;; (find-sh0 "cat /tmp/.am-tmp-file")

;; How to save (DO NOT REMOVE!!)
;; (emacswiki-post "anything-menu.el")
;;; anything-menu.el ends here