Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

1971 рядки
67 KiB

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