Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

2700 wiersze
93 KiB

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