You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2741 lines
94 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Commentary:
  3. ;;; Code:
  4. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  5. ;; SETUP_LOAD: (and (file-readable-p file)
  6. ;; SETUP_LOAD: (load-file file)))
  7. ;; make directories
  8. (unless (file-directory-p (expand-file-name user-emacs-directory))
  9. (make-directory (expand-file-name user-emacs-directory)))
  10. (let ((d (expand-file-name (concat user-emacs-directory
  11. "lisp"))))
  12. (unless (file-directory-p d)
  13. (make-directory d))
  14. (add-to-list 'load-path d))
  15. (eval-when-compile
  16. ;; TODO: Use cl-lib intead
  17. (require 'cl nil t))
  18. ;; (add-hook 'after-change-major-mode-hook
  19. ;; (lambda ()
  20. ;; (message "cmm: %S %s"
  21. ;; major-mode
  22. ;; buffer-file-name)))
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;; Some macros for internals
  25. (defun call-after-init (func)
  26. "If `after-init-hook' has been run, call FUNC immediately.
  27. Otherwize hook it."
  28. (if after-init-time
  29. (funcall func)
  30. (add-hook 'after-init-hook
  31. func)))
  32. (defmacro defvar-set (symbol value &optional docstring)
  33. "Define SYMBOL as a variable and set to VALUE.
  34. Variable will be defined with DOCSTRING if given, otherwise do not set even
  35. VALUE when defining SYMBOL."
  36. `(set (if ,docstring
  37. (defvar ,symbol
  38. nil
  39. ,docstring)
  40. (defvar ,symbol))
  41. ,value))
  42. (defmacro safe-require-or-eval (feature)
  43. "Require FEATURE if available.
  44. At compile time the feature will be loaded immediately."
  45. `(eval-when-compile
  46. (require ,feature nil t)))
  47. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  48. "Define autoloading FEATURE that defines FUNCTIONS.
  49. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  50. the function same as FEATURE is defined as autoloaded function. BODY is passed
  51. to `eval-after-load'.
  52. After this macro is expanded, this returns the path to library if FEATURE
  53. found, otherwise returns nil."
  54. (let* ((libname (symbol-name (eval feature)))
  55. (libpath (locate-library libname)))
  56. (and libpath
  57. `(progn
  58. ,@(mapcar (lambda (f)
  59. (unless (fboundp f)
  60. `(progn
  61. (message "Autoloaded function `%S' defined (%s)"
  62. (quote ,f)
  63. ,libpath)
  64. (autoload (quote ,f)
  65. ,libname
  66. ,(concat "Autoloaded function defined in \""
  67. libpath
  68. "\".")
  69. t))))
  70. (or (eval functions)
  71. `(,(eval feature))))
  72. (eval-after-load ,feature
  73. (quote (progn
  74. ,@body)))
  75. (locate-library ,libname)))))
  76. (put 'autoload-eval-lazily 'lisp-indent-function 2)
  77. (when (autoload-eval-lazily 'tetris nil
  78. (message "Tetris loaded!"))
  79. (message "Tetris found!"))
  80. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  81. ;; download library from web
  82. (defvar fetch-library-enabled-p t
  83. "Set nil to skip downloading with `fetch-library'.")
  84. (defun fetch-library (url &optional byte-compile-p force-download-p)
  85. "Download a library from URL and locate it in \"~/emacs.d/lisp/\".
  86. Return nil if library unfound and failed to download,
  87. otherwise the path where the library installed.
  88. If BYTE-COMPILE-P is t byte compile the file after downloading.
  89. If FORCE-DOWNLOAD-P it t ignore exisiting library and always download.
  90. This function also checks the value of `fetch-library-enabled-p' and do not
  91. fetch libraries if this value is nil. In this case all arguments (including
  92. FORCE-DOWNLOAD-P) will be ignored."
  93. (let* ((dir (expand-file-name (concat user-emacs-directory "lisp/")))
  94. (lib (file-name-sans-extension (file-name-nondirectory url)))
  95. (lpath (concat dir lib ".el"))
  96. (locate-p (locate-library lib)))
  97. (if (and fetch-library-enabled-p
  98. (or force-download-p
  99. (not locate-p)))
  100. (if (progn (message "Downloading %s..."
  101. url)
  102. (download-file url
  103. lpath
  104. t))
  105. (progn (message "Downloading %s...done"
  106. url)
  107. (when (and byte-compile-p
  108. (require 'bytecomp nil t))
  109. (and (file-exists-p (byte-compile-dest-file lpath))
  110. (delete-file (byte-compile-dest-file lpath)))
  111. (message "Byte-compiling %s..."
  112. lpath)
  113. (byte-compile-file lpath)
  114. (message "Byte-compiling %s...done"
  115. lpath)))
  116. (progn (and (file-writable-p lpath)
  117. (delete-file lpath))
  118. (message "Downloading %s...failed"
  119. url))))
  120. (locate-library lib)))
  121. ;; If EMACS_EL_DRY_RUN is set and it is not an empty string, fetch-library
  122. ;; does not actually fetch library.
  123. (let ((dryrun (getenv "EMACS_EL_DRY_RUN")))
  124. (when (and dryrun
  125. (< 0
  126. (length dryrun)))
  127. (setq fetch-library-enabled-p
  128. nil)
  129. (message "EMACS_EL_DRY_RUN is set. Skip fetching libraries.")))
  130. (defun download-file (url path &optional ok-if-already-exists)
  131. "Download file from URL and output to PATH.
  132. IF OK-IF-ALREADY-EXISTS is true force download."
  133. (let ((curl (executable-find "curl"))
  134. (wget (executable-find "wget")))
  135. (cond (wget
  136. (if (and (not ok-if-already-exists)
  137. (file-exists-p path))
  138. nil
  139. (and (eq 0
  140. (call-process wget
  141. nil
  142. nil
  143. nil
  144. "-O"
  145. path
  146. url
  147. ))
  148. path)))
  149. (curl
  150. (if (and (not ok-if-already-exists)
  151. (file-exists-p path))
  152. nil
  153. (and (eq 0
  154. (call-process curl
  155. nil
  156. nil
  157. nil
  158. "--output"
  159. path
  160. "-L"
  161. url
  162. ))
  163. path)))
  164. (t
  165. (ignore-errors
  166. (require 'url)
  167. (url-copy-file url
  168. path
  169. ok-if-already-exists)
  170. path)))))
  171. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  172. ;; package
  173. (defvar-set my-package-list
  174. '(
  175. markdown-mode
  176. yaml-mode
  177. gnuplot-mode
  178. erlang
  179. js2-mode
  180. git-commit-mode
  181. gitignore-mode
  182. ;; ack
  183. color-moccur
  184. gtags
  185. flycheck
  186. ;; is flymake installs are required?
  187. ;;flymake-jshint
  188. ;;flymake-python-pyflakes
  189. xclip
  190. foreign-regexp
  191. multi-term
  192. dirtree
  193. term-run
  194. editorconfig
  195. git-ps1-mode
  196. restart-emacs
  197. scala-mode2
  198. ensime
  199. editorconfig
  200. )
  201. "Package list just for me.")
  202. (when (safe-require-or-eval 'package)
  203. ;; (add-to-list 'package-archives
  204. ;; '("ELPA" . "http://tromey.com/elpa/"))
  205. (add-to-list 'package-archives
  206. '("melpa" . "https://melpa.org/packages/")
  207. t)
  208. (add-to-list 'package-archives
  209. '("marmalade" . "http://marmalade-repo.org/packages/"))
  210. (package-initialize)
  211. (defun my-auto-install-package ()
  212. "Install packages semi-automatically."
  213. (interactive)
  214. (package-refresh-contents)
  215. (mapc (lambda (pkg)
  216. (or (package-installed-p pkg)
  217. (locate-library (symbol-name pkg))
  218. (package-install pkg)))
  219. my-package-list))
  220. )
  221. ;; (lazy-load-eval 'sudoku)
  222. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  223. ;; my-idle-hook
  224. (defvar my-idle-hook nil
  225. "Hook run when idle for several secs.")
  226. (defvar my-idle-hook-sec 5
  227. "Second to run `my-idle-hook'.")
  228. (run-with-idle-timer my-idle-hook-sec
  229. t
  230. (lambda ()
  231. (run-hooks 'my-idle-hook)))
  232. ;; (add-hook 'my-idle-hook
  233. ;; (lambda ()
  234. ;; (message "idle hook message")))
  235. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  236. ;; start and quit
  237. (setq inhibit-startup-message t)
  238. (setq confirm-kill-emacs 'y-or-n-p)
  239. (setq gc-cons-threshold (* 1024 1024 4))
  240. (when window-system
  241. (add-to-list 'default-frame-alist '(cursor-type . box))
  242. (add-to-list 'default-frame-alist '(background-color . "white"))
  243. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  244. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  245. ;; does not work?
  246. )
  247. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  248. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  249. (and (fboundp 'tool-bar-mode)
  250. (tool-bar-mode 0))
  251. (and (fboundp 'set-scroll-bar-mode)
  252. (set-scroll-bar-mode nil))
  253. (add-hook 'kill-emacs-hook
  254. ;; load init file when terminating emacs to ensure file is not broken
  255. 'reload-init-file)
  256. (defun my-force-kill-emacs ()
  257. "My force kill Emacs."
  258. (interactive)
  259. (let ((kill-emacs-hook nil))
  260. (kill-emacs)))
  261. (call-after-init
  262. (lambda ()
  263. (message "%s %s" invocation-name emacs-version)
  264. (message "%s was taken to initialize emacs." (emacs-init-time))
  265. (switch-to-buffer "*Messages*")))
  266. (cd ".") ; when using windows use / instead of \ in `default-directory'
  267. ;; locale
  268. (set-language-environment "Japanese")
  269. (set-default-coding-systems 'utf-8-unix)
  270. (prefer-coding-system 'utf-8-unix)
  271. (setq system-time-locale "C")
  272. ;; my prefix map
  273. (defvar my-prefix-map nil
  274. "My prefix map.")
  275. (define-prefix-command 'my-prefix-map)
  276. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  277. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  278. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  279. ;; (comint-show-maximum-output)
  280. ;; kill scratch
  281. (call-after-init (lambda ()
  282. (let ((buf (get-buffer "*scratch*")))
  283. (when buf
  284. (kill-buffer buf)))))
  285. ;; modifier keys
  286. ;; (setq mac-option-modifier 'control)
  287. ;; display
  288. (setq visible-bell t)
  289. (setq ring-bell-function 'ignore)
  290. (mouse-avoidance-mode 'banish)
  291. (and window-system
  292. (fetch-library
  293. "https://raw.github.com/10sr/emacs-lisp/master/save-window-size.el"
  294. t)
  295. (safe-require-or-eval 'save-window-size))
  296. (defun reload-init-file ()
  297. "Reload Emacs init file."
  298. (interactive)
  299. (when (and user-init-file
  300. (file-readable-p user-init-file))
  301. (load-file user-init-file)))
  302. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  303. ;; for windows
  304. (defun start-ckw-bash ()
  305. "Start ckw in windows."
  306. (interactive)
  307. (start-process
  308. "ckw_bash"
  309. nil
  310. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe"))
  311. ;; command seems to have to be in c drive
  312. (defun my-w32-add-export-path (&rest args)
  313. "Add pathes ARGS for windows."
  314. (mapc (lambda (path)
  315. (add-to-list 'exec-path (expand-file-name path)))
  316. (reverse args))
  317. (setenv "PATH"
  318. (mapconcat 'convert-standard-filename
  319. exec-path
  320. ";")))
  321. (when (eq system-type 'windows-nt)
  322. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  323. ;; (setq python-python-command "c:/Python26/python.exe")
  324. ;; (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  325. (my-w32-add-export-path "c:/Windows/system"
  326. "c:/Windows/System32"
  327. "c:/Program Files/Git/bin"
  328. "c:/MinGW/bin"
  329. "c:/MinGW/mingw32/bin"
  330. (expand-file-name "~/.local/bin")
  331. (expand-file-name "~/dbx/apps/bin"))
  332. (when window-system
  333. (defvar-set w32-enable-synthesized-fonts t))
  334. (defvar-set w32-apps-modifier 'meta)
  335. (setq file-name-coding-system 'sjis))
  336. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  337. ;; global keys
  338. (global-set-key (kbd "<up>") 'scroll-down-line)
  339. (global-set-key (kbd "<down>") 'scroll-up-line)
  340. (global-set-key (kbd "<left>") 'scroll-down)
  341. (global-set-key (kbd "<right>") 'scroll-up)
  342. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  343. (global-set-key (kbd "C-\\") help-map)
  344. (define-key ctl-x-map (kbd "DEL") help-map)
  345. (define-key ctl-x-map (kbd "C-h") help-map)
  346. (define-key help-map "a" 'apropos)
  347. ;; disable annoying keys
  348. (global-set-key [prior] 'ignore)
  349. (global-set-key (kbd "<next>") 'ignore)
  350. (global-set-key [menu] 'ignore)
  351. (global-set-key [down-mouse-1] 'ignore)
  352. (global-set-key [down-mouse-2] 'ignore)
  353. (global-set-key [down-mouse-3] 'ignore)
  354. (global-set-key [mouse-1] 'ignore)
  355. (global-set-key [mouse-2] 'ignore)
  356. (global-set-key [mouse-3] 'ignore)
  357. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  358. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  359. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  360. ;; title and mode-line
  361. (when (and (fetch-library
  362. "https://raw.github.com/10sr/emacs-lisp/master/terminal-title.el"
  363. t)
  364. (safe-require-or-eval 'terminal-title))
  365. ;; if TERM is not screen use default value
  366. (if (getenv "TMUX")
  367. ;; if use tmux locally just basename of current dir
  368. (setq terminal-title-format
  369. '((file-name-nondirectory (directory-file-name
  370. default-directory))))
  371. (if (and (let ((tty-type (frame-parameter nil
  372. 'tty-type)))
  373. (and tty-type
  374. (equal (car (split-string tty-type
  375. "-"))
  376. "screen")))
  377. (not (getenv "SSH_CONNECTION")))
  378. (setq terminal-title-format
  379. '((file-name-nondirectory (directory-file-name
  380. default-directory))))
  381. ;; seems that TMUX is used locally and ssh to remote host
  382. (setq terminal-title-format
  383. `("em:"
  384. ,user-login-name
  385. "@"
  386. ,(car (split-string system-name
  387. "\\."))
  388. ":"
  389. default-directory))
  390. )
  391. )
  392. (terminal-title-mode))
  393. (setq eol-mnemonic-dos "\\r\\n")
  394. (setq eol-mnemonic-mac "\\r")
  395. (setq eol-mnemonic-unix "\\n")
  396. (which-function-mode 0)
  397. (line-number-mode 0)
  398. (column-number-mode 0)
  399. (size-indication-mode 0)
  400. (setq mode-line-position
  401. '(:eval (format "L%%l/%d,C%%c"
  402. (count-lines (point-max)
  403. (point-min)))))
  404. (when (require 'git-ps1-mode nil t)
  405. (git-ps1-mode))
  406. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  407. ;; display date
  408. (call-after-init (lambda ()
  409. (when display-time-mode
  410. (display-time-update))))
  411. (when (safe-require-or-eval 'time)
  412. (setq display-time-interval 29)
  413. (setq display-time-day-and-date t)
  414. (setq display-time-format "%a, %d %b %Y %T")
  415. (if window-system
  416. (display-time-mode 0)
  417. (display-time-mode 1)))
  418. ;; ;; current directory
  419. ;; (let ((ls (member 'mode-line-buffer-identification
  420. ;; mode-line-format)))
  421. ;; (setcdr ls
  422. ;; (cons '(:eval (concat " ("
  423. ;; (abbreviate-file-name default-directory)
  424. ;; ")"))
  425. ;; (cdr ls))))
  426. ;; ;; display last modified time
  427. ;; (let ((ls (member 'mode-line-buffer-identification
  428. ;; mode-line-format)))
  429. ;; (setcdr ls
  430. ;; (cons '(:eval (concat " "
  431. ;; my-buffer-file-last-modified-time))
  432. ;; (cdr ls))))
  433. (defun buffer-list-not-start-with-space ()
  434. "Return a list of buffers that not start with whitespaces."
  435. (let ((bl (buffer-list))
  436. b nbl)
  437. (while bl
  438. (setq b (pop bl))
  439. (unless (string-equal " "
  440. (substring (buffer-name b)
  441. 0
  442. 1))
  443. (add-to-list 'nbl b)))
  444. nbl))
  445. ;; http://www.masteringemacs.org/articles/2012/09/10/hiding-replacing-modeline-strings/
  446. ;; (add-to-list 'minor-mode-alist
  447. ;; '(global-whitespace-mode ""))
  448. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  449. ;; system info
  450. (defun my-message-current-info ()
  451. "Echo current login name, hostname and directory."
  452. (interactive)
  453. (message "%s@%s:%s"
  454. user-login-name
  455. system-name
  456. (abbreviate-file-name default-directory)))
  457. ;; (run-with-idle-timer 3
  458. ;; t
  459. ;; 'my-message-current-info)
  460. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  461. ;; minibuffer
  462. (setq insert-default-directory t)
  463. (setq completion-ignore-case t
  464. read-file-name-completion-ignore-case t
  465. read-buffer-completion-ignore-case t)
  466. (setq resize-mini-windows t)
  467. (temp-buffer-resize-mode 1)
  468. (savehist-mode 1)
  469. (fset 'yes-or-no-p 'y-or-n-p)
  470. ;; complete symbol when `eval'
  471. (define-key read-expression-map (kbd "TAB") 'lisp-complete-symbol)
  472. (define-key minibuffer-local-map (kbd "C-u")
  473. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  474. ;; I dont know these bindings are good
  475. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  476. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  477. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  478. ;; letters, font-lock mode and fonts
  479. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  480. ;; (set-window-margins (selected-window) 1 1)
  481. (and (or (eq system-type 'Darwin)
  482. (eq system-type 'darwin))
  483. (fboundp 'mac-set-input-method-parameter)
  484. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  485. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  486. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  487. (boundp 'input-method-inactivate-hook))
  488. (add-hook 'input-method-activate-hook
  489. (lambda () (set-cursor-color "red")))
  490. (add-hook 'input-method-inactivate-hook
  491. (lambda () (set-cursor-color "black"))))
  492. (when (safe-require-or-eval 'paren)
  493. (show-paren-mode 1)
  494. (setq show-paren-delay 0.5
  495. show-paren-style 'parenthesis) ; mixed is hard to read
  496. ;; (set-face-background 'show-paren-match
  497. ;; "black")
  498. ;; ;; (face-foreground 'default))
  499. ;; (set-face-foreground 'show-paren-match
  500. ;; "white")
  501. ;; (set-face-inverse-video-p 'show-paren-match
  502. ;; t)
  503. )
  504. (transient-mark-mode 1)
  505. (global-font-lock-mode 1)
  506. (setq font-lock-global-modes
  507. '(not
  508. help-mode
  509. eshell-mode
  510. term-mode
  511. Man-mode))
  512. ;; (standard-display-ascii ?\n "$\n")
  513. (defvar my-eol-face
  514. '(("\n" . (0 font-lock-comment-face t nil)))
  515. )
  516. (defvar my-tab-face
  517. '(("\t" . '(0 highlight t nil))))
  518. (defvar my-jspace-face
  519. '(("\u3000" . '(0 highlight t nil))))
  520. (add-hook 'font-lock-mode-hook
  521. (lambda ()
  522. ;; (font-lock-add-keywords nil my-eol-face)
  523. (font-lock-add-keywords nil my-jspace-face)
  524. ))
  525. (when (safe-require-or-eval 'whitespace)
  526. (add-to-list 'whitespace-display-mappings ; not work
  527. `(tab-mark ?\t ,(vconcat "^I\t")))
  528. (add-to-list 'whitespace-display-mappings
  529. `(newline-mark ?\n ,(vconcat "$\n")))
  530. (setq whitespace-style '(face
  531. trailing ; trailing blanks
  532. newline ; newlines
  533. newline-mark ; use display table for newline
  534. ;; tab-mark
  535. empty ; empty lines at beg or end of buffer
  536. lines-tail ; lines over 80
  537. ))
  538. ;; (setq whitespace-newline 'font-lock-comment-face)
  539. (global-whitespace-mode t)
  540. (if (eq (display-color-cells)
  541. 256)
  542. (set-face-foreground 'whitespace-newline "brightblack")
  543. ;; (progn
  544. ;; (set-face-bold-p 'whitespace-newline
  545. ;; t))
  546. ))
  547. (and nil
  548. (fetch-library
  549. "http://www.emacswiki.org/emacs/download/fill-column-indicator.el"
  550. t)
  551. (safe-require-or-eval 'fill-column-indicator)
  552. (setq fill-column-indicator))
  553. ;; highlight current line
  554. ;; http://wiki.riywo.com/index.php?Meadow
  555. (defface my-hl-line
  556. '((((min-colors 256)
  557. (background dark))
  558. (:background "color-234"))
  559. (((min-colors 256)
  560. (background light))
  561. (:background "color-234"))
  562. (t
  563. (:underline "black")))
  564. "*Face used by hl-line.")
  565. (defvar-set hl-line-face 'my-hl-line) ;; (setq hl-line-face nil)
  566. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  567. (defvar-set hl-line-global-modes
  568. '(not
  569. term-mode))
  570. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  571. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  572. ;; fonts
  573. (defun my-set-ascii-and-jp-font (list)
  574. "Set font configuration to LIST."
  575. (let ((fspec1 (if (> emacs-major-version 22)
  576. ;; font spec is available in emacs23 and later
  577. (font-spec :family (nth 2 list) :size (nth 3 list))
  578. (cons (nth 2 list) "jisx0208.*")))
  579. (fspec2 (if (> emacs-major-version 22)
  580. (font-spec :family (nth 2 list) :size (nth 3 list))
  581. (cons (nth 2 list) "jisx0201.*"))))
  582. (set-face-attribute 'default nil
  583. :family (nth 0 list)
  584. :height (nth 1 list))
  585. (set-fontset-font "fontset-default"
  586. 'japanese-jisx0208
  587. fspec1)
  588. (set-fontset-font "fontset-default"
  589. 'katakana-jisx0201
  590. fspec2)))
  591. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 90 "takaogothic" 13))
  592. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "takaogothic" 14))
  593. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "ms gothic" 14))
  594. ;; (my-set-ascii-and-jp-font '("monaco" 75 "takaogothic" 11))
  595. ;; (my-set-ascii-and-jp-font '("monaco" 90 "takaogothic" 13))
  596. ;; (my-set-ascii-and-jp-font '("ProggyCleanTTSZ" 120 "takaogothic" 11))
  597. ;; あ a
  598. (and (fetch-library
  599. "https://raw.github.com/10sr/emacs-lisp/master/set-modeline-color.el"
  600. t)
  601. (progn
  602. (safe-require-or-eval 'set-modeline-color)))
  603. (let ((fg (face-foreground 'default))
  604. (bg (face-background 'default)))
  605. (set-face-background 'mode-line-inactive
  606. (if (face-inverse-video-p 'mode-line) fg bg))
  607. (set-face-foreground 'mode-line-inactive
  608. (if (face-inverse-video-p 'mode-line) bg fg)))
  609. (set-face-underline 'mode-line-inactive
  610. t)
  611. (set-face-underline 'vertical-border
  612. nil)
  613. (and (fetch-library
  614. "https://raw.github.com/tarao/elisp/master/end-mark.el"
  615. t)
  616. (safe-require-or-eval 'end-mark)
  617. (global-end-mark-mode))
  618. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  619. ;; file handling
  620. (when (safe-require-or-eval 'editorconfig)
  621. (editorconfig-mode 1))
  622. (setq revert-without-query '(".+"))
  623. ;; save cursor position
  624. (when (safe-require-or-eval 'saveplace)
  625. (setq-default save-place t)
  626. (setq save-place-file (concat user-emacs-directory
  627. "places")))
  628. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  629. (setq make-backup-files t)
  630. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  631. (setq backup-directory-alist
  632. (cons (cons "\\.*$" (expand-file-name (concat user-emacs-directory
  633. "backup")))
  634. backup-directory-alist))
  635. (setq version-control 'never)
  636. (setq delete-old-versions t)
  637. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  638. "auto-save/")))
  639. (setq delete-auto-save-files t)
  640. (add-to-list 'completion-ignored-extensions ".bak")
  641. ;; (setq delete-by-moving-to-trash t
  642. ;; trash-directory "~/.emacs.d/trash")
  643. (add-hook 'after-save-hook
  644. 'executable-make-buffer-file-executable-if-script-p)
  645. (defvar-set bookmark-default-file (concat user-emacs-directory
  646. "bmk"))
  647. (add-hook 'recentf-load-hook
  648. (lambda ()
  649. (defvar recentf-exclude)
  650. (add-to-list 'recentf-exclude
  651. (regexp-quote bookmark-default-file))))
  652. (and (fetch-library
  653. "https://raw.github.com/10sr/emacs-lisp/master/read-only-only-mode.el"
  654. t)
  655. (autoload-eval-lazily 'read-only-only-mode))
  656. (and (fetch-library
  657. "https://raw.github.com/10sr/emacs-lisp/master/smart-revert.el"
  658. t)
  659. (safe-require-or-eval 'smart-revert)
  660. (smart-revert-on))
  661. ;; autosave
  662. (and (fetch-library
  663. "https://raw.github.com/10sr/emacs-lisp/master/autosave.el"
  664. t)
  665. (safe-require-or-eval 'autosave)
  666. (autosave-set 2))
  667. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  668. ;; editting
  669. (defun my-copy-whole-line ()
  670. "Copy whole line."
  671. (interactive)
  672. (kill-new (concat (buffer-substring (point-at-bol)
  673. (point-at-eol))
  674. "\n")))
  675. (setq require-final-newline t)
  676. (setq kill-whole-line t)
  677. (setq scroll-conservatively 35
  678. scroll-margin 2
  679. scroll-step 0)
  680. (setq-default major-mode 'text-mode)
  681. (setq next-line-add-newlines nil)
  682. (setq kill-read-only-ok t)
  683. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  684. ;; (setq-default line-spacing 0.2)
  685. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  686. (setq-default tab-width 4)
  687. (setq-default indent-tabs-mode nil)
  688. (setq-default indent-line-function nil)
  689. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  690. (delete-selection-mode 1)
  691. (cua-mode 0)
  692. (setq line-move-visual nil)
  693. ;; key bindings
  694. ;; moving around
  695. ;; (global-set-key (kbd "M-j") 'next-line)
  696. ;; (global-set-key (kbd "M-k") 'previous-line)
  697. ;; (global-set-key (kbd "M-h") 'backward-char)
  698. ;; (global-set-key (kbd "M-l") 'forward-char)
  699. ;;(keyboard-translate ?\M-j ?\C-j)
  700. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  701. (define-key esc-map "p" 'backward-paragraph)
  702. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  703. (define-key esc-map "n" 'forward-paragraph)
  704. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  705. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  706. (global-set-key (kbd "C-<left>") 'scroll-down)
  707. (global-set-key (kbd "C-<right>") 'scroll-up)
  708. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  709. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  710. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  711. ;; C-h and DEL
  712. (global-set-key (kbd "C-h") (kbd "DEL"))
  713. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  714. (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  715. (define-key esc-map "k" 'my-copy-whole-line)
  716. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  717. (define-key esc-map "u" 'undo)
  718. (define-key esc-map "i" (kbd "ESC TAB"))
  719. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  720. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  721. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  722. (define-key my-prefix-map (kbd "C-o") 'occur)
  723. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  724. ;; japanese input method
  725. (defun my-load-scim ()
  726. "Use scim-bridge.el as japanese im."
  727. ;; Load scim-bridge.
  728. (when (safe-require-or-eval 'scim-bridge)
  729. ;; Turn on scim-mode automatically after loading .emacs
  730. (call-after-init 'scim-mode-on)
  731. (setq scim-cursor-color "red")
  732. (scim-define-preedit-key ?\^h t)
  733. (scim-define-common-key ?\* nil)
  734. (scim-define-common-key ?\^/ nil)))
  735. (defun my-load-anthy ()
  736. "Use anthy.el as japanese im."
  737. ;; anthy
  738. (when (safe-require-or-eval 'anthy)
  739. (global-set-key
  740. (kbd "<muhenkan>") (lambda () (interactive) (anthy-mode-off)))
  741. (global-set-key (kbd "<henkan>") (lambda () (interactive) (anthy-mode-on)))
  742. (when (>= emacs-major-version 23)
  743. (setq anthy-accept-timeout 1))))
  744. ;; quail
  745. ;; aproposs input-method for some information
  746. ;; (setq default-input-method "japanese")
  747. (defun my-load-mozc-el ()
  748. "Use mozc.el as japanese im."
  749. (when (safe-require-or-eval 'mozc)
  750. (setq defauit-input-method "japanese-mozc")
  751. (setq mozc-leim-title "[MZ]")
  752. ))
  753. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  754. ;; gmail
  755. (setq mail-interactive t
  756. send-mail-function 'smtpmail-send-it
  757. ;; message-send-mail-function 'smtpmail-send-it
  758. smtpmail-smtp-server "smtp.gmail.com"
  759. smtpmail-smtp-service 587
  760. smtpmail-starttls-credentials '(("smtp.gmail.com" 587
  761. "8.slashes@gmail.com" nil))
  762. smtpmail-auth-credentials '(("smtp.gmail.com" 587
  763. "8.slashes@gmail.com" nil))
  764. user-mail-address "8.slashes@gmail.com")
  765. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  766. ;; buffer killing
  767. ;; (defun my-delete-window-killing-buffer () nil)
  768. (defun my-query-kill-current-buffer ()
  769. "Interactively kill current buffer."
  770. (interactive)
  771. (if (y-or-n-p (concat "kill current buffer? :"))
  772. (kill-buffer (current-buffer))))
  773. (substitute-key-definition 'kill-buffer
  774. 'my-query-kill-current-buffer
  775. global-map)
  776. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  777. (defun my-kill-buffers ()
  778. "Kill buffers that visit files."
  779. (interactive)
  780. (mapcar (lambda (buf)
  781. (when (buffer-file-name buf)
  782. (kill-buffer buf)))
  783. (buffer-list)))
  784. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  785. ;; share clipboard with x
  786. ;; this page describes this in details, but only these sexps seem to be needed
  787. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  788. (and (not window-system)
  789. (not (eq window-system 'mac))
  790. (getenv "DISPLAY")
  791. (not (equal (getenv "DISPLAY") ""))
  792. (executable-find "xclip")
  793. ;; (< emacs-major-version 24)
  794. (safe-require-or-eval 'xclip)
  795. nil
  796. (turn-on-xclip))
  797. (and (eq system-type 'darwin)
  798. (fetch-library
  799. "https://raw.github.com/10sr/emacs-lisp/master/pasteboard.el"
  800. t)
  801. (safe-require-or-eval 'pasteboard)
  802. (turn-on-pasteboard)
  803. (getenv "TMUX")
  804. (pasteboard-enable-rtun))
  805. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  806. ;; https://github.com/lunaryorn/flycheck
  807. (when (safe-require-or-eval 'flycheck)
  808. (call-after-init 'global-flycheck-mode))
  809. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  810. ;; window
  811. '(and (fetch-library
  812. "https://raw.github.com/10sr/emacs-lisp/master/window-organizer.el"
  813. t)
  814. (autoload-eval-lazily 'window-organizer)
  815. (define-key ctl-x-map (kbd "w") 'window-organizer))
  816. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  817. ;; server
  818. (when (safe-require-or-eval 'server)
  819. (setq server-name (concat "server"
  820. (number-to-string (emacs-pid))))
  821. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  822. ;; In Darwin environment, init fails with message like 'Service name too long'
  823. ;; when server-use-tcp is nil
  824. (when (or (eq system-type
  825. 'cygwin)
  826. (eq system-type
  827. 'darwin))
  828. (setq server-use-tcp t))
  829. (defun my-construct-emacsclient-editor-command ()
  830. "Construct and return command in a string to connect to current Emacs server."
  831. (if server-use-tcp
  832. (format "%s -f \"%s/%s\""
  833. "emacsclient"
  834. (expand-file-name server-auth-dir)
  835. server-name)
  836. (format "%s -s \"%s/%s\""
  837. "emacsclient"
  838. server-socket-dir
  839. server-name)))
  840. (setq process-environment
  841. `(,(concat "EDITOR="
  842. (my-construct-emacsclient-editor-command))
  843. ,(concat "GIT_EDITOR="
  844. (my-construct-emacsclient-editor-command))
  845. ,@process-environment))
  846. (server-start))
  847. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  848. ;; some modes and hooks
  849. (defvar-set ac-ignore-case nil)
  850. ;; (when (require 'ensime nil t)
  851. ;; (defvar-set ensime-ac-case-sensitive t)
  852. ;; (defvar-set ensime-company-case-sensitive t)
  853. ;; (add-hook 'scala-mode-hook
  854. ;; 'ensime-scala-mode-hook)
  855. ;; (add-hook 'ensime-scala-mode-hook
  856. ;; 'ac-stop))
  857. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  858. (define-key ctl-x-map "t" 'term-run-shell-command))
  859. (add-to-list 'safe-local-variable-values
  860. '(encoding utf-8))
  861. (setq enable-local-variables :safe)
  862. (when (autoload-eval-lazily 'dirtree nil
  863. (defun my-dirtree-current-line-directory-p ()
  864. "Return nil if element on current line is not a directory."
  865. (file-directory-p (widget-get (tree-mode-button-current-line)
  866. :file)))
  867. ;; This fix is actually a little strange. Strictly speaking
  868. ;; judging tree should be done by whether the widget is a tree one.
  869. (defun my-dirtree-next-node (arg)
  870. "Fix the problem that `tree-mode-next-node' moves cursor 2 lines."
  871. (interactive "p")
  872. (if (my-dirtree-current-line-directory-p)
  873. (widget-forward (* arg 2))
  874. (widget-forward arg)))
  875. (defun my-dirtree-previous-node (arg)
  876. "Fix the problem that `tree-mode-previous-node' moves cursor 2 lines."
  877. (interactive "p")
  878. (my-dirtree-next-node (- arg)))
  879. (define-key dirtree-mode-map "n" 'my-dirtree-next-node)
  880. (define-key dirtree-mode-map "p" 'my-dirtree-previous-node))
  881. (define-key ctl-x-map "d" 'dirtree))
  882. (and (fetch-library
  883. "https://raw.github.com/10sr/emacs-lisp/master/remember-major-modes-mode.el"
  884. t)
  885. (safe-require-or-eval 'remember-major-modes-mode)
  886. (remember-major-modes-mode 1)
  887. )
  888. ;; Detect file type from shebang and set major-mode.
  889. (add-to-list 'interpreter-mode-alist
  890. '("python3" . python-mode))
  891. (add-to-list 'interpreter-mode-alist
  892. '("python2" . python-mode))
  893. ;; http://fukuyama.co/foreign-regexp
  894. '(and (safe-require-or-eval 'foreign-regexp)
  895. (progn
  896. (setq foreign-regexp/regexp-type 'perl)
  897. '(setq reb-re-syntax 'foreign-regexp)
  898. ))
  899. (safe-require-or-eval 'session)
  900. (autoload-eval-lazily 'sql '(sql-mode)
  901. (safe-require-or-eval 'sql-indent))
  902. '(and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/gtkbm.el"
  903. t)
  904. (autoload-eval-lazily 'gtkbm)
  905. (global-set-key (kbd "C-x C-d") 'gtkbm))
  906. (and (fetch-library
  907. "https://raw.github.com/10sr/git-command-el/master/git-command.el"
  908. t)
  909. (autoload-eval-lazily 'git-command
  910. nil
  911. ;; for git-command old version
  912. (when (boundp 'git-command-major-mode-alist)
  913. (message "You are using old git-command ! Update it !!!")
  914. (add-to-list 'git-command-major-mode-alist
  915. '("di" . diff-mode))
  916. (add-to-list 'git-command-major-mode-alist
  917. '("graph" . fundamental-mode))
  918. (add-to-list 'git-command-major-mode-alist
  919. '("log" . fundamental-mode)))
  920. ;; for git-command new version
  921. (when (boundp 'git-command-view-command-list)
  922. (add-to-list 'git-command-view-command-list
  923. "graph")
  924. (add-to-list 'git-command-view-command-list
  925. "blame")
  926. (add-to-list 'git-command-view-command-list
  927. "help"))
  928. (when (boundp 'git-command-aliases-alist)
  929. ;; (message "new version of git-command!")
  930. (add-to-list 'git-command-aliases-alist
  931. '("di" . (lambda (options cmd args new-buffer-p)
  932. (git-command-exec options
  933. "diff"
  934. args
  935. new-buffer-p))))
  936. (add-to-list 'git-command-aliases-alist
  937. '("grep" . (lambda (options cmd args new-buffer-p)
  938. (my-rgrep
  939. (concat
  940. "git "
  941. (git-command-construct-commandline
  942. `(,@options "--no-pager"
  943. "-c" "color.grep=false")
  944. cmd
  945. `("-nHe" ,@args))))))))
  946. (setq git-command-use-emacsclient t)
  947. (or git-command-prompt-file
  948. (setq git-command-prompt-file
  949. (git-command-find-git-ps1
  950. "/usr/share/git-core/contrib/completion/git-prompt.sh"))))
  951. ;; (setq git-command-default-options "-c color.ui=always")
  952. (define-key ctl-x-map "g" 'git-command))
  953. (and (fetch-library
  954. "http://www.emacswiki.org/emacs/download/sl.el"
  955. t)
  956. (autoload-eval-lazily 'sl))
  957. (defalias 'qcalc 'quick-calc)
  958. (safe-require-or-eval 'simple)
  959. (add-hook 'makefile-mode-hook
  960. (lambda ()
  961. (local-set-key (kbd "C-m") 'newline-and-indent)
  962. ;; this functions is set in write-file-functions, i cannot find any
  963. ;; good way to remove this.
  964. (fset 'makefile-warn-suspicious-lines 'ignore)
  965. ))
  966. (add-hook 'verilog-mode-hook
  967. (lambda ()
  968. (local-set-key ";" 'self-insert-command)))
  969. (setq diff-switches "-u")
  970. (add-hook 'diff-mode-hook
  971. (lambda ()
  972. ;; (when (and (eq major-mode
  973. ;; 'diff-mode)
  974. ;; (not buffer-file-name))
  975. ;; ;; do not pass when major-mode is derived mode of diff-mode
  976. ;; (view-mode 1))
  977. (set-face-attribute 'diff-header nil
  978. :foreground nil
  979. :background nil
  980. :weight 'bold)
  981. (set-face-attribute 'diff-file-header nil
  982. :foreground nil
  983. :background nil
  984. :weight 'bold)
  985. (set-face-foreground 'diff-index-face "blue")
  986. (set-face-attribute 'diff-hunk-header nil
  987. :foreground "cyan"
  988. :weight 'normal)
  989. (set-face-attribute 'diff-context nil
  990. ;; :foreground "white"
  991. :foreground nil
  992. :weight 'normal)
  993. (set-face-foreground 'diff-removed-face "red")
  994. (set-face-foreground 'diff-added-face "green")
  995. (set-face-background 'diff-removed-face nil)
  996. (set-face-background 'diff-added-face nil)
  997. (set-face-attribute 'diff-changed nil
  998. :foreground "magenta"
  999. :weight 'normal)
  1000. (set-face-attribute 'diff-refine-change nil
  1001. :foreground nil
  1002. :background nil
  1003. :weight 'bold
  1004. :inverse-video t)
  1005. ;; Annoying !
  1006. ;;(diff-auto-refine-mode)
  1007. ))
  1008. ;; (ffap-bindings)
  1009. (add-hook 'sh-mode-hook
  1010. (lambda ()
  1011. (local-set-key
  1012. (kbd "C-x C-e")
  1013. 'my-execute-shell-command-current-line)))
  1014. (defvar-set sh-here-document-word "__EOC__")
  1015. (defun my-execute-shell-command-current-line ()
  1016. "Run current line as shell command."
  1017. (interactive)
  1018. (shell-command (buffer-substring-no-properties (point-at-bol)
  1019. (point))))
  1020. (setq auto-mode-alist
  1021. `(("autostart\\'" . sh-mode)
  1022. ("xinitrc\\'" . sh-mode)
  1023. ("xprograms\\'" . sh-mode)
  1024. ("PKGBUILD\\'" . sh-mode)
  1025. ,@auto-mode-alist))
  1026. (and (autoload-eval-lazily 'pkgbuild-mode)
  1027. (setq auto-mode-alist (append '(("PKGBUILD\\'" . pkgbuild-mode))
  1028. auto-mode-alist)))
  1029. (and (autoload-eval-lazily 'groovy-mode)
  1030. (add-to-list 'auto-mode-alist
  1031. '("build.gradle\\'" . groovy-mode)))
  1032. (add-hook 'yaml-mode-hook
  1033. (lambda ()
  1034. (local-set-key(kbd "C-m") 'newline)))
  1035. (add-hook 'html-mode-hook
  1036. (lambda ()
  1037. (local-set-key(kbd "C-m") 'reindent-then-newline-and-indent)))
  1038. (add-hook 'text-mode-hook
  1039. (lambda ()
  1040. (local-set-key (kbd "C-m") 'newline)))
  1041. (add-to-list 'Info-default-directory-list
  1042. (expand-file-name "~/.info/emacs-ja"))
  1043. (add-hook 'apropos-mode-hook
  1044. (lambda ()
  1045. (local-set-key "n" 'next-line)
  1046. (local-set-key "p" 'previous-line)
  1047. ))
  1048. (add-hook 'isearch-mode-hook
  1049. (lambda ()
  1050. ;; (define-key isearch-mode-map
  1051. ;; (kbd "C-j") 'isearch-other-control-char)
  1052. ;; (define-key isearch-mode-map
  1053. ;; (kbd "C-k") 'isearch-other-control-char)
  1054. ;; (define-key isearch-mode-map
  1055. ;; (kbd "C-h") 'isearch-other-control-char)
  1056. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  1057. (define-key isearch-mode-map (kbd "M-r")
  1058. 'isearch-query-replace-regexp)))
  1059. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1060. (setq lazy-highlight-cleanup nil)
  1061. ;; face for isearch highlighing
  1062. (set-face-attribute 'lazy-highlight
  1063. nil
  1064. :foreground `unspecified
  1065. :background `unspecified
  1066. :underline t
  1067. ;; :weight `bold
  1068. )
  1069. (add-hook 'outline-mode-hook
  1070. (lambda ()
  1071. (if (string-match "\\.md\\'" buffer-file-name)
  1072. (set (make-local-variable 'outline-regexp) "#+ "))))
  1073. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1074. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1075. (when (autoload-eval-lazily 'markdown-mode
  1076. '(markdown-mode gfm-mode))
  1077. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1078. (setq markdown-command (or (executable-find "markdown")
  1079. (executable-find "markdown.pl")))
  1080. (add-hook 'markdown-mode-hook
  1081. (lambda ()
  1082. (outline-minor-mode 1)
  1083. (flyspell-mode)
  1084. (set (make-local-variable 'comment-start) ";")))
  1085. (add-hook 'gfm-mode-hook
  1086. (lambda ()
  1087. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline)
  1088. ;;(electric-indent-mode 0)
  1089. )))
  1090. ;; c-mode
  1091. ;; http://www.emacswiki.org/emacs/IndentingC
  1092. ;; http://en.wikipedia.org/wiki/Indent_style
  1093. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1094. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1095. (when (autoload-eval-lazily 'cc-vars
  1096. nil
  1097. (defvar c-default-style)
  1098. (add-to-list 'c-default-style
  1099. '(c-mode . "k&r"))
  1100. (add-to-list 'c-default-style
  1101. '(c++-mode . "k&r"))
  1102. (add-hook 'c-mode-common-hook
  1103. (lambda ()
  1104. ;; why c-basic-offset in k&r style defaults to 5 ???
  1105. (defvar-set c-basic-offset 4)
  1106. (defvar-set indent-tabs-mode nil)
  1107. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  1108. (c-toggle-hungry-state -1)
  1109. ;; (and (require 'gtags nil t)
  1110. ;; (gtags-mode 1))
  1111. ))))
  1112. (when (autoload-eval-lazily 'php-mode)
  1113. (add-hook 'php-mode-hook
  1114. (lambda ()
  1115. (setq c-basic-offset 2))))
  1116. (when (autoload-eval-lazily 'js2-mode)
  1117. ;; currently do not use js2-mode
  1118. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1119. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1120. (add-hook 'js2-mode-hook
  1121. (lambda ()
  1122. (define-key js2-mode-map (kbd "C-m") (lambda ()
  1123. (interactive)
  1124. (js2-enter-key)
  1125. (indent-for-tab-command)))
  1126. ;; (add-hook (kill-local-variable 'before-save-hook)
  1127. ;; 'js2-before-save)
  1128. ;; (add-hook 'before-save-hook
  1129. ;; 'my-indent-buffer
  1130. ;; nil
  1131. ;; t)
  1132. )))
  1133. (eval-after-load "js"
  1134. (defvar-set js-indent-level 2))
  1135. (add-to-list 'interpreter-mode-alist
  1136. '("node" . js-mode))
  1137. (when (autoload-eval-lazily 'flymake-jslint
  1138. '(flymake-jslint-load))
  1139. (autoload-eval-lazily 'js nil
  1140. (add-hook 'js-mode-hook
  1141. 'flymake-jslint-load)))
  1142. (safe-require-or-eval 'js-doc)
  1143. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1144. (when (safe-require-or-eval 'uniquify)
  1145. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1146. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1147. (setq uniquify-min-dir-content 1))
  1148. (add-hook 'view-mode-hook
  1149. (lambda()
  1150. (defvar view-mode-map)
  1151. (define-key view-mode-map "j" 'scroll-up-line)
  1152. (define-key view-mode-map "k" 'scroll-down-line)
  1153. (define-key view-mode-map "v" 'toggle-read-only)
  1154. (define-key view-mode-map "q" 'bury-buffer)
  1155. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1156. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1157. ;; (define-key view-mode-map
  1158. ;; "n" 'nonincremental-repeat-search-forward)
  1159. ;; (define-key view-mode-map
  1160. ;; "N" 'nonincremental-repeat-search-backward)
  1161. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1162. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1163. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1164. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1165. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point)
  1166. ))
  1167. (global-set-key "\M-r" 'view-mode)
  1168. ;; (setq view-read-only t)
  1169. ;; (defun my-view-mode-search-word (word)
  1170. ;; "Search for word current directory and subdirectories.
  1171. ;; If called intearctively, find word at point."
  1172. ;; (interactive (list (thing-at-point 'symbol)))
  1173. ;; (if word
  1174. ;; (if (and (require 'gtags nil t)
  1175. ;; (gtags-get-rootpath))
  1176. ;; (gtags-goto-tag word "s")
  1177. ;; (my-rgrep word))
  1178. ;; (message "No word at point.")
  1179. ;; nil))
  1180. (add-hook 'Man-mode-hook
  1181. (lambda ()
  1182. (view-mode 1)
  1183. (setq truncate-lines nil)))
  1184. (defvar-set Man-notify-method (if window-system
  1185. 'newframe
  1186. 'aggressive))
  1187. (defvar-set woman-cache-filename (expand-file-name (concat user-emacs-directory
  1188. "woman_cache.el")))
  1189. (defalias 'man 'woman)
  1190. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1191. ;; python
  1192. (when (autoload-eval-lazily 'python '(python-mode))
  1193. (defvar-set python-python-command (or (executable-find "python3")
  1194. (executable-find "python")))
  1195. ;; (defun my-python-run-as-command ()
  1196. ;; ""
  1197. ;; (interactive)
  1198. ;; (shell-command (concat python-python-command " " buffer-file-name)))
  1199. (defun my-python-display-python-buffer ()
  1200. ""
  1201. (interactive)
  1202. (defvar python-buffer)
  1203. (set-window-text-height (display-buffer python-buffer
  1204. t)
  1205. 7))
  1206. (add-hook 'python-mode-hook
  1207. (lambda ()
  1208. (local-set-key (kbd "C-c C-e") 'my-python-run-as-command)
  1209. (local-set-key (kbd "C-c C-b") 'my-python-display-python-buffer)
  1210. (local-set-key (kbd "C-m") 'newline-and-indent)))
  1211. (add-hook 'inferior-python-mode-hook
  1212. (lambda ()
  1213. (my-python-display-python-buffer)
  1214. (local-set-key (kbd "<up>") 'comint-previous-input)
  1215. (local-set-key (kbd "<down>") 'comint-next-input))))
  1216. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1217. ;; GNU GLOBAL(gtags)
  1218. ;; http://uguisu.skr.jp/Windows/gtags.html
  1219. ;; http://eigyr.dip.jp/gtags.html
  1220. ;; http://cha.la.coocan.jp/doc/gnu_global.html
  1221. (let ((d "/opt/local/share/gtags/"))
  1222. (and (file-directory-p d)
  1223. (add-to-list 'load-path
  1224. d)))
  1225. (when (autoload-eval-lazily 'gtags '(gtags-mode))
  1226. (add-hook 'gtags-mode-hook
  1227. (lambda ()
  1228. (view-mode gtags-mode)
  1229. (setq gtags-select-buffer-single t)
  1230. ;; (local-set-key "\M-t" 'gtags-find-tag)
  1231. ;; (local-set-key "\M-r" 'gtags-find-rtag)
  1232. ;; (local-set-key "\M-s" 'gtags-find-symbol)
  1233. ;; (local-set-key "\C-t" 'gtags-pop-stack)
  1234. (define-key gtags-mode-map (kbd "C-x t h")
  1235. 'gtags-find-tag-from-here)
  1236. (define-key gtags-mode-map (kbd "C-x t t") 'gtags-find-tag)
  1237. (define-key gtags-mode-map (kbd "C-x t r") 'gtags-find-rtag)
  1238. (define-key gtags-mode-map (kbd "C-x t s") 'gtags-find-symbol)
  1239. (define-key gtags-mode-map (kbd "C-x t p") 'gtags-find-pattern)
  1240. (define-key gtags-mode-map (kbd "C-x t f") 'gtags-find-file)
  1241. (define-key gtags-mode-map (kbd "C-x t b") 'gtags-pop-stack) ;back
  1242. ))
  1243. (add-hook 'gtags-select-mode-hook
  1244. (lambda ()
  1245. (define-key gtags-select-mode-map (kbd "C-m") 'gtags-select-tag)
  1246. ))
  1247. )
  1248. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1249. ;; term mode
  1250. ;; (setq multi-term-program shell-file-name)
  1251. (when (autoload-eval-lazily 'multi-term)
  1252. (setq multi-term-switch-after-close nil)
  1253. (setq multi-term-dedicated-select-after-open-p t)
  1254. (setq multi-term-dedicated-window-height 20))
  1255. (when (autoload-eval-lazily 'term '(term ansi-term))
  1256. (defun my-term-quit-or-send-raw ()
  1257. ""
  1258. (interactive)
  1259. (if (get-buffer-process (current-buffer))
  1260. (call-interactively 'term-send-raw)
  1261. (kill-buffer)))
  1262. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  1263. ;; (setq term-ansi-default-program shell-file-name)
  1264. (add-hook 'term-setup-hook
  1265. (lambda ()
  1266. (defvar-set term-display-table (make-display-table))))
  1267. (add-hook 'term-mode-hook
  1268. (lambda ()
  1269. (defvar term-raw-map)
  1270. (unless (memq (current-buffer)
  1271. (and (featurep 'multi-term)
  1272. (defvar multi-term-buffer-list)
  1273. ;; current buffer is not multi-term buffer
  1274. multi-term-buffer-list))
  1275. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  1276. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  1277. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  1278. ;; (define-key term-raw-map "\C-f" 'forward-char)
  1279. ;; (define-key term-raw-map "\C-b" 'backward-char)
  1280. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  1281. (define-key term-raw-map
  1282. "\C-x" (lookup-key (current-global-map) "\C-x"))
  1283. (define-key term-raw-map
  1284. "\C-z" (lookup-key (current-global-map) "\C-z"))
  1285. )
  1286. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  1287. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  1288. (define-key term-raw-map (kbd "<up>") 'scroll-down-line)
  1289. (define-key term-raw-map (kbd "<down>") 'scroll-up-line)
  1290. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  1291. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  1292. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  1293. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  1294. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  1295. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  1296. (define-key term-raw-map [delete] 'term-send-raw)
  1297. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  1298. (define-key term-raw-map "\C-y" 'term-paste)
  1299. (define-key term-raw-map
  1300. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  1301. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  1302. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  1303. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  1304. ;; (define-key term-raw-map "\C-d" 'delete-char)
  1305. (set (make-local-variable 'scroll-margin) 0)
  1306. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  1307. ;; (cua-mode 0)
  1308. ;; (and cua-mode
  1309. ;; (local-unset-key (kbd "C-c")))
  1310. ;; (define-key cua--prefix-override-keymap
  1311. ;;"\C-c" 'term-interrupt-subjob)
  1312. (set (make-local-variable (defvar hl-line-range-function))
  1313. (lambda ()
  1314. '(0 . 0)))
  1315. ))
  1316. ;; (add-hook 'term-exec-hook 'forward-char)
  1317. )
  1318. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1319. ;; buffer switching
  1320. (defvar bs-configurations)
  1321. (when (autoload-eval-lazily 'bs '(bs-show)
  1322. ;; (add-to-list 'bs-configurations
  1323. ;; '("processes" nil get-buffer-process ".*" nil nil))
  1324. (add-to-list 'bs-configurations
  1325. '("files-and-terminals" nil nil nil
  1326. (lambda (buf)
  1327. (and (bs-visits-non-file buf)
  1328. (save-excursion
  1329. (set-buffer buf)
  1330. (not (memq major-mode
  1331. '(term-mode
  1332. eshell-mode))))))))
  1333. ;; (setq bs-configurations (list
  1334. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1335. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1336. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1337. )
  1338. ;; (global-set-key "\C-x\C-b" 'bs-show)
  1339. (defalias 'list-buffers 'bs-show)
  1340. (defvar-set bs-default-configuration "files-and-terminals")
  1341. (defvar-set bs-default-sort-name "by nothing")
  1342. (add-hook 'bs-mode-hook
  1343. (lambda ()
  1344. ;; (setq bs-default-configuration "files")
  1345. ;; (and bs--show-all
  1346. ;; (call-interactively 'bs-toggle-show-all))
  1347. (set (make-local-variable 'scroll-margin) 0))))
  1348. ;;(iswitchb-mode 1)
  1349. (icomplete-mode)
  1350. (defun iswitchb-buffer-display-other-window ()
  1351. "Do iswitchb in other window."
  1352. (interactive)
  1353. (let ((iswitchb-default-method 'display))
  1354. (call-interactively 'iswitchb-buffer)))
  1355. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1356. ;; sdic
  1357. (when (autoload-eval-lazily 'sdic '(sdic-describe-word-at-point))
  1358. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  1359. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo)
  1360. (defun sdic-describe-word-at-point-echo ()
  1361. ""
  1362. (interactive)
  1363. (save-window-excursion
  1364. (sdic-describe-word-at-point))
  1365. (save-excursion
  1366. (set-buffer sdic-buffer-name)
  1367. (message (buffer-substring (point-min)
  1368. (progn (goto-char (point-min))
  1369. (or (and (re-search-forward "^\\w"
  1370. nil
  1371. t
  1372. 4)
  1373. (progn (previous-line) t)
  1374. (point-at-eol))
  1375. (point-max)))))))
  1376. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  1377. (setq sdic-waei-dictionary-list
  1378. '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  1379. (setq sdic-disable-select-window t)
  1380. (setq sdic-window-height 7))
  1381. ;;;;;;;;;;;;;;;;;;;;;;;;
  1382. ;; ilookup
  1383. (when (fetch-library
  1384. "https://raw.github.com/10sr/emacs-lisp/master/ilookup.el"
  1385. t)
  1386. (autoload-eval-lazily 'ilookup
  1387. '(ilookup-open)
  1388. (setq ilookup-dict-alist
  1389. '(
  1390. ("sdcv" . (lambda (word)
  1391. (shell-command-to-string
  1392. (format "sdcv -n '%s'"
  1393. word))))
  1394. ("en" . (lambda (word)
  1395. (shell-command-to-string
  1396. (format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
  1397. word))))
  1398. ("ja" . (lambda (word)
  1399. (shell-command-to-string
  1400. (format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
  1401. word))))
  1402. ("jaj" . (lambda (word)
  1403. (shell-command-to-string
  1404. (format "sdcv -n -u jmdict-en-ja '%s'"
  1405. word))))
  1406. ("jag" .
  1407. (lambda (word)
  1408. (with-temp-buffer
  1409. (insert (shell-command-to-string
  1410. (format "sdcv -n -u 'Genius English-Japanese' '%s'"
  1411. word)))
  1412. (html2text)
  1413. (buffer-substring (point-min)
  1414. (point-max)))))
  1415. ("alc" . (lambda (word)
  1416. (shell-command-to-string
  1417. (format "alc '%s' | head -n 20"
  1418. word))))
  1419. ("app" . (lambda (word)
  1420. (shell-command-to-string
  1421. (format "dict_app '%s'"
  1422. word))))
  1423. ;; letters broken
  1424. ("ms" .
  1425. (lambda (word)
  1426. (let ((url (concat
  1427. "http://api.microsofttranslator.com/V2/Ajax.svc/"
  1428. "Translate?appId=%s&text=%s&to=%s"))
  1429. (apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
  1430. (target "ja")
  1431. (eword (url-hexify-string word)))
  1432. (with-current-buffer (url-retrieve-synchronously
  1433. (format url
  1434. apikey
  1435. eword
  1436. target))
  1437. (message "")
  1438. (goto-char (point-min))
  1439. (search-forward-regexp "^$"
  1440. nil
  1441. t)
  1442. (url-unhex-string (buffer-substring-no-properties
  1443. (point)
  1444. (point-max)))))))
  1445. ))
  1446. ;; (funcall (cdr (assoc "ms"
  1447. ;; ilookup-alist))
  1448. ;; "dictionary")
  1449. ;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))
  1450. ;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))
  1451. (setq ilookup-default "ja")
  1452. (when (locate-library "google-translate")
  1453. (add-to-list 'ilookup-dict-alist
  1454. '("gt" .
  1455. (lambda (word)
  1456. (save-excursion
  1457. (google-translate-translate "auto"
  1458. "ja"
  1459. word))
  1460. (with-current-buffer "*Google Translate*"
  1461. (buffer-substring-no-properties (point-min)
  1462. (point-max)))))))
  1463. ))
  1464. (when (autoload-eval-lazily 'google-translate '(google-translate-translate
  1465. google-translate-at-point))
  1466. (setq google-translate-default-source-language "auto")
  1467. (setq google-translate-default-target-language "ja"))
  1468. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1469. ;; vc
  1470. (require 'vc)
  1471. (setq vc-handled-backends '())
  1472. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1473. ;; gauche-mode
  1474. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  1475. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  1476. (when (and (fetch-library
  1477. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  1478. t)
  1479. (autoload-eval-lazily 'gauche-mode '(gauche-mode run-scheme)))
  1480. (let ((s (executable-find "gosh")))
  1481. (setq scheme-program-name s
  1482. gauche-program-name s))
  1483. (defun run-gauche-other-window ()
  1484. "Run gauche on other window"
  1485. (interactive)
  1486. (switch-to-buffer-other-window
  1487. (get-buffer-create "*scheme*"))
  1488. (run-gauche))
  1489. (defun run-gauche ()
  1490. "run gauche"
  1491. (interactive)
  1492. (run-scheme gauche-program-name)
  1493. )
  1494. (defun scheme-send-buffer ()
  1495. ""
  1496. (interactive)
  1497. (scheme-send-region (point-min) (point-max))
  1498. (my-scheme-display-scheme-buffer)
  1499. )
  1500. (defun my-scheme-display-scheme-buffer ()
  1501. ""
  1502. (interactive)
  1503. (set-window-text-height (display-buffer scheme-buffer
  1504. t)
  1505. 7))
  1506. (add-hook 'scheme-mode-hook
  1507. (lambda ()
  1508. nil))
  1509. (add-hook 'inferior-scheme-mode-hook
  1510. (lambda ()
  1511. ;; (my-scheme-display-scheme-buffer)
  1512. ))
  1513. (setq auto-mode-alist
  1514. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  1515. (setq auto-mode-alist
  1516. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  1517. (add-hook 'gauche-mode-hook
  1518. (lambda ()
  1519. (define-key gauche-mode-map
  1520. (kbd "C-c C-z") 'run-gauche-other-window)
  1521. (define-key scheme-mode-map
  1522. (kbd "C-c C-c") 'scheme-send-buffer)
  1523. (define-key scheme-mode-map
  1524. (kbd "C-c C-b") 'my-scheme-display-scheme-buffer))))
  1525. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1526. ;; recentf-mode
  1527. (setq recentf-save-file (expand-file-name (concat user-emacs-directory
  1528. "recentf"))
  1529. recentf-max-menu-items 20
  1530. recentf-max-saved-items 30
  1531. recentf-show-file-shortcuts-flag nil)
  1532. (when (safe-require-or-eval 'recentf)
  1533. (add-to-list 'recentf-exclude
  1534. (regexp-quote recentf-save-file))
  1535. (add-to-list 'recentf-exclude
  1536. (regexp-quote (expand-file-name user-emacs-directory)))
  1537. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1538. (remove-hook 'find-file-hook
  1539. 'recentf-track-opened-file)
  1540. (defun my-recentf-load-track-save-list ()
  1541. "Load current recentf list from file, track current visiting file, then save
  1542. the list."
  1543. (recentf-load-list)
  1544. (recentf-track-opened-file)
  1545. (recentf-save-list))
  1546. (add-hook 'find-file-hook
  1547. 'my-recentf-load-track-save-list)
  1548. (add-hook 'kill-emacs-hook
  1549. 'recentf-load-list)
  1550. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1551. ;; (add-hook 'find-file-hook
  1552. ;; (lambda ()
  1553. ;; (recentf-add-file default-directory)))
  1554. (and (fetch-library
  1555. "https://raw.github.com/10sr/emacs-lisp/master/recentf-show.el"
  1556. t)
  1557. (autoload-eval-lazily 'recentf-show)
  1558. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1559. (add-hook 'recentf-show-before-listing-hook
  1560. 'recentf-load-list))
  1561. (recentf-mode 1)
  1562. (add-hook 'recentf-dialog-mode-hook
  1563. (lambda ()
  1564. ;; (recentf-save-list)
  1565. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1566. ;; 'my-recentf-cd-and-find-file)
  1567. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1568. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1569. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1570. (define-key recentf-dialog-mode-map "n" 'next-line)
  1571. (cd "~/"))))
  1572. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1573. ;; dired
  1574. (when (autoload-eval-lazily 'dired nil)
  1575. (defun my-dired-echo-file-head (arg)
  1576. ""
  1577. (interactive "P")
  1578. (let ((f (dired-get-filename)))
  1579. (message "%s"
  1580. (with-temp-buffer
  1581. (insert-file-contents f)
  1582. (buffer-substring-no-properties
  1583. (point-min)
  1584. (progn (goto-line (if arg
  1585. (prefix-numeric-value arg)
  1586. 7))
  1587. (point-at-eol)))))))
  1588. (defun my-dired-diff ()
  1589. ""
  1590. (interactive)
  1591. (let ((files (dired-get-marked-files nil nil nil t)))
  1592. (if (eq (car files)
  1593. t)
  1594. (diff (cadr files) (dired-get-filename))
  1595. (message "One files must be marked!"))))
  1596. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1597. "pop up buffer using `display-buffer' and return that buffer."
  1598. (let ((bf (get-buffer-create buffer-or-name)))
  1599. (with-current-buffer bf
  1600. (cd ".")
  1601. (erase-buffer))
  1602. (display-buffer bf)
  1603. bf))
  1604. (defun my-replace-nasi-none ()
  1605. ""
  1606. (save-excursion
  1607. (let ((buffer-read-only nil))
  1608. (goto-char (point-min))
  1609. (while (search-forward "なし" nil t)
  1610. (replace-match "none")))))
  1611. (defun dired-get-file-info ()
  1612. "dired get file info"
  1613. (interactive)
  1614. (let ((f (shell-quote-argument (dired-get-filename t))))
  1615. (if (file-directory-p f)
  1616. (progn
  1617. (message "Calculating disk usage...")
  1618. (shell-command (concat "du -hsD "
  1619. f)))
  1620. (shell-command (concat "file "
  1621. f)))))
  1622. (defun my-dired-scroll-up ()
  1623. ""
  1624. (interactive)
  1625. (my-dired-previous-line (- (window-height) 1)))
  1626. (defun my-dired-scroll-down ()
  1627. ""
  1628. (interactive)
  1629. (my-dired-next-line (- (window-height) 1)))
  1630. ;; (defun my-dired-forward-line (arg)
  1631. ;; ""
  1632. ;; (interactive "p"))
  1633. (defun my-dired-previous-line (arg)
  1634. ""
  1635. (interactive "p")
  1636. (if (> arg 0)
  1637. (progn
  1638. (if (eq (line-number-at-pos)
  1639. 1)
  1640. (goto-char (point-max))
  1641. (forward-line -1))
  1642. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1643. (dired-get-subdir))
  1644. (- arg 1)
  1645. arg)))
  1646. (dired-move-to-filename)))
  1647. (defun my-dired-next-line (arg)
  1648. ""
  1649. (interactive "p")
  1650. (if (> arg 0)
  1651. (progn
  1652. (if (eq (point)
  1653. (point-max))
  1654. (goto-char (point-min))
  1655. (forward-line 1))
  1656. (my-dired-next-line (if (or (dired-get-filename nil t)
  1657. (dired-get-subdir))
  1658. (- arg 1)
  1659. arg)))
  1660. (dired-move-to-filename)))
  1661. (defun my-dired-print-current-dir-and-file ()
  1662. (message "%s %s"
  1663. default-directory
  1664. (buffer-substring-no-properties (point-at-bol)
  1665. (point-at-eol))))
  1666. (defun dired-do-execute-as-command ()
  1667. ""
  1668. (interactive)
  1669. (let ((file (dired-get-filename t)))
  1670. (if (file-executable-p file)
  1671. (start-process file nil file)
  1672. (when (y-or-n-p
  1673. "This file cant be executed. Mark as executable and go? ")
  1674. (set-file-modes file
  1675. (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1676. (start-process file nil file)))))
  1677. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1678. (defun my-dired-x-open ()
  1679. ""
  1680. (interactive)
  1681. (my-x-open (dired-get-filename t t)))
  1682. (if (eq window-system 'mac)
  1683. (setq dired-listing-switches "-lhF")
  1684. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1685. )
  1686. (setq dired-listing-switches "-lhF")
  1687. (put 'dired-find-alternate-file 'disabled nil)
  1688. ;; when using dired-find-alternate-file
  1689. ;; reuse current dired buffer for the file to open
  1690. (defvar-set dired-ls-F-marks-symlinks t)
  1691. (when (safe-require-or-eval 'ls-lisp)
  1692. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1693. (setq ls-lisp-dirs-first t)
  1694. (setq ls-lisp-use-localized-time-format t)
  1695. (setq ls-lisp-format-time-list
  1696. '("%Y-%m-%d %H:%M"
  1697. "%Y-%m-%d ")))
  1698. (defvar-set dired-dwim-target t)
  1699. (defvar-set dired-isearch-filenames t)
  1700. (defvar-set dired-hide-details-hide-symlink-targets nil)
  1701. (defvar-set dired-hide-details-hide-information-lines nil)
  1702. ;; (add-hook 'dired-after-readin-hook
  1703. ;; 'my-replace-nasi-none)
  1704. ;; (add-hook 'after-init-hook
  1705. ;; (lambda ()
  1706. ;; (dired ".")))
  1707. (add-hook 'dired-mode-hook
  1708. (lambda ()
  1709. (local-set-key "o" 'my-dired-x-open)
  1710. (local-set-key "i" 'dired-get-file-info)
  1711. (local-set-key "f" 'find-file)
  1712. (local-set-key "!" 'shell-command)
  1713. (local-set-key "&" 'async-shell-command)
  1714. (local-set-key "X" 'dired-do-async-shell-command)
  1715. (local-set-key "=" 'my-dired-diff)
  1716. (local-set-key "B" 'gtkbm-add-current-dir)
  1717. (local-set-key "b" 'gtkbm)
  1718. (local-set-key "h" 'my-dired-echo-file-head)
  1719. (local-set-key "@" (lambda ()
  1720. (interactive) (my-x-open ".")))
  1721. (local-set-key (kbd "TAB") 'other-window)
  1722. ;; (local-set-key "P" 'my-dired-do-pack-or-unpack)
  1723. (local-set-key "/" 'dired-isearch-filenames)
  1724. (local-set-key (kbd "DEL") 'dired-up-directory)
  1725. (local-set-key (kbd "C-h") 'dired-up-directory)
  1726. (substitute-key-definition 'dired-next-line
  1727. 'my-dired-next-line
  1728. (current-local-map))
  1729. (substitute-key-definition 'dired-previous-line
  1730. 'my-dired-previous-line
  1731. (current-local-map))
  1732. ;; (local-set-key (kbd "C-p") 'my-dired-previous-line)
  1733. ;; (local-set-key (kbd "p") 'my-dired-previous-line)
  1734. ;; (local-set-key (kbd "C-n") 'my-dired-next-line)
  1735. ;; (local-set-key (kbd "n") 'my-dired-next-line)
  1736. (local-set-key (kbd "<left>") 'my-dired-scroll-up)
  1737. (local-set-key (kbd "<right>") 'my-dired-scroll-down)
  1738. (local-set-key (kbd "ESC p") 'my-dired-scroll-up)
  1739. (local-set-key (kbd "ESC n") 'my-dired-scroll-down)
  1740. (when (fboundp 'dired-hide-details-mode)
  1741. (dired-hide-details-mode t)
  1742. (local-set-key "l" 'dired-hide-details-mode))
  1743. (let ((file "._Icon\015"))
  1744. (when nil (file-readable-p file)
  1745. (delete-file file)))))
  1746. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/pack.el"
  1747. t)
  1748. (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack))
  1749. (add-hook 'dired-mode-hook
  1750. (lambda ()
  1751. (local-set-key "P" 'dired-do-pack-or-unpack))))
  1752. (and (fetch-library
  1753. "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  1754. t)
  1755. (autoload-eval-lazily 'dired-list-all-mode)
  1756. (setq dired-listing-switches "-lhF")
  1757. (add-hook 'dired-mode-hook
  1758. (lambda ()
  1759. (local-set-key "a" 'dired-list-all-mode)
  1760. )))
  1761. ) ; when dired locate
  1762. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1763. (defvar dired-marker-char)
  1764. (defun my-dired-toggle-mark()
  1765. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1766. (t dired-marker-char))))
  1767. (delete-char 1)
  1768. (insert cur)))
  1769. (defun my-dired-mark (arg)
  1770. "Toggle mark the current (or next ARG) files.
  1771. If on a subdir headerline, mark all its files except `.' and `..'.
  1772. Use \\[dired-unmark-all-files] to remove all marks
  1773. and \\[dired-unmark] on a subdir to remove the marks in
  1774. this subdir."
  1775. (interactive "P")
  1776. (if (dired-get-subdir)
  1777. (save-excursion (dired-mark-subdir-files))
  1778. (let ((inhibit-read-only t))
  1779. (dired-repeat-over-lines
  1780. (prefix-numeric-value arg)
  1781. 'my-dired-toggle-mark))))
  1782. (defun my-dired-mark-backward (arg)
  1783. "In Dired, move up lines and toggle mark there.
  1784. Optional prefix ARG says how many lines to unflag; default is one line."
  1785. (interactive "p")
  1786. (my-dired-mark (- arg)))
  1787. (add-hook 'dired-mode-hook
  1788. (lambda ()
  1789. (local-set-key (kbd "SPC") 'my-dired-mark)
  1790. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1791. )
  1792. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1793. ;; eshell
  1794. (autoload-eval-lazily 'eshell nil
  1795. (defvar-set eshell-banner-message (format "Welcome to the Emacs shell
  1796. %s
  1797. C-x t to toggling emacs-text-mode
  1798. "
  1799. (shell-command-to-string "uname -a")
  1800. ))
  1801. (defvar eshell-text-mode-map
  1802. (let ((map (make-sparse-keymap)))
  1803. (define-key map (kbd "C-x t") 'eshell-text-mode-toggle)
  1804. map))
  1805. (define-derived-mode eshell-text-mode text-mode
  1806. "Eshell-Text"
  1807. "Text-mode for Eshell."
  1808. nil)
  1809. (defun eshell-text-mode-toggle ()
  1810. "Toggle eshell-text-mode and eshell-mode."
  1811. (interactive)
  1812. (cond ((eq major-mode
  1813. 'eshell-text-mode)
  1814. (goto-char (point-max))
  1815. (message "Eshell text mode disabled")
  1816. (eshell-mode))
  1817. ((eq major-mode
  1818. 'eshell-mode)
  1819. (message "Eshell text mode enabled")
  1820. (eshell-write-history)
  1821. (eshell-text-mode))
  1822. (t
  1823. (message "Not in eshell buffer")
  1824. nil)))
  1825. (defun my-eshell-backward-delete-char ()
  1826. (interactive)
  1827. (when (< (save-excursion
  1828. (eshell-bol)
  1829. (point))
  1830. (point))
  1831. (backward-delete-char 1)))
  1832. (defun my-file-owner-p (file)
  1833. "t if FILE is owned by me."
  1834. (eq (user-uid) (nth 2 (file-attributes file))))
  1835. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1836. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1837. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1838. ;; (defun eshell/less (&rest args)
  1839. ;; "Invoke `view-file' on the file.
  1840. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1841. ;; (if args
  1842. ;; (while args
  1843. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1844. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1845. ;; (file (pop args)))
  1846. ;; (view-file file)
  1847. ;; (goto-line line))
  1848. ;; (view-file (pop args))))))
  1849. (defun eshell/o (&optional file)
  1850. (my-x-open (or file ".")))
  1851. ;; (defun eshell/vi (&rest args)
  1852. ;; "Invoke `find-file' on the file.
  1853. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1854. ;; (while args
  1855. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1856. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1857. ;; (file (pop args)))
  1858. ;; (find-file file)
  1859. ;; (goto-line line))
  1860. ;; (find-file (pop args)))))
  1861. (defun eshell/clear ()
  1862. "Clear the current buffer, leaving one prompt at the top."
  1863. (interactive)
  1864. (let ((inhibit-read-only t))
  1865. (erase-buffer)))
  1866. (defun eshell-clear ()
  1867. (interactive)
  1868. (let ((inhibit-read-only t))
  1869. (erase-buffer)
  1870. (insert (funcall eshell-prompt-function))))
  1871. (defun eshell/d (&optional dirname switches)
  1872. "if first arg is omitted open current directory."
  1873. (dired (or dirname ".") switches))
  1874. (defun eshell/v ()
  1875. (view-mode 1))
  1876. ;; (defun eshell/aaa (&rest args)
  1877. ;; (message "%S"
  1878. ;; args))
  1879. (defalias 'eshell/: 'ignore)
  1880. (defalias 'eshell/type 'eshell/which)
  1881. ;; (defalias 'eshell/vim 'eshell/vi)
  1882. (defalias 'eshell/ff 'find-file)
  1883. (defalias 'eshell/q 'eshell/exit)
  1884. (defun eshell-goto-prompt ()
  1885. ""
  1886. (interactive)
  1887. (goto-char (point-max)))
  1888. (defun eshell-delete-char-or-logout (n)
  1889. (interactive "p")
  1890. (if (equal (eshell-get-old-input)
  1891. "")
  1892. (progn
  1893. (insert "exit")
  1894. (eshell-send-input))
  1895. (delete-char n)))
  1896. (defun eshell-kill-input ()
  1897. (interactive)
  1898. (delete-region (point)
  1899. (progn (eshell-bol)
  1900. (point))))
  1901. (defalias 'eshell/logout 'eshell/exit)
  1902. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1903. "open eshell and change wd
  1904. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1905. (interactive)
  1906. (let ((dir (expand-file-name default-directory)))
  1907. (switch-to-buffer (or eshell-buffer-or-name
  1908. (eshell t)))
  1909. (unless (equal dir (expand-file-name default-directory))
  1910. ;; (cd dir)
  1911. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1912. ;; (eshell-emit-prompt)
  1913. (goto-char (point-max))
  1914. (eshell-kill-input)
  1915. (insert "cd " dir)
  1916. (eshell-send-input))))
  1917. (defadvice eshell-next-matching-input-from-input
  1918. ;; do not cycle history
  1919. (around eshell-history-do-not-cycle activate)
  1920. (if (= 0
  1921. (or eshell-history-index
  1922. 0))
  1923. (progn
  1924. (delete-region eshell-last-output-end (point))
  1925. (insert-and-inherit eshell-matching-input-from-input-string)
  1926. (setq eshell-history-index nil))
  1927. ad-do-it))
  1928. (defvar-set eshell-directory-name (concat user-emacs-directory
  1929. "eshell/"))
  1930. (defvar-set eshell-term-name "eterm-color")
  1931. (defvar-set eshell-scroll-to-bottom-on-input t)
  1932. (defvar-set eshell-cmpl-ignore-case t)
  1933. (defvar-set eshell-cmpl-cycle-completions nil)
  1934. (defvar-set eshell-highlight-prompt nil)
  1935. (if (eq system-type 'darwin)
  1936. (defvar-set eshell-ls-initial-args '("-hCFG")
  1937. (defvar-set eshell-ls-initial-args '("-hCFG"
  1938. "--color=auto"
  1939. "--time-style=long-iso")) ; "-hF")
  1940. ))
  1941. (defvar-set eshell-prompt-function
  1942. 'my-eshell-prompt-function)
  1943. (defvar eshell-last-command-status)
  1944. (defun my-eshell-prompt-function()
  1945. "Prompt function.
  1946. It looks like:
  1947. :: [10sr@darwin:~/][ESHELL]
  1948. :: $
  1949. "
  1950. (concat ":: ["
  1951. (let ((str (concat user-login-name
  1952. "@"
  1953. (car (split-string system-name
  1954. "\\."))
  1955. )))
  1956. (put-text-property 0
  1957. (length str)
  1958. 'face
  1959. 'underline
  1960. str)
  1961. str)
  1962. ":"
  1963. (let ((str (abbreviate-file-name default-directory)))
  1964. (put-text-property 0
  1965. (length str)
  1966. 'face
  1967. 'underline
  1968. str)
  1969. str)
  1970. "][ESHELL]\n:: "
  1971. (if (eq 0
  1972. eshell-last-command-status)
  1973. ""
  1974. (format "[STATUS:%d] "
  1975. eshell-last-command-status))
  1976. (if (= (user-uid)
  1977. 0)
  1978. "# "
  1979. "$ ")
  1980. ))
  1981. (add-hook 'eshell-mode-hook
  1982. (lambda ()
  1983. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1984. ;; (interactive)
  1985. ;; (switch-to-buffer (other-buffer))))
  1986. ;; (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1987. ;; (interactive)
  1988. ;; (eshell-goto-prompt)
  1989. ;; (keyboard-quit)))
  1990. (local-set-key (kbd "C-x t") 'eshell-text-mode-toggle)
  1991. (local-set-key (kbd "C-u") 'eshell-kill-input)
  1992. (local-set-key (kbd "C-d") 'eshell-delete-char-or-logout)
  1993. ;; (define-key eshell-mode-map (kbd "C-l")
  1994. ;; 'eshell-clear)
  1995. (local-set-key (kbd "DEL") 'my-eshell-backward-delete-char)
  1996. (local-set-key (kbd "<up>") 'scroll-down-line)
  1997. (local-set-key (kbd "<down>") 'scroll-up-line)
  1998. ;; (define-key eshell-mode-map
  1999. ;; (kbd "C-p") 'eshell-previous-matching-input-from-input)
  2000. ;; (define-key eshell-mode-map
  2001. ;; (kbd "C-n") 'eshell-next-matching-input-from-input)
  2002. (apply 'eshell/addpath exec-path)
  2003. (set (make-local-variable 'scroll-margin) 0)
  2004. ;; (eshell/export "GIT_PAGER=")
  2005. ;; (eshell/export "GIT_EDITOR=")
  2006. (eshell/export "LC_MESSAGES=C")
  2007. (switch-to-buffer (current-buffer)) ; move buffer top of list
  2008. (set (make-local-variable (defvar hl-line-range-function))
  2009. (lambda ()
  2010. '(0 . 0)))
  2011. (defvar eshell-virtual-targets)
  2012. (add-to-list 'eshell-virtual-targets
  2013. '("/dev/less"
  2014. (lambda (str)
  2015. (if str
  2016. (with-current-buffer nil)))
  2017. nil))
  2018. ))
  2019. (add-hook 'eshell-mode-hook
  2020. (lambda ()
  2021. (defvar eshell-visual-commands)
  2022. (defvar eshell-output-filter-functions)
  2023. (defvar eshell-command-aliases-list)
  2024. (add-to-list 'eshell-visual-commands "vim")
  2025. ;; (add-to-list 'eshell-visual-commands "git")
  2026. (add-to-list 'eshell-output-filter-functions
  2027. 'eshell-truncate-buffer)
  2028. (mapcar (lambda (alias)
  2029. (add-to-list 'eshell-command-aliases-list
  2030. alias))
  2031. '(
  2032. ;; ("ll" "ls -l $*")
  2033. ;; ("la" "ls -a $*")
  2034. ;; ("lla" "ls -al $*")
  2035. ("git" "git -c color.ui=always $*")
  2036. ("g" "git $*")
  2037. ("eless"
  2038. (concat "cat >>> (with-current-buffer "
  2039. "(get-buffer-create \"*eshell output\") "
  2040. "(erase-buffer) "
  2041. "(setq buffer-read-only nil) "
  2042. "(current-buffer)) "
  2043. "(view-buffer (get-buffer \"*eshell output*\"))"))
  2044. )
  2045. )))
  2046. ) ; eval after load eshell
  2047. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2048. ;; my-term
  2049. (defvar my-term nil
  2050. "My terminal buffer.")
  2051. (defvar my-term-function nil
  2052. "Function to create terminal buffer.
  2053. This function accept no argument and return newly created buffer of terminal.")
  2054. (defun my-term (&optional arg)
  2055. "Open terminal buffer and return that buffer.
  2056. If ARG is given or called with prefix argument, create new buffer."
  2057. (interactive "P")
  2058. (if (and (not arg)
  2059. my-term
  2060. (buffer-name my-term))
  2061. (pop-to-buffer my-term)
  2062. (setq my-term
  2063. (save-window-excursion
  2064. (funcall my-term-function)))
  2065. (and my-term
  2066. (my-term))))
  2067. ;; (setq my-term-function
  2068. ;; (lambda ()
  2069. ;; (if (eq system-type 'windows-nt)
  2070. ;; (eshell)
  2071. ;; (if (require 'multi-term nil t)
  2072. ;; (multi-term)
  2073. ;; (ansi-term shell-file-name)))))
  2074. (setq my-term-function (lambda () (eshell t)))
  2075. ;;(define-key my-prefix-map (kbd "C-s") 'my-term)
  2076. (define-key ctl-x-map "i" 'my-term)
  2077. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2078. ;; x open
  2079. (defvar my-filer nil)
  2080. (setq my-filer (or (executable-find "pcmanfm")
  2081. (executable-find "nautilus")))
  2082. (defun my-x-open (file)
  2083. "Open FILE."
  2084. (interactive "FOpen File: ")
  2085. (setq file (expand-file-name file))
  2086. (message "Opening %s..." file)
  2087. (cond ((eq system-type 'windows-nt)
  2088. (call-process "cmd.exe" nil 0 nil
  2089. "/c" "start" "" (convert-standard-filename file)))
  2090. ((eq system-type 'darwin)
  2091. (call-process "open" nil 0 nil file))
  2092. ((getenv "DISPLAY")
  2093. (call-process (or my-filer "xdg-open") nil 0 nil file))
  2094. (t
  2095. (find-file file))
  2096. )
  2097. ;; (recentf-add-file file)
  2098. (message "Opening %s...done" file))
  2099. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2100. ;; misc funcs
  2101. (defun my-git-apply-index-from-buffer (&optional buf)
  2102. "Git apply buffer. BUF is buffer to apply. nil to use current buffer."
  2103. (interactive)
  2104. (let ((buf (or buf
  2105. (current-buffer)))
  2106. (file (make-temp-file "git-apply-diff.emacs")))
  2107. (with-current-buffer buf
  2108. (write-region (point-min)
  2109. (point-max)
  2110. file)
  2111. (call-process "git"
  2112. nil
  2113. nil
  2114. nil
  2115. "apply"
  2116. "--cached"
  2117. file))))
  2118. (defun memo (&optional dir)
  2119. "Open memo.txt in DIR."
  2120. (interactive)
  2121. (pop-to-buffer (find-file-noselect (concat (if dir
  2122. (file-name-as-directory dir)
  2123. "")
  2124. "memo.txt"))))
  2125. (defvar my-rgrep-alist
  2126. `(
  2127. ;; the silver searcher
  2128. ("ag"
  2129. (executable-find "ag")
  2130. "ag --nocolor --nogroup --nopager --filename ")
  2131. ;; ack
  2132. ("ack"
  2133. (executable-find "ack")
  2134. "ack --nocolor --nogroup --nopager --with-filename ")
  2135. ;; gnu global
  2136. ("global"
  2137. (and (require 'gtags nil t)
  2138. (executable-find "global")
  2139. (gtags-get-rootpath))
  2140. "global --result grep ")
  2141. ;; git grep
  2142. ("gitgrep"
  2143. (eq 0
  2144. (shell-command "git rev-parse --git-dir"))
  2145. "git --no-pager -c color.grep=false grep -nH -e ")
  2146. ;; grep
  2147. ("grep"
  2148. t
  2149. ,(concat "find . "
  2150. "-path '*/.git' -prune -o "
  2151. "-path '*/.svn' -prune -o "
  2152. "-type f -print0 | "
  2153. "xargs -0 grep -nH -e "))
  2154. )
  2155. "Alist of rgrep command.
  2156. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  2157. condition to choose COMMAND when evaluated.")
  2158. (defvar my-rgrep-default nil
  2159. "Default command name for my-rgrep.")
  2160. (defun my-rgrep-grep-command (&optional name alist)
  2161. "Return recursive grep command for current directory or nil.
  2162. If NAME is given, use that without testing.
  2163. Commands are searched from ALIST."
  2164. (if alist
  2165. (if name
  2166. ;; if name is given search that from alist and return the command
  2167. (nth 2 (assoc name
  2168. alist))
  2169. ;; if name is not given try test in 1th elem
  2170. (let ((car (car alist))
  2171. (cdr (cdr alist)))
  2172. (if (eval (nth 1 car))
  2173. ;; if the condition is true return the command
  2174. (nth 2 car)
  2175. ;; try next one
  2176. (and cdr
  2177. (my-rgrep-grep-command name cdr)))))
  2178. ;; if alist is not given set default value
  2179. (my-rgrep-grep-command name my-rgrep-alist)))
  2180. (defun my-rgrep (command-args)
  2181. "My recursive grep. Run COMMAND-ARGS."
  2182. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  2183. nil)))
  2184. (if cmd
  2185. (list (read-shell-command "grep command: "
  2186. cmd
  2187. 'grep-find-history))
  2188. (error "My-Rgrep: Command for rgrep not found")
  2189. )))
  2190. (compilation-start command-args
  2191. 'grep-mode))
  2192. ;; (defun my-rgrep-symbol-at-point (command-args)
  2193. ;; "My recursive grep. Run COMMAND-ARGS."
  2194. ;; (interactive (list (read-shell-command "grep command: "
  2195. ;; (concat (my-rgrep-grep-command)
  2196. ;; " "
  2197. ;; (thing-at-point 'symbol))
  2198. ;; 'grep-find-history)))
  2199. ;; (compilation-start command-args
  2200. ;; 'grep-mode))
  2201. (defmacro define-my-rgrep (name)
  2202. "Define rgrep for NAME."
  2203. `(defun ,(intern (concat "my-rgrep-"
  2204. name)) ()
  2205. ,(format "My recursive grep by %s."
  2206. name)
  2207. (interactive)
  2208. (let ((my-rgrep-default ,name))
  2209. (if (called-interactively-p 'any)
  2210. (call-interactively 'my-rgrep)
  2211. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2212. )
  2213. (define-my-rgrep "ack")
  2214. (define-my-rgrep "ag")
  2215. (define-my-rgrep "gitgrep")
  2216. (define-my-rgrep "grep")
  2217. (define-my-rgrep "global")
  2218. (define-key ctl-x-map "s" 'my-rgrep)
  2219. ;; (defun make ()
  2220. ;; "Run \"make -k\" in current directory."
  2221. ;; (interactive)
  2222. ;; (compile "make -k"))
  2223. (defalias 'make 'compile)
  2224. (define-key ctl-x-map "c" 'compile)
  2225. (defvar sed-in-place-history nil
  2226. "History of `sed-in-place'.")
  2227. (defvar sed-in-place-command "sed --in-place=.bak -e")
  2228. (defun sed-in-place (command)
  2229. "Issue sed in place COMMAND."
  2230. (interactive (list (read-shell-command "sed in place: "
  2231. (concat sed-in-place-command " ")
  2232. 'sed-in-place-history)))
  2233. (shell-command command
  2234. "*sed in place*"))
  2235. (defun dired-do-sed-in-place (&optional arg)
  2236. "Issue sed in place dired. If ARG is given, use the next ARG files."
  2237. (interactive "p")
  2238. (require 'dired-aux)
  2239. (let* ((files (dired-get-marked-files t arg))
  2240. (expr (dired-mark-read-string "Run sed-in-place for %s: "
  2241. nil
  2242. 'sed-in-place
  2243. arg
  2244. files)))
  2245. (if (equal expr
  2246. "")
  2247. (error "No expression specified")
  2248. (shell-command (concat sed-in-place-command
  2249. " '"
  2250. expr
  2251. "' "
  2252. (mapconcat 'shell-quote-argument
  2253. files
  2254. " "))
  2255. "*sed in place*"))))
  2256. (defun dir-show (&optional dir)
  2257. "Show DIR list."
  2258. (interactive)
  2259. (let ((bf (get-buffer-create "*dir show*"))
  2260. (list-directory-brief-switches "-C"))
  2261. (with-current-buffer bf
  2262. (list-directory (or nil
  2263. default-directory)
  2264. nil))
  2265. ))
  2266. (defun my-convmv-sjis2utf8-test ()
  2267. "Run `convmv -r -f sjis -t utf8 *'.
  2268. this is test, does not rename files."
  2269. (interactive)
  2270. (shell-command "convmv -r -f sjis -t utf8 *"))
  2271. (defun my-convmv-sjis2utf8-notest ()
  2272. "Run `convmv -r -f sjis -t utf8 * --notest'."
  2273. (interactive)
  2274. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  2275. (defun kill-ring-save-buffer-file-name ()
  2276. "Get current filename."
  2277. (interactive)
  2278. (let ((file buffer-file-name))
  2279. (if file
  2280. (progn (kill-new file)
  2281. (message file))
  2282. (message "not visiting file."))))
  2283. (defvar kill-ring-buffer-name "*kill-ring*"
  2284. "Buffer name for `kill-ring-buffer'.")
  2285. (defun open-kill-ring-buffer ()
  2286. "Open kill- ring buffer."
  2287. (interactive)
  2288. (pop-to-buffer
  2289. (with-current-buffer (get-buffer-create kill-ring-buffer-name)
  2290. (erase-buffer)
  2291. (yank)
  2292. (text-mode)
  2293. (current-local-map)
  2294. (goto-char (point-min))
  2295. (yank)
  2296. (current-buffer))))
  2297. (defun set-terminal-header (string)
  2298. "Set terminal header STRING."
  2299. (let ((savepos "\033[s")
  2300. (restorepos "\033[u")
  2301. (movecursor "\033[0;%dH")
  2302. (inverse "\033[7m")
  2303. (restorecolor "\033[0m")
  2304. (cols (frame-parameter nil 'width))
  2305. (length (length string)))
  2306. ;; (redraw-frame (selected-frame))
  2307. (send-string-to-terminal (concat savepos
  2308. (format movecursor
  2309. (1+ (- cols length)))
  2310. inverse
  2311. string
  2312. restorecolor
  2313. restorepos))
  2314. ))
  2315. (defun my-set-terminal-header ()
  2316. "Set terminal header."
  2317. (set-terminal-header (concat " "
  2318. user-login-name
  2319. "@"
  2320. (car (split-string system-name
  2321. "\\."))
  2322. " "
  2323. (format-time-string "%Y/%m/%d %T %z")
  2324. " ")))
  2325. ;; (run-with-timer
  2326. ;; 0.1
  2327. ;; 1
  2328. ;; 'my-set-terminal-header)
  2329. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2330. ;; ;; savage emacs
  2331. ;; ;; when enabled emacs fails to complete
  2332. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  2333. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  2334. ;; (setq arg
  2335. ;; (concat arg
  2336. ;; (if (eq nil
  2337. ;; (string-match "\\. *$"
  2338. ;; arg))
  2339. ;; ".")
  2340. ;; " Stupid!")))
  2341. ;; TODO: make these a library
  2342. (defvar info-in-prompt
  2343. nil
  2344. "System info in the form of \"[user@host] \".")
  2345. (setq info-in-prompt
  2346. (concat "["
  2347. user-login-name
  2348. "@"
  2349. (car (split-string system-name
  2350. "\\."))
  2351. "]"))
  2352. (defun my-real-function-subr-p (function)
  2353. "Return t if FUNCTION is a built-in function even if it is advised."
  2354. (let* ((advised (and (symbolp function)
  2355. (featurep 'advice)
  2356. (ad-get-advice-info function)))
  2357. (real-function
  2358. (or (and advised (let ((origname (cdr (assq 'origname advised))))
  2359. (and (fboundp origname)
  2360. origname)))
  2361. function))
  2362. (def (if (symbolp real-function)
  2363. (symbol-function real-function)
  2364. function)))
  2365. (subrp def)))
  2366. ;; (my-real-function-subr-p 'my-real-function-subr-p)
  2367. ;; (defadvice read-from-minibuffer (before info-in-prompt activate)
  2368. ;; "Show system info when use `read-from-minibuffer'."
  2369. ;; (ad-set-arg 0
  2370. ;; (concat my-system-info
  2371. ;; (ad-get-arg 0))))
  2372. ;; (defadvice read-string (before info-in-prompt activate)
  2373. ;; "Show system info when use `read-string'."
  2374. ;; (ad-set-arg 0
  2375. ;; (concat my-system-info
  2376. ;; (ad-get-arg 0))))
  2377. ;; (when (< emacs-major-version 24)
  2378. ;; (defadvice completing-read (before info-in-prompt activate)
  2379. ;; "Show system info when use `completing-read'."
  2380. ;; (ad-set-arg 0
  2381. ;; (concat my-system-info
  2382. ;; (ad-get-arg 0)))))
  2383. (defmacro info-in-prompt-set (&rest functions)
  2384. "Set info-in-prompt advices for FUNCTIONS."
  2385. `(progn
  2386. ,@(mapcar (lambda (f)
  2387. `(defadvice ,f (before info-in-prompt activate)
  2388. "Show info in prompt."
  2389. (let ((orig (ad-get-arg 0)))
  2390. (unless (string-match-p (regexp-quote info-in-prompt)
  2391. orig)
  2392. (ad-set-arg 0
  2393. (concat info-in-prompt
  2394. " "
  2395. orig))))))
  2396. functions)))
  2397. (info-in-prompt-set read-from-minibuffer
  2398. read-string
  2399. completing-read)
  2400. ;;; emacs.el ends here