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.
 
 
 
 
 
 

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