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.
 
 
 
 
 
 

2022 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. ;; 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. )
  498. ;; urlに細かい説明あり。でも設定は上記だけでよさそう
  499. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  500. (and (not x-select-enable-clipboard)
  501. (executable-find "xclip")
  502. (dllib-if-unfound "xclip" "http://www.emacswiki.org/emacs/download/xclip.el" t)
  503. (require 'xclip nil t)
  504. (turn-on-xclip))
  505. ;; その他のhook
  506. (add-hook 'after-save-hook
  507. 'executable-make-buffer-file-executable-if-script-p)
  508. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  509. ;; mode
  510. (add-hook 'verilog-mode-hook
  511. (lambda ()
  512. (define-key verilog-mode-map ";" 'self-insert-command)))
  513. (setq diff-switches "-u")
  514. (add-hook 'diff-mode-hook
  515. (lambda ()
  516. (view-mode 1)
  517. (set-face-foreground 'diff-index-face "blue")
  518. (set-face-foreground 'diff-hunk-header-face "magenda")
  519. (set-face-foreground 'diff-removed-face "red")
  520. (set-face-foreground 'diff-added-face "blue")
  521. (set-face-foreground 'diff-changed-face "syan")
  522. ))
  523. ;; (ffap-bindings)
  524. (add-hook 'sh-mode-hook
  525. (lambda ()
  526. (define-key sh-mode-map (kbd "C-x C-e") 'my-execute-shell-command-current-line)))
  527. (defun my-execute-shell-command-current-line ()
  528. ""
  529. (interactive)
  530. (shell-command (buffer-substring-no-properties (point-at-bol)
  531. (point))))
  532. (add-hook 'inferior-python-mode-hook
  533. (lambda ()
  534. (define-key inferior-python-mode-map (kbd "<up>") 'comint-previous-input)
  535. (define-key inferior-python-mode-map (kbd "<down>") 'comint-next-input)))
  536. (add-to-list 'Info-default-directory-list (expand-file-name "~/.info/emacs-ja"))
  537. (setq bookmark-default-file "~/.emacs.d/bmk")
  538. (add-hook 'apropos-mode-hook
  539. (lambda ()
  540. (define-key apropos-mode-map "j" 'next-line)
  541. (define-key apropos-mode-map "k" 'previous-line)))
  542. (define-key minibuffer-local-map (kbd "C-u") (lambda () (interactive) (delete-region (point-at-bol) (point-at-eol))))
  543. (add-hook 'isearch-mode-hook
  544. (lambda ()
  545. ;; (define-key isearch-mode-map (kbd "C-j") 'isearch-other-control-char)
  546. ;; (define-key isearch-mode-map (kbd "C-k") 'isearch-other-control-char)
  547. ;; (define-key isearch-mode-map (kbd "C-h") 'isearch-other-control-char)
  548. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  549. (define-key isearch-mode-map (kbd "M-r") 'isearch-query-replace-regexp)))
  550. (add-hook 'outline-mode-hook
  551. (lambda ()
  552. (if (string-match "\\.md$" buffer-file-name)
  553. (set (make-local-variable 'outline-regexp) "#+ "))))
  554. (add-to-list 'auto-mode-alist (cons "\\.ol$" 'outline-mode))
  555. (add-to-list 'auto-mode-alist (cons "\\.md$" 'outline-mode))
  556. (when (dllib-if-unfound "markdown-mode"
  557. "http://jblevins.org/projects/markdown-mode/markdown-mode.el"
  558. t)
  559. (add-to-list 'auto-mode-alist (cons "\\.md$" 'markdown-mode))
  560. (autoload 'markdown-mode "markdown-mode" "Major mode for editing Markdown files." nil)
  561. (add-hook 'markdown-mode-hook
  562. (lambda ()
  563. (outline-minor-mode 1)
  564. (set (make-local-variable 'comment-start) ";"))))
  565. ;; (add-hook 'c-mode-hook
  566. ;; (lambda ()
  567. ;; (set (make-local-variable 'comment-start) "//")
  568. ;; (set (make-local-variable 'comment-end) "")))
  569. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  570. ;; c-mode
  571. ;; (setq c-default-style "bsd")
  572. ;; BackSpace キーを「賢く」し,インデント幅は2桁,タブはスペースに展開
  573. (add-hook 'c-mode-common-hook
  574. (lambda ()
  575. (setq c-basic-offset 2
  576. indent-tabs-mode nil)
  577. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  578. (c-toggle-hungry-state 1)
  579. ))
  580. (defun my-compile-c-this-file ()
  581. ""
  582. (interactive)
  583. (compile (format "gcc -Wall -g -o %s %s"
  584. (file-name-sans-extension buffer-file-name)
  585. buffer-file-name)))
  586. ;; (when (require 'c nil t)(c-toggle-hungry-state t)
  587. (when (dllib-if-unfound "js2-mode"
  588. "https://github.com/mooz/js2-mode/raw/master/js2-mode.el"
  589. t)
  590. (autoload 'js2-mode "js2-mode" nil t)
  591. (add-to-list 'auto-mode-alist '("\\.js$" . js2-mode)))
  592. ;; (add-hook 'js2-mode-hook
  593. ;; (lambda ()
  594. ;; (add-hook 'before-save-hook
  595. ;; 'my-indent-buffer
  596. ;; nil
  597. ;; t)))
  598. (add-hook 'js2-mode-hook
  599. (lambda ()
  600. (define-key js2-mode-map (kbd "C-m") (lambda ()
  601. (interactive)
  602. (js2-enter-key)
  603. (indent-for-tab-command)))
  604. (add-hook (kill-local-variable 'before-save-hook)
  605. 'js2-before-save)))
  606. (and nil
  607. (require 'zone nil t)
  608. (not (eq system-type 'windows-nt))
  609. ;; (zone-when-idle 180)
  610. (run-with-idle-timer 180 t (lambda ()
  611. (unless (memq major-mode
  612. '(term-mode))
  613. (zone)))))
  614. (when (require 'uniquify nil t)
  615. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  616. (setq uniquify-ignore-buffers-re "*[^*]+*")
  617. (setq uniquify-min-dir-content 1))
  618. (add-hook 'view-mode-hook
  619. (lambda()
  620. (define-key view-mode-map "j" (lambda() (interactive) (scroll-up 1)))
  621. (define-key view-mode-map "k" (lambda() (interactive) (scroll-down 1)))
  622. (define-key view-mode-map "/" 'isearch-forward)
  623. (define-key view-mode-map "v" 'toggle-read-only)
  624. (define-key view-mode-map "q" 'bury-buffer)))
  625. (global-set-key "\M-r" 'view-mode)
  626. (setq view-read-only t)
  627. ;; (add-hook 'find-file-hook
  628. ;; (lambda ()
  629. ;; (when buffer-read-only
  630. ;; (view-mode 1))))
  631. (add-hook 'Man-mode-hook
  632. (lambda ()
  633. (view-mode 1)
  634. (setq truncate-lines nil)))
  635. (setq Man-notify-method (if window-system
  636. 'newframe
  637. 'pushy))
  638. ;; (when (and (executable-find "git")
  639. ;; (require 'sgit-mode nil t))
  640. ;; (add-hook 'find-file-hook
  641. ;; 'sgit-load))
  642. (require 'session nil t)
  643. (when (require 'gtkbm nil t)
  644. (global-set-key (kbd "C-x C-d") 'gtkbm))
  645. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  646. ;; frame buffer
  647. ;; (add-hook 'after-make-frame-functions
  648. ;; (lambda (frame)
  649. ;; (recentf-open-files)))
  650. ;; (defvar aaa nil)
  651. ;; (plist-get aaa 'abc)
  652. ;; (setq aaa (plist-put aaa 'abc 'efg))
  653. (defvar my-frame-buffer-plist nil)
  654. ;; (setplist my-frame-buffer-plist nil)
  655. (defun my-frame-buffer-add ()
  656. ""
  657. (setq my-frame-buffer-plist
  658. (plist-put my-frame-buffer-plist
  659. (selected-frame)
  660. (let ((lst (my-frame-buffer-get)))
  661. (if lst
  662. (add-to-list 'lst
  663. (current-buffer))
  664. (list (current-buffer)))))))
  665. (defun my-frame-buffer-remove ()
  666. ""
  667. (setq my-frame-buffer-plist
  668. (plist-put my-frame-buffer-plist
  669. (selected-frame)
  670. (delq (current-buffer)
  671. (my-frame-buffer-get)))))
  672. (defun my-frame-buffer-get (&optional frame)
  673. ""
  674. (plist-get my-frame-buffer-plist
  675. (or frame
  676. (selected-frame))))
  677. (defun my-frame-buffer-kill-all-buffer (frame)
  678. ""
  679. (mapcar 'kill-buffer
  680. (my-frame-buffer-get frame)))
  681. (add-hook 'find-file-hook
  682. 'my-frame-buffer-add)
  683. ;; (add-hook 'dired-mode-hook
  684. ;; 'my-frame-buffer-add)
  685. (add-hook 'kill-buffer-hook
  686. 'my-frame-buffer-remove)
  687. (add-hook 'delete-frame-functions
  688. 'my-frame-buffer-kill-all-buffer)
  689. (frame-parameters (selected-frame))
  690. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  691. ;; term mode
  692. ;; (setq multi-term-program shell-file-name)
  693. (and (dllib-if-unfound "multi-term"
  694. "http://www.emacswiki.org/emacs/download/multi-term.el"
  695. t)
  696. (require 'multi-term nil t)
  697. (setq multi-term-switch-after-close nil))
  698. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  699. ;; (setq term-ansi-default-program shell-file-name)
  700. (add-hook 'term-setup-hook (lambda ()
  701. (setq term-display-table (make-display-table))))
  702. (add-hook 'term-mode-hook (lambda ()
  703. (unless (memq (current-buffer) (and (featurep 'multi-term) ; current buffer is not multi-term buffer
  704. (multi-term-list)))
  705. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  706. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  707. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  708. ;; (define-key term-raw-map "\C-f" 'forward-char)
  709. ;; (define-key term-raw-map "\C-b" 'backward-char)
  710. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  711. (define-key term-raw-map "\C-x" (lookup-key (current-global-map) "\C-x"))
  712. (define-key term-raw-map "\C-z" (lookup-key (current-global-map) "\C-z")))
  713. (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  714. (define-key term-raw-map [delete] 'term-send-raw)
  715. (define-key term-raw-map "\C-h" 'term-send-backspace)
  716. (define-key term-raw-map "\C-y" 'term-paste)
  717. (define-key term-raw-map "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  718. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  719. ;; (define-key term-raw-map (kbd key) 'term-send-raw))
  720. ;; (define-key term-raw-map "\C-d" 'delete-char)
  721. (set (make-variable-buffer-local 'scroll-margin) 0)
  722. ;; (set (make-variable-buffer-local 'cua-enable-cua-keys) nil)
  723. ;; (cua-mode 0)
  724. ;; (and cua-mode
  725. ;; (local-unset-key (kbd "C-c")))
  726. ;; (define-key cua--prefix-override-keymap "\C-c" 'term-interrupt-subjob)
  727. ))
  728. ;; (add-hook 'term-exec-hook 'forward-char)
  729. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  730. ;; buffer switching
  731. (when (require 'bs nil t)
  732. ;; (global-set-key "\C-x\C-b" 'bs-show)
  733. (defalias 'list-buffers 'bs-show))
  734. ;; (add-to-list 'bs-configurations '("processes" nil get-buffer-process ".*" nil nil))
  735. (add-to-list 'bs-configurations '("same-dir" nil buffer-same-dir-p ".*" nil nil))
  736. (add-to-list 'bs-configurations '("this-frame" nil (lambda (buf) (memq buf (my-frame-buffer-get))) ".*" nil nil))
  737. ;; (setq bs-configurations (list '("processes" nil get-buffer-process ".*" nil nil)
  738. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil bs-visits-non-file bs-sort-buffer-interns-are-last)))
  739. (setq bs-default-configuration "this-frame")
  740. (setq bs-default-sort-name "by name")
  741. (add-hook 'bs-mode-hook
  742. (lambda ()
  743. (setq bs-default-configuration "this-frame")
  744. ;; (and bs--show-all
  745. ;; (call-interactively 'bs-toggle-show-all))
  746. (set (make-variable-buffer-local 'scroll-margin) 0)
  747. ))
  748. (defun buffer-same-dir-p (bf)
  749. "return t if BF's dir is same as current dir, otherwise nil."
  750. (let ((cdir (expand-file-name default-directory)))
  751. (with-current-buffer bf
  752. (equal (expand-file-name default-directory) cdir))))
  753. (defun echo-buffer-list (&optional blist)
  754. "echo buffer list as string. BLIST is list with buffer objects as elements.
  755. if arg is omitted use value of `buffer-list'."
  756. (interactive)
  757. (message (or (mapconcat (lambda (bf)
  758. (concat (buffer-name bf)
  759. "\t"
  760. (with-current-buffer bf
  761. (symbol-name major-mode))
  762. "\t"
  763. (abbreviate-file-name (buffer-file-name bf))))
  764. (or blist
  765. (buffer-list))
  766. "\n")
  767. "")))
  768. (defun my-buffer-list ()
  769. "return buffer list."
  770. (delq nil
  771. (mapcar (lambda (bf)
  772. (with-current-buffer bf
  773. (and buffer-file-name
  774. bf)))
  775. (buffer-list (selected-frame)))))
  776. (defvar buffer-switch-list-function 'my-buffer-list)
  777. (defun switch-to-previous-buffer-cycle (&optional silent-p)
  778. ""
  779. (interactive)
  780. (let ((bl (funcall buffer-switch-list-function)))
  781. (when bl
  782. (bury-buffer (car bl))
  783. (switch-to-buffer (or (nth 1 bl)
  784. (car bl)))
  785. (or silent-p
  786. (echo-buffer-list (funcall buffer-switch-list-function))))))
  787. (defun switch-to-next-buffer-cycle (&optional silent-p)
  788. ""
  789. (interactive)
  790. (let* ((bl (funcall buffer-switch-list-function))
  791. (bf (nth (- (length bl)
  792. 1)
  793. bl)))
  794. (when bl
  795. (switch-to-buffer bf)
  796. (or silent-p
  797. (echo-buffer-list (funcall buffer-switch-list-function))))))
  798. (iswitchb-mode 1)
  799. (defun iswitchb-buffer-display-other-window ()
  800. ""
  801. (interactive)
  802. (let ((iswitchb-default-method 'display))
  803. (call-interactively 'iswitchb-buffer)))
  804. (defun switch-to-other-buffer ()
  805. ""
  806. (interactive)
  807. (let ((buffer-switch-list-function 'buffer-list))
  808. (switch-to-previous-buffer-cycle t)))
  809. (global-set-key (kbd "C-.") 'switch-to-previous-buffer-cycle)
  810. (global-set-key (kbd "C-,") 'switch-to-next-buffer-cycle)
  811. ;; (global-set-key (kbd "C-\\") 'switch-to-other-buffer)
  812. ;; (global-set-key (kbd "C-\\") 'bury-buffer)
  813. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  814. ;; sdic
  815. (defun sdic-describe-word-at-point-echo ()
  816. ""
  817. (interactive)
  818. (save-window-excursion
  819. (sdic-describe-word-at-point))
  820. (save-excursion
  821. (set-buffer sdic-buffer-name)
  822. (message (buffer-substring (point-min)
  823. (progn (goto-char (point-min))
  824. (or (and (re-search-forward "^\\w" nil t 4)
  825. (progn (previous-line) t)
  826. (point-at-eol))
  827. (point-max)))))))
  828. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  829. (setq sdic-waei-dictionary-list '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  830. (setq sdic-disable-select-window t)
  831. (setq sdic-window-height 7)
  832. (when (require 'sdic nil t)
  833. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  834. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo))
  835. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  836. ;; vc
  837. ;; (require 'vc)
  838. (setq vc-handled-backends nil)
  839. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  840. ;; gauche-mode
  841. (setq scheme-program-name "gosh")
  842. (defun run-gauche-other-window ()
  843. "Run gauche on other window"
  844. (interactive)
  845. (switch-to-buffer-other-window
  846. (get-buffer-create "*scheme*"))
  847. (run-gauche))
  848. (defun run-gauche ()
  849. "run gauche"
  850. (run-scheme "gosh"))
  851. (defun scheme-send-buffer ()
  852. ""
  853. (interactive)
  854. (scheme-send-region (point-min) (point-max)))
  855. (add-hook 'scheme-mode-hook
  856. (lambda ()
  857. (define-key scheme-mode-map "\C-c\C-b" 'scheme-send-buffer)))
  858. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  859. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  860. (when (dllib-if-unfound "gauche-mode"
  861. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  862. t)
  863. (setq auto-mode-alist
  864. (cons '("\.scm$" . gauche-mode) auto-mode-alist))
  865. (autoload 'gauche-mode "gauche-mode" "Major mode for Scheme." t)
  866. (autoload 'run-scheme "gauche-mode" "Run an inferior Scheme process." t)
  867. (add-hook 'gauche-mode-hook
  868. (lambda ()
  869. (define-key scheme-mode-map "\C-c\C-z" 'run-gauche-other-window))))
  870. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  871. ;; recentf-mode
  872. (add-hook 'recentf-dialog-mode-hook
  873. 'my-recentf-abbrev-list)
  874. (defun my-recentf-delete-entry ()
  875. ""
  876. (interactive)
  877. (let ((p (point)))
  878. (setq recentf-list
  879. (delete (my-recentf-get-filename) recentf-list))
  880. (recentf-open-files)
  881. (goto-char p)))
  882. (defun my-recentf-abbrev-list ()
  883. ""
  884. (setq recentf-list
  885. (mapcar 'abbreviate-file-name
  886. recentf-list)))
  887. (defun my-recentf-view-file ()
  888. ""
  889. (interactive)
  890. (view-file (my-recentf-get-filename)))
  891. (defun my-recentf-dired ()
  892. ""
  893. (interactive)
  894. (let ((file (my-recentf-get-filename)))
  895. (if (file-directory-p file)
  896. (dired file)
  897. (dired (or (file-name-directory file)
  898. ".")))))
  899. (defun my-recentf-x-open ()
  900. ""
  901. (interactive)
  902. (my-x-open (my-recentf-get-filename)))
  903. (defun my-recentf-cd-and-find-file ()
  904. ""
  905. (interactive)
  906. (cd (file-name-directory (my-recentf-get-filename)))
  907. (call-interactively 'find-file))
  908. (defun my-recentf-get-filename ()
  909. "get file name in recentf-mode"
  910. (replace-regexp-in-string " \\(\\[.+?\\] \\)?" ; " " or " [\\d] "
  911. ""
  912. (buffer-substring-no-properties (point-at-bol)
  913. (point-at-eol))))
  914. (setq recentf-save-file (expand-file-name "~/.emacs.d/.recentf")
  915. recentf-max-menu-items 20
  916. recentf-max-saved-items 30
  917. recentf-show-file-shortcuts-flag nil)
  918. (when (require 'recentf nil t)
  919. (global-set-key "\C-x\C-r" 'recentf-open-files)
  920. ;; (add-hook 'find-file-hook
  921. ;; (lambda ()
  922. ;; (recentf-add-file default-directory)))
  923. (recentf-mode 1)
  924. (add-to-list 'recentf-filename-handlers 'abbreviate-file-name)
  925. (add-to-list 'recentf-exclude "\\.emacs\\.d/\\.recentf"))
  926. (add-hook 'recentf-dialog-mode-hook
  927. (lambda ()
  928. (recentf-save-list)
  929. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f") 'my-recentf-cd-and-find-file)
  930. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  931. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  932. (define-key recentf-dialog-mode-map "o" 'my-recentf-x-open)
  933. (define-key recentf-dialog-mode-map "d" 'my-recentf-delete-entry)
  934. (define-key recentf-dialog-mode-map "@" 'my-recentf-dired)
  935. (define-key recentf-dialog-mode-map "v" 'my-recentf-view-file)
  936. (cd "~/")))
  937. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  938. ;; dired
  939. (require 'dired)
  940. (defun my-dired-echo-file-head (&optional arg)
  941. ""
  942. (interactive "P")
  943. (let ((f (dired-get-filename)))
  944. (message "%s"
  945. (with-temp-buffer
  946. (insert-file-contents f)
  947. (buffer-substring-no-properties (point-min)
  948. (progn (goto-line (if arg
  949. (prefix-numeric-value arg)
  950. 10))
  951. (point-at-eol)))))))
  952. (defun my-dired-diff ()
  953. ""
  954. (interactive)
  955. (let ((files (dired-get-marked-files nil nil nil t)))
  956. (if (eq (car files)
  957. t)
  958. (diff (cadr files) (dired-get-filename))
  959. (message "One files must be marked!"))))
  960. (require 'dired-aux) ;; needed to use dired-dwim-target-directory
  961. (defun my-dired-do-pack-or-unpack ()
  962. "pack or unpack files.
  963. if targetting one file and that is archive file defined in `pack-program-alist', unpack that.
  964. otherwise, pack marked files. prompt user to decide filename for archive."
  965. (interactive)
  966. (let* ((infiles (dired-get-marked-files t))
  967. (onefile (and (eq 1 ; filename if only one file targeted, otherwise nil.
  968. (length infiles))
  969. (car infiles))))
  970. (if (and onefile
  971. (my-pack-file-name-association onefile))
  972. (when (y-or-n-p (format "unpack %s? " onefile))
  973. (my-unpack onefile))
  974. (let* ((dir-default (dired-dwim-target-directory))
  975. (archive-default (my-pack-file-extension (file-name-nondirectory (car infiles))))
  976. (archive ;; (if (interactive-p)
  977. (read-file-name "Output file to pack : "
  978. dir-default
  979. nil
  980. nil
  981. archive-default)
  982. ;; (concat dir-default archive-default)
  983. ))
  984. (apply 'my-pack
  985. archive
  986. infiles))))
  987. (revert-buffer)
  988. ;; (dired-unmark-all-marks)
  989. )
  990. (defun my-file-name-extension-with-tar (filename)
  991. "if FILENAME has extension with tar, like \"tar.gz\", return that.
  992. otherwise, return extension normally."
  993. (if (string-equal "tar" (file-name-extension (file-name-sans-extension filename)))
  994. (concat "tar."
  995. (file-name-extension filename))
  996. (file-name-extension filename)))
  997. (defun my-pack-file-extension (filename)
  998. "if FILENAME has extension and it can be used for pack, return FILENAME.
  999. otherwise, return FILENAME with `my-pack-default-extension'"
  1000. (if (my-pack-file-name-association filename)
  1001. filename
  1002. (concat filename "." my-pack-default-extension)))
  1003. (defvar my-7z-program-name
  1004. (or (executable-find "7z")
  1005. (executable-find "7za")
  1006. (executable-find "7zr"))
  1007. "7z program.")
  1008. (defvar my-pack-default-extension
  1009. "7z"
  1010. "default suffix for packing. filename with this suffix must matches one of `pack-program-alist'")
  1011. (defun my-pack-file-name-association (filename)
  1012. "if the pattern matching FILENAME is found at car of the list in `pack-program-alist', return cdr of that list.
  1013. otherwise, return nil."
  1014. (let ((case-fold-search nil))
  1015. (assoc-default filename
  1016. my-pack-program-alist
  1017. 'string-match-p
  1018. nil)))
  1019. (defvar my-pack-program-alist
  1020. `(
  1021. ("\\.7z\\'" ,(concat my-7z-program-name " a") ,(concat my-7z-program-name " x"))
  1022. ("\\.zip\\'" "zip -r" "unzip")
  1023. ("\\.tar\\'" "tar cf" "tar xf")
  1024. ("\\.tgz\\'" "tar czf" "tar xzf")
  1025. ("\\.tar\\.gz\\'" "tar czf" "tar xzf")
  1026. )
  1027. "Alist of filename patterns, command for pack and unpack.
  1028. Each element looks like (REGEXP PACKING-COMMAND UNPACKING-COMMAND).
  1029. PACKING-COMMAND and UNPACKING-COMMAND can be nil if the command is not available.
  1030. alist is searched from the beginning so pattern for \".tar.gz\" should be ahead of pattern for \".gz\"")
  1031. ;; (string-match-p "\\.gz\\'" "aaa.gz") ; \' matches string end, $ also matches the point before newline.
  1032. (defun my-unpack (archive)
  1033. "unpack ARCHIVE. command for unpacking is defined in `pack-program-alist'"
  1034. (interactive "fArchive to extract: ")
  1035. (let* ((earchive (expand-file-name archive))
  1036. (cmd (nth 1
  1037. (my-pack-file-name-association earchive)))
  1038. )
  1039. (if cmd
  1040. (shell-command (concat cmd
  1041. " "
  1042. (shell-quote-argument earchive)))
  1043. (message "this is not archive file defined in `pack-program-alist'!"))))
  1044. (defun my-pack (archive &rest files)
  1045. "pack FILES into ARCHIVE.
  1046. if ARCHIVE have extension defined in `pack-program-alist', use that command.
  1047. otherwise, use `pack-default-extension' for pack."
  1048. (let* ((archive-ext (my-pack-file-extension (expand-file-name archive)))
  1049. (cmd (car (my-pack-file-name-association archive-ext)))
  1050. )
  1051. (if cmd
  1052. (shell-command (concat cmd
  1053. " "
  1054. (shell-quote-argument archive-ext)
  1055. " "
  1056. (mapconcat 'shell-quote-argument
  1057. files
  1058. " ")))
  1059. (message "invalid extension for packing!"))))
  1060. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1061. "pop up buffer using `display-buffer' and return that buffer."
  1062. (let ((bf (get-buffer-create buffer-or-name)))
  1063. (with-current-buffer bf
  1064. (cd ".")
  1065. (erase-buffer))
  1066. (display-buffer bf)
  1067. bf))
  1068. (defun my-replace-nasi-none ()
  1069. ""
  1070. (save-excursion
  1071. (let ((buffer-read-only nil))
  1072. (goto-char (point-min))
  1073. (while (search-forward "なし" nil t)
  1074. (replace-match "none")))))
  1075. (defun dired-get-du () ;em-unix.el使えるかも
  1076. "dired get disk usage"
  1077. (interactive)
  1078. (message "calculating du...")
  1079. (dired-do-shell-command "du -hs * " nil (dired-get-marked-files)))
  1080. (defun my-dired-scroll-up ()
  1081. ""
  1082. (interactive)
  1083. (my-dired-previous-line (- (window-height) 1)))
  1084. (defun my-dired-scroll-down ()
  1085. ""
  1086. (interactive)
  1087. (my-dired-next-line (- (window-height) 1)))
  1088. (defun my-dired-previous-line (&optional arg)
  1089. ""
  1090. (interactive)
  1091. (dired-previous-line (or arg 1))
  1092. (my-dired-print-current-dir-and-file))
  1093. (defun my-dired-next-line (&optional arg)
  1094. ""
  1095. (interactive)
  1096. (dired-next-line (or arg 1))
  1097. (my-dired-print-current-dir-and-file))
  1098. (defun my-dired-print-current-dir-and-file ()
  1099. (message "%s %s"
  1100. default-directory
  1101. (buffer-substring-no-properties (point-at-bol)
  1102. (point-at-eol))))
  1103. (defun dired-do-execute-as-command ()
  1104. ""
  1105. (interactive)
  1106. (let ((file (dired-get-filename t)))
  1107. (if (file-executable-p file)
  1108. (start-process file nil file)
  1109. (when (y-or-n-p "this file cant be executed. mark as executable and go? : ")
  1110. (set-file-modes file (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1111. (start-process file nil file)))))
  1112. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1113. (defun my-dired-x-open ()
  1114. ""
  1115. (interactive)
  1116. (my-x-open (dired-get-filename t t)))
  1117. (if (eq window-system 'mac)
  1118. (setq dired-listing-switches "-lhFG")
  1119. (setq dired-listing-switches "-lhFG --time-style=long-iso")
  1120. )
  1121. (setq dired-listing-switches "-lhFG")
  1122. (define-minor-mode my-dired-display-all-mode
  1123. ""
  1124. :init-value nil
  1125. :global nil
  1126. :lighter " ALL"
  1127. (when (eq major-mode 'dired-mode)
  1128. (my-dired-display-all-set)
  1129. (revert-buffer)))
  1130. (defun my-dired-display-all-set ()
  1131. ""
  1132. (if my-dired-display-all-mode
  1133. (or (string-match-p my-dired-display-all-switch
  1134. dired-actual-switches)
  1135. (setq dired-actual-switches
  1136. (concat my-dired-display-all-switch
  1137. " "
  1138. dired-actual-switches)))
  1139. (setq dired-actual-switches
  1140. (replace-regexp-in-string (concat my-dired-display-all-switch
  1141. " ")
  1142. ""
  1143. dired-actual-switches))))
  1144. (defvar my-dired-display-all-switch "-A")
  1145. (add-hook 'dired-mode-hook
  1146. 'my-dired-display-all-set)
  1147. (put 'dired-find-alternate-file 'disabled nil)
  1148. (setq dired-ls-F-marks-symlinks t)
  1149. (require 'ls-lisp)
  1150. (setq ls-lisp-use-insert-directory-program nil)
  1151. (setq ls-lisp-dirs-first t)
  1152. (setq ls-lisp-use-localized-time-format t)
  1153. (setq ls-lisp-format-time-list
  1154. '("%Y-%m-%d %H:%M"
  1155. "%Y-%m-%d "))
  1156. (setq dired-dwim-target t)
  1157. ;; (add-hook 'dired-after-readin-hook
  1158. ;; 'my-replace-nasi-none)
  1159. ;; (add-hook 'after-init-hook
  1160. ;; (lambda ()
  1161. ;; (dired ".")))
  1162. (add-hook 'dired-mode-hook
  1163. (lambda ()
  1164. (define-key dired-mode-map "o" 'my-dired-x-open)
  1165. (define-key dired-mode-map "i" 'dired-get-du)
  1166. (define-key dired-mode-map "!" 'shell-command)
  1167. (define-key dired-mode-map "&" 'async-shell-command)
  1168. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1169. (define-key dired-mode-map "=" 'my-dired-diff)
  1170. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1171. (define-key dired-mode-map "b" 'gtkbm)
  1172. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1173. (define-key dired-mode-map "@" (lambda () (interactive) (my-x-open ".")))
  1174. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1175. (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1176. (define-key dired-mode-map "a" 'my-dired-display-all-mode)
  1177. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1178. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1179. (substitute-key-definition 'dired-next-line 'my-dired-next-line dired-mode-map)
  1180. (substitute-key-definition 'dired-previous-line 'my-dired-previous-line dired-mode-map)
  1181. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1182. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1183. (let ((file "._Icon\015"))
  1184. (when (file-readable-p file)
  1185. (delete-file file)))))
  1186. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1187. (defun my-dired-toggle-mark()
  1188. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1189. (t dired-marker-char))))
  1190. (delete-char 1)
  1191. (insert cur)))
  1192. (defun my-dired-mark (arg)
  1193. "toggle mark the current (or next ARG) files.
  1194. If on a subdir headerline, mark all its files except `.' and `..'.
  1195. Use \\[dired-unmark-all-files] to remove all marks
  1196. and \\[dired-unmark] on a subdir to remove the marks in
  1197. this subdir."
  1198. (interactive "P")
  1199. (if (dired-get-subdir)
  1200. (save-excursion (dired-mark-subdir-files))
  1201. (let ((inhibit-read-only t))
  1202. (dired-repeat-over-lines
  1203. (prefix-numeric-value arg)
  1204. 'my-dired-toggle-mark))))
  1205. (defun my-dired-mark-backward (arg)
  1206. "In Dired, move up lines and toggle mark there.
  1207. Optional prefix ARG says how many lines to unflag; default is one line."
  1208. (interactive "p")
  1209. (my-dired-mark (- arg)))
  1210. (defun dired-mode-hooks()
  1211. (local-set-key (kbd "SPC") 'my-dired-mark)
  1212. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1213. (add-hook 'dired-mode-hook 'dired-mode-hooks)
  1214. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1215. ;; eshell
  1216. (defun my-eshell-backward-delete-char ()
  1217. (interactive)
  1218. (when (< (save-excursion
  1219. (eshell-bol)
  1220. (point))
  1221. (point))
  1222. (backward-delete-char 1)))
  1223. (defvar my-eshell-frame-buffer-alist nil)
  1224. (defun my-eshell-frame-buffer (frame)
  1225. "get buffer associated with FRAME. if buffer doesnt exist or killed, return nil."
  1226. (let ((bf (cdr (assq frame my-eshell-frame-buffer-alist))))
  1227. (and bf ;関連付けられたバッファが存在し
  1228. (buffer-name bf) ;かつkillされてない
  1229. bf)))
  1230. (add-hook 'eshell-mode-hook
  1231. (lambda ()
  1232. (add-to-list 'my-eshell-frame-buffer-alist
  1233. (cons (selected-frame) (current-buffer)))))
  1234. (defun my-file-owner-p (file)
  1235. "t if FILE is owned by me."
  1236. (eq (user-uid) (nth 2 (file-attributes file))))
  1237. ;; ;; http://www.bookshelf.jp/pukiwiki/pukiwiki.php?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9
  1238. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1239. ;; (defun eshell/less (&rest args)
  1240. ;; "Invoke `view-file' on the file.
  1241. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1242. ;; (if args
  1243. ;; (while args
  1244. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1245. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1246. ;; (file (pop args)))
  1247. ;; (view-file file)
  1248. ;; (goto-line line))
  1249. ;; (view-file (pop args))))))
  1250. ;; (defun eshell/git (&rest args)
  1251. ;; ""
  1252. ;; (eshell-parse-arguments (point-at-bol) (point-at-eol)))
  1253. (defun eshell/o (&optional file)
  1254. (my-x-open (or file ".")))
  1255. ;; (defun eshell/vi (&rest args)
  1256. ;; "Invoke `find-file' on the file.
  1257. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1258. ;; (while args
  1259. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1260. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1261. ;; (file (pop args)))
  1262. ;; (find-file file)
  1263. ;; (goto-line line))
  1264. ;; (find-file (pop args)))))
  1265. (defun eshell/clear ()
  1266. "Clear the current buffer, leaving one prompt at the top."
  1267. (let ((inhibit-read-only t))
  1268. (erase-buffer)))
  1269. (defun eshell/d (&optional dirname switches)
  1270. "if first arg is omitted open current directory."
  1271. (dired (or dirname ".") switches))
  1272. (defun eshell/v ()
  1273. (view-mode 1))
  1274. (defalias 'eshell/: 'ignore)
  1275. (defalias 'eshell/type 'eshell/which)
  1276. ;; (defalias 'eshell/vim 'eshell/vi)
  1277. (defalias 'eshell/ff 'find-file)
  1278. (defalias 'eshell/q 'eshell/exit)
  1279. (defun eshell-goto-prompt ()
  1280. ""
  1281. (interactive)
  1282. (goto-char (point-max)))
  1283. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1284. "open eshell and change wd
  1285. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1286. (interactive)
  1287. (let ((dir (expand-file-name default-directory)))
  1288. (switch-to-buffer (or eshell-buffer-or-name
  1289. (eshell t)))
  1290. (unless (equal dir (expand-file-name default-directory))
  1291. ;; (cd dir)
  1292. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1293. ;; (eshell-emit-prompt)
  1294. (goto-char (point-max))
  1295. (eshell-kill-input)
  1296. (insert "cd " dir)
  1297. (eshell-send-input))))
  1298. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1299. (setq eshell-scroll-to-bottom-on-input t)
  1300. (setq eshell-cmpl-ignore-case t)
  1301. (setq eshell-cmpl-cycle-completions nil)
  1302. (setq eshell-highlight-prompt nil)
  1303. (setq eshell-ls-initial-args "-FG") ; "-hF")
  1304. (setq eshell-prompt-function
  1305. (lambda ()
  1306. (with-temp-buffer
  1307. (let (p1 p2 p3 p4)
  1308. (insert " [")
  1309. (setq p1 (point))
  1310. (insert (abbreviate-file-name default-directory))
  1311. (setq p2 (point))
  1312. (insert "]"
  1313. "\n")
  1314. (setq p3 (point))
  1315. (insert user-login-name
  1316. "@"
  1317. (or (getenv "HOSTNAME")
  1318. (substring (shell-command-to-string (or (executable-find "hostname")
  1319. "echo ''"))
  1320. 0
  1321. -1)))
  1322. (setq p4 (point))
  1323. (insert " "
  1324. (format-time-string "%a, %d %b %Y %T %z")
  1325. " eshell\n"
  1326. "last:"
  1327. (number-to-string eshell-last-command-status)
  1328. (if (= (user-uid)
  1329. 0)
  1330. " # "
  1331. " $ "))
  1332. (add-text-properties p1
  1333. p2
  1334. '(face ((foreground-color . "yellow"))))
  1335. (add-text-properties p3
  1336. p4
  1337. '(face ((foreground-color . "cyan"))))
  1338. (buffer-substring (point-min)
  1339. (point-max))))))
  1340. (add-hook 'eshell-mode-hook
  1341. (lambda ()
  1342. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1343. ;; (interactive)
  1344. ;; (switch-to-buffer (other-buffer))))
  1345. (define-key eshell-mode-map (kbd "C-u") (lambda ()
  1346. (interactive)
  1347. (eshell-goto-prompt)
  1348. (eshell-kill-input)))
  1349. (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1350. (interactive)
  1351. (eshell-goto-prompt)
  1352. (my-keyboard-quit)))
  1353. (define-key eshell-mode-map (kbd "DEL") 'my-eshell-backward-delete-char)
  1354. (define-key eshell-mode-map (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1355. (define-key eshell-mode-map (kbd "C-n") 'eshell-next-matching-input-from-input)
  1356. (mapcar (lambda (alias)
  1357. (add-to-list 'eshell-command-aliases-list
  1358. alias))
  1359. '(("ll" "ls -l")
  1360. ("la" "ls -a")
  1361. ("lla" "ls -al")
  1362. ("ut" "slogin 03110414@un001.ecc.u-tokyo.ac.jp")
  1363. ("aptin" "sudo apt-get install")
  1364. ("u" "uname")
  1365. ("eless" "cat >>> (with-current-buffer (get-buffer-create \"*eshell output\") (erase-buffer) (setq buffer-read-only nil) (current-buffer)); (view-buffer (get-buffer \"*eshell output*\"))")
  1366. ("g" "git")))
  1367. (add-to-list 'eshell-output-filter-functions 'eshell-truncate-buffer)
  1368. (apply 'eshell/addpath exec-path)
  1369. (set (make-variable-buffer-local 'scroll-margin) 0)
  1370. (eshell/export "GIT_PAGER=")
  1371. (eshell/export "GIT_EDITOR=")
  1372. (eshell/export "LC_MESSAGES=C")
  1373. (eshell/export "TERM=xterm")
  1374. ))
  1375. ;; (eval-after-load "em-alias"
  1376. ;; '(progn ;; (eshell/alias "ll" "ls -l")
  1377. ;; ;; (eshell/alias "la" "ls -a")
  1378. ;; ;; (eshell/alias "lla" "ls -al")
  1379. ;; (eshell/alias "sgcc" (if (eq system-type 'windows-nt)
  1380. ;; "gcc -o win.$1.exe $1"
  1381. ;; "gcc -o ${uname}.$1.out $1"))
  1382. ;; (eshell/alias "slmgcc" (if (eq system-type 'windows-nt)
  1383. ;; "gcc -lm -o win.$1.exe $1"
  1384. ;; "gcc -lm -o ${uname}.$1.out $1"))
  1385. ;; ;; (eshell/alias "ut" "ssh g841105@un001.ecc.u-tokyo.ac.jp")
  1386. ;; (add-to-list 'recentf-exclude (concat eshell-directory-name "alias"))))
  1387. ;; (define-key my-prefix-map (kbd "C-s") (lambda ()
  1388. ;; (interactive)
  1389. ;; (eshell-cd-default-directory (buffer-name (or (my-eshell-frame-buffer (selected-frame))
  1390. ;; (eshell t))))))
  1391. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1392. ;; 最終更新日時を得る
  1393. (defvar my-buffer-file-last-modified-time nil "")
  1394. (make-variable-buffer-local 'my-buffer-file-last-modified-time)
  1395. (defun my-get-file-last-modified-time (file)
  1396. ""
  1397. (nth 5
  1398. (file-attributes file)))
  1399. (defun my-set-buffer-file-last-modified-time ()
  1400. ""
  1401. (make-local-variable 'my-buffer-file-last-modified-time)
  1402. (setq my-buffer-file-last-modified-time
  1403. (format-time-string "%Y/%m/%d %H:%M" (my-get-file-last-modified-time buffer-file-name))))
  1404. (add-hook 'find-file-hook
  1405. 'my-set-buffer-file-last-modified-time)
  1406. (add-hook 'after-save-hook
  1407. 'my-set-buffer-file-last-modified-time)
  1408. (add-hook 'after-revert-hook
  1409. 'my-set-buffer-file-last-modified-time)
  1410. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1411. ;; auto saving
  1412. (defun my-save-this-buffer (silent-p)
  1413. "save current buffer if can without asking"
  1414. (let ((cm (if (current-message)
  1415. (format "%s\n" (current-message))
  1416. ""))
  1417. (fun (symbol-function (if silent-p
  1418. 'ignore
  1419. 'message))))
  1420. (cond ((active-minibuffer-window) nil)
  1421. ((not buffer-file-name) (funcall fun "%ssaving... this buffer doesn't visit any file." cm) nil)
  1422. ((not (file-exists-p buffer-file-name)) (funcall fun "%ssaving... file not exist. save manually first." com) nil)
  1423. (buffer-read-only (funcall fun "%ssaving... this buffer is read-only." cm) nil)
  1424. ((not (buffer-modified-p)) (funcal fun "%ssaving... not modified yet." cm) nil)
  1425. ((not (file-writable-p buffer-file-name)) (funcall fun "%ssaving... you cannot change this file." cm) nil)
  1426. (t (funcall fun "%ssaving..." cm)
  1427. (save-buffer)
  1428. (funcall fun "%ssaving... done." cm)))))
  1429. ;; (if (and buffer-file-name
  1430. ;; (not buffer-read-only)
  1431. ;; (buffer-modified-p)
  1432. ;; (file-writable-p buffer-file-name))
  1433. ;; (save-buffer))) ; 静かな方
  1434. (defvar my-auto-save-this-buffer nil "auto save timer object")
  1435. (defun my-auto-save-this-buffer (sec &optional silent-p)
  1436. "auto save current buffer if idle for SEC.
  1437. when SEC is nil, stop auto save if enabled."
  1438. (if sec
  1439. (progn (when my-auto-save-this-buffer
  1440. (cancel-timer my-auto-save-this-buffer)
  1441. (setq my-auto-save-this-buffer nil))
  1442. (setq my-auto-save-this-buffer (run-with-idle-timer sec t 'my-save-this-buffer silent-p)))
  1443. (when my-auto-save-this-buffer
  1444. (cancel-timer my-auto-save-this-buffer)
  1445. (setq my-auto-save-this-buffer nil))))
  1446. (my-auto-save-this-buffer 2 t)
  1447. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1448. ;; misc funcs
  1449. (defvar my-desktop-terminal "roxterm")
  1450. (defun my-execute-terminal ()
  1451. ""
  1452. (interactive)
  1453. (if (and (or (eq system-type 'windows-nt)
  1454. window-system)
  1455. my-desktop-terminal
  1456. )
  1457. (let ((process-environment (cons "TERM=xterm" process-environment)))
  1458. (start-process "terminal"
  1459. nil
  1460. my-desktop-terminal))
  1461. (my-execute-or-find-term)))
  1462. (defun my-term ()
  1463. "open terminal buffer and return that buffer."
  1464. (interactive)
  1465. (if (eq system-type 'windows-nt)
  1466. (eshell t)
  1467. (if (featurep 'multi-term)
  1468. (multi-term)
  1469. (ansi-term "/bin/bash"))))
  1470. (defvar my-frame-term-plist nil)
  1471. ;; (setplist my-frame-term-plist nil)
  1472. (defun my-execute-or-find-term ()
  1473. ""
  1474. (interactive)
  1475. (let* ((buf (plist-get my-frame-term-plist (selected-frame))))
  1476. (if (and buf
  1477. (buffer-name buf))
  1478. (switch-to-buffer buf)
  1479. (setq my-frame-term-plist
  1480. (plist-put my-frame-term-plist
  1481. (selected-frame)
  1482. (my-term))))))
  1483. (defun my-format-time-string (&optional time)
  1484. ""
  1485. (let ((system-time-locale "C"))
  1486. (format-time-string "%a, %d %b %Y %T" time)))
  1487. (defvar my-filer nil)
  1488. (setq my-filer (or (executable-find "pcmanfm")
  1489. (executable-find "nautilus")))
  1490. (defun my-x-open (file)
  1491. "open file."
  1492. (interactive "FOpen File: ")
  1493. (setq file (expand-file-name file))
  1494. (message "Opening %s..." file)
  1495. (cond ((eq system-type 'windows-nt)
  1496. (call-process "cmd.exe" nil 0 nil "/c" "start" "" (convert-standard-filename file)))
  1497. ((eq system-type 'darwin)
  1498. (call-process "open" nil 0 nil file))
  1499. ((not (getenv "DESKTOP_SESSION"))
  1500. (find-file file))
  1501. (t
  1502. (if (file-directory-p file)
  1503. (call-process my-filer nil 0 nil file)
  1504. (call-process "xdg-open" nil 0 nil file))))
  1505. (recentf-add-file file)
  1506. (message "Opening %s...done" file))
  1507. (defvar my-auto-indent-buffer-mode-list
  1508. '(emacs-lisp-mode
  1509. sh-mode
  1510. js-mode
  1511. sgml-mode
  1512. c-mode
  1513. c++-mode))
  1514. (setq my-auto-indent-buffer-mode-list nil) ;disable
  1515. (defun my-indent-buffer ()
  1516. "indent whole buffer."
  1517. (interactive)
  1518. (indent-region (point-min)
  1519. (point-max)))
  1520. (defun my-auto-indent-buffer ()
  1521. ""
  1522. (when (memq major-mode my-auto-indent-buffer-mode-list)
  1523. (my-indent-buffer)))
  1524. (add-hook 'before-save-hook
  1525. 'my-auto-indent-buffer)
  1526. (defun my-keyboard-quit ()
  1527. ""
  1528. (interactive)
  1529. (run-hooks 'before-keyboard-quit-hook)
  1530. ;; (redisplay t)
  1531. (redraw-display)
  1532. ;; (run-hooks 'window-configuration-change-hook)
  1533. (my-revert-buffer-if-needed)
  1534. ;; (revert-buffer t t)
  1535. (keyboard-quit)
  1536. (insert "insert me")
  1537. (run-hooks 'after-keyboard-quit-hook))
  1538. (substitute-key-definition 'keyboard-quit 'my-keyboard-quit global-map)
  1539. ;; (global-set-key (kbd "C-g") 'my-keyboard-quit)
  1540. (defun my-convmv-sjis2utf8-test ()
  1541. "run `convmv -r -f sjis -t utf8 *'
  1542. this is test, does not rename files"
  1543. (interactive)
  1544. (shell-command "convmv -r -f sjis -t utf8 *"))
  1545. (defun my-convmv-sjis2utf8-notest ()
  1546. "run `convmv -r -f sjis -t utf8 * --notest'"
  1547. (interactive)
  1548. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  1549. (defun my-copy-whole-line ()
  1550. ""
  1551. (interactive)
  1552. (kill-new (concat (buffer-substring (point-at-bol)
  1553. (point-at-eol))
  1554. "\n")))
  1555. (defun kill-ring-save-buffer-file-name ()
  1556. "get current filename"
  1557. (interactive)
  1558. (let ((file buffer-file-name))
  1559. (if file
  1560. (progn (kill-new file)
  1561. (message file))
  1562. (message "not visiting file."))))
  1563. ;; ;; コマンド的な
  1564. ;; (defvar my-execute-func-list nil "func list")
  1565. ;; (defvar my-execute-func-hist-list nil "func hist list")
  1566. ;; (setq my-execute-func-list '("(call-interactively 'my-francaiscd-b)"
  1567. ;; "(call-interactively 'my-francaiscd-a)"
  1568. ;; "parsec47"
  1569. ;; "chromium-browser"
  1570. ;; "inkscape"
  1571. ;; "audacious"
  1572. ;; "gnome-terminal"
  1573. ;; "zkaicd.py"
  1574. ;; "glchess"))
  1575. ;; (defun my-execute-start-process-or-eval-sexp ()
  1576. ;; "execute something"
  1577. ;; (interactive)
  1578. ;; (let ((func (completing-read "command?: " my-execute-func-list nil nil "" my-execute-func-hist-list)))
  1579. ;; (if (string= "(" (substring func 0 1))
  1580. ;; (with-temp-buffer (insert func)
  1581. ;; (eval-buffer))
  1582. ;; (start-process "ps"
  1583. ;; nil
  1584. ;; func))))
  1585. (defvar my-revert-buffer-if-needed-last-buffer nil)
  1586. (defun my-revert-buffer-if-needed ()
  1587. ""
  1588. (interactive)
  1589. (unless (eq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1590. (setq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1591. (when (or (eq major-mode 'dired-mode)
  1592. (not (verify-visited-file-modtime (current-buffer))))
  1593. (revert-buffer t t)
  1594. (message "%s reverted." (buffer-name)))))
  1595. (add-hook 'post-command-hook ; 'window-configuration-change-hook
  1596. 'my-revert-buffer-if-needed)
  1597. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1598. ;; forked from http://d.hatena.ne.jp/khiker/20100119/window_resize
  1599. (define-key my-prefix-map (kbd "C-w") 'my-window-organizer)
  1600. (defun my-window-organizer ()
  1601. "Control window size and position."
  1602. (interactive)
  1603. (save-selected-window
  1604. (select-window (window-at 0 0))
  1605. (let ( ;; (window-obj (selected-window))
  1606. ;; (current-width (window-width))
  1607. ;; (current-height (window-height))
  1608. action
  1609. c)
  1610. (catch 'end-flag
  1611. (while t
  1612. (setq action
  1613. (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."
  1614. (window-width)
  1615. (window-height))))
  1616. (setq c (aref action 0))
  1617. (cond ((= c ?l)
  1618. (unless (eq (window-width) (frame-width))
  1619. (enlarge-window-horizontally 1)))
  1620. ((= c ?h)
  1621. (unless (eq (window-width) (frame-width))
  1622. (shrink-window-horizontally 1)))
  1623. ((= c ?j)
  1624. (enlarge-window 1))
  1625. ((= c ?k)
  1626. (shrink-window 1))
  1627. ((= c ?o)
  1628. (other-window 1))
  1629. ((memq c '(?d ?0))
  1630. (unless (eq (selected-window) (next-window (selected-window) 0 1))
  1631. (delete-window (selected-window))))
  1632. ((= c ?1)
  1633. (delete-other-windows))
  1634. ((= c ?2)
  1635. (split-window-vertically))
  1636. ((= c ?3)
  1637. (split-window-horizontally))
  1638. ((memq c '(?q ?\C-g))
  1639. (message "Quit")
  1640. (throw 'end-flag t))
  1641. (t
  1642. (beep))))))))
  1643. ;; (aref (read-key-sequence-vector "aa") 0)
  1644. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1645. ;; save and restore frame size
  1646. ;;http://www.bookshelf.jp/soft/meadow_30.html#SEC416
  1647. (defun my-window-size-save ()
  1648. (let* ((rlist (frame-parameters (selected-frame)))
  1649. (ilist initial-frame-alist)
  1650. (nCHeight (frame-height))
  1651. (nCWidth (frame-width))
  1652. (tMargin (if (integerp (cdr (assoc 'top rlist)))
  1653. (cdr (assoc 'top rlist)) 0))
  1654. (lMargin (if (integerp (cdr (assoc 'left rlist)))
  1655. (cdr (assoc 'left rlist)) 0))
  1656. buf
  1657. (file "~/.emacs.d/.framesize.el")
  1658. (recentf-exclude '("\\.emacs\\.d/\\.framesize\\.el$")))
  1659. (if (get-file-buffer (expand-file-name file))
  1660. (setq buf (get-file-buffer (expand-file-name file)))
  1661. (setq buf (find-file-noselect file)))
  1662. (set-buffer buf)
  1663. (erase-buffer)
  1664. (insert (concat
  1665. ;; 初期値をいじるよりも modify-frame-parameters
  1666. ;; で変えるだけの方がいい?
  1667. "(delete 'width default-frame-alist)\n"
  1668. "(delete 'height default-frame-alist)\n"
  1669. "(delete 'top default-frame-alist)\n"
  1670. "(delete 'left default-frame-alist)\n"
  1671. "(setq default-frame-alist (append (list\n"
  1672. "'(width . " (int-to-string nCWidth) ")\n"
  1673. "'(height . " (int-to-string nCHeight) ")\n"
  1674. "'(top . " (int-to-string tMargin) ")\n"
  1675. "'(left . " (int-to-string lMargin) "))\n"
  1676. "default-frame-alist))\n"
  1677. ;;"(setq default-frame-alist default-frame-alist)"
  1678. ))
  1679. (save-buffer)
  1680. ))
  1681. (defun my-window-size-load ()
  1682. (let* ((file "~/.emacs.d/.framesize.el"))
  1683. (if (file-exists-p file)
  1684. (load file))))
  1685. (when window-system
  1686. (my-window-size-load)
  1687. (add-hook 'after-init-hook ;何かがframeの大きさ勝手に変えやがる
  1688. (lambda ()
  1689. (run-with-timer 1
  1690. nil
  1691. (lambda ()
  1692. (modify-frame-parameters (selected-frame)
  1693. default-frame-alist))))
  1694. t)
  1695. (add-hook 'kill-emacs-hook
  1696. 'my-window-size-save))
  1697. ;; windowサイズを固定
  1698. ;; setq default-frame-alist
  1699. ;; (append (list '(width . 80)
  1700. ;; '(height . 35)
  1701. ;; )
  1702. ;; default-frame-alist)
  1703. ;; ) ;;デフォルトのフレーム設定
  1704. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1705. ;; ;; emacsを殺伐とさせる
  1706. ;; ;; 補完させるとき失敗するからなし
  1707. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  1708. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  1709. ;; (setq arg
  1710. ;; (concat arg
  1711. ;; (if (eq nil (string-match "\\. *$" arg)) ".")
  1712. ;; " And You are a Coward!")))
  1713. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1714. ;; ubuntu
  1715. (defun my-load-scim ()
  1716. "use scim-bridge.el as japanese im."
  1717. ;; Load scim-bridge.
  1718. (require 'scim-bridge)
  1719. ;; Turn on scim-mode automatically after loading .emacs
  1720. (add-hook 'after-init-hook 'scim-mode-on)
  1721. (setq scim-cursor-color "red")
  1722. (scim-define-preedit-key ?\^h t)
  1723. (scim-define-common-key ?\* nil)
  1724. (scim-define-common-key ?\^/ nil))
  1725. (defun my-load-anthy ()
  1726. "use anthy.el as japanese im."
  1727. ;; anthy
  1728. (require 'anthy)
  1729. (global-set-key [muhenkan] (lambda () (interactive) (anthy-mode-off)))
  1730. (global-set-key [henkan] (lambda () (interactive) (anthy-mode-on)))
  1731. (when (>= emacs-major-version 23)
  1732. (setq anthy-accept-timeout 1)))
  1733. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1734. ;; windows用設定
  1735. ;; (add-to-list 'exec-path "c:/Program Files/Gauche/bin/")
  1736. (defun start-ckw-bash ()
  1737. ""
  1738. (interactive)
  1739. (start-process "ckw_bash"
  1740. nil
  1741. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe")) ; cじゃないといけないらしい
  1742. (defun my-w32-add-export-path (&rest args)
  1743. ""
  1744. (mapcar (lambda (path)
  1745. (add-to-list 'exec-path (expand-file-name path)))
  1746. (reverse args))
  1747. (setenv "PATH"
  1748. (mapconcat 'convert-standard-filename
  1749. exec-path
  1750. ";")))
  1751. (when (eq system-type 'windows-nt)
  1752. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  1753. ;; (setq python-python-command "c:/Python26/python.exe")
  1754. (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  1755. (my-w32-add-export-path "c:/WINDOWS"
  1756. (expand-file-name "~/bin")
  1757. (expand-file-name "~/dbx/apps/bin"))
  1758. (when window-system
  1759. (setq w32-enable-synthesized-fonts t))
  1760. (setq file-name-coding-system 'sjis))