Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

2021 righe
72 KiB

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