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.
 
 
 
 
 
 

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