forked from saidie/emacs-helm-rdefs
/
helm-rdefs.el
75 lines (58 loc) · 2.45 KB
/
helm-rdefs.el
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
;;; helm-rdefs.el --- rdefs with helm interface -*- coding: utf-8; lexical-binding: t -*-
;; Copyright (C) 2016 by Hiroshi Saito
;; Version: 1.0.0
;; Author: Hiroshi Saito <monodie@gmail.com>
;; URL: https://github.com/saidie/helm-rdefs
;; Package-Requires: ((emacs "24") (helm "1.6.4"))
;; Keywords: matching, tools
;; This program 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 3 of the License, or
;; (at your option) any later version.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides helm interface for rdefs. Run `helm-rdefs' on ruby
;; file.
;;; Code:
(require 'helm)
(defvar helm-rdefs-command (executable-find "rdefs"))
(defvar helm-rdefs--current-file)
(defun helm-rdefs--exec-rdefs (path)
"Execute rdefs for file specified by `PATH'."
(shell-command-to-string (format "%s -n %s" helm-rdefs-command path)))
(defun helm-rdefs--init ()
"Initialize a candidate helm buffer with rdefs output for current file."
(let ((buf (helm-init-candidates-in-buffer 'global
(helm-rdefs--exec-rdefs helm-rdefs--current-file))))
(with-current-buffer buf
(goto-char (point-min))
(flush-lines "^[:space:]*$")
(while (re-search-forward "^ *\\([^ 0-9].*\\)?$" nil t 1)
(beginning-of-line)
(delete-char -1)
(just-one-space)))))
(defun helm-rdefs--goto (candidate)
"Go to the line of `CANDIDATE'."
(when (string-match "^ *\\([0-9]+\\):" candidate)
(let ((line (string-to-number (match-string 1 candidate))))
(goto-char (point-min))
(forward-line (1- line))))
(recenter (/ (window-height) 2)))
(defvar helm-rdefs--source
(helm-build-in-buffer-source "helm-rdefs"
:init #'helm-rdefs--init
:action '(("Go to" . helm-rdefs--goto))))
;;;###autoload
(defun helm-rdefs ()
"Open a helm buffer with rdefs output."
(interactive)
(setq helm-rdefs--current-file (buffer-file-name))
(helm :sources '(helm-rdefs--source)
:buffer "*helm-rdefs*"))
(provide 'helm-rdefs)
;;; helm-rdefs.el ends here