{"id":63,"date":"2011-05-17T01:48:38","date_gmt":"2011-05-17T01:48:38","guid":{"rendered":"http:\/\/blogs.gentoo.org\/titanofold\/?p=63"},"modified":"2011-05-17T01:48:38","modified_gmt":"2011-05-17T01:48:38","slug":"postgresql-syntax-highlighting-in-emacs","status":"publish","type":"post","link":"https:\/\/blogs.gentoo.org\/titanofold\/2011\/05\/17\/postgresql-syntax-highlighting-in-emacs\/","title":{"rendered":"PostgreSQL Syntax Highlighting in Emacs"},"content":{"rendered":"<p>So, a little while ago I discovered sql-mode and syntax highlighting, plus a little, for PostgreSQL SQL scripts.<!--more--><\/p>\n<p>I know, you&#8217;re asking, &#8220;But, Aaron, how did you not discover this a long time ago?!&#8221; Simply put, I usually do everything I need to do in the <code>psql<\/code> shell. I&#8217;ve never really needed to write SQL scripts or edit them.<\/p>\n<p>Then, shadowdaemon was pipping his squeak about having gotten psql-mode working in Emacs. That got me to take a look at what modes are available for PostgreSQL and Emacs. (app-emacs\/psql is now masked as it has been abandoned by upstream a really long time ago, and the package required dev-db\/postgresql-server which was incorrect.)<\/p>\n<p>So, I just opened an SQL script that came along with rsyslog and Emacs set itself to sql-mode, which is included as a part of the core install. Not quite the proper mode for a PostgreSQL script, but close. There is a product selection, though, so with that one setting I had proper syntax highlighting going and I could connect straight to the database within Emacs. Pretty nifty if I do say so myself, which I do.<\/p>\n<p>Now for some interesting bits. I&#8217;m obviously quite involved with PostgreSQL and I&#8217;d want my Emacs to default to PostgreSQL syntax highlighting, so I&#8217;ve added the following to my <code>~\/.emacs<\/code>:<\/p>\n<pre style=\"font-size: 8pt\">(eval-after-load \"sql\"\r\n  '(progn\r\n    (sql-set-product 'postgres)\r\n    )\r\n  )<\/pre>\n<p>This works out pretty nicely as now Emacs defaults to using the proper syntax scheme. I wanted to take this a step further and have <code>psql<\/code> do the right thing also, but I use the server\/client mode with Emacs so I don&#8217;t have a ton of windows open at any given time. Also, when <code>emacsclient<\/code> is invoked by <code>psql<\/code>, I want it to set the proper mode, so here comes some trickery.<\/p>\n<p>In my <code>~\/.bashrc<\/code> I added:<\/p>\n<pre style=\"font-size: 8pt\">PSQL_EDITOR=\"emacsclient --eval \\\"(setq-default major-mode 'sql-mode)\\\"; emacsclient\"<\/pre>\n<p>When <code>psql<\/code> is given <code>\\e<\/code>, it&#8217;ll open a connection to my Emacs server with the sql-mode loaded, and my <code>~\/.emacs<\/code> sets the product and all is right with the world.<\/p>\n<p>So, <em>vim<\/em>, what can you do? Seriously. I&#8217;d like to resolve an easy <a title=\"dev-db\/postgresql-server - Add a vim-syntax module\" href=\"https:\/\/bugs.gentoo.org\/show_bug.cgi?id=228035\">bug<\/a> (bugs.gentoo.org) before I die.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, a little while ago I discovered sql-mode and syntax highlighting, plus a little, for PostgreSQL SQL scripts.<\/p>\n","protected":false},"author":136,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[3,4],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1tO5a-11","_links":{"self":[{"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/posts\/63"}],"collection":[{"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/users\/136"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":11,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/posts\/63\/revisions\/92"}],"wp:attachment":[{"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}