選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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