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.
 
 
 
 
 
 

2718 lines
94 KiB

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