Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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