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.
 
 
 
 
 
 

2522 lines
86 KiB

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