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.
 
 
 
 
 
 

2056 lines
73 KiB

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