1

In – and in many others I believe – indentation of an expression spreading over several lines is computed relative to the starting column of the expression rather than the column of the first non-blank character of the line where the expression starts.

This is displayed by the following snippet, where the return keyword is indented four columns counting from the a starting the array_map(…) expression:

<?php

function weights($volumes, $density)
{
    $weights = array_map(function ($volume) {
                   return $volume * $density;
               }, $volumes);
    return $weights;
}

I must follow another indentation style, where the return keyword is indented four columns counting from the $ of $weights which is the first non-blank character of the line where the array_map expression starts.

<?php

function weights($volumes, $density)
{
    $weights = array_map(function ($volume) {
        return $volume * $density;
    }, $volumes);
    return $weights;
}

How can I configure Emacs and the to use this second style? I could not find any reference to this behaviour on the emacs-wiki.

The indentation settings in use are the following:

(c-add-style "my-php-style"
             '("gnu"
               (c-basic-offset . 4)
               (c-offsets-alist
                (arglist-cont . 0)
                (arglist-intro . +)
                (block-close . 0)
                (defun-block-intro . +)
                (defun-close . 0)
                (defun-open . 0)
                (statement . 0)
                (statement-block-intro . +)
                (topmost-intro . 0)
                (access-label . -)
                (annotation-top-cont . 0)
                (annotation-var-cont . +)
                (arglist-close . c-lineup-arglist-operators)
                (arglist-cont-nonempty . c-lineup-math)
                (block-open . 0)
                (brace-entry-open . 0)
                (brace-list-close . 0)
                (brace-list-entry . 0)
                (brace-list-intro . +)
                (brace-list-open . +)
                (c . c-lineup-C-comments)
                (case-label . +)
                (catch-clause . 0)
                (class-close . 0)
                (class-open . 0)
                (comment-intro . c-lineup-comment)
                (composition-close . 0)
                (composition-open . 0)
                (cpp-define-intro c-lineup-cpp-define +)
                (cpp-macro . -1000)
                (cpp-macro-cont . +)
                (do-while-closure . 0)
                (else-clause . 0)
                (extern-lang-close . 0)
                (extern-lang-open . 0)
                (friend . 0)
                (func-decl-cont . +)
                (inclass . +)
                (incomposition . +)
                (inexpr-class . +)
                (inexpr-statement . +)
                (inextern-lang . +)
                (inher-cont . c-lineup-multi-inher)
                (inher-intro . +)
                (inlambda . c-lineup-inexpr-block)
                (inline-close . 0)
                (inline-open . 0)
                (inmodule . +)
                (innamespace . +)
                (knr-argdecl . 0)
                (knr-argdecl-intro . 5)
                (label . 0)
                (lambda-intro-cont . +)
                (member-init-cont . c-lineup-multi-inher)
                (member-init-intro . +)
                (module-close . 0)
                (module-open . 0)
                (namespace-close . 0)
                (namespace-open . 0)
                (objc-method-args-cont . c-lineup-ObjC-method-args)
                (objc-method-call-cont c-lineup-ObjC-method-call-colons c-lineup-ObjC-method-call +)
                (objc-method-intro .
                                   [0])
                (statement-case-intro . +)
                (statement-case-open . +)
                (statement-cont . +)
                (stream-op . c-lineup-streamop)
                (string . -1000)
                (substatement . +)
                (substatement-label . 0)
                (substatement-open . +)
                (template-args-cont c-lineup-template-args +)
                (topmost-intro-cont first c-lineup-topmost-intro-cont c-lineup-gnu-DEFUN-intro-cont))))
  • What does this have to do with `c-mode` ? – politza Nov 24 '16 at 23:01
  • `php-mode` derives from `c-mode` – and uses the same indentation engine – IIUC. – Michaël Le Barbier Nov 25 '16 at 05:09
  • I tried this using Emacs 25.1, Ubuntu 16.04, and `php-mode` version 1.17.0. When I press `TAB` and cursor is on `return` in your first snippet, the line is automatically indented exactly as you wanted in the second snippet. Maybe you are using some special settings that mess things up? For example, what is the value of the variable `c-syntactic-indentation`? – Håkon Hægland Nov 29 '16 at 15:33
  • @HåkonHægland Thank you for your feedback! The `c-syntactic-indentation` is `'t` but if I set it on `'nil` it does not change the automatic indentation. – Michaël Le Barbier Dec 01 '16 at 11:25
  • @MichaelLeBarbierGrünewald Can you possibly tell how I can figure out which mode derives from which? – x-yuri Jul 06 '20 at 12:28
  • I do not remember how I found this, I guess I just took a look at the file. HTH – Michaël Le Barbier Jul 06 '20 at 12:52

2 Answers2

1

Maybe the solution in the section Indentation of arrays of the emacs wiki php-mode page, or a variation of it, is what you are searching for.

Quotation:

(add-hook 'php-mode-hook (lambda ()
  (defun ywb-php-lineup-arglist-intro (langelem)
    (save-excursion
      (goto-char (cdr langelem))
      (vector (+ (current-column) c-basic-offset))))
  (defun ywb-php-lineup-arglist-close (langelem)
    (save-excursion
      (goto-char (cdr langelem))
      (vector (current-column))))
  (c-set-offset 'arglist-intro 'ywb-php-lineup-arglist-intro)
  (c-set-offset 'arglist-close 'ywb-php-lineup-arglist-close)))

(Not sure if defuns in a lambda are good style, though ...)

Vera Johanna
  • 873
  • 6
  • 9
1

Following the steps in this answer over at Stack Overflow I was able to fix this by tweaking the underlying c-mode like so:

(defun my-php-mode-hook ()
  (c-set-offset 'arglist-cont-nonempty '+))

(add-hook 'php-mode-hook 'my-php-mode-hook)
dustym
  • 11
  • 1