Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

2073 linhas
74 KiB

  1. ;; 外部ファイル読み込み
  2. ;; (load-file "~/filepath")
  3. ;; (thing-at-point 'word)
  4. ;; (define-generic-mode)
  5. (unless (file-directory-p (expand-file-name "~/.emacs.d"))
  6. (make-directory (expand-file-name "~/.emacs.d")))
  7. (unless (file-directory-p (expand-file-name "~/.emacs.d/lisp"))
  8. (make-directory (expand-file-name "~/.emacs.d/lisp")))
  9. (add-to-list 'load-path "~/.emacs.d/lisp")
  10. (add-to-list 'load-path "~/dbx/Public/myelisp")
  11. (require 'cl)
  12. (defvar my-prefix-map
  13. (make-sparse-keymap))
  14. (global-set-key (kbd "C-z") my-prefix-map)
  15. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  16. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  17. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  18. ;; start and quit
  19. (setq inhibit-startup-message t)
  20. (setq frame-title-format (list '(:eval (my-format-time-string))
  21. " | %b "
  22. '(:eval (number-to-string (length (buffer-list-not-start-with-space))))
  23. " buffers ["
  24. invocation-name
  25. " "
  26. emacs-version
  27. " "
  28. (symbol-name system-type)
  29. "] "
  30. '(:eval (symbol-name last-command))))
  31. (defun buffer-list-not-start-with-space ()
  32. (let ((bl (buffer-list))
  33. b nbl)
  34. (while bl
  35. (setq b (pop bl))
  36. (unless (string-equal " "
  37. (substring (buffer-name b)
  38. 0
  39. 1))
  40. (add-to-list 'nbl b)))
  41. nbl))
  42. (setq confirm-kill-emacs 'y-or-n-p)
  43. (setq gc-cons-threshold (* 1024 1024 4))
  44. (when window-system
  45. (add-to-list 'default-frame-alist '(cursor-type . box))
  46. (add-to-list 'default-frame-alist '(background-color . "white"))
  47. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  48. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100))) ;聞いてないみたい
  49. )
  50. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  51. (tool-bar-mode 0)
  52. (set-scroll-bar-mode nil)
  53. (add-hook 'kill-emacs-hook ; 終了時に読み込んで壊れてないか調べる
  54. (lambda ()
  55. (when (file-readable-p "~/.emacs")
  56. (load-file "~/.emacs"))))
  57. (add-hook 'after-init-hook
  58. (lambda ()
  59. (message "init time: %d msec"
  60. (+ (* (- (nth 1 after-init-time) (nth 1 before-init-time)) 1000)
  61. (/ (- (nth 2 after-init-time) (nth 2 before-init-time)) 1000)))
  62. (switch-to-buffer "*Messages*")))
  63. (cd ".") ; when using windows use / instead of \ in `default-directory'
  64. ;; locale
  65. (set-language-environment "Japanese")
  66. (set-default-coding-systems 'utf-8)
  67. (prefer-coding-system 'utf-8)
  68. (setq system-time-locale "C")
  69. ;; display
  70. (setq redisplay-dont-pause t)
  71. (setq visible-bell t)
  72. (setq ring-bell-function 'ignore)
  73. ;; (comint-show-maximum-output)
  74. ;; kill scratch
  75. (add-hook 'after-init-hook
  76. (lambda ()
  77. (kill-buffer "*scratch*")))
  78. (defun my-delete-frame-or-kill-emacs ()
  79. "delete frame when opening multiple frame, kill emacs when only one."
  80. (interactive)
  81. (if (eq 1
  82. (length (frame-list)))
  83. (save-buffers-kill-emacs)
  84. (delete-frame)))
  85. (global-set-key (kbd "C-x C-c") 'my-delete-frame-or-kill-emacs)
  86. (define-key my-prefix-map (kbd "C-x C-c") 'save-buffers-kill-emacs)
  87. ;; modifier keys
  88. (setq mac-option-modifier 'control)
  89. (setq w32-apps-modifier 'meta)
  90. ;; http://www.emacswiki.org/emacs/ChangingCursorDynamically
  91. ;; bufferの保存はなんで必要なんだろう?
  92. ;; Change cursor color according to mode
  93. (defvar hcz-set-cursor-color-color "")
  94. (defvar hcz-set-cursor-color-buffer "")
  95. (defun hcz-set-cursor-color-according-to-mode ()
  96. "change cursor color according to some minor modes."
  97. ;; set-cursor-color is somewhat costly, so we only call it when needed:
  98. (let ((color
  99. (if buffer-read-only "blue"
  100. (if overwrite-mode "yellow"
  101. "black"))))
  102. (unless (and
  103. (string= color hcz-set-cursor-color-color)
  104. (string= (buffer-name) hcz-set-cursor-color-buffer))
  105. (set-cursor-color (setq hcz-set-cursor-color-color color))
  106. (setq hcz-set-cursor-color-buffer (buffer-name)))))
  107. (add-hook 'post-command-hook 'hcz-set-cursor-color-according-to-mode)
  108. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  109. ;; minibuffer
  110. (setq insert-default-directory t)
  111. (setq completion-ignore-case t
  112. read-file-name-completion-ignore-case t
  113. read-buffer-completion-ignore-case t)
  114. (setq resize-mini-window t)
  115. (temp-buffer-resize-mode 1)
  116. (fset 'yes-or-no-p 'y-or-n-p)
  117. (define-key read-expression-map (kbd "TAB") 'lisp-complete-symbol) ; complete symbol when `eval'
  118. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  119. ;; editting
  120. (setq require-final-newline t)
  121. (setq kill-whole-line t)
  122. (setq scroll-conservatively 35
  123. scroll-margin 2
  124. scroll-step 0) ;4行ずつスクロール?
  125. (setq default-major-mode 'text-mode)
  126. (setq next-line-add-newlines nil)
  127. (setq kill-read-only-ok t)
  128. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  129. ;; (setq-default line-spacing 0.2)
  130. (setq-default indicate-empty-lines t) ; なんだろうこれ
  131. (setq-default tab-width 4)
  132. (setq-default indent-tabs-mode nil)
  133. (pc-selection-mode 1)
  134. (delete-selection-mode 1)
  135. (cua-mode 0)
  136. ;; key bindings
  137. ;; moving around
  138. ;; (global-set-key (kbd "M-j") 'next-line)
  139. ;; (global-set-key (kbd "M-k") 'previous-line)
  140. ;; (global-set-key (kbd "M-h") 'backward-char)
  141. ;; (global-set-key (kbd "M-l") 'forward-char)
  142. ;;(keyboard-translate ?\M-j ?\C-j)
  143. (global-set-key (kbd "M-p") 'backward-paragraph)
  144. (global-set-key (kbd "M-n") 'forward-paragraph)
  145. (global-set-key (kbd "C-<up>") (lambda () (interactive)(scroll-down 1)))
  146. (global-set-key (kbd "C-<down>") (lambda () (interactive)(scroll-up 1)))
  147. (global-set-key (kbd "C-<left>") 'scroll-down)
  148. (global-set-key (kbd "C-<right>") 'scroll-up)
  149. (global-set-key (kbd "C-x M-x") 'execute-extended-command)
  150. (global-set-key (kbd "C-x M-:") 'eval-expression)
  151. (global-set-key (kbd "<select>") 'previous-line-mark)
  152. ;; C-h and DEL
  153. (global-set-key (kbd "C-h") (kbd "DEL"))
  154. ;; (global-set-key (kbd "C-h") 'backward-delete-char-untabify)
  155. ;; (global-set-key (kbd "DEL") help-map)
  156. ;; (global-set-key (kbd "C-h") (lambda ()
  157. ;; (interactive)
  158. ;; (call-interactively (key-binding (kbd "DEL")))))
  159. ;; (keyboard-translate ?\^h ?\^?) ; scimにはC-hを送りたい
  160. ;; (keyboard-translate ?\b ?\^h) ; dont translate backspace
  161. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  162. (global-set-key (kbd "C-o")
  163. ;; (lambda ()
  164. ;; (interactive)
  165. ;; (move-end-of-line nil)
  166. ;; (newline-and-indent))
  167. (kbd "C-e C-m")
  168. )
  169. (global-set-key (kbd "C-k") 'kill-whole-line)
  170. (global-set-key (kbd "M-k") 'my-copy-whole-line)
  171. ;; (global-set-key "\C-z" 'undo) ; undo is C-/
  172. ;; (global-set-key (kbd "C-<return>") (lambda () (interactive) (insert "\f\n")))
  173. (global-set-key (kbd "M-u") 'undo)
  174. (global-set-key (kbd "C-r") 'query-replace-regexp)
  175. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  176. (global-set-key (kbd "M-i")
  177. (lambda ()
  178. (interactive)
  179. (call-interactively (key-binding (kbd "M-TAB"))))
  180. ;; (kbd "M-TAB")
  181. )
  182. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  183. ;; letters, font-lock mode and fonts
  184. ;; change color for border
  185. ;; (set-face-foreground (make-face 'vertical-border-face) "white")
  186. ;; (set-face-background 'vertical-border-face "black")
  187. ;; (defface vertical-border-face
  188. ;; `((((background dark))
  189. ;; (:background "white"))
  190. ;; (((background light))
  191. ;; (:background "black")))
  192. ;; "vertical border")
  193. ;; (set-display-table-slot standard-display-table 'vertical-border
  194. ;; (make-glyph-code #x3a 'vertical-border-face))
  195. ;; (set-face-foreground 'vertical-border "default")
  196. ;; (set-face-background 'vertical-border "white")
  197. (when (eq system-type 'Darwin)
  198. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  199. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  200. (when (and (boundp 'input-method-activate-hook) ;ちょっと正しいかわかんない
  201. (boundp 'input-method-inactivate-hook))
  202. (add-hook 'input-method-activate-hook
  203. (lambda () (set-cursor-color "red")))
  204. (add-hook 'input-method-inactivate-hook
  205. (lambda () (set-cursor-color "black"))))
  206. (show-paren-mode 1)
  207. (setq show-paren-style 'mixed)
  208. (transient-mark-mode 1)
  209. (global-font-lock-mode 1)
  210. (standard-display-ascii ?\n "$\n")
  211. (copy-face 'default 'my-eol-face)
  212. (set-face-foreground 'my-eol-face "green")
  213. ;; (defface my-eol-face
  214. ;; '((t (:foreground "green")))
  215. ;; "eol.")
  216. (standard-display-ascii ?\f "---------------------------------------------------------------------------------------^L")
  217. (defface my-pagebreak-face
  218. '((t (:foreground "gray")))
  219. "pagebreak.")
  220. (defvar my-eol-face
  221. '(("\n" . (0 my-eol-face t nil)))
  222. )
  223. (defvar my-pagebreak-face
  224. '(("\f" . 'my-pagebreak-face)))
  225. (defvar my-highlight-face
  226. '(("\t" . '(0 highlight t nil))
  227. (" " . '(0 highlight t nil))))
  228. ;; (defvar my-face
  229. ;; '(("\t" . 'highlight)
  230. ;; (" " . 'highlight)
  231. ;; ("\n" . '(0 my-eol-face t nil))
  232. ;; ("\f" . 'my-pagebreak-face)))
  233. ;; 現在行をハイライト
  234. ;; http://wiki.riywo.com/index.php?Meadow
  235. (defface hlline-face
  236. '((((type x w32)
  237. (class color)
  238. (background dark))
  239. (:background "midnightblue")) ; :foreground "white")) ;; ハイライトの文字色は変えない方がいいかも
  240. (((type x w32)
  241. (class color)
  242. (background light))
  243. (:background "gainsboro"))
  244. (t
  245. (:underline "black")))
  246. "*Face used by hl-line.")
  247. ;; (defface hlline-ul-face
  248. ;; '((t (:underline "yellow")))
  249. ;; "underline yellow")
  250. (setq hl-line-face 'hlline-face) ;; (setq hl-line-face nil)
  251. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  252. (setq hl-line-global-modes
  253. '(not
  254. term-mode))
  255. (setq font-lock-global-modes
  256. '(not
  257. help-mode
  258. eshell-mode
  259. term-mode
  260. Man-mode))
  261. (add-hook 'font-lock-mode-hook
  262. (lambda ()
  263. ;; (font-lock-add-keywords nil my-eol-face)
  264. ;; (font-lock-add-keywords nil my-highlight-face)
  265. ))
  266. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  267. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  268. ;; fonts
  269. ;; (defun my-w32-set-font ()
  270. ;; "set font for windows."
  271. ;; ((lambda (&rest lists)
  272. ;; (let ((dir "c:/WINDOWS/Fonts")
  273. ;; lfs)
  274. ;; (while lists
  275. ;; (setq lfs (pop lists))
  276. ;; (when (directory-files dir nil (car lfs))
  277. ;; (my-set-ascii-and-jp-font-with-size (cdr lfs))
  278. ;; (setq lists nil)))))
  279. ;; '("^DejaVuSansMono" "dejavu sans mono" 80 "ms gothic" 12)
  280. ;; '("^inconsolata" "inconsolata" 100 "ms gothic" 12)))
  281. ;; (defun my-w32-set-available-ascii-and-jp-font-with-size (&rest lists)
  282. ;; ""
  283. ;; (let ((dir "c:/WINDOWS/Fonts")
  284. ;; lfs)
  285. ;; (while lists
  286. ;; (setq lfs (pop lists))
  287. ;; (when (directory-files dir nil (car lfs))
  288. ;; (my-set-ascii-and-jp-font-with-size (cdr lfs))
  289. ;; (setq lists nil)))))
  290. (defun my-set-ascii-and-jp-font-with-size (list)
  291. ""
  292. (set-face-attribute 'default nil
  293. :family (nth 0 list)
  294. :height (nth 1 list))
  295. (set-fontset-font "fontset-default"
  296. 'japanese-jisx0208
  297. (font-spec :family (nth 2 list) :size (nth 3 list)))
  298. (set-fontset-font "fontset-default"
  299. 'katakana-jisx0201
  300. (font-spec :family (nth 2 list) :size (nth 3 list)))) ; font spec is available in emacs23 and later, cannot used in emacs22
  301. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 90 "takaogothic" 13))
  302. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 100 "takaogothic" 14))
  303. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 100 "ms gothic" 14))
  304. ;; (my-set-ascii-and-jp-font-with-size '("monaco" 75 "takaogothic" 11))
  305. ;; (my-set-ascii-and-jp-font-with-size '("monaco" 90 "takaogothic" 13))
  306. ;; (my-set-ascii-and-jp-font-with-size '("ProggyCleanTTSZ" 120 "takaogothic" 11))
  307. ;; あ a
  308. emacs-major-version
  309. (defun my-22-set-ascii-and-jp-font-with-size (list)
  310. ""
  311. (set-face-attribute 'default nil
  312. :family (nth 0 list)
  313. :height (nth 1 list))
  314. (set-fontset-font "fontset-default"
  315. 'japanese-jisx0208
  316. (cons (nth 2 list) "jisx0208.*"))
  317. (set-fontset-font "fontset-default"
  318. 'katakana-jisx0201
  319. (cons (nth 2 list) "jisx0201.*")))
  320. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  321. ;; mode-line
  322. (setq eol-mnemonic-dos "CL")
  323. (setq eol-mnemonic-mac "CR")
  324. (setq eol-mnemonic-unix "LF")
  325. (which-function-mode 0)
  326. (line-number-mode 0)
  327. (column-number-mode 0)
  328. (size-indication-mode 0)
  329. (setq mode-line-position
  330. '(:eval (format "L%%l/%d,C%%c"
  331. (count-lines (point-max)
  332. (point-min)))))
  333. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  334. ;; display date
  335. (add-hook 'after-init-hook
  336. (lambda ()
  337. (setq display-time-string-forms
  338. '(dayname ", " day " " monthname " " year " " 24-hours ":"minutes ":" seconds))
  339. (setq display-time-string-forms
  340. '((my-format-time-string)))
  341. (when display-time-mode
  342. (display-time-update))
  343. ))
  344. (setq display-time-interval 29)
  345. (setq display-time-day-and-date t)
  346. (if window-system
  347. (display-time-mode 0)
  348. (display-time-mode 1))
  349. ;; ;; current directory
  350. ;; (let ((ls (member 'mode-line-buffer-identification
  351. ;; mode-line-format)))
  352. ;; (setcdr ls
  353. ;; (cons '(:eval (concat " ("
  354. ;; (abbreviate-file-name default-directory)
  355. ;; ")"))
  356. ;; (cdr ls))))
  357. ;; ;; display last modified time
  358. ;; (let ((ls (member 'mode-line-buffer-identification
  359. ;; mode-line-format)))
  360. ;; (setcdr ls
  361. ;; (cons '(:eval (concat " "
  362. ;; my-buffer-file-last-modified-time))
  363. ;; (cdr ls))))
  364. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  365. ;; file handling
  366. ;; カーソルの場所を保存する
  367. (when (require 'saveplace nil t)
  368. (setq-default save-place t))
  369. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  370. (setq make-backup-files t)
  371. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  372. (setq backup-directory-alist
  373. (cons (cons "\\.*$" (expand-file-name "~/.emacs.d/backup"))
  374. backup-directory-alist))
  375. (setq version-control 'never)
  376. (setq delete-old-versions t)
  377. (setq auto-save-list-file-prefix (expand-file-name "~/.emacs.d/autosave/"))
  378. (setq delete-auto-save-files t)
  379. (setq delete-by-moving-to-trash t
  380. trash-directory "~/.emacs.d/trash")
  381. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  382. ;; gmail
  383. (setq mail-interactive t
  384. send-mail-function 'smtpmail-send-it
  385. ;; message-send-mail-function 'smtpmail-send-it
  386. smtpmail-smtp-server "smtp.gmail.com"
  387. smtpmail-smtp-service 587
  388. smtpmail-starttls-credentials '(("smtp.gmail.com" 587 "8.slashes@gmail.com" nil))
  389. smtpmail-auth-credentials '(("smtp.gmail.com" 587 "8.slashes@gmail.com" nil))
  390. user-mail-address "8.slashes@gmail.com")
  391. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  392. ;; buffer killing
  393. (defun kill-buffer-by-major-mode (mode &optional exclude-current-buffer-p) ;mapcarとかつかって全部書き換える
  394. "kill buffers.
  395. if EXCLUDE-CURRENT-BUFFER-P is non-nil, never kill current buffer"
  396. (interactive "xmajor mode of buffer to kill: ")
  397. (save-excursion
  398. (let ((bflist (buffer-list))
  399. (cbf (current-buffer))
  400. bf)
  401. (while bflist
  402. (setq bf (pop bflist))
  403. (set-buffer bf)
  404. (if (and (eq mode major-mode) ;メジャーモードが一致し、かつ
  405. (not (and exclude-current-buffer-p ;今のバッファを除外、今のバッファと一致 がともには満たされない
  406. (eq bf cbf))))
  407. (kill-buffer bf))))))
  408. (defun my-kill-this-buffer-when-hide (&optional buffer all-frames)
  409. ""
  410. (interactive)
  411. (let ((bf (or buffer
  412. (current-buffer))))
  413. (if (or (not buffer) (get-buffer-window bf all-frames))
  414. (run-with-timer 3 nil 'my-kill-this-buffer-when-hide bf all-frames)
  415. (kill-buffer bf))))
  416. ;; (add-hook 'dired-mode-hook
  417. ;; 'my-kill-this-buffer-when-hide)
  418. (defvar my-kill-previous-buffer nil)
  419. (defun my-kill-previous-buffer ()
  420. ""
  421. (when my-kill-previous-buffer
  422. (kill-buffer my-kill-previous-buffer))
  423. (setq my-kill-previous-buffer (current-buffer)))
  424. ;; (add-hook 'dired-mode-hook
  425. ;; 'my-kill-previous-buffer)
  426. (defun my-query-kill-this-buffer ()
  427. ""
  428. (interactive)
  429. (if (y-or-n-p (concat "kill this buffer? :"))
  430. (kill-buffer (current-buffer))))
  431. (substitute-key-definition 'kill-buffer 'my-query-kill-this-buffer global-map)
  432. ;;(global-set-key "\C-xk" 'my-query-kill-this-buffer)
  433. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  434. ;; for emacsclient
  435. ;; (if window-system (server-start))
  436. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  437. ;; global keys
  438. (define-key my-prefix-map (kbd "C-f") 'make-frame-command)
  439. (define-key my-prefix-map (kbd "C-o") 'occur)
  440. (define-key my-prefix-map (kbd "C-s") 'my-execute-terminal)
  441. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  442. (global-set-key (kbd "C-\\") help-map)
  443. (global-set-key (kbd "C-x DEL") help-map)
  444. (global-set-key (kbd "C-x C-h") help-map)
  445. (define-key help-map "a" 'apropos)
  446. ;; compose window
  447. (global-set-key [?\C--] 'other-window)
  448. (global-set-key [?\C-0] 'delete-window)
  449. (global-set-key [?\C-1] 'delete-other-windows)
  450. (global-set-key [?\C-2] 'split-window-vertically)
  451. (global-set-key [?\C-3] 'split-window-horizontally)
  452. ;; disable annoying keys
  453. (global-set-key [prior] 'ignore)
  454. (global-set-key (kbd "<next>") 'ignore)
  455. (global-set-key [menu] 'ignore)
  456. (global-set-key [down-mouse-1] 'ignore)
  457. (global-set-key [down-mouse-2] 'ignore)
  458. (global-set-key [down-mouse-3] 'ignore)
  459. (global-set-key [mouse-1] 'ignore)
  460. (global-set-key [mouse-2] 'ignore)
  461. (global-set-key [mouse-3] 'ignore)
  462. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  463. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  464. (mouse-avoidance-mode 'banish)
  465. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  466. ;; download library from web
  467. ;; (require 'url)
  468. ;; (defun dllib-if-needed (lib url &optional callback bite-compile-p force-download-p) ; dont use it
  469. ;; "if LIB does not exist, download it from URL and rename to \"~/emacs.d/lisp/LIB.el\".
  470. ;; after download LIB successfully call CALLBACK. if LIB already exist, call CALLBACK immediately."
  471. ;; (let* ((dir (expand-file-name "~/.emacs.d/lisp/"))
  472. ;; (lpath (concat dir lib ".el")))
  473. ;; (and (if (or force-download-p (not (locate-library lib)))
  474. ;; (condition-case nil
  475. ;; (progn (url-copy-file url
  476. ;; lpath
  477. ;; t)
  478. ;; (and bite-compile-p
  479. ;; (byte-compile-file lpath)
  480. ;; t))
  481. ;; (error (message "downloading %s...something wrong happened!" url)
  482. ;; nil))
  483. ;; t)
  484. ;; callback
  485. ;; (funcall callback))))
  486. (defun dllib-if-unfound (lib url &optional bite-compile-p force-download-p) ; new version
  487. "if LIB does not exist, download it from URL and locate it to \"~/emacs.d/lisp/LIB.el\".
  488. return nil if LIB unfound and downloading failed, otherwise the path of LIB."
  489. (let* ((dir (expand-file-name "~/.emacs.d/lisp/"))
  490. (lpath (concat dir lib ".el"))
  491. (locate-p (locate-library lib)))
  492. (if (or force-download-p (not locate-p))
  493. (progn (condition-case nil
  494. (progn (message "downloading %s..." url)
  495. (url-copy-file url
  496. lpath
  497. t)
  498. (when bite-compile-p
  499. (byte-compile-file lpath)))
  500. (error (message "downloading %s...something wrong happened!" url)
  501. nil))
  502. (locate-library lib))
  503. locate-p)))
  504. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  505. ;;requireが必要なelispおよびhook
  506. (require 'simple nil t)
  507. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  508. ;; share clipboard with x
  509. (when (and window-system
  510. ;; (getenv "DESKTOP_SESSION")
  511. (not (eq window-system 'mac))
  512. )
  513. (setq x-select-enable-clipboard t ; these settings seems to be useless when using emacs in terminal
  514. x-select-enable-primary nil)
  515. (global-set-key "\C-y" 'x-clipboard-yank))
  516. ;; urlに細かい説明あり。でも設定は上記だけでよさそう
  517. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  518. (and (not x-select-enable-clipboard)
  519. (executable-find "xclip")
  520. (dllib-if-unfound "xclip" "http://www.emacswiki.org/emacs/download/xclip.el" t)
  521. (require 'xclip nil t)
  522. (turn-on-xclip))
  523. ;; その他のhook
  524. (add-hook 'after-save-hook
  525. 'executable-make-buffer-file-executable-if-script-p)
  526. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  527. ;; mode関連
  528. (add-hook 'verilog-mode-hook
  529. (lambda ()
  530. (define-key verilog-mode-map ";" 'self-insert-command)))
  531. (setq diff-switches "-u")
  532. (add-hook 'diff-mode-hook
  533. (lambda ()
  534. (view-mode 1)
  535. (set-face-foreground 'diff-index-face "blue")
  536. (set-face-foreground 'diff-hunk-header-face "magenda")
  537. (set-face-foreground 'diff-removed-face "red")
  538. (set-face-foreground 'diff-added-face "blue")
  539. (set-face-foreground 'diff-changed-face "syan")
  540. ))
  541. ;; (ffap-bindings)
  542. (add-hook 'sh-mode-hook
  543. (lambda ()
  544. (define-key sh-mode-map (kbd "C-x C-e") 'my-execute-shell-command-current-line)))
  545. (defun my-execute-shell-command-current-line ()
  546. ""
  547. (interactive)
  548. (shell-command (buffer-substring-no-properties (point-at-bol)
  549. (point))))
  550. (add-hook 'inferior-python-mode-hook
  551. (lambda ()
  552. (define-key inferior-python-mode-map (kbd "<up>") 'comint-previous-input)
  553. (define-key inferior-python-mode-map (kbd "<down>") 'comint-next-input)))
  554. (add-to-list 'Info-default-directory-list (expand-file-name "~/.info/emacs-ja"))
  555. (setq bookmark-default-file "~/.emacs.d/bmk")
  556. (add-hook 'apropos-mode-hook
  557. (lambda ()
  558. (define-key apropos-mode-map "j" 'next-line)
  559. (define-key apropos-mode-map "k" 'previous-line)))
  560. (define-key minibuffer-local-map (kbd "C-u") (lambda () (interactive) (delete-region (point-at-bol) (point-at-eol))))
  561. (add-hook 'isearch-mode-hook
  562. (lambda ()
  563. ;; (define-key isearch-mode-map (kbd "C-j") 'isearch-other-control-char)
  564. ;; (define-key isearch-mode-map (kbd "C-k") 'isearch-other-control-char)
  565. ;; (define-key isearch-mode-map (kbd "C-h") 'isearch-other-control-char)
  566. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  567. (define-key isearch-mode-map (kbd "M-r") 'isearch-query-replace-regexp)))
  568. (add-hook 'outline-mode-hook
  569. (lambda ()
  570. (if (string-match "\\.md$" buffer-file-name)
  571. (set (make-local-variable 'outline-regexp) "#+ "))))
  572. (add-to-list 'auto-mode-alist (cons "\\.ol$" 'outline-mode))
  573. (add-to-list 'auto-mode-alist (cons "\\.md$" 'outline-mode))
  574. (when (dllib-if-unfound "markdown-mode"
  575. "http://jblevins.org/projects/markdown-mode/markdown-mode.el"
  576. t)
  577. (add-to-list 'auto-mode-alist (cons "\\.md$" 'markdown-mode))
  578. (autoload 'markdown-mode "markdown-mode" "Major mode for editing Markdown files." nil)
  579. (add-hook 'markdown-mode-hook
  580. (lambda ()
  581. (outline-minor-mode 1)
  582. (set (make-local-variable 'comment-start) ";"))))
  583. ;; (add-hook 'c-mode-hook
  584. ;; (lambda ()
  585. ;; (set (make-local-variable 'comment-start) "//")
  586. ;; (set (make-local-variable 'comment-end) "")))
  587. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  588. ;; c-mode
  589. ;; (setq c-default-style "bsd")
  590. ;; BackSpace キーを「賢く」し,インデント幅は2桁,タブはスペースに展開
  591. (add-hook 'c-mode-common-hook
  592. (lambda ()
  593. (setq c-basic-offset 2
  594. indent-tabs-mode nil)
  595. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  596. (c-toggle-hungry-state 1)
  597. ))
  598. (defun my-compile-c-this-file ()
  599. ""
  600. (interactive)
  601. (compile (format "gcc -Wall -g -o %s %s"
  602. (file-name-sans-extension buffer-file-name)
  603. buffer-file-name)))
  604. ;; (when (require 'c nil t)(c-toggle-hungry-state t)
  605. (when (dllib-if-unfound "js2-mode"
  606. "https://github.com/mooz/js2-mode/raw/master/js2-mode.el"
  607. t)
  608. (autoload 'js2-mode "js2-mode" nil t)
  609. (add-to-list 'auto-mode-alist '("\\.js$" . js2-mode)))
  610. ;; (add-hook 'js2-mode-hook
  611. ;; (lambda ()
  612. ;; (add-hook 'before-save-hook
  613. ;; 'my-indent-buffer
  614. ;; nil
  615. ;; t)))
  616. (add-hook 'js2-mode-hook
  617. (lambda ()
  618. (define-key js2-mode-map (kbd "C-m") (lambda ()
  619. (interactive)
  620. (js2-enter-key)
  621. (indent-for-tab-command)))
  622. (add-hook (kill-local-variable 'before-save-hook)
  623. 'js2-before-save)))
  624. (and nil
  625. (require 'zone nil t)
  626. (not (eq system-type 'windows-nt))
  627. ;; (zone-when-idle 180)
  628. (run-with-idle-timer 180 t (lambda ()
  629. (unless (memq major-mode
  630. '(term-mode))
  631. (zone)))))
  632. (when (require 'uniquify nil t)
  633. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  634. (setq uniquify-ignore-buffers-re "*[^*]+*")
  635. (setq uniquify-min-dir-content 1))
  636. (add-hook 'view-mode-hook
  637. (lambda()
  638. (define-key view-mode-map "j" (lambda() (interactive) (scroll-up 1)))
  639. (define-key view-mode-map "k" (lambda() (interactive) (scroll-down 1)))
  640. (define-key view-mode-map "/" 'isearch-forward)
  641. (define-key view-mode-map "v" 'toggle-read-only)
  642. (define-key view-mode-map "q" 'bury-buffer)))
  643. (global-set-key "\M-r" 'view-mode)
  644. (setq view-read-only t)
  645. ;; (add-hook 'find-file-hook
  646. ;; (lambda ()
  647. ;; (when buffer-read-only
  648. ;; (view-mode 1))))
  649. (add-hook 'Man-mode-hook
  650. (lambda ()
  651. (view-mode 1)
  652. (setq truncate-lines nil)))
  653. (setq Man-notify-method (if window-system
  654. 'newframe
  655. 'pushy))
  656. ;; (when (and (executable-find "git")
  657. ;; (require 'sgit-mode nil t))
  658. ;; (add-hook 'find-file-hook
  659. ;; 'sgit-load))
  660. (require 'session nil t)
  661. (when (require 'gtkbm nil t)
  662. (global-set-key (kbd "C-x C-d") 'gtkbm))
  663. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  664. ;; frame buffer
  665. ;; (add-hook 'after-make-frame-functions
  666. ;; (lambda (frame)
  667. ;; (recentf-open-files)))
  668. ;; (defvar aaa nil)
  669. ;; (plist-get aaa 'abc)
  670. ;; (setq aaa (plist-put aaa 'abc 'efg))
  671. (defvar my-frame-buffer-plist nil)
  672. ;; (setplist my-frame-buffer-plist nil)
  673. (defun my-frame-buffer-add ()
  674. ""
  675. (setq my-frame-buffer-plist
  676. (plist-put my-frame-buffer-plist
  677. (selected-frame)
  678. (let ((lst (my-frame-buffer-get)))
  679. (if lst
  680. (add-to-list 'lst
  681. (current-buffer))
  682. (list (current-buffer)))))))
  683. (defun my-frame-buffer-remove ()
  684. ""
  685. (setq my-frame-buffer-plist
  686. (plist-put my-frame-buffer-plist
  687. (selected-frame)
  688. (delq (current-buffer)
  689. (my-frame-buffer-get)))))
  690. (defun my-frame-buffer-get (&optional frame)
  691. ""
  692. (plist-get my-frame-buffer-plist
  693. (or frame
  694. (selected-frame))))
  695. (defun my-frame-buffer-get2 (&optional frame)
  696. ""
  697. (delq nil (mapcar (lambda (buf)
  698. (if (or t (buffer-file-name buf))
  699. buf
  700. nil))
  701. (cdr (assq 'buffer-list
  702. (frame-parameters (or frame
  703. (selected-frame))))))))
  704. (my-frame-buffer-get2)
  705. (defun my-frame-buffer-kill-all-buffer (frame)
  706. ""
  707. (mapcar 'kill-buffer
  708. (my-frame-buffer-get frame)))
  709. (add-hook 'find-file-hook
  710. 'my-frame-buffer-add)
  711. ;; (add-hook 'dired-mode-hook
  712. ;; 'my-frame-buffer-add)
  713. (add-hook 'kill-buffer-hook
  714. 'my-frame-buffer-remove)
  715. (add-hook 'delete-frame-functions
  716. 'my-frame-buffer-kill-all-buffer)
  717. (frame-parameters (selected-frame))
  718. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  719. ;; term mode
  720. ;; (setq multi-term-program shell-file-name)
  721. (and (dllib-if-unfound "multi-term"
  722. "http://www.emacswiki.org/emacs/download/multi-term.el"
  723. t)
  724. (require 'multi-term nil t)
  725. (setq multi-term-switch-after-close nil))
  726. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  727. ;; (setq term-ansi-default-program shell-file-name)
  728. (add-hook 'term-setup-hook (lambda ()
  729. (setq term-display-table (make-display-table))))
  730. (add-hook 'term-mode-hook (lambda ()
  731. (unless (memq (current-buffer) (and (featurep 'multi-term) ; current buffer is not multi-term buffer
  732. (multi-term-list)))
  733. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  734. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  735. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  736. ;; (define-key term-raw-map "\C-f" 'forward-char)
  737. ;; (define-key term-raw-map "\C-b" 'backward-char)
  738. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  739. (define-key term-raw-map "\C-x" (lookup-key (current-global-map) "\C-x"))
  740. (define-key term-raw-map "\C-z" (lookup-key (current-global-map) "\C-z")))
  741. (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  742. (define-key term-raw-map [delete] 'term-send-raw)
  743. (define-key term-raw-map "\C-h" 'term-send-backspace)
  744. (define-key term-raw-map "\C-y" 'term-paste)
  745. (define-key term-raw-map "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  746. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  747. ;; (define-key term-raw-map (kbd key) 'term-send-raw))
  748. ;; (define-key term-raw-map "\C-d" 'delete-char)
  749. (set (make-variable-buffer-local 'scroll-margin) 0)
  750. ;; (set (make-variable-buffer-local 'cua-enable-cua-keys) nil)
  751. ;; (cua-mode 0)
  752. ;; (and cua-mode
  753. ;; (local-unset-key (kbd "C-c")))
  754. ;; (define-key cua--prefix-override-keymap "\C-c" 'term-interrupt-subjob)
  755. ))
  756. ;; (add-hook 'term-exec-hook 'forward-char)
  757. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  758. ;; buffer switching
  759. (when (require 'bs nil t)
  760. ;; (global-set-key "\C-x\C-b" 'bs-show)
  761. (defalias 'list-buffers 'bs-show))
  762. ;; (add-to-list 'bs-configurations '("processes" nil get-buffer-process ".*" nil nil))
  763. (add-to-list 'bs-configurations '("same-dir" nil buffer-same-dir-p ".*" nil nil))
  764. (add-to-list 'bs-configurations '("this-frame" nil (lambda (buf) (memq buf (my-frame-buffer-get))) ".*" nil nil))
  765. (add-to-list 'bs-configurations '("this-frame2" nil (lambda (buf) (memq buf (my-frame-buffer-get2))) ".*" nil nil))
  766. ;; (setq bs-configurations (list '("processes" nil get-buffer-process ".*" nil nil)
  767. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil bs-visits-non-file bs-sort-buffer-interns-are-last)))
  768. (setq bs-default-configuration "this-frame")
  769. (setq bs-default-sort-name "by name")
  770. (add-hook 'bs-mode-hook
  771. (lambda ()
  772. (setq bs-default-configuration "this-frame")
  773. (set (make-variable-buffer-local 'scroll-margin) 0)
  774. ))
  775. (defun buffer-same-dir-p (bf)
  776. "return t if BF's dir is same as current dir, otherwise nil."
  777. (let ((cdir (expand-file-name default-directory)))
  778. (with-current-buffer bf
  779. (equal (expand-file-name default-directory) cdir))))
  780. (defun echo-buffer-list (&optional blist)
  781. "echo buffer list as string. BLIST is list with buffer objects as elements.
  782. if arg is omitted use value of `buffer-list'."
  783. (interactive)
  784. (message (or (mapconcat (lambda (bf)
  785. (concat (buffer-name bf)
  786. "\t"
  787. (with-current-buffer bf
  788. (symbol-name major-mode))
  789. "\t"
  790. (abbreviate-file-name (buffer-file-name bf))))
  791. (or blist
  792. (buffer-list))
  793. "\n")
  794. "")))
  795. (defun my-buffer-list ()
  796. "return buffer list."
  797. (delq nil
  798. (mapcar (lambda (bf)
  799. (with-current-buffer bf
  800. (and buffer-file-name
  801. bf)))
  802. (buffer-list (selected-frame)))))
  803. (defvar buffer-switch-list-function 'my-buffer-list)
  804. (defun switch-to-previous-buffer-cycle (&optional silent-p)
  805. ""
  806. (interactive)
  807. (let ((bl (funcall buffer-switch-list-function)))
  808. (when bl
  809. (bury-buffer (car bl))
  810. (switch-to-buffer (or (nth 1 bl)
  811. (car bl)))
  812. (or silent-p
  813. (echo-buffer-list (funcall buffer-switch-list-function))))))
  814. (defun switch-to-next-buffer-cycle (&optional silent-p)
  815. ""
  816. (interactive)
  817. (let* ((bl (funcall buffer-switch-list-function))
  818. (bf (nth (- (length bl)
  819. 1)
  820. bl)))
  821. (when bl
  822. (switch-to-buffer bf)
  823. (or silent-p
  824. (echo-buffer-list (funcall buffer-switch-list-function))))))
  825. (iswitchb-mode 1)
  826. (defun iswitchb-buffer-display-other-window ()
  827. ""
  828. (interactive)
  829. (let ((iswitchb-default-method 'display))
  830. (call-interactively 'iswitchb-buffer)))
  831. (defun switch-to-other-buffer ()
  832. ""
  833. (interactive)
  834. (let ((buffer-switch-list-function 'buffer-list))
  835. (switch-to-previous-buffer-cycle t)))
  836. (global-set-key (kbd "C-.") 'switch-to-previous-buffer-cycle)
  837. (global-set-key (kbd "C-,") 'switch-to-next-buffer-cycle)
  838. ;; (global-set-key (kbd "C-\\") 'switch-to-other-buffer)
  839. ;; (global-set-key (kbd "C-\\") 'bury-buffer)
  840. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  841. ;; sdic
  842. (defun sdic-describe-word-at-point-echo ()
  843. ""
  844. (interactive)
  845. (save-window-excursion
  846. (sdic-describe-word-at-point))
  847. (save-excursion
  848. (set-buffer sdic-buffer-name)
  849. (message (buffer-substring (point-min)
  850. (progn (goto-char (point-min))
  851. (or (and (re-search-forward "^\\w" nil t 4)
  852. (progn (previous-line) t)
  853. (point-at-eol))
  854. (point-max)))))))
  855. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  856. (setq sdic-waei-dictionary-list '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  857. (setq sdic-disable-select-window t)
  858. (setq sdic-window-height 7)
  859. (when (require 'sdic nil t)
  860. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  861. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo))
  862. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  863. ;; vc
  864. ;; (require 'vc)
  865. (setq vc-handled-backends nil)
  866. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  867. ;; gauche-mode
  868. (setq scheme-program-name "gosh")
  869. (defun run-gauche-other-window ()
  870. "Run gauche on other window"
  871. (interactive)
  872. (switch-to-buffer-other-window
  873. (get-buffer-create "*scheme*"))
  874. (run-gauche))
  875. (defun run-gauche ()
  876. "run gauche"
  877. (run-scheme "gosh"))
  878. (defun scheme-send-buffer ()
  879. ""
  880. (interactive)
  881. (scheme-send-region (point-min) (point-max)))
  882. (add-hook 'scheme-mode-hook
  883. (lambda ()
  884. (define-key scheme-mode-map "\C-c\C-b" 'scheme-send-buffer)))
  885. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  886. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  887. (when (dllib-if-unfound "gauche-mode"
  888. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  889. t)
  890. (setq auto-mode-alist
  891. (cons '("\.scm$" . gauche-mode) auto-mode-alist))
  892. (autoload 'gauche-mode "gauche-mode" "Major mode for Scheme." t)
  893. (autoload 'run-scheme "gauche-mode" "Run an inferior Scheme process." t)
  894. (add-hook 'gauche-mode-hook
  895. (lambda ()
  896. (define-key scheme-mode-map "\C-c\C-z" 'run-gauche-other-window))))
  897. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  898. ;; recentf-mode
  899. (add-hook 'recentf-dialog-mode-hook
  900. 'my-recentf-abbrev-list)
  901. (defun my-recentf-delete-entry ()
  902. ""
  903. (interactive)
  904. (let ((p (point)))
  905. (setq recentf-list
  906. (delete (my-recentf-get-filename) recentf-list))
  907. (recentf-open-files)
  908. (goto-char p)))
  909. (defun my-recentf-abbrev-list ()
  910. ""
  911. (setq recentf-list
  912. (mapcar 'abbreviate-file-name
  913. recentf-list)))
  914. (defun my-recentf-view-file ()
  915. ""
  916. (interactive)
  917. (view-file (my-recentf-get-filename)))
  918. (defun my-recentf-dired ()
  919. ""
  920. (interactive)
  921. (let ((file (my-recentf-get-filename)))
  922. (if (file-directory-p file)
  923. (dired file)
  924. (dired (or (file-name-directory file)
  925. ".")))))
  926. (defun my-recentf-x-open ()
  927. ""
  928. (interactive)
  929. (my-x-open (my-recentf-get-filename)))
  930. (defun my-recentf-cd-and-find-file ()
  931. ""
  932. (interactive)
  933. (cd (file-name-directory (my-recentf-get-filename)))
  934. (call-interactively 'find-file))
  935. (defun my-recentf-get-filename ()
  936. "get file name in recentf-mode"
  937. (replace-regexp-in-string " \\(\\[.+?\\] \\)?" ; " " or " [\\d] "
  938. ""
  939. (buffer-substring-no-properties (point-at-bol)
  940. (point-at-eol))))
  941. (setq recentf-save-file (expand-file-name "~/.emacs.d/.recentf")
  942. recentf-max-menu-items 20
  943. recentf-max-saved-items 30
  944. recentf-show-file-shortcuts-flag nil)
  945. (when (require 'recentf nil t)
  946. (global-set-key "\C-x\C-r" 'recentf-open-files)
  947. ;; (add-hook 'find-file-hook
  948. ;; (lambda ()
  949. ;; (recentf-add-file default-directory)))
  950. (recentf-mode 1)
  951. (add-to-list 'recentf-filename-handlers 'abbreviate-file-name)
  952. (add-to-list 'recentf-exclude "\\.emacs\\.d/\\.recentf"))
  953. (add-hook 'recentf-dialog-mode-hook
  954. (lambda ()
  955. (recentf-save-list)
  956. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f") 'my-recentf-cd-and-find-file)
  957. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  958. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  959. (define-key recentf-dialog-mode-map "o" 'my-recentf-x-open)
  960. (define-key recentf-dialog-mode-map "d" 'my-recentf-delete-entry)
  961. (define-key recentf-dialog-mode-map "@" 'my-recentf-dired)
  962. (define-key recentf-dialog-mode-map "v" 'my-recentf-view-file)
  963. (cd "~/")))
  964. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  965. ;; dired
  966. (require 'dired)
  967. (defun my-dired-diff ()
  968. ""
  969. (interactive)
  970. (let ((files (dired-get-marked-files nil nil nil t)))
  971. (if (eq (car files)
  972. t)
  973. (diff (cadr files) (dired-get-filename))
  974. (message "One files must be marked!"))))
  975. (require 'dired-aux) ;; needed to use dired-dwim-target-directory
  976. (defun my-dired-do-pack-or-unpack ()
  977. "pack or unpack files.
  978. if targetting one file and that is archive file defined in `pack-program-alist', unpack that.
  979. otherwise, pack marked files. prompt user to decide filename for archive."
  980. (interactive)
  981. (let* ((infiles (dired-get-marked-files t))
  982. (onefile (and (eq 1 ; filename if only one file targeted, otherwise nil.
  983. (length infiles))
  984. (car infiles))))
  985. (if (and onefile
  986. (my-pack-file-name-association onefile))
  987. (when (y-or-n-p (format "unpack %s? " onefile))
  988. (my-unpack onefile))
  989. (let* ((dir-default (dired-dwim-target-directory))
  990. (archive-default (my-pack-file-extension (file-name-nondirectory (car infiles))))
  991. (archive ;; (if (interactive-p)
  992. (read-file-name "Output file to pack : "
  993. dir-default
  994. nil
  995. nil
  996. archive-default)
  997. ;; (concat dir-default archive-default)
  998. ))
  999. (apply 'my-pack
  1000. archive
  1001. infiles))))
  1002. (revert-buffer)
  1003. ;; (dired-unmark-all-marks)
  1004. )
  1005. (defun my-file-name-extension-with-tar (filename)
  1006. "if FILENAME has extension with tar, like \"tar.gz\", return that.
  1007. otherwise, return extension normally."
  1008. (if (string-equal "tar" (file-name-extension (file-name-sans-extension filename)))
  1009. (concat "tar."
  1010. (file-name-extension filename))
  1011. (file-name-extension filename)))
  1012. (defun my-pack-file-extension (filename)
  1013. "if FILENAME has extension and it can be used for pack, return FILENAME.
  1014. otherwise, return FILENAME with `my-pack-default-extension'"
  1015. (if (my-pack-file-name-association filename)
  1016. filename
  1017. (concat filename "." my-pack-default-extension)))
  1018. (defvar my-7z-program-name
  1019. (or (executable-find "7z")
  1020. (executable-find "7za")
  1021. (executable-find "7zr"))
  1022. "7z program.")
  1023. (defvar my-pack-default-extension
  1024. "7z"
  1025. "default suffix for packing. filename with this suffix must matches one of `pack-program-alist'")
  1026. (defun my-pack-file-name-association (filename)
  1027. "if the pattern matching FILENAME is found at car of the list in `pack-program-alist', return cdr of that list.
  1028. otherwise, return nil."
  1029. (let ((case-fold-search nil))
  1030. (assoc-default filename
  1031. my-pack-program-alist
  1032. 'string-match-p
  1033. nil)))
  1034. (defvar my-pack-program-alist
  1035. `(
  1036. ("\\.7z\\'" ,(concat my-7z-program-name " a") ,(concat my-7z-program-name " x"))
  1037. ("\\.zip\\'" "zip -r" "unzip")
  1038. ("\\.tar\\'" "tar cf" "tar xf")
  1039. ("\\.tgz\\'" "tar czf" "tar xzf")
  1040. ("\\.tar\\.gz\\'" "tar czf" "tar xzf")
  1041. )
  1042. "Alist of filename patterns, command for pack and unpack.
  1043. Each element looks like (REGEXP PACKING-COMMAND UNPACKING-COMMAND).
  1044. PACKING-COMMAND and UNPACKING-COMMAND can be nil if the command is not available.
  1045. alist is searched from the beginning so pattern for \".tar.gz\" should be ahead of pattern for \".gz\"")
  1046. ;; (string-match-p "\\.gz\\'" "aaa.gz") ; \' matches string end, $ also matches the point before newline.
  1047. (defun my-unpack (archive)
  1048. "unpack ARCHIVE. command for unpacking is defined in `pack-program-alist'"
  1049. (interactive "fArchive to extract: ")
  1050. (let* ((earchive (expand-file-name archive))
  1051. (cmd (nth 1
  1052. (my-pack-file-name-association earchive)))
  1053. )
  1054. (if cmd
  1055. (shell-command (concat cmd
  1056. " "
  1057. (shell-quote-argument earchive)))
  1058. (message "this is not archive file defined in `pack-program-alist'!"))))
  1059. (defun my-pack (archive &rest files)
  1060. "pack FILES into ARCHIVE.
  1061. if ARCHIVE have extension defined in `pack-program-alist', use that command.
  1062. otherwise, use `pack-default-extension' for pack."
  1063. (let* ((archive-ext (my-pack-file-extension (expand-file-name archive)))
  1064. (cmd (car (my-pack-file-name-association archive-ext)))
  1065. )
  1066. (if cmd
  1067. (shell-command (concat cmd
  1068. " "
  1069. (shell-quote-argument archive-ext)
  1070. " "
  1071. (mapconcat 'shell-quote-argument
  1072. files
  1073. " ")))
  1074. (message "invalid extension for packing!"))))
  1075. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1076. "pop up buffer using `display-buffer' and return that buffer."
  1077. (let ((bf (get-buffer-create buffer-or-name)))
  1078. (with-current-buffer bf
  1079. (cd ".")
  1080. (erase-buffer))
  1081. (display-buffer bf)
  1082. bf))
  1083. (defun my-replace-nasi-none ()
  1084. ""
  1085. (save-excursion
  1086. (let ((buffer-read-only nil))
  1087. (goto-char (point-min))
  1088. (while (search-forward "なし" nil t)
  1089. (replace-match "none")))))
  1090. (defun dired-get-du () ;em-unix.el使えるかも
  1091. "dired get disk usage"
  1092. (interactive)
  1093. (message "calculating du...")
  1094. (dired-do-shell-command "du -hs * " nil (dired-get-marked-files)))
  1095. (defun my-dired-scroll-up ()
  1096. ""
  1097. (interactive)
  1098. (my-dired-previous-line (- (window-height) 1)))
  1099. (defun my-dired-scroll-down ()
  1100. ""
  1101. (interactive)
  1102. (my-dired-next-line (- (window-height) 1)))
  1103. (defun my-dired-previous-line (&optional arg)
  1104. ""
  1105. (interactive)
  1106. (dired-previous-line (or arg 1))
  1107. (my-dired-print-current-dir-and-file))
  1108. (defun my-dired-next-line (&optional arg)
  1109. ""
  1110. (interactive)
  1111. (dired-next-line (or arg 1))
  1112. (my-dired-print-current-dir-and-file))
  1113. (defun my-dired-print-current-dir-and-file ()
  1114. (message "%s %s"
  1115. default-directory
  1116. (buffer-substring-no-properties (point-at-bol)
  1117. (point-at-eol))))
  1118. (defun dired-do-execute-as-command ()
  1119. ""
  1120. (interactive)
  1121. (let ((file (dired-get-filename t)))
  1122. (if (file-executable-p file)
  1123. (start-process file nil file)
  1124. (when (y-or-n-p "this file cant be executed. mark as executable and go? : ")
  1125. (set-file-modes file (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1126. (start-process file nil file)))))
  1127. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1128. (defun my-dired-x-open ()
  1129. ""
  1130. (interactive)
  1131. (my-x-open (dired-get-filename t t)))
  1132. (defun my-dired-up-directory ()
  1133. ""
  1134. (interactive)
  1135. (my-dired-find-file ".."))
  1136. (defun my-dired-find-file (&optional filename)
  1137. "if the file to open is a directory, kill current buffer after opening that file."
  1138. (interactive)
  1139. (let ((f (expand-file-name (or filename
  1140. (dired-get-filename))))
  1141. (bf (current-buffer)))
  1142. (find-file f)
  1143. (when (and (file-directory-p f)
  1144. (not (get-buffer-window bf)))
  1145. (kill-buffer bf))))
  1146. (if (eq window-system 'mac)
  1147. (setq dired-listing-switches "-lhFG")
  1148. (setq dired-listing-switches "-lhFG --time-style=long-iso")
  1149. )
  1150. (define-minor-mode my-dired-display-all-mode
  1151. ""
  1152. :init-value nil
  1153. :global t
  1154. (my-dired-display-all-set)
  1155. (when (eq major-mode 'dired-mode)
  1156. (revert-buffer)))
  1157. (defun my-dired-display-all-set ()
  1158. ""
  1159. (if my-dired-display-all-mode
  1160. (setq dired-actual-switches
  1161. (concat "-A "
  1162. dired-actual-switches))
  1163. (setq dired-actual-switches
  1164. (replace-regexp-in-string "-A " "" dired-actual-switches))))
  1165. (add-hook 'dired-mode-hook
  1166. 'my-dired-display-all-set)
  1167. (put 'dired-find-alternate-file 'disabled nil)
  1168. (require 'ls-lisp)
  1169. ;; (setq ls-lisp-use-insert-directory-program nil)
  1170. (setq ls-lisp-dirs-first t)
  1171. (setq dired-ls-F-marks-symlinks t)
  1172. (setq dired-dwim-target t)
  1173. ;; (add-hook 'dired-after-readin-hook
  1174. ;; 'my-replace-nasi-none)
  1175. ;; (add-hook 'after-init-hook
  1176. ;; (lambda ()
  1177. ;; (dired ".")))
  1178. (add-hook 'dired-mode-hook
  1179. (lambda ()
  1180. (define-key dired-mode-map "o" 'my-dired-x-open)
  1181. (define-key dired-mode-map "i" 'dired-get-du)
  1182. (define-key dired-mode-map "!" 'shell-command)
  1183. (define-key dired-mode-map "&" 'async-shell-command)
  1184. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1185. (define-key dired-mode-map "=" 'my-dired-diff)
  1186. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1187. (define-key dired-mode-map "b" 'gtkbm)
  1188. (define-key dired-mode-map "@" (lambda () (interactive) (my-x-open ".")))
  1189. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1190. (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1191. (define-key dired-mode-map "a" 'my-dired-display-all-mode)
  1192. (define-key dired-mode-map "h" 'my-dired-display-all-mode)
  1193. (define-key dired-mode-map "/" 'isearch-forward)
  1194. (substitute-key-definition 'dired-advertised-find-file 'my-dired-find-file dired-mode-map)
  1195. (substitute-key-definition 'dired-up-directory 'my-dired-up-directory dired-mode-map)
  1196. (define-key dired-mode-map (kbd "DEL") 'my-dired-up-directory)
  1197. (substitute-key-definition 'dired-next-line 'my-dired-next-line dired-mode-map)
  1198. (substitute-key-definition 'dired-previous-line 'my-dired-previous-line dired-mode-map)
  1199. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1200. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1201. (let ((file "._Icon\015"))
  1202. (when (file-readable-p file)
  1203. (delete-file file)))))
  1204. ;; http://homepage1.nifty.com/blankspace/emacs/dired.html
  1205. ;; (add-hook 'dired-load-hook
  1206. ;; (lambda ()
  1207. ;; (load-library "ls-lisp")
  1208. ;; (setq ls-lisp-dirs-first t)
  1209. ;; (setq dired-listing-switches "-alhF"))) ;これ書く場所間違えてね?
  1210. ;; (defadvice dired-next-line (after dired-next-line-print-directory activate)
  1211. ;; "print current directory when go down line"
  1212. ;; (dired-print-current-dir-and-file))
  1213. ;; (defadvice dired-previous-line (after dired-previous-line-print-directory activate)
  1214. ;; "print current directory when go up line"
  1215. ;; (dired-print-current-dir-and-file))
  1216. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1217. (defun my-dired-toggle-mark()
  1218. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1219. (t dired-marker-char))))
  1220. (delete-char 1)
  1221. (insert cur)))
  1222. (defun my-dired-mark (arg)
  1223. "toggle mark the current (or next ARG) files.
  1224. If on a subdir headerline, mark all its files except `.' and `..'.
  1225. Use \\[dired-unmark-all-files] to remove all marks
  1226. and \\[dired-unmark] on a subdir to remove the marks in
  1227. this subdir."
  1228. (interactive "P")
  1229. (if (dired-get-subdir)
  1230. (save-excursion (dired-mark-subdir-files))
  1231. (let ((inhibit-read-only t))
  1232. (dired-repeat-over-lines
  1233. (prefix-numeric-value arg)
  1234. 'my-dired-toggle-mark))))
  1235. (defun my-dired-mark-backward (arg)
  1236. "In Dired, move up lines and toggle mark there.
  1237. Optional prefix ARG says how many lines to unflag; default is one line."
  1238. (interactive "p")
  1239. (my-dired-mark (- arg)))
  1240. (defun dired-mode-hooks()
  1241. (local-set-key (kbd "SPC") 'my-dired-mark)
  1242. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1243. (add-hook 'dired-mode-hook 'dired-mode-hooks)
  1244. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1245. ;; eshell
  1246. (defun my-eshell-backward-delete-char ()
  1247. (interactive)
  1248. (when (< (save-excursion
  1249. (eshell-bol)
  1250. (point))
  1251. (point))
  1252. (backward-delete-char 1)))
  1253. (defvar my-eshell-frame-buffer-alist nil)
  1254. (defun my-eshell-frame-buffer (frame)
  1255. "get buffer associated with FRAME. if buffer doesnt exist or killed, return nil."
  1256. (let ((bf (cdr (assq frame my-eshell-frame-buffer-alist))))
  1257. (and bf ;関連付けられたバッファが存在し
  1258. (buffer-name bf) ;かつkillされてない
  1259. bf)))
  1260. (add-hook 'eshell-mode-hook
  1261. (lambda ()
  1262. (add-to-list 'my-eshell-frame-buffer-alist
  1263. (cons (selected-frame) (current-buffer)))))
  1264. (defun my-file-owner-p (file)
  1265. "t if FILE is owned by me."
  1266. (eq (user-uid) (nth 2 (file-attributes file))))
  1267. ;; ;; http://www.bookshelf.jp/pukiwiki/pukiwiki.php?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9
  1268. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1269. ;; (defun eshell/less (&rest args)
  1270. ;; "Invoke `view-file' on the file.
  1271. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1272. ;; (if args
  1273. ;; (while args
  1274. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1275. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1276. ;; (file (pop args)))
  1277. ;; (view-file file)
  1278. ;; (goto-line line))
  1279. ;; (view-file (pop args))))))
  1280. ;; (defun eshell/git (&rest args)
  1281. ;; ""
  1282. ;; (eshell-parse-arguments (point-at-bol) (point-at-eol)))
  1283. (defun eshell/o (&optional file)
  1284. (my-x-open (or file ".")))
  1285. ;; (defun eshell/vi (&rest args)
  1286. ;; "Invoke `find-file' on the file.
  1287. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1288. ;; (while args
  1289. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1290. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1291. ;; (file (pop args)))
  1292. ;; (find-file file)
  1293. ;; (goto-line line))
  1294. ;; (find-file (pop args)))))
  1295. (defun eshell/clear ()
  1296. "Clear the current buffer, leaving one prompt at the top."
  1297. (let ((inhibit-read-only t))
  1298. (erase-buffer)))
  1299. (defun eshell/d (&optional dirname switches)
  1300. "if first arg is omitted open current directory."
  1301. (dired (or dirname ".") switches))
  1302. (defun eshell/v ()
  1303. (view-mode 1))
  1304. (defalias 'eshell/: 'ignore)
  1305. (defalias 'eshell/type 'eshell/which)
  1306. ;; (defalias 'eshell/vim 'eshell/vi)
  1307. (defalias 'eshell/ff 'find-file)
  1308. (defalias 'eshell/q 'eshell/exit)
  1309. (defun eshell-goto-prompt ()
  1310. ""
  1311. (interactive)
  1312. (goto-char (point-max)))
  1313. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1314. "open eshell and change wd
  1315. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1316. (interactive)
  1317. (let ((dir (expand-file-name default-directory)))
  1318. (switch-to-buffer (or eshell-buffer-or-name
  1319. (eshell t)))
  1320. (unless (equal dir (expand-file-name default-directory))
  1321. ;; (cd dir)
  1322. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1323. ;; (eshell-emit-prompt)
  1324. (goto-char (point-max))
  1325. (eshell-kill-input)
  1326. (insert "cd " dir)
  1327. (eshell-send-input))))
  1328. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1329. (setq eshell-scroll-to-bottom-on-input t)
  1330. (setq eshell-cmpl-ignore-case t)
  1331. (setq eshell-cmpl-cycle-completions nil)
  1332. (setq eshell-highlight-prompt nil)
  1333. (setq eshell-ls-initial-args "-FG") ; "-hF")
  1334. (setq eshell-prompt-function
  1335. (lambda ()
  1336. (with-temp-buffer
  1337. (let (p1 p2 p3 p4)
  1338. (insert " [")
  1339. (setq p1 (point))
  1340. (insert (abbreviate-file-name default-directory))
  1341. (setq p2 (point))
  1342. (insert "]"
  1343. "\n")
  1344. (setq p3 (point))
  1345. (insert user-login-name
  1346. "@"
  1347. (or (getenv "HOSTNAME")
  1348. (substring (shell-command-to-string (or (executable-find "hostname")
  1349. "echo ''"))
  1350. 0
  1351. -1)))
  1352. (setq p4 (point))
  1353. (insert " "
  1354. (format-time-string "%a, %d %b %Y %T %z")
  1355. " eshell\n"
  1356. "last:"
  1357. (number-to-string eshell-last-command-status)
  1358. (if (= (user-uid)
  1359. 0)
  1360. " # "
  1361. " $ "))
  1362. (add-text-properties p1
  1363. p2
  1364. '(face ((foreground-color . "yellow"))))
  1365. (add-text-properties p3
  1366. p4
  1367. '(face ((foreground-color . "cyan"))))
  1368. (buffer-substring (point-min)
  1369. (point-max))))))
  1370. (add-hook 'eshell-mode-hook
  1371. (lambda ()
  1372. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1373. ;; (interactive)
  1374. ;; (switch-to-buffer (other-buffer))))
  1375. (define-key eshell-mode-map (kbd "C-u") (lambda ()
  1376. (interactive)
  1377. (eshell-goto-prompt)
  1378. (eshell-kill-input)))
  1379. (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1380. (interactive)
  1381. (eshell-goto-prompt)
  1382. (my-keyboard-quit)))
  1383. (define-key eshell-mode-map (kbd "DEL") 'my-eshell-backward-delete-char)
  1384. (define-key eshell-mode-map (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1385. (define-key eshell-mode-map (kbd "C-n") 'eshell-next-matching-input-from-input)
  1386. (mapcar (lambda (alias)
  1387. (add-to-list 'eshell-command-aliases-list
  1388. alias))
  1389. '(("ll" "ls -l")
  1390. ("la" "ls -a")
  1391. ("lla" "ls -al")
  1392. ("ut" "slogin 03110414@un001.ecc.u-tokyo.ac.jp")
  1393. ("aptin" "sudo apt-get install")
  1394. ("u" "uname")
  1395. ("eless" "cat >>> (with-current-buffer (get-buffer-create \"*eshell output\") (erase-buffer) (setq buffer-read-only nil) (current-buffer)); (view-buffer (get-buffer \"*eshell output*\"))")
  1396. ("g" "git")))
  1397. (add-to-list 'eshell-output-filter-functions 'eshell-truncate-buffer)
  1398. (apply 'eshell/addpath exec-path)
  1399. (set (make-variable-buffer-local 'scroll-margin) 0)
  1400. (eshell/export "GIT_PAGER=")
  1401. (eshell/export "GIT_EDITOR=")
  1402. (eshell/export "LC_MESSAGES=C")
  1403. (eshell/export "TERM=xterm")
  1404. ))
  1405. ;; (eval-after-load "em-alias"
  1406. ;; '(progn ;; (eshell/alias "ll" "ls -l")
  1407. ;; ;; (eshell/alias "la" "ls -a")
  1408. ;; ;; (eshell/alias "lla" "ls -al")
  1409. ;; (eshell/alias "sgcc" (if (eq system-type 'windows-nt)
  1410. ;; "gcc -o win.$1.exe $1"
  1411. ;; "gcc -o ${uname}.$1.out $1"))
  1412. ;; (eshell/alias "slmgcc" (if (eq system-type 'windows-nt)
  1413. ;; "gcc -lm -o win.$1.exe $1"
  1414. ;; "gcc -lm -o ${uname}.$1.out $1"))
  1415. ;; ;; (eshell/alias "ut" "ssh g841105@un001.ecc.u-tokyo.ac.jp")
  1416. ;; (add-to-list 'recentf-exclude (concat eshell-directory-name "alias"))))
  1417. ;; (define-key my-prefix-map (kbd "C-s") (lambda ()
  1418. ;; (interactive)
  1419. ;; (eshell-cd-default-directory (buffer-name (or (my-eshell-frame-buffer (selected-frame))
  1420. ;; (eshell t))))))
  1421. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1422. ;; 最終更新日時を得る
  1423. (defvar my-buffer-file-last-modified-time nil "")
  1424. (make-variable-buffer-local 'my-buffer-file-last-modified-time)
  1425. (defun my-get-file-last-modified-time (file)
  1426. ""
  1427. (nth 5
  1428. (file-attributes file)))
  1429. (defun my-set-buffer-file-last-modified-time ()
  1430. ""
  1431. (make-local-variable 'my-buffer-file-last-modified-time)
  1432. (setq my-buffer-file-last-modified-time
  1433. (format-time-string "%Y/%m/%d %H:%M" (my-get-file-last-modified-time buffer-file-name))))
  1434. (add-hook 'find-file-hook
  1435. 'my-set-buffer-file-last-modified-time)
  1436. (add-hook 'after-save-hook
  1437. 'my-set-buffer-file-last-modified-time)
  1438. (add-hook 'after-revert-hook
  1439. 'my-set-buffer-file-last-modified-time)
  1440. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1441. ;; auto saving
  1442. (defun my-save-this-buffer (silent-p)
  1443. "save current buffer if can without asking"
  1444. (let ((cm (if (current-message)
  1445. (format "%s\n" (current-message))
  1446. ""))
  1447. (fun (symbol-function (if silent-p
  1448. 'ignore
  1449. 'message))))
  1450. (cond ((active-minibuffer-window) nil)
  1451. ((not buffer-file-name) (funcall fun "%ssaving... this buffer doesn't visit any file." cm) nil)
  1452. ((not (file-exists-p buffer-file-name)) (funcall fun "%ssaving... file not exist. save manually first." com) nil)
  1453. (buffer-read-only (funcall fun "%ssaving... this buffer is read-only." cm) nil)
  1454. ((not (buffer-modified-p)) (funcal fun "%ssaving... not modified yet." cm) nil)
  1455. ((not (file-writable-p buffer-file-name)) (funcall fun "%ssaving... you cannot change this file." cm) nil)
  1456. (t (funcall fun "%ssaving..." cm)
  1457. (save-buffer)
  1458. (funcall fun "%ssaving... done." cm)))))
  1459. ;; (if (and buffer-file-name
  1460. ;; (not buffer-read-only)
  1461. ;; (buffer-modified-p)
  1462. ;; (file-writable-p buffer-file-name))
  1463. ;; (save-buffer))) ; 静かな方
  1464. (defvar my-auto-save-this-buffer nil "auto save timer object")
  1465. (defun my-auto-save-this-buffer (secs &optional silent-p)
  1466. "auto save current buffer if idle for SEC.
  1467. when SEC is nil, stop auto save if enabled."
  1468. (if secs
  1469. (progn (when my-auto-save-this-buffer
  1470. (cancel-timer my-auto-save-this-buffer)
  1471. (setq my-auto-save-this-buffer nil))
  1472. (setq my-auto-save-this-buffer (run-with-idle-timer secs t 'my-save-this-buffer silent-p)))
  1473. (when my-auto-save-this-buffer
  1474. (cancel-timer my-auto-save-this-buffer)
  1475. (setq my-auto-save-this-buffer nil))))
  1476. (my-auto-save-this-buffer 2 t)
  1477. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1478. ;; misc funcs
  1479. (defvar my-desktop-terminal "roxterm")
  1480. (defun my-execute-terminal ()
  1481. ""
  1482. (interactive)
  1483. (if (and (or (eq system-type 'windows-nt)
  1484. window-system)
  1485. my-desktop-terminal
  1486. )
  1487. (let ((process-environment (cons "TERM=xterm" process-environment)))
  1488. (start-process "terminal"
  1489. nil
  1490. my-desktop-terminal))
  1491. (my-execute-or-find-term)))
  1492. (defun my-term ()
  1493. "open terminal buffer and return that buffer."
  1494. (interactive)
  1495. (if (eq system-type 'windows-nt)
  1496. (eshell t)
  1497. (if (featurep 'multi-term)
  1498. (multi-term)
  1499. (ansi-term "/bin/bash"))))
  1500. (defvar my-frame-term-plist nil)
  1501. ;; (setplist my-frame-term-plist nil)
  1502. (defun my-execute-or-find-term ()
  1503. ""
  1504. (interactive)
  1505. (let* ((buf (plist-get my-frame-term-plist (selected-frame))))
  1506. (if (and buf
  1507. (buffer-name buf))
  1508. (switch-to-buffer buf)
  1509. (setq my-frame-term-plist
  1510. (plist-put my-frame-term-plist
  1511. (selected-frame)
  1512. (my-term))))))
  1513. (defun my-format-time-string (&optional time)
  1514. ""
  1515. (let ((system-time-locale "C"))
  1516. (format-time-string "%a, %d %b %Y %T" time)))
  1517. (defvar my-filer nil)
  1518. (setq my-filer (or (executable-find "pcmanfm")
  1519. (executable-find "nautilus")))
  1520. (defun my-x-open (file)
  1521. "open file."
  1522. (interactive "FOpen File: ")
  1523. (setq file (expand-file-name file))
  1524. (message "Opening %s..." file)
  1525. (cond ((eq system-type 'windows-nt)
  1526. (call-process "cmd.exe" nil 0 nil "/c" "start" "" (convert-standard-filename file)))
  1527. ((eq system-type 'darwin)
  1528. (call-process "open" nil 0 nil file))
  1529. ((not (getenv "DESKTOP_SESSION"))
  1530. (find-file file))
  1531. (t
  1532. (if (file-directory-p file)
  1533. (call-process my-filer nil 0 nil file)
  1534. (call-process "xdg-open" nil 0 nil file))))
  1535. (recentf-add-file file)
  1536. (message "Opening %s...done" file))
  1537. (defvar my-auto-indent-buffer-mode-list
  1538. '(emacs-lisp-mode
  1539. sh-mode
  1540. js-mode
  1541. sgml-mode
  1542. c-mode
  1543. c++-mode))
  1544. (setq my-auto-indent-buffer-mode-list nil)
  1545. (defun my-indent-buffer ()
  1546. "indent whole buffer."
  1547. (interactive)
  1548. (indent-region (point-min)
  1549. (point-max)))
  1550. (defun my-auto-indent-buffer ()
  1551. ""
  1552. (when (memq major-mode my-auto-indent-buffer-mode-list)
  1553. (my-indent-buffer)))
  1554. (add-hook 'before-save-hook
  1555. 'my-auto-indent-buffer)
  1556. (defun my-keyboard-quit ()
  1557. ""
  1558. (interactive)
  1559. (run-hooks 'before-keyboard-quit-hook)
  1560. ;; (redisplay t)
  1561. (redraw-display)
  1562. ;; (run-hooks 'window-configuration-change-hook)
  1563. (my-revert-buffer-if-needed)
  1564. ;; (revert-buffer t t)
  1565. (keyboard-quit)
  1566. (insert "insert me")
  1567. (run-hooks 'after-keyboard-quit-hook))
  1568. (substitute-key-definition 'keyboard-quit 'my-keyboard-quit global-map)
  1569. ;; (global-set-key (kbd "C-g") 'my-keyboard-quit)
  1570. (defun my-convmv-sjis2utf8-test ()
  1571. "run `convmv -r -f sjis -t utf8 *'
  1572. this is test, does not rename files"
  1573. (interactive)
  1574. (shell-command "convmv -r -f sjis -t utf8 *"))
  1575. (defun my-convmv-sjis2utf8-notest ()
  1576. "run `convmv -r -f sjis -t utf8 * --notest'"
  1577. (interactive)
  1578. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  1579. (defun my-copy-whole-line ()
  1580. ""
  1581. (interactive)
  1582. (kill-new (concat (buffer-substring (point-at-bol)
  1583. (point-at-eol))
  1584. "\n")))
  1585. (defun kill-ring-save-buffer-file-name ()
  1586. "get current filename"
  1587. (interactive)
  1588. (let ((file buffer-file-name))
  1589. (if file
  1590. (progn (kill-new file)
  1591. (message file))
  1592. (message "not visiting file."))))
  1593. ;; ;; コマンド的な
  1594. ;; (defvar my-execute-func-list nil "func list")
  1595. ;; (defvar my-execute-func-hist-list nil "func hist list")
  1596. ;; (setq my-execute-func-list '("(call-interactively 'my-francaiscd-b)"
  1597. ;; "(call-interactively 'my-francaiscd-a)"
  1598. ;; "parsec47"
  1599. ;; "chromium-browser"
  1600. ;; "inkscape"
  1601. ;; "audacious"
  1602. ;; "gnome-terminal"
  1603. ;; "zkaicd.py"
  1604. ;; "glchess"))
  1605. ;; (defun my-execute-start-process-or-eval-sexp ()
  1606. ;; "execute something"
  1607. ;; (interactive)
  1608. ;; (let ((func (completing-read "command?: " my-execute-func-list nil nil "" my-execute-func-hist-list)))
  1609. ;; (if (string= "(" (substring func 0 1))
  1610. ;; (with-temp-buffer (insert func)
  1611. ;; (eval-buffer))
  1612. ;; (start-process "ps"
  1613. ;; nil
  1614. ;; func))))
  1615. ;; delete-trailing-whitespace
  1616. ;; (defun my-delete-blanks-on-eol ()
  1617. ;; ""
  1618. ;; (interactive)
  1619. ;; (save-excursion
  1620. ;; (goto-char (point-min))
  1621. ;; (while (re-search-forward "[ \t]+$" nil t)
  1622. ;; (replace-match "" nil nil))))
  1623. (defun my-revert-buffer-if-needed ()
  1624. ""
  1625. (interactive)
  1626. (unless (verify-visited-file-modtime (current-buffer))
  1627. (revert-buffer t t)))
  1628. (add-hook 'window-configuration-change-hook
  1629. (lambda ()
  1630. (run-with-timer 0.5
  1631. nil
  1632. 'my-revert-buffer-if-needed)))
  1633. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1634. ;; forked from http://d.hatena.ne.jp/khiker/20100119/window_resize
  1635. (define-key my-prefix-map (kbd "C-w") 'my-window-organizer)
  1636. (defun my-window-organizer ()
  1637. "Control window size and position."
  1638. (interactive)
  1639. (save-selected-window
  1640. (select-window (window-at 0 0))
  1641. (let ( ;; (window-obj (selected-window))
  1642. ;; (current-width (window-width))
  1643. ;; (current-height (window-height))
  1644. action
  1645. c)
  1646. (catch 'end-flag
  1647. (while t
  1648. (setq action
  1649. (read-key-sequence-vector (format "size[%dx%d] 1: maximize; 2, 3: split; 0: delete; o: select other; j, l: enlarge; h, k: shrink; q: quit."
  1650. (window-width)
  1651. (window-height))))
  1652. (setq c (aref action 0))
  1653. (cond ((= c ?l)
  1654. (unless (eq (window-width) (frame-width))
  1655. (enlarge-window-horizontally 1)))
  1656. ((= c ?h)
  1657. (unless (eq (window-width) (frame-width))
  1658. (shrink-window-horizontally 1)))
  1659. ((= c ?j)
  1660. (enlarge-window 1))
  1661. ((= c ?k)
  1662. (shrink-window 1))
  1663. ((= c ?o)
  1664. (other-window 1))
  1665. ((memq c '(?d ?0))
  1666. (unless (eq (selected-window) (next-window (selected-window) 0 1))
  1667. (delete-window (selected-window))))
  1668. ((= c ?1)
  1669. (delete-other-windows))
  1670. ((= c ?2)
  1671. (split-window-vertically))
  1672. ((= c ?3)
  1673. (split-window-horizontally))
  1674. ((memq c '(?q ?\C-g))
  1675. (message "Quit")
  1676. (throw 'end-flag t))
  1677. (t
  1678. (beep))))))))
  1679. ;; (aref (read-key-sequence-vector "aa") 0)
  1680. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1681. ;; save and restore frame size
  1682. ;;http://www.bookshelf.jp/soft/meadow_30.html#SEC416
  1683. (defun my-window-size-save ()
  1684. (let* ((rlist (frame-parameters (selected-frame)))
  1685. (ilist initial-frame-alist)
  1686. (nCHeight (frame-height))
  1687. (nCWidth (frame-width))
  1688. (tMargin (if (integerp (cdr (assoc 'top rlist)))
  1689. (cdr (assoc 'top rlist)) 0))
  1690. (lMargin (if (integerp (cdr (assoc 'left rlist)))
  1691. (cdr (assoc 'left rlist)) 0))
  1692. buf
  1693. (file "~/.emacs.d/.framesize.el")
  1694. (recentf-exclude '("\\.emacs\\.d/\\.framesize\\.el$")))
  1695. (if (get-file-buffer (expand-file-name file))
  1696. (setq buf (get-file-buffer (expand-file-name file)))
  1697. (setq buf (find-file-noselect file)))
  1698. (set-buffer buf)
  1699. (erase-buffer)
  1700. (insert (concat
  1701. ;; 初期値をいじるよりも modify-frame-parameters
  1702. ;; で変えるだけの方がいい?
  1703. "(delete 'width default-frame-alist)\n"
  1704. "(delete 'height default-frame-alist)\n"
  1705. "(delete 'top default-frame-alist)\n"
  1706. "(delete 'left default-frame-alist)\n"
  1707. "(setq default-frame-alist (append (list\n"
  1708. "'(width . " (int-to-string nCWidth) ")\n"
  1709. "'(height . " (int-to-string nCHeight) ")\n"
  1710. "'(top . " (int-to-string tMargin) ")\n"
  1711. "'(left . " (int-to-string lMargin) "))\n"
  1712. "default-frame-alist))\n"
  1713. ;;"(setq default-frame-alist default-frame-alist)"
  1714. ))
  1715. (save-buffer)
  1716. ))
  1717. (defun my-window-size-load ()
  1718. (let* ((file "~/.emacs.d/.framesize.el"))
  1719. (if (file-exists-p file)
  1720. (load file))))
  1721. (when window-system
  1722. (my-window-size-load)
  1723. (add-hook 'after-init-hook ;何かがframeの大きさ勝手に変えやがる
  1724. (lambda ()
  1725. (run-with-timer 1
  1726. nil
  1727. (lambda ()
  1728. (modify-frame-parameters (selected-frame)
  1729. default-frame-alist))))
  1730. t)
  1731. ;; (add-hook 'make-frame-hook
  1732. ;; (lambda ()
  1733. ;; (run-with-timer 1
  1734. ;; nil
  1735. ;; (lambda ()
  1736. ;; (modify-frame-parameters (selected-frame)
  1737. ;; initial-frame-alist))))
  1738. ;; t)
  1739. (add-hook 'kill-emacs-hook
  1740. 'my-window-size-save))
  1741. ;; windowサイズを固定
  1742. ;; setq default-frame-alist
  1743. ;; (append (list '(width . 80)
  1744. ;; '(height . 35)
  1745. ;; )
  1746. ;; default-frame-alist)
  1747. ;; ) ;;デフォルトのフレーム設定
  1748. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1749. ;; ;; emacsを殺伐とさせる
  1750. ;; ;; 補完させるとき失敗するからなし
  1751. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  1752. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  1753. ;; (setq arg
  1754. ;; (concat arg
  1755. ;; (if (eq nil (string-match "\\. *$" arg)) ".")
  1756. ;; " And You are a Coward!")))
  1757. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1758. ;; ubuntu
  1759. (defun my-load-scim ()
  1760. "use scim-bridge.el as japanese im."
  1761. ;; Load scim-bridge.
  1762. (require 'scim-bridge)
  1763. ;; Turn on scim-mode automatically after loading .emacs
  1764. (add-hook 'after-init-hook 'scim-mode-on)
  1765. (setq scim-cursor-color "red")
  1766. (scim-define-preedit-key ?\^h t)
  1767. (scim-define-common-key ?\* nil)
  1768. (scim-define-common-key ?\^/ nil))
  1769. (defun my-load-anthy ()
  1770. "use anthy.el as japanese im."
  1771. ;; anthy
  1772. (require 'anthy)
  1773. (global-set-key [muhenkan] (lambda () (interactive) (anthy-mode-off)))
  1774. (global-set-key [henkan] (lambda () (interactive) (anthy-mode-on)))
  1775. (when (>= emacs-major-version 23)
  1776. (setq anthy-accept-timeout 1)))
  1777. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1778. ;; windows用設定
  1779. ;; (add-to-list 'exec-path "c:/Program Files/Gauche/bin/")
  1780. (defun start-ckw-bash ()
  1781. ""
  1782. (interactive)
  1783. (start-process "ckw_bash"
  1784. nil
  1785. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe")) ; cじゃないといけないらしい
  1786. (defun my-w32-add-export-path (&rest args)
  1787. ""
  1788. (mapcar (lambda (path)
  1789. (add-to-list 'exec-path (expand-file-name path)))
  1790. (reverse args))
  1791. (setenv "PATH"
  1792. (mapconcat 'convert-standard-filename
  1793. exec-path
  1794. ";")))
  1795. (when (eq system-type 'windows-nt)
  1796. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  1797. ;; (setq python-python-command "c:/Python26/python.exe")
  1798. (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  1799. (my-w32-add-export-path "c:/WINDOWS"
  1800. (expand-file-name "~/bin")
  1801. (expand-file-name "~/dbx/apps/bin"))
  1802. (when window-system
  1803. (setq w32-enable-synthesized-fonts t))
  1804. (setq file-name-coding-system 'sjis))