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.
 
 
 
 
 
 

2704 regels
93 KiB

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