{"id":11884,"date":"2024-01-04T11:00:01","date_gmt":"2024-01-04T16:00:01","guid":{"rendered":"https:\/\/irreal.org\/blog\/?p=11884"},"modified":"2024-01-04T11:00:01","modified_gmt":"2024-01-04T16:00:01","slug":"extracting-text-from-an-emacs-buffer","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=11884","title":{"rendered":"Extracting Text From An Emacs Buffer"},"content":{"rendered":"<p>\nRajasegar Chandran has an <a href=\"https:\/\/dev.to\/rajasegar\/5-ways-to-get-text-from-an-emacs-buffer-4c3l\">interesting and useful post<\/a> on how to extract text from an Emacs buffer. You wouldn&rsquo;t think there&rsquo;s a lot to know but there are at least two wrinkles. First you might not want the whole buffer and second you (probably) don&rsquo;t want to include the text properties.\n<\/p>\n<p>\nText properties are things like font faces that allow Emacs to display text as &ldquo;rich text&rdquo;. They&rsquo;re really useful but if you want to work on just the underlying text, they can get in the way so, of course, Emacs gives you a way to omit them.\n<\/p>\n<p>\nTo deal with all this there are four functions that cover most of what you&rsquo;d want to do.\n<\/p>\n<dl class=\"org-dl\">\n<dt><code>buffer-substring<\/code><\/dt>\n<dd>Extract a substring including its text properties from the buffer.<\/dd>\n<dt><code>buffer-substring-no-properties<\/code><\/dt>\n<dd>Like <code>buffer-substring<\/code> but strips the text properties.<\/dd>\n<dt><code>buffer-string<\/code><\/dt>\n<dd>Extract the entire buffer as a string. The text properties are included.<\/dd>\n<dt><code>thing-at-point<\/code><\/dt>\n<dd>Return the object that the point is on.<\/dd>\n<\/dl>\n<p>\nMost of these are self explanatory but <code>thing-at-point<\/code> is a bit richer. You can specify the type of object you want to extract such as a symbol, list, url, filename, email address, or many other object types. It&rsquo;s sister function <code>bounds-of-thing-at-point<\/code> returns the start and end of the object. It&rsquo;s really useful if you want to extract, say, a URL from the buffer.\n<\/p>\n<p>\nThese function are more useful than you might think. My <code>init.el<\/code> has several instances of <code>buffer-substring-no-properties<\/code> and <code>thing-at-point<\/code> just for my personal configuration. If you&rsquo;re not familiar with them, take a look at Chandran&rsquo;s post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rajasegar Chandran has an interesting and useful post on how to extract text from an Emacs buffer. You wouldn&rsquo;t think there&rsquo;s a lot to know but there are at least two wrinkles. First you might not want the whole buffer &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=11884\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[18],"class_list":["post-11884","post","type-post","status-publish","format-standard","hentry","category-general","tag-emacs"],"_links":{"self":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/11884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11884"}],"version-history":[{"count":1,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/11884\/revisions"}],"predecessor-version":[{"id":11885,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/11884\/revisions\/11885"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}