image-dired.el 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603
  1. ;;; image-dired.el --- use dired to browse and manipulate your images
  2. ;;
  3. ;; Copyright (C) 2005-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Version: 0.4.11
  6. ;; Keywords: multimedia
  7. ;; Author: Mathias Dahl <mathias.rem0veth1s.dahl@gmail.com>
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;;
  21. ;; BACKGROUND
  22. ;; ==========
  23. ;;
  24. ;; I needed a program to browse, organize and tag my pictures. I got
  25. ;; tired of the old gallery program I used as it did not allow
  26. ;; multi-file operations easily. Also, it put things out of my
  27. ;; control. Image viewing programs I tested did not allow multi-file
  28. ;; operations or did not do what I wanted it to.
  29. ;;
  30. ;; So, I got the idea to use the wonderful functionality of Emacs and
  31. ;; `dired' to do it. It would allow me to do almost anything I wanted,
  32. ;; which is basically just to browse all my pictures in an easy way,
  33. ;; letting me manipulate and tag them in various ways. `dired' already
  34. ;; provide all the file handling and navigation facilities; I only
  35. ;; needed to add some functions to display the images.
  36. ;;
  37. ;; I briefly tried out thumbs.el, and although it seemed more
  38. ;; powerful than this package, it did not work the way I wanted to. It
  39. ;; was too slow to created thumbnails of all files in a directory (I
  40. ;; currently keep all my 2000+ images in the same directory) and
  41. ;; browsing the thumbnail buffer was slow too. image-dired.el will not
  42. ;; create thumbnails until they are needed and the browsing is done
  43. ;; quickly and easily in dired. I copied a great deal of ideas and
  44. ;; code from there though... :)
  45. ;;
  46. ;; `image-dired' stores the thumbnail files in `image-dired-dir'
  47. ;; using the file name format ORIGNAME.thumb.ORIGEXT. For example
  48. ;; ~/.emacs.d/image-dired/myimage01.thumb.jpg. The "database" is for
  49. ;; now just a plain text file with the following format:
  50. ;;
  51. ;; file-name-non-directory;comment:comment-text;tag1;tag2;tag3;...;tagN
  52. ;;
  53. ;;
  54. ;; PREREQUISITES
  55. ;; =============
  56. ;;
  57. ;; * The ImageMagick package. Currently, `convert' and `mogrify' are
  58. ;; used. Find it here: http://www.imagemagick.org.
  59. ;;
  60. ;; * For non-lossy rotation of JPEG images, the JpegTRAN program is
  61. ;; needed.
  62. ;;
  63. ;; * For `image-dired-get-exif-data' and `image-dired-write-exif-data' to work,
  64. ;; the command line tool `exiftool' is needed. It can be found here:
  65. ;; http://www.sno.phy.queensu.ca/~phil/exiftool/. These two functions
  66. ;; are, among other things, used for writing comments to image files
  67. ;; using `image-dired-thumbnail-set-image-description' and to create
  68. ;; "unique" file names using `image-dired-get-exif-file-name' (used by
  69. ;; `image-dired-copy-with-exif-file-name').
  70. ;;
  71. ;;
  72. ;; USAGE
  73. ;; =====
  74. ;;
  75. ;; This information has been moved to the manual. Type `C-h r' to open
  76. ;; the Emacs manual and go to the node Thumbnails by typing `g
  77. ;; Thumbnails RET'.
  78. ;;
  79. ;; Quickstart: M-x image-dired RET DIRNAME RET
  80. ;;
  81. ;; where DIRNAME is a directory containing image files.
  82. ;;
  83. ;; LIMITATIONS
  84. ;; ===========
  85. ;;
  86. ;; * Supports all image formats that Emacs and convert supports, but
  87. ;; the thumbnails are hard-coded to JPEG format.
  88. ;;
  89. ;; * WARNING: The "database" format used might be changed so keep a
  90. ;; backup of `image-dired-db-file' when testing new versions.
  91. ;;
  92. ;;
  93. ;; TODO
  94. ;; ====
  95. ;;
  96. ;; * Support gallery creation when using per-directory thumbnail
  97. ;; storage.
  98. ;;
  99. ;; * Some sort of auto-rotate function based on rotate info in the
  100. ;; EXIF data.
  101. ;;
  102. ;; * Check if exiftool exist before trying to call it to give a better
  103. ;; error message.
  104. ;;
  105. ;; * Investigate if it is possible to also write the tags to the image
  106. ;; files.
  107. ;;
  108. ;; * From thumbs.el: Add an option for clean-up/max-size functionality
  109. ;; for thumbnail directory.
  110. ;;
  111. ;; * From thumbs.el: Add setroot function.
  112. ;;
  113. ;; * From thumbs.el: Add image resizing, if useful (image-dired's automatic
  114. ;; "image fit" might be enough)
  115. ;;
  116. ;; * From thumbs.el: Add the "modify" commands (emboss, negate,
  117. ;; monochrome etc).
  118. ;;
  119. ;; * Asynchronous creation of thumbnails.
  120. ;;
  121. ;; * Add `image-dired-display-thumbs-ring' and functions to cycle that. Find
  122. ;; out which is best, saving old batch just before inserting new, or
  123. ;; saving the current batch in the ring when inserting it. Adding it
  124. ;; probably needs rewriting `image-dired-display-thumbs' to be more general.
  125. ;;
  126. ;; * Find some way of toggling on and off really nice keybindings in
  127. ;; dired (for example, using C-n or <down> instead of C-S-n). Richard
  128. ;; suggested that we could keep C-t as prefix for image-dired commands
  129. ;; as it is currently not used in dired. He also suggested that
  130. ;; `dired-next-line' and `dired-previous-line' figure out if
  131. ;; image-dired is enabled in the current buffer and, if it is, call
  132. ;; `image-dired-dired-next-line' and
  133. ;; `image-dired-dired-previous-line', respectively. Update: This is
  134. ;; partly done; some bindings have now been added to dired.
  135. ;;
  136. ;; * Enhanced gallery creation with basic CSS-support and pagination
  137. ;; of tag pages with many pictures.
  138. ;;
  139. ;; * Rewrite `image-dired-modify-mark-on-thumb-original-file' to be
  140. ;; less ugly.
  141. ;;
  142. ;; * In some way keep track of buffers and windows and stuff so that
  143. ;; it works as the user expects.
  144. ;;
  145. ;; * More/better documentation
  146. ;;
  147. ;;
  148. ;;; Code:
  149. (require 'dired)
  150. (require 'format-spec)
  151. (require 'widget)
  152. (eval-when-compile
  153. (require 'cl)
  154. (require 'wid-edit))
  155. (defgroup image-dired nil
  156. "Use dired to browse your images as thumbnails, and more."
  157. :prefix "image-dired-"
  158. :group 'multimedia)
  159. (defcustom image-dired-dir (locate-user-emacs-file "image-dired/")
  160. "Directory where thumbnail images are stored."
  161. :type 'string
  162. :group 'image-dired)
  163. (defcustom image-dired-thumbnail-storage 'use-image-dired-dir
  164. "How to store image-dired's thumbnail files.
  165. Image-Dired can store thumbnail files in one of two ways and this is
  166. controlled by this variable. \"Use image-dired dir\" means that the
  167. thumbnails are stored in a central directory. \"Per directory\"
  168. means that each thumbnail is stored in a subdirectory called
  169. \".image-dired\" in the same directory where the image file is.
  170. \"Thumbnail Managing Standard\" means that the thumbnails are
  171. stored and generated according to the Thumbnail Managing Standard
  172. that allows sharing of thumbnails across different programs."
  173. :type '(choice :tag "How to store thumbnail files"
  174. (const :tag "Thumbnail Managing Standard" standard)
  175. (const :tag "Use image-dired-dir" use-image-dired-dir)
  176. (const :tag "Per-directory" per-directory))
  177. :group 'image-dired)
  178. (defcustom image-dired-db-file
  179. (expand-file-name ".image-dired_db" image-dired-dir)
  180. "Database file where file names and their associated tags are stored."
  181. :type 'string
  182. :group 'image-dired)
  183. (defcustom image-dired-temp-image-file
  184. (expand-file-name ".image-dired_temp" image-dired-dir)
  185. "Name of temporary image file used by various commands."
  186. :type 'string
  187. :group 'image-dired)
  188. (defcustom image-dired-gallery-dir
  189. (expand-file-name ".image-dired_gallery" image-dired-dir)
  190. "Directory to store generated gallery html pages.
  191. This path needs to be \"shared\" to the public so that it can access
  192. the index.html page that image-dired creates."
  193. :type 'string
  194. :group 'image-dired)
  195. (defcustom image-dired-gallery-image-root-url
  196. "http://your.own.server/image-diredpics"
  197. "URL where the full size images are to be found.
  198. Note that this path has to be configured in your web server. Image-Dired
  199. expects to find pictures in this directory."
  200. :type 'string
  201. :group 'image-dired)
  202. (defcustom image-dired-gallery-thumb-image-root-url
  203. "http://your.own.server/image-diredthumbs"
  204. "URL where the thumbnail images are to be found.
  205. Note that this path has to be configured in your web server. Image-Dired
  206. expects to find pictures in this directory."
  207. :type 'string
  208. :group 'image-dired)
  209. (defcustom image-dired-cmd-create-thumbnail-program
  210. "convert"
  211. "Executable used to create thumbnail.
  212. Used together with `image-dired-cmd-create-thumbnail-options'."
  213. :type 'string
  214. :group 'image-dired)
  215. (defcustom image-dired-cmd-create-thumbnail-options
  216. "%p -size %wx%h \"%f\" -resize \"%wx%h>\" -strip jpeg:\"%t\""
  217. "Format of command used to create thumbnail image.
  218. Available options are %p which is replaced by
  219. `image-dired-cmd-create-thumbnail-program', %w which is replaced by
  220. `image-dired-thumb-width', %h which is replaced by `image-dired-thumb-height',
  221. %f which is replaced by the file name of the original image and %t
  222. which is replaced by the file name of the thumbnail file."
  223. :type 'string
  224. :group 'image-dired)
  225. (defcustom image-dired-cmd-create-temp-image-program
  226. "convert"
  227. "Executable used to create temporary image.
  228. Used together with `image-dired-cmd-create-temp-image-options'."
  229. :type 'string
  230. :group 'image-dired)
  231. (defcustom image-dired-cmd-create-temp-image-options
  232. "%p -size %wx%h \"%f\" -resize \"%wx%h>\" -strip jpeg:\"%t\""
  233. "Format of command used to create temporary image for display window.
  234. Available options are %p which is replaced by
  235. `image-dired-cmd-create-temp-image-program', %w and %h which is replaced by
  236. the calculated max size for width and height in the image display window,
  237. %f which is replaced by the file name of the original image and %t which
  238. is replaced by the file name of the temporary file."
  239. :type 'string
  240. :group 'image-dired)
  241. (defcustom image-dired-cmd-pngnq-program (executable-find "pngnq")
  242. "The file name of the `pngnq' program.
  243. It quantizes colors of PNG images down to 256 colors."
  244. :type '(choice (const :tag "Not Set" nil) string)
  245. :group 'image-dired)
  246. (defcustom image-dired-cmd-pngcrush-program (executable-find "pngcrush")
  247. "The file name of the `pngcrush' program.
  248. It optimizes the compression of PNG images. Also it adds PNG textual chunks
  249. with the information required by the Thumbnail Managing Standard."
  250. :type '(choice (const :tag "Not Set" nil) string)
  251. :group 'image-dired)
  252. (defcustom image-dired-cmd-create-standard-thumbnail-command
  253. (concat
  254. image-dired-cmd-create-thumbnail-program " "
  255. "-size %wx%h \"%f\" "
  256. (unless (or image-dired-cmd-pngcrush-program image-dired-cmd-pngnq-program)
  257. (concat
  258. "-set \"Thumb::MTime\" \"%m\" "
  259. "-set \"Thumb::URI\" \"file://%f\" "
  260. "-set \"Description\" \"Thumbnail of file://%f\" "
  261. "-set \"Software\" \"" (emacs-version) "\" "))
  262. "-thumbnail \"%wx%h>\" png:\"%t\""
  263. (if image-dired-cmd-pngnq-program
  264. (concat
  265. " ; " image-dired-cmd-pngnq-program " -f \"%t\""
  266. (unless image-dired-cmd-pngcrush-program
  267. " ; mv %q %t")))
  268. (if image-dired-cmd-pngcrush-program
  269. (concat
  270. (unless image-dired-cmd-pngcrush-program
  271. " ; cp %t %q")
  272. " ; " image-dired-cmd-pngcrush-program " -q "
  273. "-text b \"Description\" \"Thumbnail of file://%f\" "
  274. "-text b \"Software\" \"" (emacs-version) "\" "
  275. ;; "-text b \"Thumb::Image::Height\" \"%oh\" "
  276. ;; "-text b \"Thumb::Image::Mimetype\" \"%mime\" "
  277. ;; "-text b \"Thumb::Image::Width\" \"%ow\" "
  278. "-text b \"Thumb::MTime\" \"%m\" "
  279. ;; "-text b \"Thumb::Size\" \"%b\" "
  280. "-text b \"Thumb::URI\" \"file://%f\" "
  281. "%q %t"
  282. " ; rm %q")))
  283. "Command to create thumbnails according to the Thumbnail Managing Standard."
  284. :type 'string
  285. :group 'image-dired)
  286. (defcustom image-dired-cmd-rotate-thumbnail-program
  287. "mogrify"
  288. "Executable used to rotate thumbnail.
  289. Used together with `image-dired-cmd-rotate-thumbnail-options'."
  290. :type 'string
  291. :group 'image-dired)
  292. (defcustom image-dired-cmd-rotate-thumbnail-options
  293. "%p -rotate %d \"%t\""
  294. "Format of command used to rotate thumbnail image.
  295. Available options are %p which is replaced by
  296. `image-dired-cmd-rotate-thumbnail-program', %d which is replaced by the
  297. number of (positive) degrees to rotate the image, normally 90 or 270
  298. \(for 90 degrees right and left), %t which is replaced by the file name
  299. of the thumbnail file."
  300. :type 'string
  301. :group 'image-dired)
  302. (defcustom image-dired-cmd-rotate-original-program
  303. "jpegtran"
  304. "Executable used to rotate original image.
  305. Used together with `image-dired-cmd-rotate-original-options'."
  306. :type 'string
  307. :group 'image-dired)
  308. (defcustom image-dired-cmd-rotate-original-options
  309. "%p -rotate %d -copy all -outfile %t \"%o\""
  310. "Format of command used to rotate original image.
  311. Available options are %p which is replaced by
  312. `image-dired-cmd-rotate-original-program', %d which is replaced by the
  313. number of (positive) degrees to rotate the image, normally 90 or
  314. 270 \(for 90 degrees right and left), %o which is replaced by the
  315. original image file name and %t which is replaced by
  316. `image-dired-temp-image-file'."
  317. :type 'string
  318. :group 'image-dired)
  319. (defcustom image-dired-temp-rotate-image-file
  320. (expand-file-name ".image-dired_rotate_temp" image-dired-dir)
  321. "Temporary file for rotate operations."
  322. :type 'string
  323. :group 'image-dired)
  324. (defcustom image-dired-rotate-original-ask-before-overwrite t
  325. "Confirm overwrite of original file after rotate operation.
  326. If non-nil, ask user for confirmation before overwriting the
  327. original file with `image-dired-temp-rotate-image-file'."
  328. :type 'boolean
  329. :group 'image-dired)
  330. (defcustom image-dired-cmd-write-exif-data-program
  331. "exiftool"
  332. "Program used to write EXIF data to image.
  333. Used together with `image-dired-cmd-write-exif-data-options'."
  334. :type 'string
  335. :group 'image-dired)
  336. (defcustom image-dired-cmd-write-exif-data-options
  337. "%p -%t=\"%v\" \"%f\""
  338. "Format of command used to write EXIF data.
  339. Available options are %p which is replaced by
  340. `image-dired-cmd-write-exif-data-program', %f which is replaced by
  341. the image file name, %t which is replaced by the tag name and %v
  342. which is replaced by the tag value."
  343. :type 'string
  344. :group 'image-dired)
  345. (defcustom image-dired-cmd-read-exif-data-program
  346. "exiftool"
  347. "Program used to read EXIF data to image.
  348. Used together with `image-dired-cmd-read-exif-data-program-options'."
  349. :type 'string
  350. :group 'image-dired)
  351. (defcustom image-dired-cmd-read-exif-data-options
  352. "%p -s -s -s -%t \"%f\""
  353. "Format of command used to read EXIF data.
  354. Available options are %p which is replaced by
  355. `image-dired-cmd-write-exif-data-program', %f which is replaced
  356. by the image file name and %t which is replaced by the tag name."
  357. :type 'string
  358. :group 'image-dired)
  359. (defcustom image-dired-gallery-hidden-tags
  360. (list "private" "hidden" "pending")
  361. "List of \"hidden\" tags.
  362. Used by `image-dired-gallery-generate' to leave out \"hidden\" images."
  363. :type '(repeat string)
  364. :group 'image-dired)
  365. (defcustom image-dired-thumb-size (if (eq 'standard image-dired-thumbnail-storage) 128 100)
  366. "Size of thumbnails, in pixels.
  367. This is the default size for both `image-dired-thumb-width'
  368. and `image-dired-thumb-height'."
  369. :type 'integer
  370. :group 'image-dired)
  371. (defcustom image-dired-thumb-width image-dired-thumb-size
  372. "Width of thumbnails, in pixels."
  373. :type 'integer
  374. :group 'image-dired)
  375. (defcustom image-dired-thumb-height image-dired-thumb-size
  376. "Height of thumbnails, in pixels."
  377. :type 'integer
  378. :group 'image-dired)
  379. (defcustom image-dired-thumb-relief 2
  380. "Size of button-like border around thumbnails."
  381. :type 'integer
  382. :group 'image-dired)
  383. (defcustom image-dired-thumb-margin 2
  384. "Size of the margin around thumbnails.
  385. This is where you see the cursor."
  386. :type 'integer
  387. :group 'image-dired)
  388. (defcustom image-dired-line-up-method 'dynamic
  389. "Default method for line-up of thumbnails in thumbnail buffer.
  390. Used by `image-dired-display-thumbs' and other functions that needs
  391. to line-up thumbnails. Dynamic means to use the available width of
  392. the window containing the thumbnail buffer, Fixed means to use
  393. `image-dired-thumbs-per-row', Interactive is for asking the user,
  394. and No line-up means that no automatic line-up will be done."
  395. :type '(choice :tag "Default line-up method"
  396. (const :tag "Dynamic" dynamic)
  397. (const :tag "Fixed" fixed)
  398. (const :tag "Interactive" interactive)
  399. (const :tag "No line-up" none))
  400. :group 'image-dired)
  401. (defcustom image-dired-thumbs-per-row 3
  402. "Number of thumbnails to display per row in thumb buffer."
  403. :type 'integer
  404. :group 'image-dired)
  405. (defcustom image-dired-display-window-width-correction 1
  406. "Number to be used to correct image display window width.
  407. Change if the default (1) does not work (i.e. if the image does not
  408. completely fit)."
  409. :type 'integer
  410. :group 'image-dired)
  411. (defcustom image-dired-display-window-height-correction 0
  412. "Number to be used to correct image display window height.
  413. Change if the default (0) does not work (i.e. if the image does not
  414. completely fit)."
  415. :type 'integer
  416. :group 'image-dired)
  417. (defcustom image-dired-track-movement t
  418. "The current state of the tracking and mirroring.
  419. For more information, see the documentation for
  420. `image-dired-toggle-movement-tracking'."
  421. :type 'boolean
  422. :group 'image-dired)
  423. (defcustom image-dired-append-when-browsing nil
  424. "Append thumbnails in thumbnail buffer when browsing.
  425. If non-nil, using `image-dired-next-line-and-display' and
  426. `image-dired-previous-line-and-display' will leave a trail of thumbnail
  427. images in the thumbnail buffer. If you enable this and want to clean
  428. the thumbnail buffer because it is filled with too many thumbnails,
  429. just call `image-dired-display-thumb' to display only the image at point.
  430. This value can be toggled using `image-dired-toggle-append-browsing'."
  431. :type 'boolean
  432. :group 'image-dired)
  433. (defcustom image-dired-dired-disp-props t
  434. "If non-nil, display properties for dired file when browsing.
  435. Used by `image-dired-next-line-and-display',
  436. `image-dired-previous-line-and-display' and `image-dired-mark-and-display-next'.
  437. If the database file is large, this can slow down image browsing in
  438. dired and you might want to turn it off."
  439. :type 'boolean
  440. :group 'image-dired)
  441. (defcustom image-dired-display-properties-format "%b: %f (%t): %c"
  442. "Display format for thumbnail properties.
  443. %b is replaced with associated dired buffer name, %f with file name
  444. \(without path) of original image file, %t with the list of tags and %c
  445. with the comment."
  446. :type 'string
  447. :group 'image-dired)
  448. (defcustom image-dired-external-viewer
  449. ;; TODO: Use mailcap, dired-guess-shell-alist-default,
  450. ;; dired-view-command-alist.
  451. (cond ((executable-find "display"))
  452. ((executable-find "xli"))
  453. ((executable-find "qiv") "qiv -t"))
  454. "Name of external viewer.
  455. Including parameters. Used when displaying original image from
  456. `image-dired-thumbnail-mode'."
  457. :type 'string
  458. :group 'image-dired)
  459. (defcustom image-dired-main-image-directory "~/pics/"
  460. "Name of main image directory, if any.
  461. Used by `image-dired-copy-with-exif-file-name'."
  462. :type 'string
  463. :group 'image-dired)
  464. (defcustom image-dired-show-all-from-dir-max-files 50
  465. "Maximum number of files to show using `image-dired-show-all-from-dir'
  466. before warning the user."
  467. :type 'integer
  468. :group 'image-dired)
  469. (defmacro image-dired--with-db-file (&rest body)
  470. "Run BODY in a temp buffer containing `image-dired-db-file'.
  471. Return the last form in BODY."
  472. `(with-temp-buffer
  473. (if (file-exists-p image-dired-db-file)
  474. (insert-file-contents image-dired-db-file))
  475. ,@body))
  476. (defun image-dired-dir ()
  477. "Return the current thumbnails directory (from variable `image-dired-dir').
  478. Create the thumbnails directory if it does not exist."
  479. (let ((image-dired-dir (file-name-as-directory
  480. (expand-file-name image-dired-dir))))
  481. (unless (file-directory-p image-dired-dir)
  482. (make-directory image-dired-dir t)
  483. (message "Creating thumbnails directory"))
  484. image-dired-dir))
  485. (defun image-dired-insert-image (file type relief margin)
  486. "Insert image FILE of image TYPE, using RELIEF and MARGIN, at point."
  487. (let ((i `(image :type ,type
  488. :file ,file
  489. :relief ,relief
  490. :margin ,margin)))
  491. (insert-image i)))
  492. (defun image-dired-get-thumbnail-image (file)
  493. "Return the image descriptor for a thumbnail of image file FILE."
  494. (unless (string-match (image-file-name-regexp) file)
  495. (error "%s is not a valid image file" file))
  496. (let ((thumb-file (image-dired-thumb-name file)))
  497. (unless (and (file-exists-p thumb-file)
  498. (<= (float-time (nth 5 (file-attributes file)))
  499. (float-time (nth 5 (file-attributes thumb-file)))))
  500. (image-dired-create-thumb file thumb-file))
  501. (create-image thumb-file)
  502. ;; (list 'image :type 'jpeg
  503. ;; :file thumb-file
  504. ;; :relief image-dired-thumb-relief :margin image-dired-thumb-margin)
  505. ))
  506. (defun image-dired-insert-thumbnail (file original-file-name
  507. associated-dired-buffer)
  508. "Insert thumbnail image FILE.
  509. Add text properties ORIGINAL-FILE-NAME and ASSOCIATED-DIRED-BUFFER."
  510. (let (beg end)
  511. (setq beg (point))
  512. (image-dired-insert-image file
  513. ;; TODO: this should depend on the real file type
  514. (if (eq 'standard image-dired-thumbnail-storage)
  515. 'png 'jpeg)
  516. image-dired-thumb-relief
  517. image-dired-thumb-margin)
  518. (setq end (point))
  519. (add-text-properties
  520. beg end
  521. (list 'image-dired-thumbnail t
  522. 'original-file-name original-file-name
  523. 'associated-dired-buffer associated-dired-buffer
  524. 'tags (image-dired-list-tags original-file-name)
  525. 'mouse-face 'highlight
  526. 'comment (image-dired-get-comment original-file-name)))))
  527. (defun image-dired-thumb-name (file)
  528. "Return thumbnail file name for FILE.
  529. Depending on the value of `image-dired-thumbnail-storage', the file
  530. name will vary. For central thumbnail file storage, make a
  531. MD5-hash of the image file's directory name and add that to make
  532. the thumbnail file name unique. For per-directory storage, just
  533. add a subdirectory. For standard storage, produce the file name
  534. according to the Thumbnail Managing Standard."
  535. (cond ((eq 'standard image-dired-thumbnail-storage)
  536. (expand-file-name
  537. (concat "~/.thumbnails/normal/"
  538. (md5 (concat "file://" (expand-file-name file))) ".png")))
  539. ((eq 'use-image-dired-dir image-dired-thumbnail-storage)
  540. (let* ((f (expand-file-name file))
  541. (md5-hash
  542. ;; Is MD5 hashes fast enough? The checksum of a
  543. ;; thumbnail file name need not be that
  544. ;; "cryptographically" good so a faster one could
  545. ;; be used here.
  546. (md5 (file-name-as-directory (file-name-directory f)))))
  547. (format "%s%s%s.thumb.%s"
  548. (file-name-as-directory (expand-file-name (image-dired-dir)))
  549. (file-name-sans-extension (file-name-nondirectory f))
  550. (if md5-hash (concat "_" md5-hash) "")
  551. (file-name-extension f))))
  552. ((eq 'per-directory image-dired-thumbnail-storage)
  553. (let ((f (expand-file-name file)))
  554. (format "%s.image-dired/%s.thumb.%s"
  555. (file-name-directory f)
  556. (file-name-sans-extension (file-name-nondirectory f))
  557. (file-name-extension f))))))
  558. (defun image-dired-create-thumb (original-file thumbnail-file)
  559. "For ORIGINAL-FILE, create thumbnail image named THUMBNAIL-FILE."
  560. (let* ((width (int-to-string image-dired-thumb-width))
  561. (height (int-to-string image-dired-thumb-height))
  562. (modif-time (format "%.0f" (float-time (nth 5 (file-attributes
  563. original-file)))))
  564. (thumbnail-nq8-file (replace-regexp-in-string ".png\\'" "-nq8.png"
  565. thumbnail-file))
  566. (command
  567. (format-spec
  568. (if (eq 'standard image-dired-thumbnail-storage)
  569. image-dired-cmd-create-standard-thumbnail-command
  570. image-dired-cmd-create-thumbnail-options)
  571. (list
  572. (cons ?p image-dired-cmd-create-thumbnail-program)
  573. (cons ?w width)
  574. (cons ?h height)
  575. (cons ?m modif-time)
  576. (cons ?f original-file)
  577. (cons ?q thumbnail-nq8-file)
  578. (cons ?t thumbnail-file))))
  579. thumbnail-dir)
  580. (when (not (file-exists-p
  581. (setq thumbnail-dir (file-name-directory thumbnail-file))))
  582. (message "Creating thumbnail directory.")
  583. (make-directory thumbnail-dir))
  584. (call-process shell-file-name nil nil nil shell-command-switch command)))
  585. ;;;###autoload
  586. (defun image-dired-dired-toggle-marked-thumbs (&optional arg)
  587. "Toggle thumbnails in front of file names in the dired buffer.
  588. If no marked file could be found, insert or hide thumbnails on the
  589. current line. ARG, if non-nil, specifies the files to use instead
  590. of the marked files. If ARG is an integer, use the next ARG (or
  591. previous -ARG, if ARG<0) files."
  592. (interactive "P")
  593. (dired-map-over-marks
  594. (let* ((image-pos (dired-move-to-filename))
  595. (image-file (dired-get-filename nil t))
  596. thumb-file
  597. overlay)
  598. (when (and image-file (string-match-p (image-file-name-regexp) image-file))
  599. (setq thumb-file (image-dired-get-thumbnail-image image-file))
  600. ;; If image is not already added, then add it.
  601. (let ((cur-ov (overlays-in (point) (1+ (point)))))
  602. (if cur-ov
  603. (delete-overlay (car cur-ov))
  604. (put-image thumb-file image-pos)
  605. (setq overlay (loop for o in (overlays-in (point) (1+ (point)))
  606. when (overlay-get o 'put-image) collect o into ov
  607. finally return (car ov)))
  608. (overlay-put overlay 'image-file image-file)
  609. (overlay-put overlay 'thumb-file thumb-file)))))
  610. arg ; Show or hide image on ARG next files.
  611. 'show-progress) ; Update dired display after each image is updated.
  612. (add-hook 'dired-after-readin-hook 'image-dired-dired-after-readin-hook nil t))
  613. (defun image-dired-dired-after-readin-hook ()
  614. "Relocate existing thumbnail overlays in dired buffer after reverting.
  615. Move them to their corresponding files if they still exist.
  616. Otherwise, delete overlays."
  617. (mapc (lambda (overlay)
  618. (when (overlay-get overlay 'put-image)
  619. (let* ((image-file (overlay-get overlay 'image-file))
  620. (image-pos (dired-goto-file image-file)))
  621. (if image-pos
  622. (move-overlay overlay image-pos image-pos)
  623. (delete-overlay overlay)))))
  624. (overlays-in (point-min) (point-max))))
  625. (defun image-dired-next-line-and-display ()
  626. "Move to next dired line and display thumbnail image."
  627. (interactive)
  628. (dired-next-line 1)
  629. (image-dired-display-thumbs
  630. t (or image-dired-append-when-browsing nil) t)
  631. (if image-dired-dired-disp-props
  632. (image-dired-dired-display-properties)))
  633. (defun image-dired-previous-line-and-display ()
  634. "Move to previous dired line and display thumbnail image."
  635. (interactive)
  636. (dired-previous-line 1)
  637. (image-dired-display-thumbs
  638. t (or image-dired-append-when-browsing nil) t)
  639. (if image-dired-dired-disp-props
  640. (image-dired-dired-display-properties)))
  641. (defun image-dired-toggle-append-browsing ()
  642. "Toggle `image-dired-append-when-browsing'."
  643. (interactive)
  644. (setq image-dired-append-when-browsing
  645. (not image-dired-append-when-browsing))
  646. (message "Append browsing %s."
  647. (if image-dired-append-when-browsing
  648. "on"
  649. "off")))
  650. (defun image-dired-mark-and-display-next ()
  651. "Mark current file in dired and display next thumbnail image."
  652. (interactive)
  653. (dired-mark 1)
  654. (image-dired-display-thumbs
  655. t (or image-dired-append-when-browsing nil) t)
  656. (if image-dired-dired-disp-props
  657. (image-dired-dired-display-properties)))
  658. (defun image-dired-toggle-dired-display-properties ()
  659. "Toggle `image-dired-dired-disp-props'."
  660. (interactive)
  661. (setq image-dired-dired-disp-props
  662. (not image-dired-dired-disp-props))
  663. (message "Dired display properties %s."
  664. (if image-dired-dired-disp-props
  665. "on"
  666. "off")))
  667. (defvar image-dired-thumbnail-buffer "*image-dired*"
  668. "Image-Dired's thumbnail buffer.")
  669. (defun image-dired-create-thumbnail-buffer ()
  670. "Create thumb buffer and set `image-dired-thumbnail-mode'."
  671. (let ((buf (get-buffer-create image-dired-thumbnail-buffer)))
  672. (with-current-buffer buf
  673. (setq buffer-read-only t)
  674. (if (not (eq major-mode 'image-dired-thumbnail-mode))
  675. (image-dired-thumbnail-mode)))
  676. buf))
  677. (defvar image-dired-display-image-buffer "*image-dired-display-image*"
  678. "Where larger versions of the images are display.")
  679. (defun image-dired-create-display-image-buffer ()
  680. "Create image display buffer and set `image-dired-display-image-mode'."
  681. (let ((buf (get-buffer-create image-dired-display-image-buffer)))
  682. (with-current-buffer buf
  683. (setq buffer-read-only t)
  684. (if (not (eq major-mode 'image-dired-display-image-mode))
  685. (image-dired-display-image-mode)))
  686. buf))
  687. (defvar image-dired-saved-window-configuration nil
  688. "Saved window configuration.")
  689. ;;;###autoload
  690. (defun image-dired-dired-with-window-configuration (dir &optional arg)
  691. "Open directory DIR and create a default window configuration.
  692. Convenience command that:
  693. - Opens dired in folder DIR
  694. - Splits windows in most useful (?) way
  695. - Set `truncate-lines' to t
  696. After the command has finished, you would typically mark some
  697. image files in dired and type
  698. \\[image-dired-display-thumbs] (`image-dired-display-thumbs').
  699. If called with prefix argument ARG, skip splitting of windows.
  700. The current window configuration is saved and can be restored by
  701. calling `image-dired-restore-window-configuration'."
  702. (interactive "DDirectory: \nP")
  703. (let ((buf (image-dired-create-thumbnail-buffer))
  704. (buf2 (image-dired-create-display-image-buffer)))
  705. (setq image-dired-saved-window-configuration
  706. (current-window-configuration))
  707. (dired dir)
  708. (delete-other-windows)
  709. (when (not arg)
  710. (split-window-right)
  711. (setq truncate-lines t)
  712. (save-excursion
  713. (other-window 1)
  714. (switch-to-buffer buf)
  715. (select-window (split-window-below))
  716. (switch-to-buffer buf2)
  717. (other-window -2)))))
  718. (defun image-dired-restore-window-configuration ()
  719. "Restore window configuration.
  720. Restore any changes to the window configuration made by calling
  721. `image-dired-dired-with-window-configuration'."
  722. (interactive)
  723. (if image-dired-saved-window-configuration
  724. (set-window-configuration image-dired-saved-window-configuration)
  725. (message "No saved window configuration")))
  726. ;;;###autoload
  727. (defun image-dired-display-thumbs (&optional arg append do-not-pop)
  728. "Display thumbnails of all marked files, in `image-dired-thumbnail-buffer'.
  729. If a thumbnail image does not exist for a file, it is created on the
  730. fly. With prefix argument ARG, display only thumbnail for file at
  731. point (this is useful if you have marked some files but want to show
  732. another one).
  733. Recommended usage is to split the current frame horizontally so that
  734. you have the dired buffer in the left window and the
  735. `image-dired-thumbnail-buffer' buffer in the right window.
  736. With optional argument APPEND, append thumbnail to thumbnail buffer
  737. instead of erasing it first.
  738. Optional argument DO-NOT-POP controls if `pop-to-buffer' should be
  739. used or not. If non-nil, use `display-buffer' instead of
  740. `pop-to-buffer'. This is used from functions like
  741. `image-dired-next-line-and-display' and
  742. `image-dired-previous-line-and-display' where we do not want the
  743. thumbnail buffer to be selected."
  744. (interactive "P")
  745. (let ((buf (image-dired-create-thumbnail-buffer))
  746. thumb-name files dired-buf)
  747. (if arg
  748. (setq files (list (dired-get-filename)))
  749. (setq files (dired-get-marked-files)))
  750. (setq dired-buf (current-buffer))
  751. (with-current-buffer buf
  752. (let ((inhibit-read-only t))
  753. (if (not append)
  754. (erase-buffer)
  755. (goto-char (point-max)))
  756. (mapc
  757. (lambda (curr-file)
  758. (setq thumb-name (image-dired-thumb-name curr-file))
  759. (if (and (not (file-exists-p thumb-name))
  760. (not (= 0 (image-dired-create-thumb curr-file thumb-name))))
  761. (message "Thumb could not be created for file %s" curr-file)
  762. (image-dired-insert-thumbnail thumb-name curr-file dired-buf)))
  763. files))
  764. (cond ((eq 'dynamic image-dired-line-up-method)
  765. (image-dired-line-up-dynamic))
  766. ((eq 'fixed image-dired-line-up-method)
  767. (image-dired-line-up))
  768. ((eq 'interactive image-dired-line-up-method)
  769. (image-dired-line-up-interactive))
  770. ((eq 'none image-dired-line-up-method)
  771. nil)
  772. (t
  773. (image-dired-line-up-dynamic))))
  774. (if do-not-pop
  775. (display-buffer image-dired-thumbnail-buffer)
  776. (pop-to-buffer image-dired-thumbnail-buffer))))
  777. ;;;###autoload
  778. (defun image-dired-show-all-from-dir (dir)
  779. "Make a preview buffer for all images in DIR and display it.
  780. If the number of files in DIR matching `image-file-name-regexp'
  781. exceeds `image-dired-show-all-from-dir-max-files', a warning will be
  782. displayed."
  783. (interactive "DDir: ")
  784. (dired dir)
  785. (dired-mark-files-regexp (image-file-name-regexp))
  786. (let ((files (dired-get-marked-files)))
  787. (if (or (<= (length files) image-dired-show-all-from-dir-max-files)
  788. (and (> (length files) image-dired-show-all-from-dir-max-files)
  789. (y-or-n-p
  790. (format
  791. "Directory contains more than %d image files. Proceed? "
  792. image-dired-show-all-from-dir-max-files))))
  793. (progn
  794. (image-dired-display-thumbs)
  795. (pop-to-buffer image-dired-thumbnail-buffer))
  796. (message "Cancelled."))))
  797. ;;;###autoload
  798. (defalias 'image-dired 'image-dired-show-all-from-dir)
  799. ;;;###autoload
  800. (defalias 'tumme 'image-dired-show-all-from-dir)
  801. (defun image-dired-sane-db-file ()
  802. "Check if `image-dired-db-file' exists.
  803. If not, try to create it (including any parent directories).
  804. Signal error if there are problems creating it."
  805. (or (file-exists-p image-dired-db-file)
  806. (let (dir buf)
  807. (unless (file-directory-p (setq dir (file-name-directory
  808. image-dired-db-file)))
  809. (make-directory dir t))
  810. (with-current-buffer (setq buf (create-file-buffer
  811. image-dired-db-file))
  812. (write-file image-dired-db-file))
  813. (kill-buffer buf)
  814. (file-exists-p image-dired-db-file))
  815. (error "Could not create %s" image-dired-db-file)))
  816. (defun image-dired-write-tags (file-tags)
  817. "Write file tags to database.
  818. Write each file and tag in FILE-TAGS to the database.
  819. FILE-TAGS is an alist in the following form:
  820. ((FILE . TAG) ... )"
  821. (image-dired-sane-db-file)
  822. (let (end file tag)
  823. (image-dired--with-db-file
  824. (setq buffer-file-name image-dired-db-file)
  825. (dolist (elt file-tags)
  826. (setq file (car elt)
  827. tag (cdr elt))
  828. (goto-char (point-min))
  829. (if (search-forward-regexp (format "^%s.*$" file) nil t)
  830. (progn
  831. (setq end (point))
  832. (beginning-of-line)
  833. (when (not (search-forward (format ";%s" tag) end t))
  834. (end-of-line)
  835. (insert (format ";%s" tag))))
  836. (goto-char (point-max))
  837. (insert (format "\n%s;%s" file tag))))
  838. (save-buffer))))
  839. (defun image-dired-remove-tag (files tag)
  840. "For all FILES, remove TAG from the image database."
  841. (image-dired-sane-db-file)
  842. (image-dired--with-db-file
  843. (setq buffer-file-name image-dired-db-file)
  844. (let (end)
  845. (unless (listp files)
  846. (if (stringp files)
  847. (setq files (list files))
  848. (error "Files must be a string or a list of strings!")))
  849. (dolist (file files)
  850. (goto-char (point-min))
  851. (when (search-forward-regexp (format "^%s" file) nil t)
  852. (end-of-line)
  853. (setq end (point))
  854. (beginning-of-line)
  855. (when (search-forward-regexp (format "\\(;%s\\)" tag) end t)
  856. (delete-region (match-beginning 1) (match-end 1))
  857. ;; Check if file should still be in the database. If
  858. ;; it has no tags or comments, it will be removed.
  859. (end-of-line)
  860. (setq end (point))
  861. (beginning-of-line)
  862. (when (not (search-forward ";" end t))
  863. (kill-line 1)
  864. ;; If on empty line at end of buffer
  865. (and (eobp)
  866. (looking-at "^$")
  867. (delete-char -1)))))))
  868. (save-buffer)))
  869. (defun image-dired-list-tags (file)
  870. "Read all tags for image FILE from the image database."
  871. (image-dired-sane-db-file)
  872. (image-dired--with-db-file
  873. (let (end (tags ""))
  874. (when (search-forward-regexp (format "^%s" file) nil t)
  875. (end-of-line)
  876. (setq end (point))
  877. (beginning-of-line)
  878. (if (search-forward ";" end t)
  879. (if (search-forward "comment:" end t)
  880. (if (search-forward ";" end t)
  881. (setq tags (buffer-substring (point) end)))
  882. (setq tags (buffer-substring (point) end)))))
  883. (split-string tags ";"))))
  884. ;;;###autoload
  885. (defun image-dired-tag-files (arg)
  886. "Tag marked file(s) in dired. With prefix ARG, tag file at point."
  887. (interactive "P")
  888. (let ((tag (read-string "Tags to add (separate tags with a semicolon): "))
  889. files)
  890. (if arg
  891. (setq files (list (dired-get-filename)))
  892. (setq files (dired-get-marked-files)))
  893. (image-dired-write-tags
  894. (mapcar
  895. (lambda (x)
  896. (cons x tag))
  897. files))))
  898. (defun image-dired-tag-thumbnail ()
  899. "Tag current thumbnail."
  900. (interactive)
  901. (let ((tag (read-string "Tags to add (separate tags with a semicolon): ")))
  902. (image-dired-write-tags (list (cons (image-dired-original-file-name) tag))))
  903. (image-dired-update-property
  904. 'tags (image-dired-list-tags (image-dired-original-file-name))))
  905. ;;;###autoload
  906. (defun image-dired-delete-tag (arg)
  907. "Remove tag for selected file(s).
  908. With prefix argument ARG, remove tag from file at point."
  909. (interactive "P")
  910. (let ((tag (read-string "Tag to remove: "))
  911. files)
  912. (if arg
  913. (setq files (list (dired-get-filename)))
  914. (setq files (dired-get-marked-files)))
  915. (image-dired-remove-tag files tag)))
  916. (defun image-dired-tag-thumbnail-remove ()
  917. "Remove tag from thumbnail."
  918. (interactive)
  919. (let ((tag (read-string "Tag to remove: ")))
  920. (image-dired-remove-tag (image-dired-original-file-name) tag))
  921. (image-dired-update-property
  922. 'tags (image-dired-list-tags (image-dired-original-file-name))))
  923. (defun image-dired-original-file-name ()
  924. "Get original file name for thumbnail or display image at point."
  925. (get-text-property (point) 'original-file-name))
  926. (defun image-dired-associated-dired-buffer ()
  927. "Get associated dired buffer at point."
  928. (get-text-property (point) 'associated-dired-buffer))
  929. (defun image-dired-get-buffer-window (buf)
  930. "Return window where buffer BUF is."
  931. (get-window-with-predicate
  932. (lambda (window)
  933. (equal (window-buffer window) buf))
  934. nil t))
  935. (defun image-dired-track-original-file ()
  936. "Track the original file in the associated dired buffer.
  937. See documentation for `image-dired-toggle-movement-tracking'.
  938. Interactive use only useful if `image-dired-track-movement' is nil."
  939. (interactive)
  940. (let ((old-buf (current-buffer))
  941. (dired-buf (image-dired-associated-dired-buffer))
  942. (file-name (image-dired-original-file-name)))
  943. (when (and (buffer-live-p dired-buf) file-name)
  944. (set-buffer dired-buf)
  945. (if (not (dired-goto-file file-name))
  946. (message "Could not track file")
  947. (set-window-point
  948. (image-dired-get-buffer-window dired-buf) (point)))
  949. (set-buffer old-buf))))
  950. (defun image-dired-toggle-movement-tracking ()
  951. "Turn on and off `image-dired-track-movement'.
  952. Tracking of the movements between thumbnail and dired buffer so that
  953. they are \"mirrored\" in the dired buffer. When this is on, moving
  954. around in the thumbnail or dired buffer will find the matching
  955. position in the other buffer."
  956. (interactive)
  957. (setq image-dired-track-movement (not image-dired-track-movement))
  958. (message "Tracking %s" (if image-dired-track-movement "on" "off")))
  959. (defun image-dired-track-thumbnail ()
  960. "Track current dired file's thumb in `image-dired-thumbnail-buffer'.
  961. This is almost the same as what `image-dired-track-original-file' does,
  962. but the other way around."
  963. (let ((file (dired-get-filename))
  964. (old-buf (current-buffer))
  965. prop-val found)
  966. (when (get-buffer image-dired-thumbnail-buffer)
  967. (set-buffer image-dired-thumbnail-buffer)
  968. (goto-char (point-min))
  969. (while (and (not (eobp))
  970. (not found))
  971. (if (and (setq prop-val
  972. (get-text-property (point) 'original-file-name))
  973. (string= prop-val file))
  974. (setq found t))
  975. (if (not found)
  976. (forward-char 1)))
  977. (when found
  978. (set-window-point
  979. (image-dired-thumbnail-window) (point))
  980. (image-dired-display-thumb-properties))
  981. (set-buffer old-buf))))
  982. (defun image-dired-dired-next-line (&optional arg)
  983. "Call `dired-next-line', then track thumbnail.
  984. This can safely replace `dired-next-line'.
  985. With prefix argument, move ARG lines."
  986. (interactive "P")
  987. (dired-next-line (or arg 1))
  988. (if image-dired-track-movement
  989. (image-dired-track-thumbnail)))
  990. (defun image-dired-dired-previous-line (&optional arg)
  991. "Call `dired-previous-line', then track thumbnail.
  992. This can safely replace `dired-previous-line'.
  993. With prefix argument, move ARG lines."
  994. (interactive "P")
  995. (dired-previous-line (or arg 1))
  996. (if image-dired-track-movement
  997. (image-dired-track-thumbnail)))
  998. (defun image-dired-forward-image (&optional arg)
  999. "Move to next image and display properties.
  1000. Optional prefix ARG says how many images to move; default is one
  1001. image."
  1002. (interactive "p")
  1003. (let (pos (steps (or arg 1)))
  1004. (dotimes (i steps)
  1005. (if (and (not (eobp))
  1006. (save-excursion
  1007. (forward-char)
  1008. (while (and (not (eobp))
  1009. (not (image-dired-image-at-point-p)))
  1010. (forward-char))
  1011. (setq pos (point))
  1012. (image-dired-image-at-point-p)))
  1013. (goto-char pos)
  1014. (error "At last image"))))
  1015. (when image-dired-track-movement
  1016. (image-dired-track-original-file))
  1017. (image-dired-display-thumb-properties))
  1018. (defun image-dired-backward-image (&optional arg)
  1019. "Move to previous image and display properties.
  1020. Optional prefix ARG says how many images to move; default is one
  1021. image."
  1022. (interactive "p")
  1023. (let (pos (steps (or arg 1)))
  1024. (dotimes (i steps)
  1025. (if (and (not (bobp))
  1026. (save-excursion
  1027. (backward-char)
  1028. (while (and (not (bobp))
  1029. (not (image-dired-image-at-point-p)))
  1030. (backward-char))
  1031. (setq pos (point))
  1032. (image-dired-image-at-point-p)))
  1033. (goto-char pos)
  1034. (error "At first image"))))
  1035. (when image-dired-track-movement
  1036. (image-dired-track-original-file))
  1037. (image-dired-display-thumb-properties))
  1038. (defun image-dired-next-line ()
  1039. "Move to next line and display properties."
  1040. (interactive)
  1041. (forward-line 1)
  1042. ;; If we end up in an empty spot, back up to the next thumbnail.
  1043. (if (not (image-dired-image-at-point-p))
  1044. (image-dired-backward-image))
  1045. (if image-dired-track-movement
  1046. (image-dired-track-original-file))
  1047. (image-dired-display-thumb-properties))
  1048. (defun image-dired-previous-line ()
  1049. "Move to previous line and display properties."
  1050. (interactive)
  1051. (forward-line -1)
  1052. ;; If we end up in an empty spot, back up to the next
  1053. ;; thumbnail. This should only happen if the user deleted a
  1054. ;; thumbnail and did not refresh, so it is not very common. But we
  1055. ;; can handle it in a good manner, so why not?
  1056. (if (not (image-dired-image-at-point-p))
  1057. (image-dired-backward-image))
  1058. (if image-dired-track-movement
  1059. (image-dired-track-original-file))
  1060. (image-dired-display-thumb-properties))
  1061. (defun image-dired-format-properties-string (buf file props comment)
  1062. "Format display properties.
  1063. BUF is the associated dired buffer, FILE is the original image file
  1064. name, PROPS is a list of tags and COMMENT is the image file's
  1065. comment."
  1066. (format-spec
  1067. image-dired-display-properties-format
  1068. (list
  1069. (cons ?b (or buf ""))
  1070. (cons ?f file)
  1071. (cons ?t (or (princ props) ""))
  1072. (cons ?c (or comment "")))))
  1073. (defun image-dired-display-thumb-properties ()
  1074. "Display thumbnail properties in the echo area."
  1075. (if (not (eobp))
  1076. (let ((file-name (file-name-nondirectory (image-dired-original-file-name)))
  1077. (dired-buf (buffer-name (image-dired-associated-dired-buffer)))
  1078. (props (mapconcat
  1079. 'princ
  1080. (get-text-property (point) 'tags)
  1081. ", "))
  1082. (comment (get-text-property (point) 'comment)))
  1083. (if file-name
  1084. (message "%s"
  1085. (image-dired-format-properties-string
  1086. dired-buf
  1087. file-name
  1088. props
  1089. comment))))))
  1090. (defun image-dired-dired-file-marked-p ()
  1091. "Check whether file on current line is marked or not."
  1092. (save-excursion
  1093. (beginning-of-line)
  1094. (not (looking-at "^ .*$"))))
  1095. (defun image-dired-modify-mark-on-thumb-original-file (command)
  1096. "Modify mark in dired buffer.
  1097. COMMAND is one of 'mark for marking file in dired, 'unmark for
  1098. unmarking file in dired or 'flag for flagging file for delete in
  1099. dired."
  1100. (let ((file-name (image-dired-original-file-name))
  1101. (dired-buf (image-dired-associated-dired-buffer)))
  1102. (if (not (and dired-buf file-name))
  1103. (message "No image, or image with correct properties, at point.")
  1104. (with-current-buffer dired-buf
  1105. (message "%s" file-name)
  1106. (if (dired-goto-file file-name)
  1107. (cond ((eq command 'mark) (dired-mark 1))
  1108. ((eq command 'unmark) (dired-unmark 1))
  1109. ((eq command 'toggle)
  1110. (if (image-dired-dired-file-marked-p)
  1111. (dired-unmark 1)
  1112. (dired-mark 1)))
  1113. ((eq command 'flag) (dired-flag-file-deletion 1))))))))
  1114. (defun image-dired-mark-thumb-original-file ()
  1115. "Mark original image file in associated dired buffer."
  1116. (interactive)
  1117. (image-dired-modify-mark-on-thumb-original-file 'mark)
  1118. (image-dired-forward-image))
  1119. (defun image-dired-unmark-thumb-original-file ()
  1120. "Unmark original image file in associated dired buffer."
  1121. (interactive)
  1122. (image-dired-modify-mark-on-thumb-original-file 'unmark)
  1123. (image-dired-forward-image))
  1124. (defun image-dired-flag-thumb-original-file ()
  1125. "Flag original image file for deletion in associated dired buffer."
  1126. (interactive)
  1127. (image-dired-modify-mark-on-thumb-original-file 'flag)
  1128. (image-dired-forward-image))
  1129. (defun image-dired-toggle-mark-thumb-original-file ()
  1130. "Toggle mark on original image file in associated dired buffer."
  1131. (interactive)
  1132. (image-dired-modify-mark-on-thumb-original-file 'toggle))
  1133. (defun image-dired-jump-original-dired-buffer ()
  1134. "Jump to the dired buffer associated with the current image file.
  1135. You probably want to use this together with
  1136. `image-dired-track-original-file'."
  1137. (interactive)
  1138. (let ((buf (image-dired-associated-dired-buffer))
  1139. window frame)
  1140. (setq window (image-dired-get-buffer-window buf))
  1141. (if window
  1142. (progn
  1143. (if (not (equal (selected-frame) (setq frame (window-frame window))))
  1144. (select-frame-set-input-focus frame))
  1145. (select-window window))
  1146. (message "Associated dired buffer not visible"))))
  1147. ;;;###autoload
  1148. (defun image-dired-jump-thumbnail-buffer ()
  1149. "Jump to thumbnail buffer."
  1150. (interactive)
  1151. (let ((window (image-dired-thumbnail-window))
  1152. frame)
  1153. (if window
  1154. (progn
  1155. (if (not (equal (selected-frame) (setq frame (window-frame window))))
  1156. (select-frame-set-input-focus frame))
  1157. (select-window window))
  1158. (message "Thumbnail buffer not visible"))))
  1159. (defvar image-dired-thumbnail-mode-map (make-sparse-keymap)
  1160. "Keymap for `image-dired-thumbnail-mode'.")
  1161. (defvar image-dired-thumbnail-mode-line-up-map (make-sparse-keymap)
  1162. "Keymap for line-up commands in `image-dired-thumbnail-mode'.")
  1163. (defvar image-dired-thumbnail-mode-tag-map (make-sparse-keymap)
  1164. "Keymap for tag commands in `image-dired-thumbnail-mode'.")
  1165. (defun image-dired-define-thumbnail-mode-keymap ()
  1166. "Define keymap for `image-dired-thumbnail-mode'."
  1167. ;; Keys
  1168. (define-key image-dired-thumbnail-mode-map [right] 'image-dired-forward-image)
  1169. (define-key image-dired-thumbnail-mode-map [left] 'image-dired-backward-image)
  1170. (define-key image-dired-thumbnail-mode-map [up] 'image-dired-previous-line)
  1171. (define-key image-dired-thumbnail-mode-map [down] 'image-dired-next-line)
  1172. (define-key image-dired-thumbnail-mode-map "\C-f" 'image-dired-forward-image)
  1173. (define-key image-dired-thumbnail-mode-map "\C-b" 'image-dired-backward-image)
  1174. (define-key image-dired-thumbnail-mode-map "\C-p" 'image-dired-previous-line)
  1175. (define-key image-dired-thumbnail-mode-map "\C-n" 'image-dired-next-line)
  1176. (define-key image-dired-thumbnail-mode-map "d" 'image-dired-flag-thumb-original-file)
  1177. (define-key image-dired-thumbnail-mode-map [delete]
  1178. 'image-dired-flag-thumb-original-file)
  1179. (define-key image-dired-thumbnail-mode-map "m" 'image-dired-mark-thumb-original-file)
  1180. (define-key image-dired-thumbnail-mode-map "u" 'image-dired-unmark-thumb-original-file)
  1181. (define-key image-dired-thumbnail-mode-map "." 'image-dired-track-original-file)
  1182. (define-key image-dired-thumbnail-mode-map [tab] 'image-dired-jump-original-dired-buffer)
  1183. ;; add line-up map
  1184. (define-key image-dired-thumbnail-mode-map "g" image-dired-thumbnail-mode-line-up-map)
  1185. ;; map it to "g" so that the user can press it more quickly
  1186. (define-key image-dired-thumbnail-mode-line-up-map "g" 'image-dired-line-up-dynamic)
  1187. ;; "f" for "fixed" number of thumbs per row
  1188. (define-key image-dired-thumbnail-mode-line-up-map "f" 'image-dired-line-up)
  1189. ;; "i" for "interactive"
  1190. (define-key image-dired-thumbnail-mode-line-up-map "i" 'image-dired-line-up-interactive)
  1191. ;; add tag map
  1192. (define-key image-dired-thumbnail-mode-map "t" image-dired-thumbnail-mode-tag-map)
  1193. ;; map it to "t" so that the user can press it more quickly
  1194. (define-key image-dired-thumbnail-mode-tag-map "t" 'image-dired-tag-thumbnail)
  1195. ;; "r" for "remove"
  1196. (define-key image-dired-thumbnail-mode-tag-map "r" 'image-dired-tag-thumbnail-remove)
  1197. (define-key image-dired-thumbnail-mode-map "\C-m"
  1198. 'image-dired-display-thumbnail-original-image)
  1199. (define-key image-dired-thumbnail-mode-map [C-return]
  1200. 'image-dired-thumbnail-display-external)
  1201. (define-key image-dired-thumbnail-mode-map "l" 'image-dired-rotate-thumbnail-left)
  1202. (define-key image-dired-thumbnail-mode-map "r" 'image-dired-rotate-thumbnail-right)
  1203. (define-key image-dired-thumbnail-mode-map "L" 'image-dired-rotate-original-left)
  1204. (define-key image-dired-thumbnail-mode-map "R" 'image-dired-rotate-original-right)
  1205. (define-key image-dired-thumbnail-mode-map "D"
  1206. 'image-dired-thumbnail-set-image-description)
  1207. (define-key image-dired-thumbnail-mode-map "\C-d" 'image-dired-delete-char)
  1208. (define-key image-dired-thumbnail-mode-map " "
  1209. 'image-dired-display-next-thumbnail-original)
  1210. (define-key image-dired-thumbnail-mode-map
  1211. (kbd "DEL") 'image-dired-display-previous-thumbnail-original)
  1212. (define-key image-dired-thumbnail-mode-map "c" 'image-dired-comment-thumbnail)
  1213. (define-key image-dired-thumbnail-mode-map "q" 'image-dired-kill-buffer-and-window)
  1214. ;; Mouse
  1215. (define-key image-dired-thumbnail-mode-map [mouse-2] 'image-dired-mouse-display-image)
  1216. (define-key image-dired-thumbnail-mode-map [mouse-1] 'image-dired-mouse-select-thumbnail)
  1217. ;; Seems I must first set C-down-mouse-1 to undefined, or else it
  1218. ;; will trigger the buffer menu. If I try to instead bind
  1219. ;; C-down-mouse-1 to `image-dired-mouse-toggle-mark', I get a message
  1220. ;; about C-mouse-1 not being defined afterwards. Annoying, but I
  1221. ;; probably do not completely understand mouse events.
  1222. (define-key image-dired-thumbnail-mode-map [C-down-mouse-1] 'undefined)
  1223. (define-key image-dired-thumbnail-mode-map [C-mouse-1] 'image-dired-mouse-toggle-mark)
  1224. ;; Menu
  1225. (define-key image-dired-thumbnail-mode-map [menu-bar image-dired]
  1226. (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
  1227. (define-key image-dired-thumbnail-mode-map
  1228. [menu-bar image-dired image-dired-kill-buffer-and-window]
  1229. '("Quit" . image-dired-kill-buffer-and-window))
  1230. (define-key image-dired-thumbnail-mode-map
  1231. [menu-bar image-dired image-dired-delete-char]
  1232. '("Delete thumbnail from buffer" . image-dired-delete-char))
  1233. (define-key image-dired-thumbnail-mode-map
  1234. [menu-bar image-dired image-dired-tag-thumbnail-remove]
  1235. '("Remove tag from thumbnail" . image-dired-tag-thumbnail-remove))
  1236. (define-key image-dired-thumbnail-mode-map
  1237. [menu-bar image-dired image-dired-tag-thumbnail]
  1238. '("Tag thumbnail" . image-dired-tag-thumbnail))
  1239. (define-key image-dired-thumbnail-mode-map
  1240. [menu-bar image-dired image-dired-comment-thumbnail]
  1241. '("Comment thumbnail" . image-dired-comment-thumbnail))
  1242. (define-key image-dired-thumbnail-mode-map
  1243. [menu-bar image-dired image-dired-refresh-thumb]
  1244. '("Refresh thumb" . image-dired-refresh-thumb))
  1245. (define-key image-dired-thumbnail-mode-map
  1246. [menu-bar image-dired image-dired-line-up-dynamic]
  1247. '("Dynamic line up" . image-dired-line-up-dynamic))
  1248. (define-key image-dired-thumbnail-mode-map
  1249. [menu-bar image-dired image-dired-line-up]
  1250. '("Line up thumbnails" . image-dired-line-up))
  1251. (define-key image-dired-thumbnail-mode-map
  1252. [menu-bar image-dired image-dired-rotate-thumbnail-left]
  1253. '("Rotate thumbnail left" . image-dired-rotate-thumbnail-left))
  1254. (define-key image-dired-thumbnail-mode-map
  1255. [menu-bar image-dired image-dired-rotate-thumbnail-right]
  1256. '("Rotate thumbnail right" . image-dired-rotate-thumbnail-right))
  1257. (define-key image-dired-thumbnail-mode-map
  1258. [menu-bar image-dired image-dired-rotate-original-left]
  1259. '("Rotate original left" . image-dired-rotate-original-left))
  1260. (define-key image-dired-thumbnail-mode-map
  1261. [menu-bar image-dired image-dired-rotate-original-right]
  1262. '("Rotate original right" . image-dired-rotate-original-right))
  1263. (define-key image-dired-thumbnail-mode-map
  1264. [menu-bar image-dired image-dired-toggle-movement-tracking]
  1265. '("Toggle movement tracking on/off" . image-dired-toggle-movement-tracking))
  1266. (define-key image-dired-thumbnail-mode-map
  1267. [menu-bar image-dired image-dired-jump-original-dired-buffer]
  1268. '("Jump to dired buffer" . image-dired-jump-original-dired-buffer))
  1269. (define-key image-dired-thumbnail-mode-map
  1270. [menu-bar image-dired image-dired-track-original-file]
  1271. '("Track original" . image-dired-track-original-file))
  1272. (define-key image-dired-thumbnail-mode-map
  1273. [menu-bar image-dired image-dired-flag-thumb-original-file]
  1274. '("Flag original for deletion" . image-dired-flag-thumb-original-file))
  1275. (define-key image-dired-thumbnail-mode-map
  1276. [menu-bar image-dired image-dired-unmark-thumb-original-file]
  1277. '("Unmark original" . image-dired-unmark-thumb-original-file))
  1278. (define-key image-dired-thumbnail-mode-map
  1279. [menu-bar image-dired image-dired-mark-thumb-original-file]
  1280. '("Mark original" . image-dired-mark-thumb-original-file))
  1281. (define-key image-dired-thumbnail-mode-map
  1282. [menu-bar image-dired image-dired-thumbnail-display-external]
  1283. '("Display in external viewer" . image-dired-thumbnail-display-external))
  1284. (define-key image-dired-thumbnail-mode-map
  1285. [menu-bar image-dired image-dired-display-thumbnail-original-image]
  1286. '("Display image" . image-dired-display-thumbnail-original-image)))
  1287. (defvar image-dired-display-image-mode-map (make-sparse-keymap)
  1288. "Keymap for `image-dired-display-image-mode'.")
  1289. (defun image-dired-define-display-image-mode-keymap ()
  1290. "Define keymap for `image-dired-display-image-mode'."
  1291. ;; Keys
  1292. (define-key image-dired-display-image-mode-map "q" 'image-dired-kill-buffer-and-window)
  1293. (define-key image-dired-display-image-mode-map "f"
  1294. 'image-dired-display-current-image-full)
  1295. (define-key image-dired-display-image-mode-map "s"
  1296. 'image-dired-display-current-image-sized)
  1297. ;; Menu
  1298. (define-key image-dired-display-image-mode-map [menu-bar image-dired]
  1299. (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
  1300. (define-key image-dired-display-image-mode-map
  1301. [menu-bar image-dired image-dired-kill-buffer-and-window]
  1302. '("Quit" . image-dired-kill-buffer-and-window))
  1303. (define-key image-dired-display-image-mode-map
  1304. [menu-bar image-dired image-dired-display-current-image-sized]
  1305. '("Display original, sized to fit" . image-dired-display-current-image-sized))
  1306. (define-key image-dired-display-image-mode-map
  1307. [menu-bar image-dired image-dired-display-current-image-full]
  1308. '("Display original, full size" . image-dired-display-current-image-full))
  1309. )
  1310. (defun image-dired-display-current-image-full ()
  1311. "Display current image in full size."
  1312. (interactive)
  1313. (let ((file (image-dired-original-file-name)))
  1314. (if file
  1315. (progn
  1316. (image-dired-display-image file t)
  1317. (message "Full size image displayed"))
  1318. (error "No original file name at point"))))
  1319. (defun image-dired-display-current-image-sized ()
  1320. "Display current image in sized to fit window dimensions."
  1321. (interactive)
  1322. (let ((file (image-dired-original-file-name)))
  1323. (if file
  1324. (progn
  1325. (image-dired-display-image file)
  1326. (message "Full size image displayed"))
  1327. (error "No original file name at point"))))
  1328. (define-derived-mode image-dired-thumbnail-mode
  1329. fundamental-mode "image-dired-thumbnail"
  1330. "Browse and manipulate thumbnail images using dired.
  1331. Use `image-dired-dired' and `image-dired-setup-dired-keybindings' to get a
  1332. nice setup to start with."
  1333. (image-dired-define-thumbnail-mode-keymap)
  1334. (message "image-dired-thumbnail-mode enabled"))
  1335. (define-derived-mode image-dired-display-image-mode
  1336. fundamental-mode "image-dired-image-display"
  1337. "Mode for displaying and manipulating original image.
  1338. Resized or in full-size."
  1339. (image-dired-define-display-image-mode-keymap)
  1340. (message "image-dired-display-image-mode enabled"))
  1341. ;;;###autoload
  1342. (defun image-dired-setup-dired-keybindings ()
  1343. "Setup easy-to-use keybindings for the commands to be used in dired mode.
  1344. Note that n, p and <down> and <up> will be hijacked and bound to
  1345. `image-dired-dired-x-line'."
  1346. (interactive)
  1347. ;; Hijack previous and next line movement. Let C-p and C-b be
  1348. ;; though...
  1349. (define-key dired-mode-map "p" 'image-dired-dired-previous-line)
  1350. (define-key dired-mode-map "n" 'image-dired-dired-next-line)
  1351. (define-key dired-mode-map [up] 'image-dired-dired-previous-line)
  1352. (define-key dired-mode-map [down] 'image-dired-dired-next-line)
  1353. (define-key dired-mode-map (kbd "C-S-n") 'image-dired-next-line-and-display)
  1354. (define-key dired-mode-map (kbd "C-S-p") 'image-dired-previous-line-and-display)
  1355. (define-key dired-mode-map (kbd "C-S-m") 'image-dired-mark-and-display-next)
  1356. (define-key dired-mode-map "\C-td" 'image-dired-display-thumbs)
  1357. (define-key dired-mode-map "\C-tt" 'image-dired-tag-files)
  1358. (define-key dired-mode-map "\C-tr" 'image-dired-delete-tag)
  1359. (define-key dired-mode-map [tab] 'image-dired-jump-thumbnail-buffer)
  1360. (define-key dired-mode-map "\C-ti" 'image-dired-dired-display-image)
  1361. (define-key dired-mode-map "\C-tx" 'image-dired-dired-display-external)
  1362. (define-key dired-mode-map "\C-ta" 'image-dired-display-thumbs-append)
  1363. (define-key dired-mode-map "\C-t." 'image-dired-display-thumb)
  1364. (define-key dired-mode-map "\C-tc" 'image-dired-dired-comment-files)
  1365. (define-key dired-mode-map "\C-tf" 'image-dired-mark-tagged-files)
  1366. ;; Menu for dired
  1367. (define-key dired-mode-map [menu-bar image-dired]
  1368. (cons "Image-Dired" (make-sparse-keymap "Image-Dired")))
  1369. (define-key dired-mode-map [menu-bar image-dired image-dired-copy-with-exif-file-name]
  1370. '("Copy with EXIF file name" . image-dired-copy-with-exif-file-name))
  1371. (define-key dired-mode-map [menu-bar image-dired image-dired-dired-comment-files]
  1372. '("Comment files" . image-dired-dired-comment-files))
  1373. (define-key dired-mode-map [menu-bar image-dired image-dired-mark-tagged-files]
  1374. '("Mark tagged files" . image-dired-mark-tagged-files))
  1375. (define-key dired-mode-map [menu-bar image-dired image-dired-delete-tag]
  1376. '("Remove tag from files" . image-dired-delete-tag))
  1377. (define-key dired-mode-map [menu-bar image-dired image-dired-tag-files]
  1378. '("Tag files" . image-dired-tag-files))
  1379. (define-key dired-mode-map [menu-bar image-dired image-dired-jump-thumbnail-buffer]
  1380. '("Jump to thumbnail buffer" . image-dired-jump-thumbnail-buffer))
  1381. (define-key dired-mode-map [menu-bar image-dired image-dired-toggle-movement-tracking]
  1382. '("Toggle movement tracking" . image-dired-toggle-movement-tracking))
  1383. (define-key dired-mode-map
  1384. [menu-bar image-dired image-dired-toggle-append-browsing]
  1385. '("Toggle append browsing" . image-dired-toggle-append-browsing))
  1386. (define-key dired-mode-map
  1387. [menu-bar image-dired image-dired-toggle-disp-props]
  1388. '("Toggle display properties" . image-dired-toggle-dired-display-properties))
  1389. (define-key dired-mode-map
  1390. [menu-bar image-dired image-dired-dired-display-external]
  1391. '("Display in external viewer" . image-dired-dired-display-external))
  1392. (define-key dired-mode-map
  1393. [menu-bar image-dired image-dired-dired-display-image]
  1394. '("Display image" . image-dired-dired-display-image))
  1395. (define-key dired-mode-map
  1396. [menu-bar image-dired image-dired-display-thumb]
  1397. '("Display this thumbnail" . image-dired-display-thumb))
  1398. (define-key dired-mode-map
  1399. [menu-bar image-dired image-dired-display-thumbs-append]
  1400. '("Display thumbnails append" . image-dired-display-thumbs-append))
  1401. (define-key dired-mode-map
  1402. [menu-bar image-dired image-dired-display-thumbs]
  1403. '("Display thumbnails" . image-dired-display-thumbs))
  1404. (define-key dired-mode-map
  1405. [menu-bar image-dired image-dired-create-thumbs]
  1406. '("Create thumbnails for marked files" . image-dired-create-thumbs))
  1407. (define-key dired-mode-map
  1408. [menu-bar image-dired image-dired-mark-and-display-next]
  1409. '("Mark and display next" . image-dired-mark-and-display-next))
  1410. (define-key dired-mode-map
  1411. [menu-bar image-dired image-dired-previous-line-and-display]
  1412. '("Display thumb for previous file" . image-dired-previous-line-and-display))
  1413. (define-key dired-mode-map
  1414. [menu-bar image-dired image-dired-next-line-and-display]
  1415. '("Display thumb for next file" . image-dired-next-line-and-display)))
  1416. (declare-function clear-image-cache "image.c" (&optional filter))
  1417. (defun image-dired-create-thumbs (&optional arg)
  1418. "Create thumbnail images for all marked files in dired.
  1419. With prefix argument ARG, create thumbnails even if they already exist
  1420. \(i.e. use this to refresh your thumbnails)."
  1421. (interactive "P")
  1422. (let (thumb-name files)
  1423. (setq files (dired-get-marked-files))
  1424. (mapcar
  1425. (lambda (curr-file)
  1426. (setq thumb-name (image-dired-thumb-name curr-file))
  1427. ;; If the user overrides the exist check, we must clear the
  1428. ;; image cache so that if the user wants to display the
  1429. ;; thumbnail, it is not fetched from cache.
  1430. (if arg
  1431. (clear-image-cache))
  1432. (if (or (not (file-exists-p thumb-name))
  1433. arg)
  1434. (if (not (= 0 (image-dired-create-thumb curr-file
  1435. (image-dired-thumb-name curr-file))))
  1436. (error "Thumb could not be created"))))
  1437. files)))
  1438. (defvar image-dired-slideshow-timer nil
  1439. "Slideshow timer.")
  1440. (defvar image-dired-slideshow-count 0
  1441. "Keeping track on number of images in slideshow.")
  1442. (defvar image-dired-slideshow-times 0
  1443. "Number of pictures to display in slideshow.")
  1444. (defun image-dired-slideshow-step ()
  1445. "Step to next file, if `image-dired-slideshow-times' has not been reached."
  1446. (if (< image-dired-slideshow-count image-dired-slideshow-times)
  1447. (progn
  1448. (message "%s" (1+ image-dired-slideshow-count))
  1449. (setq image-dired-slideshow-count (1+ image-dired-slideshow-count))
  1450. (image-dired-next-line-and-display))
  1451. (image-dired-slideshow-stop)))
  1452. (defun image-dired-slideshow-start ()
  1453. "Start slideshow.
  1454. Ask user for number of images to show and the delay in between."
  1455. (interactive)
  1456. (setq image-dired-slideshow-count 0)
  1457. (setq image-dired-slideshow-times (string-to-number (read-string "How many: ")))
  1458. (let ((repeat (string-to-number
  1459. (read-string
  1460. "Delay, in seconds. Decimals are accepted : " "1"))))
  1461. (setq image-dired-slideshow-timer
  1462. (run-with-timer
  1463. 0 repeat
  1464. 'image-dired-slideshow-step))))
  1465. (defun image-dired-slideshow-stop ()
  1466. "Cancel slideshow."
  1467. (interactive)
  1468. (cancel-timer image-dired-slideshow-timer))
  1469. (defun image-dired-delete-char ()
  1470. "Remove current thumbnail from thumbnail buffer and line up."
  1471. (interactive)
  1472. (let ((inhibit-read-only t))
  1473. (delete-char 1)
  1474. (if (looking-at " ")
  1475. (delete-char 1))))
  1476. ;;;###autoload
  1477. (defun image-dired-display-thumbs-append ()
  1478. "Append thumbnails to `image-dired-thumbnail-buffer'."
  1479. (interactive)
  1480. (image-dired-display-thumbs nil t t))
  1481. ;;;###autoload
  1482. (defun image-dired-display-thumb ()
  1483. "Shorthand for `image-dired-display-thumbs' with prefix argument."
  1484. (interactive)
  1485. (image-dired-display-thumbs t nil t))
  1486. (defun image-dired-line-up ()
  1487. "Line up thumbnails according to `image-dired-thumbs-per-row'.
  1488. See also `image-dired-line-up-dynamic'."
  1489. (interactive)
  1490. (let ((inhibit-read-only t))
  1491. (goto-char (point-min))
  1492. (while (and (not (image-dired-image-at-point-p))
  1493. (not (eobp)))
  1494. (delete-char 1))
  1495. (while (not (eobp))
  1496. (forward-char)
  1497. (while (and (not (image-dired-image-at-point-p))
  1498. (not (eobp)))
  1499. (delete-char 1)))
  1500. (goto-char (point-min))
  1501. (let ((count 0))
  1502. (while (not (eobp))
  1503. (forward-char)
  1504. (if (= image-dired-thumbs-per-row 1)
  1505. (insert "\n")
  1506. (insert " ")
  1507. (setq count (1+ count))
  1508. (when (and (= count (- image-dired-thumbs-per-row 1))
  1509. (not (eobp)))
  1510. (forward-char)
  1511. (insert "\n")
  1512. (setq count 0)))))
  1513. (goto-char (point-min))))
  1514. (defun image-dired-line-up-dynamic ()
  1515. "Line up thumbnails images dynamically.
  1516. Calculate how many thumbnails fit."
  1517. (interactive)
  1518. (let* ((char-width (frame-char-width))
  1519. (width (image-dired-window-width-pixels (image-dired-thumbnail-window)))
  1520. (image-dired-thumbs-per-row
  1521. (/ width
  1522. (+ (* 2 image-dired-thumb-relief)
  1523. (* 2 image-dired-thumb-margin)
  1524. image-dired-thumb-width char-width))))
  1525. (image-dired-line-up)))
  1526. (defun image-dired-line-up-interactive ()
  1527. "Line up thumbnails interactively.
  1528. Ask user how many thumbnails should be displayed per row."
  1529. (interactive)
  1530. (let ((image-dired-thumbs-per-row
  1531. (string-to-number (read-string "How many thumbs per row: "))))
  1532. (if (not (> image-dired-thumbs-per-row 0))
  1533. (message "Number must be greater than 0")
  1534. (image-dired-line-up))))
  1535. (defun image-dired-thumbnail-display-external ()
  1536. "Display original image for thumbnail at point using external viewer."
  1537. (interactive)
  1538. (let ((file (image-dired-original-file-name)))
  1539. (if (not (image-dired-image-at-point-p))
  1540. (message "No thumbnail at point")
  1541. (if (not file)
  1542. (message "No original file name found")
  1543. (call-process shell-file-name nil nil nil shell-command-switch
  1544. (format "%s \"%s\"" image-dired-external-viewer file))))))
  1545. ;;;###autoload
  1546. (defun image-dired-dired-display-external ()
  1547. "Display file at point using an external viewer."
  1548. (interactive)
  1549. (let ((file (dired-get-filename)))
  1550. (call-process shell-file-name nil nil nil shell-command-switch
  1551. (format "%s \"%s\"" image-dired-external-viewer file))))
  1552. (defun image-dired-window-width-pixels (window)
  1553. "Calculate WINDOW width in pixels."
  1554. (* (window-width window) (frame-char-width)))
  1555. (defun image-dired-window-height-pixels (window)
  1556. "Calculate WINDOW height in pixels."
  1557. ;; Note: The mode-line consumes one line
  1558. (* (- (window-height window) 1) (frame-char-height)))
  1559. (defun image-dired-display-window ()
  1560. "Return window where `image-dired-display-image-buffer' is visible."
  1561. (get-window-with-predicate
  1562. (lambda (window)
  1563. (equal (buffer-name (window-buffer window)) image-dired-display-image-buffer))
  1564. nil t))
  1565. (defun image-dired-thumbnail-window ()
  1566. "Return window where `image-dired-thumbnail-buffer' is visible."
  1567. (get-window-with-predicate
  1568. (lambda (window)
  1569. (equal (buffer-name (window-buffer window)) image-dired-thumbnail-buffer))
  1570. nil t))
  1571. (defun image-dired-associated-dired-buffer-window ()
  1572. "Return window where associated dired buffer is visible."
  1573. (let (buf)
  1574. (if (image-dired-image-at-point-p)
  1575. (progn
  1576. (setq buf (image-dired-associated-dired-buffer))
  1577. (get-window-with-predicate
  1578. (lambda (window)
  1579. (equal (window-buffer window) buf))))
  1580. (error "No thumbnail image at point"))))
  1581. (defun image-dired-display-window-width ()
  1582. "Return width, in pixels, of image-dired's image display window."
  1583. (- (image-dired-window-width-pixels (image-dired-display-window))
  1584. image-dired-display-window-width-correction))
  1585. (defun image-dired-display-window-height ()
  1586. "Return height, in pixels, of image-dired's image display window."
  1587. (- (image-dired-window-height-pixels (image-dired-display-window))
  1588. image-dired-display-window-height-correction))
  1589. (defun image-dired-display-image (file &optional original-size)
  1590. "Display image FILE in image buffer.
  1591. Use this when you want to display the image, semi sized, in a new
  1592. window. The image is sized to fit the display window (using a
  1593. temporary file, don't worry). Because of this, it will not be as
  1594. quick as opening it directly, but on most modern systems it
  1595. should feel snappy enough.
  1596. If optional argument ORIGINAL-SIZE is non-nil, display image in its
  1597. original size."
  1598. (let ((new-file (expand-file-name image-dired-temp-image-file))
  1599. width height command ret
  1600. (image-type 'jpeg))
  1601. (setq file (expand-file-name file))
  1602. (if (not original-size)
  1603. (progn
  1604. (setq width (image-dired-display-window-width))
  1605. (setq height (image-dired-display-window-height))
  1606. (setq command
  1607. (format-spec
  1608. image-dired-cmd-create-temp-image-options
  1609. (list
  1610. (cons ?p image-dired-cmd-create-temp-image-program)
  1611. (cons ?w width)
  1612. (cons ?h height)
  1613. (cons ?f file)
  1614. (cons ?t new-file))))
  1615. (setq ret (call-process shell-file-name nil nil nil
  1616. shell-command-switch command))
  1617. (if (not (= 0 ret))
  1618. (error "Could not resize image")))
  1619. (setq image-type (image-type-from-file-name file))
  1620. (copy-file file new-file t))
  1621. (with-current-buffer (image-dired-create-display-image-buffer)
  1622. (let ((inhibit-read-only t))
  1623. (erase-buffer)
  1624. (clear-image-cache)
  1625. (image-dired-insert-image image-dired-temp-image-file image-type 0 0)
  1626. (goto-char (point-min))
  1627. (image-dired-update-property 'original-file-name file)))))
  1628. (defun image-dired-display-thumbnail-original-image (&optional arg)
  1629. "Display current thumbnail's original image in display buffer.
  1630. See documentation for `image-dired-display-image' for more information.
  1631. With prefix argument ARG, display image in its original size."
  1632. (interactive "P")
  1633. (let ((file (image-dired-original-file-name)))
  1634. (if (not (string-equal major-mode "image-dired-thumbnail-mode"))
  1635. (message "Not in image-dired-thumbnail-mode")
  1636. (if (not (image-dired-image-at-point-p))
  1637. (message "No thumbnail at point")
  1638. (if (not file)
  1639. (message "No original file name found")
  1640. (image-dired-create-display-image-buffer)
  1641. (display-buffer image-dired-display-image-buffer)
  1642. (image-dired-display-image file arg))))))
  1643. ;;;###autoload
  1644. (defun image-dired-dired-display-image (&optional arg)
  1645. "Display current image file.
  1646. See documentation for `image-dired-display-image' for more information.
  1647. With prefix argument ARG, display image in its original size."
  1648. (interactive "P")
  1649. (image-dired-create-display-image-buffer)
  1650. (display-buffer image-dired-display-image-buffer)
  1651. (image-dired-display-image (dired-get-filename) arg))
  1652. (defun image-dired-image-at-point-p ()
  1653. "Return true if there is an image-dired thumbnail at point."
  1654. (get-text-property (point) 'image-dired-thumbnail))
  1655. (defun image-dired-rotate-thumbnail (degrees)
  1656. "Rotate thumbnail DEGREES degrees."
  1657. (if (not (image-dired-image-at-point-p))
  1658. (message "No thumbnail at point")
  1659. (let ((file (image-dired-thumb-name (image-dired-original-file-name)))
  1660. command)
  1661. (setq command (format-spec
  1662. image-dired-cmd-rotate-thumbnail-options
  1663. (list
  1664. (cons ?p image-dired-cmd-rotate-thumbnail-program)
  1665. (cons ?d degrees)
  1666. (cons ?t (expand-file-name file)))))
  1667. (call-process shell-file-name nil nil nil shell-command-switch command)
  1668. ;; Clear the cache to refresh image. I wish I could just refresh
  1669. ;; the current file but I do not know how to do that. Yet...
  1670. (clear-image-cache))))
  1671. (defun image-dired-rotate-thumbnail-left ()
  1672. "Rotate thumbnail left (counter clockwise) 90 degrees.
  1673. The result of the rotation is displayed in the image display area
  1674. and a confirmation is needed before the original image files is
  1675. overwritten. This confirmation can be turned off using
  1676. `image-dired-rotate-original-ask-before-overwrite'."
  1677. (interactive)
  1678. (image-dired-rotate-thumbnail "270"))
  1679. (defun image-dired-rotate-thumbnail-right ()
  1680. "Rotate thumbnail counter right (clockwise) 90 degrees.
  1681. The result of the rotation is displayed in the image display area
  1682. and a confirmation is needed before the original image files is
  1683. overwritten. This confirmation can be turned off using
  1684. `image-dired-rotate-original-ask-before-overwrite'."
  1685. (interactive)
  1686. (image-dired-rotate-thumbnail "90"))
  1687. (defun image-dired-refresh-thumb ()
  1688. "Force creation of new image for current thumbnail."
  1689. (interactive)
  1690. (let ((file (image-dired-original-file-name)))
  1691. (clear-image-cache)
  1692. (image-dired-create-thumb file (image-dired-thumb-name file))))
  1693. (defun image-dired-rotate-original (degrees)
  1694. "Rotate original image DEGREES degrees."
  1695. (if (not (image-dired-image-at-point-p))
  1696. (message "No image at point")
  1697. (let ((file (image-dired-original-file-name))
  1698. command)
  1699. (if (not (string-match "\.[jJ][pP[eE]?[gG]$" file))
  1700. (error "Only JPEG images can be rotated!"))
  1701. (setq command (format-spec
  1702. image-dired-cmd-rotate-original-options
  1703. (list
  1704. (cons ?p image-dired-cmd-rotate-original-program)
  1705. (cons ?d degrees)
  1706. (cons ?o (expand-file-name file))
  1707. (cons ?t image-dired-temp-rotate-image-file))))
  1708. (if (not (= 0 (call-process shell-file-name nil nil nil
  1709. shell-command-switch command)))
  1710. (error "Could not rotate image")
  1711. (image-dired-display-image image-dired-temp-rotate-image-file)
  1712. (if (or (and image-dired-rotate-original-ask-before-overwrite
  1713. (y-or-n-p
  1714. "Rotate to temp file OK. Overwrite original image? "))
  1715. (not image-dired-rotate-original-ask-before-overwrite))
  1716. (progn
  1717. (copy-file image-dired-temp-rotate-image-file file t)
  1718. (image-dired-refresh-thumb))
  1719. (image-dired-display-image file))))))
  1720. (defun image-dired-rotate-original-left ()
  1721. "Rotate original image left (counter clockwise) 90 degrees."
  1722. (interactive)
  1723. (image-dired-rotate-original "270"))
  1724. (defun image-dired-rotate-original-right ()
  1725. "Rotate original image right (clockwise) 90 degrees."
  1726. (interactive)
  1727. (image-dired-rotate-original "90"))
  1728. (defun image-dired-get-exif-file-name (file)
  1729. "Use the image's EXIF information to return a unique file name.
  1730. The file name should be unique as long as you do not take more than
  1731. one picture per second. The original file name is suffixed at the end
  1732. for traceability. The format of the returned file name is
  1733. YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg. Used from
  1734. `image-dired-copy-with-exif-file-name'."
  1735. (let (data no-exif-data-found)
  1736. (if (not (string-match "\.[Jj][Pp][Ee]?[Gg]$" (expand-file-name file)))
  1737. (progn
  1738. (setq no-exif-data-found t)
  1739. (setq data
  1740. (format-time-string
  1741. "%Y:%m:%d %H:%M:%S"
  1742. (nth 5 (file-attributes (expand-file-name file))))))
  1743. (setq data (image-dired-get-exif-data (expand-file-name file)
  1744. "DateTimeOriginal")))
  1745. (while (string-match "[ :]" data)
  1746. (setq data (replace-match "_" nil nil data)))
  1747. (format "%s%s%s" data
  1748. (if no-exif-data-found
  1749. "_noexif_"
  1750. "_")
  1751. (file-name-nondirectory file))))
  1752. (defun image-dired-thumbnail-set-image-description ()
  1753. "Set the ImageDescription EXIF tag for the original image.
  1754. If the image already has a value for this tag, it is used as the
  1755. default value at the prompt."
  1756. (interactive)
  1757. (if (not (image-dired-image-at-point-p))
  1758. (message "No thumbnail at point")
  1759. (let* ((file (image-dired-original-file-name))
  1760. (old-value (image-dired-get-exif-data file "ImageDescription")))
  1761. (if (eq 0
  1762. (image-dired-set-exif-data file "ImageDescription"
  1763. (read-string "Value of ImageDescription: "
  1764. old-value)))
  1765. (message "Successfully wrote ImageDescription tag.")
  1766. (error "Could not write ImageDescription tag")))))
  1767. (defun image-dired-set-exif-data (file tag-name tag-value)
  1768. "In FILE, set EXIF tag TAG-NAME to value TAG-VALUE."
  1769. (let (command)
  1770. (setq command (format-spec
  1771. image-dired-cmd-write-exif-data-options
  1772. (list
  1773. (cons ?p image-dired-cmd-write-exif-data-program)
  1774. (cons ?f (expand-file-name file))
  1775. (cons ?t tag-name)
  1776. (cons ?v tag-value))))
  1777. (call-process shell-file-name nil nil nil shell-command-switch command)))
  1778. (defun image-dired-get-exif-data (file tag-name)
  1779. "From FILE, return EXIF tag TAG-NAME."
  1780. (let ((buf (get-buffer-create "*image-dired-get-exif-data*"))
  1781. command tag-value)
  1782. (setq command (format-spec
  1783. image-dired-cmd-read-exif-data-options
  1784. (list
  1785. (cons ?p image-dired-cmd-read-exif-data-program)
  1786. (cons ?f file)
  1787. (cons ?t tag-name))))
  1788. (with-current-buffer buf
  1789. (delete-region (point-min) (point-max))
  1790. (if (not (eq (call-process shell-file-name nil t nil
  1791. shell-command-switch command) 0))
  1792. (error "Could not get EXIF tag")
  1793. (goto-char (point-min))
  1794. ;; Clean buffer from newlines and carriage returns before
  1795. ;; getting final info
  1796. (while (search-forward-regexp "[\n\r]" nil t)
  1797. (replace-match "" nil t))
  1798. (setq tag-value (buffer-substring (point-min) (point-max)))))
  1799. tag-value))
  1800. (defun image-dired-copy-with-exif-file-name ()
  1801. "Copy file with unique name to main image directory.
  1802. Copy current or all marked files in dired to a new file in your
  1803. main image directory, using a file name generated by
  1804. `image-dired-get-exif-file-name'. A typical usage for this if when
  1805. copying images from a digital camera into the image directory.
  1806. Typically, you would open up the folder with the incoming
  1807. digital images, mark the files to be copied, and execute this
  1808. function. The result is a couple of new files in
  1809. `image-dired-main-image-directory' called
  1810. 2005_05_08_12_52_00_dscn0319.jpg,
  1811. 2005_05_08_14_27_45_dscn0320.jpg etc."
  1812. (interactive)
  1813. (let (new-name
  1814. (files (dired-get-marked-files)))
  1815. (mapcar
  1816. (lambda (curr-file)
  1817. (setq new-name
  1818. (format "%s/%s"
  1819. (file-name-as-directory
  1820. (expand-file-name image-dired-main-image-directory))
  1821. (image-dired-get-exif-file-name curr-file)))
  1822. (message "Copying %s to %s" curr-file new-name)
  1823. (copy-file curr-file new-name))
  1824. files)))
  1825. (defun image-dired-display-next-thumbnail-original ()
  1826. "In thumbnail buffer, move to next thumbnail and display the image."
  1827. (interactive)
  1828. (image-dired-forward-image)
  1829. (image-dired-display-thumbnail-original-image))
  1830. (defun image-dired-display-previous-thumbnail-original ()
  1831. "Move to previous thumbnail and display image."
  1832. (interactive)
  1833. (image-dired-backward-image)
  1834. (image-dired-display-thumbnail-original-image))
  1835. (defun image-dired-write-comments (file-comments)
  1836. "Write file comments to database.
  1837. Write file comments to one or more files.
  1838. FILE-COMMENTS is an alist on the following form:
  1839. ((FILE . COMMENT) ... )"
  1840. (image-dired-sane-db-file)
  1841. (let (end comment-beg-pos comment-end-pos file comment)
  1842. (image-dired--with-db-file
  1843. (setq buffer-file-name image-dired-db-file)
  1844. (dolist (elt file-comments)
  1845. (setq file (car elt)
  1846. comment (cdr elt))
  1847. (goto-char (point-min))
  1848. (if (search-forward-regexp (format "^%s.*$" file) nil t)
  1849. (progn
  1850. (setq end (point))
  1851. (beginning-of-line)
  1852. ;; Delete old comment, if any
  1853. (when (search-forward ";comment:" end t)
  1854. (setq comment-beg-pos (match-beginning 0))
  1855. ;; Any tags after the comment?
  1856. (if (search-forward ";" end t)
  1857. (setq comment-end-pos (- (point) 1))
  1858. (setq comment-end-pos end))
  1859. ;; Delete comment tag and comment
  1860. (delete-region comment-beg-pos comment-end-pos))
  1861. ;; Insert new comment
  1862. (beginning-of-line)
  1863. (unless (search-forward ";" end t)
  1864. (end-of-line)
  1865. (insert ";"))
  1866. (insert (format "comment:%s;" comment)))
  1867. ;; File does not exist in database - add it.
  1868. (goto-char (point-max))
  1869. (insert (format "\n%s;comment:%s" file comment))))
  1870. (save-buffer))))
  1871. (defun image-dired-update-property (prop value)
  1872. "Update text property PROP with value VALUE at point."
  1873. (let ((inhibit-read-only t))
  1874. (put-text-property
  1875. (point) (1+ (point))
  1876. prop
  1877. value)))
  1878. ;;;###autoload
  1879. (defun image-dired-dired-comment-files ()
  1880. "Add comment to current or marked files in dired."
  1881. (interactive)
  1882. (let ((comment (image-dired-read-comment)))
  1883. (image-dired-write-comments
  1884. (mapcar
  1885. (lambda (curr-file)
  1886. (cons curr-file comment))
  1887. (dired-get-marked-files)))))
  1888. (defun image-dired-comment-thumbnail ()
  1889. "Add comment to current thumbnail in thumbnail buffer."
  1890. (interactive)
  1891. (let* ((file (image-dired-original-file-name))
  1892. (comment (image-dired-read-comment file)))
  1893. (image-dired-write-comments (list (cons file comment)))
  1894. (image-dired-update-property 'comment comment))
  1895. (image-dired-display-thumb-properties))
  1896. (defun image-dired-read-comment (&optional file)
  1897. "Read comment for an image.
  1898. Optionally use old comment from FILE as initial value."
  1899. (let ((comment
  1900. (read-string
  1901. "Comment: "
  1902. (if file (image-dired-get-comment file)))))
  1903. comment))
  1904. (defun image-dired-get-comment (file)
  1905. "Get comment for file FILE."
  1906. (image-dired-sane-db-file)
  1907. (image-dired--with-db-file
  1908. (let (end comment-beg-pos comment-end-pos comment)
  1909. (when (search-forward-regexp (format "^%s" file) nil t)
  1910. (end-of-line)
  1911. (setq end (point))
  1912. (beginning-of-line)
  1913. (when (search-forward ";comment:" end t)
  1914. (setq comment-beg-pos (point))
  1915. (if (search-forward ";" end t)
  1916. (setq comment-end-pos (- (point) 1))
  1917. (setq comment-end-pos end))
  1918. (setq comment (buffer-substring
  1919. comment-beg-pos comment-end-pos))))
  1920. comment)))
  1921. ;;;###autoload
  1922. (defun image-dired-mark-tagged-files ()
  1923. "Use regexp to mark files with matching tag.
  1924. A `tag' is a keyword, a piece of meta data, associated with an
  1925. image file and stored in image-dired's database file. This command
  1926. lets you input a regexp and this will be matched against all tags
  1927. on all image files in the database file. The files that have a
  1928. matching tag will be marked in the dired buffer."
  1929. (interactive)
  1930. (image-dired-sane-db-file)
  1931. (let ((tag (read-string "Mark tagged files (regexp): "))
  1932. (hits 0)
  1933. files)
  1934. (image-dired--with-db-file
  1935. ;; Collect matches
  1936. (while (search-forward-regexp
  1937. (concat "\\(^[^;\n]+\\);.*" tag ".*$") nil t)
  1938. (push (match-string 1) files)))
  1939. ;; Mark files
  1940. (dolist (curr-file files)
  1941. ;; I tried using `dired-mark-files-regexp' but it was waaaay to
  1942. ;; slow. Don't bother about hits found in other directories
  1943. ;; than the current one.
  1944. (when (string= (file-name-as-directory
  1945. (expand-file-name default-directory))
  1946. (file-name-as-directory
  1947. (file-name-directory curr-file)))
  1948. (setq curr-file (file-name-nondirectory curr-file))
  1949. (goto-char (point-min))
  1950. (when (search-forward-regexp (format "\\s %s$" curr-file) nil t)
  1951. (setq hits (+ hits 1))
  1952. (dired-mark 1))))
  1953. (message "%d files with matching tag marked." hits)))
  1954. (defun image-dired-mouse-display-image (event)
  1955. "Use mouse EVENT, call `image-dired-display-image' to display image.
  1956. Track this in associated dired buffer if `image-dired-track-movement' is
  1957. non-nil."
  1958. (interactive "e")
  1959. (mouse-set-point event)
  1960. (goto-char (posn-point (event-end event)))
  1961. (let ((file (image-dired-original-file-name)))
  1962. (when file
  1963. (if image-dired-track-movement
  1964. (image-dired-track-original-file))
  1965. (image-dired-create-display-image-buffer)
  1966. (display-buffer image-dired-display-image-buffer)
  1967. (image-dired-display-image file))))
  1968. (defun image-dired-mouse-select-thumbnail (event)
  1969. "Use mouse EVENT to select thumbnail image.
  1970. Track this in associated dired buffer if `image-dired-track-movement' is
  1971. non-nil."
  1972. (interactive "e")
  1973. (mouse-set-point event)
  1974. (goto-char (posn-point (event-end event)))
  1975. (if image-dired-track-movement
  1976. (image-dired-track-original-file))
  1977. (image-dired-display-thumb-properties))
  1978. (defun image-dired-mouse-toggle-mark (event)
  1979. "Use mouse EVENT to toggle dired mark for thumbnail.
  1980. Track this in associated dired buffer if `image-dired-track-movement' is
  1981. non-nil."
  1982. (interactive "e")
  1983. (mouse-set-point event)
  1984. (goto-char (posn-point (event-end event)))
  1985. (if image-dired-track-movement
  1986. (image-dired-track-original-file))
  1987. (image-dired-toggle-mark-thumb-original-file))
  1988. (defun image-dired-dired-display-properties ()
  1989. "Display properties for dired file in the echo area."
  1990. (interactive)
  1991. (let* ((file (dired-get-filename))
  1992. (file-name (file-name-nondirectory file))
  1993. (dired-buf (buffer-name (current-buffer)))
  1994. (props (mapconcat
  1995. 'princ
  1996. (image-dired-list-tags file)
  1997. ", "))
  1998. (comment (image-dired-get-comment file)))
  1999. (if file-name
  2000. (message "%s"
  2001. (image-dired-format-properties-string
  2002. dired-buf
  2003. file-name
  2004. props
  2005. comment)))))
  2006. (defvar image-dired-tag-file-list nil
  2007. "List to store tag-file structure.")
  2008. (defvar image-dired-file-tag-list nil
  2009. "List to store file-tag structure.")
  2010. (defvar image-dired-file-comment-list nil
  2011. "List to store file comments.")
  2012. (defun image-dired-add-to-tag-file-list (tag file)
  2013. "Add relation between TAG and FILE."
  2014. (let (curr)
  2015. (if image-dired-tag-file-list
  2016. (if (setq curr (assoc tag image-dired-tag-file-list))
  2017. (if (not (member file curr))
  2018. (setcdr curr (cons file (cdr curr))))
  2019. (setcdr image-dired-tag-file-list
  2020. (cons (list tag file) (cdr image-dired-tag-file-list))))
  2021. (setq image-dired-tag-file-list (list (list tag file))))))
  2022. (defun image-dired-add-to-tag-file-lists (tag file)
  2023. "Helper function used from `image-dired-create-gallery-lists'.
  2024. Add TAG to FILE in one list and FILE to TAG in the other.
  2025. Lisp structures look like the following:
  2026. image-dired-file-tag-list:
  2027. ((\"filename1\" \"tag1\" \"tag2\" \"tag3\" ...)
  2028. (\"filename2\" \"tag1\" \"tag2\" \"tag3\" ...)
  2029. ...)
  2030. image-dired-tag-file-list:
  2031. ((\"tag1\" \"filename1\" \"filename2\" \"filename3\" ...)
  2032. (\"tag2\" \"filename1\" \"filename2\" \"filename3\" ...)
  2033. ...)"
  2034. ;; Add tag to file list
  2035. (let (curr)
  2036. (if image-dired-file-tag-list
  2037. (if (setq curr (assoc file image-dired-file-tag-list))
  2038. (setcdr curr (cons tag (cdr curr)))
  2039. (setcdr image-dired-file-tag-list
  2040. (cons (list file tag) (cdr image-dired-file-tag-list))))
  2041. (setq image-dired-file-tag-list (list (list file tag))))
  2042. ;; Add file to tag list
  2043. (if image-dired-tag-file-list
  2044. (if (setq curr (assoc tag image-dired-tag-file-list))
  2045. (if (not (member file curr))
  2046. (setcdr curr (cons file (cdr curr))))
  2047. (setcdr image-dired-tag-file-list
  2048. (cons (list tag file) (cdr image-dired-tag-file-list))))
  2049. (setq image-dired-tag-file-list (list (list tag file))))))
  2050. (defun image-dired-add-to-file-comment-list (file comment)
  2051. "Helper function used from `image-dired-create-gallery-lists'.
  2052. For FILE, add COMMENT to list.
  2053. Lisp structure looks like the following:
  2054. image-dired-file-comment-list:
  2055. ((\"filename1\" . \"comment1\")
  2056. (\"filename2\" . \"comment2\")
  2057. ...)"
  2058. (if image-dired-file-comment-list
  2059. (if (not (assoc file image-dired-file-comment-list))
  2060. (setcdr image-dired-file-comment-list
  2061. (cons (cons file comment)
  2062. (cdr image-dired-file-comment-list))))
  2063. (setq image-dired-file-comment-list (list (cons file comment)))))
  2064. (defun image-dired-create-gallery-lists ()
  2065. "Create temporary lists used by `image-dired-gallery-generate'."
  2066. (image-dired-sane-db-file)
  2067. (image-dired--with-db-file
  2068. (let (end beg file row-tags)
  2069. (setq image-dired-tag-file-list nil)
  2070. (setq image-dired-file-tag-list nil)
  2071. (setq image-dired-file-comment-list nil)
  2072. (goto-char (point-min))
  2073. (while (search-forward-regexp "^." nil t)
  2074. (end-of-line)
  2075. (setq end (point))
  2076. (beginning-of-line)
  2077. (setq beg (point))
  2078. (unless (search-forward ";" end nil)
  2079. (error "Something is really wrong, check format of database"))
  2080. (setq row-tags (split-string
  2081. (buffer-substring beg end) ";"))
  2082. (setq file (car row-tags))
  2083. (dolist (x (cdr row-tags))
  2084. (if (not (string-match "^comment:\\(.*\\)" x))
  2085. (image-dired-add-to-tag-file-lists x file)
  2086. (image-dired-add-to-file-comment-list file (match-string 1 x)))))))
  2087. ;; Sort tag-file list
  2088. (setq image-dired-tag-file-list
  2089. (sort image-dired-tag-file-list
  2090. (lambda (x y)
  2091. (string< (car x) (car y))))))
  2092. (defun image-dired-hidden-p (file)
  2093. "Return t if image FILE has a \"hidden\" tag."
  2094. (let (hidden)
  2095. (mapc
  2096. (lambda (tag)
  2097. (if (member tag image-dired-gallery-hidden-tags)
  2098. (setq hidden t)))
  2099. (cdr (assoc file image-dired-file-tag-list)))
  2100. hidden))
  2101. (defun image-dired-gallery-generate ()
  2102. "Generate gallery pages.
  2103. First we create a couple of Lisp structures from the database to make
  2104. it easier to generate, then HTML-files are created in
  2105. `image-dired-gallery-dir'."
  2106. (interactive)
  2107. (if (eq 'per-directory image-dired-thumbnail-storage)
  2108. (error "Currently, gallery generation is not supported \
  2109. when using per-directory thumbnail file storage"))
  2110. (image-dired-create-gallery-lists)
  2111. (let ((tags image-dired-tag-file-list)
  2112. (index-file (format "%s/index.html" image-dired-gallery-dir))
  2113. count tag tag-file
  2114. comment file-tags tag-link tag-link-list)
  2115. ;; Make sure gallery root exist
  2116. (if (file-exists-p image-dired-gallery-dir)
  2117. (if (not (file-directory-p image-dired-gallery-dir))
  2118. (error "Variable image-dired-gallery-dir is not a directory"))
  2119. (make-directory image-dired-gallery-dir))
  2120. ;; Open index file
  2121. (with-temp-file index-file
  2122. (if (file-exists-p index-file)
  2123. (insert-file-contents index-file))
  2124. (insert "<html>\n")
  2125. (insert " <body>\n")
  2126. (insert " <h2>Image-Dired Gallery</h2>\n")
  2127. (insert (format "<p>\n Gallery generated %s\n <p>\n"
  2128. (current-time-string)))
  2129. (insert " <h3>Tag index</h3>\n")
  2130. (setq count 1)
  2131. ;; Pre-generate list of all tag links
  2132. (dolist (curr tags)
  2133. (setq tag (car curr))
  2134. (when (not (member tag image-dired-gallery-hidden-tags))
  2135. (setq tag-link (format "<a href=\"%d.html\">%s</a>" count tag))
  2136. (if tag-link-list
  2137. (setq tag-link-list
  2138. (append tag-link-list (list (cons tag tag-link))))
  2139. (setq tag-link-list (list (cons tag tag-link))))
  2140. (setq count (1+ count))))
  2141. (setq count 1)
  2142. ;; Main loop where we generated thumbnail pages per tag
  2143. (dolist (curr tags)
  2144. (setq tag (car curr))
  2145. ;; Don't display hidden tags
  2146. (when (not (member tag image-dired-gallery-hidden-tags))
  2147. ;; Insert link to tag page in index
  2148. (insert (format " %s<br>\n" (cdr (assoc tag tag-link-list))))
  2149. ;; Open per-tag file
  2150. (setq tag-file (format "%s/%s.html" image-dired-gallery-dir count))
  2151. (with-temp-file tag-file
  2152. (if (file-exists-p tag-file)
  2153. (insert-file-contents tag-file))
  2154. (erase-buffer)
  2155. (insert "<html>\n")
  2156. (insert " <body>\n")
  2157. (insert " <p><a href=\"index.html\">Index</a></p>\n")
  2158. (insert (format " <h2>Images with tag &quot;%s&quot;</h2>" tag))
  2159. ;; Main loop for files per tag page
  2160. (dolist (file (cdr curr))
  2161. (unless (image-dired-hidden-p file)
  2162. ;; Insert thumbnail with link to full image
  2163. (insert
  2164. (format "<a href=\"%s/%s\"><img src=\"%s/%s\"%s></a>\n"
  2165. image-dired-gallery-image-root-url
  2166. (file-name-nondirectory file)
  2167. image-dired-gallery-thumb-image-root-url
  2168. (file-name-nondirectory (image-dired-thumb-name file)) file))
  2169. ;; Insert comment, if any
  2170. (if (setq comment (cdr (assoc file image-dired-file-comment-list)))
  2171. (insert (format "<br>\n%s<br>\n" comment))
  2172. (insert "<br>\n"))
  2173. ;; Insert links to other tags, if any
  2174. (when (> (length
  2175. (setq file-tags (assoc file image-dired-file-tag-list))) 2)
  2176. (insert "[ ")
  2177. (dolist (extra-tag file-tags)
  2178. ;; Only insert if not file name or the main tag
  2179. (if (and (not (equal extra-tag tag))
  2180. (not (equal extra-tag file)))
  2181. (insert
  2182. (format "%s " (cdr (assoc extra-tag tag-link-list))))))
  2183. (insert "]<br>\n"))))
  2184. (insert " <p><a href=\"index.html\">Index</a></p>\n")
  2185. (insert " </body>\n")
  2186. (insert "</html>\n"))
  2187. (setq count (1+ count))))
  2188. (insert " </body>\n")
  2189. (insert "</html>"))))
  2190. (defun image-dired-kill-buffer-and-window ()
  2191. "Kill the current buffer and, if possible, also the window."
  2192. (interactive)
  2193. (let ((buffer (current-buffer)))
  2194. (condition-case nil
  2195. (delete-window (selected-window))
  2196. (error nil))
  2197. (kill-buffer buffer)))
  2198. (defvar image-dired-widget-list nil
  2199. "List to keep track of meta data in edit buffer.")
  2200. ;;;###autoload
  2201. (defun image-dired-dired-edit-comment-and-tags ()
  2202. "Edit comment and tags of current or marked image files.
  2203. Edit comment and tags for all marked image files in an
  2204. easy-to-use form."
  2205. (interactive)
  2206. (setq image-dired-widget-list nil)
  2207. ;; Setup buffer.
  2208. (let ((files (dired-get-marked-files)))
  2209. (switch-to-buffer "*Image-Dired Edit Meta Data*")
  2210. (kill-all-local-variables)
  2211. (make-local-variable 'widget-example-repeat)
  2212. (let ((inhibit-read-only t))
  2213. (erase-buffer))
  2214. (remove-overlays)
  2215. ;; Some help for the user.
  2216. (widget-insert
  2217. "\nEdit comments and tags for each image. Separate multiple tags
  2218. with a comma. Move forward between fields using TAB or RET.
  2219. Move to the previous field using backtab (S-TAB). Save by
  2220. activating the Save button at the bottom of the form or cancel
  2221. the operation by activating the Cancel button.\n\n")
  2222. ;; Here comes all images and a comment and tag field for each
  2223. ;; image.
  2224. (let (thumb-file img comment-widget tag-widget)
  2225. (dolist (file files)
  2226. (setq thumb-file (image-dired-thumb-name file)
  2227. img (create-image thumb-file))
  2228. (insert-image img)
  2229. (widget-insert "\n\nComment: ")
  2230. (setq comment-widget
  2231. (widget-create 'editable-field
  2232. :size 60
  2233. :format "%v "
  2234. :value (or (image-dired-get-comment file) "")))
  2235. (widget-insert "\nTags: ")
  2236. (setq tag-widget
  2237. (widget-create 'editable-field
  2238. :size 60
  2239. :format "%v "
  2240. :value (or (mapconcat
  2241. (lambda (tag)
  2242. tag)
  2243. (image-dired-list-tags file)
  2244. ",") "")))
  2245. ;; Save information in all widgets so that we can use it when
  2246. ;; the user saves the form.
  2247. (setq image-dired-widget-list
  2248. (append image-dired-widget-list
  2249. (list (list file comment-widget tag-widget))))
  2250. (widget-insert "\n\n")))
  2251. ;; Footer with Save and Cancel button.
  2252. (widget-insert "\n")
  2253. (widget-create 'push-button
  2254. :notify
  2255. (lambda (&rest _ignore)
  2256. (image-dired-save-information-from-widgets)
  2257. (bury-buffer)
  2258. (message "Done."))
  2259. "Save")
  2260. (widget-insert " ")
  2261. (widget-create 'push-button
  2262. :notify
  2263. (lambda (&rest _ignore)
  2264. (bury-buffer)
  2265. (message "Operation canceled."))
  2266. "Cancel")
  2267. (widget-insert "\n")
  2268. (use-local-map widget-keymap)
  2269. (widget-setup)
  2270. ;; Jump to the first widget.
  2271. (widget-forward 1)))
  2272. (defun image-dired-save-information-from-widgets ()
  2273. "Save information found in `image-dired-widget-list'.
  2274. Use the information in `image-dired-widget-list' to save comments and
  2275. tags to their respective image file. Internal function used by
  2276. `image-dired-dired-edit-comment-and-tags'."
  2277. (let (file comment tag-string tag-list lst)
  2278. (image-dired-write-comments
  2279. (mapcar
  2280. (lambda (widget)
  2281. (setq file (car widget)
  2282. comment (widget-value (cadr widget)))
  2283. (cons file comment))
  2284. image-dired-widget-list))
  2285. (image-dired-write-tags
  2286. (dolist (widget image-dired-widget-list lst)
  2287. (setq file (car widget)
  2288. tag-string (widget-value (car (cddr widget)))
  2289. tag-list (split-string tag-string ","))
  2290. (dolist (tag tag-list)
  2291. (push (cons file tag) lst))))))
  2292. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2293. ;;;;;;;;; TEST-SECTION ;;;;;;;;;;;
  2294. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2295. ;; (defvar image-dired-dir-max-size 12300000)
  2296. ;; (defun image-dired-test-clean-old-files ()
  2297. ;; "Clean `image-dired-dir' from old thumbnail files.
  2298. ;; \"Oldness\" measured using last access time. If the total size of all
  2299. ;; thumbnail files in `image-dired-dir' is larger than 'image-dired-dir-max-size',
  2300. ;; old files are deleted until the max size is reached."
  2301. ;; (let* ((files
  2302. ;; (sort
  2303. ;; (mapcar
  2304. ;; (lambda (f)
  2305. ;; (let ((fattribs (file-attributes f)))
  2306. ;; ;; Get last access time and file size
  2307. ;; `(,(nth 4 fattribs) ,(nth 7 fattribs) ,f)))
  2308. ;; (directory-files (image-dired-dir) t ".+\.thumb\..+$"))
  2309. ;; ;; Sort function. Compare time between two files.
  2310. ;; (lambda (l1 l2)
  2311. ;; (time-less-p (car l1) (car l2)))))
  2312. ;; (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files))))
  2313. ;; (while (> dirsize image-dired-dir-max-size)
  2314. ;; (y-or-n-p
  2315. ;; (format "Size of thumbnail directory: %d, delete old file %s? "
  2316. ;; dirsize (cadr (cdar files))))
  2317. ;; (delete-file (cadr (cdar files)))
  2318. ;; (setq dirsize (- dirsize (car (cdar files))))
  2319. ;; (setq files (cdr files)))))
  2320. ;;;;;;;;;;;;;;;;;;;;;;,
  2321. ;; (defun dired-speedbar-buttons (dired-buffer)
  2322. ;; (when (and (boundp 'image-dired-use-speedbar)
  2323. ;; image-dired-use-speedbar)
  2324. ;; (let ((filename (with-current-buffer dired-buffer
  2325. ;; (dired-get-filename))))
  2326. ;; (when (and (not (string-equal filename (buffer-string)))
  2327. ;; (string-match (image-file-name-regexp) filename))
  2328. ;; (erase-buffer)
  2329. ;; (insert (propertize
  2330. ;; filename
  2331. ;; 'display
  2332. ;; (image-dired-get-thumbnail-image filename)))))))
  2333. ;; (setq image-dired-use-speedbar t)
  2334. (provide 'image-dired)
  2335. ;;; image-dired.el ends here