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.
 
 
 
 
 
 

2015 lines
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. (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. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  693. ;; (setq term-ansi-default-program shell-file-name)
  694. (add-hook 'term-setup-hook (lambda ()
  695. (setq term-display-table (make-display-table))))
  696. (add-hook 'term-mode-hook (lambda ()
  697. (unless (memq (current-buffer) (and (featurep 'multi-term) ; current buffer is not multi-term buffer
  698. (multi-term-list)))
  699. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  700. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  701. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  702. ;; (define-key term-raw-map "\C-f" 'forward-char)
  703. ;; (define-key term-raw-map "\C-b" 'backward-char)
  704. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  705. (define-key term-raw-map "\C-x" (lookup-key (current-global-map) "\C-x"))
  706. (define-key term-raw-map "\C-z" (lookup-key (current-global-map) "\C-z")))
  707. (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  708. (define-key term-raw-map [delete] 'term-send-raw)
  709. (define-key term-raw-map "\C-h" 'term-send-backspace)
  710. (define-key term-raw-map "\C-y" 'term-paste)
  711. (define-key term-raw-map "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  712. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  713. ;; (define-key term-raw-map (kbd key) 'term-send-raw))
  714. ;; (define-key term-raw-map "\C-d" 'delete-char)
  715. (set (make-variable-buffer-local 'scroll-margin) 0)
  716. ;; (set (make-variable-buffer-local 'cua-enable-cua-keys) nil)
  717. ;; (cua-mode 0)
  718. ;; (and cua-mode
  719. ;; (local-unset-key (kbd "C-c")))
  720. ;; (define-key cua--prefix-override-keymap "\C-c" 'term-interrupt-subjob)
  721. ))
  722. ;; (add-hook 'term-exec-hook 'forward-char)
  723. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  724. ;; buffer switching
  725. (when (require 'bs nil t)
  726. ;; (global-set-key "\C-x\C-b" 'bs-show)
  727. (defalias 'list-buffers 'bs-show))
  728. ;; (add-to-list 'bs-configurations '("processes" nil get-buffer-process ".*" nil nil))
  729. (add-to-list 'bs-configurations '("same-dir" nil buffer-same-dir-p ".*" nil nil))
  730. (add-to-list 'bs-configurations '("this-frame" nil (lambda (buf) (memq buf (my-frame-buffer-get))) ".*" nil nil))
  731. ;; (setq bs-configurations (list '("processes" nil get-buffer-process ".*" nil nil)
  732. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil bs-visits-non-file bs-sort-buffer-interns-are-last)))
  733. (setq bs-default-configuration "this-frame")
  734. (setq bs-default-sort-name "by name")
  735. (add-hook 'bs-mode-hook
  736. (lambda ()
  737. (setq bs-default-configuration "this-frame")
  738. ;; (and bs--show-all
  739. ;; (call-interactively 'bs-toggle-show-all))
  740. (set (make-variable-buffer-local 'scroll-margin) 0)
  741. ))
  742. (defun buffer-same-dir-p (bf)
  743. "return t if BF's dir is same as current dir, otherwise nil."
  744. (let ((cdir (expand-file-name default-directory)))
  745. (with-current-buffer bf
  746. (equal (expand-file-name default-directory) cdir))))
  747. (defun echo-buffer-list (&optional blist)
  748. "echo buffer list as string. BLIST is list with buffer objects as elements.
  749. if arg is omitted use value of `buffer-list'."
  750. (interactive)
  751. (message (or (mapconcat (lambda (bf)
  752. (concat (buffer-name bf)
  753. "\t"
  754. (with-current-buffer bf
  755. (symbol-name major-mode))
  756. "\t"
  757. (abbreviate-file-name (buffer-file-name bf))))
  758. (or blist
  759. (buffer-list))
  760. "\n")
  761. "")))
  762. (defun my-buffer-list ()
  763. "return buffer list."
  764. (delq nil
  765. (mapcar (lambda (bf)
  766. (with-current-buffer bf
  767. (and buffer-file-name
  768. bf)))
  769. (buffer-list (selected-frame)))))
  770. (defvar buffer-switch-list-function 'my-buffer-list)
  771. (defun switch-to-previous-buffer-cycle (&optional silent-p)
  772. ""
  773. (interactive)
  774. (let ((bl (funcall buffer-switch-list-function)))
  775. (when bl
  776. (bury-buffer (car bl))
  777. (switch-to-buffer (or (nth 1 bl)
  778. (car bl)))
  779. (or silent-p
  780. (echo-buffer-list (funcall buffer-switch-list-function))))))
  781. (defun switch-to-next-buffer-cycle (&optional silent-p)
  782. ""
  783. (interactive)
  784. (let* ((bl (funcall buffer-switch-list-function))
  785. (bf (nth (- (length bl)
  786. 1)
  787. bl)))
  788. (when bl
  789. (switch-to-buffer bf)
  790. (or silent-p
  791. (echo-buffer-list (funcall buffer-switch-list-function))))))
  792. (iswitchb-mode 1)
  793. (defun iswitchb-buffer-display-other-window ()
  794. ""
  795. (interactive)
  796. (let ((iswitchb-default-method 'display))
  797. (call-interactively 'iswitchb-buffer)))
  798. (defun switch-to-other-buffer ()
  799. ""
  800. (interactive)
  801. (let ((buffer-switch-list-function 'buffer-list))
  802. (switch-to-previous-buffer-cycle t)))
  803. (global-set-key (kbd "C-.") 'switch-to-previous-buffer-cycle)
  804. (global-set-key (kbd "C-,") 'switch-to-next-buffer-cycle)
  805. ;; (global-set-key (kbd "C-\\") 'switch-to-other-buffer)
  806. ;; (global-set-key (kbd "C-\\") 'bury-buffer)
  807. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  808. ;; sdic
  809. (defun sdic-describe-word-at-point-echo ()
  810. ""
  811. (interactive)
  812. (save-window-excursion
  813. (sdic-describe-word-at-point))
  814. (save-excursion
  815. (set-buffer sdic-buffer-name)
  816. (message (buffer-substring (point-min)
  817. (progn (goto-char (point-min))
  818. (or (and (re-search-forward "^\\w" nil t 4)
  819. (progn (previous-line) t)
  820. (point-at-eol))
  821. (point-max)))))))
  822. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  823. (setq sdic-waei-dictionary-list '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  824. (setq sdic-disable-select-window t)
  825. (setq sdic-window-height 7)
  826. (when (require 'sdic nil t)
  827. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  828. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo))
  829. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  830. ;; vc
  831. ;; (require 'vc)
  832. (setq vc-handled-backends nil)
  833. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  834. ;; gauche-mode
  835. (setq scheme-program-name "gosh")
  836. (defun run-gauche-other-window ()
  837. "Run gauche on other window"
  838. (interactive)
  839. (switch-to-buffer-other-window
  840. (get-buffer-create "*scheme*"))
  841. (run-gauche))
  842. (defun run-gauche ()
  843. "run gauche"
  844. (run-scheme "gosh"))
  845. (defun scheme-send-buffer ()
  846. ""
  847. (interactive)
  848. (scheme-send-region (point-min) (point-max)))
  849. (add-hook 'scheme-mode-hook
  850. (lambda ()
  851. (define-key scheme-mode-map "\C-c\C-b" 'scheme-send-buffer)))
  852. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  853. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  854. (when (dllib-if-unfound "gauche-mode"
  855. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  856. t)
  857. (setq auto-mode-alist
  858. (cons '("\.scm$" . gauche-mode) auto-mode-alist))
  859. (autoload 'gauche-mode "gauche-mode" "Major mode for Scheme." t)
  860. (autoload 'run-scheme "gauche-mode" "Run an inferior Scheme process." t)
  861. (add-hook 'gauche-mode-hook
  862. (lambda ()
  863. (define-key scheme-mode-map "\C-c\C-z" 'run-gauche-other-window))))
  864. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  865. ;; recentf-mode
  866. (add-hook 'recentf-dialog-mode-hook
  867. 'my-recentf-abbrev-list)
  868. (defun my-recentf-delete-entry ()
  869. ""
  870. (interactive)
  871. (let ((p (point)))
  872. (setq recentf-list
  873. (delete (my-recentf-get-filename) recentf-list))
  874. (recentf-open-files)
  875. (goto-char p)))
  876. (defun my-recentf-abbrev-list ()
  877. ""
  878. (setq recentf-list
  879. (mapcar 'abbreviate-file-name
  880. recentf-list)))
  881. (defun my-recentf-view-file ()
  882. ""
  883. (interactive)
  884. (view-file (my-recentf-get-filename)))
  885. (defun my-recentf-dired ()
  886. ""
  887. (interactive)
  888. (let ((file (my-recentf-get-filename)))
  889. (if (file-directory-p file)
  890. (dired file)
  891. (dired (or (file-name-directory file)
  892. ".")))))
  893. (defun my-recentf-x-open ()
  894. ""
  895. (interactive)
  896. (my-x-open (my-recentf-get-filename)))
  897. (defun my-recentf-cd-and-find-file ()
  898. ""
  899. (interactive)
  900. (cd (file-name-directory (my-recentf-get-filename)))
  901. (call-interactively 'find-file))
  902. (defun my-recentf-get-filename ()
  903. "get file name in recentf-mode"
  904. (replace-regexp-in-string " \\(\\[.+?\\] \\)?" ; " " or " [\\d] "
  905. ""
  906. (buffer-substring-no-properties (point-at-bol)
  907. (point-at-eol))))
  908. (setq recentf-save-file (expand-file-name "~/.emacs.d/.recentf")
  909. recentf-max-menu-items 20
  910. recentf-max-saved-items 30
  911. recentf-show-file-shortcuts-flag nil)
  912. (when (require 'recentf nil t)
  913. (global-set-key "\C-x\C-r" 'recentf-open-files)
  914. ;; (add-hook 'find-file-hook
  915. ;; (lambda ()
  916. ;; (recentf-add-file default-directory)))
  917. (recentf-mode 1)
  918. (add-to-list 'recentf-filename-handlers 'abbreviate-file-name)
  919. (add-to-list 'recentf-exclude "\\.emacs\\.d/\\.recentf"))
  920. (add-hook 'recentf-dialog-mode-hook
  921. (lambda ()
  922. (recentf-save-list)
  923. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f") 'my-recentf-cd-and-find-file)
  924. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  925. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  926. (define-key recentf-dialog-mode-map "o" 'my-recentf-x-open)
  927. (define-key recentf-dialog-mode-map "d" 'my-recentf-delete-entry)
  928. (define-key recentf-dialog-mode-map "@" 'my-recentf-dired)
  929. (define-key recentf-dialog-mode-map "v" 'my-recentf-view-file)
  930. (cd "~/")))
  931. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  932. ;; dired
  933. (require 'dired)
  934. (defun my-dired-echo-file-head (&optional arg)
  935. ""
  936. (interactive "P")
  937. (let ((f (dired-get-filename)))
  938. (message "%s"
  939. (with-temp-buffer
  940. (insert-file-contents f)
  941. (buffer-substring-no-properties (point-min)
  942. (progn (goto-line (if arg
  943. (prefix-numeric-value arg)
  944. 10))
  945. (point-at-eol)))))))
  946. (defun my-dired-diff ()
  947. ""
  948. (interactive)
  949. (let ((files (dired-get-marked-files nil nil nil t)))
  950. (if (eq (car files)
  951. t)
  952. (diff (cadr files) (dired-get-filename))
  953. (message "One files must be marked!"))))
  954. (require 'dired-aux) ;; needed to use dired-dwim-target-directory
  955. (defun my-dired-do-pack-or-unpack ()
  956. "pack or unpack files.
  957. if targetting one file and that is archive file defined in `pack-program-alist', unpack that.
  958. otherwise, pack marked files. prompt user to decide filename for archive."
  959. (interactive)
  960. (let* ((infiles (dired-get-marked-files t))
  961. (onefile (and (eq 1 ; filename if only one file targeted, otherwise nil.
  962. (length infiles))
  963. (car infiles))))
  964. (if (and onefile
  965. (my-pack-file-name-association onefile))
  966. (when (y-or-n-p (format "unpack %s? " onefile))
  967. (my-unpack onefile))
  968. (let* ((dir-default (dired-dwim-target-directory))
  969. (archive-default (my-pack-file-extension (file-name-nondirectory (car infiles))))
  970. (archive ;; (if (interactive-p)
  971. (read-file-name "Output file to pack : "
  972. dir-default
  973. nil
  974. nil
  975. archive-default)
  976. ;; (concat dir-default archive-default)
  977. ))
  978. (apply 'my-pack
  979. archive
  980. infiles))))
  981. (revert-buffer)
  982. ;; (dired-unmark-all-marks)
  983. )
  984. (defun my-file-name-extension-with-tar (filename)
  985. "if FILENAME has extension with tar, like \"tar.gz\", return that.
  986. otherwise, return extension normally."
  987. (if (string-equal "tar" (file-name-extension (file-name-sans-extension filename)))
  988. (concat "tar."
  989. (file-name-extension filename))
  990. (file-name-extension filename)))
  991. (defun my-pack-file-extension (filename)
  992. "if FILENAME has extension and it can be used for pack, return FILENAME.
  993. otherwise, return FILENAME with `my-pack-default-extension'"
  994. (if (my-pack-file-name-association filename)
  995. filename
  996. (concat filename "." my-pack-default-extension)))
  997. (defvar my-7z-program-name
  998. (or (executable-find "7z")
  999. (executable-find "7za")
  1000. (executable-find "7zr"))
  1001. "7z program.")
  1002. (defvar my-pack-default-extension
  1003. "7z"
  1004. "default suffix for packing. filename with this suffix must matches one of `pack-program-alist'")
  1005. (defun my-pack-file-name-association (filename)
  1006. "if the pattern matching FILENAME is found at car of the list in `pack-program-alist', return cdr of that list.
  1007. otherwise, return nil."
  1008. (let ((case-fold-search nil))
  1009. (assoc-default filename
  1010. my-pack-program-alist
  1011. 'string-match-p
  1012. nil)))
  1013. (defvar my-pack-program-alist
  1014. `(
  1015. ("\\.7z\\'" ,(concat my-7z-program-name " a") ,(concat my-7z-program-name " x"))
  1016. ("\\.zip\\'" "zip -r" "unzip")
  1017. ("\\.tar\\'" "tar cf" "tar xf")
  1018. ("\\.tgz\\'" "tar czf" "tar xzf")
  1019. ("\\.tar\\.gz\\'" "tar czf" "tar xzf")
  1020. )
  1021. "Alist of filename patterns, command for pack and unpack.
  1022. Each element looks like (REGEXP PACKING-COMMAND UNPACKING-COMMAND).
  1023. PACKING-COMMAND and UNPACKING-COMMAND can be nil if the command is not available.
  1024. alist is searched from the beginning so pattern for \".tar.gz\" should be ahead of pattern for \".gz\"")
  1025. ;; (string-match-p "\\.gz\\'" "aaa.gz") ; \' matches string end, $ also matches the point before newline.
  1026. (defun my-unpack (archive)
  1027. "unpack ARCHIVE. command for unpacking is defined in `pack-program-alist'"
  1028. (interactive "fArchive to extract: ")
  1029. (let* ((earchive (expand-file-name archive))
  1030. (cmd (nth 1
  1031. (my-pack-file-name-association earchive)))
  1032. )
  1033. (if cmd
  1034. (shell-command (concat cmd
  1035. " "
  1036. (shell-quote-argument earchive)))
  1037. (message "this is not archive file defined in `pack-program-alist'!"))))
  1038. (defun my-pack (archive &rest files)
  1039. "pack FILES into ARCHIVE.
  1040. if ARCHIVE have extension defined in `pack-program-alist', use that command.
  1041. otherwise, use `pack-default-extension' for pack."
  1042. (let* ((archive-ext (my-pack-file-extension (expand-file-name archive)))
  1043. (cmd (car (my-pack-file-name-association archive-ext)))
  1044. )
  1045. (if cmd
  1046. (shell-command (concat cmd
  1047. " "
  1048. (shell-quote-argument archive-ext)
  1049. " "
  1050. (mapconcat 'shell-quote-argument
  1051. files
  1052. " ")))
  1053. (message "invalid extension for packing!"))))
  1054. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1055. "pop up buffer using `display-buffer' and return that buffer."
  1056. (let ((bf (get-buffer-create buffer-or-name)))
  1057. (with-current-buffer bf
  1058. (cd ".")
  1059. (erase-buffer))
  1060. (display-buffer bf)
  1061. bf))
  1062. (defun my-replace-nasi-none ()
  1063. ""
  1064. (save-excursion
  1065. (let ((buffer-read-only nil))
  1066. (goto-char (point-min))
  1067. (while (search-forward "なし" nil t)
  1068. (replace-match "none")))))
  1069. (defun dired-get-du () ;em-unix.el使えるかも
  1070. "dired get disk usage"
  1071. (interactive)
  1072. (message "calculating du...")
  1073. (dired-do-shell-command "du -hs * " nil (dired-get-marked-files)))
  1074. (defun my-dired-scroll-up ()
  1075. ""
  1076. (interactive)
  1077. (my-dired-previous-line (- (window-height) 1)))
  1078. (defun my-dired-scroll-down ()
  1079. ""
  1080. (interactive)
  1081. (my-dired-next-line (- (window-height) 1)))
  1082. (defun my-dired-previous-line (&optional arg)
  1083. ""
  1084. (interactive)
  1085. (dired-previous-line (or arg 1))
  1086. (my-dired-print-current-dir-and-file))
  1087. (defun my-dired-next-line (&optional arg)
  1088. ""
  1089. (interactive)
  1090. (dired-next-line (or arg 1))
  1091. (my-dired-print-current-dir-and-file))
  1092. (defun my-dired-print-current-dir-and-file ()
  1093. (message "%s %s"
  1094. default-directory
  1095. (buffer-substring-no-properties (point-at-bol)
  1096. (point-at-eol))))
  1097. (defun dired-do-execute-as-command ()
  1098. ""
  1099. (interactive)
  1100. (let ((file (dired-get-filename t)))
  1101. (if (file-executable-p file)
  1102. (start-process file nil file)
  1103. (when (y-or-n-p "this file cant be executed. mark as executable and go? : ")
  1104. (set-file-modes file (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1105. (start-process file nil file)))))
  1106. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1107. (defun my-dired-x-open ()
  1108. ""
  1109. (interactive)
  1110. (my-x-open (dired-get-filename t t)))
  1111. (if (eq window-system 'mac)
  1112. (setq dired-listing-switches "-lhFG")
  1113. (setq dired-listing-switches "-lhFG --time-style=long-iso")
  1114. )
  1115. (setq dired-listing-switches "-lhFG")
  1116. (define-minor-mode my-dired-display-all-mode
  1117. ""
  1118. :init-value nil
  1119. :global nil
  1120. :lighter " ALL"
  1121. (when (eq major-mode 'dired-mode)
  1122. (my-dired-display-all-set)
  1123. (revert-buffer)))
  1124. (defun my-dired-display-all-set ()
  1125. ""
  1126. (if my-dired-display-all-mode
  1127. (or (string-match-p my-dired-display-all-switch
  1128. dired-actual-switches)
  1129. (setq dired-actual-switches
  1130. (concat my-dired-display-all-switch
  1131. " "
  1132. dired-actual-switches)))
  1133. (setq dired-actual-switches
  1134. (replace-regexp-in-string (concat my-dired-display-all-switch
  1135. " ")
  1136. ""
  1137. dired-actual-switches))))
  1138. (defvar my-dired-display-all-switch "-A")
  1139. (add-hook 'dired-mode-hook
  1140. 'my-dired-display-all-set)
  1141. (put 'dired-find-alternate-file 'disabled nil)
  1142. (setq dired-ls-F-marks-symlinks t)
  1143. (require 'ls-lisp)
  1144. (setq ls-lisp-use-insert-directory-program nil)
  1145. (setq ls-lisp-dirs-first t)
  1146. (setq ls-lisp-use-localized-time-format t)
  1147. (setq ls-lisp-format-time-list
  1148. '("%Y-%m-%d %H:%M"
  1149. "%Y-%m-%d "))
  1150. (setq dired-dwim-target t)
  1151. ;; (add-hook 'dired-after-readin-hook
  1152. ;; 'my-replace-nasi-none)
  1153. ;; (add-hook 'after-init-hook
  1154. ;; (lambda ()
  1155. ;; (dired ".")))
  1156. (add-hook 'dired-mode-hook
  1157. (lambda ()
  1158. (define-key dired-mode-map "o" 'my-dired-x-open)
  1159. (define-key dired-mode-map "i" 'dired-get-du)
  1160. (define-key dired-mode-map "!" 'shell-command)
  1161. (define-key dired-mode-map "&" 'async-shell-command)
  1162. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1163. (define-key dired-mode-map "=" 'my-dired-diff)
  1164. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1165. (define-key dired-mode-map "b" 'gtkbm)
  1166. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1167. (define-key dired-mode-map "@" (lambda () (interactive) (my-x-open ".")))
  1168. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1169. (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1170. (define-key dired-mode-map "a" 'my-dired-display-all-mode)
  1171. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1172. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1173. (substitute-key-definition 'dired-next-line 'my-dired-next-line dired-mode-map)
  1174. (substitute-key-definition 'dired-previous-line 'my-dired-previous-line dired-mode-map)
  1175. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1176. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1177. (let ((file "._Icon\015"))
  1178. (when (file-readable-p file)
  1179. (delete-file file)))))
  1180. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1181. (defun my-dired-toggle-mark()
  1182. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1183. (t dired-marker-char))))
  1184. (delete-char 1)
  1185. (insert cur)))
  1186. (defun my-dired-mark (arg)
  1187. "toggle mark the current (or next ARG) files.
  1188. If on a subdir headerline, mark all its files except `.' and `..'.
  1189. Use \\[dired-unmark-all-files] to remove all marks
  1190. and \\[dired-unmark] on a subdir to remove the marks in
  1191. this subdir."
  1192. (interactive "P")
  1193. (if (dired-get-subdir)
  1194. (save-excursion (dired-mark-subdir-files))
  1195. (let ((inhibit-read-only t))
  1196. (dired-repeat-over-lines
  1197. (prefix-numeric-value arg)
  1198. 'my-dired-toggle-mark))))
  1199. (defun my-dired-mark-backward (arg)
  1200. "In Dired, move up lines and toggle mark there.
  1201. Optional prefix ARG says how many lines to unflag; default is one line."
  1202. (interactive "p")
  1203. (my-dired-mark (- arg)))
  1204. (defun dired-mode-hooks()
  1205. (local-set-key (kbd "SPC") 'my-dired-mark)
  1206. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1207. (add-hook 'dired-mode-hook 'dired-mode-hooks)
  1208. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1209. ;; eshell
  1210. (defun my-eshell-backward-delete-char ()
  1211. (interactive)
  1212. (when (< (save-excursion
  1213. (eshell-bol)
  1214. (point))
  1215. (point))
  1216. (backward-delete-char 1)))
  1217. (defvar my-eshell-frame-buffer-alist nil)
  1218. (defun my-eshell-frame-buffer (frame)
  1219. "get buffer associated with FRAME. if buffer doesnt exist or killed, return nil."
  1220. (let ((bf (cdr (assq frame my-eshell-frame-buffer-alist))))
  1221. (and bf ;関連付けられたバッファが存在し
  1222. (buffer-name bf) ;かつkillされてない
  1223. bf)))
  1224. (add-hook 'eshell-mode-hook
  1225. (lambda ()
  1226. (add-to-list 'my-eshell-frame-buffer-alist
  1227. (cons (selected-frame) (current-buffer)))))
  1228. (defun my-file-owner-p (file)
  1229. "t if FILE is owned by me."
  1230. (eq (user-uid) (nth 2 (file-attributes file))))
  1231. ;; ;; http://www.bookshelf.jp/pukiwiki/pukiwiki.php?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9
  1232. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1233. ;; (defun eshell/less (&rest args)
  1234. ;; "Invoke `view-file' on the file.
  1235. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1236. ;; (if args
  1237. ;; (while args
  1238. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1239. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1240. ;; (file (pop args)))
  1241. ;; (view-file file)
  1242. ;; (goto-line line))
  1243. ;; (view-file (pop args))))))
  1244. ;; (defun eshell/git (&rest args)
  1245. ;; ""
  1246. ;; (eshell-parse-arguments (point-at-bol) (point-at-eol)))
  1247. (defun eshell/o (&optional file)
  1248. (my-x-open (or file ".")))
  1249. ;; (defun eshell/vi (&rest args)
  1250. ;; "Invoke `find-file' on the file.
  1251. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1252. ;; (while args
  1253. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1254. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1255. ;; (file (pop args)))
  1256. ;; (find-file file)
  1257. ;; (goto-line line))
  1258. ;; (find-file (pop args)))))
  1259. (defun eshell/clear ()
  1260. "Clear the current buffer, leaving one prompt at the top."
  1261. (let ((inhibit-read-only t))
  1262. (erase-buffer)))
  1263. (defun eshell/d (&optional dirname switches)
  1264. "if first arg is omitted open current directory."
  1265. (dired (or dirname ".") switches))
  1266. (defun eshell/v ()
  1267. (view-mode 1))
  1268. (defalias 'eshell/: 'ignore)
  1269. (defalias 'eshell/type 'eshell/which)
  1270. ;; (defalias 'eshell/vim 'eshell/vi)
  1271. (defalias 'eshell/ff 'find-file)
  1272. (defalias 'eshell/q 'eshell/exit)
  1273. (defun eshell-goto-prompt ()
  1274. ""
  1275. (interactive)
  1276. (goto-char (point-max)))
  1277. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1278. "open eshell and change wd
  1279. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1280. (interactive)
  1281. (let ((dir (expand-file-name default-directory)))
  1282. (switch-to-buffer (or eshell-buffer-or-name
  1283. (eshell t)))
  1284. (unless (equal dir (expand-file-name default-directory))
  1285. ;; (cd dir)
  1286. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1287. ;; (eshell-emit-prompt)
  1288. (goto-char (point-max))
  1289. (eshell-kill-input)
  1290. (insert "cd " dir)
  1291. (eshell-send-input))))
  1292. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1293. (setq eshell-scroll-to-bottom-on-input t)
  1294. (setq eshell-cmpl-ignore-case t)
  1295. (setq eshell-cmpl-cycle-completions nil)
  1296. (setq eshell-highlight-prompt nil)
  1297. (setq eshell-ls-initial-args "-FG") ; "-hF")
  1298. (setq eshell-prompt-function
  1299. (lambda ()
  1300. (with-temp-buffer
  1301. (let (p1 p2 p3 p4)
  1302. (insert " [")
  1303. (setq p1 (point))
  1304. (insert (abbreviate-file-name default-directory))
  1305. (setq p2 (point))
  1306. (insert "]"
  1307. "\n")
  1308. (setq p3 (point))
  1309. (insert user-login-name
  1310. "@"
  1311. (or (getenv "HOSTNAME")
  1312. (substring (shell-command-to-string (or (executable-find "hostname")
  1313. "echo ''"))
  1314. 0
  1315. -1)))
  1316. (setq p4 (point))
  1317. (insert " "
  1318. (format-time-string "%a, %d %b %Y %T %z")
  1319. " eshell\n"
  1320. "last:"
  1321. (number-to-string eshell-last-command-status)
  1322. (if (= (user-uid)
  1323. 0)
  1324. " # "
  1325. " $ "))
  1326. (add-text-properties p1
  1327. p2
  1328. '(face ((foreground-color . "yellow"))))
  1329. (add-text-properties p3
  1330. p4
  1331. '(face ((foreground-color . "cyan"))))
  1332. (buffer-substring (point-min)
  1333. (point-max))))))
  1334. (add-hook 'eshell-mode-hook
  1335. (lambda ()
  1336. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1337. ;; (interactive)
  1338. ;; (switch-to-buffer (other-buffer))))
  1339. (define-key eshell-mode-map (kbd "C-u") (lambda ()
  1340. (interactive)
  1341. (eshell-goto-prompt)
  1342. (eshell-kill-input)))
  1343. (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1344. (interactive)
  1345. (eshell-goto-prompt)
  1346. (my-keyboard-quit)))
  1347. (define-key eshell-mode-map (kbd "DEL") 'my-eshell-backward-delete-char)
  1348. (define-key eshell-mode-map (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1349. (define-key eshell-mode-map (kbd "C-n") 'eshell-next-matching-input-from-input)
  1350. (mapcar (lambda (alias)
  1351. (add-to-list 'eshell-command-aliases-list
  1352. alias))
  1353. '(("ll" "ls -l")
  1354. ("la" "ls -a")
  1355. ("lla" "ls -al")
  1356. ("ut" "slogin 03110414@un001.ecc.u-tokyo.ac.jp")
  1357. ("aptin" "sudo apt-get install")
  1358. ("u" "uname")
  1359. ("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*\"))")
  1360. ("g" "git")))
  1361. (add-to-list 'eshell-output-filter-functions 'eshell-truncate-buffer)
  1362. (apply 'eshell/addpath exec-path)
  1363. (set (make-variable-buffer-local 'scroll-margin) 0)
  1364. (eshell/export "GIT_PAGER=")
  1365. (eshell/export "GIT_EDITOR=")
  1366. (eshell/export "LC_MESSAGES=C")
  1367. (eshell/export "TERM=xterm")
  1368. ))
  1369. ;; (eval-after-load "em-alias"
  1370. ;; '(progn ;; (eshell/alias "ll" "ls -l")
  1371. ;; ;; (eshell/alias "la" "ls -a")
  1372. ;; ;; (eshell/alias "lla" "ls -al")
  1373. ;; (eshell/alias "sgcc" (if (eq system-type 'windows-nt)
  1374. ;; "gcc -o win.$1.exe $1"
  1375. ;; "gcc -o ${uname}.$1.out $1"))
  1376. ;; (eshell/alias "slmgcc" (if (eq system-type 'windows-nt)
  1377. ;; "gcc -lm -o win.$1.exe $1"
  1378. ;; "gcc -lm -o ${uname}.$1.out $1"))
  1379. ;; ;; (eshell/alias "ut" "ssh g841105@un001.ecc.u-tokyo.ac.jp")
  1380. ;; (add-to-list 'recentf-exclude (concat eshell-directory-name "alias"))))
  1381. ;; (define-key my-prefix-map (kbd "C-s") (lambda ()
  1382. ;; (interactive)
  1383. ;; (eshell-cd-default-directory (buffer-name (or (my-eshell-frame-buffer (selected-frame))
  1384. ;; (eshell t))))))
  1385. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1386. ;; 最終更新日時を得る
  1387. (defvar my-buffer-file-last-modified-time nil "")
  1388. (make-variable-buffer-local 'my-buffer-file-last-modified-time)
  1389. (defun my-get-file-last-modified-time (file)
  1390. ""
  1391. (nth 5
  1392. (file-attributes file)))
  1393. (defun my-set-buffer-file-last-modified-time ()
  1394. ""
  1395. (make-local-variable 'my-buffer-file-last-modified-time)
  1396. (setq my-buffer-file-last-modified-time
  1397. (format-time-string "%Y/%m/%d %H:%M" (my-get-file-last-modified-time buffer-file-name))))
  1398. (add-hook 'find-file-hook
  1399. 'my-set-buffer-file-last-modified-time)
  1400. (add-hook 'after-save-hook
  1401. 'my-set-buffer-file-last-modified-time)
  1402. (add-hook 'after-revert-hook
  1403. 'my-set-buffer-file-last-modified-time)
  1404. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1405. ;; auto saving
  1406. (defun my-save-this-buffer (silent-p)
  1407. "save current buffer if can without asking"
  1408. (let ((cm (if (current-message)
  1409. (format "%s\n" (current-message))
  1410. ""))
  1411. (fun (symbol-function (if silent-p
  1412. 'ignore
  1413. 'message))))
  1414. (cond ((active-minibuffer-window) nil)
  1415. ((not buffer-file-name) (funcall fun "%ssaving... this buffer doesn't visit any file." cm) nil)
  1416. ((not (file-exists-p buffer-file-name)) (funcall fun "%ssaving... file not exist. save manually first." com) nil)
  1417. (buffer-read-only (funcall fun "%ssaving... this buffer is read-only." cm) nil)
  1418. ((not (buffer-modified-p)) (funcal fun "%ssaving... not modified yet." cm) nil)
  1419. ((not (file-writable-p buffer-file-name)) (funcall fun "%ssaving... you cannot change this file." cm) nil)
  1420. (t (funcall fun "%ssaving..." cm)
  1421. (save-buffer)
  1422. (funcall fun "%ssaving... done." cm)))))
  1423. ;; (if (and buffer-file-name
  1424. ;; (not buffer-read-only)
  1425. ;; (buffer-modified-p)
  1426. ;; (file-writable-p buffer-file-name))
  1427. ;; (save-buffer))) ; 静かな方
  1428. (defvar my-auto-save-this-buffer nil "auto save timer object")
  1429. (defun my-auto-save-this-buffer (sec &optional silent-p)
  1430. "auto save current buffer if idle for SEC.
  1431. when SEC is nil, stop auto save if enabled."
  1432. (if sec
  1433. (progn (when my-auto-save-this-buffer
  1434. (cancel-timer my-auto-save-this-buffer)
  1435. (setq my-auto-save-this-buffer nil))
  1436. (setq my-auto-save-this-buffer (run-with-idle-timer sec t 'my-save-this-buffer silent-p)))
  1437. (when my-auto-save-this-buffer
  1438. (cancel-timer my-auto-save-this-buffer)
  1439. (setq my-auto-save-this-buffer nil))))
  1440. (my-auto-save-this-buffer 2 t)
  1441. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1442. ;; misc funcs
  1443. (defvar my-desktop-terminal "roxterm")
  1444. (defun my-execute-terminal ()
  1445. ""
  1446. (interactive)
  1447. (if (and (or (eq system-type 'windows-nt)
  1448. window-system)
  1449. my-desktop-terminal
  1450. )
  1451. (let ((process-environment (cons "TERM=xterm" process-environment)))
  1452. (start-process "terminal"
  1453. nil
  1454. my-desktop-terminal))
  1455. (my-execute-or-find-term)))
  1456. (defun my-term ()
  1457. "open terminal buffer and return that buffer."
  1458. (interactive)
  1459. (if (eq system-type 'windows-nt)
  1460. (eshell t)
  1461. (if (featurep 'multi-term)
  1462. (multi-term)
  1463. (ansi-term "/bin/bash"))))
  1464. (defvar my-frame-term-plist nil)
  1465. ;; (setplist my-frame-term-plist nil)
  1466. (defun my-execute-or-find-term ()
  1467. ""
  1468. (interactive)
  1469. (let* ((buf (plist-get my-frame-term-plist (selected-frame))))
  1470. (if (and buf
  1471. (buffer-name buf))
  1472. (switch-to-buffer buf)
  1473. (setq my-frame-term-plist
  1474. (plist-put my-frame-term-plist
  1475. (selected-frame)
  1476. (my-term))))))
  1477. (defun my-format-time-string (&optional time)
  1478. ""
  1479. (let ((system-time-locale "C"))
  1480. (format-time-string "%a, %d %b %Y %T" time)))
  1481. (defvar my-filer nil)
  1482. (setq my-filer (or (executable-find "pcmanfm")
  1483. (executable-find "nautilus")))
  1484. (defun my-x-open (file)
  1485. "open file."
  1486. (interactive "FOpen File: ")
  1487. (setq file (expand-file-name file))
  1488. (message "Opening %s..." file)
  1489. (cond ((eq system-type 'windows-nt)
  1490. (call-process "cmd.exe" nil 0 nil "/c" "start" "" (convert-standard-filename file)))
  1491. ((eq system-type 'darwin)
  1492. (call-process "open" nil 0 nil file))
  1493. ((not (getenv "DESKTOP_SESSION"))
  1494. (find-file file))
  1495. (t
  1496. (if (file-directory-p file)
  1497. (call-process my-filer nil 0 nil file)
  1498. (call-process "xdg-open" nil 0 nil file))))
  1499. (recentf-add-file file)
  1500. (message "Opening %s...done" file))
  1501. (defvar my-auto-indent-buffer-mode-list
  1502. '(emacs-lisp-mode
  1503. sh-mode
  1504. js-mode
  1505. sgml-mode
  1506. c-mode
  1507. c++-mode))
  1508. (setq my-auto-indent-buffer-mode-list nil) ;disable
  1509. (defun my-indent-buffer ()
  1510. "indent whole buffer."
  1511. (interactive)
  1512. (indent-region (point-min)
  1513. (point-max)))
  1514. (defun my-auto-indent-buffer ()
  1515. ""
  1516. (when (memq major-mode my-auto-indent-buffer-mode-list)
  1517. (my-indent-buffer)))
  1518. (add-hook 'before-save-hook
  1519. 'my-auto-indent-buffer)
  1520. (defun my-keyboard-quit ()
  1521. ""
  1522. (interactive)
  1523. (run-hooks 'before-keyboard-quit-hook)
  1524. ;; (redisplay t)
  1525. (redraw-display)
  1526. ;; (run-hooks 'window-configuration-change-hook)
  1527. (my-revert-buffer-if-needed)
  1528. ;; (revert-buffer t t)
  1529. (keyboard-quit)
  1530. (insert "insert me")
  1531. (run-hooks 'after-keyboard-quit-hook))
  1532. (substitute-key-definition 'keyboard-quit 'my-keyboard-quit global-map)
  1533. ;; (global-set-key (kbd "C-g") 'my-keyboard-quit)
  1534. (defun my-convmv-sjis2utf8-test ()
  1535. "run `convmv -r -f sjis -t utf8 *'
  1536. this is test, does not rename files"
  1537. (interactive)
  1538. (shell-command "convmv -r -f sjis -t utf8 *"))
  1539. (defun my-convmv-sjis2utf8-notest ()
  1540. "run `convmv -r -f sjis -t utf8 * --notest'"
  1541. (interactive)
  1542. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  1543. (defun my-copy-whole-line ()
  1544. ""
  1545. (interactive)
  1546. (kill-new (concat (buffer-substring (point-at-bol)
  1547. (point-at-eol))
  1548. "\n")))
  1549. (defun kill-ring-save-buffer-file-name ()
  1550. "get current filename"
  1551. (interactive)
  1552. (let ((file buffer-file-name))
  1553. (if file
  1554. (progn (kill-new file)
  1555. (message file))
  1556. (message "not visiting file."))))
  1557. ;; ;; コマンド的な
  1558. ;; (defvar my-execute-func-list nil "func list")
  1559. ;; (defvar my-execute-func-hist-list nil "func hist list")
  1560. ;; (setq my-execute-func-list '("(call-interactively 'my-francaiscd-b)"
  1561. ;; "(call-interactively 'my-francaiscd-a)"
  1562. ;; "parsec47"
  1563. ;; "chromium-browser"
  1564. ;; "inkscape"
  1565. ;; "audacious"
  1566. ;; "gnome-terminal"
  1567. ;; "zkaicd.py"
  1568. ;; "glchess"))
  1569. ;; (defun my-execute-start-process-or-eval-sexp ()
  1570. ;; "execute something"
  1571. ;; (interactive)
  1572. ;; (let ((func (completing-read "command?: " my-execute-func-list nil nil "" my-execute-func-hist-list)))
  1573. ;; (if (string= "(" (substring func 0 1))
  1574. ;; (with-temp-buffer (insert func)
  1575. ;; (eval-buffer))
  1576. ;; (start-process "ps"
  1577. ;; nil
  1578. ;; func))))
  1579. (defvar my-revert-buffer-if-needed-last-buffer nil)
  1580. (defun my-revert-buffer-if-needed ()
  1581. ""
  1582. (interactive)
  1583. (unless (eq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1584. (setq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1585. (when (or (eq major-mode 'dired-mode)
  1586. (not (verify-visited-file-modtime (current-buffer))))
  1587. (revert-buffer t t)
  1588. (message "%s reverted." (buffer-name)))))
  1589. (add-hook 'post-command-hook ; 'window-configuration-change-hook
  1590. 'my-revert-buffer-if-needed)
  1591. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1592. ;; forked from http://d.hatena.ne.jp/khiker/20100119/window_resize
  1593. (define-key my-prefix-map (kbd "C-w") 'my-window-organizer)
  1594. (defun my-window-organizer ()
  1595. "Control window size and position."
  1596. (interactive)
  1597. (save-selected-window
  1598. (select-window (window-at 0 0))
  1599. (let ( ;; (window-obj (selected-window))
  1600. ;; (current-width (window-width))
  1601. ;; (current-height (window-height))
  1602. action
  1603. c)
  1604. (catch 'end-flag
  1605. (while t
  1606. (setq action
  1607. (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."
  1608. (window-width)
  1609. (window-height))))
  1610. (setq c (aref action 0))
  1611. (cond ((= c ?l)
  1612. (unless (eq (window-width) (frame-width))
  1613. (enlarge-window-horizontally 1)))
  1614. ((= c ?h)
  1615. (unless (eq (window-width) (frame-width))
  1616. (shrink-window-horizontally 1)))
  1617. ((= c ?j)
  1618. (enlarge-window 1))
  1619. ((= c ?k)
  1620. (shrink-window 1))
  1621. ((= c ?o)
  1622. (other-window 1))
  1623. ((memq c '(?d ?0))
  1624. (unless (eq (selected-window) (next-window (selected-window) 0 1))
  1625. (delete-window (selected-window))))
  1626. ((= c ?1)
  1627. (delete-other-windows))
  1628. ((= c ?2)
  1629. (split-window-vertically))
  1630. ((= c ?3)
  1631. (split-window-horizontally))
  1632. ((memq c '(?q ?\C-g))
  1633. (message "Quit")
  1634. (throw 'end-flag t))
  1635. (t
  1636. (beep))))))))
  1637. ;; (aref (read-key-sequence-vector "aa") 0)
  1638. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1639. ;; save and restore frame size
  1640. ;;http://www.bookshelf.jp/soft/meadow_30.html#SEC416
  1641. (defun my-window-size-save ()
  1642. (let* ((rlist (frame-parameters (selected-frame)))
  1643. (ilist initial-frame-alist)
  1644. (nCHeight (frame-height))
  1645. (nCWidth (frame-width))
  1646. (tMargin (if (integerp (cdr (assoc 'top rlist)))
  1647. (cdr (assoc 'top rlist)) 0))
  1648. (lMargin (if (integerp (cdr (assoc 'left rlist)))
  1649. (cdr (assoc 'left rlist)) 0))
  1650. buf
  1651. (file "~/.emacs.d/.framesize.el")
  1652. (recentf-exclude '("\\.emacs\\.d/\\.framesize\\.el$")))
  1653. (if (get-file-buffer (expand-file-name file))
  1654. (setq buf (get-file-buffer (expand-file-name file)))
  1655. (setq buf (find-file-noselect file)))
  1656. (set-buffer buf)
  1657. (erase-buffer)
  1658. (insert (concat
  1659. ;; 初期値をいじるよりも modify-frame-parameters
  1660. ;; で変えるだけの方がいい?
  1661. "(delete 'width default-frame-alist)\n"
  1662. "(delete 'height default-frame-alist)\n"
  1663. "(delete 'top default-frame-alist)\n"
  1664. "(delete 'left default-frame-alist)\n"
  1665. "(setq default-frame-alist (append (list\n"
  1666. "'(width . " (int-to-string nCWidth) ")\n"
  1667. "'(height . " (int-to-string nCHeight) ")\n"
  1668. "'(top . " (int-to-string tMargin) ")\n"
  1669. "'(left . " (int-to-string lMargin) "))\n"
  1670. "default-frame-alist))\n"
  1671. ;;"(setq default-frame-alist default-frame-alist)"
  1672. ))
  1673. (save-buffer)
  1674. ))
  1675. (defun my-window-size-load ()
  1676. (let* ((file "~/.emacs.d/.framesize.el"))
  1677. (if (file-exists-p file)
  1678. (load file))))
  1679. (when window-system
  1680. (my-window-size-load)
  1681. (add-hook 'after-init-hook ;何かがframeの大きさ勝手に変えやがる
  1682. (lambda ()
  1683. (run-with-timer 1
  1684. nil
  1685. (lambda ()
  1686. (modify-frame-parameters (selected-frame)
  1687. default-frame-alist))))
  1688. t)
  1689. (add-hook 'kill-emacs-hook
  1690. 'my-window-size-save))
  1691. ;; windowサイズを固定
  1692. ;; setq default-frame-alist
  1693. ;; (append (list '(width . 80)
  1694. ;; '(height . 35)
  1695. ;; )
  1696. ;; default-frame-alist)
  1697. ;; ) ;;デフォルトのフレーム設定
  1698. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1699. ;; ;; emacsを殺伐とさせる
  1700. ;; ;; 補完させるとき失敗するからなし
  1701. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  1702. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  1703. ;; (setq arg
  1704. ;; (concat arg
  1705. ;; (if (eq nil (string-match "\\. *$" arg)) ".")
  1706. ;; " And You are a Coward!")))
  1707. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1708. ;; ubuntu
  1709. (defun my-load-scim ()
  1710. "use scim-bridge.el as japanese im."
  1711. ;; Load scim-bridge.
  1712. (require 'scim-bridge)
  1713. ;; Turn on scim-mode automatically after loading .emacs
  1714. (add-hook 'after-init-hook 'scim-mode-on)
  1715. (setq scim-cursor-color "red")
  1716. (scim-define-preedit-key ?\^h t)
  1717. (scim-define-common-key ?\* nil)
  1718. (scim-define-common-key ?\^/ nil))
  1719. (defun my-load-anthy ()
  1720. "use anthy.el as japanese im."
  1721. ;; anthy
  1722. (require 'anthy)
  1723. (global-set-key [muhenkan] (lambda () (interactive) (anthy-mode-off)))
  1724. (global-set-key [henkan] (lambda () (interactive) (anthy-mode-on)))
  1725. (when (>= emacs-major-version 23)
  1726. (setq anthy-accept-timeout 1)))
  1727. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1728. ;; windows用設定
  1729. ;; (add-to-list 'exec-path "c:/Program Files/Gauche/bin/")
  1730. (defun start-ckw-bash ()
  1731. ""
  1732. (interactive)
  1733. (start-process "ckw_bash"
  1734. nil
  1735. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe")) ; cじゃないといけないらしい
  1736. (defun my-w32-add-export-path (&rest args)
  1737. ""
  1738. (mapcar (lambda (path)
  1739. (add-to-list 'exec-path (expand-file-name path)))
  1740. (reverse args))
  1741. (setenv "PATH"
  1742. (mapconcat 'convert-standard-filename
  1743. exec-path
  1744. ";")))
  1745. (when (eq system-type 'windows-nt)
  1746. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  1747. ;; (setq python-python-command "c:/Python26/python.exe")
  1748. (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  1749. (my-w32-add-export-path "c:/WINDOWS"
  1750. (expand-file-name "~/bin")
  1751. (expand-file-name "~/dbx/apps/bin"))
  1752. (when window-system
  1753. (setq w32-enable-synthesized-fonts t))
  1754. (setq file-name-coding-system 'sjis))