Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

2087 Zeilen
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. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1211. (substitute-key-definition 'dired-next-line 'my-dired-next-line dired-mode-map)
  1212. (substitute-key-definition 'dired-previous-line 'my-dired-previous-line dired-mode-map)
  1213. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1214. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1215. (let ((file "._Icon\015"))
  1216. (when (file-readable-p file)
  1217. (delete-file file)))))
  1218. ;; http://homepage1.nifty.com/blankspace/emacs/dired.html
  1219. ;; (add-hook 'dired-load-hook
  1220. ;; (lambda ()
  1221. ;; (load-library "ls-lisp")
  1222. ;; (setq ls-lisp-dirs-first t)
  1223. ;; (setq dired-listing-switches "-alhF"))) ;これ書く場所間違えてね?
  1224. ;; (defadvice dired-next-line (after dired-next-line-print-directory activate)
  1225. ;; "print current directory when go down line"
  1226. ;; (dired-print-current-dir-and-file))
  1227. ;; (defadvice dired-previous-line (after dired-previous-line-print-directory activate)
  1228. ;; "print current directory when go up line"
  1229. ;; (dired-print-current-dir-and-file))
  1230. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1231. (defun my-dired-toggle-mark()
  1232. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1233. (t dired-marker-char))))
  1234. (delete-char 1)
  1235. (insert cur)))
  1236. (defun my-dired-mark (arg)
  1237. "toggle mark the current (or next ARG) files.
  1238. If on a subdir headerline, mark all its files except `.' and `..'.
  1239. Use \\[dired-unmark-all-files] to remove all marks
  1240. and \\[dired-unmark] on a subdir to remove the marks in
  1241. this subdir."
  1242. (interactive "P")
  1243. (if (dired-get-subdir)
  1244. (save-excursion (dired-mark-subdir-files))
  1245. (let ((inhibit-read-only t))
  1246. (dired-repeat-over-lines
  1247. (prefix-numeric-value arg)
  1248. 'my-dired-toggle-mark))))
  1249. (defun my-dired-mark-backward (arg)
  1250. "In Dired, move up lines and toggle mark there.
  1251. Optional prefix ARG says how many lines to unflag; default is one line."
  1252. (interactive "p")
  1253. (my-dired-mark (- arg)))
  1254. (defun dired-mode-hooks()
  1255. (local-set-key (kbd "SPC") 'my-dired-mark)
  1256. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1257. (add-hook 'dired-mode-hook 'dired-mode-hooks)
  1258. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1259. ;; eshell
  1260. (defun my-eshell-backward-delete-char ()
  1261. (interactive)
  1262. (when (< (save-excursion
  1263. (eshell-bol)
  1264. (point))
  1265. (point))
  1266. (backward-delete-char 1)))
  1267. (defvar my-eshell-frame-buffer-alist nil)
  1268. (defun my-eshell-frame-buffer (frame)
  1269. "get buffer associated with FRAME. if buffer doesnt exist or killed, return nil."
  1270. (let ((bf (cdr (assq frame my-eshell-frame-buffer-alist))))
  1271. (and bf ;関連付けられたバッファが存在し
  1272. (buffer-name bf) ;かつkillされてない
  1273. bf)))
  1274. (add-hook 'eshell-mode-hook
  1275. (lambda ()
  1276. (add-to-list 'my-eshell-frame-buffer-alist
  1277. (cons (selected-frame) (current-buffer)))))
  1278. (defun my-file-owner-p (file)
  1279. "t if FILE is owned by me."
  1280. (eq (user-uid) (nth 2 (file-attributes file))))
  1281. ;; ;; http://www.bookshelf.jp/pukiwiki/pukiwiki.php?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9
  1282. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1283. ;; (defun eshell/less (&rest args)
  1284. ;; "Invoke `view-file' on the file.
  1285. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1286. ;; (if args
  1287. ;; (while args
  1288. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1289. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1290. ;; (file (pop args)))
  1291. ;; (view-file file)
  1292. ;; (goto-line line))
  1293. ;; (view-file (pop args))))))
  1294. ;; (defun eshell/git (&rest args)
  1295. ;; ""
  1296. ;; (eshell-parse-arguments (point-at-bol) (point-at-eol)))
  1297. (defun eshell/o (&optional file)
  1298. (my-x-open (or file ".")))
  1299. ;; (defun eshell/vi (&rest args)
  1300. ;; "Invoke `find-file' on the file.
  1301. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1302. ;; (while args
  1303. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1304. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1305. ;; (file (pop args)))
  1306. ;; (find-file file)
  1307. ;; (goto-line line))
  1308. ;; (find-file (pop args)))))
  1309. (defun eshell/clear ()
  1310. "Clear the current buffer, leaving one prompt at the top."
  1311. (let ((inhibit-read-only t))
  1312. (erase-buffer)))
  1313. (defun eshell/d (&optional dirname switches)
  1314. "if first arg is omitted open current directory."
  1315. (dired (or dirname ".") switches))
  1316. (defun eshell/v ()
  1317. (view-mode 1))
  1318. (defalias 'eshell/: 'ignore)
  1319. (defalias 'eshell/type 'eshell/which)
  1320. ;; (defalias 'eshell/vim 'eshell/vi)
  1321. (defalias 'eshell/ff 'find-file)
  1322. (defalias 'eshell/q 'eshell/exit)
  1323. (defun eshell-goto-prompt ()
  1324. ""
  1325. (interactive)
  1326. (goto-char (point-max)))
  1327. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1328. "open eshell and change wd
  1329. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1330. (interactive)
  1331. (let ((dir (expand-file-name default-directory)))
  1332. (switch-to-buffer (or eshell-buffer-or-name
  1333. (eshell t)))
  1334. (unless (equal dir (expand-file-name default-directory))
  1335. ;; (cd dir)
  1336. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1337. ;; (eshell-emit-prompt)
  1338. (goto-char (point-max))
  1339. (eshell-kill-input)
  1340. (insert "cd " dir)
  1341. (eshell-send-input))))
  1342. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1343. (setq eshell-scroll-to-bottom-on-input t)
  1344. (setq eshell-cmpl-ignore-case t)
  1345. (setq eshell-cmpl-cycle-completions nil)
  1346. (setq eshell-highlight-prompt nil)
  1347. (setq eshell-ls-initial-args "-FG") ; "-hF")
  1348. (setq eshell-prompt-function
  1349. (lambda ()
  1350. (with-temp-buffer
  1351. (let (p1 p2 p3 p4)
  1352. (insert " [")
  1353. (setq p1 (point))
  1354. (insert (abbreviate-file-name default-directory))
  1355. (setq p2 (point))
  1356. (insert "]"
  1357. "\n")
  1358. (setq p3 (point))
  1359. (insert user-login-name
  1360. "@"
  1361. (or (getenv "HOSTNAME")
  1362. (substring (shell-command-to-string (or (executable-find "hostname")
  1363. "echo ''"))
  1364. 0
  1365. -1)))
  1366. (setq p4 (point))
  1367. (insert " "
  1368. (format-time-string "%a, %d %b %Y %T %z")
  1369. " eshell\n"
  1370. "last:"
  1371. (number-to-string eshell-last-command-status)
  1372. (if (= (user-uid)
  1373. 0)
  1374. " # "
  1375. " $ "))
  1376. (add-text-properties p1
  1377. p2
  1378. '(face ((foreground-color . "yellow"))))
  1379. (add-text-properties p3
  1380. p4
  1381. '(face ((foreground-color . "cyan"))))
  1382. (buffer-substring (point-min)
  1383. (point-max))))))
  1384. (add-hook 'eshell-mode-hook
  1385. (lambda ()
  1386. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1387. ;; (interactive)
  1388. ;; (switch-to-buffer (other-buffer))))
  1389. (define-key eshell-mode-map (kbd "C-u") (lambda ()
  1390. (interactive)
  1391. (eshell-goto-prompt)
  1392. (eshell-kill-input)))
  1393. (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1394. (interactive)
  1395. (eshell-goto-prompt)
  1396. (my-keyboard-quit)))
  1397. (define-key eshell-mode-map (kbd "DEL") 'my-eshell-backward-delete-char)
  1398. (define-key eshell-mode-map (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1399. (define-key eshell-mode-map (kbd "C-n") 'eshell-next-matching-input-from-input)
  1400. (mapcar (lambda (alias)
  1401. (add-to-list 'eshell-command-aliases-list
  1402. alias))
  1403. '(("ll" "ls -l")
  1404. ("la" "ls -a")
  1405. ("lla" "ls -al")
  1406. ("ut" "slogin 03110414@un001.ecc.u-tokyo.ac.jp")
  1407. ("aptin" "sudo apt-get install")
  1408. ("u" "uname")
  1409. ("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*\"))")
  1410. ("g" "git")))
  1411. (add-to-list 'eshell-output-filter-functions 'eshell-truncate-buffer)
  1412. (apply 'eshell/addpath exec-path)
  1413. (set (make-variable-buffer-local 'scroll-margin) 0)
  1414. (eshell/export "GIT_PAGER=")
  1415. (eshell/export "GIT_EDITOR=")
  1416. (eshell/export "LC_MESSAGES=C")
  1417. (eshell/export "TERM=xterm")
  1418. ))
  1419. ;; (eval-after-load "em-alias"
  1420. ;; '(progn ;; (eshell/alias "ll" "ls -l")
  1421. ;; ;; (eshell/alias "la" "ls -a")
  1422. ;; ;; (eshell/alias "lla" "ls -al")
  1423. ;; (eshell/alias "sgcc" (if (eq system-type 'windows-nt)
  1424. ;; "gcc -o win.$1.exe $1"
  1425. ;; "gcc -o ${uname}.$1.out $1"))
  1426. ;; (eshell/alias "slmgcc" (if (eq system-type 'windows-nt)
  1427. ;; "gcc -lm -o win.$1.exe $1"
  1428. ;; "gcc -lm -o ${uname}.$1.out $1"))
  1429. ;; ;; (eshell/alias "ut" "ssh g841105@un001.ecc.u-tokyo.ac.jp")
  1430. ;; (add-to-list 'recentf-exclude (concat eshell-directory-name "alias"))))
  1431. ;; (define-key my-prefix-map (kbd "C-s") (lambda ()
  1432. ;; (interactive)
  1433. ;; (eshell-cd-default-directory (buffer-name (or (my-eshell-frame-buffer (selected-frame))
  1434. ;; (eshell t))))))
  1435. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1436. ;; 最終更新日時を得る
  1437. (defvar my-buffer-file-last-modified-time nil "")
  1438. (make-variable-buffer-local 'my-buffer-file-last-modified-time)
  1439. (defun my-get-file-last-modified-time (file)
  1440. ""
  1441. (nth 5
  1442. (file-attributes file)))
  1443. (defun my-set-buffer-file-last-modified-time ()
  1444. ""
  1445. (make-local-variable 'my-buffer-file-last-modified-time)
  1446. (setq my-buffer-file-last-modified-time
  1447. (format-time-string "%Y/%m/%d %H:%M" (my-get-file-last-modified-time buffer-file-name))))
  1448. (add-hook 'find-file-hook
  1449. 'my-set-buffer-file-last-modified-time)
  1450. (add-hook 'after-save-hook
  1451. 'my-set-buffer-file-last-modified-time)
  1452. (add-hook 'after-revert-hook
  1453. 'my-set-buffer-file-last-modified-time)
  1454. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1455. ;; auto saving
  1456. (defun my-save-this-buffer (silent-p)
  1457. "save current buffer if can without asking"
  1458. (let ((cm (if (current-message)
  1459. (format "%s\n" (current-message))
  1460. ""))
  1461. (fun (symbol-function (if silent-p
  1462. 'ignore
  1463. 'message))))
  1464. (cond ((active-minibuffer-window) nil)
  1465. ((not buffer-file-name) (funcall fun "%ssaving... this buffer doesn't visit any file." cm) nil)
  1466. ((not (file-exists-p buffer-file-name)) (funcall fun "%ssaving... file not exist. save manually first." com) nil)
  1467. (buffer-read-only (funcall fun "%ssaving... this buffer is read-only." cm) nil)
  1468. ((not (buffer-modified-p)) (funcal fun "%ssaving... not modified yet." cm) nil)
  1469. ((not (file-writable-p buffer-file-name)) (funcall fun "%ssaving... you cannot change this file." cm) nil)
  1470. (t (funcall fun "%ssaving..." cm)
  1471. (save-buffer)
  1472. (funcall fun "%ssaving... done." cm)))))
  1473. ;; (if (and buffer-file-name
  1474. ;; (not buffer-read-only)
  1475. ;; (buffer-modified-p)
  1476. ;; (file-writable-p buffer-file-name))
  1477. ;; (save-buffer))) ; 静かな方
  1478. (defvar my-auto-save-this-buffer nil "auto save timer object")
  1479. (defun my-auto-save-this-buffer (sec &optional silent-p)
  1480. "auto save current buffer if idle for SEC.
  1481. when SEC is nil, stop auto save if enabled."
  1482. (if sec
  1483. (progn (when my-auto-save-this-buffer
  1484. (cancel-timer my-auto-save-this-buffer)
  1485. (setq my-auto-save-this-buffer nil))
  1486. (setq my-auto-save-this-buffer (run-with-idle-timer sec t 'my-save-this-buffer silent-p)))
  1487. (when my-auto-save-this-buffer
  1488. (cancel-timer my-auto-save-this-buffer)
  1489. (setq my-auto-save-this-buffer nil))))
  1490. (my-auto-save-this-buffer 2 t)
  1491. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1492. ;; misc funcs
  1493. (defvar my-desktop-terminal "roxterm")
  1494. (defun my-execute-terminal ()
  1495. ""
  1496. (interactive)
  1497. (if (and (or (eq system-type 'windows-nt)
  1498. window-system)
  1499. my-desktop-terminal
  1500. )
  1501. (let ((process-environment (cons "TERM=xterm" process-environment)))
  1502. (start-process "terminal"
  1503. nil
  1504. my-desktop-terminal))
  1505. (my-execute-or-find-term)))
  1506. (defun my-term ()
  1507. "open terminal buffer and return that buffer."
  1508. (interactive)
  1509. (if (eq system-type 'windows-nt)
  1510. (eshell t)
  1511. (if (featurep 'multi-term)
  1512. (multi-term)
  1513. (ansi-term "/bin/bash"))))
  1514. (defvar my-frame-term-plist nil)
  1515. ;; (setplist my-frame-term-plist nil)
  1516. (defun my-execute-or-find-term ()
  1517. ""
  1518. (interactive)
  1519. (let* ((buf (plist-get my-frame-term-plist (selected-frame))))
  1520. (if (and buf
  1521. (buffer-name buf))
  1522. (switch-to-buffer buf)
  1523. (setq my-frame-term-plist
  1524. (plist-put my-frame-term-plist
  1525. (selected-frame)
  1526. (my-term))))))
  1527. (defun my-format-time-string (&optional time)
  1528. ""
  1529. (let ((system-time-locale "C"))
  1530. (format-time-string "%a, %d %b %Y %T" time)))
  1531. (defvar my-filer nil)
  1532. (setq my-filer (or (executable-find "pcmanfm")
  1533. (executable-find "nautilus")))
  1534. (defun my-x-open (file)
  1535. "open file."
  1536. (interactive "FOpen File: ")
  1537. (setq file (expand-file-name file))
  1538. (message "Opening %s..." file)
  1539. (cond ((eq system-type 'windows-nt)
  1540. (call-process "cmd.exe" nil 0 nil "/c" "start" "" (convert-standard-filename file)))
  1541. ((eq system-type 'darwin)
  1542. (call-process "open" nil 0 nil file))
  1543. ((not (getenv "DESKTOP_SESSION"))
  1544. (find-file file))
  1545. (t
  1546. (if (file-directory-p file)
  1547. (call-process my-filer nil 0 nil file)
  1548. (call-process "xdg-open" nil 0 nil file))))
  1549. (recentf-add-file file)
  1550. (message "Opening %s...done" file))
  1551. (defvar my-auto-indent-buffer-mode-list
  1552. '(emacs-lisp-mode
  1553. sh-mode
  1554. js-mode
  1555. sgml-mode
  1556. c-mode
  1557. c++-mode))
  1558. (setq my-auto-indent-buffer-mode-list nil)
  1559. (defun my-indent-buffer ()
  1560. "indent whole buffer."
  1561. (interactive)
  1562. (indent-region (point-min)
  1563. (point-max)))
  1564. (defun my-auto-indent-buffer ()
  1565. ""
  1566. (when (memq major-mode my-auto-indent-buffer-mode-list)
  1567. (my-indent-buffer)))
  1568. (add-hook 'before-save-hook
  1569. 'my-auto-indent-buffer)
  1570. (defun my-keyboard-quit ()
  1571. ""
  1572. (interactive)
  1573. (run-hooks 'before-keyboard-quit-hook)
  1574. ;; (redisplay t)
  1575. (redraw-display)
  1576. ;; (run-hooks 'window-configuration-change-hook)
  1577. (my-revert-buffer-if-needed)
  1578. ;; (revert-buffer t t)
  1579. (keyboard-quit)
  1580. (insert "insert me")
  1581. (run-hooks 'after-keyboard-quit-hook))
  1582. (substitute-key-definition 'keyboard-quit 'my-keyboard-quit global-map)
  1583. ;; (global-set-key (kbd "C-g") 'my-keyboard-quit)
  1584. (defun my-convmv-sjis2utf8-test ()
  1585. "run `convmv -r -f sjis -t utf8 *'
  1586. this is test, does not rename files"
  1587. (interactive)
  1588. (shell-command "convmv -r -f sjis -t utf8 *"))
  1589. (defun my-convmv-sjis2utf8-notest ()
  1590. "run `convmv -r -f sjis -t utf8 * --notest'"
  1591. (interactive)
  1592. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  1593. (defun my-copy-whole-line ()
  1594. ""
  1595. (interactive)
  1596. (kill-new (concat (buffer-substring (point-at-bol)
  1597. (point-at-eol))
  1598. "\n")))
  1599. (defun kill-ring-save-buffer-file-name ()
  1600. "get current filename"
  1601. (interactive)
  1602. (let ((file buffer-file-name))
  1603. (if file
  1604. (progn (kill-new file)
  1605. (message file))
  1606. (message "not visiting file."))))
  1607. ;; ;; コマンド的な
  1608. ;; (defvar my-execute-func-list nil "func list")
  1609. ;; (defvar my-execute-func-hist-list nil "func hist list")
  1610. ;; (setq my-execute-func-list '("(call-interactively 'my-francaiscd-b)"
  1611. ;; "(call-interactively 'my-francaiscd-a)"
  1612. ;; "parsec47"
  1613. ;; "chromium-browser"
  1614. ;; "inkscape"
  1615. ;; "audacious"
  1616. ;; "gnome-terminal"
  1617. ;; "zkaicd.py"
  1618. ;; "glchess"))
  1619. ;; (defun my-execute-start-process-or-eval-sexp ()
  1620. ;; "execute something"
  1621. ;; (interactive)
  1622. ;; (let ((func (completing-read "command?: " my-execute-func-list nil nil "" my-execute-func-hist-list)))
  1623. ;; (if (string= "(" (substring func 0 1))
  1624. ;; (with-temp-buffer (insert func)
  1625. ;; (eval-buffer))
  1626. ;; (start-process "ps"
  1627. ;; nil
  1628. ;; func))))
  1629. ;; delete-trailing-whitespace
  1630. ;; (defun my-delete-blanks-on-eol ()
  1631. ;; ""
  1632. ;; (interactive)
  1633. ;; (save-excursion
  1634. ;; (goto-char (point-min))
  1635. ;; (while (re-search-forward "[ \t]+$" nil t)
  1636. ;; (replace-match "" nil nil))))
  1637. (defvar my-revert-buffer-if-needed-last-buffer nil)
  1638. (defun my-revert-buffer-if-needed ()
  1639. ""
  1640. (interactive)
  1641. (unless (eq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1642. (setq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1643. (when (or (eq major-mode 'dired-mode)
  1644. (not (verify-visited-file-modtime (current-buffer))))
  1645. (revert-buffer t t))))
  1646. (add-hook 'window-configuration-change-hook
  1647. 'my-revert-buffer-if-needed)
  1648. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1649. ;; forked from http://d.hatena.ne.jp/khiker/20100119/window_resize
  1650. (define-key my-prefix-map (kbd "C-w") 'my-window-organizer)
  1651. (defun my-window-organizer ()
  1652. "Control window size and position."
  1653. (interactive)
  1654. (save-selected-window
  1655. (select-window (window-at 0 0))
  1656. (let ( ;; (window-obj (selected-window))
  1657. ;; (current-width (window-width))
  1658. ;; (current-height (window-height))
  1659. action
  1660. c)
  1661. (catch 'end-flag
  1662. (while t
  1663. (setq action
  1664. (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."
  1665. (window-width)
  1666. (window-height))))
  1667. (setq c (aref action 0))
  1668. (cond ((= c ?l)
  1669. (unless (eq (window-width) (frame-width))
  1670. (enlarge-window-horizontally 1)))
  1671. ((= c ?h)
  1672. (unless (eq (window-width) (frame-width))
  1673. (shrink-window-horizontally 1)))
  1674. ((= c ?j)
  1675. (enlarge-window 1))
  1676. ((= c ?k)
  1677. (shrink-window 1))
  1678. ((= c ?o)
  1679. (other-window 1))
  1680. ((memq c '(?d ?0))
  1681. (unless (eq (selected-window) (next-window (selected-window) 0 1))
  1682. (delete-window (selected-window))))
  1683. ((= c ?1)
  1684. (delete-other-windows))
  1685. ((= c ?2)
  1686. (split-window-vertically))
  1687. ((= c ?3)
  1688. (split-window-horizontally))
  1689. ((memq c '(?q ?\C-g))
  1690. (message "Quit")
  1691. (throw 'end-flag t))
  1692. (t
  1693. (beep))))))))
  1694. ;; (aref (read-key-sequence-vector "aa") 0)
  1695. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1696. ;; save and restore frame size
  1697. ;;http://www.bookshelf.jp/soft/meadow_30.html#SEC416
  1698. (defun my-window-size-save ()
  1699. (let* ((rlist (frame-parameters (selected-frame)))
  1700. (ilist initial-frame-alist)
  1701. (nCHeight (frame-height))
  1702. (nCWidth (frame-width))
  1703. (tMargin (if (integerp (cdr (assoc 'top rlist)))
  1704. (cdr (assoc 'top rlist)) 0))
  1705. (lMargin (if (integerp (cdr (assoc 'left rlist)))
  1706. (cdr (assoc 'left rlist)) 0))
  1707. buf
  1708. (file "~/.emacs.d/.framesize.el")
  1709. (recentf-exclude '("\\.emacs\\.d/\\.framesize\\.el$")))
  1710. (if (get-file-buffer (expand-file-name file))
  1711. (setq buf (get-file-buffer (expand-file-name file)))
  1712. (setq buf (find-file-noselect file)))
  1713. (set-buffer buf)
  1714. (erase-buffer)
  1715. (insert (concat
  1716. ;; 初期値をいじるよりも modify-frame-parameters
  1717. ;; で変えるだけの方がいい?
  1718. "(delete 'width default-frame-alist)\n"
  1719. "(delete 'height default-frame-alist)\n"
  1720. "(delete 'top default-frame-alist)\n"
  1721. "(delete 'left default-frame-alist)\n"
  1722. "(setq default-frame-alist (append (list\n"
  1723. "'(width . " (int-to-string nCWidth) ")\n"
  1724. "'(height . " (int-to-string nCHeight) ")\n"
  1725. "'(top . " (int-to-string tMargin) ")\n"
  1726. "'(left . " (int-to-string lMargin) "))\n"
  1727. "default-frame-alist))\n"
  1728. ;;"(setq default-frame-alist default-frame-alist)"
  1729. ))
  1730. (save-buffer)
  1731. ))
  1732. (defun my-window-size-load ()
  1733. (let* ((file "~/.emacs.d/.framesize.el"))
  1734. (if (file-exists-p file)
  1735. (load file))))
  1736. (when window-system
  1737. (my-window-size-load)
  1738. (add-hook 'after-init-hook ;何かがframeの大きさ勝手に変えやがる
  1739. (lambda ()
  1740. (run-with-timer 1
  1741. nil
  1742. (lambda ()
  1743. (modify-frame-parameters (selected-frame)
  1744. default-frame-alist))))
  1745. t)
  1746. ;; (add-hook 'make-frame-hook
  1747. ;; (lambda ()
  1748. ;; (run-with-timer 1
  1749. ;; nil
  1750. ;; (lambda ()
  1751. ;; (modify-frame-parameters (selected-frame)
  1752. ;; initial-frame-alist))))
  1753. ;; t)
  1754. (add-hook 'kill-emacs-hook
  1755. 'my-window-size-save))
  1756. ;; windowサイズを固定
  1757. ;; setq default-frame-alist
  1758. ;; (append (list '(width . 80)
  1759. ;; '(height . 35)
  1760. ;; )
  1761. ;; default-frame-alist)
  1762. ;; ) ;;デフォルトのフレーム設定
  1763. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1764. ;; ;; emacsを殺伐とさせる
  1765. ;; ;; 補完させるとき失敗するからなし
  1766. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  1767. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  1768. ;; (setq arg
  1769. ;; (concat arg
  1770. ;; (if (eq nil (string-match "\\. *$" arg)) ".")
  1771. ;; " And You are a Coward!")))
  1772. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1773. ;; ubuntu
  1774. (defun my-load-scim ()
  1775. "use scim-bridge.el as japanese im."
  1776. ;; Load scim-bridge.
  1777. (require 'scim-bridge)
  1778. ;; Turn on scim-mode automatically after loading .emacs
  1779. (add-hook 'after-init-hook 'scim-mode-on)
  1780. (setq scim-cursor-color "red")
  1781. (scim-define-preedit-key ?\^h t)
  1782. (scim-define-common-key ?\* nil)
  1783. (scim-define-common-key ?\^/ nil))
  1784. (defun my-load-anthy ()
  1785. "use anthy.el as japanese im."
  1786. ;; anthy
  1787. (require 'anthy)
  1788. (global-set-key [muhenkan] (lambda () (interactive) (anthy-mode-off)))
  1789. (global-set-key [henkan] (lambda () (interactive) (anthy-mode-on)))
  1790. (when (>= emacs-major-version 23)
  1791. (setq anthy-accept-timeout 1)))
  1792. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1793. ;; windows用設定
  1794. ;; (add-to-list 'exec-path "c:/Program Files/Gauche/bin/")
  1795. (defun start-ckw-bash ()
  1796. ""
  1797. (interactive)
  1798. (start-process "ckw_bash"
  1799. nil
  1800. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe")) ; cじゃないといけないらしい
  1801. (defun my-w32-add-export-path (&rest args)
  1802. ""
  1803. (mapcar (lambda (path)
  1804. (add-to-list 'exec-path (expand-file-name path)))
  1805. (reverse args))
  1806. (setenv "PATH"
  1807. (mapconcat 'convert-standard-filename
  1808. exec-path
  1809. ";")))
  1810. (when (eq system-type 'windows-nt)
  1811. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  1812. ;; (setq python-python-command "c:/Python26/python.exe")
  1813. (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  1814. (my-w32-add-export-path "c:/WINDOWS"
  1815. (expand-file-name "~/bin")
  1816. (expand-file-name "~/dbx/apps/bin"))
  1817. (when window-system
  1818. (setq w32-enable-synthesized-fonts t))
  1819. (setq file-name-coding-system 'sjis))