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.
 
 
 
 
 
 

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