Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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