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.
 
 
 
 
 
 

2040 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 : "
  975. dir-default
  976. nil
  977. nil
  978. archive-default)
  979. ;; (concat dir-default archive-default)
  980. ))
  981. (apply 'my-pack
  982. archive
  983. infiles))))
  984. (revert-buffer)
  985. ;; (dired-unmark-all-marks)
  986. )
  987. (defun my-pack-file-name-association (filename)
  988. ""
  989. (let ((case-fold-search nil))
  990. (assoc-default filename
  991. my-pack-program-alist
  992. 'string-match-p
  993. nil)))
  994. (defun my-file-name-extension-with-tar (filename)
  995. "if FILENAME has extension with tar, like \"tar.gz\", return that.
  996. otherwise, return extension normally."
  997. (if (string-equal "tar" (file-name-extension (file-name-sans-extension filename)))
  998. (concat "tar."
  999. (file-name-extension filename))
  1000. (file-name-extension filename)))
  1001. (defun my-pack-file-extension (filename)
  1002. "if FILENAME has extension and it can be used for pack, return FILENAME.
  1003. otherwise, return FILENAME with `my-pack-default-extension'"
  1004. (if (my-pack-file-name-association filename)
  1005. filename
  1006. (concat filename "." my-pack-default-extension)))
  1007. (defvar my-7z-program-name
  1008. (or (executable-find "7z")
  1009. (executable-find "7za")
  1010. (executable-find "7zr"))
  1011. "path to 7z program.")
  1012. (defvar my-pack-default-extension
  1013. "7z"
  1014. "default suffix for packing. filename with this suffix must matches `pack-program-alist'")
  1015. (defvar my-pack-program-alist
  1016. `(("\\.7z\\'" ,(concat my-7z-program-name " a"), (concat my-7z-program-name " x"))
  1017. ("\\.tar\\'" "tar cf" "tar xf")
  1018. ("\\.tgz\\'" "tar czf" "tar xzf")
  1019. ("\\.zip\\'" "zip -r" "unzip")))
  1020. ;; (string-match-p "\\.gz\\'" "aaa.gz") ; \' matches string end, $ also matches the point before newline.
  1021. (defun my-unpack (archive)
  1022. "unpack ARCHIVE. command for unpacking is defined in `pack-program-alist'"
  1023. (interactive "fArchive to extract: ")
  1024. (let* ((earchive (expand-file-name archive))
  1025. (lst (my-pack-file-name-association earchive))
  1026. )
  1027. (if lst
  1028. (shell-command (concat (nth 1
  1029. lst)
  1030. " "
  1031. (shell-quote-argument earchive)))
  1032. (message "this is not archive file defined in `pack-program-alist'!"))))
  1033. (defun my-pack (archive &rest files)
  1034. "pack FILES into ARCHIVE.
  1035. if ARCHIVE have extension defined in `pack-program-alist', use that command.
  1036. otherwise, use `pack-default-extension' for pack."
  1037. (let* ((archive-ext (my-pack-file-extension (expand-file-name archive)))
  1038. (lst (my-pack-file-name-association archive-ext))
  1039. )
  1040. (if lst
  1041. (shell-command (concat (nth 0 lst)
  1042. " "
  1043. (shell-quote-argument archive-ext)
  1044. " "
  1045. (mapconcat 'shell-quote-argument
  1046. files
  1047. " ")))
  1048. (message "invalid extension for packing!"))))
  1049. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1050. "pop up buffer using `display-buffer' and return that buffer."
  1051. (let ((bf (get-buffer-create buffer-or-name)))
  1052. (with-current-buffer bf
  1053. (cd ".")
  1054. (erase-buffer))
  1055. (display-buffer bf)
  1056. bf))
  1057. (defun my-replace-nasi-none ()
  1058. ""
  1059. (save-excursion
  1060. (let ((buffer-read-only nil))
  1061. (goto-char (point-min))
  1062. (while (search-forward "なし" nil t)
  1063. (replace-match "none")))))
  1064. (defun dired-get-du () ;em-unix.el使えるかも
  1065. "dired get disk usage"
  1066. (interactive)
  1067. (message "calculating du...")
  1068. (dired-do-shell-command "du -hs * " nil (dired-get-marked-files)))
  1069. (defun my-dired-scroll-up ()
  1070. ""
  1071. (interactive)
  1072. (my-dired-previous-line (- (window-height) 1)))
  1073. (defun my-dired-scroll-down ()
  1074. ""
  1075. (interactive)
  1076. (my-dired-next-line (- (window-height) 1)))
  1077. (defun my-dired-previous-line (&optional arg)
  1078. ""
  1079. (interactive)
  1080. (dired-previous-line (or arg 1))
  1081. (my-dired-print-current-dir-and-file))
  1082. (defun my-dired-next-line (&optional arg)
  1083. ""
  1084. (interactive)
  1085. (dired-next-line (or arg 1))
  1086. (my-dired-print-current-dir-and-file))
  1087. (defun my-dired-print-current-dir-and-file ()
  1088. (message "%s %s"
  1089. default-directory
  1090. (buffer-substring-no-properties (point-at-bol)
  1091. (point-at-eol))))
  1092. (defun dired-do-execute-as-command ()
  1093. ""
  1094. (interactive)
  1095. (let ((file (dired-get-filename t)))
  1096. (if (file-executable-p file)
  1097. (start-process file nil file)
  1098. (when (y-or-n-p "this file cant be executed. mark as executable and go? : ")
  1099. (set-file-modes file (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1100. (start-process file nil file)))))
  1101. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1102. (defun my-dired-x-open ()
  1103. ""
  1104. (interactive)
  1105. (my-x-open (dired-get-filename t t)))
  1106. (defun my-dired-up-directory ()
  1107. ""
  1108. (interactive)
  1109. (my-dired-find-file ".."))
  1110. (defun my-dired-find-file (&optional filename)
  1111. "if the file to open is a directory, kill current buffer after opening that file."
  1112. (interactive)
  1113. (let ((f (expand-file-name (or filename
  1114. (dired-get-filename))))
  1115. (bf (current-buffer)))
  1116. (find-file f)
  1117. (when (and (file-directory-p f)
  1118. (not (get-buffer-window bf)))
  1119. (kill-buffer bf))))
  1120. (setq dired-listing-switches "-lhFG --time-style=long-iso")
  1121. (define-minor-mode my-dired-display-all-mode
  1122. ""
  1123. :init-value nil
  1124. :global t
  1125. (my-dired-display-all-set)
  1126. (when (eq major-mode 'dired-mode)
  1127. (revert-buffer)))
  1128. (defun my-dired-display-all-set ()
  1129. ""
  1130. (if my-dired-display-all-mode
  1131. (setq dired-actual-switches
  1132. (concat "-A "
  1133. dired-actual-switches))
  1134. (setq dired-actual-switches
  1135. (replace-regexp-in-string "-A " "" dired-actual-switches))))
  1136. (add-hook 'dired-mode-hook
  1137. 'my-dired-display-all-set)
  1138. (put 'dired-find-alternate-file 'disabled nil)
  1139. (require 'ls-lisp)
  1140. ;; (setq ls-lisp-use-insert-directory-program nil)
  1141. (setq ls-lisp-dirs-first t)
  1142. (setq dired-ls-F-marks-symlinks t)
  1143. (setq dired-dwim-target t)
  1144. ;; (add-hook 'dired-after-readin-hook
  1145. ;; 'my-replace-nasi-none)
  1146. (add-hook 'after-init-hook
  1147. (lambda ()
  1148. (dired ".")))
  1149. (add-hook 'dired-mode-hook
  1150. (lambda ()
  1151. (define-key dired-mode-map "o" 'my-dired-x-open)
  1152. (define-key dired-mode-map "i" 'dired-get-du)
  1153. (define-key dired-mode-map "!" 'shell-command)
  1154. (define-key dired-mode-map "&" 'async-shell-command)
  1155. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1156. (define-key dired-mode-map "=" 'my-dired-diff)
  1157. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1158. (define-key dired-mode-map "b" 'gtkbm)
  1159. (define-key dired-mode-map "@" (lambda () (interactive) (my-x-open ".")))
  1160. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1161. (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1162. (define-key dired-mode-map "a" 'my-dired-display-all-mode)
  1163. (define-key dired-mode-map "h" 'my-dired-display-all-mode)
  1164. (substitute-key-definition 'dired-advertised-find-file 'my-dired-find-file dired-mode-map)
  1165. (substitute-key-definition 'dired-up-directory 'my-dired-up-directory dired-mode-map)
  1166. (define-key dired-mode-map (kbd "DEL") 'my-dired-up-directory)
  1167. (substitute-key-definition 'dired-next-line 'my-dired-next-line dired-mode-map)
  1168. (substitute-key-definition 'dired-previous-line 'my-dired-previous-line dired-mode-map)
  1169. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1170. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1171. (let ((file "._Icon\015"))
  1172. (when (file-readable-p file)
  1173. (delete-file file)))))
  1174. ;; http://homepage1.nifty.com/blankspace/emacs/dired.html
  1175. ;; (add-hook 'dired-load-hook
  1176. ;; (lambda ()
  1177. ;; (load-library "ls-lisp")
  1178. ;; (setq ls-lisp-dirs-first t)
  1179. ;; (setq dired-listing-switches "-alhF"))) ;これ書く場所間違えてね?
  1180. ;; (defadvice dired-next-line (after dired-next-line-print-directory activate)
  1181. ;; "print current directory when go down line"
  1182. ;; (dired-print-current-dir-and-file))
  1183. ;; (defadvice dired-previous-line (after dired-previous-line-print-directory activate)
  1184. ;; "print current directory when go up line"
  1185. ;; (dired-print-current-dir-and-file))
  1186. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1187. (defun my-dired-toggle-mark()
  1188. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1189. (t dired-marker-char))))
  1190. (delete-char 1)
  1191. (insert cur)))
  1192. (defun my-dired-mark (arg)
  1193. "toggle mark the current (or next ARG) files.
  1194. If on a subdir headerline, mark all its files except `.' and `..'.
  1195. Use \\[dired-unmark-all-files] to remove all marks
  1196. and \\[dired-unmark] on a subdir to remove the marks in
  1197. this subdir."
  1198. (interactive "P")
  1199. (if (dired-get-subdir)
  1200. (save-excursion (dired-mark-subdir-files))
  1201. (let ((inhibit-read-only t))
  1202. (dired-repeat-over-lines
  1203. (prefix-numeric-value arg)
  1204. 'my-dired-toggle-mark))))
  1205. (defun my-dired-mark-backward (arg)
  1206. "In Dired, move up lines and toggle mark there.
  1207. Optional prefix ARG says how many lines to unflag; default is one line."
  1208. (interactive "p")
  1209. (my-dired-mark (- arg)))
  1210. (defun dired-mode-hooks()
  1211. (local-set-key (kbd "SPC") 'my-dired-mark)
  1212. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1213. (add-hook 'dired-mode-hook 'dired-mode-hooks)
  1214. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1215. ;; eshell
  1216. (defun my-eshell-backward-delete-char ()
  1217. (interactive)
  1218. (when (< (save-excursion
  1219. (eshell-bol)
  1220. (point))
  1221. (point))
  1222. (backward-delete-char 1)))
  1223. (defvar my-eshell-frame-buffer-alist nil)
  1224. (defun my-eshell-frame-buffer (frame)
  1225. "get buffer associated with FRAME. if buffer doesnt exist or killed, return nil."
  1226. (let ((bf (cdr (assq frame my-eshell-frame-buffer-alist))))
  1227. (and bf ;関連付けられたバッファが存在し
  1228. (buffer-name bf) ;かつkillされてない
  1229. bf)))
  1230. (add-hook 'eshell-mode-hook
  1231. (lambda ()
  1232. (add-to-list 'my-eshell-frame-buffer-alist
  1233. (cons (selected-frame) (current-buffer)))))
  1234. (defun my-file-owner-p (file)
  1235. "t if FILE is owned by me."
  1236. (eq (user-uid) (nth 2 (file-attributes file))))
  1237. ;; ;; http://www.bookshelf.jp/pukiwiki/pukiwiki.php?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9
  1238. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1239. ;; (defun eshell/less (&rest args)
  1240. ;; "Invoke `view-file' on the file.
  1241. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1242. ;; (if args
  1243. ;; (while args
  1244. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1245. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1246. ;; (file (pop args)))
  1247. ;; (view-file file)
  1248. ;; (goto-line line))
  1249. ;; (view-file (pop args))))))
  1250. ;; (defun eshell/git (&rest args)
  1251. ;; ""
  1252. ;; (eshell-parse-arguments (point-at-bol) (point-at-eol)))
  1253. (defun eshell/o (&optional file)
  1254. (my-x-open (or file ".")))
  1255. ;; (defun eshell/vi (&rest args)
  1256. ;; "Invoke `find-file' on the file.
  1257. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1258. ;; (while args
  1259. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1260. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1261. ;; (file (pop args)))
  1262. ;; (find-file file)
  1263. ;; (goto-line line))
  1264. ;; (find-file (pop args)))))
  1265. (defun eshell/clear ()
  1266. "Clear the current buffer, leaving one prompt at the top."
  1267. (let ((inhibit-read-only t))
  1268. (erase-buffer)))
  1269. (defun eshell/d (&optional dirname switches)
  1270. "if first arg is omitted open current directory."
  1271. (dired (or dirname ".") switches))
  1272. (defun eshell/v ()
  1273. (view-mode 1))
  1274. (defalias 'eshell/: 'ignore)
  1275. (defalias 'eshell/type 'eshell/which)
  1276. ;; (defalias 'eshell/vim 'eshell/vi)
  1277. (defalias 'eshell/ff 'find-file)
  1278. (defalias 'eshell/q 'eshell/exit)
  1279. (defun eshell-goto-prompt ()
  1280. ""
  1281. (interactive)
  1282. (goto-char (point-max)))
  1283. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1284. "open eshell and change wd
  1285. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1286. (interactive)
  1287. (let ((dir (expand-file-name default-directory)))
  1288. (switch-to-buffer (or eshell-buffer-or-name
  1289. (eshell t)))
  1290. (unless (equal dir (expand-file-name default-directory))
  1291. ;; (cd dir)
  1292. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1293. ;; (eshell-emit-prompt)
  1294. (goto-char (point-max))
  1295. (eshell-kill-input)
  1296. (insert "cd " dir)
  1297. (eshell-send-input))))
  1298. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1299. (setq eshell-scroll-to-bottom-on-input t)
  1300. (setq eshell-cmpl-ignore-case t)
  1301. (setq eshell-cmpl-cycle-completions nil)
  1302. (setq eshell-highlight-prompt nil)
  1303. (setq eshell-ls-initial-args "-FG") ; "-hF")
  1304. (setq eshell-prompt-function
  1305. (lambda ()
  1306. (with-temp-buffer
  1307. (let (p1 p2 p3 p4)
  1308. (insert " [")
  1309. (setq p1 (point))
  1310. (insert (abbreviate-file-name default-directory))
  1311. (setq p2 (point))
  1312. (insert "]"
  1313. "\n")
  1314. (setq p3 (point))
  1315. (insert user-login-name
  1316. "@"
  1317. (or (getenv "HOSTNAME")
  1318. (substring (shell-command-to-string (or (executable-find "hostname")
  1319. "echo ''"))
  1320. 0
  1321. -1)))
  1322. (setq p4 (point))
  1323. (insert " "
  1324. (format-time-string "%a, %d %b %Y %T %z")
  1325. " eshell\n"
  1326. "last:"
  1327. (number-to-string eshell-last-command-status)
  1328. (if (= (user-uid)
  1329. 0)
  1330. " # "
  1331. " $ "))
  1332. (add-text-properties p1
  1333. p2
  1334. '(face ((foreground-color . "yellow"))))
  1335. (add-text-properties p3
  1336. p4
  1337. '(face ((foreground-color . "cyan"))))
  1338. (buffer-substring (point-min)
  1339. (point-max))))))
  1340. (add-hook 'eshell-mode-hook
  1341. (lambda ()
  1342. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1343. ;; (interactive)
  1344. ;; (switch-to-buffer (other-buffer))))
  1345. (define-key eshell-mode-map (kbd "C-u") (lambda ()
  1346. (interactive)
  1347. (eshell-goto-prompt)
  1348. (eshell-kill-input)))
  1349. (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1350. (interactive)
  1351. (eshell-goto-prompt)
  1352. (my-keyboard-quit)))
  1353. (define-key eshell-mode-map (kbd "DEL") 'my-eshell-backward-delete-char)
  1354. (define-key eshell-mode-map (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1355. (define-key eshell-mode-map (kbd "C-n") 'eshell-next-matching-input-from-input)
  1356. (mapcar (lambda (alias)
  1357. (add-to-list 'eshell-command-aliases-list
  1358. alias))
  1359. '(("ll" "ls -l")
  1360. ("la" "ls -a")
  1361. ("lla" "ls -al")
  1362. ("ut" "slogin 03110414@un001.ecc.u-tokyo.ac.jp")
  1363. ("aptin" "sudo apt-get install")
  1364. ("u" "uname")
  1365. ("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*\"))")
  1366. ("g" "git")))
  1367. (add-to-list 'eshell-output-filter-functions 'eshell-truncate-buffer)
  1368. (apply 'eshell/addpath exec-path)
  1369. (set (make-variable-buffer-local 'scroll-margin) 0)
  1370. (eshell/export "GIT_PAGER=")
  1371. (eshell/export "GIT_EDITOR=")
  1372. (eshell/export "LC_MESSAGES=C")
  1373. (eshell/export "TERM=xterm")
  1374. )))))
  1375. ;; (eval-after-load "em-alias"
  1376. ;; '(progn ;; (eshell/alias "ll" "ls -l")
  1377. ;; ;; (eshell/alias "la" "ls -a")
  1378. ;; ;; (eshell/alias "lla" "ls -al")
  1379. ;; (eshell/alias "sgcc" (if (eq system-type 'windows-nt)
  1380. ;; "gcc -o win.$1.exe $1"
  1381. ;; "gcc -o ${uname}.$1.out $1"))
  1382. ;; (eshell/alias "slmgcc" (if (eq system-type 'windows-nt)
  1383. ;; "gcc -lm -o win.$1.exe $1"
  1384. ;; "gcc -lm -o ${uname}.$1.out $1"))
  1385. ;; ;; (eshell/alias "ut" "ssh g841105@un001.ecc.u-tokyo.ac.jp")
  1386. ;; (add-to-list 'recentf-exclude (concat eshell-directory-name "alias"))))
  1387. ;; (define-key my-prefix-map (kbd "C-s") (lambda ()
  1388. ;; (interactive)
  1389. ;; (eshell-cd-default-directory (buffer-name (or (my-eshell-frame-buffer (selected-frame))
  1390. ;; (eshell t))))))
  1391. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1392. ;; 最終更新日時を得る
  1393. (defvar my-buffer-file-last-modified-time nil "")
  1394. (make-variable-buffer-local 'my-buffer-file-last-modified-time)
  1395. (defun my-get-file-last-modified-time (file)
  1396. ""
  1397. (nth 5
  1398. (file-attributes file)))
  1399. (defun my-set-buffer-file-last-modified-time ()
  1400. ""
  1401. (make-local-variable 'my-buffer-file-last-modified-time)
  1402. (setq my-buffer-file-last-modified-time
  1403. (format-time-string "%Y/%m/%d %H:%M" (my-get-file-last-modified-time buffer-file-name))))
  1404. (add-hook 'find-file-hook
  1405. 'my-set-buffer-file-last-modified-time)
  1406. (add-hook 'after-save-hook
  1407. 'my-set-buffer-file-last-modified-time)
  1408. (add-hook 'after-revert-hook
  1409. 'my-set-buffer-file-last-modified-time)
  1410. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1411. ;; auto saving
  1412. (defun my-save-this-buffer (silent-p)
  1413. "save current buffer if can without asking"
  1414. (let ((cm (if (current-message)
  1415. (format "%s\n" (current-message))
  1416. ""))
  1417. (fun (symbol-function (if silent-p
  1418. 'ignore
  1419. 'message))))
  1420. (cond ((active-minibuffer-window) nil)
  1421. ((not buffer-file-name) (funcall fun "%ssaving... this buffer doesn't visit any file." cm) nil)
  1422. ((not (file-exists-p buffer-file-name)) (funcall fun "%ssaving... file not exist. save manually first." com) nil)
  1423. (buffer-read-only (funcall fun "%ssaving... this buffer is read-only." cm) nil)
  1424. ((not (buffer-modified-p)) (funcal fun "%ssaving... not modified yet." cm) nil)
  1425. ((not (file-writable-p buffer-file-name)) (funcall fun "%ssaving... you cannot change this file." cm) nil)
  1426. (t (funcall fun "%ssaving..." cm)
  1427. (save-buffer)
  1428. (funcall fun "%ssaving... done." cm)))))
  1429. ;; (if (and buffer-file-name
  1430. ;; (not buffer-read-only)
  1431. ;; (buffer-modified-p)
  1432. ;; (file-writable-p buffer-file-name))
  1433. ;; (save-buffer))) ; 静かな方
  1434. (defvar my-auto-save-this-buffer nil "auto save timer object")
  1435. (defun my-auto-save-this-buffer (secs &optional silent-p)
  1436. "auto save current buffer if idle for SEC.
  1437. when SEC is nil, stop auto save if enabled."
  1438. (if secs
  1439. (progn (when my-auto-save-this-buffer
  1440. (cancel-timer my-auto-save-this-buffer)
  1441. (setq my-auto-save-this-buffer nil))
  1442. (setq my-auto-save-this-buffer (run-with-idle-timer secs t 'my-save-this-buffer silent-p)))
  1443. (when my-auto-save-this-buffer
  1444. (cancel-timer my-auto-save-this-buffer)
  1445. (setq my-auto-save-this-buffer nil))))
  1446. (my-auto-save-this-buffer 2 t)
  1447. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1448. ;; misc funcs
  1449. (defvar my-desktop-terminal "roxterm")
  1450. (defun my-execute-terminal ()
  1451. ""
  1452. (interactive)
  1453. (if (and (or (eq system-type 'windows-nt)
  1454. window-system)
  1455. my-desktop-terminal
  1456. )
  1457. (let ((process-environment (cons "TERM=xterm" process-environment)))
  1458. (start-process "terminal"
  1459. nil
  1460. my-desktop-terminal))
  1461. (my-execute-or-find-term)))
  1462. (defun my-term ()
  1463. ""
  1464. (interactive)
  1465. (if (eq system-type 'windows-nt)
  1466. (eshell t)
  1467. (if (featurep 'multi-term)
  1468. (multi-term)
  1469. (ansi-term "/bin/bash"))))
  1470. (defvar my-frame-term-plist nil)
  1471. (setplist my-frame-term-plist nil)
  1472. (defun my-execute-or-find-term ()
  1473. ""
  1474. (interactive)
  1475. (let* ((buf (get 'my-frame-term-plist (selected-frame))))
  1476. (if (and buf
  1477. (buffer-name buf))
  1478. (switch-to-buffer buf)
  1479. (put 'my-frame-term-plist
  1480. (selected-frame)
  1481. (my-term)))))
  1482. (defun my-format-time-string (&optional time)
  1483. ""
  1484. (let ((system-time-locale "C"))
  1485. (format-time-string "%a, %d %b %Y %T" time)))
  1486. (defvar my-filer nil)
  1487. (setq my-filer (or (executable-find "pcmanfm")
  1488. (executable-find "nautilus")))
  1489. (defun my-x-open (file)
  1490. "open file."
  1491. (interactive "FOpen File: ")
  1492. (setq file (expand-file-name file))
  1493. (message "Opening %s..." file)
  1494. (cond ((eq system-type 'windows-nt)
  1495. (call-process "cmd.exe" nil 0 nil "/c" "start" "" (convert-standard-filename file)))
  1496. ((eq system-type 'darwin)
  1497. (call-process "open" nil 0 nil file))
  1498. ((not (getenv "DESKTOP_SESSION"))
  1499. (find-file file))
  1500. (t
  1501. (if (file-directory-p file)
  1502. (call-process my-filer nil 0 nil file)
  1503. (call-process "xdg-open" nil 0 nil file))))
  1504. (recentf-add-file file)
  1505. (message "Opening %s...done" file))
  1506. (defvar my-auto-indent-buffer-mode-list
  1507. '(emacs-lisp-mode
  1508. sh-mode
  1509. js-mode
  1510. sgml-mode
  1511. c-mode
  1512. c++-mode))
  1513. (setq my-auto-indent-buffer-mode-list nil)
  1514. (defun my-indent-buffer ()
  1515. "indent whole buffer."
  1516. (interactive)
  1517. (indent-region (point-min)
  1518. (point-max)))
  1519. (defun my-auto-indent-buffer ()
  1520. ""
  1521. (when (memq major-mode my-auto-indent-buffer-mode-list)
  1522. (my-indent-buffer)))
  1523. (add-hook 'before-save-hook
  1524. 'my-auto-indent-buffer)
  1525. (defun my-keyboard-quit ()
  1526. ""
  1527. (interactive)
  1528. (run-hooks 'before-keyboard-quit-hook)
  1529. ;; (redisplay t)
  1530. (redraw-display)
  1531. ;; (run-hooks 'window-configuration-change-hook)
  1532. (my-revert-buffer-if-needed)
  1533. ;; (revert-buffer t t)
  1534. (keyboard-quit)
  1535. (insert "insert me")
  1536. (run-hooks 'after-keyboard-quit-hook))
  1537. (substitute-key-definition 'keyboard-quit 'my-keyboard-quit global-map)
  1538. ;; (global-set-key (kbd "C-g") 'my-keyboard-quit)
  1539. (defun my-convmv-sjis2utf8-test ()
  1540. "run `convmv -r -f sjis -t utf8 *'
  1541. this is test, does not rename files"
  1542. (interactive)
  1543. (shell-command "convmv -r -f sjis -t utf8 *"))
  1544. (defun my-convmv-sjis2utf8-notest ()
  1545. "run `convmv -r -f sjis -t utf8 * --notest'"
  1546. (interactive)
  1547. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  1548. (defun my-copy-whole-line ()
  1549. ""
  1550. (interactive)
  1551. (kill-new (concat (buffer-substring (point-at-bol)
  1552. (point-at-eol))
  1553. "\n")))
  1554. (defun kill-ring-save-buffer-file-name ()
  1555. "get current filename"
  1556. (interactive)
  1557. (let ((file buffer-file-name))
  1558. (if file
  1559. (progn (kill-new file)
  1560. (message file))
  1561. (message "not visiting file."))))
  1562. ;; ;; コマンド的な
  1563. ;; (defvar my-execute-func-list nil "func list")
  1564. ;; (defvar my-execute-func-hist-list nil "func hist list")
  1565. ;; (setq my-execute-func-list '("(call-interactively 'my-francaiscd-b)"
  1566. ;; "(call-interactively 'my-francaiscd-a)"
  1567. ;; "parsec47"
  1568. ;; "chromium-browser"
  1569. ;; "inkscape"
  1570. ;; "audacious"
  1571. ;; "gnome-terminal"
  1572. ;; "zkaicd.py"
  1573. ;; "glchess"))
  1574. ;; (defun my-execute-start-process-or-eval-sexp ()
  1575. ;; "execute something"
  1576. ;; (interactive)
  1577. ;; (let ((func (completing-read "command?: " my-execute-func-list nil nil "" my-execute-func-hist-list)))
  1578. ;; (if (string= "(" (substring func 0 1))
  1579. ;; (with-temp-buffer (insert func)
  1580. ;; (eval-buffer))
  1581. ;; (start-process "ps"
  1582. ;; nil
  1583. ;; func))))
  1584. ;; delete-trailing-whitespace
  1585. ;; (defun my-delete-blanks-on-eol ()
  1586. ;; ""
  1587. ;; (interactive)
  1588. ;; (save-excursion
  1589. ;; (goto-char (point-min))
  1590. ;; (while (re-search-forward "[ \t]+$" nil t)
  1591. ;; (replace-match "" nil nil))))
  1592. (defun my-revert-buffer-if-needed ()
  1593. ""
  1594. (interactive)
  1595. (unless (verify-visited-file-modtime (current-buffer))
  1596. (revert-buffer t t)))
  1597. (add-hook 'window-configuration-change-hook
  1598. (lambda ()
  1599. (run-with-timer 0.5
  1600. nil
  1601. 'my-revert-buffer-if-needed)))
  1602. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1603. ;; forked from http://d.hatena.ne.jp/khiker/20100119/window_resize
  1604. (define-key my-prefix-map (kbd "C-w") 'my-window-organizer)
  1605. (defun my-window-organizer ()
  1606. "Control window size and position."
  1607. (interactive)
  1608. (save-selected-window
  1609. (select-window (window-at 0 0))
  1610. (let ( ;; (window-obj (selected-window))
  1611. ;; (current-width (window-width))
  1612. ;; (current-height (window-height))
  1613. action
  1614. c)
  1615. (catch 'end-flag
  1616. (while t
  1617. (setq action
  1618. (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."
  1619. (window-width)
  1620. (window-height))))
  1621. (setq c (aref action 0))
  1622. (cond ((= c ?l)
  1623. (unless (eq (window-width) (frame-width))
  1624. (enlarge-window-horizontally 1)))
  1625. ((= c ?h)
  1626. (unless (eq (window-width) (frame-width))
  1627. (shrink-window-horizontally 1)))
  1628. ((= c ?j)
  1629. (enlarge-window 1))
  1630. ((= c ?k)
  1631. (shrink-window 1))
  1632. ((= c ?o)
  1633. (other-window 1))
  1634. ((memq c '(?d ?0))
  1635. (unless (eq (selected-window) (next-window (selected-window) 0 1))
  1636. (delete-window (selected-window))))
  1637. ((= c ?1)
  1638. (delete-other-windows))
  1639. ((= c ?2)
  1640. (split-window-vertically))
  1641. ((= c ?3)
  1642. (split-window-horizontally))
  1643. ((memq c '(?q ?\C-g))
  1644. (message "Quit")
  1645. (throw 'end-flag t))
  1646. (t
  1647. (beep))))))))
  1648. ;; (aref (read-key-sequence-vector "aa") 0)
  1649. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1650. ;; save and restore frame size
  1651. ;;http://www.bookshelf.jp/soft/meadow_30.html#SEC416
  1652. (defun my-window-size-save ()
  1653. (let* ((rlist (frame-parameters (selected-frame)))
  1654. (ilist initial-frame-alist)
  1655. (nCHeight (frame-height))
  1656. (nCWidth (frame-width))
  1657. (tMargin (if (integerp (cdr (assoc 'top rlist)))
  1658. (cdr (assoc 'top rlist)) 0))
  1659. (lMargin (if (integerp (cdr (assoc 'left rlist)))
  1660. (cdr (assoc 'left rlist)) 0))
  1661. buf
  1662. (file "~/.emacs.d/.framesize.el")
  1663. (recentf-exclude '("\\.emacs\\.d/\\.framesize\\.el$")))
  1664. (if (get-file-buffer (expand-file-name file))
  1665. (setq buf (get-file-buffer (expand-file-name file)))
  1666. (setq buf (find-file-noselect file)))
  1667. (set-buffer buf)
  1668. (erase-buffer)
  1669. (insert (concat
  1670. ;; 初期値をいじるよりも modify-frame-parameters
  1671. ;; で変えるだけの方がいい?
  1672. "(delete 'width default-frame-alist)\n"
  1673. "(delete 'height default-frame-alist)\n"
  1674. "(delete 'top default-frame-alist)\n"
  1675. "(delete 'left default-frame-alist)\n"
  1676. "(setq default-frame-alist (append (list\n"
  1677. "'(width . " (int-to-string nCWidth) ")\n"
  1678. "'(height . " (int-to-string nCHeight) ")\n"
  1679. "'(top . " (int-to-string tMargin) ")\n"
  1680. "'(left . " (int-to-string lMargin) "))\n"
  1681. "default-frame-alist))\n"
  1682. ;;"(setq default-frame-alist default-frame-alist)"
  1683. ))
  1684. (save-buffer)
  1685. ))
  1686. (defun my-window-size-load ()
  1687. (let* ((file "~/.emacs.d/.framesize.el"))
  1688. (if (file-exists-p file)
  1689. (load file))))
  1690. (when window-system
  1691. (my-window-size-load)
  1692. (add-hook 'after-init-hook ;何かがframeの大きさ勝手に変えやがる
  1693. (lambda ()
  1694. (run-with-timer 1
  1695. nil
  1696. (lambda ()
  1697. (modify-frame-parameters (selected-frame)
  1698. default-frame-alist))))
  1699. t)
  1700. ;; (add-hook 'make-frame-hook
  1701. ;; (lambda ()
  1702. ;; (run-with-timer 1
  1703. ;; nil
  1704. ;; (lambda ()
  1705. ;; (modify-frame-parameters (selected-frame)
  1706. ;; initial-frame-alist))))
  1707. ;; t)
  1708. (add-hook 'kill-emacs-hook
  1709. 'my-window-size-save))
  1710. ;; windowサイズを固定
  1711. ;; setq default-frame-alist
  1712. ;; (append (list '(width . 80)
  1713. ;; '(height . 35)
  1714. ;; )
  1715. ;; default-frame-alist)
  1716. ;; ) ;;デフォルトのフレーム設定
  1717. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1718. ;; ;; emacsを殺伐とさせる
  1719. ;; ;; 補完させるとき失敗するからなし
  1720. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  1721. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  1722. ;; (setq arg
  1723. ;; (concat arg
  1724. ;; (if (eq nil (string-match "\\. *$" arg)) ".")
  1725. ;; " And You are a Coward!")))
  1726. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1727. ;; ubuntu
  1728. (defun my-load-scim ()
  1729. "use scim-bridge.el as japanese im."
  1730. ;; Load scim-bridge.
  1731. (require 'scim-bridge)
  1732. ;; Turn on scim-mode automatically after loading .emacs
  1733. (add-hook 'after-init-hook 'scim-mode-on)
  1734. (setq scim-cursor-color "red")
  1735. (scim-define-preedit-key ?\^h t)
  1736. (scim-define-common-key ?\* nil)
  1737. (scim-define-common-key ?\^/ nil))
  1738. (defun my-load-anthy ()
  1739. "use anthy.el as japanese im."
  1740. ;; anthy
  1741. (require 'anthy)
  1742. (global-set-key [muhenkan] (lambda () (interactive) (anthy-mode-off)))
  1743. (global-set-key [henkan] (lambda () (interactive) (anthy-mode-on)))
  1744. (when (>= emacs-major-version 23)
  1745. (setq anthy-accept-timeout 1)))
  1746. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1747. ;; windows用設定
  1748. ;; (add-to-list 'exec-path "c:/Program Files/Gauche/bin/")
  1749. (defun start-ckw-bash ()
  1750. ""
  1751. (interactive)
  1752. (start-process "ckw_bash"
  1753. nil
  1754. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe")) ; cじゃないといけないらしい
  1755. (defun my-w32-add-export-path (&rest args)
  1756. ""
  1757. (mapcar (lambda (path)
  1758. (add-to-list 'exec-path (expand-file-name path)))
  1759. (reverse args))
  1760. (setenv "PATH"
  1761. (mapconcat 'convert-standard-filename
  1762. exec-path
  1763. ";")))
  1764. (when (eq system-type 'windows-nt)
  1765. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  1766. ;; (setq python-python-command "c:/Python26/python.exe")
  1767. (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  1768. (my-w32-add-export-path "c:/WINDOWS"
  1769. (expand-file-name "~/bin")
  1770. (expand-file-name "~/dbx/apps/bin"))
  1771. (when window-system
  1772. (setq w32-enable-synthesized-fonts t))
  1773. (setq file-name-coding-system 'sjis))