00-utils.el 6.7 KB
Newer Older
1 2 3
;;
;; Thanks to:
;; http://emacsredux.com/blog/2013/03/27/copy-filename-to-the-clipboard/
4
;; https://github.com/bwilkins
5 6
;;

7 8
(setq multi-term-program "zsh")

9
(defun utils/add-to-load-path (path-list)
10 11
  "Adds each path in path-list to the load path."
  (mapcar
12 13 14
    (lambda (path)
      (add-to-list 'load-path path))
    path-list))
15

16
(defun utils/change-all-terminal-dirs (new-directory)
17
  "Changes the default directory for every buffer that is a terminal."
18
  (interactive (list (read-directory-name "New directory:")))
19
  (mapcar
20 21
    '(lambda (buf)
       (if (eq (with-current-buffer buf major-mode) 'term-mode)
22 23 24 25 26 27
         (with-current-buffer buf (progn
                                    ;; change the buffer working directory
                                    (cd new-directory)

                                    ;; send a 'cd' command to the terminal
                                    (term-send-raw-string (concat "cd " new-directory "\n"))))))
28
    (buffer-list)))
29

30
(defun utils/create-or-switch-to-term (terminal-name)
Duncan Bayne's avatar
Duncan Bayne committed
31
  "Creates or switches to a named terminal, earmuffing the name if it isn't already."
32
  (interactive (list (ido-completing-read "Switch to term buffer: " (utils/term-buffer-names))))
33 34
  (if (string= "" terminal-name)
    (message "Refusing to create buffer with empty name")
35
    (let ((new-buffer-name (utils/earmuffenate terminal-name)))
36
      (if (get-buffer new-buffer-name)
Duncan Bayne's avatar
Duncan Bayne committed
37
        (switch-to-buffer new-buffer-name)
38 39
        (progn
          (require 'multi-term)
40
          (set-buffer (multi-term))
41
          (rename-buffer new-buffer-name))))))
42

43
(defun utils/create-terminals (terminal-names)
44 45
  "Creates terminals with the specified names using multi-term."
  (require 'multi-term)
46
  (mapcar
47
    (lambda (desired-name)
48
      (utils/create-or-switch-to-term desired-name))
Duncan Bayne's avatar
Duncan Bayne committed
49
    terminal-names))
50

51
(defun utils/earmuffenate (name)
Duncan Bayne's avatar
Duncan Bayne committed
52 53 54 55 56 57 58 59
  "Surrounds name with earmuffs, if it isn't already."
  (let ((new-name name))
    (if (not (string-prefix-p "*" new-name))
      (setq new-name (concat "*" new-name)))
    (if (not (string-suffix-p "*" new-name))
      (setq new-name (concat new-name "*")))
    new-name))

60
(defun utils/kill-unmodified-buffers ()
Duncan Bayne's avatar
Duncan Bayne committed
61
  "Kill any unmodified buffers that are inspecting files."
62 63 64 65 66 67 68
  (interactive)
  (mapcar
    '(lambda (buf)
       (if (and (buffer-file-name buf) (not (buffer-modified-p buf)))
         (kill-buffer buf)))
    (buffer-list)))

69 70 71 72 73 74 75 76 77
(defun utils/kill-ag-buffers ()
  "Kill all Silver Searcher buffers."
  (interactive)
  (mapcar
    '(lambda (buf)
       (if (string-match "^\*ag search text.*\*$" (buffer-name buf))
         (kill-buffer buf)))
    (buffer-list)))

78
(defun utils/kill-other-unmodified-buffers ()
Duncan Bayne's avatar
Duncan Bayne committed
79
  "Kill any unmodified buffers that are inspecting files, except for the current buffer."
80 81 82 83 84 85 86 87 88 89
  (interactive)
  (mapcar
    '(lambda (buf)
       (if (and
             (buffer-file-name buf)
             (not (buffer-modified-p buf))
             (not (equalp (current-buffer) buf)))
         (kill-buffer buf)))
    (buffer-list)))

90
(defun utils/set-mode (mode filename-pattern-list)
91 92 93 94 95 96 97 98
  "Sets the specified mode for each filename pattern in filename-pattern-list."
  (mapcar
    (lambda (filename-pattern)
      (setq
        auto-mode-alist
        (cons (cons filename-pattern mode) auto-mode-alist)))
    filename-pattern-list))

99
(defun utils/term-buffers ()
Duncan Bayne's avatar
Duncan Bayne committed
100 101 102 103
  "Returns all buffers that are terminals."
  (remove-if-not
    #'nil
    (mapcar
104
      (lambda (buffer) (if (utils/term-p buffer) buffer nil))
Duncan Bayne's avatar
Duncan Bayne committed
105 106
      (buffer-list))))

107
(defun utils/term-buffer-names ()
Duncan Bayne's avatar
Duncan Bayne committed
108
  "Returns the names of all buffers that are terminals."
109
  (mapcar #'buffer-name (utils/term-buffers)))
Duncan Bayne's avatar
Duncan Bayne committed
110

111
(defun utils/term-p (buffer)
Duncan Bayne's avatar
Duncan Bayne committed
112
  "Returns t if buffer is a terminal, nil otherwise."
113
  (equal 'term-mode (buffer-local-value 'major-mode buffer)))
114

115
(defun utils/region-string ()
116 117 118
  "Returns a string containing the currently selected region."
  (buffer-substring (mark) (point)))

119
(defun utils/visit-buffer-named-in-region ()
120 121
  "Opens the file named in the currently selected region."
  (interactive)
122
  (find-file (utils/region-string)))
Duncan Bayne's avatar
Duncan Bayne committed
123

124
(defun utils/display-ansi-colors ()
125 126 127 128
  "Replaces all ANSI color sequences in current buffer with actual colors."
  (interactive)
  (let ((inhibit-read-only t))
    (ansi-color-apply-on-region (point-min) (point-max))))
Duncan Bayne's avatar
Duncan Bayne committed
129

130
(defun utils/ensure-syncthing-running ()
Duncan Bayne's avatar
Duncan Bayne committed
131 132
  "Ensures syncthing is running."
  (interactive)
133
  (if (not (member "*syncthing*" (utils/term-buffer-names)))
Duncan Bayne's avatar
Duncan Bayne committed
134
    (progn
135
      (utils/create-or-switch-to-term "syncthing")
Duncan Bayne's avatar
Duncan Bayne committed
136
      (cd "~")
137
      (term-send-raw-string "killall syncthing\n")
Duncan Bayne's avatar
Duncan Bayne committed
138
      (term-send-raw-string "syncthing\n"))))
139

140
(defun utils/ensure-syncthing-stopped ()
141 142
  "Ensures syncthing is not running."
  (interactive)
143
  (if (member "*syncthing*" (utils/term-buffer-names))
144 145
    (progn
      (shell-command "killall syncthing")
146
      (utils/create-or-switch-to-term "syncthing")
147 148
      (term-send-raw-string "exit\n"))))

149
(defun utils/safe-shell-command (command)
Duncan Bayne's avatar
Duncan Bayne committed
150
  "Runs a shell command, and raises an error if it fails."
151 152
  (if (not (= 0 (shell-command command)))
    (error (concat "shell-command failed: " command))))
Duncan Bayne's avatar
Duncan Bayne committed
153

154
(defun utils/cd (directory-name)
Duncan Bayne's avatar
Duncan Bayne committed
155 156 157 158
  "Prompt user to enter a string, with input history support."
  (interactive
   (list
    (read-directory-name "Change into directory:")))
159 160 161
  (term-send-raw-string (concat "cd " directory-name "\n"))
  (cd directory-name))

162
(defun utils/atlas ()
163 164 165
  "Inserts a Unicode shrug"
  (interactive)
  (insert "¯\\_(ツ)_/¯"))
166 167 168 169 170 171 172 173 174 175

(defun copy-file-name-to-clipboard ()
  "Copy the current buffer file name to the clipboard."
  (interactive)
  (let ((filename (if (equal major-mode 'dired-mode)
                      default-directory
                    (buffer-file-name))))
    (when filename
      (kill-new filename)
      (message "Copied buffer file name '%s' to the clipboard." filename))))
176 177 178 179 180 181 182 183 184 185

(defun utils/insert-date ()
  "Insert the current date in org-mode format (YYYY-MM-DD DAY)."
  (interactive)
  (insert (format-time-string "%Y-%m-%d %a")))

(defun utils/insert-date-time ()
  "Insert the current datetime in org-mode format (YYYY-MM-DD DAY HH:MM)."
  (interactive)
  (insert (format-time-string "%Y-%m-%d %a %H:%M ")))
186

187 188 189 190 191
(defun utils/insert-time ()
  "Insert the current time in org-mode format (HH:MM)"
  (interactive)
  (insert (format-time-string "%H:%M")))

192 193 194 195 196
(defun utils/align-char (start end char)
 "Align block of text to first instance of given character."
 (interactive "r\nsAlign char: ")
 (align-regexp start end
   (concat "\\(\\s-*\\)" char) 1 1 nil))
197 198 199 200

(defun utils/disable-linum-mode ()
  "Disable linum mode."
  (linum-mode 0))
201 202 203 204

(defun utils/xrandr (args)
  "Run xrandr with the specified args, and wait for completion."
  (let ((split-args (split-string args)))
205
    (call-process "xrandr" nil "*xrandr*" t)
206
    (apply #'call-process `("xrandr" nil "*xrandr*" t ,@split-args))))