summary refs log tree commit diff
path: root/emacs.d/core/core-defun.el
blob: 855d17f7383b895386171541cf23ae09f71a021d (plain) (blame)
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
(defun fcuny/switch-to-scratch ()
  "Switch to scratch, grab the region if it's active."
  (interactive)
  (let ((contents
         (and (region-active-p)
              (buffer-substring (region-beginning)
                                (region-end)))))
    (switch-to-buffer "*scratch*")
    (if contents
        (progn
          (goto-char (buffer-end 1))
          (insert contents)))))

(defun fcuny/rename-this-buffer-and-file ()
  "Renames current buffer and file it is visiting."
  (interactive)
  (let ((name (buffer-name))
        (filename (buffer-file-name))
        (read-file-name-function 'read-file-name-default))
    (if (not (and filename (file-exists-p filename)))
        (error "Buffer '%s' is not visiting a file!" name)
      (let ((new-name (read-file-name "New name: " filename)))
        (cond ((get-buffer new-name)
               (error "A buffer named '%s' already exists!" new-name))
              (t
               (rename-file filename new-name 1)
               (rename-buffer new-name)
               (set-visited-file-name new-name)
               (set-buffer-modified-p nil)
               (message "File '%s' successfully renamed to '%s'" name (file-name-nondirectory new-name))))))))

(defun fcuny/visit-term-buffer ()
  "Create or visit a terminal buffer."
  (interactive)
  (if (not (get-buffer "*ansi-term*"))
      (progn
        (split-window-sensibly (selected-window))
        (other-window 1)
        (ansi-term (getenv "SHELL")))
    (switch-to-buffer-other-window "*ansi-term*")))

;; this functions are to make it easy to work with `pants'
(defvar fcuny/build-command "cd ~/src/source && ./pants --no-colors"
  "Command to use to execute the target")

(defvar fcuny/build-file "BUILD"
  "Name of the file containing our build targets")

(defun fcuny/--find-directory-containing-build-file (file)
  "Find the directory containing the build file."
  (let ((root nil)
        try)
    (while (not (or root
                    (null file)
                    (string-match locate-dominating-stop-dir-regexp file)))
      (setq try (if (stringp fcuny/build-file)
                    (file-exists-p (expand-file-name fcuny/build-file file))))
      (cond (try (setq root file))
            ((equal file (setq file (file-name-directory
                                     (directory-file-name file))))
             (setq file nil))))
    (and root (expand-file-name (file-name-as-directory root)))))

(defun fcuny/--build-action (target)
  (compile (format "%s binary %s" fcuny/build-command target)))

(defun fcuny/--build-target-list (file)
  "Generate a list of existing targets"
  (let ((build-command (format "%s list %s:" fcuny/build-command file))
        targets target)
    (with-temp-buffer
      (insert
       (shell-command-to-string build-command))
      (goto-char (point-min))
      (while (re-search-forward "^\\(.+\\)$" nil t)
        (setq target (match-string 1))
        (unless (or (save-excursion
                      (goto-char (match-beginning 0))
                      (forward-line -1)
                      (looking-at "^# Not a target:"))
                    (string-match "^\\." target))
          (push target targets))))
    (helm
     (helm :sources
           `((name . "Targets")
             (candidates . ,targets)
             (action . fcuny/build-action))))))

(defun fcuny/find-build-file ()
  "Find the build file and if it exists, open it."
  (interactive)
  (let ((build-file (fcuny/--find-directory-containing-build-file (file-name-directory (buffer-file-name)))))
    (if build-file
        (find-file (concat build-file fcuny/build-file))
      (error "Could not find %s" fcuny/build-file))))

(defun fcuny/build-run-target ()
  "List the targets for a BUILD file."
  (interactive)
  (let ((build-file (fcuny/--find-directory-containing-build-file (file-name-directory (buffer-file-name)))))
    (if build-file
        (fcuny/--build-target-list build-file)
      (error "Could not find %s" fcuny/build-file))))

(provide 'core-defun)