summaryrefslogtreecommitdiff
path: root/lisp/init-buffile.el
blob: 18f82736648da0dcca18d4e1feae4c23ac51866b (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
;;; Files and buffers

(defun km/rename-current-buffer-file ()
  "Rename current buffer and file it is visiting."
  (interactive)
  (let ((name (buffer-name))
        (filename (buffer-file-name)))
    (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)))
        (if (get-buffer new-name)
            (error "A buffer named '%s' already exists!" new-name)
          (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)))))))

(global-set-key (kbd "C-x C-r") 'km/rename-current-buffer-file)

;; https://github.com/purcell/emacs.d/blob/master/lisp/init-utils.el
(defun km/delete-this-file ()
  "Delete the current file, and kill the buffer."
  (interactive)
  (or (buffer-file-name) (error "No file is currently being edited"))
  (when (yes-or-no-p (format "Really delete '%s'?"
                             (file-name-nondirectory buffer-file-name)))
    (delete-file (buffer-file-name))
    (kill-this-buffer)))

;; http://emacs-fu.blogspot.com/2013/03/editing-with-root-privileges-once-more.html
(defun km/find-file-as-root ()
  "`ido-find-file` that automatically edits the file with
root-privileges (using tramp/sudo) if the file is not writable by
user."
  (interactive)
  (let ((file (ido-read-file-name "Edit as root: ")))
    (unless (file-writable-p file)
      (setq file (concat "/sudo:root@localhost:" file)))
    (find-file file)))

(global-set-key (kbd "C-x F") 'km/find-file-as-root)

(defun km/save-and-kill-buffer ()
  "Save current buffer and then kill it"
  (interactive)
  (save-buffer)
  (kill-this-buffer))

(global-set-key (kbd "C-x K") 'kill-buffer-and-window)
(key-chord-define-global ",f" 'find-file)

(key-chord-define-global ",s" 'save-buffer)
(key-chord-define-global ",q" 'kill-this-buffer)
(key-chord-define-global ",d" 'km/save-and-kill-buffer)
(key-chord-define-global ",e" '(lambda ()
                                 (interactive)
                                 (save-buffer)
                                 (server-edit)))

;;; Ibuffer

;; Replace buffer-menu with ibuffer.
(global-set-key (kbd "C-x C-b") 'ibuffer)

;; Modified from http://martinowen.net/blog/2010/02/tips-for-emacs-ibuffer.html
;; and here http://www.elliotglaysher.org/emacs/.

(setq ibuffer-saved-filter-groups
      '(("home"
         ("elisp" (mode . emacs-lisp-mode))
         ("org" (or (mode . org-mode)
                    (name . "^\\*Org Agenda\\*$")
                    (name . "^\\*Calendar\\*$")))
         ("web" (or (mode . html-mode)
                    (mode . nxml-mode)
                    (mode . css-mode)
                    (mode . less-css-mode)))
         ("r" (or (mode . ess-mode)
                  (mode . inferior-ess-mode)
                  (name . "^\\*ESS\\*$")))
         ("markup" (or (mode . markdown-mode)
                       (mode . rst-mode)))
         ("lua" (mode . lua-mode))
         ("perl" (mode . perl-mode))
         ("python" (or (mode . python-mode)
                       (name . "^\\*Python")))
         ("haskell" (mode . haskell-mode))
         ("text" (mode . text-mode))
         ("shell" (mode . sh-mode))
         ("latex" (or (mode . latex-mode)
                      (mode . LaTeX-mode)
                      (mode . bibtex-mode)
                      (mode . reftex-mode)))
         ("dir" (or (mode . dired-mode)
                    (name . "^\\*tramp")))
         ("terminal" (or (mode . term-mode)
                         (name . "^\\*Shell Command Output*")
                         (name . "^\\*External terminal*")))
         ("packages" (or (name . "^\\*Compile-Log\\*")
                         (name . "^\\*Packages\\*")))
         ("magit" (name . "^\\*magit"))
         ("emacs" (or (name . "^\\*scratch\\*$")
                      (name . "^\\*Messages\\*$")))
         ("mail" (or (mode . message-mode)
                     (mode . mail-mode)
                     (mode . gnus-group-mode)
                     (mode . gnus-summary-mode)
                     (mode . gnus-article-mode)
                     (mode . notmuch-search-mode)
                     (mode . notmuch-show-mode)
                     (name . "^\\*Mail sync\\*$")
                     (name . "^\\*Gnus sync\\*$")
                     (name . "^\\.newsrc-dribble")))
         ("help" (or (name . "^\\*Help\\*$")
                     (name . "^\\*Apropos\\*$")
                     (name . "^\\*info\\*$")
                     (name . "^\\*Completions\\*"))))))

(add-hook 'ibuffer-mode-hook
          '(lambda ()
             (ibuffer-auto-mode 1) ; Keep buffer list up-to-date.
             (ibuffer-switch-to-saved-filter-groups "home")))

(setq
 ;; Don't prompt to delete unmodified buffers.
 ibuffer-expert t
 ;; Don't show empty filter groups.
 ibuffer-show-empty-filter-groups nil)

;; Recent files
(setq recentf-save-file "~/.emacs.d/cache/recentf"
      recentf-max-saved-items 200
      recentf-max-menu-items 15)
(recentf-mode t)

(provide 'init-buffile)