Emacs web-mode tabs not working - emacs

I am using emacs web-mode to edit my php files and have noticed that the tab key doesn't work the way I expect it to in a couple of different situations. For example if I have a string that spans multiple lines like this...
<?php
return "
<nav>
<a href='index.php?page=skills'>My skills and background</a>
<a href='index.php?page=projects'>Some projects</a>
</nav>
";
?>
and I go to the beginning of the lines with anchor tags and press tab nothing happens. I have to use the space bar to indent the lines the way I want.
The other situation is occurs inside of a php block for example
<?php
error_reporting( E_ALL );
ini_set( "display_errors", 1);
$pageData = new stdClass();
$pageData->title = "Jordan: Portfolio site";
$pageData->content = include_once "views/navigation.php";
$navigationIsClicked = isset($_GET['page']);
if($navigationIsClicked) {
$fileToLoad = $_GET['page'];
$pageData->content .= "<p>Will soon load $fileToLoad.php</p>";
}
$page = include_once "templates/page.php";
echo $page;
?>
if I go to one of the variable assignment lines and push tab nothing happens, again spaces work fine, then inside the if block only 1 tab is allowed. Is there any way to change how this mode uses tabs in my .emacs file. I currently have this in my .emacs
(defun my-setup-php ()
;; enable web mode
(web-mode)
;; make these variables local
(make-local-variable 'web-mode-code-indent-offset)
(make-local-variable 'web-mode-markup-indent-offset)
(make-local-variable 'web-mode-css-indent-offset)
;; set indentation, can set different indentation level for different code type
(setq web-mode-code-indent-offset 4)
(setq web-mode-css-indent-offset 2)
(setq web-mode-markup-indent-offset 2))
(add-to-list 'auto-mode-alist '("\\.php$" . my-setup-php))

As it turns out my question is completely unrelated to web-mode. The function that emacs binds to the tab key by default is called indent-for-tab-command the function I was looking for is called tab-to-tab-stop which by default is bound to M - i. This website has a very detailed explanation of how the tab key works in emacs and how to customize it.

You should try (setq-default indent-tabs-mode t)

Related

Tab indentation in CoffeeScript using Emacs

I hope this is within the reasonable scope of this site and not too trivial, this is my first post. I am new to Emacs and am trying to set up the environment so that when I start a new line in coffee-mode the automatic indentation is in the form of tabs. As I understood the documentation of coffee-mode all I need to do is set coffee-indent-tabs-mode to t. I've appended my init file with the code below:
(custom-set-variables
'(coffee-tab-width 2)
'(coffee-indent-tabs-mode t))
However when I start Emacs and open a .coffee file, though it gets the tab width right, when I press enter it indents with spaces. Quibbles about whether I need to indent with tabs aside, what am I doing wrong?
In the coffee-mode I find in GNU ELPA, there is no coffee-indent-tabs-mode. I recommend you simply do:
(add-hook 'coffee-mode-hook
(lambda ()
(set (make-local-variable 'tab-width) 2)
(set (make-local-variable 'indent-tabs-mode) t)))
This should work for pretty much any major mode.

Close HTML tags as soon as one opens them

I'd like the corresponding, closing HTML tag to be automatically inserted whenever I open one.
So if I type
<div>
I should get
<div></div>
Without having to call to sgml-close-tag myself.
How to achieve this?
Rather than calling a hook function after every single key-stroke, it makes sense to only call it after a > was typed. This can be achieved by rebinding the > character in the keymap that sgml-mode uses.
In addition, sgml-close-tag shouldn't get called if the tag is already closed. Therefore, the following code adds a simple regexp check for that:
(defun my-sgml-insert-gt ()
"Inserts a `>' character and calls
`my-sgml-close-tag-if-necessary', leaving point where it is."
(interactive)
(insert ">")
(save-excursion (my-sgml-close-tag-if-necessary)))
(defun my-sgml-close-tag-if-necessary ()
"Calls sgml-close-tag if the tag immediately before point is
an opening tag that is not followed by a matching closing tag."
(when (looking-back "<\\s-*\\([^</> \t\r\n]+\\)[^</>]*>")
(let ((tag (match-string 1)))
(unless (and (not (sgml-unclosed-tag-p tag))
(looking-at (concat "\\s-*<\\s-*/\\s-*" tag "\\s-*>")))
(sgml-close-tag)))))
(eval-after-load "sgml-mode"
'(define-key sgml-mode-map ">" 'my-sgml-insert-gt))
If you like paredit (and if you're an emacs user, chances are you do), you may be interested in tagedit, an emacs package written by Magnar Sveen that provides paredit-like features for editing html.
The library is here: https://github.com/magnars/tagedit, and can be installed through Melpa/Marmalade (package-install tagedit).
If you enable the experimental features (tagedit-add-experimental-features), then it will automatically close tags for you and keep the corresponding closing tag text matching the opening tag text. That's on top of being able to splice, slurp, barf and all the other crazy things that paredit lets you do when working with balanced expressions...I think it's great!
I'm using yasnippet for this purpose.
To type shortcuts this answer, like <kbd>C-o</kbd>, I have the following snippet:
# -*- mode: snippet -*-
# name: kbd
# key: kbd
# --
<kbd>$0</kbd>
So I type kbdC-o and it get's expanded to <kbd></kbd> with cursor
right in the middle. You can have the same behavior for div.
You may eval this on your sgml-buffer or add ii to your sgml-hook:
(add-hook 'post-self-insert-hook
(lambda () (and (eq (char-before) ?>) (sgml-close-tag))) nil t)
Whenever you insert a ">", the function sgml-close-tag will be run for you

Sync Emacs AUCTeX with Sumatra PDF

With these lines in my init.el I am able to sync the Emacs LaTeX buffer with Sumatra:
(setq TeX-source-correlate-mode t)
(setq TeX-source-correlate-method 'synctex)
(setq TeX-view-program-list
'(("Sumatra PDF" ("\"C:/bin86/SumatraPDF/SumatraPDF.exe\" -reuse-instance"
(mode-io-correlate " -forward-search %b %n ") " %o"))))
(setq TeX-view-program-selection
'(((output-dvi style-pstricks) "dvips and start") (output-dvi "Yap")
(output-pdf "Sumatra PDF") (output-html "start")))
To set a double click on the PDF to get me to the related LaTeX code, I also set in Sumatra options Set inverse search command line to:
"c:\bin86\GNU Emacs 24.2\bin\emacsclient.exe" --no-wait +%l "%f"
Despite the sync works, I’d like to code it differently.
If I didn’t set the last expression, (setq TeX-view-program-selection..., I would get the default values, which are the same as above, apart from the value for the PDF output that would be: (output-pdf "start").
I’d like to change this one to "Sumatra PDF" and leave the other values to their default, that is, I’d like to ask Emacs the default values for the viewers and change only the PDF value.
It is mostly an ELisp question concerning the manipulation of the variable TeX-view-program-selection.
Thanks for helping.
P.S. Please tell me if this question is best fit on tex.stackexchange
Update based on lunaryorn comments/answer
To update TeX-view-program-selection I could use:
(assq-delete-all 'output-pdf TeX-view-program-selection)
(add-to-list 'TeX-view-program-selection '(output-pdf "Sumatra PDF"))
The first line is optional, but it makes the list look "cleaner".
In both cases (with or without assq-delete-all) I now need to insert the code in the proper hook, since TeX-view-program-selection is void in init.el.
My credits to lunaryorn for suggestions! I am repackaging the steps involved to the benefits of the others.
Emacs side
Add to your init.el:
(setq TeX-PDF-mode t)
(setq TeX-source-correlate-mode t)
(setq TeX-source-correlate-method 'synctex)
(setq TeX-view-program-list
'(("Sumatra PDF" ("\"path/to/SumatraPDF/SumatraPDF.exe\" -reuse-instance"
(mode-io-correlate " -forward-search %b %n ") " %o"))))
(eval-after-load 'tex
'(progn
(assq-delete-all 'output-pdf TeX-view-program-selection)
(add-to-list 'TeX-view-program-selection '(output-pdf "Sumatra PDF"))))
Sumatra side
In Settings->Options dialog set Set inverse search command line to:
"path\to\GNU Emacs ver\bin\emacsclient.exe" --no-wait +%l "%f"
How to use
In your LaTeX document in Emacs type C-c C-v or double click the related PDF in Sumatra and ... enjoy))
Use add-to-list instead of setq. See C-h f add-to-list for more information.
TeX-view-program-selection is defined in tex.el, so you'll need to execute this code after this library is loaded:
(eval-after-load 'tex
'(progn
(assq-delete-all 'output-pdf TeX-view-program-selection)
(add-to-list 'TeX-view-program-selection '(output-pdf "Sumatra PDF"))))
I was driven nearly mad by the quoting conventions for windows ->emacs
If you install SumatraPDF in the default place, then this works on windows 8.1
(setq TeX-view-program-list
'(("Sumatra PDF" ("\"C:/Program Files (x86)/SumatraPDF/SumatraPDF.exe\" -reuse-instance"
(mode-io-correlate " -forward-search %b %n ") " %o")))))
That's an hour or so I'll never get back :)
I have prepared a batch file, Emacs_SumatraPDF.bat.
It works perfect with sumatra-forward.el to realize forward and backward search.
For example:
Download
http://www.ai.soc.i.kyoto-u.ac.jp/~shi/files/Emacs_SumatraPDF.bat
http:/william.famille-blum.org/software/sumatra/sumatra-forward.el
Prepare
put Emacs_SumatraPDF.bat and SumatraPDF.exe -->
%Emacs_Home%/bin.
put sumatra-forward.el --> %Emacs_Home%/site-lisp
Usage
;Emacs_Home=C:/Program Files (x86)/GNU Emacs 23.4
;~\.emacs add following information
;;; Emacs_SumatraPDF.bat
;; #info: pdf viewer
; #refer:
(setq TeX-view-program-list '(
("Sumatra" "C:/Program Files (x86)/GNU Emacs 23.4/bin/Emacs_SumatraPDF.bat
%o %t %n") ))
(setq TeX-view-program-selection '(
(output-pdf "Sumatra")
(output-dvi "Yap")
((output-dvi style-pstricks) "dvips and start")
(output-html "start")))
;;; sumatra-forward.el
;; #info: forward search.
; #refer: http:/william.famille-blum.org/blog/static.php?page=static081010-000413
(require 'sumatra-forward)
That is all.
Besides
Emacs 4 Latex Support under Windows 7 for newcomer for Emacs.
http://chunqishi.github.io/emacs4ls/
It's worth mentioning that Set inverse search command line is invisible in the Settings->Options dialog by default. To show this option field, go to Settings->Advanced options or open SumatraPDF-settings.txt and turn on EnableTeXEnhancements = true.

how to put elscreen tabs on the top only?

I'm using elscreen in my GNU Emacs 24.2.1
Currently, when i split my window, I have a same tab panel in each half:
elscreen http://i.zlowiki.ru/121101_0f30ebba.png/800
Note that the two lower windows have the same tabs as the largest one.
How can I remove these two duplicates, and keep only the top one?
If it is too hard, what another alternative could be used for GNU screen?
Here's something to technically do what you asked:
(setq elscreen-display-tab nil) ; disable tabs display
;; get-alist was removed somewhere along the line
;; You can try substituting all instances of get-alist with assoc-default
;; instead of using defalias and see if that works; I haven't tried.
(defalias 'get-alist 'assoc-default) ; get-alist is gone
;; Put tabs display in your frame title bar instead.
(defun elscreen-frame-title-update ()
(when (elscreen-screen-modified-p 'elscreen-frame-title-update)
(let* ((screen-list (sort (elscreen-get-screen-list) '<))
(screen-to-name-alist (elscreen-get-screen-to-name-alist))
(title (concat "| " (mapconcat
(lambda (screen)
(format "%d%s %s |"
screen (elscreen-status-label screen)
(get-alist screen screen-to-name-alist)))
screen-list " "))))
(if (fboundp 'set-frame-name)
(set-frame-name title)
(setq frame-title-format title)))))
(eval-after-load "elscreen"
'(add-hook 'elscreen-screen-update-hook 'elscreen-frame-title-update))
I'm sure it's not what you had in mind, but hey, it's at the VERY top now and only at the top.
Take a look at http://www.emacswiki.org/emacs/ElscreenSeparateBufferLists modifies the operation of elscreen slightly borrowing from escreen. It allows the list of tabs to differ on each buffer.
How about using elscreen-tab plugin?
This plugins is created to resolve what you are annoyed with.
You can install via melpa.

Can't change Emacs's default indentation between HTML tags

I'm confused about the Emacs indentation paradigm.
I have this in my .emacs file:
(setq-default tab-width 4)
If I press TAB in the following situation
<ul>
(caret)
</ul>
it end up like this
<ul>
(caret)
</ul>
(with 2 spaces indentation between the HTML tags.)
It should end up like this:
<ul>
(caret)
</ul>
I tried everything:
(setq-default tab-width 4)
(setq-default indent-tabs-mode t)
(setq tab-stop-list '(4 8 12 16))
I've set every possible Emacs setting about indentation to 4 but that 2 space indentation is still there.
Any suggestions?
Setting the tab width isn't applicable in this scenario, but I understand your confusion; Emacs provides several tab-related variables and determining the correct one for a particular scenario can be confusing.
This EmacsWiki article provides more details about setting the indentation level for HTML; in general, EmacsWiki is a great resource for Emacs tips.
In this specific case, since you're using Emacs' standard HTML mode (html-mode, as defined by sgml-mode), the variable that you want to set is sgml-basic-offset. That variable defaults to 2, but you can change it to 4 as follows:
(setq sgml-basic-offset 4)
To make this change specific only to html-mode, you can use the following code:
(add-hook 'html-mode-hook
(lambda ()
;; Default indentation is usually 2 spaces, changing to 4.
(set (make-local-variable 'sgml-basic-offset) 4)))
This all assumes that you're using Emacs 22 or later; if that's not the case, the EmacsWiki page that I linked to contains a workaround for earlier versions of Emacs.