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.
 
 
 
 
 
 

2023 lines
71 KiB

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