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.
 
 
 
 
 
 

2704 lines
94 KiB

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