You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2710 lines
93 KiB

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