{"id":374,"date":"2011-11-29T17:27:25","date_gmt":"2011-11-29T22:27:25","guid":{"rendered":"http:\/\/irreal.org\/blog\/?p=374"},"modified":"2011-11-29T17:27:25","modified_gmt":"2011-11-29T22:27:25","slug":"emacs-markers","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=374","title":{"rendered":"Emacs Markers"},"content":{"rendered":"\n<p>In a <a href=\"http:\/\/irreal.org\/blog\/?p=371#comment-1543\">comment<\/a> to my <a href=\"http:\/\/irreal.org\/blog\/?p=371\">Emacs comment-box Revisited<\/a> post, Aaron showed me a nice way to deal with the boundaries of expanding regions. Recall that in that post I wanted to pad the first line of a comment out to the fill column so that <code>comment-box<\/code> would draw the box across the whole width (more or less) of the window. The problem was that adding those blanks increased the size of the region so the character number of the end of the region was no longer correct. In my code that was easily fixed because I could use <code>(point-max)<\/code> to locate the end of the region since I had narrowed the buffer to the region. <\/p>\n<p> Aaron&#8217;s way of handling this is nice because it feels less ad hoc and because it works for any position in the buffer not just the beginning or end. The idea is to use a marker to point at the end of the region. Conceptually, a marker is just like a position (that is a character index into the buffer) but it has the nice property that if you change the size of the buffer, the marker is adjusted when necessary. It&#8217;s as if you planted a flag at the position and the flag moves as the buffer expands or contracts. <\/p>\n<p> Here&#8217;s what the revised code looks like <\/p>\n<pre class=\"src src-emacs-lisp\">(<span style=\"color: #7f007f;\">defun<\/span> <span style=\"color: #0000ff;\">jcs-comment-box<\/span> (b e)\n  <span style=\"color: #8b2252;\">\"Draw a box comment around the region but arrange for the region<\/span>\n<span style=\"color: #8b2252;\">to extend to at least the fill column. Place the point after the<\/span>\n<span style=\"color: #8b2252;\">comment box.\"<\/span>\n  (interactive <span style=\"color: #8b2252;\">\"r\"<\/span>)\n  (<span style=\"color: #7f007f;\">let<\/span> ((e (copy-marker e t)))\n    (goto-char b)\n    (end-of-line)\n    (insert-char ?  (- fill-column (current-column)))\n    (comment-box b e 1)\n    (goto-char e)\n    (set-marker e nil)))\n<\/pre>\n<p> As you can see, I&#8217;ve replaced the <code>with-region-or-buffer<\/code> macro with the more sensible <code>(interactive \"r\")<\/code>. The <code>copy-marker<\/code> function will make a copy of an existing marker or if its first argument is a position it will make a marker with that offset. The (optional) second argument says that if text is added <i>at the marker<\/i> then move the marker ahead. A <code>nil<\/code> would say the leave the marker where it is. The <code>(set-marker e nil)<\/code> at the end of the function makes the marker point nowhere. It&#8217;s not strictly necessary but it lets Emacs stop worrying about keeping it updated as you further edit the buffer. As you can see, you can treat the marker just you would a position: you can use it as a position, move to it, and even do arithmetic on it. <\/p>\n<p> <a href=\"http:\/\/www.gnu.org\/software\/emacs\/manual\/html_node\/elisp\/Markers.html#Markers\">Chapter 31<\/a> of the Emacs Lisp Manual explains markers further. It&#8217;s short and interesting so it&#8217;s worth a read if you like programming in Emacs Lisp. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a comment to my Emacs comment-box Revisited post, Aaron showed me a nice way to deal with the boundaries of expanding regions. Recall that in that post I wanted to pad the first line of a comment out to &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=374\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[18],"class_list":["post-374","post","type-post","status-publish","format-standard","hentry","category-programming","tag-emacs"],"_links":{"self":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/374","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=374"}],"version-history":[{"count":0,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/374\/revisions"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}