No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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