00-utils.el 3.67 KB
Newer Older
1
(defun utils:add-to-load-path (path-list)
2 3
  "Adds each path in path-list to the load path."
  (mapcar
4 5 6
    (lambda (path)
      (add-to-list 'load-path path))
    path-list))
7

8
(defun utils:change-all-terminal-dirs (new-directory)
9
  "Changes the default directory for every buffer that is a terminal."
10
  (interactive (list (read-directory-name "New directory:")))
11
  (mapcar
12 13
    '(lambda (buf)
       (if (eq (with-current-buffer buf major-mode) 'term-mode)
14 15 16 17 18 19
         (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"))))))
20
    (buffer-list)))
21

22
(defun utils:create-or-switch-to-term (terminal-name)
Duncan Bayne's avatar
Duncan Bayne committed
23
  "Creates or switches to a named terminal, earmuffing the name if it isn't already."
24
  (interactive (list (ido-completing-read "Term buffer: " (utils:term-buffer-names))))
25 26
  (if (string= "" terminal-name)
    (message "Refusing to create buffer with empty name")
27
    (let ((new-buffer-name (utils:earmuffenate terminal-name)))
28
      (if (get-buffer new-buffer-name)
Duncan Bayne's avatar
Duncan Bayne committed
29
        (switch-to-buffer new-buffer-name)
30 31 32
        (progn
          (require 'multi-term)
          (setq multi-term-program "/usr/bin/zsh")
33
          (set-buffer (multi-term))
34
          (rename-buffer new-buffer-name))))))
35

36
(defun utils:create-terminals (terminal-names)
37 38
  "Creates terminals with the specified names using multi-term."
  (require 'multi-term)
Duncan Bayne's avatar
Duncan Bayne committed
39
  (setq multi-term-program "/usr/bin/zsh")
40
  (mapcar
41
    (lambda (desired-name)
42
      (utils:create-or-switch-to-term desired-name))
Duncan Bayne's avatar
Duncan Bayne committed
43
    terminal-names))
44

45
(defun utils:earmuffenate (name)
Duncan Bayne's avatar
Duncan Bayne committed
46 47 48 49 50 51 52 53
  "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))

54
(defun utils:kill-unmodified-buffers ()
55 56 57 58 59 60 61 62
  "Kill any unmodifier buffers that are inspecting files."
  (interactive)
  (mapcar
    '(lambda (buf)
       (if (and (buffer-file-name buf) (not (buffer-modified-p buf)))
         (kill-buffer buf)))
    (buffer-list)))

63
(defun utils:set-mode (mode filename-pattern-list)
64 65 66 67 68 69 70 71
  "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))

72
(defun utils:term-buffers ()
Duncan Bayne's avatar
Duncan Bayne committed
73 74 75 76
  "Returns all buffers that are terminals."
  (remove-if-not
    #'nil
    (mapcar
77
      (lambda (buffer) (if (utils:term-p buffer) buffer nil))
Duncan Bayne's avatar
Duncan Bayne committed
78 79
      (buffer-list))))

80
(defun utils:term-buffer-names ()
Duncan Bayne's avatar
Duncan Bayne committed
81
  "Returns the names of all buffers that are terminals."
82
  (mapcar #'buffer-name (utils:term-buffers)))
Duncan Bayne's avatar
Duncan Bayne committed
83

84
(defun utils:term-p (buffer)
Duncan Bayne's avatar
Duncan Bayne committed
85
  "Returns t if buffer is a terminal, nil otherwise."
86
  (equal 'term-mode (buffer-local-value 'major-mode buffer)))
87 88 89 90 91 92 93 94 95

(defun utils:region-string ()
  "Returns a string containing the currently selected region."
  (buffer-substring (mark) (point)))

(defun utils:visit-buffer-named-in-region ()
  "Opens the file named in the currently selected region."
  (interactive)
  (find-file (utils:region-string)))
Duncan Bayne's avatar
Duncan Bayne committed
96

97 98 99 100 101 102 103 104 105
(defun utils:ensure-bluetooth-running ()
  "Ensures the bluetooth applet is running."
  (interactive)
  (if (not (member "*bluetooth*" (utils:term-buffer-names)))
    (progn
      (utils:create-or-switch-to-term "bluetooth")
      (cd "~")
      (mapcar #'term-send-raw-string '("cd ~\n"
                                        "blueman-applet\n")))))