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.
 
 
 
 
 
 

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