Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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