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.
 
 
 
 
 
 

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