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.
 
 
 
 
 
 

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