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.
 
 
 
 
 
 

2009 lines
71 KiB

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