25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

emacs.el 86 KiB

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