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.
 
 
 
 
 
 

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