7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

anythingから、ローカルにあるrubygemsのファイル開く。bundlerも対応

Last updated at Posted at 2012-10-02

以下を評価して、
M-x anything-local-gems
すると、インストールされているgemの一覧が出るので、選択すると、 そのgemのlib/を開く。

また、カレントバッファの祖先ディレクトリにGemfileがみつかった場合、bundlerで管理されている方のgemが選択されまうす。

(defvar anything-c-sources-local-gem-file
  '((name . "rubygems")
    (candidates-in-buffer)
    (init . (lambda ()
              (let ((gemfile-dir (block 'find-gemfile
                                   (let* ((cur-dir (file-name-directory
                                                    (expand-file-name (or (buffer-file-name)
                                                                          default-directory))))
                                          (cnt 0))
                                     (while (and (< (setq cnt (+ 1 cnt)) 10)
                                                 (not (equal cur-dir "/")))
                                       (when (member "Gemfile" (directory-files cur-dir))
                                         (return-from 'find-gemfile cur-dir))
                                       (setq cur-dir (expand-file-name (concat cur-dir "/.."))))
                                     ))))
                (anything-attrset 'gem-command
                                  (concat (if gemfile-dir
                                              (concat "BUNDLE_GEMFILE=" gemfile-dir "/Gemfile "
                                                      "bundle exec ")
                                            "")
                                          "gem 2>/dev/null"))
                (unless (anything-candidate-buffer)
                  (call-process-shell-command (concat (anything-attr 'gem-command) " list")
                                              nil
                                              (anything-candidate-buffer 'local))))))
    (action . (lambda (gem-name)
                (let ((path (file-name-directory
                             (shell-command-to-string
                              (concat (anything-attr 'gem-command) " which "
                                      (replace-regexp-in-string "\s+(.+)$" "" gem-name))))))
                  (if (and path (file-exists-p path))
                      (find-file path)
                    (message "no such file or directory: \"%s\"" path))
                  )))))

(defun anything-local-gems ()
  (interactive)
  (anything-other-buffer
   '(anything-c-sources-local-gem-file)
   "*anything local gems*"
  ))
7
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?