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.

emacs.el 64 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  1. ;; load another file
  2. ;; (load-file "~/filepath")
  3. (unless (file-directory-p (expand-file-name user-emacs-directory))
  4. (make-directory (expand-file-name user-emacs-directory)))
  5. (let ((d (expand-file-name (concat user-emacs-directory
  6. "lisp"))))
  7. (unless (file-directory-p d)
  8. (make-directory d))
  9. (add-to-list 'load-path d))
  10. (require 'cl nil t)
  11. (progn ; hook run when directory changed
  12. (defvar buffer-file-changed-function nil "Hook run when buffer file changed.
  13. Each function is called with two args, the filename before changing and after changing.")
  14. (declare-function run-buffer-file-change-function "emacs.el")
  15. (add-hook 'post-command-hook
  16. 'run-buffer-file-changed-function)
  17. (lexical-let (previous-file)
  18. (defun run-buffer-file-changed-function ()
  19. ""
  20. (unless (and previous-file
  21. (equal previous-file
  22. (expand-file-name (or buffer-file-name
  23. default-directory))))
  24. (let ((pfile previous-file)
  25. (cfile (expand-file-name (or buffer-file-name
  26. default-directory))))
  27. (setq previous-file cfile)
  28. (run-hook-with-args 'directory-changed-function pfile cfile)))))
  29. ;; (add-hook 'directory-changed-function
  30. ;; (lambda (pdir cdir)
  31. ;; (message "dir changed %s to %s !" pdir cdir)))
  32. )
  33. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  34. ;; download library from web
  35. (require 'url)
  36. (defun dllib-if-unfound (lib url &optional bite-compile-p force-download-p)
  37. "if LIB does not exist, download it from URL and locate it to \"~/emacs.d/lisp/LIB.el\".
  38. return nil if LIB unfound and downloading failed, otherwise the path of LIB."
  39. (let* ((dir (expand-file-name (concat user-emacs-directory "lisp/")))
  40. (lpath (concat dir lib ".el"))
  41. (locate-p (locate-library lib)))
  42. (if (or force-download-p (not locate-p))
  43. (progn (condition-case nil
  44. (progn (message "downloading %s..." url)
  45. (url-copy-file url
  46. lpath
  47. t)
  48. (when bite-compile-p
  49. (byte-compile-file lpath)))
  50. (error (and (file-readable-p lpath)
  51. (delete-file lpath))
  52. (message "downloading %s...something wrong happened!" url)
  53. nil))
  54. (locate-library lib))
  55. locate-p)))
  56. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  57. ;; start and quit
  58. (setq inhibit-startup-message t)
  59. (setq frame-title-format (list '(:eval (format-time-string (or display-time-format "")))
  60. " | %b "
  61. '(:eval (number-to-string (length (buffer-list-not-start-with-space))))
  62. " buffers ["
  63. invocation-name
  64. " "
  65. emacs-version
  66. " "
  67. (symbol-name system-type)
  68. "] "
  69. '(:eval (symbol-name last-command))))
  70. (defun set-terminal-title (&rest args)
  71. ""
  72. (interactive "sString to set as title: ")
  73. (let ((tty (frame-parameter nil
  74. 'tty-type)))
  75. (when (and tty
  76. (eq t (compare-strings "xterm" 0 5
  77. tty 0 5)))
  78. (send-string-to-terminal (apply 'concat
  79. "\033]0;"
  80. `(,@args "\007"))))))
  81. (defun my-set-terminal-title ()
  82. ""
  83. (set-terminal-title "["
  84. invocation-name
  85. " "
  86. emacs-version
  87. " "
  88. (symbol-name system-type)
  89. "] "
  90. (abbreviate-file-name (or buffer-file-name default-directory))))
  91. (add-hook 'directory-changed-function
  92. (lambda (p c)
  93. (my-set-terminal-title)))
  94. (add-hook 'suspend-resume-hook
  95. 'my-set-terminal-title)
  96. (defun buffer-list-not-start-with-space ()
  97. (let ((bl (buffer-list))
  98. b nbl)
  99. (while bl
  100. (setq b (pop bl))
  101. (unless (string-equal " "
  102. (substring (buffer-name b)
  103. 0
  104. 1))
  105. (add-to-list 'nbl b)))
  106. nbl))
  107. (setq confirm-kill-emacs 'y-or-n-p)
  108. (setq gc-cons-threshold (* 1024 1024 4))
  109. (when window-system
  110. (add-to-list 'default-frame-alist '(cursor-type . box))
  111. (add-to-list 'default-frame-alist '(background-color . "white"))
  112. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  113. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100))) ; does not work?
  114. )
  115. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  116. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  117. (tool-bar-mode 0)
  118. (set-scroll-bar-mode nil)
  119. (add-hook 'kill-emacs-hook ; load when exitting to examine if file is written properly
  120. (lambda ()
  121. (when (file-readable-p "~/.emacs")
  122. (load-file "~/.emacs"))
  123. ))
  124. (add-hook 'after-init-hook
  125. (lambda ()
  126. (message (emacs-init-time))
  127. (switch-to-buffer "*Messages*")
  128. ))
  129. (cd ".") ; when using windows use / instead of \ in `default-directory'
  130. ;; locale
  131. (set-language-environment "Japanese")
  132. (set-default-coding-systems 'utf-8-unix)
  133. (prefer-coding-system 'utf-8-unix)
  134. (setq system-time-locale "C")
  135. ;; my prefix map
  136. (define-prefix-command 'my-prefix-map)
  137. '(add-hook 'after-init-hook
  138. (lambda ()
  139. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  140. ))
  141. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  142. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  143. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  144. ;; (comint-show-maximum-output)
  145. ;; kill scratch
  146. (add-hook 'after-init-hook
  147. (lambda ()
  148. (kill-buffer "*scratch*")))
  149. ;; modifier keys
  150. (setq mac-option-modifier 'control)
  151. (setq w32-apps-modifier 'meta)
  152. ;; display
  153. (setq redisplay-dont-pause t)
  154. (setq visible-bell t)
  155. (setq ring-bell-function 'ignore)
  156. (mouse-avoidance-mode 'banish)
  157. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  158. ;; global keys
  159. (and (dllib-if-unfound "drill-instructor"
  160. "https://raw.github.com/k1LoW/emacs-drill-instructor/master/drill-instructor.el"
  161. t)
  162. (require 'drill-instructor nil t)
  163. (setq drill-instructor-global t)
  164. (let ((map drill-instructor-key-map))
  165. (define-key map (kbd "RET") nil)
  166. (define-key map (kbd "DEL") nil)))
  167. '(mapc (lambda (key)
  168. (global-set-key (read-kbd-macro key) 'ignore))
  169. '("<up>" "<down>" "<right>" "<left>"))
  170. (define-key my-prefix-map (kbd "C-o") 'occur)
  171. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  172. (global-set-key (kbd "C-\\") help-map)
  173. (define-key ctl-x-map (kbd "DEL") help-map)
  174. (define-key ctl-x-map (kbd "C-h") help-map)
  175. (define-key help-map "a" 'apropos)
  176. ;; compose window
  177. (global-set-key [?\C--] 'other-window)
  178. (global-set-key [?\C-0] 'delete-window)
  179. (global-set-key [?\C-1] 'delete-other-windows)
  180. (global-set-key [?\C-2] 'split-window-vertically)
  181. (global-set-key [?\C-3] 'split-window-horizontally)
  182. ;; disable annoying keys
  183. (global-set-key [prior] 'ignore)
  184. (global-set-key (kbd "<next>") 'ignore)
  185. (global-set-key [menu] 'ignore)
  186. (global-set-key [down-mouse-1] 'ignore)
  187. (global-set-key [down-mouse-2] 'ignore)
  188. (global-set-key [down-mouse-3] 'ignore)
  189. (global-set-key [mouse-1] 'ignore)
  190. (global-set-key [mouse-2] 'ignore)
  191. (global-set-key [mouse-3] 'ignore)
  192. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  193. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  194. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  195. ;; mode-line
  196. (setq eol-mnemonic-dos "crlf")
  197. (setq eol-mnemonic-mac "cr")
  198. (setq eol-mnemonic-unix "lf")
  199. (which-function-mode 0)
  200. (line-number-mode 0)
  201. (column-number-mode 0)
  202. (size-indication-mode 0)
  203. (setq mode-line-position
  204. '(:eval (format "L%%l/%d,C%%c"
  205. (count-lines (point-max)
  206. (point-min)))))
  207. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  208. ;; display date
  209. (add-hook 'after-init-hook
  210. (lambda ()
  211. (when display-time-mode
  212. (display-time-update))
  213. ))
  214. (setq display-time-interval 29)
  215. (setq display-time-day-and-date t)
  216. (setq display-time-format "%a, %d %b %Y %T")
  217. (if window-system
  218. (display-time-mode 0)
  219. (display-time-mode 1))
  220. ;; ;; current directory
  221. ;; (let ((ls (member 'mode-line-buffer-identification
  222. ;; mode-line-format)))
  223. ;; (setcdr ls
  224. ;; (cons '(:eval (concat " ("
  225. ;; (abbreviate-file-name default-directory)
  226. ;; ")"))
  227. ;; (cdr ls))))
  228. ;; ;; display last modified time
  229. ;; (let ((ls (member 'mode-line-buffer-identification
  230. ;; mode-line-format)))
  231. ;; (setcdr ls
  232. ;; (cons '(:eval (concat " "
  233. ;; my-buffer-file-last-modified-time))
  234. ;; (cdr ls))))
  235. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  236. ;; minibuffer
  237. (setq insert-default-directory t)
  238. (setq completion-ignore-case t
  239. read-file-name-completion-ignore-case t
  240. read-buffer-completion-ignore-case t)
  241. (setq resize-mini-window t)
  242. (temp-buffer-resize-mode 1)
  243. (savehist-mode 1)
  244. (fset 'yes-or-no-p 'y-or-n-p)
  245. (define-key read-expression-map (kbd "TAB") 'lisp-complete-symbol) ; complete symbol when `eval'
  246. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  247. ;; letters, font-lock mode and fonts
  248. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  249. (when (eq system-type 'Darwin)
  250. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  251. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  252. (when (and (boundp 'input-method-activate-hook) ;ちょっと正しいかわかんない
  253. (boundp 'input-method-inactivate-hook))
  254. (add-hook 'input-method-activate-hook
  255. (lambda () (set-cursor-color "red")))
  256. (add-hook 'input-method-inactivate-hook
  257. (lambda () (set-cursor-color "black"))))
  258. (show-paren-mode 1)
  259. (setq show-paren-style 'mixed)
  260. (transient-mark-mode 1)
  261. (global-font-lock-mode 1)
  262. (setq font-lock-global-modes
  263. '(not
  264. help-mode
  265. eshell-mode
  266. term-mode
  267. Man-mode))
  268. (standard-display-ascii ?\n "$\n")
  269. (defvar my-eol-face
  270. '(("\n" . (0 font-lock-comment-face t nil)))
  271. )
  272. (defvar my-tab-face
  273. '(("\t" . '(0 highlight t nil))))
  274. (defvar my-jspace-face
  275. '(("\u3000" . '(0 highlight t nil))))
  276. (add-hook 'font-lock-mode-hook
  277. (lambda ()
  278. (font-lock-add-keywords nil my-eol-face)
  279. (font-lock-add-keywords nil my-jspace-face)
  280. ))
  281. ;; highlight current line
  282. ;; http://wiki.riywo.com/index.php?Meadow
  283. (defface hlline-face
  284. '((((type x w32)
  285. (class color)
  286. (background dark))
  287. (:background "midnightblue")) ; :foreground "white")) ;; ハイライトの文字色は変えない方がいいかも
  288. (((type x w32)
  289. (class color)
  290. (background light))
  291. (:background "gainsboro"))
  292. (t
  293. (:underline "black")))
  294. "*Face used by hl-line.")
  295. ;; (defface hlline-ul-face
  296. ;; '((t (:underline "yellow")))
  297. ;; "underline yellow")
  298. (setq hl-line-face 'hlline-face) ;; (setq hl-line-face nil)
  299. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  300. (setq hl-line-global-modes
  301. '(not
  302. term-mode))
  303. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  304. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  305. ;; fonts
  306. (defun my-set-ascii-and-jp-font-with-size (list)
  307. ""
  308. (if (> emacs-major-version 22)
  309. (progn ; 23 or later
  310. (set-face-attribute 'default nil
  311. :family (nth 0 list)
  312. :height (nth 1 list))
  313. (set-fontset-font "fontset-default"
  314. 'japanese-jisx0208
  315. (font-spec :family (nth 2 list) :size (nth 3 list)))
  316. (set-fontset-font "fontset-default"
  317. 'katakana-jisx0201
  318. (font-spec :family (nth 2 list) :size (nth 3 list))))
  319. ; font spec is available in emacs23 and later, cannot used in emacs22
  320. (progn ; 22
  321. (set-face-attribute 'default nil
  322. :family (nth 0 list)
  323. :height (nth 1 list))
  324. (set-fontset-font "fontset-default"
  325. 'japanese-jisx0208
  326. (cons (nth 2 list) "jisx0208.*"))
  327. (set-fontset-font "fontset-default"
  328. 'katakana-jisx0201
  329. (cons (nth 2 list) "jisx0201.*"))
  330. )))
  331. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 90 "takaogothic" 13))
  332. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 100 "takaogothic" 14))
  333. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 100 "ms gothic" 14))
  334. ;; (my-set-ascii-and-jp-font-with-size '("monaco" 75 "takaogothic" 11))
  335. ;; (my-set-ascii-and-jp-font-with-size '("monaco" 90 "takaogothic" 13))
  336. ;; (my-set-ascii-and-jp-font-with-size '("ProggyCleanTTSZ" 120 "takaogothic" 11))
  337. ;; あ a
  338. (and (dllib-if-unfound "set-modeline-color"
  339. "https://raw.github.com/10sr/emacs-lisp/master/set-modeline-color.el"
  340. t)
  341. (progn
  342. (require 'set-modeline-color nil t)))
  343. (let ((fg (face-foreground 'default))
  344. (bg (face-background 'default)))
  345. (set-face-background 'mode-line-inactive (if (face-inverse-video-p 'mode-line) fg bg))
  346. (set-face-foreground 'mode-line-inactive (if (face-inverse-video-p 'mode-line) bg fg)))
  347. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  348. ;; file handling
  349. (setq revert-without-query '(".+"))
  350. ;; save cursor position
  351. (setq save-place-file (concat user-emacs-directory
  352. "places"))
  353. (when (require 'saveplace nil t)
  354. (setq-default save-place t))
  355. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  356. (setq make-backup-files t)
  357. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  358. (setq backup-directory-alist
  359. (cons (cons "\\.*$" (expand-file-name "~/.emacs.d/backup"))
  360. backup-directory-alist))
  361. (setq version-control 'never)
  362. (setq delete-old-versions t)
  363. (setq auto-save-list-file-prefix (expand-file-name "~/.emacs.d/autosave/"))
  364. (setq delete-auto-save-files t)
  365. (add-to-list 'completion-ignored-extensions ".bak")
  366. ;; (setq delete-by-moving-to-trash t
  367. ;; trash-directory "~/.emacs.d/trash")
  368. (add-hook 'after-save-hook
  369. 'executable-make-buffer-file-executable-if-script-p)
  370. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  371. ;; editting
  372. (setq require-final-newline t)
  373. (setq kill-whole-line t)
  374. (setq scroll-conservatively 35
  375. scroll-margin 2
  376. scroll-step 0)
  377. (setq default-major-mode 'text-mode)
  378. (setq next-line-add-newlines nil)
  379. (setq kill-read-only-ok t)
  380. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  381. ;; (setq-default line-spacing 0.2)
  382. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  383. (setq-default tab-width 4)
  384. (setq-default indent-tabs-mode nil)
  385. (setq-default indent-line-function nil)
  386. ;(pc-selection-mode 1) ; this make some already defined keybind back to default
  387. (delete-selection-mode 1)
  388. (cua-mode 0)
  389. ;; key bindings
  390. ;; moving around
  391. ;; (global-set-key (kbd "M-j") 'next-line)
  392. ;; (global-set-key (kbd "M-k") 'previous-line)
  393. ;; (global-set-key (kbd "M-h") 'backward-char)
  394. ;; (global-set-key (kbd "M-l") 'forward-char)
  395. ;;(keyboard-translate ?\M-j ?\C-j)
  396. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  397. (define-key esc-map "p" 'backward-paragraph)
  398. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  399. (define-key esc-map "n" 'forward-paragraph)
  400. (global-set-key (kbd "C-<up>") (lambda () (interactive)(scroll-down 1)))
  401. (global-set-key (kbd "C-<down>") (lambda () (interactive)(scroll-up 1)))
  402. (global-set-key (kbd "C-<left>") 'scroll-down)
  403. (global-set-key (kbd "C-<right>") 'scroll-up)
  404. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  405. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  406. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  407. ;; C-h and DEL
  408. (global-set-key (kbd "C-h") (kbd "DEL"))
  409. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  410. (global-set-key (kbd "C-o")
  411. ;; (lambda ()
  412. ;; (interactive)
  413. ;; (move-end-of-line nil)
  414. ;; (newline-and-indent))
  415. (kbd "C-e C-m")
  416. )
  417. (global-set-key (kbd "C-k") 'kill-whole-line)
  418. (global-set-key (kbd "M-k") 'my-copy-whole-line)
  419. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  420. (global-set-key (kbd "M-u") 'undo)
  421. (global-set-key (kbd "C-r") 'query-replace-regexp)
  422. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  423. (global-set-key (kbd "M-i") (kbd "ESC TAB"))
  424. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  425. ;; gmail
  426. (setq mail-interactive t
  427. send-mail-function 'smtpmail-send-it
  428. ;; message-send-mail-function 'smtpmail-send-it
  429. smtpmail-smtp-server "smtp.gmail.com"
  430. smtpmail-smtp-service 587
  431. smtpmail-starttls-credentials '(("smtp.gmail.com" 587 "8.slashes@gmail.com" nil))
  432. smtpmail-auth-credentials '(("smtp.gmail.com" 587 "8.slashes@gmail.com" nil))
  433. user-mail-address "8.slashes@gmail.com")
  434. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  435. ;; buffer killing
  436. (defun my-query-kill-this-buffer ()
  437. ""
  438. (interactive)
  439. (if (y-or-n-p (concat "kill this buffer? :"))
  440. (kill-buffer (current-buffer))))
  441. (substitute-key-definition 'kill-buffer 'my-query-kill-this-buffer global-map)
  442. ;;(global-set-key "\C-xk" 'my-query-kill-this-buffer)
  443. '(setq package-archives '(("ELPA" . "http://tromey.com/elpa/")
  444. ("gnu" . "http://elpa.gnu.org/packages/")
  445. ("marmalade" . "http://marmalade-repo.org/packages/")))
  446. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  447. ;; some modes and hooks
  448. (require 'simple nil t)
  449. (and window-system
  450. (dllib-if-unfound "save-window-size" "https://raw.github.com/10sr/emacs-lisp/master/save-window-size.el" t)
  451. (require 'save-window-size nil t))
  452. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  453. ;; share clipboard with x
  454. ;; this page describes this in details, but only these sexps are needed
  455. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  456. (when (and window-system
  457. (not (eq window-system 'mac))
  458. )
  459. (setq x-select-enable-clipboard t ; these settings seems to be useless when using emacs in terminal
  460. x-select-enable-primary nil)
  461. ;; (global-set-key "\C-y" 'x-clipboard-yank)
  462. )
  463. (and (not x-select-enable-clipboard)
  464. (getenv "DISPLAY")
  465. (executable-find "xclip")
  466. (dllib-if-unfound "xclip" "http://www.emacswiki.org/emacs/download/xclip.el" t)
  467. (require 'xclip nil t)
  468. (turn-on-xclip))
  469. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  470. ;; mode
  471. (add-hook 'verilog-mode-hook
  472. (lambda ()
  473. (define-key verilog-mode-map ";" 'self-insert-command)))
  474. (setq diff-switches "-u")
  475. (add-hook 'diff-mode-hook
  476. (lambda ()
  477. (view-mode 1)
  478. (set-face-foreground 'diff-file-header-face nil)
  479. (set-face-foreground 'diff-header-face nil)
  480. (set-face-foreground 'diff-index-face "blue")
  481. (set-face-foreground 'diff-hunk-header-face "cyan")
  482. (set-face-foreground 'diff-context-face nil)
  483. (set-face-foreground 'diff-removed-face "red")
  484. (set-face-foreground 'diff-added-face "green")
  485. (set-face-foreground 'diff-changed-face "magenda")
  486. ))
  487. ;; (ffap-bindings)
  488. (add-hook 'sh-mode-hook
  489. (lambda ()
  490. (define-key sh-mode-map (kbd "C-x C-e") 'my-execute-shell-command-current-line)))
  491. (defun my-execute-shell-command-current-line ()
  492. ""
  493. (interactive)
  494. (shell-command (buffer-substring-no-properties (point-at-bol)
  495. (point))))
  496. (add-to-list 'auto-mode-alist
  497. '("\\(xinitrc\\|xprograms\\)\\'" . sh-mode))
  498. (setq python-python-command (or (executable-find "python3")
  499. (executable-find "python")))
  500. (defun my-python-run-as-command ()
  501. ""
  502. (interactive)
  503. (shell-command (concat python-python-command " " buffer-file-name)))
  504. (defun my-python-display-python-buffer ()
  505. ""
  506. (interactive)
  507. (set-window-text-height (display-buffer python-buffer
  508. t)
  509. 7))
  510. (add-hook 'python-mode-hook
  511. (lambda ()
  512. (define-key python-mode-map (kbd "C-c C-e") 'my-python-run-as-command)
  513. (define-key python-mode-map (kbd "C-c C-b") 'my-python-display-python-buffer)
  514. (define-key python-mode-map (kbd "C-m") 'newline-and-indent)))
  515. (add-hook 'inferior-python-mode-hook
  516. (lambda ()
  517. (my-python-display-python-buffer)
  518. (define-key inferior-python-mode-map (kbd "<up>") 'comint-previous-input)
  519. (define-key inferior-python-mode-map (kbd "<down>") 'comint-next-input)))
  520. (add-hook 'text-mode-hook
  521. (lambda ()
  522. (define-key text-mode-map (kbd "C-m") 'newline)))
  523. (add-to-list 'Info-default-directory-list (expand-file-name "~/.info/emacs-ja"))
  524. (setq bookmark-default-file "~/.emacs.d/bmk")
  525. (add-hook 'apropos-mode-hook
  526. (lambda ()
  527. (define-key apropos-mode-map "n" 'next-line)
  528. (define-key apropos-mode-map "p" 'previous-line)
  529. ))
  530. (define-key minibuffer-local-map (kbd "C-u") (lambda () (interactive) (delete-region (point-at-bol) (point))))
  531. (add-hook 'isearch-mode-hook
  532. (lambda ()
  533. ;; (define-key isearch-mode-map (kbd "C-j") 'isearch-other-control-char)
  534. ;; (define-key isearch-mode-map (kbd "C-k") 'isearch-other-control-char)
  535. ;; (define-key isearch-mode-map (kbd "C-h") 'isearch-other-control-char)
  536. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  537. (define-key isearch-mode-map (kbd "M-r") 'isearch-query-replace-regexp)))
  538. (add-hook 'outline-mode-hook
  539. (lambda ()
  540. (if (string-match "\\.md\\'" buffer-file-name)
  541. (set (make-local-variable 'outline-regexp) "#+ "))))
  542. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  543. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  544. (setq markdown-command (or (executable-find "markdown")
  545. (executable-find "markdown.pl")))
  546. (when (dllib-if-unfound "markdown-mode"
  547. "http://jblevins.org/projects/markdown-mode/markdown-mode.el"
  548. t)
  549. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'markdown-mode))
  550. (autoload 'markdown-mode "markdown-mode" "Major mode for editing Markdown files." nil)
  551. (add-hook 'markdown-mode-hook
  552. (lambda ()
  553. (outline-minor-mode 1)
  554. (set (make-local-variable 'comment-start) ";"))))
  555. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  556. ;; c-mode
  557. ;; (setq c-default-style "bsd")
  558. ;; BackSpace キーを「賢く」し,インデント幅は2桁,タブはスペースに展開
  559. (add-hook 'c-mode-common-hook
  560. (lambda ()
  561. (setq c-basic-offset 2
  562. indent-tabs-mode nil)
  563. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  564. (c-toggle-hungry-state 1)
  565. ))
  566. (when (dllib-if-unfound "js2-mode"
  567. "https://raw.github.com/mooz/js2-mode/master/js2-mode.el"
  568. t)
  569. (autoload 'js2-mode "js2-mode" nil t)
  570. (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  571. (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode)))
  572. ;; (add-hook 'js2-mode-hook
  573. ;; (lambda ()
  574. ;; (add-hook 'before-save-hook
  575. ;; 'my-indent-buffer
  576. ;; nil
  577. ;; t)))
  578. (add-hook 'js2-mode-hook
  579. (lambda ()
  580. (define-key js2-mode-map (kbd "C-m") (lambda ()
  581. (interactive)
  582. (js2-enter-key)
  583. (indent-for-tab-command)))
  584. (add-hook (kill-local-variable 'before-save-hook)
  585. 'js2-before-save)))
  586. (and nil
  587. (require 'zone nil t)
  588. (not (eq system-type 'windows-nt))
  589. ;; (zone-when-idle 180)
  590. (run-with-idle-timer 180 t (lambda ()
  591. (unless (memq major-mode
  592. '(term-mode))
  593. (zone)))))
  594. (when (require 'uniquify nil t)
  595. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  596. (setq uniquify-ignore-buffers-re "*[^*]+*")
  597. (setq uniquify-min-dir-content 1))
  598. (add-hook 'view-mode-hook
  599. (lambda()
  600. (define-key view-mode-map "j" (lambda() (interactive) (scroll-up 1)))
  601. (define-key view-mode-map "k" (lambda() (interactive) (scroll-down 1)))
  602. (define-key view-mode-map "v" 'toggle-read-only)
  603. (define-key view-mode-map "q" 'bury-buffer)
  604. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  605. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  606. ;; (define-key view-mode-map "n" 'nonincremental-repeat-search-forward)
  607. ;; (define-key view-mode-map "N" 'nonincremental-repeat-search-backward)
  608. (define-key view-mode-map "/" 'isearch-forward-regexp)
  609. (define-key view-mode-map "?" 'isearch-backward-regexp)
  610. (define-key view-mode-map "n" 'isearch-repeat-forward)
  611. (define-key view-mode-map "N" 'isearch-repeat-backward)
  612. ))
  613. (global-set-key "\M-r" 'view-mode)
  614. (setq view-read-only t)
  615. (add-hook 'Man-mode-hook
  616. (lambda ()
  617. (view-mode 1)
  618. (setq truncate-lines nil)))
  619. (setq Man-notify-method (if window-system
  620. 'newframe
  621. 'pushy))
  622. (require 'session nil t)
  623. (and (dllib-if-unfound "gtkbm"
  624. "https://raw.github.com/10sr/emacs-lisp/master/gtkbm.el"
  625. t)
  626. (require 'gtkbm nil t)
  627. (global-set-key (kbd "C-x C-d") 'gtkbm))
  628. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  629. ;; term mode
  630. ;; (setq multi-term-program shell-file-name)
  631. (and (dllib-if-unfound "multi-term"
  632. "http://www.emacswiki.org/emacs/download/multi-term.el"
  633. t)
  634. (require 'multi-term nil t)
  635. (setq multi-term-switch-after-close nil))
  636. (defun my-term-quit-or-send-raw ()
  637. ""
  638. (interactive)
  639. (if (get-buffer-process (current-buffer))
  640. (call-interactively 'term-send-raw)
  641. (kill-buffer)))
  642. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  643. ;; (setq term-ansi-default-program shell-file-name)
  644. (add-hook 'term-setup-hook
  645. (lambda ()
  646. (setq term-display-table (make-display-table))))
  647. (add-hook 'term-mode-hook
  648. (lambda ()
  649. (unless (memq (current-buffer) (and (featurep 'multi-term) ; current buffer is not multi-term buffer
  650. (multi-term-list)))
  651. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  652. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  653. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  654. ;; (define-key term-raw-map "\C-f" 'forward-char)
  655. ;; (define-key term-raw-map "\C-b" 'backward-char)
  656. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  657. (define-key term-raw-map "\C-x" (lookup-key (current-global-map) "\C-x"))
  658. (define-key term-raw-map "\C-z" (lookup-key (current-global-map) "\C-z")))
  659. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  660. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  661. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  662. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  663. (define-key term-raw-map [delete] 'term-send-raw)
  664. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  665. (define-key term-raw-map "\C-y" 'term-paste)
  666. (define-key term-raw-map "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  667. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  668. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  669. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  670. ;; (define-key term-raw-map "\C-d" 'delete-char)
  671. (set (make-local-variable 'scroll-margin) 0)
  672. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  673. ;; (cua-mode 0)
  674. ;; (and cua-mode
  675. ;; (local-unset-key (kbd "C-c")))
  676. ;; (define-key cua--prefix-override-keymap "\C-c" 'term-interrupt-subjob)
  677. (set (make-local-variable 'hl-line-range-function)
  678. (lambda ()
  679. '(0 . 0)))
  680. ))
  681. ;; (add-hook 'term-exec-hook 'forward-char)
  682. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  683. ;; buffer switching
  684. (when (require 'bs nil t)
  685. ;; (global-set-key "\C-x\C-b" 'bs-show)
  686. (defalias 'list-buffers 'bs-show))
  687. ;; (add-to-list 'bs-configurations '("processes" nil get-buffer-process ".*" nil nil))
  688. (add-to-list 'bs-configurations '("same-dir" nil buffer-same-dir-p ".*" nil nil))
  689. (add-to-list 'bs-configurations '("this-frame" nil (lambda (buf) (memq buf (my-frame-buffer-get))) ".*" nil nil))
  690. ;; (setq bs-configurations (list '("processes" nil get-buffer-process ".*" nil nil)
  691. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil bs-visits-non-file bs-sort-buffer-interns-are-last)))
  692. (setq bs-default-configuration "this-frame")
  693. (setq bs-default-sort-name "by name")
  694. (add-hook 'bs-mode-hook
  695. (lambda ()
  696. (setq bs-default-configuration "this-frame")
  697. ;; (and bs--show-all
  698. ;; (call-interactively 'bs-toggle-show-all))
  699. (set (make-local-variable 'scroll-margin) 0)
  700. ))
  701. (defun buffer-same-dir-p (bf)
  702. "return t if BF's dir is same as current dir, otherwise nil."
  703. (let ((cdir (expand-file-name default-directory)))
  704. (with-current-buffer bf
  705. (equal (expand-file-name default-directory) cdir))))
  706. (iswitchb-mode 1)
  707. (defun iswitchb-buffer-display-other-window ()
  708. ""
  709. (interactive)
  710. (let ((iswitchb-default-method 'display))
  711. (call-interactively 'iswitchb-buffer)))
  712. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  713. ;; sdic
  714. (defun sdic-describe-word-at-point-echo ()
  715. ""
  716. (interactive)
  717. (save-window-excursion
  718. (sdic-describe-word-at-point))
  719. (save-excursion
  720. (set-buffer sdic-buffer-name)
  721. (message (buffer-substring (point-min)
  722. (progn (goto-char (point-min))
  723. (or (and (re-search-forward "^\\w" nil t 4)
  724. (progn (previous-line) t)
  725. (point-at-eol))
  726. (point-max)))))))
  727. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  728. (setq sdic-waei-dictionary-list '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  729. (setq sdic-disable-select-window t)
  730. (setq sdic-window-height 7)
  731. (when (require 'sdic nil t)
  732. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  733. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo))
  734. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  735. ;; vc
  736. ;; (require 'vc)
  737. (setq vc-handled-backends '())
  738. (and (executable-find "git")
  739. (add-to-list 'vc-handled-backends 'GIT))
  740. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  741. ;; gauche-mode
  742. (let ((s (executable-find "gosh")))
  743. (setq scheme-program-name s
  744. gauche-program-name s))
  745. (defun run-gauche-other-window ()
  746. "Run gauche on other window"
  747. (interactive)
  748. (switch-to-buffer-other-window
  749. (get-buffer-create "*scheme*"))
  750. (run-gauche))
  751. (defun run-gauche ()
  752. "run gauche"
  753. (run-scheme gauche-program-name)
  754. )
  755. (defun scheme-send-buffer ()
  756. ""
  757. (interactive)
  758. (scheme-send-region (point-min) (point-max))
  759. (my-scheme-display-scheme-buffer)
  760. )
  761. (defun my-scheme-display-scheme-buffer ()
  762. ""
  763. (interactive)
  764. (set-window-text-height (display-buffer scheme-buffer
  765. t)
  766. 7))
  767. (add-hook 'scheme-mode-hook
  768. (lambda ()
  769. nil))
  770. (add-hook 'inferior-scheme-mode-hook
  771. (lambda ()
  772. ;; (my-scheme-display-scheme-buffer)
  773. ))
  774. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  775. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  776. (when (dllib-if-unfound "gauche-mode"
  777. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  778. t)
  779. (setq auto-mode-alist
  780. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  781. (setq auto-mode-alist
  782. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  783. (autoload 'gauche-mode "gauche-mode" "Major mode for Scheme." t)
  784. (autoload 'run-scheme "gauche-mode" "Run an inferior Scheme process." t)
  785. (add-hook 'gauche-mode-hook
  786. (lambda ()
  787. (define-key gauche-mode-map (kbd "C-c C-z") 'run-gauche-other-window)
  788. (define-key scheme-mode-map (kbd "C-c C-c") 'scheme-send-buffer)
  789. (define-key scheme-mode-map (kbd "C-c C-b") 'my-scheme-display-scheme-buffer)
  790. )))
  791. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  792. ;; recentf-mode
  793. (setq recentf-save-file (expand-file-name "~/.emacs.d/recentf")
  794. recentf-max-menu-items 20
  795. recentf-max-saved-items 30
  796. recentf-show-file-shortcuts-flag nil)
  797. (when (require 'recentf nil t)
  798. ;; (global-set-key "\C-x\C-r" 'recentf-open-files)
  799. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  800. ;; (add-hook 'find-file-hook
  801. ;; (lambda ()
  802. ;; (recentf-add-file default-directory)))
  803. (recentf-mode 1)
  804. ;; (add-to-list 'recentf-filename-handlers 'abbreviate-file-name)
  805. (add-to-list 'recentf-exclude (regexp-quote recentf-save-file))
  806. (and (dllib-if-unfound "recentf-show"
  807. "https://raw.github.com/10sr/emacs-lisp/master/recentf-show.el"
  808. t)
  809. (require 'recentf-show nil t)
  810. (define-key ctl-x-map (kbd "C-r") 'recentf-show)))
  811. (add-hook 'recentf-dialog-mode-hook
  812. (lambda ()
  813. (recentf-save-list)
  814. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f") 'my-recentf-cd-and-find-file)
  815. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  816. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  817. (define-key recentf-dialog-mode-map "p" 'previous-line)
  818. (define-key recentf-dialog-mode-map "n" 'next-line)
  819. (cd "~/")))
  820. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  821. ;; dired
  822. (require 'dired)
  823. (defun my-dired-echo-file-head (arg)
  824. ""
  825. (interactive "P")
  826. (let ((f (dired-get-filename)))
  827. (message "%s"
  828. (with-temp-buffer
  829. (insert-file-contents f)
  830. (buffer-substring-no-properties (point-min)
  831. (progn (goto-line (if arg
  832. (prefix-numeric-value arg)
  833. 10))
  834. (point-at-eol)))))))
  835. (defun my-dired-diff ()
  836. ""
  837. (interactive)
  838. (let ((files (dired-get-marked-files nil nil nil t)))
  839. (if (eq (car files)
  840. t)
  841. (diff (cadr files) (dired-get-filename))
  842. (message "One files must be marked!"))))
  843. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  844. "pop up buffer using `display-buffer' and return that buffer."
  845. (let ((bf (get-buffer-create buffer-or-name)))
  846. (with-current-buffer bf
  847. (cd ".")
  848. (erase-buffer))
  849. (display-buffer bf)
  850. bf))
  851. (defun my-replace-nasi-none ()
  852. ""
  853. (save-excursion
  854. (let ((buffer-read-only nil))
  855. (goto-char (point-min))
  856. (while (search-forward "なし" nil t)
  857. (replace-match "none")))))
  858. (defun dired-get-file-info ()
  859. "dired get disk usage"
  860. (interactive)
  861. (let ((f (dired-get-filename)))
  862. (if (file-directory-p f)
  863. (progn
  864. (message "calculating du...")
  865. (shell-command (concat "du -hsD "
  866. f)))
  867. (shell-command (concat "file "
  868. f)))))
  869. (defun my-dired-scroll-up ()
  870. ""
  871. (interactive)
  872. (my-dired-previous-line (- (window-height) 1)))
  873. (defun my-dired-scroll-down ()
  874. ""
  875. (interactive)
  876. (my-dired-next-line (- (window-height) 1)))
  877. (defun my-dired-previous-line (arg)
  878. ""
  879. (interactive "p")
  880. (when (> arg 0)
  881. ;; (ignore 'my-dired-print-current-dir-and-file)
  882. (dired-previous-line 1)
  883. (when (eq (line-number-at-pos)
  884. 2)
  885. (goto-line (- (line-number-at-pos (point-max))
  886. 1))
  887. (dired-move-to-filename))
  888. (my-dired-previous-line (- arg 1))
  889. ))
  890. (defun my-dired-next-line (arg)
  891. ""
  892. (interactive "p")
  893. (when (> arg 0)
  894. ;; (ignore 'my-dired-print-current-dir-and-file)
  895. (dired-next-line 1)
  896. (when (eq (point)
  897. (point-max))
  898. (goto-line 3)
  899. (dired-move-to-filename))
  900. (my-dired-next-line (- arg 1))
  901. ))
  902. (defun my-dired-print-current-dir-and-file ()
  903. (message "%s %s"
  904. default-directory
  905. (buffer-substring-no-properties (point-at-bol)
  906. (point-at-eol))))
  907. (defun dired-do-execute-as-command ()
  908. ""
  909. (interactive)
  910. (let ((file (dired-get-filename t)))
  911. (if (file-executable-p file)
  912. (start-process file nil file)
  913. (when (y-or-n-p "this file cant be executed. mark as executable and go? : ")
  914. (set-file-modes file (file-modes-symbolic-to-number "u+x" (file-modes file)))
  915. (start-process file nil file)))))
  916. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  917. (defun my-dired-x-open ()
  918. ""
  919. (interactive)
  920. (my-x-open (dired-get-filename t t)))
  921. (if (eq window-system 'mac)
  922. (setq dired-listing-switches "-lhFG")
  923. (setq dired-listing-switches "-lhFG --time-style=long-iso")
  924. )
  925. (setq dired-listing-switches "-lhFG")
  926. (put 'dired-find-alternate-file 'disabled nil) ; when using dired-find-alternate-file reuse current dired buffer for the file to open
  927. (setq dired-ls-F-marks-symlinks t)
  928. (require 'ls-lisp)
  929. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  930. (setq ls-lisp-dirs-first t)
  931. (setq ls-lisp-use-localized-time-format t)
  932. (setq ls-lisp-format-time-list
  933. '("%Y-%m-%d %H:%M"
  934. "%Y-%m-%d "))
  935. (setq dired-dwim-target t)
  936. ;; (add-hook 'dired-after-readin-hook
  937. ;; 'my-replace-nasi-none)
  938. ;; (add-hook 'after-init-hook
  939. ;; (lambda ()
  940. ;; (dired ".")))
  941. (add-hook 'dired-mode-hook
  942. (lambda ()
  943. (define-key dired-mode-map "o" 'my-dired-x-open)
  944. (define-key dired-mode-map "i" 'dired-get-file-info)
  945. (define-key dired-mode-map "f" 'find-file)
  946. (define-key dired-mode-map "!" 'shell-command)
  947. (define-key dired-mode-map "&" 'async-shell-command)
  948. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  949. (define-key dired-mode-map "=" 'my-dired-diff)
  950. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  951. (define-key dired-mode-map "b" 'gtkbm)
  952. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  953. (define-key dired-mode-map "@" (lambda () (interactive) (my-x-open ".")))
  954. (define-key dired-mode-map (kbd "TAB") 'other-window)
  955. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  956. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  957. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  958. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  959. (substitute-key-definition 'dired-next-line 'my-dired-next-line dired-mode-map)
  960. (substitute-key-definition 'dired-previous-line 'my-dired-previous-line dired-mode-map)
  961. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  962. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  963. (let ((file "._Icon\015"))
  964. (when (file-readable-p file)
  965. (delete-file file)))))
  966. (and (dllib-if-unfound "pack"
  967. "https://raw.github.com/10sr/emacs-lisp/master/pack.el"
  968. t)
  969. (require 'pack nil t)
  970. (add-hook 'dired-mode-hook
  971. (lambda ()
  972. (define-key dired-mode-map "P" 'dired-do-pack-or-unpack))))
  973. (and (dllib-if-unfound "dired-list-all-mode"
  974. "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  975. t)
  976. (require 'dired-list-all-mode nil t)
  977. (setq dired-listing-switches "-lhFG")
  978. (add-hook 'dired-mode-hook
  979. (lambda ()
  980. (define-key dired-mode-map "a" 'dired-list-all-mode)
  981. )))
  982. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  983. (defun my-dired-toggle-mark()
  984. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  985. (t dired-marker-char))))
  986. (delete-char 1)
  987. (insert cur)))
  988. (defun my-dired-mark (arg)
  989. "toggle mark the current (or next ARG) files.
  990. If on a subdir headerline, mark all its files except `.' and `..'.
  991. Use \\[dired-unmark-all-files] to remove all marks
  992. and \\[dired-unmark] on a subdir to remove the marks in
  993. this subdir."
  994. (interactive "P")
  995. (if (dired-get-subdir)
  996. (save-excursion (dired-mark-subdir-files))
  997. (let ((inhibit-read-only t))
  998. (dired-repeat-over-lines
  999. (prefix-numeric-value arg)
  1000. 'my-dired-toggle-mark))))
  1001. (defun my-dired-mark-backward (arg)
  1002. "In Dired, move up lines and toggle mark there.
  1003. Optional prefix ARG says how many lines to unflag; default is one line."
  1004. (interactive "p")
  1005. (my-dired-mark (- arg)))
  1006. (add-hook 'dired-mode-hook
  1007. (lambda ()
  1008. (local-set-key (kbd "SPC") 'my-dired-mark)
  1009. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1010. )
  1011. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1012. ;; eshell
  1013. (defun my-eshell-backward-delete-char ()
  1014. (interactive)
  1015. (when (< (save-excursion
  1016. (eshell-bol)
  1017. (point))
  1018. (point))
  1019. (backward-delete-char 1)))
  1020. (defun my-file-owner-p (file)
  1021. "t if FILE is owned by me."
  1022. (eq (user-uid) (nth 2 (file-attributes file))))
  1023. ;; ;; http://www.bookshelf.jp/pukiwiki/pukiwiki.php?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9
  1024. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1025. ;; (defun eshell/less (&rest args)
  1026. ;; "Invoke `view-file' on the file.
  1027. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1028. ;; (if args
  1029. ;; (while args
  1030. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1031. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1032. ;; (file (pop args)))
  1033. ;; (view-file file)
  1034. ;; (goto-line line))
  1035. ;; (view-file (pop args))))))
  1036. (defun eshell/o (&optional file)
  1037. (my-x-open (or file ".")))
  1038. ;; (defun eshell/vi (&rest args)
  1039. ;; "Invoke `find-file' on the file.
  1040. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1041. ;; (while args
  1042. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1043. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1044. ;; (file (pop args)))
  1045. ;; (find-file file)
  1046. ;; (goto-line line))
  1047. ;; (find-file (pop args)))))
  1048. (defun eshell/clear ()
  1049. "Clear the current buffer, leaving one prompt at the top."
  1050. (let ((inhibit-read-only t))
  1051. (erase-buffer)))
  1052. (defun eshell/d (&optional dirname switches)
  1053. "if first arg is omitted open current directory."
  1054. (dired (or dirname ".") switches))
  1055. (defun eshell/v ()
  1056. (view-mode 1))
  1057. (defun eshell/git (&rest args)
  1058. ""
  1059. (if (member (car args)
  1060. '("di" "diff" "log" "show"))
  1061. (apply 'eshell-exec-visual "git" args)
  1062. (shell-command (mapconcat 'shell-quote-argument
  1063. `("git" ,@args)
  1064. " ")
  1065. t)
  1066. ;; (eshell-external-command "git" args)
  1067. ))
  1068. (defalias 'eshell/: 'ignore)
  1069. (defalias 'eshell/type 'eshell/which)
  1070. ;; (defalias 'eshell/vim 'eshell/vi)
  1071. (defalias 'eshell/ff 'find-file)
  1072. (defalias 'eshell/q 'eshell/exit)
  1073. (defun eshell-goto-prompt ()
  1074. ""
  1075. (interactive)
  1076. (goto-char (point-max)))
  1077. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1078. "open eshell and change wd
  1079. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1080. (interactive)
  1081. (let ((dir (expand-file-name default-directory)))
  1082. (switch-to-buffer (or eshell-buffer-or-name
  1083. (eshell t)))
  1084. (unless (equal dir (expand-file-name default-directory))
  1085. ;; (cd dir)
  1086. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1087. ;; (eshell-emit-prompt)
  1088. (goto-char (point-max))
  1089. (eshell-kill-input)
  1090. (insert "cd " dir)
  1091. (eshell-send-input))))
  1092. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1093. (setq eshell-term-name "eterm-color")
  1094. (setq eshell-scroll-to-bottom-on-input t)
  1095. (setq eshell-cmpl-ignore-case t)
  1096. (setq eshell-cmpl-cycle-completions nil)
  1097. (setq eshell-highlight-prompt nil)
  1098. (setq eshell-ls-initial-args '("-hCFG"
  1099. "--color=auto"
  1100. "--time-style=long-iso")) ; "-hF")
  1101. (setq eshell-prompt-function
  1102. (lambda ()
  1103. (with-temp-buffer
  1104. (let (p1 p2 p3 p4)
  1105. (insert " [")
  1106. (setq p1 (point))
  1107. (insert (abbreviate-file-name default-directory))
  1108. (setq p2 (point))
  1109. (insert "]"
  1110. "\n")
  1111. (setq p3 (point))
  1112. (insert user-login-name
  1113. "@"
  1114. (or (getenv "HOSTNAME")
  1115. (substring (shell-command-to-string (or (executable-find "hostname")
  1116. "echo ''"))
  1117. 0
  1118. -1)))
  1119. (setq p4 (point))
  1120. (insert " "
  1121. (format-time-string "%a, %d %b %Y %T %z")
  1122. " eshell\n"
  1123. "last:"
  1124. (number-to-string eshell-last-command-status)
  1125. (if (= (user-uid)
  1126. 0)
  1127. " # "
  1128. " $ "))
  1129. (add-text-properties p1
  1130. p2
  1131. '(face ((foreground-color . "yellow"))))
  1132. (add-text-properties p3
  1133. p4
  1134. '(face ((foreground-color . "cyan"))))
  1135. (buffer-substring (point-min)
  1136. (point-max))))))
  1137. (add-hook 'eshell-mode-hook
  1138. (lambda ()
  1139. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1140. ;; (interactive)
  1141. ;; (switch-to-buffer (other-buffer))))
  1142. (define-key eshell-mode-map (kbd "C-u") (lambda ()
  1143. (interactive)
  1144. (eshell-goto-prompt)
  1145. (eshell-kill-input)))
  1146. (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1147. (interactive)
  1148. (eshell-goto-prompt)
  1149. (my-keyboard-quit)))
  1150. (define-key eshell-mode-map (kbd "DEL") 'my-eshell-backward-delete-char)
  1151. (define-key eshell-mode-map (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1152. (define-key eshell-mode-map (kbd "C-n") 'eshell-next-matching-input-from-input)
  1153. (apply 'eshell/addpath exec-path)
  1154. (set (make-local-variable 'scroll-margin) 0)
  1155. ;; (eshell/export "GIT_PAGER=")
  1156. ;; (eshell/export "GIT_EDITOR=")
  1157. (eshell/export "LC_MESSAGES=C")
  1158. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1159. (set (make-local-variable 'hl-line-range-function)
  1160. (lambda ()
  1161. '(0 . 0)))
  1162. (add-to-list 'eshell-virtual-targets
  1163. '("/dev/less"
  1164. (lambda (str)
  1165. (if str
  1166. (with-current-buffer nil)))
  1167. nil))
  1168. ))
  1169. (add-hook 'eshell-mode-hook
  1170. (lambda ()
  1171. (add-to-list 'eshell-visual-commands "vim")
  1172. ;; (add-to-list 'eshell-visual-commands "git")
  1173. (add-to-list 'eshell-output-filter-functions 'eshell-truncate-buffer)
  1174. (mapcar (lambda (alias)
  1175. (add-to-list 'eshell-command-aliases-list
  1176. alias))
  1177. '(
  1178. ; ("ll" "ls -l $*")
  1179. ; ("la" "ls -a $*")
  1180. ; ("lla" "ls -al $*")
  1181. ("ut" "slogin 03110414@un001.ecc.u-tokyo.ac.jp $*")
  1182. ("aptin" "apt-get install $*")
  1183. ("eless" "cat >>> (with-current-buffer (get-buffer-create \"*eshell output\") (erase-buffer) (setq buffer-read-only nil) (current-buffer)); (view-buffer (get-buffer \"*eshell output*\"))")
  1184. ("g" "git $*")
  1185. ))
  1186. ))
  1187. ;; (eval-after-load "em-alias"
  1188. ;; '(progn ;; (eshell/alias "ll" "ls -l")
  1189. ;; ;; (eshell/alias "la" "ls -a")
  1190. ;; ;; (eshell/alias "lla" "ls -al")
  1191. ;; (eshell/alias "sgcc" (if (eq system-type 'windows-nt)
  1192. ;; "gcc -o win.$1.exe $1"
  1193. ;; "gcc -o ${uname}.$1.out $1"))
  1194. ;; (eshell/alias "slmgcc" (if (eq system-type 'windows-nt)
  1195. ;; "gcc -lm -o win.$1.exe $1"
  1196. ;; "gcc -lm -o ${uname}.$1.out $1"))
  1197. ;; ;; (eshell/alias "ut" "ssh g841105@un001.ecc.u-tokyo.ac.jp")
  1198. ;; (add-to-list 'recentf-exclude (concat eshell-directory-name "alias"))))
  1199. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1200. ;; get last modified date
  1201. (defvar my-buffer-file-last-modified-time nil "")
  1202. (make-variable-buffer-local 'my-buffer-file-last-modified-time)
  1203. (defun my-get-file-last-modified-time (file)
  1204. ""
  1205. (nth 5
  1206. (file-attributes file)))
  1207. (defun my-set-buffer-file-last-modified-time ()
  1208. ""
  1209. (make-local-variable 'my-buffer-file-last-modified-time)
  1210. (setq my-buffer-file-last-modified-time
  1211. (format-time-string "%Y/%m/%d %H:%M" (my-get-file-last-modified-time buffer-file-name))))
  1212. (add-hook 'find-file-hook
  1213. 'my-set-buffer-file-last-modified-time)
  1214. (add-hook 'after-save-hook
  1215. 'my-set-buffer-file-last-modified-time)
  1216. (add-hook 'after-revert-hook
  1217. 'my-set-buffer-file-last-modified-time)
  1218. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1219. ;; frame buffer
  1220. ;; todo: work well when opening the file that was already opened on another window
  1221. (add-hook 'after-make-frame-functions
  1222. (lambda (f)
  1223. (set-window-buffer (frame-selected-window f)
  1224. "*Messages*")))
  1225. (defun make-frame-command-with-name (name)
  1226. "Make frame with name specified."
  1227. (interactive "sName for new frame: ")
  1228. (set-frame-parameter (make-frame-command)
  1229. 'name
  1230. name))
  1231. (defvar my-frame-buffer-plist nil)
  1232. (defun my-frame-buffer-add (&optional buf frame)
  1233. ""
  1234. (setq my-frame-buffer-plist
  1235. (plist-put my-frame-buffer-plist
  1236. (or frame
  1237. (selected-frame))
  1238. (let ((lst (my-frame-buffer-get frame)))
  1239. (if lst
  1240. (add-to-list 'lst
  1241. (or buf
  1242. (current-buffer)))
  1243. (list (or buf
  1244. (current-buffer))))))))
  1245. (defun my-frame-buffer-remove (&optional buf frame)
  1246. ""
  1247. (setq my-frame-buffer-plist
  1248. (plist-put my-frame-buffer-plist
  1249. (or frame
  1250. (selected-frame))
  1251. (delq (or buf
  1252. (current-buffer))
  1253. (my-frame-buffer-get frame)))))
  1254. (defun my-frame-buffer-get (&optional frame)
  1255. ""
  1256. (plist-get my-frame-buffer-plist
  1257. (or frame
  1258. (selected-frame))))
  1259. (defun my-frame-buffer-kill-all-buffer (&optional frame)
  1260. ""
  1261. (mapcar 'kill-buffer
  1262. (my-frame-buffer-get frame)))
  1263. (add-hook 'find-file-hook
  1264. 'my-frame-buffer-add)
  1265. (add-hook 'term-mode-hook
  1266. 'my-frame-buffer-add)
  1267. (add-hook 'eshell-mode-hook
  1268. 'my-frame-buffer-add)
  1269. (add-hook 'Man-mode-hook
  1270. 'my-frame-buffer-add)
  1271. (add-hook 'kill-buffer-hook
  1272. 'my-frame-buffer-remove)
  1273. (add-hook 'delete-frame-functions
  1274. 'my-frame-buffer-kill-all-buffer)
  1275. (defvar my-desktop-terminal "roxterm")
  1276. (defun my-execute-terminal ()
  1277. ""
  1278. (interactive)
  1279. (if (and (or (eq system-type 'windows-nt)
  1280. window-system)
  1281. my-desktop-terminal
  1282. )
  1283. (let ((process-environment (cons "TERM=xterm" process-environment)))
  1284. (start-process "terminal"
  1285. nil
  1286. my-desktop-terminal))
  1287. (my-term)))
  1288. (defun my-term ()
  1289. "open terminal buffer and return that buffer."
  1290. (interactive)
  1291. (if (eq system-type 'windows-nt)
  1292. (eshell t)
  1293. (if (featurep 'multi-term)
  1294. (multi-term)
  1295. (ansi-term "/bin/bash"))))
  1296. (defun my-delete-frame-or-kill-emacs ()
  1297. "delete frame when opening multiple frame, kill emacs when only one."
  1298. (interactive)
  1299. (if (eq 1
  1300. (length (frame-list)))
  1301. (save-buffers-kill-emacs)
  1302. (delete-frame)))
  1303. (define-key my-prefix-map (kbd "C-s") 'my-execute-terminal)
  1304. (define-key my-prefix-map (kbd "C-f") 'make-frame-command-with-name)
  1305. (global-set-key (kbd "C-x C-c") 'my-delete-frame-or-kill-emacs)
  1306. (define-key my-prefix-map (kbd "C-x C-c") 'save-buffers-kill-emacs)
  1307. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1308. ;; auto saving
  1309. (defun my-save-this-buffer (silent-p)
  1310. "save current buffer if can without asking"
  1311. (let ((cm (if (current-message)
  1312. (format "%s\n" (current-message))
  1313. ""))
  1314. (fun (symbol-function (if silent-p
  1315. 'ignore
  1316. 'message))))
  1317. (cond ((active-minibuffer-window) nil)
  1318. ((not buffer-file-name) (funcall fun "%ssaving... this buffer doesn't visit any file." cm) nil)
  1319. ((not (file-exists-p buffer-file-name)) (funcall fun "%ssaving... file not exist. save manually first." com) nil)
  1320. (buffer-read-only (funcall fun "%ssaving... this buffer is read-only." cm) nil)
  1321. ((not (buffer-modified-p)) (funcal fun "%ssaving... not modified yet." cm) nil)
  1322. ((not (file-writable-p buffer-file-name)) (funcall fun "%ssaving... you cannot change this file." cm) nil)
  1323. (t (funcall fun "%ssaving..." cm)
  1324. (save-buffer)
  1325. (funcall fun "%ssaving... done." cm)))))
  1326. ;; (if (and buffer-file-name
  1327. ;; (not buffer-read-only)
  1328. ;; (buffer-modified-p)
  1329. ;; (file-writable-p buffer-file-name))
  1330. ;; (save-buffer))) ; silent one
  1331. (defvar my-auto-save-this-buffer nil "auto save timer object")
  1332. (defun my-auto-save-this-buffer (sec &optional silent-p)
  1333. "auto save current buffer if idle for SEC.
  1334. when SEC is nil, stop auto save if enabled."
  1335. (if sec
  1336. (progn (when my-auto-save-this-buffer
  1337. (cancel-timer my-auto-save-this-buffer)
  1338. (setq my-auto-save-this-buffer nil))
  1339. (setq my-auto-save-this-buffer (run-with-idle-timer sec t 'my-save-this-buffer silent-p)))
  1340. (when my-auto-save-this-buffer
  1341. (cancel-timer my-auto-save-this-buffer)
  1342. (setq my-auto-save-this-buffer nil))))
  1343. (my-auto-save-this-buffer 2 t)
  1344. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1345. ;; misc funcs
  1346. (defalias 'qcalc 'quick-calc)
  1347. (defvar git-command-history nil
  1348. "History list for git command.")
  1349. (defun my-git-branch-name (str)
  1350. ""
  1351. (with-temp-buffer
  1352. (shell-command "git branch --no-color" t)
  1353. (if (search-forward "*" nil t)
  1354. (progn (forward-char 1)
  1355. (format str
  1356. (buffer-substring-no-properties (point)
  1357. (point-at-eol))))
  1358. "")))
  1359. (defun my-git-shell-command (cmd)
  1360. ""
  1361. (interactive (list (read-shell-command (format "[%s]%s $ git : "
  1362. (abbreviate-file-name default-directory)
  1363. (my-git-branch-name "[GIT:%s]"))
  1364. nil
  1365. 'git-command-history)))
  1366. (let ((dir default-directory)
  1367. (bf (get-buffer-create "*Git Output*"))
  1368. )
  1369. (delete-windows-on bf t)
  1370. (shell-command (concat "git "
  1371. cmd)
  1372. bf)
  1373. (with-current-buffer bf
  1374. (cd dir)
  1375. (and (require 'ansi-color nil t)
  1376. (ansi-color-apply-on-region (point-min)
  1377. (point-max)))
  1378. (setq buffer-read-only t)
  1379. (view-mode 1)
  1380. )))
  1381. (define-key ctl-x-map "g" 'my-git-shell-command)
  1382. (defun my-kill-buffers ()
  1383. ""
  1384. (interactive)
  1385. (mapcar (lambda (buf)
  1386. (when (buffer-file-name buf)
  1387. (kill-buffer buf)))
  1388. (buffer-list)))
  1389. (defvar my-filer nil)
  1390. (setq my-filer (or (executable-find "pcmanfm")
  1391. (executable-find "nautilus")))
  1392. (defun my-x-open (file)
  1393. "open file."
  1394. (interactive "FOpen File: ")
  1395. (setq file (expand-file-name file))
  1396. (message "Opening %s..." file)
  1397. (cond ((eq system-type 'windows-nt)
  1398. (call-process "cmd.exe" nil 0 nil "/c" "start" "" (convert-standard-filename file)))
  1399. ((eq system-type 'darwin)
  1400. (call-process "open" nil 0 nil file))
  1401. ((getenv "DISPLAY")
  1402. (call-process (or my-filer "xdg-open") nil 0 nil file))
  1403. (t
  1404. (find-file file))
  1405. )
  1406. ;; (recentf-add-file file)
  1407. (message "Opening %s...done" file))
  1408. (defun my-keyboard-quit ()
  1409. ""
  1410. (interactive)
  1411. (run-hooks 'before-keyboard-quit-hook)
  1412. ;; (redisplay t)
  1413. (redraw-display)
  1414. ;; (run-hooks 'window-configuration-change-hook)
  1415. (my-revert-buffer-if-needed)
  1416. ;; (revert-buffer t t)
  1417. (keyboard-quit)
  1418. (insert "insert me")
  1419. (run-hooks 'after-keyboard-quit-hook))
  1420. (substitute-key-definition 'keyboard-quit 'my-keyboard-quit global-map)
  1421. ;; (global-set-key (kbd "C-g") 'my-keyboard-quit)
  1422. (defun my-convmv-sjis2utf8-test ()
  1423. "run `convmv -r -f sjis -t utf8 *'
  1424. this is test, does not rename files"
  1425. (interactive)
  1426. (shell-command "convmv -r -f sjis -t utf8 *"))
  1427. (defun my-convmv-sjis2utf8-notest ()
  1428. "run `convmv -r -f sjis -t utf8 * --notest'"
  1429. (interactive)
  1430. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  1431. (defun my-copy-whole-line ()
  1432. ""
  1433. (interactive)
  1434. (kill-new (concat (buffer-substring (point-at-bol)
  1435. (point-at-eol))
  1436. "\n")))
  1437. (defun kill-ring-save-buffer-file-name ()
  1438. "get current filename"
  1439. (interactive)
  1440. (let ((file buffer-file-name))
  1441. (if file
  1442. (progn (kill-new file)
  1443. (message file))
  1444. (message "not visiting file."))))
  1445. (defvar my-revert-buffer-if-needed-last-buffer nil)
  1446. (defun my-revert-buffer-if-needed ()
  1447. ""
  1448. (interactive)
  1449. (unless (eq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1450. (setq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1451. (when (or (and (eq major-mode 'dired-mode)
  1452. (dired-directory-changed-p default-directory))
  1453. (not (verify-visited-file-modtime (current-buffer))))
  1454. (revert-buffer t t)
  1455. (message "%s reverted." (buffer-name))
  1456. )))
  1457. (add-hook 'post-command-hook ; 'window-configuration-change-hook
  1458. 'my-revert-buffer-if-needed)
  1459. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1460. ;; forked from http://d.hatena.ne.jp/khiker/20100119/window_resize
  1461. (define-key my-prefix-map (kbd "C-w") 'my-window-organizer)
  1462. (defun my-window-organizer ()
  1463. "Control window size and position."
  1464. (interactive)
  1465. (save-selected-window
  1466. (select-window (window-at 0 0))
  1467. (let ( ;; (window-obj (selected-window))
  1468. ;; (current-width (window-width))
  1469. ;; (current-height (window-height))
  1470. action
  1471. c)
  1472. (catch 'end-flag
  1473. (while t
  1474. (setq action
  1475. (read-key-sequence-vector (format "size[%dx%d] 1: maximize; 2, 3: split; 0: delete; o: select other; j, l: enlarge; h, k: shrink; q: quit."
  1476. (window-width)
  1477. (window-height))))
  1478. (setq c (aref action 0))
  1479. (cond ((= c ?l)
  1480. (unless (eq (window-width) (frame-width))
  1481. (enlarge-window-horizontally 1)))
  1482. ((= c ?h)
  1483. (unless (eq (window-width) (frame-width))
  1484. (shrink-window-horizontally 1)))
  1485. ((= c ?j)
  1486. (enlarge-window 1))
  1487. ((= c ?k)
  1488. (shrink-window 1))
  1489. ((= c ?o)
  1490. (other-window 1))
  1491. ((memq c '(?d ?0))
  1492. (unless (eq (selected-window) (next-window (selected-window) 0 1))
  1493. (delete-window (selected-window))))
  1494. ((= c ?1)
  1495. (delete-other-windows))
  1496. ((= c ?2)
  1497. (split-window-vertically))
  1498. ((= c ?3)
  1499. (split-window-horizontally))
  1500. ((memq c '(?q ?\C-g))
  1501. (message "Quit")
  1502. (throw 'end-flag t))
  1503. (t
  1504. (beep))))))))
  1505. ;; (aref (read-key-sequence-vector "aa") 0)
  1506. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1507. ;; ;; emacsを殺伐とさせる
  1508. ;; ;; 補完させるとき失敗するからなし
  1509. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  1510. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  1511. ;; (setq arg
  1512. ;; (concat arg
  1513. ;; (if (eq nil (string-match "\\. *$" arg)) ".")
  1514. ;; " Stupid!")))
  1515. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1516. ;; japanese input method
  1517. (defun my-load-scim ()
  1518. "use scim-bridge.el as japanese im."
  1519. ;; Load scim-bridge.
  1520. (when (require 'scim-bridge nil t)
  1521. ;; Turn on scim-mode automatically after loading .emacs
  1522. (add-hook 'after-init-hook 'scim-mode-on)
  1523. (setq scim-cursor-color "red")
  1524. (scim-define-preedit-key ?\^h t)
  1525. (scim-define-common-key ?\* nil)
  1526. (scim-define-common-key ?\^/ nil)))
  1527. (defun my-load-anthy ()
  1528. "use anthy.el as japanese im."
  1529. ;; anthy
  1530. (when (require 'anthy nil t)
  1531. (global-set-key (kbd "<muhenkan>") (lambda () (interactive) (anthy-mode-off)))
  1532. (global-set-key (kbd "<henkan>") (lambda () (interactive) (anthy-mode-on)))
  1533. (when (>= emacs-major-version 23)
  1534. (setq anthy-accept-timeout 1))))
  1535. ;; quail
  1536. ;; aproposs input-method for some information
  1537. ;; (setq default-input-method "japanese")
  1538. (defun my-load-mozc-el ()
  1539. ""
  1540. (setq mozc-leim-title "[MZ]")
  1541. (when (require 'mozc nil t)
  1542. (setq defauit-input-method "japanese-mozc")
  1543. ))
  1544. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1545. ;; for windows
  1546. ;; (add-to-list 'exec-path "c:/Program Files/Gauche/bin/")
  1547. (defun start-ckw-bash ()
  1548. ""
  1549. (interactive)
  1550. (start-process "ckw_bash"
  1551. nil
  1552. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe")) ; cじゃないといけないらしい
  1553. (defun my-w32-add-export-path (&rest args)
  1554. ""
  1555. (mapcar (lambda (path)
  1556. (add-to-list 'exec-path (expand-file-name path)))
  1557. (reverse args))
  1558. (setenv "PATH"
  1559. (mapconcat 'convert-standard-filename
  1560. exec-path
  1561. ";")))
  1562. (when (eq system-type 'windows-nt)
  1563. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  1564. ;; (setq python-python-command "c:/Python26/python.exe")
  1565. (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  1566. (my-w32-add-export-path "c:/WINDOWS"
  1567. (expand-file-name "~/bin")
  1568. (expand-file-name "~/dbx/apps/bin"))
  1569. (when window-system
  1570. (setq w32-enable-synthesized-fonts t))
  1571. (setq file-name-coding-system 'sjis))