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.
 
 
 
 
 
 

2679 regels
92 KiB

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