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.
 
 
 
 
 
 

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