您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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