-
Notifications
You must be signed in to change notification settings - Fork 1
/
pheliox-functions.el
206 lines (182 loc) · 7.58 KB
/
pheliox-functions.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
;; go to char next function
(defun wy-go-to-char (n char)
"Move forward to Nth occurence of CHAR.
Typing `wy-go-to-char-key' again will move forwad to the next Nth
occurence of CHAR."
(interactive "p\ncGo to char: ")
(search-forward (string char) nil nil n)
(while (char-equal (read-char)
char)
(search-forward (string char) nil nil n))
(setq unread-command-events (list last-input-event)))
(define-key global-map (kbd "C-c a") 'wy-go-to-char)
;; auto make scripts executable
(setq my-shebang-patterns
(list "^#!/usr/.*/perl\\(\\( \\)\\|\\( .+ \\)\\)-w *.*"
"^#!/usr/.*/sh"
"^#!/usr/.*/bash"
"^#!/bin/sh"
"^#!/.*/perl"
"^#!/.*/awk"
"^#!/.*/sed"
"^#!/bin/bash"))
(add-hook
'after-save-hook
(lambda ()
(if (not (= (shell-command (concat "test -x " (buffer-file-name))) 0))
(progn
;; This puts message in *Message* twice, but minibuffer
;; output looks better.
(message (concat "Wrote " (buffer-file-name)))
(save-excursion
(goto-char (point-min))
;; Always checks every pattern even after
;; match. Inefficient but easy.
(dolist (my-shebang-pat my-shebang-patterns)
(if (looking-at my-shebang-pat)
(if (= (shell-command
(concat "chmod u+x " (buffer-file-name)))
0)
(message (concat
"Wrote and made executable "
(buffer-file-name))))))))
;; This puts message in *Message* twice, but minibuffer output
;; looks better.
(message (concat "Wrote " (buffer-file-name))))))
;;auto set frame and window size
(defun set-frame-size-according-to-resolution ()
(interactive)
(if (display-graphic-p)
(progn
;; use 120 char wide window for largeish displays
;; and smaller 80 column windows for smaller displays
;; pick whatever numbers make sense for you
(if (> (x-display-pixel-width) 2560)
(add-to-list 'default-frame-alist (cons 'width 260))
(add-to-list 'default-frame-alist (cons 'width 120)))
;; for the height, subtract a couple hundred pixels
;; from the screen height (for panels, menubars and
;; whatnot), then divide by the height of a char to
;; get the height we want
(add-to-list 'default-frame-alist
(cons 'height (/ (- (x-display-pixel-height) 200)
(frame-char-height)))))))
(set-frame-size-according-to-resolution)
(defun copy-line (&optional arg)
"Do a kill-line but copy rather than kill. This function directly calls
kill-line, so see documentation of kill-line for how to use it including prefix
argument and relevant variables. This function works by temporarily making the
buffer read-only, so I suggest setting kill-read-only-ok to t."
(interactive "P")
(toggle-read-only 1)
(kill-line arg)
(toggle-read-only 0))
(setq-default kill-read-only-ok t)
(global-set-key "\C-c\C-k" 'copy-line)
;;yank and indent function replacement
(defun yank-and-indent ()
"Yank and then indent the newly formed region according to mode."
(interactive)
(yank)
(call-interactively 'indent-region))
(global-set-key "\C-y" 'yank-and-indent)
;;kill line and join forward
(defun kill-and-join-forward (&optional arg)
"If at end of line, join with following; otherwise kill line.
Deletes whitespace at join."
(interactive "P")
(if (and (eolp) (not (bolp)))
(progn
(delete-indentation t)
(if (looking-at " $")
(delete-char 1)))
(kill-line arg)))
(global-set-key "\C-k" 'kill-and-join-forward)
;;real auto-save link by save-buffer
(defun save-buffer-if-visiting-file (&optional args)
"Save the current buffer only if it is visiting a file"
(interactive)
(if (and (buffer-file-name) (buffer-modified-p))
(save-buffer args)))
(add-hook 'auto-save-hook 'save-buffer-if-visiting-file)
;; Allow buffer reverts to be undone
(defun my-revert-buffer (&optional ignore-auto noconfirm preserve-modes)
"Revert buffer from file in an undo-able manner."
(interactive)
(when (buffer-file-name)
;; Based upon `delphi-save-state':
;; Ensure that any buffer modifications do not have any side
;; effects beyond the actual content changes.
(let ((buffer-read-only nil)
(inhibit-read-only t)
(before-change-functions nil)
(after-change-functions nil))
(unwind-protect
(progn
;; Prevent triggering `ask-user-about-supersession-threat'
(set-visited-file-modtime)
;; Kill buffer contents and insert from associated file.
(widen)
(kill-region (point-min) (point-max))
(insert-file-contents (buffer-file-name))
;; Mark buffer as unmodified.
(set-buffer-modified-p nil))))))
(defadvice ask-user-about-supersession-threat
(around my-supersession-revert-buffer)
"Use my-revert-buffer in place of revert-buffer."
(let ((real-revert-buffer (symbol-function 'revert-buffer)))
(fset 'revert-buffer 'my-revert-buffer)
;; Note that `ask-user-about-supersession-threat' calls
;; (signal 'file-supersession ...), so we need to handle
;; the error in order to restore revert-buffer.
(unwind-protect
ad-do-it
(fset 'revert-buffer real-revert-buffer))))
(ad-activate 'ask-user-about-supersession-threat)
;;rename buffer and file
(defun rename-this-buffer-and-file ()
"Renames 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)))
(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))))))))
;;(global-set-key (kbd "C-c r") 'rename-this-buffer-and-file)
;; toggle comment single line
(defun comment-or-uncomment-region-or-line ()
"Comments or uncomments the region or the current line if there's no active region."
(interactive)
(let (beg end)
(if (region-active-p)
(setq beg (region-beginning) end (region-end))
(setq beg (line-beginning-position) end (line-end-position)))
(comment-or-uncomment-region beg end)))
(global-set-key (kbd "M-;") 'comment-or-uncomment-region-or-line)
;;desktop-save lock override
;;; desktop-override-stale-locks.el begins here
(defun emacs-process-p (pid)
"If pid is the process ID of an emacs process, return t, else nil.
Also returns nil if pid is nil."
(when pid
(let* ((cmdline-file (concat "/proc/" (int-to-string pid) "/cmdline")))
(when (file-exists-p cmdline-file)
(with-temp-buffer
(insert-file-contents-literally cmdline-file)
(goto-char (point-min))
(search-forward "emacs" nil t)
pid)))))
(defadvice desktop-owner (after pry-from-cold-dead-hands activate)
"Don't allow dead emacsen to own the desktop file."
(when (not (emacs-process-p ad-return-value))
(setq ad-return-value nil)))
;;; desktop-override-stale-locks.el ends here
(provide 'pheliox-functions)