You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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